Added a Performence Monitor for debug

Many multi-threading work in ServerSource
This commit is contained in:
NotAlwaysTrue
2025-12-19 13:43:12 +08:00
parent c38d519ee6
commit fff157d5ca
5 changed files with 245 additions and 41 deletions
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Barotrauma
@@ -647,35 +648,40 @@ namespace Barotrauma
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
#endif
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
Task StructuralTask = Task.Factory.StartNew(() =>
{
foreach (Hull hull in Hull.HullList)
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
{
hull.Update(deltaTime * MapEntityUpdateInterval, cam);
}
foreach (Hull hull in Hull.HullList)
{
hull.Update(deltaTime * MapEntityUpdateInterval, cam);
}
#if CLIENT
Hull.UpdateCheats(deltaTime * MapEntityUpdateInterval, cam);
Hull.UpdateCheats(deltaTime * MapEntityUpdateInterval, cam);
#endif
foreach (Structure structure in Structure.WallList)
{
structure.Update(deltaTime * MapEntityUpdateInterval, cam);
foreach (Structure structure in Structure.WallList)
{
structure.Update(deltaTime * MapEntityUpdateInterval, cam);
}
}
}
});
foreach (Gap gap in Gap.GapList)
Task GapTask = Task.Factory.StartNew(() =>
{
gap.ResetWaterFlowThisFrame();
}
//update gaps in random order, because otherwise in rooms with multiple gaps
//the water/air will always tend to flow through the first gap in the list,
//which may lead to weird behavior like water draining down only through
//one gap in a room even if there are several
foreach (Gap gap in Gap.GapList.OrderBy(g => Rand.Int(int.MaxValue)))
{
gap.Update(deltaTime, cam);
}
foreach (Gap gap in Gap.GapList)
{
gap.ResetWaterFlowThisFrame();
}
//update gaps in random order, because otherwise in rooms with multiple gaps
//the water/air will always tend to flow through the first gap in the list,
//which may lead to weird behavior like water draining down only through
//one gap in a room even if there are several
foreach (Gap gap in Gap.GapList.OrderBy(g => Rand.Int(int.MaxValue)))
{
gap.Update(deltaTime, cam);
}
});
if (mapEntityUpdateTick % PoweredUpdateInterval == 0)
{
@@ -687,7 +693,6 @@ namespace Barotrauma
GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Misc", sw.ElapsedTicks);
sw.Restart();
#endif
Item.UpdatePendingConditionUpdates(deltaTime);
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
{
@@ -705,32 +710,38 @@ namespace Barotrauma
catch (InvalidOperationException e)
{
GameAnalyticsManager.AddErrorEventOnce(
"MapEntity.UpdateAll:ItemUpdateInvalidOperation",
GameAnalyticsManager.ErrorSeverity.Critical,
"MapEntity.UpdateAll:ItemUpdateInvalidOperation",
GameAnalyticsManager.ErrorSeverity.Critical,
$"Error while updating item {lastUpdatedItem?.Name ?? "null"}: {e.Message}");
throw new InvalidOperationException($"Error while updating item {lastUpdatedItem?.Name ?? "null"}", innerException: e);
}
}
foreach (var item in GameMain.LuaCs.Game.UpdatePriorityItems)
Task PItemTask = Task.Factory.StartNew(() =>
{
if (item.Removed) continue;
foreach (var item in GameMain.LuaCs.Game.UpdatePriorityItems)
{
if (item.Removed) continue;
item.Update(deltaTime, cam);
}
item.Update(deltaTime, cam);
}
});
#if CLIENT
sw.Stop();
GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Items", sw.ElapsedTicks);
sw.Restart();
#endif
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
Task SpawnerTask = Task.Factory.StartNew(() =>
{
UpdateAllProjSpecific(deltaTime * MapEntityUpdateInterval);
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
{
UpdateAllProjSpecific(deltaTime * MapEntityUpdateInterval);
Spawner?.Update();
}
Spawner?.Update();
}
});
Task.WaitAll(SpawnerTask, PItemTask, GapTask, StructuralTask);
}
static partial void UpdateAllProjSpecific(float deltaTime);
@@ -174,7 +174,9 @@ namespace Barotrauma.Networking
exceptionMsg += " Child process has not exited.";
}
#endif
#if !DEBUG
throw new Exception(exceptionMsg);
#endif
}
}
@@ -4,6 +4,8 @@ using Microsoft.Xna.Framework;
using System.Threading;
using FarseerPhysics.Dynamics;
using FarseerPhysics;
using System.Threading.Tasks;
using System.Linq;
#if DEBUG && CLIENT
@@ -69,6 +71,7 @@ namespace Barotrauma
MapEntity.ClearHighlightedEntities();
Task PhysicsTask = Task.Factory.StartNew(() => ExecutePhysics());
#if RUN_PHYSICS_IN_SEPARATE_THREAD
var physicsThread = new Thread(ExecutePhysics)
{
@@ -138,6 +141,7 @@ namespace Barotrauma
GameTime += deltaTime;
//Physics Update; wait for changes.
foreach (PhysicsBody body in PhysicsBody.List)
{
//update character (colliders) regardless if they're enabled or not, so that the draw position is updated
@@ -146,15 +150,20 @@ namespace Barotrauma
body.BodyType != BodyType.Static)
{
body.Update();
}
}
if (body.Enabled && body.BodyType != FarseerPhysics.BodyType.Static)
{
body.SetPrevTransform(body.SimPosition, body.Rotation);
}
}
MapEntity.ClearHighlightedEntities();
#if CLIENT
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
#endif
//This is not changed yet. Will be back for it.
GameMain.GameSession?.Update((float)deltaTime);
#if CLIENT
@@ -195,8 +204,14 @@ namespace Barotrauma
Character.UpdateAll((float)deltaTime, cam);
#elif SERVER
if (Level.Loaded != null) Level.Loaded.Update((float)deltaTime, Camera.Instance);
Character.UpdateAll((float)deltaTime, Camera.Instance);
Task LevelTask = Task.Factory.StartNew(() =>
{
if (Level.Loaded != null)
{
Level.Loaded.Update((float)deltaTime, Camera.Instance);
}
});
Task CharacterTask = Task.Factory.StartNew(() => Character.UpdateAll((float)deltaTime, Camera.Instance));
#endif
@@ -206,7 +221,7 @@ namespace Barotrauma
sw.Restart();
#endif
StatusEffect.UpdateAll((float)deltaTime);
Task SETask = Task.Factory.StartNew(() => StatusEffect.UpdateAll((float)deltaTime));
#if CLIENT
sw.Stop();
@@ -259,10 +274,12 @@ namespace Barotrauma
body.SetPrevTransform(body.SimPosition, body.Rotation);
}
}
#if CLIENT
MapEntity.UpdateAll((float)deltaTime, cam);
#elif SERVER
Task.WaitAll(LevelTask, CharacterTask, SETask);
//This is internally multi-threaded
MapEntity.UpdateAll((float)deltaTime, Camera.Instance);
#endif
@@ -284,7 +301,7 @@ namespace Barotrauma
GameMain.PerformanceCounter.AddElapsedTicks("Update:Ragdolls", sw.ElapsedTicks);
sw.Restart();
#endif
//Sub update. Wait for change
foreach (Submarine sub in Submarine.Loaded)
{
sub.Update((float)deltaTime);