-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
116 lines (94 loc) · 3.35 KB
/
main.cpp
File metadata and controls
116 lines (94 loc) · 3.35 KB
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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdint>
#include <thread>
#include <atomic>
#include <x86intrin.h>
#include <new> // For hardware_destructive_interference_size()
std::atomic<bool> stop_noise(false);
// The "Noisy Neighbor" thread function
void noise_maker(long L3_size) {
const size_t noise_size = 2 * L3_size * 1024; // Value should exceed L3
std::vector<uint8_t> junk(noise_size, 0);
while (!stop_noise) {
for (size_t i = 0; i < noise_size; i += std::hardware_destructive_interference_size) { // Access every cache line
++junk[i];
}
}
}
struct CacheSpec {
std::string label;
long size_kb;
};
std::vector<CacheSpec> get_cpu_specs() {
std::vector<CacheSpec> specs;
for (int i = 0; i <= 4; ++i) {
std::string path = "/sys/devices/system/cpu/cpu0/cache/index" + std::to_string(i) + "/";
std::ifstream l_file(path + "level"), s_file(path + "size"), t_file(path + "type");
std::string level, size_str, type;
if (l_file >> level && s_file >> size_str && t_file >> type) {
char unit = size_str.back();
long size = std::stol(size_str.substr(0, size_str.size() - 1));
if (unit == 'M') size *= 1024;
specs.push_back({"L" + level + "-" + type, size});
}
}
return specs;
}
uint64_t get_latency(size_t kb) {
size_t bytes = kb * 1024;
size_t elements = bytes / sizeof(void*);
if (elements < 2) return 0;
if (elements < 4096) elements = 4096; // Minimum size for meaningful stride
std::vector<void*> buffer(elements);
// Large prime stride (2053) to defeat modern complex prefetchers
size_t stride = 2053;
for (size_t i = 0; i < elements; ++i) {
buffer[i] = (void*)&buffer[(i + stride) % elements];
}
void* ptr = buffer.data();
unsigned int ui;
// Longer warmup to ensure TLB is primed
for (int i = 0; i < 2000000; ++i) ptr = *(void**)ptr;
uint64_t start = __rdtscp(&ui);
for (int i = 0; i < 10000000; ++i) ptr = *(void**)ptr;
uint64_t end = __rdtscp(&ui);
if (ptr == nullptr) std::cout << " ";
return (end - start) / 10000000;
}
int main(int argc, char** argv) {
bool enable_noise = (argc > 1 && std::string(argv[1]) == "--noise");
auto specs = get_cpu_specs();
std::ofstream spec_out("cpu_specs.csv");
spec_out << "label,size_kb\n";
long L3_size_kb = 0;
for (auto& s : specs) {
spec_out << s.label << "," << s.size_kb << "\n";
if (s.label.find("L3") != std::string::npos) {
L3_size_kb = s.size_kb;
}
}
if (L3_size_kb == 0) {
std::cout << "[!] L3 cache size is unclear. Can't detect Noisy Neighbors. Stop." << std::endl;
return 0;
}
std::thread n_thread;
if (enable_noise) {
std::cout << "[!] Starting NOISY NEIGHBOR thread..." << std::endl;
n_thread = std::thread(noise_maker, L3_size_kb);
}
std::ofstream out("latency_data.csv");
out << "size_kb,latency\n";
for (size_t kb = 8; kb <= 10 * L3_size_kb; kb = static_cast<size_t>(kb * 1.15) + 1) {
uint64_t lat = get_latency(kb);
out << kb << "," << lat << "\n";
std::cout << "KB: " << kb << "\tLat: " << lat << std::endl;
}
if (enable_noise) {
stop_noise = true;
n_thread.join();
}
return 0;
}