-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCacheRTT.cpp
230 lines (182 loc) · 5.9 KB
/
CacheRTT.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
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
#include <cstdint>
#include <atomic>
#include <thread>
#include <stdio.h>
#include <chrono>
#include <pthread.h>
#include <sched.h>
#include <iostream>
#include <immintrin.h>
#ifdef TOPOLOGY
#include <cpuid.h>
#endif
std::atomic_bool keepRunning = true;
#define rdtsc __builtin_ia32_rdtsc
#ifdef PAUSE_NOP
#define pause() \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop"); \
asm("nop");
#else
#define pause() _mm_pause()
#endif
constexpr auto kCacheLineSize = 64;
struct
{
std::atomic<uint64_t> clientToServerChannel{0};
uint8_t padding1[kCacheLineSize * 8];
std::atomic<uint64_t> serverToClientChannel{0};
uint8_t padding2[kCacheLineSize * 8];
struct
{
int64_t offset;
uint64_t rtt;
} results[64];
uint16_t idx;
} data;
void client(uint16_t core)
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(core, &set);
pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, nullptr);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, nullptr);
uint64_t localTickReqSend, remoteTickReqReceive, remoteTickRespSend, localTickRespReceive, resp = 0;
while (keepRunning)
{
localTickReqSend = rdtsc();
data.clientToServerChannel.store(localTickReqSend);
uint64_t originalResp = resp;
while ((resp = data.serverToClientChannel.load(std::memory_order_seq_cst)) == originalResp)
{
pause();
}
localTickRespReceive = rdtsc();
auto upperBits = (localTickRespReceive & 0xffffffff00000000);
remoteTickRespSend = ((resp & 0xffffffff00000000) >> 32) | upperBits;
remoteTickReqReceive = (resp & 0x00000000ffffffff) | upperBits;
auto offset = (((int64_t)remoteTickReqReceive - (int64_t)localTickReqSend) + ((int64_t)remoteTickRespSend - (int64_t)localTickRespReceive)) / 2;
auto rtt = (localTickRespReceive - localTickReqSend) - (remoteTickRespSend - remoteTickReqReceive);
data.results[data.idx++ % 64] = typeof(data.results[0]){.offset = offset, .rtt = rtt};
}
}
void server(uint16_t core)
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(core, &set);
pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, nullptr);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, nullptr);
uint64_t localTickReqReceived, remoteTickReqSend = 0, remoteTickRespReceived, localTickRespSend;
while (keepRunning)
{
auto originalTick = remoteTickReqSend;
while ((remoteTickReqSend = data.clientToServerChannel.load()) == originalTick)
{
pause();
}
localTickReqReceived = rdtsc();
data.serverToClientChannel.store(((uint64_t)((uint32_t)(localTickRespSend = rdtsc())) << 32) | (uint64_t)(uint32_t)localTickReqReceived);
}
}
int main(int argc, char **argv)
{
int cores = atoi(argv[1]);
// Enumerate topology information at startup for later analysis
#ifdef TOPOLOGY
for (uint16_t idx = 0; idx < cores; idx++)
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(idx, &set);
pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
sched_yield();
unsigned int eax, ebx, ecx, edx;
uint16_t type[5];
uint16_t siblings[5];
uint16_t level = 0;
for (level = 0; level < 5; level++)
{
ecx = level;
__get_cpuid_count(0x1F /* v2 extended topology leaf */, level, &eax, &ebx, &ecx, &edx);
std::cout << " eax: " << eax << ", ebx: " << ebx << ", ecx: " << ecx << ", edx: " << edx << std::endl;
siblings[level] = ebx;
type[level] = (ecx >> 8) & 0xff;
if (type[level] == 0)
{
break;
}
}
std::cout << "CPU: " << idx;
for (uint16_t idx = 0; idx <= level; idx++)
{
std::cout << ", level, " << level << ", type: " << type[idx] << ", siblings: " << siblings[idx];
}
std::cout << std::endl;
}
#endif
return 0;
for (uint16_t idx = 0; idx < cores; idx++)
{
for (uint16_t jdx = 0; jdx < cores; jdx++)
{
if (idx == jdx)
{
continue;
}
keepRunning = true;
std::cerr << "Starting threads for " << idx << ", " << jdx << std::endl;
std::thread serverThread{[&]()
{
server(idx);
}};
std::thread clientThread{[&]()
{
client(jdx);
}};
std::this_thread::sleep_for(std::chrono::milliseconds{250});
std::cerr << "Cleaning up threads..." << std::endl;
keepRunning = false;
std::this_thread::sleep_for(std::chrono::milliseconds{25});
pthread_cancel(serverThread.native_handle());
pthread_cancel(clientThread.native_handle());
std::cerr << "Joining threads..." << std::endl;
if (serverThread.joinable())
{
serverThread.join();
}
if (clientThread.joinable())
{
clientThread.join();
}
for (uint16_t i = 0; i < 64; i++)
{
std::cout << idx << "," << jdx << "," << data.results[i].rtt << "," << data.results[i].offset << std::endl;
}
}
}
return 0;
}