Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions RLBotCS/ManagerTools/ConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static class Fields
public const string MatchStateSetting = "enable_state_setting";
public const string MatchAutoSaveReplays = "auto_save_replays";
public const string MatchFreePlay = "freeplay";
public const string MatchPerformanceMonitor = "performance_monitor";

public const string MutatorsTable = "mutators";
public const string MutatorsMatchLength = "match_length";
Expand Down Expand Up @@ -749,6 +750,11 @@ public MatchConfigurationT LoadMatchConfig(string path)
Fields.RlBotWaitForAgents,
true
);
matchConfig.PerformanceMonitor = GetEnum(
rlbotTable,
Fields.MatchPerformanceMonitor,
PerformanceMonitor.ShowWhenSuboptimal
);
}

TomlTableArray players = GetValue<TomlTableArray>(outerTable, Fields.CarsList, []);
Expand Down
2 changes: 1 addition & 1 deletion RLBotCS/ManagerTools/MatchStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ private void LoadMatch(MatchConfigurationT matchConfig, PlayerSpawner spawner)

private bool IsDifferentFromLast(MatchConfigurationT matchConfig)
{
// Don't consider rendering/state setting because that can be enabled/disabled without restarting the match
// Don't consider rendering/state setting/perf monitor because they can be toggled without restarting the match

var lastMatchConfig = _matchConfig;
if (lastMatchConfig == null)
Expand Down
33 changes: 20 additions & 13 deletions RLBotCS/ManagerTools/PerfMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class PerfMonitor
private const int _maxSamples = 120;
private const float _timeSkip = 0.5f;

private static readonly ColorT TextColor = new ColorT()
private static readonly ColorT TextColor = new()
{
A = 255,
R = 255,
G = 255,
B = 255,
};
private static readonly ColorT BackColor = new ColorT()
private static readonly ColorT BackColor = new()
{
A = 100,
R = 0,
Expand All @@ -27,7 +27,8 @@ public class PerfMonitor
};

private readonly CircularBuffer<Deltas> _rlbotSamples = new(_maxSamples);
private readonly SortedDictionary<string, CircularBuffer<bool>> _samples = new();
private readonly SortedDictionary<string, CircularBuffer<bool>> _samples = [];
private PerformanceMonitor _displayMode = PerformanceMonitor.ShowWhenSuboptimal;
private float time = 0;

public void AddRLBotSample(Deltas deltas)
Expand All @@ -37,19 +38,19 @@ public void AddRLBotSample(Deltas deltas)

public void AddSample(string name, bool gotInput)
{
if (!_samples.ContainsKey(name))
{
if (_samples.TryGetValue(name, out var buffer))
buffer.AddLast(gotInput);
else
_samples[name] = new(_maxSamples);
}

_samples[name].AddLast(gotInput);
}

public void RemoveBot(string name)
{
_samples.Remove(name);
}

public void SetDisplayMode(PerformanceMonitor mode) => _displayMode = mode;

public static float GetPercentile(IEnumerable<float> data, float p)
{
var sorted = data.OrderBy(x => x).ToList();
Expand All @@ -66,6 +67,12 @@ public static float GetPercentile(IEnumerable<float> data, float p)

public void RenderSummary(Rendering rendering, GameState gameState, float deltaTime)
{
if (_displayMode == PerformanceMonitor.NeverShow)
{
rendering.RemoveRenderGroup(ClientId, RenderGroupId);
return;
}

time += deltaTime;
if (time < _timeSkip)
return;
Expand Down Expand Up @@ -99,7 +106,7 @@ public void RenderSummary(Rendering rendering, GameState gameState, float deltaT
0.99f
) * 1000f:0.0}ms
""";
bool shouldRender = misses120 > 0;
bool shouldRender = _displayMode == PerformanceMonitor.AlwaysShow || misses120 > 0;

foreach (var (name, samples) in _samples)
{
Expand All @@ -124,10 +131,10 @@ public void RenderSummary(Rendering rendering, GameState gameState, float deltaT
VAlign = TextVAlign.Top,
};

var renderMessages = new List<RenderMessageT>()
{
new RenderMessageT() { Variety = RenderTypeUnion.FromString2D(renderText) },
};
List<RenderMessageT> renderMessages =
[
new() { Variety = RenderTypeUnion.FromString2D(renderText) },
];

if (shouldRender)
rendering.AddRenderGroup(ClientId, RenderGroupId, renderMessages, gameState);
Expand Down
1 change: 1 addition & 0 deletions RLBotCS/Server/BridgeMessage/StartMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void HandleMessage(BridgeContext context)
context.AgentMapping.SetAgents(MatchConfig);
context.MatchStarter.StartMatch(MatchConfig, context.GetPlayerSpawner()); // May modify the match config
context.UpdateTimeMutators();
context.PerfMonitor.SetDisplayMode(MatchConfig.PerformanceMonitor);

// Handle messages that required a match config
foreach (var infoRequest in context.WaitingAgentRequests)
Expand Down
11 changes: 11 additions & 0 deletions RLBotCS/Server/BridgeMessage/UpdatePerformanceMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using RLBot.Flat;

namespace RLBotCS.Server.BridgeMessage;

readonly struct UpdatePerformanceMonitor(PerformanceMonitor Mode) : IBridgeMessage
{
public void HandleMessage(BridgeContext context)
{
context.PerfMonitor.SetDisplayMode(Mode);
}
}
8 changes: 8 additions & 0 deletions RLBotCS/Server/FlatBuffersSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ await _bridge.WriteAsync(
await _rlbotServer.WriteAsync(new UpdateRendering(renderingStatus));
break;

case InterfaceMessage.UpdatePerformanceMonitor:
var updatePerformanceMonitor = msg.MessageAsUpdatePerformanceMonitor()
.UnPack();
await _rlbotServer.WriteAsync(
new ServerMessage.UpdatePerformanceMonitor(updatePerformanceMonitor.Show)
);
break;

case InterfaceMessage.PingRequest:
_incomingMessages.Writer.TryWrite(
new SessionMessage.PingResponse(msg.MessageAsPingRequest().UnPack().Cookie)
Expand Down
16 changes: 16 additions & 0 deletions RLBotCS/Server/ServerMessage/UpdatePerformanceMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using RLBot.Flat;

namespace RLBotCS.Server.ServerMessage;

readonly struct UpdatePerformanceMonitor(PerformanceMonitor Mode) : IServerMessage
{
public ServerAction Execute(ServerContext context)
{
if (context.MatchConfig is { } matchConfig)
matchConfig.PerformanceMonitor = Mode;

context.Bridge.TryWrite(new BridgeMessage.UpdatePerformanceMonitor(Mode));

return ServerAction.Continue;
}
}
2 changes: 1 addition & 1 deletion RLBotCS/Server/ServerMessage/UpdateRendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ServerAction Execute(ServerContext context)

// Distribute to all sessions;
// they will figure out on their own if rendering should be enable/disabled
foreach (var (id, (writer, _)) in context.Sessions)
foreach (var (_, (writer, _)) in context.Sessions)
{
writer.TryWrite(message);
}
Expand Down
2 changes: 1 addition & 1 deletion flatbuffers-schema
Loading