2017-04-01 20:10:38 +02:00
|
|
|
#include "zprofiling.h"
|
|
|
|
|
|
|
|
#ifdef VOXEL_PROFILING
|
2018-09-19 21:25:04 +02:00
|
|
|
#include <core/os/os.h>
|
2017-04-01 20:10:38 +02:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
class ZProfileVar {
|
|
|
|
public:
|
|
|
|
static const unsigned int BUFFERED_TIME_COUNT = 100;
|
|
|
|
|
|
|
|
ZProfileVar();
|
|
|
|
|
|
|
|
void begin(uint64_t time);
|
|
|
|
void end(uint64_t time);
|
|
|
|
|
|
|
|
Dictionary serialize();
|
|
|
|
|
|
|
|
private:
|
|
|
|
float min_time;
|
|
|
|
float max_time;
|
|
|
|
float total_time;
|
|
|
|
float instant_time;
|
|
|
|
int hits;
|
|
|
|
float buffered_times[BUFFERED_TIME_COUNT];
|
|
|
|
unsigned int buffered_time_index;
|
|
|
|
|
|
|
|
float _begin_time;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
inline uint64_t get_time() {
|
|
|
|
return OS::get_singleton()->get_ticks_usec();
|
|
|
|
//return OS::get_singleton()->get_ticks_msec();
|
|
|
|
}
|
|
|
|
|
|
|
|
ZProfileVar::ZProfileVar() {
|
|
|
|
instant_time = 0.f;
|
|
|
|
min_time = 0.f;
|
|
|
|
max_time = 0.f;
|
|
|
|
total_time = 0.f;
|
|
|
|
hits = 0;
|
|
|
|
_begin_time = 0.f;
|
2017-08-13 01:19:39 +02:00
|
|
|
for (unsigned int i = 0; i < BUFFERED_TIME_COUNT; ++i)
|
2017-04-01 20:10:38 +02:00
|
|
|
buffered_times[i] = 0;
|
|
|
|
buffered_time_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZProfileVar::begin(uint64_t time) {
|
|
|
|
_begin_time = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZProfileVar::end(uint64_t time) {
|
|
|
|
instant_time = time - _begin_time;
|
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
if (hits == 0) {
|
2017-04-01 20:10:38 +02:00
|
|
|
min_time = instant_time;
|
|
|
|
max_time = instant_time;
|
2017-08-13 01:19:39 +02:00
|
|
|
} else {
|
|
|
|
if (instant_time < min_time)
|
2017-04-01 20:10:38 +02:00
|
|
|
min_time = instant_time;
|
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
if (instant_time > max_time)
|
2017-04-01 20:10:38 +02:00
|
|
|
max_time = instant_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_time += instant_time;
|
|
|
|
|
|
|
|
buffered_times[buffered_time_index] = instant_time;
|
|
|
|
++buffered_time_index;
|
2017-08-13 01:19:39 +02:00
|
|
|
if (buffered_time_index >= BUFFERED_TIME_COUNT)
|
2017-04-01 20:10:38 +02:00
|
|
|
buffered_time_index = 0;
|
|
|
|
|
|
|
|
++hits;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary ZProfileVar::serialize() {
|
|
|
|
Dictionary d;
|
|
|
|
d["instant_time"] = instant_time;
|
|
|
|
d["min_time"] = min_time;
|
|
|
|
d["max_time"] = max_time;
|
|
|
|
d["mean_time"] = hits > 0 ? total_time / static_cast<float>(hits) : 0;
|
|
|
|
d["total_time"] = total_time;
|
|
|
|
d["hits"] = hits;
|
|
|
|
|
|
|
|
Array a;
|
2017-08-13 01:19:39 +02:00
|
|
|
for (unsigned int i = 0; i < BUFFERED_TIME_COUNT; ++i) {
|
2017-04-01 20:10:38 +02:00
|
|
|
a.append(buffered_times[i]);
|
|
|
|
}
|
|
|
|
d["buffered_times"] = a;
|
|
|
|
d["buffered_time_index"] = buffered_time_index;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//ZProfiler g_zprofiler;
|
|
|
|
|
|
|
|
//ZProfiler & ZProfiler::get() {
|
|
|
|
// return g_zprofiler;
|
|
|
|
//}
|
|
|
|
|
|
|
|
ZProfiler::~ZProfiler() {
|
2017-08-13 01:19:39 +02:00
|
|
|
const String *key = NULL;
|
2017-04-01 20:10:38 +02:00
|
|
|
while (key = _vars.next(key)) {
|
2017-08-13 01:19:39 +02:00
|
|
|
ZProfileVar *v = _vars.get(*key);
|
2017-04-01 20:10:38 +02:00
|
|
|
memdelete(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 01:19:39 +02:00
|
|
|
ZProfileVar *ZProfiler::get_var(String key) {
|
|
|
|
ZProfileVar *v = NULL;
|
|
|
|
ZProfileVar **pv = _vars.getptr(key);
|
|
|
|
if (pv == NULL) {
|
2017-04-01 20:10:38 +02:00
|
|
|
v = memnew(ZProfileVar);
|
|
|
|
_vars[key] = v;
|
2017-08-13 01:19:39 +02:00
|
|
|
} else {
|
2017-04-01 20:10:38 +02:00
|
|
|
v = *pv;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZProfiler::begin(String key) {
|
2017-08-13 01:19:39 +02:00
|
|
|
ZProfileVar *v = get_var(key);
|
2017-04-01 20:10:38 +02:00
|
|
|
v->begin(get_time());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZProfiler::end(String key) {
|
|
|
|
uint64_t time = get_time();
|
2017-08-13 01:19:39 +02:00
|
|
|
ZProfileVar *v = get_var(key);
|
2017-04-01 20:10:38 +02:00
|
|
|
v->end(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary ZProfiler::get_all_serialized_info() const {
|
|
|
|
Dictionary d;
|
2017-08-13 01:19:39 +02:00
|
|
|
const String *key = NULL;
|
2017-04-01 20:10:38 +02:00
|
|
|
while (key = _vars.next(key)) {
|
2017-08-13 01:19:39 +02:00
|
|
|
ZProfileVar *v = _vars.get(*key);
|
2017-04-01 20:10:38 +02:00
|
|
|
d[*key] = v->serialize();
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // VOXEL_PROFILING
|