From: Mikko Rasa Date: Sun, 16 Sep 2012 20:21:11 +0000 (+0300) Subject: Refactor Profiler X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=27efc167cb10ee03c1d2a6711dd149d1093179c8;p=libs%2Fcore.git Refactor Profiler Have record() take in a ProfilingScope for easier transport of the parameters. Rename scope() to get_scope() since it provides read-only access. --- diff --git a/source/debug/profiler.cpp b/source/debug/profiler.cpp index e3d9840..6cce271 100644 --- a/source/debug/profiler.cpp +++ b/source/debug/profiler.cpp @@ -1,6 +1,7 @@ #include #include #include "profiler.h" +#include "profilingscope.h" using namespace std; @@ -45,24 +46,25 @@ ProfilingScope *Profiler::enter(ProfilingScope *ps) 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::iterator i = scopes.find(scope_name); + map::iterator i = scopes.find(scope.get_name()); if(i==scopes.end()) { - i = scopes.insert(map::value_type(scope_name, ScopeInfo())).first; + i = scopes.insert(map::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; } @@ -70,7 +72,7 @@ void Profiler::record(const string &scope_name, const string &parent, const Time 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); } diff --git a/source/debug/profiler.h b/source/debug/profiler.h index 7f88dd2..5ec8f3f 100644 --- a/source/debug/profiler.h +++ b/source/debug/profiler.h @@ -65,21 +65,14 @@ public: */ 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 diff --git a/source/debug/profilingscope.cpp b/source/debug/profilingscope.cpp index bf3363d..723529c 100644 --- a/source/debug/profilingscope.cpp +++ b/source/debug/profilingscope.cpp @@ -10,19 +10,15 @@ ProfilingScope::ProfilingScope(Profiler &p, const string &n): 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); } diff --git a/source/debug/profilingscope.h b/source/debug/profilingscope.h index fe9f38c..1076888 100644 --- a/source/debug/profilingscope.h +++ b/source/debug/profilingscope.h @@ -18,14 +18,21 @@ private: 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