2024-02-26 21:12:04 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime;
|
2024-04-16 09:51:43 +08:00
|
|
|
|
using System.Threading;
|
2024-02-26 21:12:04 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2024-02-27 18:17:34 +08:00
|
|
|
|
using osu.Framework.Logging;
|
2024-02-27 18:36:03 +08:00
|
|
|
|
using osu.Game.Performance;
|
2024-02-26 21:12:04 +08:00
|
|
|
|
|
2024-02-27 18:36:03 +08:00
|
|
|
|
namespace osu.Desktop.Performance
|
2024-02-26 21:12:04 +08:00
|
|
|
|
{
|
2024-02-27 18:36:03 +08:00
|
|
|
|
public class HighPerformanceSessionManager : IHighPerformanceSessionManager
|
2024-02-26 21:12:04 +08:00
|
|
|
|
{
|
2024-04-16 09:51:43 +08:00
|
|
|
|
public bool IsSessionActive => activeSessions > 0;
|
|
|
|
|
|
2024-04-16 09:50:51 +08:00
|
|
|
|
private int activeSessions;
|
|
|
|
|
|
2024-02-26 21:12:04 +08:00
|
|
|
|
private GCLatencyMode originalGCMode;
|
|
|
|
|
|
|
|
|
|
public IDisposable BeginSession()
|
|
|
|
|
{
|
2024-04-16 09:50:51 +08:00
|
|
|
|
enterSession();
|
|
|
|
|
return new InvokeOnDisposal<HighPerformanceSessionManager>(this, static m => m.exitSession());
|
2024-02-26 21:12:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 09:50:51 +08:00
|
|
|
|
private void enterSession()
|
2024-02-26 21:12:04 +08:00
|
|
|
|
{
|
2024-04-16 09:50:51 +08:00
|
|
|
|
if (Interlocked.Increment(ref activeSessions) > 1)
|
|
|
|
|
{
|
2024-04-16 16:37:11 +08:00
|
|
|
|
Logger.Log($"High performance session requested ({activeSessions} running in total)");
|
2024-04-16 09:50:51 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 18:17:34 +08:00
|
|
|
|
Logger.Log("Starting high performance session");
|
|
|
|
|
|
2024-02-26 21:12:04 +08:00
|
|
|
|
originalGCMode = GCSettings.LatencyMode;
|
|
|
|
|
GCSettings.LatencyMode = GCLatencyMode.LowLatency;
|
2024-02-26 21:13:15 +08:00
|
|
|
|
|
|
|
|
|
// Without doing this, the new GC mode won't kick in until the next GC, which could be at a more noticeable point in time.
|
2024-02-26 21:12:04 +08:00
|
|
|
|
GC.Collect(0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 09:50:51 +08:00
|
|
|
|
private void exitSession()
|
2024-02-26 21:12:04 +08:00
|
|
|
|
{
|
2024-04-16 09:50:51 +08:00
|
|
|
|
if (Interlocked.Decrement(ref activeSessions) > 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.Log($"High performance session finished ({activeSessions} others remain)");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 18:17:34 +08:00
|
|
|
|
Logger.Log("Ending high performance session");
|
|
|
|
|
|
2024-02-26 21:12:04 +08:00
|
|
|
|
if (GCSettings.LatencyMode == GCLatencyMode.LowLatency)
|
|
|
|
|
GCSettings.LatencyMode = originalGCMode;
|
2024-02-26 21:13:15 +08:00
|
|
|
|
|
|
|
|
|
// No GC.Collect() as we were already collecting at a higher frequency in the old mode.
|
2024-02-26 21:12:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|