#include <msp/core/maputils.h>
#include <msp/time/units.h>
#include "profiler.h"
+#include "profilingscope.h"
using namespace std;
return old;
}
-void Profiler::record(const string &scope_name, const string &parent, const Time::TimeDelta &time, const Time::TimeDelta &child_t)
+void Profiler::record(const ProfilingScope &scope)
{
- map<string, ScopeInfo>::iterator i = scopes.find(scope_name);
+ map<string, ScopeInfo>::iterator i = scopes.find(scope.get_name());
if(i==scopes.end())
{
- i = scopes.insert(map<string, ScopeInfo>::value_type(scope_name, ScopeInfo())).first;
+ i = scopes.insert(map<string, ScopeInfo>::value_type(scope.get_name(), ScopeInfo())).first;
i->second.history.resize(period);
}
ScopeInfo &si = i->second;
++si.calls;
- ++si.called_from[parent];
- si.total_time += time;
- si.self_time += time-child_t;
+ if(scope.get_parent())
+ ++si.called_from[scope.get_parent()->get_name()];
+ si.total_time += scope.get_time_spent();
+ si.self_time += scope.get_time_spent()-scope.get_child_time();
if(period)
{
- si.avg_time += time/period-si.history[si.hist_pos]/period;
- si.history[si.hist_pos++] = time;
+ si.avg_time += scope.get_time_spent()/period-si.history[si.hist_pos]/period;
+ si.history[si.hist_pos++] = scope.get_time_spent();
if(si.hist_pos>=period)
si.hist_pos -= period;
}
si.avg_time = si.total_time/si.calls;
}
-const Profiler::ScopeInfo &Profiler::scope(const string &sn) const
+const Profiler::ScopeInfo &Profiler::get_scope(const string &sn) const
{
return get_item(scopes, sn);
}
*/
ProfilingScope *enter(ProfilingScope *ps);
- /**
- Records a call to a scope. You'll probably want to use a ProfilingScope
- instead of calling this manually.
-
- @param sn Scope name
- @param pn Parent scope name
- @param t Time spent in the scope
- @param ct Time spent in child scopes
- */
- void record(const std::string &sn, const std::string &pn, const Time::TimeDelta &t, const Time::TimeDelta &ct);
+ /** Records the data from a ProfilingScope. It is not useful to call this
+ manually. */
+ void record(const ProfilingScope &);
/**
Returns informations about a scope.
*/
- const ScopeInfo &scope(const std::string &) const;
+ const ScopeInfo &get_scope(const std::string &) const;
};
} // namespace Debug
profiler(p),
name(n),
parent(profiler.enter(this)),
- start_t(Time::now())
+ entry_time(Time::now())
{ }
ProfilingScope::~ProfilingScope()
{
- const Time::TimeDelta dt = Time::now()-start_t;
+ time_spent = Time::now()-entry_time;
if(parent)
- {
- parent->child_t += dt;
- profiler.record(name, parent->name, dt, child_t);
- }
- else
- profiler.record(name, string(), dt, child_t);
+ parent->child_time += get_time_spent();
+ profiler.record(*this);
profiler.enter(parent);
}
Profiler &profiler;
std::string name;
ProfilingScope *parent;
- Time::TimeStamp start_t;
- Time::TimeDelta child_t;
+ Time::TimeStamp entry_time;
+ Time::TimeDelta time_spent;
+ Time::TimeDelta child_time;
ProfilingScope(const ProfilingScope &);
ProfilingScope &operator=(const ProfilingScope &);
public:
ProfilingScope(Profiler &p, const std::string &n);
~ProfilingScope();
+
+ const std::string &get_name() const { return name; }
+ const ProfilingScope *get_parent() const { return parent; }
+ const Time::TimeStamp &get_entry_time() const { return entry_time; }
+ const Time::TimeDelta &get_time_spent() const { return time_spent; }
+ const Time::TimeDelta &get_child_time() const { return child_time; }
};
} // namespace Debug