2018-03-09 18:12:57 -08:00
|
|
|
|
using System.Diagnostics;
|
2018-03-06 12:18:49 -08:00
|
|
|
|
using System.Timers;
|
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
private const int FrameTypeSystem = 0;
|
|
|
|
|
private const int FrameTypeGame = 1;
|
|
|
|
|
|
|
|
|
|
private double[] AverageFrameRate;
|
|
|
|
|
private double[] AccumulatedFrameTime;
|
|
|
|
|
private double[] PreviousFrameTime;
|
|
|
|
|
|
|
|
|
|
private long[] FramesRendered;
|
|
|
|
|
|
|
|
|
|
private object[] FrameLock;
|
|
|
|
|
|
|
|
|
|
private double TicksToSeconds;
|
|
|
|
|
|
|
|
|
|
private Stopwatch ExecutionTime;
|
|
|
|
|
|
|
|
|
|
private Timer ResetTimer;
|
2018-03-06 12:18:49 -08:00
|
|
|
|
|
|
|
|
|
public PerformanceStatistics()
|
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
AverageFrameRate = new double[2];
|
|
|
|
|
AccumulatedFrameTime = new double[2];
|
|
|
|
|
PreviousFrameTime = new double[2];
|
|
|
|
|
|
|
|
|
|
FramesRendered = new long[2];
|
|
|
|
|
|
|
|
|
|
FrameLock = new object[] { new object(), new object() };
|
|
|
|
|
|
|
|
|
|
ExecutionTime = new Stopwatch();
|
|
|
|
|
|
2018-03-06 12:18:49 -08:00
|
|
|
|
ExecutionTime.Start();
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
|
|
|
|
ResetTimer = new Timer(1000);
|
|
|
|
|
|
2018-03-06 12:18:49 -08:00
|
|
|
|
ResetTimer.Elapsed += ResetTimerElapsed;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-03-06 12:18:49 -08:00
|
|
|
|
ResetTimer.AutoReset = true;
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
2018-03-06 12:18:49 -08:00
|
|
|
|
ResetTimer.Start();
|
2018-06-23 17:39:25 -07:00
|
|
|
|
|
|
|
|
|
TicksToSeconds = 1.0 / Stopwatch.Frequency;
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetTimerElapsed(object sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
CalculateAverageFrameRate(FrameTypeSystem);
|
|
|
|
|
CalculateAverageFrameRate(FrameTypeGame);
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
|
private void CalculateAverageFrameRate(int FrameType)
|
2018-03-06 12:18:49 -08:00
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
double FrameRate = 0;
|
|
|
|
|
|
|
|
|
|
if (AccumulatedFrameTime[FrameType] > 0)
|
|
|
|
|
{
|
|
|
|
|
FrameRate = FramesRendered[FrameType] / AccumulatedFrameTime[FrameType];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (FrameLock[FrameType])
|
|
|
|
|
{
|
|
|
|
|
AverageFrameRate[FrameType] = LinearInterpolate(AverageFrameRate[FrameType], FrameRate);
|
|
|
|
|
|
|
|
|
|
FramesRendered[FrameType] = 0;
|
|
|
|
|
|
|
|
|
|
AccumulatedFrameTime[FrameType] = 0;
|
|
|
|
|
}
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
|
private double LinearInterpolate(double Old, double New)
|
2018-03-06 12:18:49 -08:00
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
return Old * (1.0 - FrameRateWeight) + New * FrameRateWeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RecordSystemFrameTime()
|
|
|
|
|
{
|
|
|
|
|
RecordFrameTime(FrameTypeSystem);
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RecordGameFrameTime()
|
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
RecordFrameTime(FrameTypeGame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RecordFrameTime(int FrameType)
|
|
|
|
|
{
|
|
|
|
|
double CurrentFrameTime = ExecutionTime.ElapsedTicks * TicksToSeconds;
|
|
|
|
|
|
|
|
|
|
double ElapsedFrameTime = CurrentFrameTime - PreviousFrameTime[FrameType];
|
|
|
|
|
|
|
|
|
|
PreviousFrameTime[FrameType] = CurrentFrameTime;
|
|
|
|
|
|
|
|
|
|
lock (FrameLock[FrameType])
|
|
|
|
|
{
|
|
|
|
|
AccumulatedFrameTime[FrameType] += ElapsedFrameTime;
|
|
|
|
|
|
|
|
|
|
FramesRendered[FrameType]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double GetSystemFrameRate()
|
|
|
|
|
{
|
|
|
|
|
return AverageFrameRate[FrameTypeSystem];
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
|
public double GetGameFrameRate()
|
2018-03-06 12:18:49 -08:00
|
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
|
return AverageFrameRate[FrameTypeGame];
|
2018-03-06 12:18:49 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|