summaryrefslogtreecommitdiffstats
path: root/libparc/parc/developer/parc_Timing.h
blob: bd5c334046b77f1354f8e95a5983bdf6a82ca45a (plain)
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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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.
 */

/**
 * @file parc_Timing.h
 * @ingroup developer
 * @brief Macros for timing code
 *
 * These macros allow the developer to measure time spent in sections of code.
 *    On Intel platforms (i386 or x86_64), the timing is done with the TSC counter, so it will be measured in CPU cycles.
 *    On non-Intel Linux platforms, it will be be done with the nano-second oscillator clock (CLOCK_MONOTONIC_RAW).
 *    On Darwin, it will use the nano-second SYSTEM_CLOCK.
 *    Otherwise, uses gettimeofday(), which will be micro-second timing.
 *
 * This set of headers will define several macros for timing:
 *    parcTiming_Init(prefix)
 *    parcTiming_Fini(prefix)
 *    parcTiming_Start(prefix)
 *    parcTiming_Stop(prefix)
 *    (uint64_t) parcTiming_Delta(prefix)
 *
 * The units returned from parcTiming_Delta() will be consistent, but not necessarily
 * related to wall clock time, real time, or any discernable time unit.  For example, they
 * may be in CPU instruction cycles or raw oscillator ticks or nano-seconds.
 *
 * These macros only work if the user defines PARCTIMING_ENABLE.  Otherwise, they do not
 * generate any instructions and parcTiming_Delta will always return 0.
 *
 * @code
 * #define PARCTIMING_ENABLE 1
 * #include <parc/developer/parc_Timing.h>
 *
 * void
 * foo(void)
 * {
 *    parcTiming_Init(_foo);
 *    // ... other stuff ..
 *    parcTiming_Start(_foo);
 *    // ... stuff to measure ...
 *    parcTiming_Stop(_foo);
 *
 *    uint64_t delta = parcTiming_Delta(_foo);
 *    parcTiming_Fini(_foo)
 * }
 * @endcode
 *
 */
#ifndef libparc_parc_Timing_h
#define libparc_parc_Timing_h

#if defined(PARCTIMING_ENABLE)
// begin platform detection
#if defined(__i386__) || defined(__x86_64__)
#define PARCTIMING_INTEL
#include <parc/developer/parc_TimingIntel.h>
#elif defined(__APPLE__)
#define PARCTIMING_DARWIN
#include <parc/developer/parc_TimingDarwin.h>
#elif defined(__linux__)
#define PARCTIMING_LINUX
#include <parc/developer/parc_TimingLinux.h>
#else
#define PARCTIMING_GENERIC
#include <parc/developer/parc_TimingGeneric.h>
#endif // platform detection
#else // PARCTIMING_ENABLE
#define _private_parcTiming_Init(prefix)
#define _private_parcTiming_Start(prefix)
#define _private_parcTiming_Stop(prefix)
#define _private_parcTiming_Delta(prefix) ((uint64_t) 0)
#define _private_parcTiming_Fini(prefix)
#endif // PARCTIMING_ENABLE

/**
 * Initialize the timing facility for namespace prefix `prefix`
 *
 * Used inside a code block, this macro will define several variables prefixed with
 * the name `prefix`.  It may also allocate memory or system resources.  It must be used
 * within a function scope, not at a global scope.
 *
 * You must use parcTiming_Fini(prefix) when done with the timing facility.
 *
 * If `PARCTIMING_ENABLE` is not defined before including the header file, this function is a no-op.
 *
 * @param [in] prefix The namespace prefix used for variables in scope.
 *
 * Example:
 * @code
 * #define PARCTIMING_ENABLE 1
 * #include <parc/developer/parc_Timing.h>
 *
 * void
 * foo(void)
 * {
 *    parcTiming_Init(_foo);
 *    parcTiming_Start(_foo);
 *    // ... stuff to measure ...
 *    parcTiming_Stop(_foo);
 *
 *    uint64_t delta = parcTiming_Delta(_foo);
 *    parcTiming_Fini(_foo)
 * }
 * @endcode
 */
