blob: 00e3a1ac1f33c71baeae06c5c19ce7baedfd4ea8 (
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
|
/* Copyright (c) 2015-2018, Linaro Limited
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <odp_posix_extensions.h>
#include <time.h>
#include <odp/api/hints.h>
#include <odp_debug_internal.h>
#include <odp/api/abi/cpu_time.h>
#define SEC_IN_NS 1000000000ULL
/* Measure TSC frequency. Frequency information registers are defined for x86,
* but those are often not enumerated. */
uint64_t _odp_cpu_global_time_freq(void)
{
struct timespec sleep, ts1, ts2;
uint64_t t1, t2, ts_nsec, cycles, hz;
int i;
uint64_t avg = 0;
int rounds = 3;
int warm_up = 1;
for (i = 0; i < rounds; i++) {
sleep.tv_sec = 0;
if (warm_up)
sleep.tv_nsec = SEC_IN_NS / 1000;
else
sleep.tv_nsec = SEC_IN_NS / 4;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts1)) {
_ODP_DBG("clock_gettime failed\n");
return 0;
}
t1 = _odp_cpu_global_time();
if (nanosleep(&sleep, NULL) < 0) {
_ODP_DBG("nanosleep failed\n");
return 0;
}
if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts2)) {
_ODP_DBG("clock_gettime failed\n");
return 0;
}
t2 = _odp_cpu_global_time();
ts_nsec = (ts2.tv_sec - ts1.tv_sec) * SEC_IN_NS;
ts_nsec += ts2.tv_nsec - ts1.tv_nsec;
cycles = t2 - t1;
hz = (cycles * SEC_IN_NS) / ts_nsec;
if (warm_up)
warm_up = 0;
else
avg += hz;
}
return avg / (rounds - 1);
}
|