Skip to content

Commit

Permalink
Merge pull request #6327 from smoogipoo/headless-thread-rate-matching
Browse files Browse the repository at this point in the history
Make time move along at the same rate in all threads during headless execution
  • Loading branch information
peppy authored Jul 16, 2024
2 parents 2ddb18c + 8a9f261 commit 691d867
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
60 changes: 52 additions & 8 deletions osu.Framework/Platform/HeadlessGameHost.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Configuration;
using osu.Framework.Graphics.Rendering.Dummy;
using osu.Framework.Input.Handlers;
using osu.Framework.Logging;
using osu.Framework.Threading;
using osu.Framework.Timing;

namespace osu.Framework.Platform
Expand All @@ -21,7 +22,7 @@ public class HeadlessGameHost : DesktopGameHost
public const double CLOCK_RATE = 1000.0 / 30;

private readonly bool realtime;
private IFrameBasedClock customClock;
private IFrameBasedClock? customClock;

protected override IFrameBasedClock SceneGraphClock => customClock ?? base.SceneGraphClock;

Expand All @@ -41,15 +42,15 @@ public override bool PresentFileExternally(string filename)

public override IEnumerable<string> UserStoragePaths => new[] { "./headless/" };

public HeadlessGameHost(string gameName = null, HostOptions options = null, bool realtime = true)
public HeadlessGameHost(string? gameName = null, HostOptions? options = null, bool realtime = true)
: base(gameName ?? Guid.NewGuid().ToString(), options)
{
this.realtime = realtime;
}

protected override bool RequireWindowExists => false;

protected override IWindow CreateWindow(GraphicsSurfaceType preferredSurface) => null;
protected override IWindow CreateWindow(GraphicsSurfaceType preferredSurface) => null!;

protected override Clipboard CreateClipboard() => new HeadlessClipboard();

Expand Down Expand Up @@ -78,7 +79,7 @@ protected override void SetupForRun()

if (!realtime)
{
customClock = new FramedClock(new FastClock(CLOCK_RATE));
customClock = new FramedClock(new FastClock(CLOCK_RATE, Threads.ToArray()));

// time is incremented per frame, rather than based on the real-world time.
// therefore our goal is to run frames as fast as possible.
Expand Down Expand Up @@ -108,19 +109,62 @@ protected override void UpdateFrame()
private class FastClock : IClock
{
private readonly double increment;

private readonly GameThread[] gameThreads;
private readonly ulong[] gameThreadLastFrames;

private readonly Stopwatch stopwatch = new Stopwatch();

private double time;

/// <summary>
/// A clock which increments each time <see cref="CurrentTime"/> is requested.
/// Run fast. Run consistent.
/// </summary>
/// <param name="increment">Milliseconds we should increment the clock by each time the time is requested.</param>
public FastClock(double increment)
/// <param name="gameThreads">The game threads.</param>
public FastClock(double increment, GameThread[] gameThreads)
{
this.increment = increment;
this.gameThreads = gameThreads;
gameThreadLastFrames = new ulong[gameThreads.Length];
}

public double CurrentTime
{
get
{
double realElapsedTime = stopwatch.Elapsed.TotalMilliseconds;
stopwatch.Restart();

if (allThreadsHaveProgressed)
{
for (int i = 0; i < gameThreads.Length; i++)
gameThreadLastFrames[i] = gameThreads[i].FrameIndex;

// Increment time at the expedited rate.
return time += increment;
}

// Fall back to real time to ensure we don't break random tests that expect threads to be running.
return time += realElapsedTime;
}
}

private bool allThreadsHaveProgressed
{
get
{
for (int i = 0; i < gameThreads.Length; i++)
{
if (gameThreads[i].FrameIndex == gameThreadLastFrames[i])
return false;
}

return true;
}
}

public double CurrentTime => time += increment;
public double Rate => 1;
public bool IsRunning => true;
}
Expand Down
7 changes: 7 additions & 0 deletions osu.Framework/Threading/GameThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ public double InactiveHz

internal virtual IEnumerable<StatisticsCounterType> StatisticsCounters => Array.Empty<StatisticsCounterType>();

/// <summary>
/// The amount of times this thread has run.
/// </summary>
internal ulong FrameIndex { get; private set; }

/// <summary>
/// The main work which is fired on each frame.
/// </summary>
Expand Down Expand Up @@ -439,6 +444,8 @@ void runWork()

try
{
FrameIndex++;

Monitor?.NewFrame();

using (Monitor?.BeginCollecting(PerformanceCollectionType.Scheduler))
Expand Down

0 comments on commit 691d867

Please sign in to comment.