1 #include <msp/core/maputils.h>
3 #include "profilingscope.h"
15 void Profiler::set_period(unsigned p)
21 for(map<string, ScopeInfo>::iterator i=scopes.begin(); i!=scopes.end(); ++i)
23 ScopeInfo &si = i->second;
27 si.history.assign(period, CallInfo());
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.first_call = scope.get_entry_time();
56 i->second.history.resize(period);
59 ScopeInfo &si = i->second;
61 if(scope.get_parent())
62 ++si.called_from[scope.get_parent()->get_name()];
63 si.total_time += scope.get_time_spent();
64 si.self_time += scope.get_time_spent()-scope.get_child_time();
67 si.avg_time += scope.get_time_spent()/period-si.history[si.hist_pos].duration/period;
69 CallInfo &ci = si.history[si.hist_pos];
70 Time::TimeStamp old_entry = ci.entry_time;
71 ci.entry_time = scope.get_entry_time();
72 ci.duration = scope.get_time_spent();
75 if(si.hist_pos>=period)
76 si.hist_pos -= period;
79 si.calls_per_sec = period*Time::sec/(scope.get_entry_time()-old_entry);
80 else if(si.hist_pos>1)
81 si.calls_per_sec = (si.hist_pos-1)*Time::sec/(scope.get_entry_time()-si.history.front().entry_time);
88 si.avg_time = si.total_time/si.calls;
90 si.calls_per_sec = (si.calls-1)*Time::sec/(scope.get_entry_time()-si.first_call);
94 const Profiler::ScopeInfo &Profiler::get_scope(const string &sn) const
96 return get_item(scopes, sn);
100 Profiler::ScopeInfo::ScopeInfo():