-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
127 lines (108 loc) · 3.83 KB
/
main.cpp
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
#include <iostream>
#include <immintrin.h>
#include <x86intrin.h>
#include <emmintrin.h>
#include <chrono>
static inline void cpu_pause()
{
__asm__ volatile ("pause" ::: "memory");
}
static inline uint32_t read_tsc_lo(void)
{
uint32_t hi, lo;
asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
return lo;
}
static inline unsigned long read_tsc(void)
{
unsigned long var;
unsigned int hi, lo;
asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
var = ((unsigned long long int) hi << 32) | lo;
return var;
}
static inline uint64_t inject_cycle(uint64_t inject_cycle_num){
uint64_t start_c, end_c;
uint64_t temp_c;
start_c = _rdtsc();
temp_c = start_c + inject_cycle_num;
while((end_c = _rdtsc()) < temp_c) cpu_pause();
return end_c-start_c;
}
int main(){
uint64_t cpu_freq_mhz = 2300;
// int repeat_count = 1;
int repeat_count = 10000;
uint64_t inject_count = 500;
uint64_t inject_latencys[500];
uint64_t inject_cycles[500];
for (int i = 0 ;i < inject_count; i++){
inject_latencys[i] = i;
inject_cycles[i] = inject_latencys[i]*cpu_freq_mhz/1000;
}
// for (int i = 0; i < inject_count; i++){
// {
// // inject_cycles[i]
// uint64_t cycles_sum = 0;
// for (int j = 0; j < repeat_count; j++){
// // volatile uint64_t start_c, end_c;
// // volatile uint64_t temp_c;
// uint64_t start_c, end_c;
// uint64_t temp_c;
// // inject here !!
// start_c = _rdtsc();
// temp_c = start_c + inject_cycles[i];
// while((end_c = _rdtsc()) < temp_c) _mm_pause();
// cycles_sum += end_c - start_c;
// }
// std::cout << inject_latencys[i] << "," << inject_cycles[i] << "," << cycles_sum/repeat_count << std::endl;
// }
// }
for (int i = 0; i < inject_count; i++){
{
// inject_cycles[i]
uint64_t cycles_sum = 0;
for (int j = 0; j < repeat_count; j++){
// volatile uint64_t start_c, end_c;
// volatile uint64_t temp_c;
uint64_t start_c, end_c;
uint64_t temp_c;
// inject here !!
start_c = _rdtsc();
temp_c = start_c + inject_cycles[i];
while((end_c = _rdtsc()) < temp_c) cpu_pause();
cycles_sum += end_c - start_c;
}
std::cout << inject_latencys[i] << "," << inject_cycles[i] << "," << cycles_sum/repeat_count << std::endl;
}
}
// for (int i = 0; i < inject_count; i++){
// {
// // inject_cycles[i]
// uint64_t cycles_sum = 0;
// for (int j = 0; j < repeat_count; j++){
// uint64_t start_c, end_c;
// uint64_t temp_c;
// // inject here !!
// start_c = read_tsc();
// temp_c = start_c + inject_cycles[i];
// while((end_c = read_tsc()) < temp_c) cpu_pause();
// cycles_sum += end_c - start_c;
// }
// std::cout << inject_latencys[i] << "," << inject_cycles[i] << "," << cycles_sum/repeat_count << std::endl;
// }
// }
// for (int i = 0; i < inject_count; i++){
// {
// // inject_cycles[i]
// uint64_t cycles_sum = 0;
// for (int j = 0; j < repeat_count; j++){
// // inject here !!
// // a little slower than before
// uint64_t actual_cycle_num = inject_cycle(inject_cycles[i]);
// cycles_sum += actual_cycle_num;
// }
// std::cout << inject_latencys[i] << "," << inject_cycles[i] << "," << cycles_sum/repeat_count << std::endl;
// }
// }
}