]> git.tdb.fi Git - libs/core.git/blobdiff - source/debug/profiler.h
Use nullptr instead of 0 for pointers
[libs/core.git] / source / debug / profiler.h
index f2d5812da30befafb53914168eeb4237c2966b1c..22d2c9a82c9fcb7df8545d16e7545d9ebb0720a3 100644 (file)
@@ -1,17 +1,12 @@
-/* $Id$
-
-This file is part of libmspcore
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_DEBUG_PROFILER_H_
 #define MSP_DEBUG_PROFILER_H_
 
 #include <map>
 #include <string>
 #include <vector>
-#include "../time/timedelta.h"
+#include <msp/core/noncopyable.h>
+#include <msp/time/timedelta.h>
+#include <msp/time/timestamp.h>
 
 namespace Msp {
 namespace Debug {
@@ -24,69 +19,60 @@ external profilers, but allows profiling of custom scopes and retrieving the
 profiling data at run time.  An example usage could be showing realtime
 performance statistics of a game or a simulation.
 
-See also class ProfilingScope.
+Data can be recorded by creating a ProfilingScope object in the scope to be
+profiled.
 
 Note: This is not thread-safe.  To profile multiple threads, create a separate
 Profiler for each thread.
 */
-class Profiler
+class Profiler: private NonCopyable
 {
 public:
+       struct CallInfo
+       {
+               Msp::Time::TimeStamp entry_time;
+               Msp::Time::TimeDelta duration;
+       };
+
        struct ScopeInfo
        {
-               unsigned calls;
+               Time::TimeStamp first_call;
+               unsigned calls = 0;
                Time::TimeDelta total_time;
                Time::TimeDelta self_time;
                Time::TimeDelta avg_time;
-               std::vector<Time::TimeDelta> history;
-               unsigned hist_pos;
+               float calls_per_sec = 0;
+               std::vector<CallInfo> history;
+               unsigned hist_pos = 0;
+               bool hist_full = false;
                std::map<std::string, unsigned> called_from;
-
-               ScopeInfo();
        };
 
 private:
-       unsigned period;
+       unsigned period = 0;
        std::map<std::string, ScopeInfo> scopes;
-       ProfilingScope *inner;
+       ProfilingScope *inner = nullptr;
 
 public:
-       Profiler();
-
-       /**
-       Sets the averaging period for timing data, measured in calls.  Previous
-       average timings are cleared.
-       */
+       /** Sets the averaging period for timing data, measured in calls.  Previous
+       average timings are cleared. */
        void set_period(unsigned p);
 
-       /**
-       Adds a scope without recording any calls to it.  Useful if you might need to
-       access a scope before it has finished for the first time.
-       */
+       /** Adds a scope without recording any calls to it.  Useful if you might
+       need to access a scope before it has finished for the first time. */
        void add_scope(const std::string &name);
 
-       /**
-       Changes the recorded innermost scope pointer and returns the old one.  This
-       is used by ProfilingScope to track child time and should not be called
-       manually.
-       */
+       /** Changes the recorded innermost scope pointer and returns the old one.
+       This is used by ProfilingScope to track child time and should not be called
+       manually. */
        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;
+       /** Returns informations about a scope. */
+       const ScopeInfo &get_scope(const std::string &) const;
 };
 
 } // namespace Debug