File: TimeProfiler.h

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (101 lines) | stat: -rwxr-xr-x 2,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef TIME_PROFILER_H
#define TIME_PROFILER_H

#include <string>
#include <map>
#include <boost/noncopyable.hpp>
#include <cstring>

#include "System/float3.h"

// disable this if you want minimal profiling
// (sim time is still measured because of game slowdown)
#define SCOPED_TIMER(name) ScopedTimer myScopedTimerFromMakro(name);


class BasicTimer : public boost::noncopyable
{
public:
	BasicTimer(const char* const name);

protected:
	const std::string name;
	const unsigned starttime;
};


/**
 * @brief Time profiling helper class
 *
 * Construct an instance of this class where you want to begin time measuring,
 * and destruct it at the end (or let it be autodestructed).
 */
class ScopedTimer : public BasicTimer
{
public:
	ScopedTimer(const char* const name, bool autoShow = false): BasicTimer(name) {
		autoShowGraph = autoShow;
	}
	/**
	 * @brief destroy and add time to profiler
	 */
	~ScopedTimer();

private:
	bool autoShowGraph;
};


class ScopedOnceTimer : public BasicTimer
{
public:
	ScopedOnceTimer(const char* const name): BasicTimer(name) {}
	ScopedOnceTimer(const std::string& name): BasicTimer(name.c_str()) {}
	/**
	 * @brief destroy and print passed time to infolog
	 */
	~ScopedOnceTimer();
};



class CTimeProfiler
{
public:
	struct TimeRecord {
		TimeRecord() : total(0), current(0), percent(0), color(0,0,0), showGraph(false), peak(0), newpeak(false) { 
			memset(frames, 0, sizeof(frames));
		}
		unsigned total;
		unsigned current;
		static const unsigned frames_size = 128;
		unsigned frames[frames_size];
		float percent;
		float3 color;
		bool showGraph;
		float peak;
		bool newpeak;
	};

	CTimeProfiler();
	~CTimeProfiler();

	float GetPercent(const char *name);
	void AddTime(const std::string& name, unsigned time, bool showGraph = false);
	void Update();

	void PrintProfilingInfo() const;

	std::map<std::string,TimeRecord> profile;

private:
	unsigned lastBigUpdate;
	/// increases each update, from 0 to (frames_size-1)
	unsigned currentPosition;
};

extern CTimeProfiler profiler;

#endif // TIME_PROFILER_H