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, Time::zero);
33 void Profiler::add_scope(const std::string &name)
35 if(!scopes.count(name))
37 map<string, ScopeInfo>::iterator i = scopes.insert(map<string, ScopeInfo>::value_type(name, ScopeInfo())).first;
38 i->second.history.resize(period);
42 ProfilingScope *Profiler::enter(ProfilingScope *ps)
44 ProfilingScope *old = inner;
49 void Profiler::record(const ProfilingScope &scope)
51 map<string, ScopeInfo>::iterator i = scopes.find(scope.get_name());
54 i = scopes.insert(map<string, ScopeInfo>::value_type(scope.get_name(), ScopeInfo())).first;
55 i->second.history.resize(period);
58 ScopeInfo &si = i->second;
60 if(scope.get_parent())
61 ++si.called_from[scope.get_parent()->get_name()];
62 si.total_time += scope.get_time_spent();
63 si.self_time += scope.get_time_spent()-scope.get_child_time();
66 si.avg_time += scope.get_time_spent()/period-si.history[si.hist_pos]/period;
67 si.history[si.hist_pos++] = scope.get_time_spent();
68 if(si.hist_pos>=period)
69 si.hist_pos -= period;
72 si.avg_time = si.total_time/si.calls;
75 const Profiler::ScopeInfo &Profiler::get_scope(const string &sn) const
77 return get_item(scopes, sn);
81 Profiler::ScopeInfo::ScopeInfo():