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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "System/TimeProfiler.h"
#include <cstring>
#include <boost/unordered_map.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
#include "System/Log/ILog.h"
#include "System/UnsyncedRNG.h"
#ifdef THREADPOOL
#include "System/ThreadPool.h"
#endif
static boost::mutex m;
static std::map<int, std::string> hashToName;
static std::map<int, int> refs;
static unsigned hash_(const std::string& s)
{
unsigned hash = s.size();
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
hash += *it;
}
return hash;
}
static unsigned hash_(const char* s)
{
unsigned hash = 0;
for (size_t i = 0; ; ++i) {
if (s[i]) {
hash += s[i];
} else {
unsigned len = i;
hash += len;
break;
}
}
return hash;
}
BasicTimer::BasicTimer(const std::string& myname)
: hash(hash_(myname))
, starttime(spring_gettime())
{
nameIterator = hashToName.find(hash);
if (nameIterator == hashToName.end()) {
nameIterator = hashToName.insert(std::pair<int,std::string>(hash, myname)).first;
} else {
assert(nameIterator->second == myname);
}
}
BasicTimer::BasicTimer(const char* myname)
: hash(hash_(myname))
, starttime(spring_gettime())
{
nameIterator = hashToName.find(hash);
if (nameIterator == hashToName.end()) {
nameIterator = hashToName.insert(std::pair<int,std::string>(hash, myname)).first;
} else {
assert(nameIterator->second == myname);
}
}
const std::string& BasicTimer::GetName() const
{
return nameIterator->second;
}
spring_time BasicTimer::GetDuration() const
{
return spring_difftime(spring_gettime(), starttime);
}
ScopedTimer::ScopedTimer(const std::string& name, bool autoShow)
: BasicTimer(name)
, autoShowGraph(autoShow)
{
it = refs.find(hash);
if (it == refs.end()) {
it = refs.insert(std::pair<int,int>(hash, 0)).first;
}
++(it->second);
}
ScopedTimer::ScopedTimer(const char* name, bool autoShow)
: BasicTimer(name)
, autoShowGraph(autoShow)
{
it = refs.find(hash);
if (it == refs.end()) {
it = refs.insert(std::pair<int,int>(hash, 0)).first;
}
++(it->second);
}
ScopedTimer::~ScopedTimer()
{
int& ref = it->second;
if (--ref == 0)
profiler.AddTime(GetName(), GetDuration(), autoShowGraph);
}
ScopedOnceTimer::~ScopedOnceTimer()
{
LOG("%s: %i ms", GetName().c_str(), int(GetDuration().toMilliSecsi()));
}
ScopedMtTimer::ScopedMtTimer(const std::string& name, bool autoShow)
: BasicTimer(name)
, autoShowGraph(autoShow)
{
}
ScopedMtTimer::ScopedMtTimer(const char* name, bool autoShow)
: BasicTimer(name)
, autoShowGraph(autoShow)
{
}
ScopedMtTimer::~ScopedMtTimer()
{
profiler.AddTime(GetName(), GetDuration(), autoShowGraph);
#ifdef THREADPOOL
auto& list = profiler.profileCore[ThreadPool::GetThreadNum()];
list.emplace_back(starttime, spring_gettime());
#endif
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTimeProfiler::CTimeProfiler():
lastBigUpdate(spring_gettime()),
currentPosition(0)
{
#ifdef THREADPOOL
profileCore.resize(ThreadPool::GetMaxThreads());
#endif
}
CTimeProfiler::~CTimeProfiler()
{
boost::unique_lock<boost::mutex> ulk(m, boost::defer_lock);
while (!ulk.try_lock()) {}
}
CTimeProfiler& CTimeProfiler::GetInstance()
{
static CTimeProfiler tp;
return tp;
}
void CTimeProfiler::Update()
{
//FIXME non-locking threadsafe
boost::unique_lock<boost::mutex> ulk(m, boost::defer_lock);
while (!ulk.try_lock()) {}
++currentPosition;
currentPosition &= TimeRecord::frames_size-1;
for (auto& pi: profile) {
pi.second.frames[currentPosition] = spring_notime;
}
const spring_time curTime = spring_gettime();
const float timeDiff = spring_diffmsecs(curTime, lastBigUpdate);
if (timeDiff > 500.0f) // twice every second
{
for (auto& pi: profile) {
auto& p = pi.second;
p.percent = spring_tomsecs(p.current) / timeDiff;
p.current = spring_notime;
p.newLagPeak = false;
p.newPeak = false;
if(p.percent > p.peak) {
p.peak = p.percent;
p.newPeak = true;
}
}
lastBigUpdate = curTime;
}
if (curTime.toSecsi() % 6 == 0) {
for (auto& pi: profile) {
auto& p = pi.second;
p.maxLag *= 0.5f;
}
}
}
float CTimeProfiler::GetPercent(const char* name)
{
boost::unique_lock<boost::mutex> ulk(m, boost::defer_lock);
while (!ulk.try_lock()) {}
return profile[name].percent;
}
void CTimeProfiler::AddTime(const std::string& name, const spring_time time, const bool showGraph)
{
auto pi = profile.find(name);
if (pi != profile.end()) {
// profile already exists
//FIXME use atomic ints
auto& p = pi->second;
p.total += time;
p.current += time;
p.frames[currentPosition] += time;
if (p.maxLag < time.toMilliSecsf()) {
p.maxLag = time.toMilliSecsf();
p.newLagPeak = true;
}
} else {
boost::unique_lock<boost::mutex> ulk(m, boost::defer_lock);
while (!ulk.try_lock()) {}
// create a new profile
auto& p = profile[name];
p.total = time;
p.current = time;
p.maxLag = time.toMilliSecsf();
p.percent = 0;
memset(p.frames, 0, TimeRecord::frames_size * sizeof(unsigned));
static UnsyncedRNG rand;
rand.Seed(spring_tomsecs(spring_gettime()));
p.color.x = rand.RandFloat();
p.color.y = rand.RandFloat();
p.color.z = rand.RandFloat();
p.showGraph = showGraph;
}
}
void CTimeProfiler::PrintProfilingInfo() const
{
LOG("%35s|%18s|%s", "Part", "Total Time", "Time of the last 0.5s");
for (auto pi = profile.begin(); pi != profile.end(); ++pi) {
const std::string& name = pi->first;
const TimeRecord& tr = pi->second;
LOG("%35s %16.2fms %5.2f%%", name.c_str(), tr.total.toMilliSecsf(), tr.percent * 100);
}
}
|