1 #include <msp/core/maputils.h>
2 #include <msp/time/units.h>
4 #include "profilingscope.h"
16 void Profiler::set_period(unsigned p)
22 for(map<string, ScopeInfo>::iterator i=scopes.begin(); i!=scopes.end(); ++i)
24 ScopeInfo &si = i->second;
28 si.history.assign(period, CallInfo());
34 void Profiler::add_scope(const std::string &name)
36 if(!scopes.count(name))
38 map<string, ScopeInfo>::iterator i = scopes.insert(map<string, ScopeInfo>::value_type(name, ScopeInfo())).first;
39 i->second.history.resize(period);
43 ProfilingScope *Profiler::enter(ProfilingScope *ps)
45 ProfilingScope *old = inner;
50 void Profiler::record(const ProfilingScope &scope)
52 map<string, ScopeInfo>::iterator i = scopes.find(scope.get_name());
55 i = scopes.insert(map<string, ScopeInfo>::value_type(scope.get_name(), ScopeInfo())).first;
56 i->second.first_call = scope.get_entry_time();
57 i->second.history.resize(period);
60 ScopeInfo &si = i->second;
62 if(scope.get_parent())
63 ++si.called_from[scope.get_parent()->get_name()];
64 si.total_time += scope.get_time_spent();
65 si.self_time += scope.get_time_spent()-scope.get_child_time();
68 si.avg_time += scope.get_time_spent()/period-si.history[si.hist_pos].duration/period;
70 CallInfo &ci = si.history[si.hist_pos];
71 Time::TimeStamp old_entry = ci.entry_time;
72 ci.entry_time = scope.get_entry_time();
73 ci.duration = scope.get_time_spent();
76 if(si.hist_pos>=period)
77 si.hist_pos -= period;
80 si.calls_per_sec = period*Time::sec/(scope.get_entry_time()-old_entry);
81 else if(si.hist_pos>1)
82 si.calls_per_sec = (si.hist_pos-1)*Time::sec/(scope.get_entry_time()-si.history.front().entry_time);
89 si.avg_time = si.total_time/si.calls;
91 si.calls_per_sec = (si.calls-1)*Time::sec/(scope.get_entry_time()-si.first_call);
95 const Profiler::ScopeInfo &Profiler::get_scope(const string &sn) const
97 return get_item(scopes, sn);
101 Profiler::ScopeInfo::ScopeInfo():