-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench.cc
323 lines (283 loc) · 7.3 KB
/
bench.cc
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <cstdint>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
#include <thread>
#include <atomic>
#include <random>
#include <mutex>
#include <numa.h>
#include <sched.h>
#include <getopt.h>
#include "timer.hh"
// hardcoded for x86
#define CACHELINE 64
// allocate 512MB per thread- this makes the data not fit in L1/L2/L3 caches.
// for a 30MB L3 cache, only 6% of *each* thread's data can fit completely in
// the cache
#define WORKING_SET_BYTES (512 * (1<<20))
#define NOPS 10000000
using namespace std;
static atomic<int> g_ctr(0); // set by main()
static atomic<bool> g_go(false); // set by main()
static char *g_slab_px = nullptr;
static int g_verbose = 0;
template <typename T>
static string
hexify(const T &t)
{
ostringstream buf;
buf << hex << t;
return buf.str();
}
static vector<int>
range(int n)
{
vector<int> ret;
for (int i = 0; i < n; i++)
ret.push_back(i);
return ret;
}
// pin to specific cpu
static void
pin_to_cpu(int cpu)
{
auto ncpus = numa_num_task_cpus();
assert(CPU_SETSIZE >= ncpus); // lazy
cpu_set_t cs;
CPU_ZERO(&cs);
CPU_SET(cpu, &cs);
assert(CPU_COUNT(&cs) == 1);
auto ret = sched_setaffinity(0, sizeof(cs), &cs);
if (ret) {
perror("sched_setaffinity");
assert(false);
}
ret = sched_yield();
if (ret)
assert(false);
}
// pin to the numa node of the cpu
static void
pin_to_node(int cpu)
{
auto node = numa_node_of_cpu(cpu);
// pin to node
auto ret = numa_run_on_node(node);
if (ret)
assert(false);
// is numa_run_on_node() guaranteed to take effect immediately?
ret = sched_yield();
if (ret)
assert(false);
}
static void *
numa_alloc(size_t bytes, int node)
{
void *p = numa_alloc_onnode(bytes, node);
assert(p);
// force the OS to allocate physical memory for the region
memset(p, 0, bytes);
return p;
}
static void *
regular_alloc(size_t bytes, int node)
{
void *p = malloc(bytes);
assert(p);
// force the OS to allocate physical memory for the region
memset(p, 0, bytes);
return p;
}
static void *
large_slab_alloc(size_t bytes, int node)
{
static mutex s_mutex;
lock_guard<mutex> l(s_mutex);
// XXX: assumes that the slab is big enough for now
void *p = g_slab_px;
g_slab_px += bytes;
return p;
}
enum {
CONFIG_PIN_NONE,
CONFIG_PIN_NODE,
CONFIG_PIN_CPU,
};
enum {
CONFIG_ALLOC_ONCE,
CONFIG_ALLOC_PER_THREAD,
CONFIG_ALLOC_NUMA,
};
static void
work_main(int PinPolicy, int AllocPolicy, int cpu)
{
auto node = numa_node_of_cpu(cpu);
// pin to cpu/node
switch (PinPolicy) {
case CONFIG_PIN_NONE:
break;
case CONFIG_PIN_NODE:
pin_to_node(cpu);
break;
case CONFIG_PIN_CPU:
pin_to_cpu(cpu);
break;
default:
assert(false);
break;
}
void *p = nullptr;
switch (AllocPolicy) {
case CONFIG_ALLOC_ONCE:
p = large_slab_alloc(WORKING_SET_BYTES, node);
break;
case CONFIG_ALLOC_PER_THREAD:
p = regular_alloc(WORKING_SET_BYTES, node);
break;
case CONFIG_ALLOC_NUMA:
p = numa_alloc(WORKING_SET_BYTES, node);
break;
default:
assert(false);
break;
}
default_random_engine gen;
uniform_int_distribution<unsigned> dist(0, WORKING_SET_BYTES/CACHELINE - 1);
--g_ctr;
while (!g_go.load())
;
for (unsigned i = 0; i < NOPS; i++) {
// pick a random cache line in the working set, do some useless RMW work on
// that cache-line
auto cl = dist(gen);
char *px = reinterpret_cast<char *>(p) + cl * CACHELINE;
for (unsigned j = 0; j < CACHELINE/sizeof(uint64_t); j++, px += sizeof(uint64_t)) {
uint64_t *u64px = reinterpret_cast<uint64_t *>(px);
*u64px = *u64px + 1;
}
}
// XXX: p is leaked, but we don't care
}
int
main(int argc, char **argv)
{
if (numa_available() < 0) {
cerr << "no numa API" << endl;
return 1;
}
numa_set_strict(1);
auto ncpus = numa_num_task_cpus();
assert(ncpus >= 1);
static int s_num_cpus = 1;
static int s_pin_policy = CONFIG_PIN_NONE;
static int s_alloc_policy = CONFIG_ALLOC_ONCE;
while (1) {
static struct option long_options[] =
{
{"verbose" , no_argument , &g_verbose , 1} ,
{"num-cpus" , required_argument , 0 , 'n'} ,
{"pin-policy" , required_argument , 0 , 'p'} ,
{"alloc-policy" , required_argument , 0 , 'a'} ,
{0, 0, 0, 0}
};
int option_index = 0;
int c = getopt_long(argc, argv, "p:a:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
if (long_options[option_index].flag != 0)
break;
abort();
break;
case 'n':
s_num_cpus = strtoul(optarg, nullptr, 10);
assert(s_num_cpus > 0 && s_num_cpus <= ncpus);
break;
case 'p':
{
const string sopt = optarg;
if (sopt == "none")
s_pin_policy = CONFIG_PIN_NONE;
else if (sopt == "node")
s_pin_policy = CONFIG_PIN_NODE;
else if (sopt == "cpu")
s_pin_policy = CONFIG_PIN_CPU;
else
assert(false);
}
break;
case 'a':
{
const string sopt = optarg;
if (sopt == "once")
s_alloc_policy = CONFIG_ALLOC_ONCE;
else if (sopt == "per-thread")
s_alloc_policy = CONFIG_ALLOC_PER_THREAD;
else if (sopt == "numa")
s_alloc_policy = CONFIG_ALLOC_NUMA;
else
assert(false);
}
break;
case '?':
/* getopt_long already printed an error message. */
exit(1);
default:
abort();
}
}
if (g_verbose) {
cout << "bench parameters:" << endl;
cout << " num_cpus: " << s_num_cpus << endl;
cout << " pin_policy: " << s_pin_policy << endl;
cout << " alloc_policy: " << s_alloc_policy << endl;
cout << "NUMA system info:" << endl;
bitmask *bm = numa_bitmask_alloc(ncpus);
auto nm = numa_max_node();
cout << " numa_num_task_cpus(): " << ncpus << endl;
cout << " numa_max_node(): " << nm << endl;
cout << " numa_pagesize(): " << numa_pagesize() << endl;
for (auto i : range(nm + 1)) {
auto nsize = numa_node_size(i, nullptr);
cout << " numa_node_size(" << i << "): " << nsize << endl;
}
for (auto i : range(nm + 1)) {
numa_node_to_cpus(i, bm);
cout << " numa_node_to_cpus(" << i << "):";
for (auto c : range(ncpus))
if (numa_bitmask_isbitset(bm, c))
cout << " " << c;
cout << endl;
}
numa_bitmask_free(bm);
}
// slab init
if (s_alloc_policy == CONFIG_ALLOC_ONCE) {
g_slab_px = (char *) malloc(WORKING_SET_BYTES * static_cast<size_t>(ncpus));
assert(g_slab_px); memset(g_slab_px, 0, WORKING_SET_BYTES * static_cast<size_t>(ncpus));
}
g_ctr.store(s_num_cpus);
vector<thread> thds;
for (auto i : range(s_num_cpus))
thds.emplace_back(work_main, s_pin_policy, s_alloc_policy, i);
while (g_ctr.load())
;
timer t;
g_go.store(true);
for (auto &t : thds)
t.join();
auto elasped_us = t.lap();
auto elasped_sec = static_cast<double>(elasped_us)/1000000.0;
auto throughput_per_core = static_cast<double>(NOPS) / elasped_sec;
if (g_verbose) {
cout << "results:" << endl;
cout << " elasped_sec : " << elasped_sec << endl;
cout << " throughput_per_core: " << throughput_per_core << " ops/sec/core" << endl;
} else {
cout << throughput_per_core << endl;
}
return 0;
}