diff --git a/Ryujinx.Profiler/Profile.cs b/Ryujinx.Profiler/Profile.cs index a2c18d46a0..66e0204e2d 100644 --- a/Ryujinx.Profiler/Profile.cs +++ b/Ryujinx.Profiler/Profile.cs @@ -1,10 +1,22 @@ using System; -using Ryujinx.Common.Logging; +using System.Collections.Concurrent; +using System.Diagnostics; namespace Ryujinx.Profiler { public class Profile { + private struct TimingInfo + { + public long BeginTime, LastTime, TotalTime, Count; + public long AverageTime + { + get => (Count == 0) ? -1 : TotalTime / Count; + } + } + + + // Static private static Profile ProfileInstance; private static ProfilerSettings Settings; @@ -27,14 +39,14 @@ namespace Ryujinx.Profiler { if (!ProfilingEnabled()) return; - Logger.PrintInfo(LogClass.Gpu, $"Begin {config.Name}"); + ProfileInstance.BeginProfile(config); } public static void End(ProfileConfig config) { if (!ProfilingEnabled()) return; - Logger.PrintInfo(LogClass.Gpu, $"End {config.Name}"); + ProfileInstance.EndProfile(config); } public static void Method(ProfileConfig config, Action method) @@ -47,5 +59,58 @@ namespace Ryujinx.Profiler method(); End(config); } + + + // Non-static + private Stopwatch SW; + private ConcurrentDictionary Timers; + + public Profile() + { + Timers = new ConcurrentDictionary(); + SW = new Stopwatch(); + SW.Start(); + } + + public void BeginProfile(ProfileConfig config) + { + long timestamp = SW.ElapsedTicks; + + Timers.AddOrUpdate(config.Name, + (string s) => CreateTimer(timestamp), + ((s, info) => + { + info.BeginTime = timestamp; + return info; + })); + } + + public void EndProfile(ProfileConfig config) + { + long timestamp = SW.ElapsedTicks; + + Timers.AddOrUpdate(config.Name, + (s => new TimingInfo()), + ((s, time) => UpdateTimer(time, timestamp))); + } + + private TimingInfo CreateTimer(long timestamp) + { + return new TimingInfo() + { + BeginTime = timestamp, + LastTime = 0, + Count = 0, + }; + } + + private TimingInfo UpdateTimer(TimingInfo time, long timestamp) + { + time.Count++; + time.LastTime = timestamp - time.BeginTime; + time.TotalTime += time.LastTime; + + return time; + } } } diff --git a/Ryujinx.Profiler/ProfileConfig.cs b/Ryujinx.Profiler/ProfileConfig.cs index 4aaacf1f13..842e87fcd4 100644 --- a/Ryujinx.Profiler/ProfileConfig.cs +++ b/Ryujinx.Profiler/ProfileConfig.cs @@ -15,7 +15,7 @@ namespace Ryujinx.Profiler { public static ProfileConfig Test = new ProfileConfig() { - Name = "Test", + Name = "CPU.Test", }; } }