Debian Codegraph @

Source/Core/Common/Profiler.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/Profiler.h"
5
6 #include <algorithm>
7 #include <cmath>
8 #include <iomanip>
9 #include <ios>
10 #include <sstream>
11 #include <utility>
12
13 #include "Common/Timer.h"
14
15 namespace Common
16 {
17 static const u32 PROFILER_FIELD_LENGTH = 8;
18 static const u32 PROFILER_FIELD_LENGTH_FP = PROFILER_FIELD_LENGTH + 3;
19 static const int PROFILER_LAZY_DELAY = 60; // in frames
20
21 std::list<Profiler*> Profiler::s_all_profilers;
22 std::mutex Profiler::s_mutex;
23 u32 Profiler::s_max_length = 0;
24 u64 Profiler::s_frame_time;
25 u64 Profiler::s_usecs_frame;
26
27 std::string Profiler::s_lazy_result;
28 int Profiler::s_lazy_delay = 0;
29
30 Profiler::Profiler(std::string name)
31 : m_name(std::move(name)), m_usecs(0), m_usecs_min(UINT64_MAX), m_usecs_max(0), m_usecs_quad(0),
32 m_calls(0)
33 {
34 s_max_length = std::max<u32>(s_max_length, u32(m_name.length()));
35
36 std::lock_guard<std::mutex> lk(s_mutex);
37 s_all_profilers.push_back(this);
38 }
39
40 Profiler::~Profiler()
41 {
42 std::lock_guard<std::mutex> lk(s_mutex);
43 s_all_profilers.remove(this);
44 }
45
46 bool Profiler::operator<(const Profiler& b) const
47 {
48 return m_usecs < b.m_usecs;
49 }
50
51 std::string Profiler::ToString()
52 {
53 if (s_lazy_delay > 0)
54 {
55 s_lazy_delay--;
56 return s_lazy_result;
57 }
58 s_lazy_delay = PROFILER_LAZY_DELAY - 1;
59
60 // don't write anything if no profilation is enabled
61 std::lock_guard<std::mutex> lk(s_mutex);
62 if (s_all_profilers.empty())
63 return "";
64
65 u64 end = Common::Timer::NowUs();
66 s_usecs_frame = end - s_frame_time;
67 s_frame_time = end;
68
69 std::ostringstream buffer;
70 buffer << std::setw(s_max_length) << std::left << ""
71 << " ";
72 buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << "calls"
73 << " ";
74 buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << "sum"
75 << " ";
76 buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << "rel"
77 << " ";
78 buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << "min"
79 << " ";
80 buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << "avg"
81 << " ";
82 buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << "stdev"
83 << " ";
84 buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << "max"
85 << " ";
86 buffer << "/ usec" << std::endl;
87
88 s_all_profilers.sort([](Profiler* a, Profiler* b) { return *b < *a; });
89
90 for (auto profiler : s_all_profilers)
91 {
92 buffer << profiler->Read() << std::endl;
93 }
94 s_lazy_result = buffer.str();
95 return s_lazy_result;
96 }
97
98 void Profiler::Start(u64* time, int* depth)
99 {
100 if ((*depth)++ == 0)
101 {
102 *time = Common::Timer::NowUs();
103 }
104 }
105
106 void Profiler::Stop(u64* time, int* depth)
107 {
108 if (--(*depth) == 0)
109 {
110 u64 end = Common::Timer::NowUs();
111
112 u64 diff = end - *time;
113
114 std::lock_guard lk(m_mutex);
115 m_usecs += diff;
116 m_usecs_min = std::min(m_usecs_min, diff);
117 m_usecs_max = std::max(m_usecs_max, diff);
118 m_usecs_quad += diff * diff;
119 m_calls++;
120 }
121 }
122
123 std::string Profiler::Read()
124 {
125 std::lock_guard lk(m_mutex);
126
127 double avg = 0;
128 double stdev = 0;
129 double time_rel = 0;
130 if (m_calls)
131 {
132 avg = double(m_usecs) / m_calls;
133 stdev = std::sqrt(double(m_usecs_quad) / m_calls - avg * avg);
134 }
135 else
136 {
137 m_usecs_min = 0;
138 }
139 if (s_usecs_frame)
140 {
141 time_rel = double(m_usecs) * 100 / s_usecs_frame;
142 }
143
144 std::ostringstream buffer;
145
146 buffer << std::setw(s_max_length) << std::left << m_name << " ";
147 buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << m_calls << " ";
148 buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << m_usecs << " ";
149 buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << time_rel << " ";
150 buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << m_usecs_min << " ";
151 buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << std::fixed << std::setprecision(2)
152 << avg << " ";
153 buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << std::fixed << std::setprecision(2)
154 << stdev << " ";
155 buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << m_usecs_max;
156
157 m_usecs = 0;
158 m_usecs_min = UINT64_MAX;
159 m_usecs_max = 0;
160 m_usecs_quad = 0;
161 m_calls = 0;
162
163 return buffer.str();
164 }
165 } // namespace Common