perf_timer: split timer class into a base one and a logging one

This commit is contained in:
moneromooo-monero 2018-05-28 19:16:25 +01:00
parent d126a02b5d
commit 7314d919e7
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
2 changed files with 43 additions and 15 deletions

View File

@ -33,7 +33,7 @@
#undef MONERO_DEFAULT_LOG_CATEGORY #undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "perf" #define MONERO_DEFAULT_LOG_CATEGORY "perf"
namespace namespace tools
{ {
uint64_t get_tick_count() uint64_t get_tick_count()
{ {
@ -83,7 +83,7 @@ namespace tools
el::Level performance_timer_log_level = el::Level::Debug; el::Level performance_timer_log_level = el::Level::Debug;
static __thread std::vector<PerformanceTimer*> *performance_timers = NULL; static __thread std::vector<LoggingPerformanceTimer*> *performance_timers = NULL;
void set_performance_timer_log_level(el::Level level) void set_performance_timer_log_level(el::Level level)
{ {
@ -96,17 +96,24 @@ void set_performance_timer_log_level(el::Level level)
performance_timer_log_level = level; performance_timer_log_level = level;
} }
PerformanceTimer::PerformanceTimer(const std::string &s, uint64_t unit, el::Level l): name(s), unit(unit), level(l), started(false), paused(false) PerformanceTimer::PerformanceTimer(bool paused): started(true), paused(paused)
{
if (paused)
ticks = 0;
else
ticks = get_tick_count();
}
LoggingPerformanceTimer::LoggingPerformanceTimer(const std::string &s, uint64_t unit, el::Level l): PerformanceTimer(), name(s), unit(unit), level(l)
{ {
ticks = get_tick_count();
if (!performance_timers) if (!performance_timers)
{ {
MLOG(level, "PERF ----------"); MLOG(level, "PERF ----------");
performance_timers = new std::vector<PerformanceTimer*>(); performance_timers = new std::vector<LoggingPerformanceTimer*>();
} }
else else
{ {
PerformanceTimer *pt = performance_timers->back(); LoggingPerformanceTimer *pt = performance_timers->back();
if (!pt->started && !pt->paused) if (!pt->started && !pt->paused)
{ {
size_t size = 0; for (const auto *tmp: *performance_timers) if (!tmp->paused) ++size; size_t size = 0; for (const auto *tmp: *performance_timers) if (!tmp->paused) ++size;
@ -119,9 +126,14 @@ PerformanceTimer::PerformanceTimer(const std::string &s, uint64_t unit, el::Leve
PerformanceTimer::~PerformanceTimer() PerformanceTimer::~PerformanceTimer()
{ {
performance_timers->pop_back();
if (!paused) if (!paused)
ticks = get_tick_count() - ticks; ticks = get_tick_count() - ticks;
}
LoggingPerformanceTimer::~LoggingPerformanceTimer()
{
pause();
performance_timers->pop_back();
char s[12]; char s[12];
snprintf(s, sizeof(s), "%8llu ", (unsigned long long)(ticks_to_ns(ticks) / (1000000000 / unit))); snprintf(s, sizeof(s), "%8llu ", (unsigned long long)(ticks_to_ns(ticks) / (1000000000 / unit)));
size_t size = 0; for (const auto *tmp: *performance_timers) if (!tmp->paused || tmp==this) ++size; size_t size = 0; for (const auto *tmp: *performance_timers) if (!tmp->paused || tmp==this) ++size;

View File

@ -43,30 +43,46 @@ class PerformanceTimer;
extern el::Level performance_timer_log_level; extern el::Level performance_timer_log_level;
uint64_t get_tick_count();
uint64_t get_ticks_per_ns();
uint64_t ticks_to_ns(uint64_t ticks);
class PerformanceTimer class PerformanceTimer
{ {
public: public:
PerformanceTimer(const std::string &s, uint64_t unit, el::Level l = el::Level::Debug); PerformanceTimer(bool paused = false);
~PerformanceTimer(); ~PerformanceTimer();
void pause(); void pause();
void resume(); void resume();
private: uint64_t value() const { return ticks; }
std::string name; void set(uint64_t v){ticks=v;}
uint64_t unit;
el::Level level; protected:
uint64_t ticks; uint64_t ticks;
bool started; bool started;
bool paused; bool paused;
}; };
class LoggingPerformanceTimer: public PerformanceTimer
{
public:
LoggingPerformanceTimer(const std::string &s, uint64_t unit, el::Level l = el::Level::Debug);
~LoggingPerformanceTimer();
private:
std::string name;
uint64_t unit;
el::Level level;
};
void set_performance_timer_log_level(el::Level level); void set_performance_timer_log_level(el::Level level);
#define PERF_TIMER_UNIT(name, unit) tools::PerformanceTimer pt_##name(#name, unit, tools::performance_timer_log_level) #define PERF_TIMER_UNIT(name, unit) tools::LoggingPerformanceTimer pt_##name(#name, unit, tools::performance_timer_log_level)
#define PERF_TIMER_UNIT_L(name, unit, l) tools::PerformanceTimer pt_##name(#name, unit, l) #define PERF_TIMER_UNIT_L(name, unit, l) tools::LoggingPerformanceTimer pt_##name(#name, unit, l)
#define PERF_TIMER(name) PERF_TIMER_UNIT(name, 1000) #define PERF_TIMER(name) PERF_TIMER_UNIT(name, 1000)
#define PERF_TIMER_L(name, l) PERF_TIMER_UNIT_L(name, 1000, l) #define PERF_TIMER_L(name, l) PERF_TIMER_UNIT_L(name, 1000, l)
#define PERF_TIMER_START_UNIT(name, unit) std::unique_ptr<tools::PerformanceTimer> pt_##name(new tools::PerformanceTimer(#name, unit, el::Level::Info)) #define PERF_TIMER_START_UNIT(name, unit) std::unique_ptr<tools::LoggingPerformanceTimer> pt_##name(new tools::LoggingPerformanceTimer(#name, unit, el::Level::Info))
#define PERF_TIMER_START(name) PERF_TIMER_START_UNIT(name, 1000) #define PERF_TIMER_START(name) PERF_TIMER_START_UNIT(name, 1000)
#define PERF_TIMER_STOP(name) do { pt_##name.reset(NULL); } while(0) #define PERF_TIMER_STOP(name) do { pt_##name.reset(NULL); } while(0)
#define PERF_TIMER_PAUSE(name) pt_##name->pause() #define PERF_TIMER_PAUSE(name) pt_##name->pause()