1 #ifndef MSP_DEBUG_PROFILER_H_
2 #define MSP_DEBUG_PROFILER_H_
7 #include <msp/core/noncopyable.h>
8 #include <msp/time/timedelta.h>
9 #include <msp/time/timestamp.h>
17 A class for collecting timing data from a program. It's not as efficient as
18 external profilers, but allows profiling of custom scopes and retrieving the
19 profiling data at run time. An example usage could be showing realtime
20 performance statistics of a game or a simulation.
22 Data can be recorded by creating a ProfilingScope object in the scope to be
25 Note: This is not thread-safe. To profile multiple threads, create a separate
26 Profiler for each thread.
28 class Profiler: private NonCopyable
33 Msp::Time::TimeStamp entry_time;
34 Msp::Time::TimeDelta duration;
39 Time::TimeStamp first_call;
41 Time::TimeDelta total_time;
42 Time::TimeDelta self_time;
43 Time::TimeDelta avg_time;
44 float calls_per_sec = 0;
45 std::vector<CallInfo> history;
46 unsigned hist_pos = 0;
47 bool hist_full = false;
48 std::map<std::string, unsigned> called_from;
53 std::map<std::string, ScopeInfo> scopes;
54 ProfilingScope *inner = 0;
57 /** Sets the averaging period for timing data, measured in calls. Previous
58 average timings are cleared. */
59 void set_period(unsigned p);
61 /** Adds a scope without recording any calls to it. Useful if you might
62 need to access a scope before it has finished for the first time. */
63 void add_scope(const std::string &name);
65 /** Changes the recorded innermost scope pointer and returns the old one.
66 This is used by ProfilingScope to track child time and should not be called
68 ProfilingScope *enter(ProfilingScope *ps);
70 /** Records the data from a ProfilingScope. It is not useful to call this
72 void record(const ProfilingScope &);
74 /** Returns informations about a scope. */
75 const ScopeInfo &get_scope(const std::string &) const;