-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsc.c
86 lines (76 loc) · 1.5 KB
/
tsc.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
#define pr_fmt(fmt) "ambix.tsc: " fmt
/**
* @file tsc.c
* @author INESC-ID
* @date 26 jul 2023
* @version 2.2.0
* @brief Helper functions to convert clock cycles to seconds. Intended for
* the 5.10.0 linux kernel. Adapted from the code provided by ilia kuzmin
*/
#include <linux/delay.h>
#include "tsc.h"
static u64 _rdtsc_rate;
inline u64 tsc_rd(void)
{
u32 lo, hi;
__asm__ __volatile__("lfence;rdtsc" : "=a"(lo), "=d"(hi));
return (u64)hi << 32 | lo;
}
void tsc_init(void)
{
if (!_rdtsc_rate) {
const u64 tsc = tsc_rd();
msleep(1000);
_rdtsc_rate = tsc_rd() - tsc;
if (!_rdtsc_rate) {
_rdtsc_rate = 1; // for unsupported platforms
}
}
}
/**
* Convert an amount of clock cycles to msec
*
*/
u64 tsc_to_msec(u64 tsc)
{
if (!_rdtsc_rate) {
tsc_init();
}
return tsc / (_rdtsc_rate / 1000);
}
/**
* Convert an amount of clock cycles to msec
*
*/
u64 tsc_to_usec(u64 tsc)
{
if (!_rdtsc_rate) {
tsc_init();
}
// drop high 10 bit to avoid operation overflow
return ((tsc & 0x3FFFFFFFFFFFFFULL) * 1000) / (_rdtsc_rate / 1000);
}
/**
* Convert an amount of clock cycles to msec
*
*/
u64 tsc_to_nsec(u64 tsc)
{
if (!_rdtsc_rate) {
tsc_init();
}
// drop high 20 bit to avoid operation overflow
return ((tsc & 0xFFFFFFFFFFFULL) * 1000000) / (_rdtsc_rate / 1000);
}
/**
* Convert an amount of clock cycles to msec
*
*/
u64 tsc_from_usec(u64 usec)
{
if (!_rdtsc_rate) {
tsc_init();
}
return (usec * (_rdtsc_rate / 1000)) / 1000;
}