1 #include <msp/core/maputils.h>
3 #include "profilingscope.h"
10 void Profiler::set_period(unsigned p)
16 for(auto &kvp: scopes)
18 ScopeInfo &si = kvp.second;
22 si.history.assign(period, CallInfo());
28 void Profiler::add_scope(const string &name)
30 if(!scopes.count(name))
32 auto i = scopes.insert(make_pair(name, ScopeInfo())).first;
33 i->second.history.resize(period);
37 ProfilingScope *Profiler::enter(ProfilingScope *ps)
39 ProfilingScope *old = inner;
44 void Profiler::record(const ProfilingScope &scope)
46 auto i = scopes.find(scope.get_name());
49 i = scopes.insert(make_pair(scope.get_name(), ScopeInfo())).first;
50 i->second.first_call = scope.get_entry_time();
51 i->second.history.resize(period);
54 ScopeInfo &si = i->second;
56 if(scope.get_parent())
57 ++si.called_from[scope.get_parent()->get_name()];
58 si.total_time += scope.get_time_spent();
59 si.self_time += scope.get_time_spent()-scope.get_child_time();
62 si.avg_time += scope.get_time_spent()/period-si.history[si.hist_pos].duration/period;
64 CallInfo &ci = si.history[si.hist_pos];
65 Time::TimeStamp old_entry = ci.entry_time;
66 ci.entry_time = scope.get_entry_time();
67 ci.duration = scope.get_time_spent();
70 if(si.hist_pos>=period)
71 si.hist_pos -= period;
74 si.calls_per_sec = period*Time::sec/(scope.get_entry_time()-old_entry);
75 else if(si.hist_pos>1)
76 si.calls_per_sec = (si.hist_pos-1)*Time::sec/(scope.get_entry_time()-si.history.front().entry_time);
83 si.avg_time = si.total_time/si.calls;
85 si.calls_per_sec = (si.calls-1)*Time::sec/(scope.get_entry_time()-si.first_call);
89 const Profiler::ScopeInfo &Profiler::get_scope(const string &sn) const
91 return get_item(scopes, sn);