forked from rigtorp/c2clat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c2clat.cpp
148 lines (126 loc) · 3.45 KB
/
c2clat.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
// © 2020 Erik Rigtorp <erik@rigtorp.se>
// SPDX-License-Identifier: MIT
// Measure inter-core one-way data latency
//
// Build:
// g++ -O3 -DNDEBUG c2clat.cpp -o c2clat -pthread
//
// Plot results using gnuplot:
// $ c2clat -p | gnuplot -p
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <atomic>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <thread>
#include <vector>
void pinThread(int cpu) {
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
if (sched_setaffinity(0, sizeof(set), &set) == -1) {
perror("sched_setaffinity");
exit(1);
}
}
int main(int argc, char *argv[]) {
int nsamples = 1000;
bool plot = false;
int opt;
while ((opt = getopt(argc, argv, "ps:")) != -1) {
switch (opt) {
case 'p':
plot = true;
break;
case 's':
nsamples = std::stoi(optarg);
break;
default:
goto usage;
}
}
if (optind != argc) {
usage:
std::cerr << "c2clat 1.0.0 © 2020 Erik Rigtorp <erik@rigtorp.se>\n"
"usage: c2clat [-p] [-s number_of_samples]\n"
"\nPlot results using gnuplot:\n"
"c2clat -p | gnuplot -p\n";
exit(1);
}
cpu_set_t set;
CPU_ZERO(&set);
if (sched_getaffinity(0, sizeof(set), &set) == -1) {
perror("sched_getaffinity");
exit(1);
}
// enumerate available CPUs
std::vector<int> cpus;
for (int i = 0; i < CPU_SETSIZE; ++i) {
if (CPU_ISSET(i, &set)) {
cpus.push_back(i);
}
}
std::map<std::pair<int, int>, std::chrono::nanoseconds> data;
for (size_t i = 0; i < cpus.size(); ++i) {
for (size_t j = i + 1; j < cpus.size(); ++j) {
alignas(64) std::atomic<int> seq1 = {-1};
alignas(64) std::atomic<int> seq2 = {-1};
auto t = std::thread([&] {
pinThread(cpus[i]);
for (int m = 0; m < nsamples; ++m) {
for (int n = 0; n < 100; ++n) {
while (seq1.load(std::memory_order_acquire) != n)
;
seq2.store(n, std::memory_order_release);
}
}
});
std::chrono::nanoseconds rtt = std::chrono::nanoseconds::max();
pinThread(cpus[j]);
for (int m = 0; m < nsamples; ++m) {
seq1 = seq2 = -1;
auto ts1 = std::chrono::steady_clock::now();
for (int n = 0; n < 100; ++n) {
seq1.store(n, std::memory_order_release);
while (seq2.load(std::memory_order_acquire) != n)
;
}
auto ts2 = std::chrono::steady_clock::now();
rtt = std::min(rtt, ts2 - ts1);
}
t.join();
data[{i, j}] = rtt / 2 / 100;
data[{j, i}] = rtt / 2 / 100;
}
}
if (plot) {
std::cout
<< "set title \"Inter-core one-way data latency between CPU cores\"\n"
<< "set xlabel \"CPU\"\n"
<< "set ylabel \"CPU\"\n"
<< "set cblabel \"Latency (ns)\"\n"
<< "$data << EOD\n";
}
std::cout << std::setw(4) << "CPU";
for (size_t i = 0; i < cpus.size(); ++i) {
std::cout << " " << std::setw(4) << cpus[i];
}
std::cout << std::endl;
for (size_t i = 0; i < cpus.size(); ++i) {
std::cout << std::setw(4) << cpus[i];
for (size_t j = 0; j < cpus.size(); ++j) {
std::cout << " " << std::setw(4) << data[{i, j}].count();
}
std::cout << std::endl;
}
if (plot) {
std::cout << "EOD\n"
<< "plot '$data' matrix rowheaders columnheaders using 2:1:3 "
"with image\n";
}
return 0;
}