-
Notifications
You must be signed in to change notification settings - Fork 4
/
clock.c
executable file
·277 lines (225 loc) · 6.5 KB
/
clock.c
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
268
269
270
271
272
273
274
275
276
277
/*
Copyright(c) 2010-2017 Intel Corporation.
Copyright(c) 2016-2018 Viosoft Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "clock.h"
#include <stdio.h>
#include <string.h>
#include <rte_cycles.h>
/* Calibrate TSC overhead by reading NB_READ times and take the smallest value.
Bigger values are caused by external influence and can be discarded. The best
estimate is the smallest read value. */
#define NB_READ 10000
uint32_t rdtsc_overhead;
uint32_t rdtsc_overhead_stats;
uint64_t thresh;
uint64_t tsc_hz;
/* calculate how much overhead is involved with calling rdtsc. This value has
to be taken into account where the time spent running a small piece of code
is measured */
static void init_tsc_overhead(void)
{
volatile uint32_t min_without_overhead = UINT32_MAX;
volatile uint32_t min_with_overhead = UINT32_MAX;
volatile uint32_t min_stats_overhead = UINT32_MAX;
volatile uint64_t start1, end1;
volatile uint64_t start2, end2;
for (uint32_t i = 0; i < NB_READ; ++i) {
start1 = rte_rdtsc();
end1 = rte_rdtsc();
start2 = rte_rdtsc();
end2 = rte_rdtsc();
end2 = rte_rdtsc();
if (min_without_overhead > end1 - start1) {
min_without_overhead = end1 - start1;
}
if (min_with_overhead > end2 - start2) {
min_with_overhead = end2 - start2;
}
}
rdtsc_overhead = min_with_overhead - min_without_overhead;
start1 = rte_rdtsc();
end1 = rte_rdtsc();
/* forbid the compiler to optimize this dummy variable */
volatile int dummy = 0;
for (uint32_t i = 0; i < NB_READ; ++i) {
start1 = rte_rdtsc();
dummy += 32;
end1 = rte_rdtsc();
if (min_stats_overhead > end2 - start2) {
min_stats_overhead = end1 - start1;
}
}
rdtsc_overhead_stats = rdtsc_overhead + min_stats_overhead - min_without_overhead;
}
void clock_init(void)
{
init_tsc_overhead();
tsc_hz = rte_get_tsc_hz();
thresh = UINT64_MAX/tsc_hz;
}
uint64_t str_to_tsc(const char *from)
{
const uint64_t hz = rte_get_tsc_hz();
uint64_t ret;
char str[16];
strncpy(str, from, sizeof(str));
char *frac = strchr(str, '.');
if (frac) {
*frac = 0;
frac++;
}
ret = hz * atoi(str);
if (!frac)
return ret;
uint64_t nsec = 0;
uint64_t multiplier = 100000000;
for (size_t i = 0; i < strlen(frac); ++i) {
nsec += (frac[i] - '0') * multiplier;
multiplier /= 10;
}
/* Wont overflow until CPU freq is ~18.44 GHz */
ret += hz * nsec/1000000000;
return ret;
}
uint64_t sec_to_tsc(uint64_t sec)
{
if (sec < UINT64_MAX/rte_get_tsc_hz())
return sec * rte_get_tsc_hz();
else
return UINT64_MAX;
}
uint64_t msec_to_tsc(uint64_t msec)
{
if (msec < UINT64_MAX/rte_get_tsc_hz())
return msec * rte_get_tsc_hz() / 1000;
else
return msec / 1000 * rte_get_tsc_hz();
}
uint64_t usec_to_tsc(uint64_t usec)
{
if (usec < UINT64_MAX/rte_get_tsc_hz())
return usec * rte_get_tsc_hz() / 1000000;
else
return usec / 1000000 * rte_get_tsc_hz();
}
uint64_t nsec_to_tsc(uint64_t nsec)
{
if (nsec < UINT64_MAX/rte_get_tsc_hz())
return nsec * rte_get_tsc_hz() / 1000000000;
else
return nsec / 1000000000 * rte_get_tsc_hz();
}
uint64_t tsc_to_msec(uint64_t tsc)
{
if (tsc < UINT64_MAX / 1000) {
return tsc * 1000 / rte_get_tsc_hz();
} else {
return tsc / (rte_get_tsc_hz() / 1000);
}
}
uint64_t tsc_to_usec(uint64_t tsc)
{
if (tsc < UINT64_MAX / 1000000) {
return tsc * 1000000 / rte_get_tsc_hz();
} else {
return tsc / (rte_get_tsc_hz() / 1000000);
}
}
uint64_t tsc_to_nsec(uint64_t tsc)
{
if (tsc < UINT64_MAX / 1000000000) {
return tsc * 1000000000 / rte_get_tsc_hz();
} else {
return tsc / (rte_get_tsc_hz() / 1000000000);
}
}
uint64_t tsc_to_sec(uint64_t tsc)
{
return tsc / rte_get_tsc_hz();
}
struct time_unit tsc_to_time_unit(uint64_t tsc)
{
struct time_unit ret;
uint64_t hz = rte_get_tsc_hz();
ret.sec = tsc/hz;
ret.nsec = (tsc - ret.sec*hz)*1000000000/hz;
return ret;
}
uint64_t time_unit_to_usec(struct time_unit *time_unit)
{
return time_unit->sec * 1000000 + time_unit->nsec/1000;
}
uint64_t time_unit_to_nsec(struct time_unit *time_unit)
{
return time_unit->sec * 1000000000 + time_unit->nsec;
}
int time_unit_cmp(struct time_unit *left, struct time_unit *right)
{
if (left->sec < right->sec)
return -1;
if (left->sec > right->sec)
return 1;
if (left->nsec < right->nsec)
return -1;
if (left->nsec > right->nsec)
return -1;
return 0;
}
uint64_t freq_to_tsc(uint64_t times_per_sec)
{
return rte_get_tsc_hz()/times_per_sec;
}
void tsc_to_tv(struct timeval *tv, const uint64_t tsc)
{
uint64_t hz = rte_get_tsc_hz();
uint64_t sec = tsc/hz;
tv->tv_sec = sec;
tv->tv_usec = ((tsc - sec * hz) * 1000000) / hz;
}
void tv_to_tsc(const struct timeval *tv, uint64_t *tsc)
{
uint64_t hz = rte_get_tsc_hz();
*tsc = tv->tv_sec * hz;
*tsc += tv->tv_usec * hz / 1000000;
}
struct timeval tv_diff(const struct timeval *cur, const struct timeval *next)
{
uint64_t sec, usec;
sec = next->tv_sec - cur->tv_sec;
if (next->tv_usec < cur->tv_usec) {
usec = next->tv_usec + 1000000 - cur->tv_usec;
sec -= 1;
}
else
usec = next->tv_usec - cur->tv_usec;
struct timeval ret = {
.tv_sec = sec,
.tv_usec = usec,
};
return ret;
}