| 1 |
/* |
| 2 |
* Profiler.cpp |
| 3 |
* ------------ |
| 4 |
* Purpose: Performance measuring |
| 5 |
* Notes : (currently none) |
| 6 |
* Authors: OpenMPT Devs |
| 7 |
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details. |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#include "stdafx.h" |
| 12 |
#include "Profiler.h" |
| 13 |
|
| 14 |
|
| 15 |
OPENMPT_NAMESPACE_BEGIN |
| 16 |
|
| 17 |
|
| 18 |
#ifdef USE_PROFILER |
| 19 |
|
| 20 |
|
| 21 |
class Statistics |
| 22 |
{ |
| 23 |
public: |
| 24 |
Profile &profile; |
| 25 |
Profile::Data data; |
| 26 |
double usage; |
| 27 |
Statistics(Profile &p) : profile(p) |
| 28 |
{ |
| 29 |
usage = 0.0; |
| 30 |
Update(); |
| 31 |
} |
| 32 |
void Update() |
| 33 |
{ |
| 34 |
data = profile.GetAndResetData(); |
| 35 |
uint64 now = profile.GetTime(); |
| 36 |
uint64 timewindow = now - data.Start; |
| 37 |
if(data.Calls > 0 && timewindow > 0) |
| 38 |
{ |
| 39 |
usage = (double)data.Sum / (double)timewindow; |
| 40 |
} else |
| 41 |
{ |
| 42 |
usage = 0.0; |
| 43 |
} |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
struct ProfileBlock |
| 49 |
{ |
| 50 |
class Profile * profile; |
| 51 |
const char * name; |
| 52 |
class Statistics * stats; |
| 53 |
}; |
| 54 |
|
| 55 |
static constexpr std::size_t MAX_PROFILES = 1024; |
| 56 |
|
| 57 |
static ProfileBlock Profiles[ MAX_PROFILES ]; |
| 58 |
|
| 59 |
static std::size_t NextProfile = 0; |
| 60 |
|
| 61 |
|
| 62 |
static void RegisterProfile(Profile *newprofile) |
| 63 |
{ |
| 64 |
if(NextProfile < MAX_PROFILES) |
| 65 |
{ |
| 66 |
Profiles[NextProfile].profile = newprofile; |
| 67 |
Profiles[NextProfile].stats = 0; |
| 68 |
NextProfile++; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
static void UnregisterProfile(Profile *oldprofile) |
| 74 |
{ |
| 75 |
for(std::size_t i=0; i<NextProfile; i++) { |
| 76 |
if(Profiles[i].profile == oldprofile) { |
| 77 |
Profiles[i].profile = 0; |
| 78 |
delete Profiles[i].stats; |
| 79 |
Profiles[i].stats = 0; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
void Profiler::Update() |
| 86 |
{ |
| 87 |
for(std::size_t i=0; i<NextProfile; i++) |
| 88 |
{ |
| 89 |
if(!Profiles[i].stats) |
| 90 |
{ |
| 91 |
Profiles[i].stats = new Statistics(*Profiles[i].profile); |
| 92 |
} else |
| 93 |
{ |
| 94 |
Profiles[i].stats->Update(); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
std::string Profiler::DumpProfiles() |
| 101 |
{ |
| 102 |
std::string ret; |
| 103 |
for(std::size_t i=0; i<NextProfile; i++) |
| 104 |
{ |
| 105 |
if(Profiles[i].stats) |
| 106 |
{ |
| 107 |
Statistics &stats = *Profiles[i].stats; |
| 108 |
std::string cat; |
| 109 |
switch(stats.profile.Category) |
| 110 |
{ |
| 111 |
case Profiler::GUI: cat = "GUI"; break; |
| 112 |
case Profiler::Audio: cat = "Audio"; break; |
| 113 |
case Profiler::Notify: cat = "Notify"; break; |
| 114 |
} |
| 115 |
ret += cat + " " + std::string(stats.profile.Name) + ": " + mpt::afmt::right(6, mpt::afmt::fix(stats.usage * 100.0, 3)) + "%\r\n"; |
| 116 |
} |
| 117 |
} |
| 118 |
ret += "\r\n"; |
| 119 |
return ret; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
std::vector<double> Profiler::DumpCategories() |
| 124 |
{ |
| 125 |
std::vector<double> ret; |
| 126 |
ret.resize(Profiler::CategoriesCount); |
| 127 |
for(std::size_t i=0; i<NextProfile; i++) |
| 128 |
{ |
| 129 |
if(Profiles[i].stats) |
| 130 |
{ |
| 131 |
ret[Profiles[i].profile->Category] += Profiles[i].stats->usage; |
| 132 |
} |
| 133 |
} |
| 134 |
return ret; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
uint64 Profile::GetTime() const |
| 139 |
{ |
| 140 |
LARGE_INTEGER ret; |
| 141 |
ret.QuadPart = 0; |
| 142 |
QueryPerformanceCounter(&ret); |
| 143 |
return ret.QuadPart; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
uint64 Profile::GetFrequency() const |
| 148 |
{ |
| 149 |
LARGE_INTEGER ret; |
| 150 |
ret.QuadPart = 0; |
| 151 |
QueryPerformanceFrequency(&ret); |
| 152 |
return ret.QuadPart; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
Profile::Profile(Profiler::Category category, const char *name) : Category(category), Name(name) |
| 157 |
{ |
| 158 |
data.Calls = 0; |
| 159 |
data.Sum = 0; |
| 160 |
data.Overhead = 0; |
| 161 |
data.Start = GetTime(); |
| 162 |
EnterTime = 0; |
| 163 |
RegisterProfile(this); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
Profile::~Profile() |
| 168 |
{ |
| 169 |
UnregisterProfile(this); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
Profile::Data Profile::GetAndResetData() |
| 174 |
{ |
| 175 |
Profile::Data ret; |
| 176 |
datamutex.lock(); |
| 177 |
ret = data; |
| 178 |
data.Calls = 0; |
| 179 |
data.Sum = 0; |
| 180 |
data.Overhead = 0; |
| 181 |
data.Start = GetTime(); |
| 182 |
datamutex.unlock(); |
| 183 |
return ret; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
void Profile::Reset() |
| 188 |
{ |
| 189 |
datamutex.lock(); |
| 190 |
data.Calls = 0; |
| 191 |
data.Sum = 0; |
| 192 |
data.Overhead = 0; |
| 193 |
data.Start = GetTime(); |
| 194 |
datamutex.unlock(); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
void Profile::Enter() |
| 199 |
{ |
| 200 |
EnterTime = GetTime(); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
void Profile::Leave() |
| 205 |
{ |
| 206 |
uint64 LeaveTime = GetTime(); |
| 207 |
datamutex.lock(); |
| 208 |
data.Calls += 1; |
| 209 |
data.Sum += LeaveTime - EnterTime; |
| 210 |
datamutex.unlock(); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
#else // !USE_PROFILER |
| 215 |
|
| 216 |
MPT_MSVC_WORKAROUND_LNK4221(Profiler) |
| 217 |
|
| 218 |
#endif // USE_PROFILER |
| 219 |
|
| 220 |
|
| 221 |
OPENMPT_NAMESPACE_END |