2020-10-13 13:54:42 -07:00
|
|
|
|
using Ryujinx.Common;
|
|
|
|
|
using System.Diagnostics;
|
2018-03-06 12:18:49 -08:00
|
|
|
|
using System.Timers;
|
|
|
|
|
|
2018-06-10 17:46:42 -07:00
|
|
|
|
namespace Ryujinx.HLE
|
2018-03-06 12:18:49 -08:00
|
|
|
|
{
|
|
|
|
|
public class PerformanceStatistics
|
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
private const double FrameRateWeight = 0.5;
|
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
private const int FrameTypeGame = 0;
|
|
|
|
|
private const int PercentTypeFifo = 0;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
private double[] _averageFrameRate;
|
|
|
|
|
private double[] _accumulatedFrameTime;
|
|
|
|
|
private double[] _previousFrameTime;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
private double[] _averagePercent;
|
|
|
|
|
private double[] _accumulatedPercent;
|
|
|
|
|
private double[] _percentLastEndTime;
|
|
|
|
|
private double[] _percentStartTime;
|
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
private long[] _framesRendered;
|
2020-10-13 13:54:42 -07:00
|
|
|
|
private long[] _percentCount;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
private object[] _frameLock;
|
2020-10-13 13:54:42 -07:00
|
|
|
|
private object[] _percentLock;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
private double _ticksToSeconds;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
private Timer _resetTimer;
|
2018-03-06 12:18:49 -08:00
|
|
|
|
|
|
|
|
|
public PerformanceStatistics()
|
|
|
|
|
{
|
2020-10-13 13:54:42 -07:00
|
|
|
|
_averageFrameRate = new double[1];
|
|
|
|
|
_accumulatedFrameTime = new double[1];
|
|
|
|
|
_previousFrameTime = new double[1];
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
_averagePercent = new double[1];
|
|
|
|
|
_accumulatedPercent = new double[1];
|
|
|
|
|
_percentLastEndTime = new double[1];
|
|
|
|
|
_percentStartTime = new double[1];
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
_framesRendered = new long[1];
|
|
|
|
|
_percentCount = new long[1];
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
_frameLock = new object[] { new object() };
|
|
|
|
|
_percentLock = new object[] { new object() };
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_resetTimer = new Timer(1000);
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_resetTimer.Elapsed += ResetTimerElapsed;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_resetTimer.AutoReset = true;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_resetTimer.Start();
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
_ticksToSeconds = 1.0 / PerformanceCounter.TicksPerSecond;
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetTimerElapsed(object sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
CalculateAverageFrameRate(FrameTypeGame);
|
2020-10-13 13:54:42 -07:00
|
|
|
|
CalculateAveragePercent(PercentTypeFifo);
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
private void CalculateAverageFrameRate(int frameType)
|
2018-03-06 12:18:49 -08:00
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
|
double frameRate = 0;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
lock (_frameLock[frameType])
|
2018-06-23 17:39:25 -07:00
|
|
|
|
{
|
2020-10-13 13:54:42 -07:00
|
|
|
|
if (_accumulatedFrameTime[frameType] > 0)
|
|
|
|
|
{
|
|
|
|
|
frameRate = _framesRendered[frameType] / _accumulatedFrameTime[frameType];
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_averageFrameRate[frameType] = LinearInterpolate(_averageFrameRate[frameType], frameRate);
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_framesRendered[frameType] = 0;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_accumulatedFrameTime[frameType] = 0;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
}
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
private void CalculateAveragePercent(int percentType)
|
2018-03-06 12:18:49 -08:00
|
|
|
|
{
|
2020-10-13 13:54:42 -07: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])
|
|
|
|
|
{
|
|
|
|
|
if (_percentCount[percentType] > 0)
|
|
|
|
|
{
|
|
|
|
|
percent = _accumulatedPercent[percentType] / _percentCount[percentType];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_averagePercent[percentType] = percent;
|
|
|
|
|
|
|
|
|
|
_percentCount[percentType] = 0;
|
|
|
|
|
|
|
|
|
|
_accumulatedPercent[percentType] = 0;
|
|
|
|
|
}
|
2018-06-23 17:39:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
private double LinearInterpolate(double lhs, double rhs)
|
2018-06-23 17:39:25 -07:00
|
|
|
|
{
|
2020-10-13 13:54:42 -07:00
|
|
|
|
return lhs * (1.0 - FrameRateWeight) + rhs * FrameRateWeight;
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RecordGameFrameTime()
|
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
RecordFrameTime(FrameTypeGame);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 13:54:42 -07: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)
|
|
|
|
|
{
|
|
|
|
|
double currentTime = PerformanceCounter.ElapsedTicks * _ticksToSeconds;
|
|
|
|
|
|
|
|
|
|
double elapsedTime = currentTime - _percentLastEndTime[percentType];
|
|
|
|
|
double elapsedActiveTime = currentTime - _percentStartTime[percentType];
|
|
|
|
|
|
|
|
|
|
double percentActive = (elapsedActiveTime / elapsedTime) * 100;
|
|
|
|
|
|
|
|
|
|
lock (_percentLock[percentType])
|
|
|
|
|
{
|
|
|
|
|
_accumulatedPercent[percentType] += percentActive;
|
|
|
|
|
|
|
|
|
|
_percentCount[percentType]++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_percentLastEndTime[percentType] = currentTime;
|
|
|
|
|
_percentStartTime[percentType] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
private void RecordFrameTime(int frameType)
|
2018-06-23 17:39:25 -07:00
|
|
|
|
{
|
2020-10-13 13:54:42 -07:00
|
|
|
|
double currentFrameTime = PerformanceCounter.ElapsedTicks * _ticksToSeconds;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
double elapsedFrameTime = currentFrameTime - _previousFrameTime[frameType];
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_previousFrameTime[frameType] = currentFrameTime;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
lock (_frameLock[frameType])
|
2018-06-23 17:39:25 -07:00
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_accumulatedFrameTime[frameType] += elapsedFrameTime;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
|
_framesRendered[frameType]++;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
public double GetGameFrameRate()
|
2018-06-23 17:39:25 -07:00
|
|
|
|
{
|
2020-10-13 13:54:42 -07:00
|
|
|
|
return _averageFrameRate[FrameTypeGame];
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 13:54:42 -07:00
|
|
|
|
public double GetFifoPercent()
|
2018-03-06 12:18:49 -08:00
|
|
|
|
{
|
2020-10-13 13:54:42 -07:00
|
|
|
|
return _averagePercent[PercentTypeFifo];
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|