#pragma once #include "lib/utils/bts_no_copy.h" #include "datetime.h" #include "lib/logging/logging.h" #include #include #if 0 // Change to 1 to enable tracing #define TIME_FUNCTION(MAX_US) static utils::LogAtExit __lae__(__FUNCTION__); \ utils::CBTimer timer(__lae__.sum, __lae__.max, __lae__.count, __lae__.slow_count, __FUNCTION__, __LINE__); #define LOG_IF_SLOW TIME_FUNCTION(1000) // 1 ms #else #define TIME_FUNCTION(MAX_MS) #define LOG_IF_SLOW #endif namespace bts { namespace utils { template class CBTimer { public: CBTimer(double& sum, double& max, double& count, double& slow_count, const char* fn, uint32_t line) : _time(tbb::tick_count::now()), _fn(fn), _line(line), _max(max), _sum(sum), _count(count), _slow_count(slow_count) {} ~CBTimer() { double us = (tbb::tick_count::now() - _time).seconds() * 1000 * 1000; _count++; _sum += us; _max = std::max(us, _max); if (us > T) { _slow_count++; LOG(Gen,Warn,"Slow callback at {}:{} {} us",_fn ,_line ,us ); } } private: BTS_NO_COPY(CBTimer); tbb::tick_count _time; const char* _fn; uint32_t _line; double& _max; double& _sum; double& _count; double& _slow_count; }; struct LogAtExit { LogAtExit(const char* fn) : fn(fn), sum(0), max(0), count(0), slow_count(0) { } const char* fn; double sum; double max; double count; double slow_count; ~LogAtExit() { LOG(Gen, Info, "{} -- average: {} max: {} count: {} slow_count: {}", (sum/count),max,count,slow_count); } private: BTS_NO_COPY(LogAtExit); }; }}//namespace