2023-12-04 13:17:13 +00:00
|
|
|
using Ryujinx.Common;
|
2018-03-06 20:18:49 +00:00
|
|
|
using System.Timers;
|
|
|
|
|
2018-06-11 00:46:42 +00:00
|
|
|
namespace Ryujinx.HLE
|
2018-03-06 20:18:49 +00:00
|
|
|
{
|
|
|
|
public class PerformanceStatistics
|
|
|
|
{
|
2023-07-16 17:31:14 +00:00
|
|
|
private const int FrameTypeGame = 0;
|
2021-09-15 00:26:10 +00:00
|
|
|
private const int PercentTypeFifo = 0;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
private readonly double[] _frameRate;
|
|
|
|
private readonly double[] _accumulatedFrameTime;
|
|
|
|
private readonly double[] _previousFrameTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
private readonly double[] _averagePercent;
|
|
|
|
private readonly double[] _accumulatedActiveTime;
|
|
|
|
private readonly double[] _percentLastEndTime;
|
|
|
|
private readonly double[] _percentStartTime;
|
2020-10-13 20:54:42 +00:00
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
private readonly long[] _framesRendered;
|
|
|
|
private readonly double[] _percentTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
private readonly object[] _frameLock;
|
|
|
|
private readonly object[] _percentLock;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
private readonly double _ticksToSeconds;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
private readonly Timer _resetTimer;
|
2018-03-06 20:18:49 +00:00
|
|
|
|
|
|
|
public PerformanceStatistics()
|
|
|
|
{
|
2023-07-16 17:31:14 +00:00
|
|
|
_frameRate = new double[1];
|
2020-10-13 20:54:42 +00:00
|
|
|
_accumulatedFrameTime = new double[1];
|
2023-07-16 17:31:14 +00:00
|
|
|
_previousFrameTime = new double[1];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
_averagePercent = new double[1];
|
2020-12-17 18:39:52 +00:00
|
|
|
_accumulatedActiveTime = new double[1];
|
2023-07-16 17:31:14 +00:00
|
|
|
_percentLastEndTime = new double[1];
|
|
|
|
_percentStartTime = new double[1];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2020-10-13 20:54:42 +00:00
|
|
|
_framesRendered = new long[1];
|
2023-07-16 17:31:14 +00:00
|
|
|
_percentTime = new double[1];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
_frameLock = new[] { new object() };
|
|
|
|
_percentLock = new[] { new object() };
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2021-09-15 00:26:10 +00:00
|
|
|
_resetTimer = new Timer(750);
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_resetTimer.Elapsed += ResetTimerElapsed;
|
|
|
|
_resetTimer.AutoReset = true;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_resetTimer.Start();
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2020-10-13 20:54:42 +00:00
|
|
|
_ticksToSeconds = 1.0 / PerformanceCounter.TicksPerSecond;
|
2018-03-06 20:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ResetTimerElapsed(object sender, ElapsedEventArgs e)
|
|
|
|
{
|
2021-09-15 00:26:10 +00:00
|
|
|
CalculateFrameRate(FrameTypeGame);
|
2020-10-13 20:54:42 +00:00
|
|
|
CalculateAveragePercent(PercentTypeFifo);
|
2018-03-06 20:18:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:26:10 +00:00
|
|
|
private void CalculateFrameRate(int frameType)
|
2018-03-06 20:18:49 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
double frameRate = 0;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
lock (_frameLock[frameType])
|
2018-06-24 00:39:25 +00:00
|
|
|
{
|
2020-10-13 20:54:42 +00:00
|
|
|
if (_accumulatedFrameTime[frameType] > 0)
|
|
|
|
{
|
|
|
|
frameRate = _framesRendered[frameType] / _accumulatedFrameTime[frameType];
|
|
|
|
}
|
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
_frameRate[frameType] = frameRate;
|
|
|
|
_framesRendered[frameType] = 0;
|
2018-12-06 11:16:24 +00:00
|
|
|
_accumulatedFrameTime[frameType] = 0;
|
2018-06-24 00:39:25 +00:00
|
|
|
}
|
2018-03-06 20:18:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 20:54:42 +00:00
|
|
|
private void CalculateAveragePercent(int percentType)
|
2018-03-06 20:18:49 +00:00
|
|
|
{
|
2020-10-13 20:54:42 +00:00
|
|
|
// If start time is non-zero, a percent reading is still being measured.
|
|
|
|
// If there aren't any readings, the default should be 100% if still being measured, or 0% if not.
|
|
|
|
double percent = (_percentStartTime[percentType] == 0) ? 0 : 100;
|
|
|
|
|
|
|
|
lock (_percentLock[percentType])
|
|
|
|
{
|
2020-12-17 18:39:52 +00:00
|
|
|
if (_percentTime[percentType] > 0)
|
2020-10-13 20:54:42 +00:00
|
|
|
{
|
2020-12-17 18:39:52 +00:00
|
|
|
percent = (_accumulatedActiveTime[percentType] / _percentTime[percentType]) * 100;
|
2020-10-13 20:54:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 17:31:14 +00:00
|
|
|
_averagePercent[percentType] = percent;
|
|
|
|
_percentTime[percentType] = 0;
|
2020-12-17 18:39:52 +00:00
|
|
|
_accumulatedActiveTime[percentType] = 0;
|
2020-10-13 20:54:42 +00:00
|
|
|
}
|
2018-06-24 00:39:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 20:18:49 +00:00
|
|
|
public void RecordGameFrameTime()
|
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
RecordFrameTime(FrameTypeGame);
|
|
|
|
}
|
|
|
|
|
2020-10-13 20:54:42 +00:00
|
|
|
public void RecordFifoStart()
|
|
|
|
{
|
|
|
|
StartPercentTime(PercentTypeFifo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RecordFifoEnd()
|
|
|
|
{
|
|
|
|
EndPercentTime(PercentTypeFifo);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StartPercentTime(int percentType)
|
|
|
|
{
|
|
|
|
double currentTime = PerformanceCounter.ElapsedTicks * _ticksToSeconds;
|
|
|
|
|
|
|
|
_percentStartTime[percentType] = currentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EndPercentTime(int percentType)
|
|
|
|
{
|
2023-07-16 17:31:14 +00:00
|
|
|
double currentTime = PerformanceCounter.ElapsedTicks * _ticksToSeconds;
|
|
|
|
double elapsedTime = currentTime - _percentLastEndTime[percentType];
|
2020-10-13 20:54:42 +00:00
|
|
|
double elapsedActiveTime = currentTime - _percentStartTime[percentType];
|
|
|
|
|
|
|
|
lock (_percentLock[percentType])
|
|
|
|
{
|
2020-12-17 18:39:52 +00:00
|
|
|
_accumulatedActiveTime[percentType] += elapsedActiveTime;
|
2023-07-16 17:31:14 +00:00
|
|
|
_percentTime[percentType] += elapsedTime;
|
2020-10-13 20:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_percentLastEndTime[percentType] = currentTime;
|
2023-07-16 17:31:14 +00:00
|
|
|
_percentStartTime[percentType] = 0;
|
2020-10-13 20:54:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private void RecordFrameTime(int frameType)
|
2018-06-24 00:39:25 +00:00
|
|
|
{
|
2020-10-13 20:54:42 +00:00
|
|
|
double currentFrameTime = PerformanceCounter.ElapsedTicks * _ticksToSeconds;
|
2018-12-06 11:16:24 +00:00
|
|
|
double elapsedFrameTime = currentFrameTime - _previousFrameTime[frameType];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_previousFrameTime[frameType] = currentFrameTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
lock (_frameLock[frameType])
|
2018-06-24 00:39:25 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_accumulatedFrameTime[frameType] += elapsedFrameTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_framesRendered[frameType]++;
|
2018-06-24 00:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 20:54:42 +00:00
|
|
|
public double GetGameFrameRate()
|
2018-06-24 00:39:25 +00:00
|
|
|
{
|
2021-09-15 00:26:10 +00:00
|
|
|
return _frameRate[FrameTypeGame];
|
2018-03-06 20:18:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 20:54:42 +00:00
|
|
|
public double GetFifoPercent()
|
2018-03-06 20:18:49 +00:00
|
|
|
{
|
2020-10-13 20:54:42 +00:00
|
|
|
return _averagePercent[PercentTypeFifo];
|
2018-03-06 20:18:49 +00:00
|
|
|
}
|
2021-09-15 00:26:10 +00:00
|
|
|
|
|
|
|
public double GetGameFrameTime()
|
|
|
|
{
|
|
|
|
return 1000 / _frameRate[FrameTypeGame];
|
|
|
|
}
|
2018-03-06 20:18:49 +00:00
|
|
|
}
|
2023-07-16 17:31:14 +00:00
|
|
|
}
|