Moved Item related stuff to the end of MapEntity.UpdateAll to avoid issues

Added a threadlock to avoid some issue(someday i will take care of)
Add a function that automatically log server performence every 60s
This commit is contained in:
NotAlwaysTrue
2025-12-20 00:08:42 +08:00
parent d0a5d13a0e
commit 2f845b40ca
3 changed files with 57 additions and 42 deletions
@@ -8,6 +8,7 @@ using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Barotrauma namespace Barotrauma
{ {
public class PerformenceMonitor public class PerformenceMonitor
@@ -129,6 +130,9 @@ namespace Barotrauma
TickRateLow = 60; TickRateLow = 60;
TickRateHigh = 60; TickRateHigh = 60;
tickrate60stimer = TotalTimeElapsed; tickrate60stimer = TotalTimeElapsed;
#if !DEBUG
GameServer.Log(PM.ToString(), ServerLog.MessageType.ServerMessage);
#endif
} }
if (RealTickRate > TickRateHigh) if (RealTickRate > TickRateHigh)
{ {
@@ -147,7 +151,12 @@ namespace Barotrauma
} }
override public string ToString() override public string ToString()
{ {
return $"Item Count: {ItemCount}\n" + return
#if !DEBUG
$"[{DateTime.Now}]\n" +
$"Server Performence Info \n" +
#endif
$"Item Count: {ItemCount}\n" +
$"Character Count: {CharacterCount}\n" + $"Character Count: {CharacterCount}\n" +
$"PhysicsBody Count: {PhysicsBodyCount}\n" + $"PhysicsBody Count: {PhysicsBodyCount}\n" +
$"Tick Rate: {RealTickRate}\n" + $"Tick Rate: {RealTickRate}\n" +
@@ -156,8 +165,8 @@ namespace Barotrauma
$"Total Ticks: {TotalTicks}\n" + $"Total Ticks: {TotalTicks}\n" +
$"All time Average Tick Rate: {AverageTickRate}\n" + $"All time Average Tick Rate: {AverageTickRate}\n" +
$"10s Average Tick Rate: {AverageTickRate10s}\n" + $"10s Average Tick Rate: {AverageTickRate10s}\n" +
$"TotalTimeElapsed: {TotalTimeElapsed}\n" + $"Total Time Elapsed: {TotalTimeElapsed}\n" +
$"Memory Usage: {MemoryUsage}"; $"Memory Usage: {MemoryUsage}\n";
} }
} }
@@ -503,6 +503,7 @@ namespace Barotrauma
/// </summary> /// </summary>
public float GetResistance(AfflictionPrefab afflictionPrefab, LimbType limbType) public float GetResistance(AfflictionPrefab afflictionPrefab, LimbType limbType)
{ {
lock (afflictions) {
// This is a % resistance (0 to 1.0) // This is a % resistance (0 to 1.0)
float resistance = 0.0f; float resistance = 0.0f;
foreach (KeyValuePair<Affliction, LimbHealth> kvp in afflictions) foreach (KeyValuePair<Affliction, LimbHealth> kvp in afflictions)
@@ -515,6 +516,7 @@ namespace Barotrauma
// The returned value is calculated to be a % resistance again // The returned value is calculated to be a % resistance again
return 1 - ((1 - resistance) * abilityResistanceMultiplier); return 1 - ((1 - resistance) * abilityResistanceMultiplier);
} }
}
public float GetStatValue(StatTypes statType) public float GetStatValue(StatTypes statType)
{ {
@@ -693,8 +693,32 @@ namespace Barotrauma
GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Misc", sw.ElapsedTicks); GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Misc", sw.ElapsedTicks);
sw.Restart(); sw.Restart();
#endif #endif
Task PItemTask = Task.Factory.StartNew(() =>
{
foreach (var item in GameMain.LuaCs.Game.UpdatePriorityItems)
{
if (item.Removed) continue;
item.Update(deltaTime, cam);
}
});
Task SpawnerTask = Task.Factory.StartNew(() =>
{
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
{
UpdateAllProjSpecific(deltaTime * MapEntityUpdateInterval);
Spawner?.Update();
}
});
Task.WaitAll(SpawnerTask, PItemTask, GapTask, StructuralTask);
Item.UpdatePendingConditionUpdates(deltaTime); Item.UpdatePendingConditionUpdates(deltaTime);
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0) if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
{
lock(Item.ItemList)
{ {
Item lastUpdatedItem = null; Item lastUpdatedItem = null;
@@ -716,32 +740,12 @@ namespace Barotrauma
throw new InvalidOperationException($"Error while updating item {lastUpdatedItem?.Name ?? "null"}", innerException: e); throw new InvalidOperationException($"Error while updating item {lastUpdatedItem?.Name ?? "null"}", innerException: e);
} }
} }
Task PItemTask = Task.Factory.StartNew(() =>
{
foreach (var item in GameMain.LuaCs.Game.UpdatePriorityItems)
{
if (item.Removed) continue;
item.Update(deltaTime, cam);
} }
});
#if CLIENT #if CLIENT
sw.Stop(); sw.Stop();
GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Items", sw.ElapsedTicks); GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Items", sw.ElapsedTicks);
sw.Restart(); sw.Restart();
#endif #endif
Task SpawnerTask = Task.Factory.StartNew(() =>
{
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
{
UpdateAllProjSpecific(deltaTime * MapEntityUpdateInterval);
Spawner?.Update();
}
});
Task.WaitAll(SpawnerTask, PItemTask, GapTask, StructuralTask);
} }
static partial void UpdateAllProjSpecific(float deltaTime); static partial void UpdateAllProjSpecific(float deltaTime);