]> git.tdb.fi Git - libs/core.git/commitdiff
Refactor Profiler
authorMikko Rasa <tdb@tdb.fi>
Sun, 16 Sep 2012 20:21:11 +0000 (23:21 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 16 Sep 2012 20:21:11 +0000 (23:21 +0300)
Have record() take in a ProfilingScope for easier transport of the
parameters.

Rename scope() to get_scope() since it provides read-only access.

source/debug/profiler.cpp
source/debug/profiler.h
source/debug/profilingscope.cpp
source/debug/profilingscope.h

index e3d98408d6d2244c47da824c8cf0fde3c0998c89..6cce271983022302a6a3820acf20e049d02d7143 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/core/maputils.h>
 #include <msp/time/units.h>
 #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<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;
        }
@@ -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);
 }
index 7f88dd2c099620189168ed8ed11645d385bed942..5ec8f3f1fcafa5dae8ebe4508a5b02abaeb7e1f3 100644 (file)
@@ -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
index bf3363de2fdce5d42d8f0e78655d35b3febea5c8..723529c9a4b7ccaa259fc14d6ce4696bac3cc472 100644 (file)
@@ -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);
 }
 
index fe9f38cecb84212f833f26e4d759303af3e9fe7c..107688886a931db5891049551e6aa1ddc2c2ecba 100644 (file)
@@ -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