File: TimeProfiler.h

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (192 lines) | stat: -rw-r--r-- 4,095 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef TIME_PROFILER_H
#define TIME_PROFILER_H

#include <atomic>
#include <cstring> // memset
#include <string>
#include <deque>
#include <vector>

#include "System/Misc/SpringTime.h"
#include "System/Misc/NonCopyable.h"
#include "System/float3.h"
#include "System/UnorderedMap.hpp"

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


class BasicTimer : public spring::noncopyable
{
public:
	BasicTimer(const spring_time time): nameHash(0), startTime(time) {}
	BasicTimer(const std::string& timerName);
	BasicTimer(const char* timerName);

	const std::string& GetName() const { return name; }
	spring_time GetDuration() const;

protected:
	const unsigned nameHash;
	const spring_time startTime;

	std::string name;
};


/**
 * @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& timerName, bool _autoShowGraph = false, bool _specialTimer = false);
	ScopedTimer(const char* timerName, bool _autoShowGraph = false, bool _specialTimer = false);
	~ScopedTimer();

private:
	const bool autoShowGraph;
	const bool specialTimer;
};


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

private:
	const bool autoShowGraph;
};



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

	spring_time GetDuration() const;

protected:
	const spring_time startTime;

	std::string name;
};



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

	static CTimeProfiler& GetInstance();

	float GetPercent(const char* name) const;
	float GetPercentRaw(const char* name) const {
		// do not default-create keys, breaks resorting
		const auto it = profile.find(name);
		if (it != profile.end())
			return ((it->second).percent);
		return 0.0f;
	}

	void ResetState();
	void ToggleLock(bool lock);

	void Update();
	void UpdateRaw();

	void ResortProfilesRaw();
	void RefreshProfiles();
	void RefreshProfilesRaw();

	void SetEnabled(bool b) { enabled = b; }
	void PrintProfilingInfo() const;

	void AddTime(
		const std::string& name,
		const spring_time startTime,
		const spring_time deltaTime,
		const bool showGraph = false,
		const bool specialTimer = false,
		const bool threadTimer = false
	);
	void AddTimeRaw(
		const std::string& name,
		const spring_time startTime,
		const spring_time deltaTime,
		const bool showGraph,
		const bool threadTimer
	);

public:
	struct TimeRecord {
		TimeRecord()
		: total(0.0f)
		, current(0.0f)

		, maxLag(0.0f)
		, percent(0.0f)
		, peak(0.0f)

		, newPeak(false)
		, newLagPeak(false)
		, showGraph(false)
		{
			memset(frames, 0, sizeof(frames));
		}

		static constexpr unsigned numFrames = 128;

		spring_time total;
		spring_time current;
		spring_time frames[numFrames];

		float maxLag;
		float percent;
		float peak;

		float3 color;

		bool newPeak;
		bool newLagPeak;
		bool showGraph;
	};

	spring::unordered_map<std::string, TimeRecord> profile;

	std::vector< std::pair<std::string, TimeRecord> > sortedProfile;
	std::vector< std::deque< std::pair<spring_time, spring_time> > > threadProfile;

private:
	spring_time lastBigUpdate;

	/// increases each update, from 0 to (numFrames-1)
	unsigned currentPosition;
	unsigned resortProfiles;

	// if false, AddTime is a no-op for (almost) all timers
	std::atomic<bool> enabled;
};

#define profiler (CTimeProfiler::GetInstance())

#endif // TIME_PROFILER_H