X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fdebug%2Fprofiler.cpp;h=65cc71ced0876d6863f7613d6f99561e6a82244b;hp=6cce271983022302a6a3820acf20e049d02d7143;hb=HEAD;hpb=27efc167cb10ee03c1d2a6711dd149d1093179c8 diff --git a/source/debug/profiler.cpp b/source/debug/profiler.cpp index 6cce271..65cc71c 100644 --- a/source/debug/profiler.cpp +++ b/source/debug/profiler.cpp @@ -1,5 +1,4 @@ #include -#include #include "profiler.h" #include "profilingscope.h" @@ -8,33 +7,29 @@ using namespace std; namespace Msp { namespace Debug { -Profiler::Profiler(): - period(0), - inner(0) -{ } - void Profiler::set_period(unsigned p) { if(p==period) return; period = p; - for(map::iterator i=scopes.begin(); i!=scopes.end(); ++i) + for(auto &kvp: scopes) { - ScopeInfo &si = i->second; + ScopeInfo &si = kvp.second; if(p==0) si.history.clear(); else - si.history.assign(period, Time::zero); + si.history.assign(period, CallInfo()); si.hist_pos = 0; + si.hist_full = false; } } -void Profiler::add_scope(const std::string &name) +void Profiler::add_scope(const string &name) { if(!scopes.count(name)) { - map::iterator i = scopes.insert(map::value_type(name, ScopeInfo())).first; + auto i = scopes.insert(make_pair(name, ScopeInfo())).first; i->second.history.resize(period); } } @@ -48,10 +43,11 @@ ProfilingScope *Profiler::enter(ProfilingScope *ps) void Profiler::record(const ProfilingScope &scope) { - map::iterator i = scopes.find(scope.get_name()); + auto i = scopes.find(scope.get_name()); if(i==scopes.end()) { - i = scopes.insert(map::value_type(scope.get_name(), ScopeInfo())).first; + i = scopes.insert(make_pair(scope.get_name(), ScopeInfo())).first; + i->second.first_call = scope.get_entry_time(); i->second.history.resize(period); } @@ -63,13 +59,31 @@ void Profiler::record(const ProfilingScope &scope) si.self_time += scope.get_time_spent()-scope.get_child_time(); if(period) { - si.avg_time += scope.get_time_spent()/period-si.history[si.hist_pos]/period; - si.history[si.hist_pos++] = scope.get_time_spent(); + si.avg_time += scope.get_time_spent()/period-si.history[si.hist_pos].duration/period; + + CallInfo &ci = si.history[si.hist_pos]; + Time::TimeStamp old_entry = ci.entry_time; + ci.entry_time = scope.get_entry_time(); + ci.duration = scope.get_time_spent(); + + ++si.hist_pos; if(si.hist_pos>=period) si.hist_pos -= period; + + if(si.hist_full) + si.calls_per_sec = period*Time::sec/(scope.get_entry_time()-old_entry); + else if(si.hist_pos>1) + si.calls_per_sec = (si.hist_pos-1)*Time::sec/(scope.get_entry_time()-si.history.front().entry_time); + + if(si.hist_pos==0) + si.hist_full = true; } else + { si.avg_time = si.total_time/si.calls; + if(si.calls>1) + si.calls_per_sec = (si.calls-1)*Time::sec/(scope.get_entry_time()-si.first_call); + } } const Profiler::ScopeInfo &Profiler::get_scope(const string &sn) const @@ -77,11 +91,5 @@ const Profiler::ScopeInfo &Profiler::get_scope(const string &sn) const return get_item(scopes, sn); } - -Profiler::ScopeInfo::ScopeInfo(): - calls(0), - hist_pos(0) -{ } - } // namespace Debug } // namespace Msp