aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Profiler/Profile.cs
blob: 4dba6ea5420086329d7a76b99772c8c1d632e2b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Ryujinx.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace Ryujinx.Profiler
{
    public static class Profile
    {
        public static float UpdateRate    => _settings.UpdateRate;
        public static long  HistoryLength => _settings.History;

        public static ProfilerKeyboardHandler Controls      => _settings.Controls;

        private static InternalProfile  _profileInstance;
        private static ProfilerSettings _settings;

        [Conditional("USE_PROFILING")]
        public static void Initialize()
        {
            var config = ProfilerConfiguration.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ProfilerConfig.jsonc"));

            _settings = new ProfilerSettings()
            {
                Enabled         = config.Enabled,
                FileDumpEnabled = config.DumpPath != "",
                DumpLocation    = config.DumpPath,
                UpdateRate      = (config.UpdateRate <= 0) ? -1 : 1.0f / config.UpdateRate,
                History         = (long)(config.History * PerformanceCounter.TicksPerSecond),
                MaxLevel        = config.MaxLevel,
                Controls        = config.Controls,
                MaxFlags        = config.MaxFlags,
            };
        }

        public static bool ProfilingEnabled()
        {
#if USE_PROFILING
            if (!_settings.Enabled)
                return false;

            if (_profileInstance == null)
                _profileInstance = new InternalProfile(_settings.History, _settings.MaxFlags);

            return true;
#else
            return false;
#endif
        }

        [Conditional("USE_PROFILING")]
        public static void FinishProfiling()
        {
            if (!ProfilingEnabled())
                return;

            if (_settings.FileDumpEnabled)
                DumpProfile.ToFile(_settings.DumpLocation, _profileInstance);

            _profileInstance.Dispose();
        }

        [Conditional("USE_PROFILING")]
        public static void FlagTime(TimingFlagType flagType)
        {
            if (!ProfilingEnabled())
                return;
            _profileInstance.FlagTime(flagType);
        }

        [Conditional("USE_PROFILING")]
        public static void RegisterFlagReceiver(Action<TimingFlag> receiver)
        {
            if (!ProfilingEnabled())
                return;
            _profileInstance.RegisterFlagReceiver(receiver);
        }

        [Conditional("USE_PROFILING")]
        public static void Begin(ProfileConfig config)
        {
            if (!ProfilingEnabled())
                return;
            if (config.Level > _settings.MaxLevel)
                return;
            _profileInstance.BeginProfile(config);
        }

        [Conditional("USE_PROFILING")]
        public static void End(ProfileConfig config)
        {
            if (!ProfilingEnabled())
                return;
            if (config.Level > _settings.MaxLevel)
                return;
            _profileInstance.EndProfile(config);
        }

        public static string GetSession()
        {
#if USE_PROFILING
            if (!ProfilingEnabled())
                return null;
            return _profileInstance.GetSession();
#else
            return "";
#endif
        }

        public static List<KeyValuePair<ProfileConfig, TimingInfo>> GetProfilingData()
        {
#if USE_PROFILING
            if (!ProfilingEnabled())
                return new List<KeyValuePair<ProfileConfig, TimingInfo>>();
            return _profileInstance.GetProfilingData();
#else
            return new List<KeyValuePair<ProfileConfig, TimingInfo>>();
#endif
        }

        public static TimingFlag[] GetTimingFlags()
        {
#if USE_PROFILING
            if (!ProfilingEnabled())
                return new TimingFlag[0];
            return _profileInstance.GetTimingFlags();
#else
            return new TimingFlag[0];
#endif
        }

        public static (long[], long[]) GetTimingAveragesAndLast()
        {
#if USE_PROFILING
            if (!ProfilingEnabled())
                return (new long[0], new long[0]);
            return _profileInstance.GetTimingAveragesAndLast();
#else
            return (new long[0], new long[0]);
#endif
        }
    }
}