2020-10-13 20:54:42 +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
|
|
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
|
private const double FrameRateWeight = 0.5;
|
2021-09-14 23:24:49 +00:00
|
|
|
|
private const int FrameTypeGame = 0;
|
|
|
|
|
private const int PercentTypeFifo = 0;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private double[] _averageFrameRate;
|
|
|
|
|
private double[] _accumulatedFrameTime;
|
|
|
|
|
private double[] _previousFrameTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2020-10-13 20:54:42 +00:00
|
|
|
|
private double[] _averagePercent;
|
2020-12-17 18:39:52 +00:00
|
|
|
|
private double[] _accumulatedActiveTime;
|
2020-10-13 20:54:42 +00:00
|
|
|
|
private double[] _percentLastEndTime;
|
|
|
|
|
private double[] _percentStartTime;
|
|
|
|
|
|
2020-12-17 18:39:52 +00:00
|
|
|
|
private long[] _framesRendered;
|
|
|
|
|
private double[] _percentTime;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private object[] _frameLock;
|
2020-10-13 20:54:42 +00:00
|
|
|
|
private object[] _percentLock;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private double _ticksToSeconds;
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private Timer _resetTimer;
|
2018-03-06 20:18:49 +00:00
|
|
|
|
|
|
|
|
|
public PerformanceStatistics()
|
|
|
|
|
{
|
2020-10-13 20:54:42 +00:00
|
|
|
|
_averageFrameRate = new double[1];
|
|
|
|
|
_accumulatedFrameTime = new double[1];
|
|
|
|
|
_previousFrameTime = new double[1];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2020-12-17 18:39:52 +00:00
|
|
|
|
_averagePercent = new double[1];
|
|
|
|
|
_accumulatedActiveTime = new double[1];
|
|
|
|
|
_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];
|
2020-12-17 18:39:52 +00:00
|
|
|
|
_percentTime = new double[1];
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2020-10-13 20:54:42 +00:00
|
|
|
|
_frameLock = new object[] { new object() };
|
|
|
|
|
_percentLock = new object[] { new object() };
|
2018-06-24 00:39:25 +00:00
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
_resetTimer = new Timer(1000);
|
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)
|
|
|
|
|
{
|
2018-06-24 00:39:25 +00:00
|
|
|
|
CalculateAverageFrameRate(FrameTypeGame);
|
2020-10-13 20:54:42 +00:00
|
|
|
|
CalculateAveragePercent(PercentTypeFifo);
|
2018-03-06 20:18:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
|
private void CalculateAverageFrameRate(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];
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 23:24:49 +00:00
|
|
|
|
_averageFrameRate[frameType] = LinearInterpolate(_averageFrameRate[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
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 23:24:49 +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
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 23:24:49 +00:00
|
|
|
|
private static double LinearInterpolate(double lhs, double rhs)
|
2018-06-24 00:39:25 +00:00
|
|
|
|
{
|
2020-10-13 20:54:42 +00:00
|
|
|
|
return lhs * (1.0 - FrameRateWeight) + rhs * FrameRateWeight;
|
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)
|
|
|
|
|
{
|
2021-09-14 23:24:49 +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;
|
2021-09-14 23:24:49 +00:00
|
|
|
|
_percentTime[percentType] += elapsedTime;
|
2020-10-13 20:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_percentLastEndTime[percentType] = currentTime;
|
2021-09-14 23:24:49 +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
|
|
|
|
{
|
2020-10-13 20:54:42 +00:00
|
|
|
|
return _averageFrameRate[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-14 23:24:49 +00:00
|
|
|
|
}
|