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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "SimpleProfiler.h"
#include <sstream>
#include <string>
#include <map>
#include <string.h>
#include "TimeUtil.h"
#include "Util.h"
#include "System/maindefines.h"
#include "System/SafeCStrings.h"
Profiler Profiler::def("");
Profiler* Profiler::GetDefault() {
return &Profiler::def;
}
Profiler::Profiler(const char* const name)
: name(name)
{
}
const char* Profiler::GetName() const {
return name;
}
void Profiler::AddTime(const char* const part, unsigned long time) {
parts[part] += time;
}
const std::map<const char* const, unsigned long>& Profiler::GetParts() {
return parts;
}
std::string Profiler::ToString() const {
std::ostringstream res;
static const size_t line_sizeMax = 256;
char line[line_sizeMax];
SNPRINTF(line, line_sizeMax, "%35s |%20s\n", "Part", "Total Time");
res << line;
std::map<const char* const, unsigned long>::const_iterator pi;
for (pi = parts.begin(); pi != parts.end(); ++pi) {
SNPRINTF(line, line_sizeMax, "%35s %16.3fs\n", pi->first, pi->second / 1000.f);
res << line;
}
return res.str();
}
ScopedTimer::ScopedTimer(const char* const part, Profiler* profiler)
: part(part)
, profiler(profiler ? profiler : Profiler::GetDefault())
, startTime(timeUtil_getCurrentTimeMillis())
{
}
ScopedTimer::~ScopedTimer()
{
const unsigned long stopTime = timeUtil_getCurrentTimeMillis();
profiler->AddTime(part, stopTime - startTime);
}
void simpleProfiler_addTime(const char* const part, unsigned time) {
Profiler::GetDefault()->AddTime(part, time);
}
unsigned long simpleProfiler_getTime(const char* const part) {
return Profiler::GetDefault()->GetParts().find(part)->second;
}
unsigned simpleProfiler_getParts() {
return Profiler::GetDefault()->GetParts().size();
}
unsigned simpleProfiler_getPartNames(const char** parts, const unsigned parts_sizeMax) {
unsigned p = 0;
std::map<const char* const, unsigned long>::const_iterator pi;
for (pi = Profiler::GetDefault()->GetParts().begin(); (pi != Profiler::GetDefault()->GetParts().end()) && (p < parts_sizeMax); ++pi) {
parts[p++] = pi->first;
}
return p;
}
char* simpleProfiler_getSummaryString() {
const std::string& summaryStr = Profiler::GetDefault()->ToString();
const int length = summaryStr.length();
char* summary = util_allocStr(length);
STRCPY_T(summary, length, summaryStr.c_str());
return summary;
}
|