summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pchrono.h
blob: 8eb2e64d358d339540ec0b9a7ec2d695fd010b50 (plain) (blame)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * pchrono.h
 *
 */

#ifndef PCHRONO_H_
#define PCHRONO_H_

#include "pconfig.h"
#include "ptypes.h"

#include <chrono>
//#include <cstdint>

namespace plib {
	namespace chrono {
		template <typename T>
		struct sys_ticks
		{
			using type = typename T::rep;
			static inline constexpr type start() noexcept { return T::now().time_since_epoch().count(); }
			static inline constexpr type stop() noexcept { return T::now().time_since_epoch().count(); }
			static inline constexpr type per_second() noexcept { return T::period::den / T::period::num; }
		};

		using hires_ticks = sys_ticks<std::chrono::high_resolution_clock>;
		using steady_ticks = sys_ticks<std::chrono::steady_clock>;
		using system_ticks = sys_ticks<std::chrono::system_clock>;

		#if defined(__x86_64__) &&  !defined(_clang__) && !defined(_MSC_VER) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6))

		template <typename T, typename R>
		struct base_ticks
		{
			using ret_type = R;
			static ret_type per_second() noexcept
			{
				static ret_type persec = 0;
				if (persec == 0)
				{
					ret_type x = 0;
					system_ticks::type t = system_ticks::start();
					system_ticks::type e;
					x = - T :: start();
					do {
						e = system_ticks::stop();
					} while (e - t < system_ticks::per_second() / 100 );
					x += T :: stop();
					persec = (ret_type)(double)((double) x * (double) system_ticks::per_second() / double (e - t));
				}
				return persec;
			}
		};


		#if PHAS_RDTSCP
		struct fast_ticks : public base_ticks<fast_ticks, int64_t>
		{
			typedef int64_t type;
			static inline type start() noexcept
			{
				int64_t v;
				__asm__ __volatile__ (
						"rdtscp;"
						"shl $32, %%rdx;"
						"or %%rdx, %%rax;"
						: "=a"(v)           /* outputs  */
						:                   /* inputs   */
						: "%rcx", "%rdx"    /* clobbers */
				);
				return v;
			}
			static inline type stop() noexcept
			{
				return start();
			}
		};

		#else
		struct fast_ticks : public base_ticks<fast_ticks, int64_t>
		{
			typedef int64_t type;
			static inline type start() noexcept
			{
				int64_t v;
				__asm__ __volatile__ (
						"rdtsc;"
						"shl $32, %%rdx;"
						"or %%rdx, %%rax;"
						: "=a"(v)   /* outputs  */
						:           /* inputs   */
						: "%rdx"    /* clobbers */
				);
				return v;
			}
			static inline type stop() noexcept
			{
				return start();
			}
		};

		#endif


		/* Based on "How to Benchmark Code Execution Times on Intel?? IA-32 and IA-64
		 * Instruction Set Architectures", Intel, 2010
		 *
		 */
		#if PUSE_ACCURATE_STATS && PHAS_RDTSCP
		/*
		 * kills performance completely, but is accurate
		 * cpuid serializes, but clobbers ebx and ecx
		 *
		 */

		struct exact_ticks : public base_ticks<exact_ticks, int64_t>
		{
			typedef int64_t type;

			static inline type start() noexcept
			{
				int64_t v;
				__asm__ __volatile__ (
						"cpuid;"
						//"xor %%eax, %%eax\n\t"
						"rdtsc;"
						"shl $32, %%rdx;"
						"or %%rdx, %%rax;"
						: "=a"(v) /* outputs */
						: "a"(0x0)                /* inputs */
						: "%ebx", "%ecx", "%rdx"  /* clobbers*/
				);
				return v;
			}
			static inline type stop() noexcept
			{
				int64_t v;
				__asm__ __volatile__ (
						"rdtscp;"
						"shl $32, %%rdx;"
						"or %%rax, %%rdx;"
						"mov %%rdx, %%r10;"
						"xor %%eax, %%eax\n\t"
						"cpuid;"
						"mov %%r10, %%rax;"
						: "=a" (v)
						:
						: "%ebx", "%ecx", "%rdx", "%r10"
				);
				return v;
			}
		};
		#else
		using exact_ticks = fast_ticks;
		#endif


		#else
		using fast_ticks = hires_ticks;
		using exact_ticks = fast_ticks;
		#endif

		template<bool enabled_>
		struct counter
		{
			counter() : m_count(0) { }
			using type = uint_least64_t;
			type operator()() const noexcept { return m_count; }
			void inc() noexcept { ++m_count; }
			void reset() noexcept { m_count = 0; }
			constexpr static bool enabled = enabled_;
		private:
			type m_count;
		};

		template<>
		struct counter<false>
		{
			using type = uint_least64_t;
			constexpr type operator()() const noexcept { return 0; }
			void inc() const noexcept { }
			void reset() const noexcept { }
			constexpr static bool enabled = false;
		};


		template< typename T, bool enabled_ = true>
		struct timer
		{
			using type = typename T::type;
			using ctype = uint_least64_t;
			constexpr static bool enabled = enabled_;

			struct guard_t
			{
				guard_t() = delete;
				guard_t(timer &m) noexcept : m_m(m) { m_m.m_time -= T::start(); }
				~guard_t() { m_m.m_time += T::stop(); ++m_m.m_count; }

				COPYASSIGNMOVE(guard_t, default)

			private:
				timer &m_m;
			};

			friend struct guard_t;

			timer() : m_time(0), m_count(0) { }

			type operator()() const { return m_time; }

			void reset() { m_time = 0; m_count = 0; }
			type average() const noexcept { return (m_count == 0) ? 0 : m_time / m_count; }
			type total() const noexcept { return m_time; }
			ctype count() const noexcept { return m_count; }

			double as_seconds() const noexcept { return static_cast<double>(total())
					/ static_cast<double>(T::per_second()); }

			guard_t guard() noexcept { return guard_t(*this); }
		private:
			type m_time;
			ctype m_count;
		};

		template<typename T>
		struct timer<T, false>
		{
			using type = typename T::type;
			using ctype = uint_least64_t;

			struct guard_t
			{
				guard_t() = default;
				COPYASSIGNMOVE(guard_t, default)
				/* using default constructor will trigger warning on
				 * unused local variable.
				 */
				// NOLINTNEXTLINE(modernize-use-equals-default)
				~guard_t() { }
			};

			constexpr type operator()() const noexcept { return 0; }
			void reset() const noexcept { }
			constexpr type average() const noexcept { return 0; }
			constexpr type total() const noexcept { return 0; }
			constexpr ctype count() const noexcept { return 0; }
			constexpr double as_seconds() const noexcept { return 0.0; }
			constexpr static bool enabled = false;
			guard_t guard() { return guard_t(); }
		};

	} // namespace chrono
	//============================================================
	//  Performance tracking
	//============================================================

	template<bool enabled_>
	using pperftime_t = plib::chrono::timer<plib::chrono::exact_ticks, enabled_>;

	template<bool enabled_>
	using pperfcount_t = plib::chrono::counter<enabled_>;
} // namespace plib

#endif /* PCHRONO_H_ */