File: TimeProfiler.h

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (139 lines) | stat: -rw-r--r-- 2,932 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef TIME_PROFILER_H
#define TIME_PROFILER_H

#include "System/Misc/SpringTime.h"
#include "System/float3.h"

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

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


class BasicTimer : public boost::noncopyable
{
public:
	BasicTimer(const std::string& myname);
	BasicTimer(const char* myname);

	const std::string& GetName() const;
	spring_time GetDuration() const;

protected:
	const unsigned hash;
	const spring_time starttime;
	std::map<int, std::string>::iterator nameIterator;
};


/**
 * @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 std::string& name, bool autoShow = false);
	ScopedTimer(const char* name, bool autoShow = false);
	~ScopedTimer();

private:
	const bool autoShowGraph;
	std::map<int, int>::iterator it;
};


class ScopedMtTimer : public BasicTimer
{
public:
	ScopedMtTimer(const std::string& name, bool autoShow = false);
	ScopedMtTimer(const char* name, bool autoShow = false);
	~ScopedMtTimer();

private:
	const bool autoShowGraph;
	std::map<int, int>::iterator it;
};



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



class CTimeProfiler
{
public:
	CTimeProfiler();
	~CTimeProfiler();

	static CTimeProfiler& GetInstance();

	float GetPercent(const char *name);
	void Update();

	void PrintProfilingInfo() const;

	void AddTime(const std::string& name, const spring_time time, const bool showGraph = false);

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

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

	std::vector<std::deque<std::pair<spring_time,spring_time>>> profileCore;

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

#define profiler (CTimeProfiler::GetInstance())

#endif // TIME_PROFILER_H