3 This file is part of libmspcore
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include "../core/except.h"
9 #include "../time/units.h"
22 void Profiler::set_period(unsigned p)
28 for(map<string, ScopeInfo>::iterator i=scopes.begin(); i!=scopes.end(); ++i)
30 ScopeInfo &si = i->second;
34 si.history.assign(period, Time::zero);
39 void Profiler::add_scope(const std::string &name)
41 if(!scopes.count(name))
43 map<string, ScopeInfo>::iterator i = scopes.insert(map<string, ScopeInfo>::value_type(name, ScopeInfo())).first;
44 i->second.history.resize(period);
48 ProfilingScope *Profiler::enter(ProfilingScope *ps)
50 ProfilingScope *old = inner;
55 void Profiler::record(const string &scope_name, const string &parent, const Time::TimeDelta &time, const Time::TimeDelta &child_t)
57 map<string, ScopeInfo>::iterator i = scopes.find(scope_name);
60 i = scopes.insert(map<string, ScopeInfo>::value_type(scope_name, ScopeInfo())).first;
61 i->second.history.resize(period);
64 ScopeInfo &si = i->second;
66 ++si.called_from[parent];
67 si.total_time += time;
68 si.self_time += time-child_t;
71 si.avg_time += time/period-si.history[si.hist_pos]/period;
72 si.history[si.hist_pos++] = time;
73 if(si.hist_pos>=period)
74 si.hist_pos -= period;
77 si.avg_time = si.total_time/si.calls;
80 const Profiler::ScopeInfo &Profiler::scope(const string &sn) const
82 map<string, ScopeInfo>::const_iterator i = scopes.find(sn);
84 throw KeyError("Unknown scope");
90 Profiler::ScopeInfo::ScopeInfo():