#define parcTiming_Init(prefix) _private_parcTiming_Init(prefix)

/**
 * Marks the time in the start variable
 *
 * Records the current time in the start variable for use by parcTiming_Delta().
 *
 * If PARCTIMING_ENABLE is not defined before including the header file, this function is a no-op.
 *
 * @param [in] prefix The namespace prefix used for variables in scope.
 *
 * Example:
 * @code
 * #define PARCTIMING_ENABLE 1
 * #include <parc/developer/parc_Timing.h>
 *
 * void
 * foo(void)
 * {
 *    parcTiming_Init(_foo);
 *    parcTiming_Start(_foo);
 *    // ... stuff to measure ...
 *    parcTiming_Stop(_foo);
 *
 *    uint64_t delta = parcTiming_Delta(_foo);
 *    parcTiming_Fini(_foo)
 * }
 * @endcode
 */
#define parcTiming_Start(prefix) _private_parcTiming_Start(prefix)

/**
 * Marks the time in the stop variable
 *
 * Records the current time in the stop variable for use by parcTiming_Delta().
 *
 * If PARCTIMING_ENABLE is not defined before including the header file, this function is a no-op.
 *
 * @param [in] prefix The namespace prefix used for variables in scope.
 *
 * Example:
 * @code
 * #define PARCTIMING_ENABLE 1
 * #include <parc/developer/parc_Timing.h>
 *
 * void
 * foo(void)
 * {
 *    parcTiming_Init(_foo);
 *    parcTiming_Start(_foo);
 *    // ... stuff to measure ...
 *    parcTiming_Stop(_foo);
 *
 *    uint64_t delta = parcTiming_Delta(_foo);
 *    parcTiming_Fini(_foo)
 * }
 * @endcode
 */
#define parcTiming_Stop(prefix) _private_parcTiming_Stop(prefix)

/**
 * Returns the number of ticks between calls to start and stop.
 *
 * The delta will be in whatever units the best clock and provide.  It may be CPU cycles
 * or oscillator ticks, or nanos, or in the worst case micros.
 *
 * If PARCTIMING_ENABLE is not defined before including the header file, this function is a no-op.
 *
 * @param [in] prefix The namespace prefix used for variables in scope.
 *
 * @return uint64_t The number of ticks between start and stop
 *
 * Example:
 * @code
 * #define PARCTIMING_ENABLE 1
 * #include <parc/developer/parc_Timing.h>
 *
 * void
 * foo(void)
 * {
 *    parcTiming_Init(_foo);
 *    parcTiming_Start(_foo);
 *    // ... stuff to measure ...
 *    parcTiming_Stop(_foo);
 *
 *    uint64_t delta = parcTiming_Delta(_foo);
 *    parcTiming_Fini(_foo)
 * }
 * @endcode
 */
#define parcTiming_Delta(prefix) _private_parcTiming_Delta(prefix)

/**
 * Finalized the timing, releasing any system resources or memory
 *
 * Must be called when done with each namespace initialized by parcTiming_Init().
 *
 * If PARCTIMING_ENABLE is not defined before including the header file, this function is a no-op.
 *
 * @param [in] prefix The namespace prefix used for variables in scope.
 *
 * Example:
 * @code
 * #define PARCTIMING_ENABLE 1
 * #include <parc/developer/parc_Timing.h>
 *
 * void
 * foo(void)
 * {
 *    parcTiming_Init(_foo);
 *    parcTiming_Start(_foo);
 *    // ... stuff to measure ...
 *    parcTiming_Stop(_foo);
 *
 *    uint64_t delta = parcTiming_Delta(_foo);
 *    parcTiming_Fini(_foo)
 * }
 * @endcode
 */
#define parcTiming_Fini(prefix) _private_parcTiming_Fini(prefix)
#endif // libparc_parc_Timing_h