2020-10-13 22:54:42 +02:00
|
|
|
|
using Ryujinx.Common;
|
2018-03-06 21:18:49 +01:00
|
|
|
|
using System.Timers;
|
|
|
|
|
|
2018-06-11 02:46:42 +02:00
|
|
|
|
namespace Ryujinx.HLE
|
2018-03-06 21:18:49 +01:00
|
|
|
|
{
|
|
|
|
|
public class PerformanceStatistics
|
|
|
|
|
{
|
2021-09-15 02:26:10 +02:00
|
|
|
|
private const int FrameTypeGame = 0;
|
|
|
|
|
private const int PercentTypeFifo = 0;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2021-09-15 02:26:10 +02:00
|
|
|
|
private double[] _frameRate;
|
2018-12-06 12:16:24 +01:00
|
|
|
|
private double[] _accumulatedFrameTime;
|
|
|
|
|
private double[] _previousFrameTime;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2020-10-13 22:54:42 +02:00
|
|
|
|
private double[] _averagePercent;
|
2020-12-17 19:39:52 +01:00
|
|
|
|
private double[] _accumulatedActiveTime;
|
2020-10-13 22:54:42 +02:00
|
|
|
|
private double[] _percentLastEndTime;
|
|
|
|
|
private double[] _percentStartTime;
|
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
private long[] _framesRendered;
|
|
|
|
|
private double[] _percentTime;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
private object[] _frameLock;
|
2020-10-13 22:54:42 +02:00
|
|
|
|
private object[] _percentLock;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
private double _ticksToSeconds;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
private Timer _resetTimer;
|
2018-03-06 21:18:49 +01:00
|
|
|
|
|
|
|
|
|
public PerformanceStatistics()
|
|
|
|
|
{
|
2021-09-15 02:26:10 +02:00
|
|
|
|
_frameRate = new double[1];
|
2020-10-13 22:54:42 +02:00
|
|
|
|
_accumulatedFrameTime = new double[1];
|
|
|
|
|
_previousFrameTime = new double[1];
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2020-12-17 19:39:52 +01:00
|
|
|
|
_averagePercent = new double[1];
|
|
|
|
|
_accumulatedActiveTime = new double[1];
|
|
|
|
|
_percentLastEndTime = new double[1];
|
|
|
|
|
_percentStartTime = new double[1];
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2020-10-13 22:54:42 +02:00
|
|
|
|
_framesRendered = new long[1];
|
2020-12-17 19:39:52 +01:00
|
|
|
|
_percentTime = new double[1];
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2020-10-13 22:54:42 +02:00
|
|
|
|
_frameLock = new object[] { new object() };
|
|
|
|
|
_percentLock = new object[] { new object() };
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2021-09-15 02:26:10 +02:00
|
|
|
|
_resetTimer = new Timer(750);
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_resetTimer.Elapsed += ResetTimerElapsed;
|
|
|
|
|
_resetTimer.AutoReset = true;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_resetTimer.Start();
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2020-10-13 22:54:42 +02:00
|
|
|
|
_ticksToSeconds = 1.0 / PerformanceCounter.TicksPerSecond;
|
2018-03-06 21:18:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetTimerElapsed(object sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
2021-09-15 02:26:10 +02:00
|
|
|
|
CalculateFrameRate(FrameTypeGame);
|
2020-10-13 22:54:42 +02:00
|
|
|
|
CalculateAveragePercent(PercentTypeFifo);
|
2018-03-06 21:18:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 02:26:10 +02:00
|
|
|
|
private void CalculateFrameRate(int frameType)
|
2018-03-06 21:18:49 +01:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
double frameRate = 0;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
lock (_frameLock[frameType])
|
2018-06-24 02:39:25 +02:00
|
|
|
|
{
|
2020-10-13 22:54:42 +02:00
|
|
|
|
if (_accumulatedFrameTime[frameType] > 0)
|
|
|
|
|
{
|
|
|
|
|
frameRate = _framesRendered[frameType] / _accumulatedFrameTime[frameType];
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 02:26:10 +02:00
|
|
|
|
_frameRate[frameType] = frameRate;
|
2021-09-15 01:24:49 +02:00
|
|
|
|
_framesRendered[frameType] = 0;
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_accumulatedFrameTime[frameType] = 0;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
}
|
2018-03-06 21:18:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 22:54:42 +02:00
|
|
|
|
private void CalculateAveragePercent(int percentType)
|
2018-03-06 21:18:49 +01:00
|
|
|
|
{
|
2020-10-13 22:54:42 +02: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 19:39:52 +01:00
|
|
|
|
if (_percentTime[percentType] > 0)
|
2020-10-13 22:54:42 +02:00
|
|
|
|
{
|
2020-12-17 19:39:52 +01:00
|
|
|
|
percent = (_accumulatedActiveTime[percentType] / _percentTime[percentType]) * 100;
|
2020-10-13 22:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 01:24:49 +02:00
|
|
|
|
_averagePercent[percentType] = percent;
|
|
|
|
|
_percentTime[percentType] = 0;
|
2020-12-17 19:39:52 +01:00
|
|
|
|
_accumulatedActiveTime[percentType] = 0;
|
2020-10-13 22:54:42 +02:00
|
|
|
|
}
|
2018-06-24 02:39:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 21:18:49 +01:00
|
|
|
|
public void RecordGameFrameTime()
|
|
|
|
|
{
|
2018-06-24 02:39:25 +02:00
|
|
|
|
RecordFrameTime(FrameTypeGame);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 22:54:42 +02: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)
|
|
|
|
|
{
|
2021-09-15 01:24:49 +02:00
|
|
|
|
double currentTime = PerformanceCounter.ElapsedTicks * _ticksToSeconds;
|
|
|
|
|
double elapsedTime = currentTime - _percentLastEndTime[percentType];
|
2020-10-13 22:54:42 +02:00
|
|
|
|
double elapsedActiveTime = currentTime - _percentStartTime[percentType];
|
|
|
|
|
|
|
|
|
|
lock (_percentLock[percentType])
|
|
|
|
|
{
|
2020-12-17 19:39:52 +01:00
|
|
|
|
_accumulatedActiveTime[percentType] += elapsedActiveTime;
|
2021-09-15 01:24:49 +02:00
|
|
|
|
_percentTime[percentType] += elapsedTime;
|
2020-10-13 22:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_percentLastEndTime[percentType] = currentTime;
|
2021-09-15 01:24:49 +02:00
|
|
|
|
_percentStartTime[percentType] = 0;
|
2020-10-13 22:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
private void RecordFrameTime(int frameType)
|
2018-06-24 02:39:25 +02:00
|
|
|
|
{
|
2020-10-13 22:54:42 +02:00
|
|
|
|
double currentFrameTime = PerformanceCounter.ElapsedTicks * _ticksToSeconds;
|
2018-12-06 12:16:24 +01:00
|
|
|
|
double elapsedFrameTime = currentFrameTime - _previousFrameTime[frameType];
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_previousFrameTime[frameType] = currentFrameTime;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
lock (_frameLock[frameType])
|
2018-06-24 02:39:25 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_accumulatedFrameTime[frameType] += elapsedFrameTime;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_framesRendered[frameType]++;
|
2018-06-24 02:39:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 22:54:42 +02:00
|
|
|
|
public double GetGameFrameRate()
|
2018-06-24 02:39:25 +02:00
|
|
|
|
{
|
2021-09-15 02:26:10 +02:00
|
|
|
|
return _frameRate[FrameTypeGame];
|
2018-03-06 21:18:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 22:54:42 +02:00
|
|
|
|
public double GetFifoPercent()
|
2018-03-06 21:18:49 +01:00
|
|
|
|
{
|
2020-10-13 22:54:42 +02:00
|
|
|
|
return _averagePercent[PercentTypeFifo];
|
2018-03-06 21:18:49 +01:00
|
|
|
|
}
|
2021-09-15 02:26:10 +02:00
|
|
|
|
|
|
|
|
|
public double GetGameFrameTime()
|
|
|
|
|
{
|
|
|
|
|
return 1000 / _frameRate[FrameTypeGame];
|
|
|
|
|
}
|
2018-03-06 21:18:49 +01:00
|
|
|
|
}
|
2021-09-15 01:24:49 +02:00
|
|
|
|
}
|