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
@@ -13,6 +13,7 @@ using System.Xml.Linq;
using MoonSharp.Interpreter; using MoonSharp.Interpreter;
using System.Net; using System.Net;
using Barotrauma.Extensions; using Barotrauma.Extensions;
using System.Threading.Tasks;
namespace Barotrauma namespace Barotrauma
{ {
@@ -327,11 +328,16 @@ namespace Barotrauma
} }
Stopwatch performanceCounterTimer = Stopwatch.StartNew(); Stopwatch performanceCounterTimer = Stopwatch.StartNew();
stopwatch = Stopwatch.StartNew(); stopwatch = Stopwatch.StartNew();
PerformenceMonitor PM = new PerformenceMonitor();
long prevTicks = stopwatch.ElapsedTicks; long prevTicks = stopwatch.ElapsedTicks;
while (ShouldRun) while (ShouldRun)
{ {
#if DEBUG
Console.WriteLine(PM.ToString());
#endif
long currTicks = stopwatch.ElapsedTicks; long currTicks = stopwatch.ElapsedTicks;
double elapsedTime = Math.Max(currTicks - prevTicks, 0) / frequency; double elapsedTime = Math.Max(currTicks - prevTicks, 0) / frequency;
Timing.Accumulator += elapsedTime; Timing.Accumulator += elapsedTime;
@@ -374,6 +380,7 @@ namespace Barotrauma
Timing.Accumulator -= Timing.Step; Timing.Accumulator -= Timing.Step;
updateCount++; updateCount++;
PM.Update();
} }
#if !DEBUG #if !DEBUG
@@ -421,6 +428,9 @@ namespace Barotrauma
updateCount = 0; updateCount = 0;
} }
} }
PerformenceMonitor.PM.Dispose();
stopwatch.Stop(); stopwatch.Stop();
CloseServer(); CloseServer();
@@ -0,0 +1,164 @@
using Barotrauma.Networking;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Barotrauma
{
public class PerformenceMonitor
{
static public PerformenceMonitor PM;
private Stopwatch PMStopwatch = new Stopwatch();
private double tickratetimer = 0;
private double tickrate60stimer = 0;
private static Queue<double> tickrate10s = new Queue<double>(10);
public int ItemCount
{
get{ return Item.ItemList.Count; }
}
public int CharacterCount
{
get { return Character.CharacterList.Count; }
}
public int PhysicsBodyCount
{
get { return PhysicsBody.List.Count; }
}
public double RealTickRate
{
get; set;
}
public long TotalTicks
{
get;set;
}
public int LastSecondTicks
{
get; set;
} = 0;
public float AverageTickRate
{
get
{
return TotalTicks / (float)TotalTimeElapsed * 1000;
}
}
public double AverageTickRate10s
{
get
{
return tickrate10s.Count > 0 ? tickrate10s.Average() : 60;
}
}
public double TotalTimeElapsed
{
get
{
return PMStopwatch.Elapsed.TotalMilliseconds;
}
}
public float MemoryUsage
{
get
{
Process proc = Process.GetCurrentProcess();
float memory = MathF.Round(proc.PrivateMemorySize64 / (1024 * 1024), 2);
proc.Dispose();
return memory;
}
}
public double TickRateLow
{
get; set;
}
public double TickRateHigh
{
get; set;
}
public PerformenceMonitor()
{
PM = this;
RealTickRate = 60;
TotalTicks = 0;
LastSecondTicks = 60;
TickRateLow = 60;
TickRateHigh = 60;
PMStopwatch.Start();
}
public void Update()
{
TotalTicks += 1;
LastSecondTicks += 1;
if(tickrate10s.Count >= 10)
{
tickrate10s.Dequeue();
}
if (TotalTimeElapsed - 1000 >= tickratetimer)
{
RealTickRate = LastSecondTicks / (TotalTimeElapsed - tickratetimer) * 1000;
tickrate10s.Enqueue(RealTickRate);
tickratetimer = TotalTimeElapsed;
LastSecondTicks = 0;
}
if (TotalTimeElapsed - 60000 >= tickrate60stimer)
{
TickRateLow = 60;
TickRateHigh = 60;
tickrate60stimer = TotalTimeElapsed;
}
if (RealTickRate > TickRateHigh)
{
TickRateHigh = RealTickRate;
}
if (RealTickRate < TickRateLow)
{
TickRateLow = RealTickRate;
}
}
public void Dispose()
{
PMStopwatch.Reset();
PM = null;
}
override public string ToString()
{
return $"Item Count: {ItemCount}\n" +
$"Character Count: {CharacterCount}\n" +
$"PhysicsBody Count: {PhysicsBodyCount}\n" +
$"Tick Rate: {RealTickRate}\n" +
$"Min Tick Rate: {TickRateLow}\n" +
$"Max Tick Rate: {TickRateHigh}\n" +
$"Total Ticks: {TotalTicks}\n" +
$"All time Average Tick Rate: {AverageTickRate}\n" +
$"10s Average Tick Rate: {AverageTickRate10s}\n" +
$"TotalTimeElapsed: {TotalTimeElapsed}\n" +
$"Memory Usage: {MemoryUsage}";
}
}
}
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks;
using System.Xml.Linq; using System.Xml.Linq;
namespace Barotrauma namespace Barotrauma
@@ -647,35 +648,40 @@ namespace Barotrauma
var sw = new System.Diagnostics.Stopwatch(); var sw = new System.Diagnostics.Stopwatch();
sw.Start(); sw.Start();
#endif #endif
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0) Task StructuralTask = Task.Factory.StartNew(() =>
{ {
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
foreach (Hull hull in Hull.HullList)
{ {
hull.Update(deltaTime * MapEntityUpdateInterval, cam); foreach (Hull hull in Hull.HullList)
} {
hull.Update(deltaTime * MapEntityUpdateInterval, cam);
}
#if CLIENT #if CLIENT
Hull.UpdateCheats(deltaTime * MapEntityUpdateInterval, cam); Hull.UpdateCheats(deltaTime * MapEntityUpdateInterval, cam);
#endif #endif
foreach (Structure structure in Structure.WallList) foreach (Structure structure in Structure.WallList)
{ {
structure.Update(deltaTime * MapEntityUpdateInterval, cam); structure.Update(deltaTime * MapEntityUpdateInterval, cam);
}
} }
} });
foreach (Gap gap in Gap.GapList) Task GapTask = Task.Factory.StartNew(() =>
{ {
gap.ResetWaterFlowThisFrame(); foreach (Gap gap in Gap.GapList)
} {
//update gaps in random order, because otherwise in rooms with multiple gaps gap.ResetWaterFlowThisFrame();
//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 //update gaps in random order, because otherwise in rooms with multiple gaps
//one gap in a room even if there are several //the water/air will always tend to flow through the first gap in the list,
foreach (Gap gap in Gap.GapList.OrderBy(g => Rand.Int(int.MaxValue))) //which may lead to weird behavior like water draining down only through
{ //one gap in a room even if there are several
gap.Update(deltaTime, cam); foreach (Gap gap in Gap.GapList.OrderBy(g => Rand.Int(int.MaxValue)))
} {
gap.Update(deltaTime, cam);
}
});
if (mapEntityUpdateTick % PoweredUpdateInterval == 0) if (mapEntityUpdateTick % PoweredUpdateInterval == 0)
{ {
@@ -687,7 +693,6 @@ namespace Barotrauma
GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Misc", sw.ElapsedTicks); GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Misc", sw.ElapsedTicks);
sw.Restart(); sw.Restart();
#endif #endif
Item.UpdatePendingConditionUpdates(deltaTime); Item.UpdatePendingConditionUpdates(deltaTime);
if (mapEntityUpdateTick % MapEntityUpdateInterval == 0) if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
{ {
@@ -705,32 +710,38 @@ namespace Barotrauma
catch (InvalidOperationException e) catch (InvalidOperationException e)
{ {
GameAnalyticsManager.AddErrorEventOnce( GameAnalyticsManager.AddErrorEventOnce(
"MapEntity.UpdateAll:ItemUpdateInvalidOperation", "MapEntity.UpdateAll:ItemUpdateInvalidOperation",
GameAnalyticsManager.ErrorSeverity.Critical, GameAnalyticsManager.ErrorSeverity.Critical,
$"Error while updating item {lastUpdatedItem?.Name ?? "null"}: {e.Message}"); $"Error while updating item {lastUpdatedItem?.Name ?? "null"}: {e.Message}");
throw new InvalidOperationException($"Error while updating item {lastUpdatedItem?.Name ?? "null"}", innerException: e); 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 #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); if (mapEntityUpdateTick % MapEntityUpdateInterval == 0)
{
UpdateAllProjSpecific(deltaTime * MapEntityUpdateInterval);
Spawner?.Update(); Spawner?.Update();
} }
});
Task.WaitAll(SpawnerTask, PItemTask, GapTask, StructuralTask);
} }
static partial void UpdateAllProjSpecific(float deltaTime); static partial void UpdateAllProjSpecific(float deltaTime);
@@ -174,7 +174,9 @@ namespace Barotrauma.Networking
exceptionMsg += " Child process has not exited."; exceptionMsg += " Child process has not exited.";
} }
#endif #endif
#if !DEBUG
throw new Exception(exceptionMsg); throw new Exception(exceptionMsg);
#endif
} }
} }
@@ -4,6 +4,8 @@ using Microsoft.Xna.Framework;
using System.Threading; using System.Threading;
using FarseerPhysics.Dynamics; using FarseerPhysics.Dynamics;
using FarseerPhysics; using FarseerPhysics;
using System.Threading.Tasks;
using System.Linq;
#if DEBUG && CLIENT #if DEBUG && CLIENT
@@ -69,6 +71,7 @@ namespace Barotrauma
MapEntity.ClearHighlightedEntities(); MapEntity.ClearHighlightedEntities();
Task PhysicsTask = Task.Factory.StartNew(() => ExecutePhysics());
#if RUN_PHYSICS_IN_SEPARATE_THREAD #if RUN_PHYSICS_IN_SEPARATE_THREAD
var physicsThread = new Thread(ExecutePhysics) var physicsThread = new Thread(ExecutePhysics)
{ {
@@ -138,6 +141,7 @@ namespace Barotrauma
GameTime += deltaTime; GameTime += deltaTime;
//Physics Update; wait for changes.
foreach (PhysicsBody body in PhysicsBody.List) foreach (PhysicsBody body in PhysicsBody.List)
{ {
//update character (colliders) regardless if they're enabled or not, so that the draw position is updated //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.BodyType != BodyType.Static)
{ {
body.Update(); body.Update();
} }
if (body.Enabled && body.BodyType != FarseerPhysics.BodyType.Static)
{
body.SetPrevTransform(body.SimPosition, body.Rotation);
}
} }
MapEntity.ClearHighlightedEntities(); MapEntity.ClearHighlightedEntities();
#if CLIENT #if CLIENT
var sw = new System.Diagnostics.Stopwatch(); var sw = new System.Diagnostics.Stopwatch();
sw.Start(); sw.Start();
#endif #endif
//This is not changed yet. Will be back for it.
GameMain.GameSession?.Update((float)deltaTime); GameMain.GameSession?.Update((float)deltaTime);
#if CLIENT #if CLIENT
@@ -195,8 +204,14 @@ namespace Barotrauma
Character.UpdateAll((float)deltaTime, cam); Character.UpdateAll((float)deltaTime, cam);
#elif SERVER #elif SERVER
if (Level.Loaded != null) Level.Loaded.Update((float)deltaTime, Camera.Instance); Task LevelTask = Task.Factory.StartNew(() =>
Character.UpdateAll((float)deltaTime, Camera.Instance); {
if (Level.Loaded != null)
{
Level.Loaded.Update((float)deltaTime, Camera.Instance);
}
});
Task CharacterTask = Task.Factory.StartNew(() => Character.UpdateAll((float)deltaTime, Camera.Instance));
#endif #endif
@@ -206,7 +221,7 @@ namespace Barotrauma
sw.Restart(); sw.Restart();
#endif #endif
StatusEffect.UpdateAll((float)deltaTime); Task SETask = Task.Factory.StartNew(() => StatusEffect.UpdateAll((float)deltaTime));
#if CLIENT #if CLIENT
sw.Stop(); sw.Stop();
@@ -259,10 +274,12 @@ namespace Barotrauma
body.SetPrevTransform(body.SimPosition, body.Rotation); body.SetPrevTransform(body.SimPosition, body.Rotation);
} }
} }
#if CLIENT #if CLIENT
MapEntity.UpdateAll((float)deltaTime, cam); MapEntity.UpdateAll((float)deltaTime, cam);
#elif SERVER #elif SERVER
Task.WaitAll(LevelTask, CharacterTask, SETask);
//This is internally multi-threaded
MapEntity.UpdateAll((float)deltaTime, Camera.Instance); MapEntity.UpdateAll((float)deltaTime, Camera.Instance);
#endif #endif
@@ -284,7 +301,7 @@ namespace Barotrauma
GameMain.PerformanceCounter.AddElapsedTicks("Update:Ragdolls", sw.ElapsedTicks); GameMain.PerformanceCounter.AddElapsedTicks("Update:Ragdolls", sw.ElapsedTicks);
sw.Restart(); sw.Restart();
#endif #endif
//Sub update. Wait for change
foreach (Submarine sub in Submarine.Loaded) foreach (Submarine sub in Submarine.Loaded)
{ {
sub.Update((float)deltaTime); sub.Update((float)deltaTime);