-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathperf.h
More file actions
253 lines (197 loc) · 8.77 KB
/
perf.h
File metadata and controls
253 lines (197 loc) · 8.77 KB
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
/*
Copyright (c) 2005-2019 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __tbb_perf_h__
#define __tbb_perf_h__
#ifndef TBB_PERF_TYPEINFO
#define TBB_PERF_TYPEINFO 1
#endif
#if TBB_PERF_TYPEINFO
#include <typeinfo>
#define __TBB_PERF_TEST_CLASS_NAME(T) typeid(T).name()
#else /* !TBB_PERF_TYPEINFO */
#define __TBB_PERF_TEST_CLASS_NAME(T) NULL
#endif /* !TBB_PERF_TYPEINFO */
#include "tbb/tick_count.h"
// TODO: Fix build scripts to provide more reliable build phase identification means
#ifndef __TBB_PERF_API
#if _USRDLL
#if _MSC_VER
#define __TBB_PERF_API __declspec(dllexport)
#else /* !_MSC_VER */
#define __TBB_PERF_API
#endif /* !_MSC_VER */
#else /* !_USRDLL */
#if _MSC_VER
#define __TBB_PERF_API __declspec(dllimport)
#else /* !_MSC_VER */
#define __TBB_PERF_API
#endif /* !_MSC_VER */
#endif /* !_USRDLL */
#endif /* !__TBB_PERF_API */
#if _WIN32||_WIN64
namespace Perf {
typedef unsigned __int64 tick_t;
#if defined(_M_X64)
inline tick_t rdtsc () { return __rdtsc(); }
#elif _M_IX86
inline tick_t rdtsc () { __asm { rdtsc } }
#else
#error Unsupported ISA
#endif
} // namespace Perf
#elif __linux__ || __APPLE__
#include <stdint.h>
namespace Perf {
typedef uint64_t tick_t;
#if __x86_64__ || __i386__ || __i386
inline tick_t rdtsc () {
uint32_t lo, hi;
__asm__ __volatile__ ( "rdtsc" : "=a" (lo), "=d" (hi) );
return (tick_t)lo | ((tick_t)hi) << 32;
}
#else
#error Unsupported ISA
#endif
} // namespace Perf
#else
#error Unsupported OS
#endif /* OS */
__TBB_PERF_API extern int NumThreads,
MaxConcurrency,
NumCpus;
// Functions and global variables provided by the benchmarking framework
namespace Perf {
typedef double duration_t;
static const int MaxWorkloadNameLen = 64;
static const char* NoHistogram = (char*)-1;
static const char* DefaultHistogram = (char*)-2;
__TBB_PERF_API void AnchorFunc ( void* );
__TBB_PERF_API void AnchorFunc2 ( void*, void* );
//! Helper that can be used in the preprocess handler to clean caches
/** Cleaning caches is necessary to obtain reproducible results when a test
accesses significant ranges of memory. **/
__TBB_PERF_API void WipeCaches ();
//! Specifies the name to be used to designate the current workload in output
/** Should be used from Test::SetWorkload(). If necessary workload name will be
truncated to MaxWorkloadNameLen characters. **/
__TBB_PERF_API void SetWorkloadName( const char* format, ... );
class __TBB_PERF_API Test {
public:
virtual ~Test () {}
//! Struct used by tests running in multiple masters mode
struct ThreadInfo {
//! Zero based thread ID
int tid;
//! Pointer to test specific data
/** If used by the test, should be initialized by OnStartLocal(), and
finalized by OnFinishLocal(). **/
void* data;
};
////////////////////////////////////////////////////////////////////////////////
// Mandatory methods
//! Returns the number of workloads supported
virtual int NumWorkloads () = 0;
//! Set workload info for the subsequent calls to Run() and RunSerial()
/** This method can use global helper function Perf::SetWorkloadName() in order
to specify the name of the current workload, which will be used in output
to designate the workload. If SetWorkloadName is not called, workloadIndex
will be used for this purpose.
When testing task scheduler, make sure that this method does not trigger
its automatic initialization. **/
virtual void SetWorkload ( int workloadIndex ) = 0;
//! Test implementation
/** Called by the timing framework several times in a loop to achieve approx.
RunDuration time, and this loop is timed NumRuns times to collect statistics.
Argument ti specifies information about the master thread calling this method. **/
virtual void Run ( ThreadInfo& ti ) = 0;
////////////////////////////////////////////////////////////////////////////////
// Optional methods
//! Returns short title string to be used in the regular output to identify the test
/** Should uniquely identify the test among other ones in the given benchmark suite.
If not implemented, the test implementation class' RTTI name is used. **/
virtual const char* Name () { return NULL; };
//! Returns minimal number of master threads
/** Used for task scheduler tests only (when UseTbbScheduler option is specified
in session settings). **/
virtual int MinNumMasters () { return 1; }
//! Returns maximal number of master threads
/** Used for task scheduler tests only (when UseTbbScheduler option is specified
in session settings). **/
virtual int MaxNumMasters () { return 1; }
//! Executes serial workload equivalent to the one processed by Run()
/** Called by the timing framework several times in a loop to collect statistics. **/
virtual void RunSerial ( ThreadInfo& ti );
//! Invoked before each call to Run()
/** Can be used to preinitialize data necessary for the test, clean up
caches (see Perf::WipeCaches), etc.
In multiple masters mode this method is called on each thread. **/
virtual void OnStart ( ThreadInfo& ti );
//! Invoked after each call to Run()
/** Can be used to free resources allocated by OnStart().
Note that this method must work correctly independently of whether Run(),
RunSerial() or nothing is called between OnStart() and OnFinish().
In multiple masters mode this method is called on each thread. **/
virtual void OnFinish ( ThreadInfo& ti );
//! Functionality, the cost of which has to be factored out from timing results
/** Applies to both parallel and serial versions. **/
virtual void Baseline ( ThreadInfo& );
//! Returns description string to be used in the benchmark info/summary output
virtual const char* Description () { return NULL; }
//! Specifies if the histogram of individual run times in a series
/** If the method is not overridden, histogramName argument of TestMain is used. **/
virtual const char* HistogramName () { return DefaultHistogram; }
}; // class Test
namespace internal {
__TBB_PERF_API void RegisterTest ( Test*, const char* testClassName, bool takeOwnership );
}
template<class T>
void RegisterTest() { internal::RegisterTest( new T, __TBB_PERF_TEST_CLASS_NAME(T), true ); }
template<class T>
void RegisterTest( T& t ) { internal::RegisterTest( &t, __TBB_PERF_TEST_CLASS_NAME(T), false ); }
enum SessionOptions {
//! Use Test::RunSerial if present
UseBaseline = 0x01,
UseSerialBaseline = 0x02,
UseBaselines = UseBaseline | UseSerialBaseline,
UseTaskScheduler = 0x10,
UseAffinityModes = 0x20,
UseSmallestWorkloadOnly = 0x40
};
struct SessionSettings {
//! A combination of SessionOptions flags
uintptr_t my_opts;
//! Name of a file to store performance results
/** These results are duplicates of what is printed on the console. **/
const char* my_resFile;
//! Output destination for the histogram of individual run times in a series
/** If it is a string, the histogram is stored in a file with such name.
If it is NULL, the histogram is printed on the console. By default histograms
are suppressed.
The histogram is formatted as two column table:
"time bucket start" "number of tests in this bucket"
When this setting enables histogram generation, an individual test
can override it by implementing HistogramName method. **/
const char* my_histogramName;
SessionSettings ( uintptr_t opts = 0, const char* resFile = NULL, const char* histogram = NoHistogram )
: my_opts(opts)
, my_resFile(resFile)
, my_histogramName(histogram)
{}
}; // struct SessionSettings
//! Benchmarking session entry point
/** Executes all the individual tests registered previously by means of
RegisterTest<MycrotestImpl> **/
__TBB_PERF_API int TestMain( int argc, char* argv[],
const SessionSettings* defaultSettings = NULL );
} // namespace Perf
#endif /* __tbb_perf_h__ */