1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * numa.c
4 *
5 * numa: Simulate NUMA-sensitive workload and measure their NUMA performance
6 */
7
8 #include <inttypes.h>
9
10 #include <subcmd/parse-options.h>
11 #include "../util/cloexec.h"
12
13 #include "bench.h"
14
15 #include <errno.h>
16 #include <sched.h>
17 #include <stdio.h>
18 #include <assert.h>
19 #include <malloc.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/time.h>
26 #include <sys/resource.h>
27 #include <sys/wait.h>
28 #include <sys/prctl.h>
29 #include <sys/types.h>
30 #include <linux/kernel.h>
31 #include <linux/time64.h>
32 #include <linux/numa.h>
33 #include <linux/zalloc.h>
34
35 #include "../util/header.h"
36 #include "../util/mutex.h"
37 #include <numa.h>
38 #include <numaif.h>
39
40 #ifndef RUSAGE_THREAD
41 # define RUSAGE_THREAD 1
42 #endif
43
44 /*
45 * Regular printout to the terminal, suppressed if -q is specified:
46 */
47 #define tprintf(x...) do { if (g && g->p.show_details >= 0) printf(x); } while (0)
48
49 /*
50 * Debug printf:
51 */
52 #undef dprintf
53 #define dprintf(x...) do { if (g && g->p.show_details >= 1) printf(x); } while (0)
54
55 struct thread_data {
56 int curr_cpu;
57 cpu_set_t *bind_cpumask;
58 int bind_node;
59 u8 *process_data;
60 int process_nr;
61 int thread_nr;
62 int task_nr;
63 unsigned int loops_done;
64 u64 val;
65 u64 runtime_ns;
66 u64 system_time_ns;
67 u64 user_time_ns;
68 double speed_gbs;
69 struct mutex *process_lock;
70 };
71
72 /* Parameters set by options: */
73
74 struct params {
75 /* Startup synchronization: */
76 bool serialize_startup;
77
78 /* Task hierarchy: */
79 int nr_proc;
80 int nr_threads;
81
82 /* Working set sizes: */
83 const char *mb_global_str;
84 const char *mb_proc_str;
85 const char *mb_proc_locked_str;
86 const char *mb_thread_str;
87
88 double mb_global;
89 double mb_proc;
90 double mb_proc_locked;
91 double mb_thread;
92
93 /* Access patterns to the working set: */
94 bool data_reads;
95 bool data_writes;
96 bool data_backwards;
97 bool data_zero_memset;
98 bool data_rand_walk;
99 u32 nr_loops;
100 u32 nr_secs;
101 u32 sleep_usecs;
102
103 /* Working set initialization: */
104 bool init_zero;
105 bool init_random;
106 bool init_cpu0;
107
108 /* Misc options: */
109 int show_details;
110 int run_all;
111 int thp;
112
113 long bytes_global;
114 long bytes_process;
115 long bytes_process_locked;
116 long bytes_thread;
117
118 int nr_tasks;
119 bool show_quiet;
120
121 bool show_convergence;
122 bool measure_convergence;
123
124 int perturb_secs;
125 int nr_cpus;
126 int nr_nodes;
127
128 /* Affinity options -C and -N: */
129 char *cpu_list_str;
130 char *node_list_str;
131 };
132
133
134 /* Global, read-writable area, accessible to all processes and threads: */
135
136 struct global_info {
137 u8 *data;
138
139 struct mutex startup_mutex;
140 struct cond startup_cond;
141 int nr_tasks_started;
142
143 struct mutex start_work_mutex;
144 struct cond start_work_cond;
145 int nr_tasks_working;
146 bool start_work;
147
148 struct mutex stop_work_mutex;
149 u64 bytes_done;
150
151 struct thread_data *threads;
152
153 /* Convergence latency measurement: */
154 bool all_converged;
155 bool stop_work;
156
157 int print_once;
158
159 struct params p;
160 };
161
162 static struct global_info *g = NULL;
163
164 static int parse_cpus_opt(const struct option *opt, const char *arg, int unset);
165 static int parse_nodes_opt(const struct option *opt, const char *arg, int unset);
166
167 struct params p0;
168
169 static const struct option options[] = {
170 OPT_INTEGER('p', "nr_proc" , &p0.nr_proc, "number of processes"),
171 OPT_INTEGER('t', "nr_threads" , &p0.nr_threads, "number of threads per process"),
172
173 OPT_STRING('G', "mb_global" , &p0.mb_global_str, "MB", "global memory (MBs)"),
174 OPT_STRING('P', "mb_proc" , &p0.mb_proc_str, "MB", "process memory (MBs)"),
175 OPT_STRING('L', "mb_proc_locked", &p0.mb_proc_locked_str,"MB", "process serialized/locked memory access (MBs), <= process_memory"),
176 OPT_STRING('T', "mb_thread" , &p0.mb_thread_str, "MB", "thread memory (MBs)"),
177
178 OPT_UINTEGER('l', "nr_loops" , &p0.nr_loops, "max number of loops to run (default: unlimited)"),
179 OPT_UINTEGER('s', "nr_secs" , &p0.nr_secs, "max number of seconds to run (default: 5 secs)"),
180 OPT_UINTEGER('u', "usleep" , &p0.sleep_usecs, "usecs to sleep per loop iteration"),
181
182 OPT_BOOLEAN('R', "data_reads" , &p0.data_reads, "access the data via reads (can be mixed with -W)"),
183 OPT_BOOLEAN('W', "data_writes" , &p0.data_writes, "access the data via writes (can be mixed with -R)"),
184 OPT_BOOLEAN('B', "data_backwards", &p0.data_backwards, "access the data backwards as well"),
185 OPT_BOOLEAN('Z', "data_zero_memset", &p0.data_zero_memset,"access the data via glibc bzero only"),
186 OPT_BOOLEAN('r', "data_rand_walk", &p0.data_rand_walk, "access the data with random (32bit LFSR) walk"),
187
188
189 OPT_BOOLEAN('z', "init_zero" , &p0.init_zero, "bzero the initial allocations"),
190 OPT_BOOLEAN('I', "init_random" , &p0.init_random, "randomize the contents of the initial allocations"),
191 OPT_BOOLEAN('0', "init_cpu0" , &p0.init_cpu0, "do the initial allocations on CPU#0"),
192 OPT_INTEGER('x', "perturb_secs", &p0.perturb_secs, "perturb thread 0/0 every X secs, to test convergence stability"),
193
194 OPT_INCR ('d', "show_details" , &p0.show_details, "Show details"),
195 OPT_INCR ('a', "all" , &p0.run_all, "Run all tests in the suite"),
196 OPT_INTEGER('H', "thp" , &p0.thp, "MADV_NOHUGEPAGE < 0 < MADV_HUGEPAGE"),
197 OPT_BOOLEAN('c', "show_convergence", &p0.show_convergence, "show convergence details, "
198 "convergence is reached when each process (all its threads) is running on a single NUMA node."),
199 OPT_BOOLEAN('m', "measure_convergence", &p0.measure_convergence, "measure convergence latency"),
200 OPT_BOOLEAN('q', "quiet" , &p0.show_quiet, "quiet mode"),
201 OPT_BOOLEAN('S', "serialize-startup", &p0.serialize_startup,"serialize thread startup"),
202
203 /* Special option string parsing callbacks: */
204 OPT_CALLBACK('C', "cpus", NULL, "cpu[,cpu2,...cpuN]",
205 "bind the first N tasks to these specific cpus (the rest is unbound)",
206 parse_cpus_opt),
207 OPT_CALLBACK('M', "memnodes", NULL, "node[,node2,...nodeN]",
208 "bind the first N tasks to these specific memory nodes (the rest is unbound)",
209 parse_nodes_opt),
210 OPT_END()
211 };
212
213 static const char * const bench_numa_usage[] = {
214 "perf bench numa <options>",
215 NULL
216 };
217
218 static const char * const numa_usage[] = {
219 "perf bench numa mem [<options>]",
220 NULL
221 };
222
223 /*
224 * To get number of numa nodes present.
225 */
nr_numa_nodes(void)226 static int nr_numa_nodes(void)
227 {
228 int i, nr_nodes = 0;
229
230 for (i = 0; i < g->p.nr_nodes; i++) {
231 if (numa_bitmask_isbitset(numa_nodes_ptr, i))
232 nr_nodes++;
233 }
234
235 return nr_nodes;
236 }
237
238 /*
239 * To check if given numa node is present.
240 */
is_node_present(int node)241 static int is_node_present(int node)
242 {
243 return numa_bitmask_isbitset(numa_nodes_ptr, node);
244 }
245
246 /*
247 * To check given numa node has cpus.
248 */
node_has_cpus(int node)249 static bool node_has_cpus(int node)
250 {
251 struct bitmask *cpumask = numa_allocate_cpumask();
252 bool ret = false; /* fall back to nocpus */
253 int cpu;
254
255 BUG_ON(!cpumask);
256 if (!numa_node_to_cpus(node, cpumask)) {
257 for (cpu = 0; cpu < (int)cpumask->size; cpu++) {
258 if (numa_bitmask_isbitset(cpumask, cpu)) {
259 ret = true;
260 break;
261 }
262 }
263 }
264 numa_free_cpumask(cpumask);
265
266 return ret;
267 }
268
bind_to_cpu(int target_cpu)269 static cpu_set_t *bind_to_cpu(int target_cpu)
270 {
271 int nrcpus = numa_num_possible_cpus();
272 cpu_set_t *orig_mask, *mask;
273 size_t size;
274
275 orig_mask = CPU_ALLOC(nrcpus);
276 BUG_ON(!orig_mask);
277 size = CPU_ALLOC_SIZE(nrcpus);
278 CPU_ZERO_S(size, orig_mask);
279
280 if (sched_getaffinity(0, size, orig_mask))
281 goto err_out;
282
283 mask = CPU_ALLOC(nrcpus);
284 if (!mask)
285 goto err_out;
286
287 CPU_ZERO_S(size, mask);
288
289 if (target_cpu == -1) {
290 int cpu;
291
292 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
293 CPU_SET_S(cpu, size, mask);
294 } else {
295 if (target_cpu < 0 || target_cpu >= g->p.nr_cpus)
296 goto err;
297
298 CPU_SET_S(target_cpu, size, mask);
299 }
300
301 if (sched_setaffinity(0, size, mask))
302 goto err;
303
304 return orig_mask;
305
306 err:
307 CPU_FREE(mask);
308 err_out:
309 CPU_FREE(orig_mask);
310
311 /* BUG_ON due to failure in allocation of orig_mask/mask */
312 BUG_ON(-1);
313 return NULL;
314 }
315
bind_to_node(int target_node)316 static cpu_set_t *bind_to_node(int target_node)
317 {
318 int nrcpus = numa_num_possible_cpus();
319 size_t size;
320 cpu_set_t *orig_mask, *mask;
321 int cpu;
322
323 orig_mask = CPU_ALLOC(nrcpus);
324 BUG_ON(!orig_mask);
325 size = CPU_ALLOC_SIZE(nrcpus);
326 CPU_ZERO_S(size, orig_mask);
327
328 if (sched_getaffinity(0, size, orig_mask))
329 goto err_out;
330
331 mask = CPU_ALLOC(nrcpus);
332 if (!mask)
333 goto err_out;
334
335 CPU_ZERO_S(size, mask);
336
337 if (target_node == NUMA_NO_NODE) {
338 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
339 CPU_SET_S(cpu, size, mask);
340 } else {
341 struct bitmask *cpumask = numa_allocate_cpumask();
342
343 if (!cpumask)
344 goto err;
345
346 if (!numa_node_to_cpus(target_node, cpumask)) {
347 for (cpu = 0; cpu < (int)cpumask->size; cpu++) {
348 if (numa_bitmask_isbitset(cpumask, cpu))
349 CPU_SET_S(cpu, size, mask);
350 }
351 }
352 numa_free_cpumask(cpumask);
353 }
354
355 if (sched_setaffinity(0, size, mask))
356 goto err;
357
358 return orig_mask;
359
360 err:
361 CPU_FREE(mask);
362 err_out:
363 CPU_FREE(orig_mask);
364
365 /* BUG_ON due to failure in allocation of orig_mask/mask */
366 BUG_ON(-1);
367 return NULL;
368 }
369
bind_to_cpumask(cpu_set_t * mask)370 static void bind_to_cpumask(cpu_set_t *mask)
371 {
372 int ret;
373 size_t size = CPU_ALLOC_SIZE(numa_num_possible_cpus());
374
375 ret = sched_setaffinity(0, size, mask);
376 if (ret) {
377 CPU_FREE(mask);
378 BUG_ON(ret);
379 }
380 }
381
mempol_restore(void)382 static void mempol_restore(void)
383 {
384 int ret;
385
386 ret = set_mempolicy(MPOL_DEFAULT, NULL, g->p.nr_nodes-1);
387
388 BUG_ON(ret);
389 }
390
bind_to_memnode(int node)391 static void bind_to_memnode(int node)
392 {
393 struct bitmask *node_mask;
394 int ret;
395
396 if (node == NUMA_NO_NODE)
397 return;
398
399 node_mask = numa_allocate_nodemask();
400 BUG_ON(!node_mask);
401
402 numa_bitmask_clearall(node_mask);
403 numa_bitmask_setbit(node_mask, node);
404
405 ret = set_mempolicy(MPOL_BIND, node_mask->maskp, node_mask->size + 1);
406 dprintf("binding to node %d, mask: %016lx => %d\n", node, *node_mask->maskp, ret);
407
408 numa_bitmask_free(node_mask);
409 BUG_ON(ret);
410 }
411
412 #define HPSIZE (2*1024*1024)
413
414 #define set_taskname(fmt...) \
415 do { \
416 char name[20]; \
417 \
418 snprintf(name, 20, fmt); \
419 prctl(PR_SET_NAME, name); \
420 } while (0)
421
alloc_data(ssize_t bytes0,int map_flags,int init_zero,int init_cpu0,int thp,int init_random)422 static u8 *alloc_data(ssize_t bytes0, int map_flags,
423 int init_zero, int init_cpu0, int thp, int init_random)
424 {
425 cpu_set_t *orig_mask = NULL;
426 ssize_t bytes;
427 u8 *buf;
428 int ret;
429
430 if (!bytes0)
431 return NULL;
432
433 /* Allocate and initialize all memory on CPU#0: */
434 if (init_cpu0) {
435 int node = numa_node_of_cpu(0);
436
437 orig_mask = bind_to_node(node);
438 bind_to_memnode(node);
439 }
440
441 bytes = bytes0 + HPSIZE;
442
443 buf = (void *)mmap(0, bytes, PROT_READ|PROT_WRITE, MAP_ANON|map_flags, -1, 0);
444 BUG_ON(buf == (void *)-1);
445
446 if (map_flags == MAP_PRIVATE) {
447 if (thp > 0) {
448 ret = madvise(buf, bytes, MADV_HUGEPAGE);
449 if (ret && !g->print_once) {
450 g->print_once = 1;
451 printf("WARNING: Could not enable THP - do: 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled'\n");
452 }
453 }
454 if (thp < 0) {
455 ret = madvise(buf, bytes, MADV_NOHUGEPAGE);
456 if (ret && !g->print_once) {
457 g->print_once = 1;
458 printf("WARNING: Could not disable THP: run a CONFIG_TRANSPARENT_HUGEPAGE kernel?\n");
459 }
460 }
461 }
462
463 if (init_zero) {
464 bzero(buf, bytes);
465 } else {
466 /* Initialize random contents, different in each word: */
467 if (init_random) {
468 u64 *wbuf = (void *)buf;
469 long off = rand();
470 long i;
471
472 for (i = 0; i < bytes/8; i++)
473 wbuf[i] = i + off;
474 }
475 }
476
477 /* Align to 2MB boundary: */
478 buf = (void *)(((unsigned long)buf + HPSIZE-1) & ~(HPSIZE-1));
479
480 /* Restore affinity: */
481 if (init_cpu0) {
482 bind_to_cpumask(orig_mask);
483 CPU_FREE(orig_mask);
484 mempol_restore();
485 }
486
487 return buf;
488 }
489
free_data(void * data,ssize_t bytes)490 static void free_data(void *data, ssize_t bytes)
491 {
492 int ret;
493
494 if (!data)
495 return;
496
497 ret = munmap(data, bytes);
498 BUG_ON(ret);
499 }
500
501 /*
502 * Create a shared memory buffer that can be shared between processes, zeroed:
503 */
zalloc_shared_data(ssize_t bytes)504 static void * zalloc_shared_data(ssize_t bytes)
505 {
506 return alloc_data(bytes, MAP_SHARED, 1, g->p.init_cpu0, g->p.thp, g->p.init_random);
507 }
508
509 /*
510 * Create a shared memory buffer that can be shared between processes:
511 */
setup_shared_data(ssize_t bytes)512 static void * setup_shared_data(ssize_t bytes)
513 {
514 return alloc_data(bytes, MAP_SHARED, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
515 }
516
517 /*
518 * Allocate process-local memory - this will either be shared between
519 * threads of this process, or only be accessed by this thread:
520 */
setup_private_data(ssize_t bytes)521 static void * setup_private_data(ssize_t bytes)
522 {
523 return alloc_data(bytes, MAP_PRIVATE, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
524 }
525
parse_cpu_list(const char * arg)526 static int parse_cpu_list(const char *arg)
527 {
528 p0.cpu_list_str = strdup(arg);
529
530 dprintf("got CPU list: {%s}\n", p0.cpu_list_str);
531
532 return 0;
533 }
534
parse_setup_cpu_list(void)535 static int parse_setup_cpu_list(void)
536 {
537 struct thread_data *td;
538 char *str0, *str;
539 int t;
540
541 if (!g->p.cpu_list_str)
542 return 0;
543
544 dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
545
546 str0 = str = strdup(g->p.cpu_list_str);
547 t = 0;
548
549 BUG_ON(!str);
550
551 tprintf("# binding tasks to CPUs:\n");
552 tprintf("# ");
553
554 while (true) {
555 int bind_cpu, bind_cpu_0, bind_cpu_1;
556 char *tok, *tok_end, *tok_step, *tok_len, *tok_mul;
557 int bind_len;
558 int step;
559 int mul;
560
561 tok = strsep(&str, ",");
562 if (!tok)
563 break;
564
565 tok_end = strstr(tok, "-");
566
567 dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
568 if (!tok_end) {
569 /* Single CPU specified: */
570 bind_cpu_0 = bind_cpu_1 = atol(tok);
571 } else {
572 /* CPU range specified (for example: "5-11"): */
573 bind_cpu_0 = atol(tok);
574 bind_cpu_1 = atol(tok_end + 1);
575 }
576
577 step = 1;
578 tok_step = strstr(tok, "#");
579 if (tok_step) {
580 step = atol(tok_step + 1);
581 BUG_ON(step <= 0 || step >= g->p.nr_cpus);
582 }
583
584 /*
585 * Mask length.
586 * Eg: "--cpus 8_4-16#4" means: '--cpus 8_4,12_4,16_4',
587 * where the _4 means the next 4 CPUs are allowed.
588 */
589 bind_len = 1;
590 tok_len = strstr(tok, "_");
591 if (tok_len) {
592 bind_len = atol(tok_len + 1);
593 BUG_ON(bind_len <= 0 || bind_len > g->p.nr_cpus);
594 }
595
596 /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
597 mul = 1;
598 tok_mul = strstr(tok, "x");
599 if (tok_mul) {
600 mul = atol(tok_mul + 1);
601 BUG_ON(mul <= 0);
602 }
603
604 dprintf("CPUs: %d_%d-%d#%dx%d\n", bind_cpu_0, bind_len, bind_cpu_1, step, mul);
605
606 if (bind_cpu_0 >= g->p.nr_cpus || bind_cpu_1 >= g->p.nr_cpus) {
607 printf("\nTest not applicable, system has only %d CPUs.\n", g->p.nr_cpus);
608 return -1;
609 }
610
611 if (is_cpu_online(bind_cpu_0) != 1 || is_cpu_online(bind_cpu_1) != 1) {
612 printf("\nTest not applicable, bind_cpu_0 or bind_cpu_1 is offline\n");
613 return -1;
614 }
615
616 BUG_ON(bind_cpu_0 < 0 || bind_cpu_1 < 0);
617 BUG_ON(bind_cpu_0 > bind_cpu_1);
618
619 for (bind_cpu = bind_cpu_0; bind_cpu <= bind_cpu_1; bind_cpu += step) {
620 size_t size = CPU_ALLOC_SIZE(g->p.nr_cpus);
621 int i;
622
623 for (i = 0; i < mul; i++) {
624 int cpu;
625
626 if (t >= g->p.nr_tasks) {
627 printf("\n# NOTE: ignoring bind CPUs starting at CPU#%d\n #", bind_cpu);
628 goto out;
629 }
630 td = g->threads + t;
631
632 if (t)
633 tprintf(",");
634 if (bind_len > 1) {
635 tprintf("%2d/%d", bind_cpu, bind_len);
636 } else {
637 tprintf("%2d", bind_cpu);
638 }
639
640 td->bind_cpumask = CPU_ALLOC(g->p.nr_cpus);
641 BUG_ON(!td->bind_cpumask);
642 CPU_ZERO_S(size, td->bind_cpumask);
643 for (cpu = bind_cpu; cpu < bind_cpu+bind_len; cpu++) {
644 if (cpu < 0 || cpu >= g->p.nr_cpus) {
645 CPU_FREE(td->bind_cpumask);
646 BUG_ON(-1);
647 }
648 CPU_SET_S(cpu, size, td->bind_cpumask);
649 }
650 t++;
651 }
652 }
653 }
654 out:
655
656 tprintf("\n");
657
658 if (t < g->p.nr_tasks)
659 printf("# NOTE: %d tasks bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
660
661 free(str0);
662 return 0;
663 }
664
parse_cpus_opt(const struct option * opt __maybe_unused,const char * arg,int unset __maybe_unused)665 static int parse_cpus_opt(const struct option *opt __maybe_unused,
666 const char *arg, int unset __maybe_unused)
667 {
668 if (!arg)
669 return -1;
670
671 return parse_cpu_list(arg);
672 }
673
parse_node_list(const char * arg)674 static int parse_node_list(const char *arg)
675 {
676 p0.node_list_str = strdup(arg);
677
678 dprintf("got NODE list: {%s}\n", p0.node_list_str);
679
680 return 0;
681 }
682
parse_setup_node_list(void)683 static int parse_setup_node_list(void)
684 {
685 struct thread_data *td;
686 char *str0, *str;
687 int t;
688
689 if (!g->p.node_list_str)
690 return 0;
691
692 dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
693
694 str0 = str = strdup(g->p.node_list_str);
695 t = 0;
696
697 BUG_ON(!str);
698
699 tprintf("# binding tasks to NODEs:\n");
700 tprintf("# ");
701
702 while (true) {
703 int bind_node, bind_node_0, bind_node_1;
704 char *tok, *tok_end, *tok_step, *tok_mul;
705 int step;
706 int mul;
707
708 tok = strsep(&str, ",");
709 if (!tok)
710 break;
711
712 tok_end = strstr(tok, "-");
713
714 dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
715 if (!tok_end) {
716 /* Single NODE specified: */
717 bind_node_0 = bind_node_1 = atol(tok);
718 } else {
719 /* NODE range specified (for example: "5-11"): */
720 bind_node_0 = atol(tok);
721 bind_node_1 = atol(tok_end + 1);
722 }
723
724 step = 1;
725 tok_step = strstr(tok, "#");
726 if (tok_step) {
727 step = atol(tok_step + 1);
728 BUG_ON(step <= 0 || step >= g->p.nr_nodes);
729 }
730
731 /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
732 mul = 1;
733 tok_mul = strstr(tok, "x");
734 if (tok_mul) {
735 mul = atol(tok_mul + 1);
736 BUG_ON(mul <= 0);
737 }
738
739 dprintf("NODEs: %d-%d #%d\n", bind_node_0, bind_node_1, step);
740
741 if (bind_node_0 >= g->p.nr_nodes || bind_node_1 >= g->p.nr_nodes) {
742 printf("\nTest not applicable, system has only %d nodes.\n", g->p.nr_nodes);
743 return -1;
744 }
745
746 BUG_ON(bind_node_0 < 0 || bind_node_1 < 0);
747 BUG_ON(bind_node_0 > bind_node_1);
748
749 for (bind_node = bind_node_0; bind_node <= bind_node_1; bind_node += step) {
750 int i;
751
752 for (i = 0; i < mul; i++) {
753 if (t >= g->p.nr_tasks || !node_has_cpus(bind_node)) {
754 printf("\n# NOTE: ignoring bind NODEs starting at NODE#%d\n", bind_node);
755 goto out;
756 }
757 td = g->threads + t;
758
759 if (!t)
760 tprintf(" %2d", bind_node);
761 else
762 tprintf(",%2d", bind_node);
763
764 td->bind_node = bind_node;
765 t++;
766 }
767 }
768 }
769 out:
770
771 tprintf("\n");
772
773 if (t < g->p.nr_tasks)
774 printf("# NOTE: %d tasks mem-bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
775
776 free(str0);
777 return 0;
778 }
779
parse_nodes_opt(const struct option * opt __maybe_unused,const char * arg,int unset __maybe_unused)780 static int parse_nodes_opt(const struct option *opt __maybe_unused,
781 const char *arg, int unset __maybe_unused)
782 {
783 if (!arg)
784 return -1;
785
786 return parse_node_list(arg);
787 }
788
lfsr_32(uint32_t lfsr)789 static inline uint32_t lfsr_32(uint32_t lfsr)
790 {
791 const uint32_t taps = BIT(1) | BIT(5) | BIT(6) | BIT(31);
792 return (lfsr>>1) ^ ((0x0u - (lfsr & 0x1u)) & taps);
793 }
794
795 /*
796 * Make sure there's real data dependency to RAM (when read
797 * accesses are enabled), so the compiler, the CPU and the
798 * kernel (KSM, zero page, etc.) cannot optimize away RAM
799 * accesses:
800 */
access_data(u64 * data,u64 val)801 static inline u64 access_data(u64 *data, u64 val)
802 {
803 if (g->p.data_reads)
804 val += *data;
805 if (g->p.data_writes)
806 *data = val + 1;
807 return val;
808 }
809
810 /*
811 * The worker process does two types of work, a forwards going
812 * loop and a backwards going loop.
813 *
814 * We do this so that on multiprocessor systems we do not create
815 * a 'train' of processing, with highly synchronized processes,
816 * skewing the whole benchmark.
817 */
do_work(u8 * __data,long bytes,int nr,int nr_max,int loop,u64 val)818 static u64 do_work(u8 *__data, long bytes, int nr, int nr_max, int loop, u64 val)
819 {
820 long words = bytes/sizeof(u64);
821 u64 *data = (void *)__data;
822 long chunk_0, chunk_1;
823 u64 *d0, *d, *d1;
824 long off;
825 long i;
826
827 BUG_ON(!data && words);
828 BUG_ON(data && !words);
829
830 if (!data)
831 return val;
832
833 /* Very simple memset() work variant: */
834 if (g->p.data_zero_memset && !g->p.data_rand_walk) {
835 bzero(data, bytes);
836 return val;
837 }
838
839 /* Spread out by PID/TID nr and by loop nr: */
840 chunk_0 = words/nr_max;
841 chunk_1 = words/g->p.nr_loops;
842 off = nr*chunk_0 + loop*chunk_1;
843
844 while (off >= words)
845 off -= words;
846
847 if (g->p.data_rand_walk) {
848 u32 lfsr = nr + loop + val;
849 int j;
850
851 for (i = 0; i < words/1024; i++) {
852 long start, end;
853
854 lfsr = lfsr_32(lfsr);
855
856 start = lfsr % words;
857 end = min(start + 1024, words-1);
858
859 if (g->p.data_zero_memset) {
860 bzero(data + start, (end-start) * sizeof(u64));
861 } else {
862 for (j = start; j < end; j++)
863 val = access_data(data + j, val);
864 }
865 }
866 } else if (!g->p.data_backwards || (nr + loop) & 1) {
867 /* Process data forwards: */
868
869 d0 = data + off;
870 d = data + off + 1;
871 d1 = data + words;
872
873 for (;;) {
874 if (unlikely(d >= d1))
875 d = data;
876 if (unlikely(d == d0))
877 break;
878
879 val = access_data(d, val);
880
881 d++;
882 }
883 } else {
884 /* Process data backwards: */
885
886 d0 = data + off;
887 d = data + off - 1;
888 d1 = data + words;
889
890 for (;;) {
891 if (unlikely(d < data))
892 d = data + words-1;
893 if (unlikely(d == d0))
894 break;
895
896 val = access_data(d, val);
897
898 d--;
899 }
900 }
901
902 return val;
903 }
904
update_curr_cpu(int task_nr,unsigned long bytes_worked)905 static void update_curr_cpu(int task_nr, unsigned long bytes_worked)
906 {
907 unsigned int cpu;
908
909 cpu = sched_getcpu();
910
911 g->threads[task_nr].curr_cpu = cpu;
912 prctl(0, bytes_worked);
913 }
914
915 /*
916 * Count the number of nodes a process's threads
917 * are spread out on.
918 *
919 * A count of 1 means that the process is compressed
920 * to a single node. A count of g->p.nr_nodes means it's
921 * spread out on the whole system.
922 */
count_process_nodes(int process_nr)923 static int count_process_nodes(int process_nr)
924 {
925 char *node_present;
926 int nodes;
927 int n, t;
928
929 node_present = (char *)malloc(g->p.nr_nodes * sizeof(char));
930 BUG_ON(!node_present);
931 for (nodes = 0; nodes < g->p.nr_nodes; nodes++)
932 node_present[nodes] = 0;
933
934 for (t = 0; t < g->p.nr_threads; t++) {
935 struct thread_data *td;
936 int task_nr;
937 int node;
938
939 task_nr = process_nr*g->p.nr_threads + t;
940 td = g->threads + task_nr;
941
942 node = numa_node_of_cpu(td->curr_cpu);
943 if (node < 0) /* curr_cpu was likely still -1 */ {
944 free(node_present);
945 return 0;
946 }
947
948 node_present[node] = 1;
949 }
950
951 nodes = 0;
952
953 for (n = 0; n < g->p.nr_nodes; n++)
954 nodes += node_present[n];
955
956 free(node_present);
957 return nodes;
958 }
959
960 /*
961 * Count the number of distinct process-threads a node contains.
962 *
963 * A count of 1 means that the node contains only a single
964 * process. If all nodes on the system contain at most one
965 * process then we are well-converged.
966 */
count_node_processes(int node)967 static int count_node_processes(int node)
968 {
969 int processes = 0;
970 int t, p;
971
972 for (p = 0; p < g->p.nr_proc; p++) {
973 for (t = 0; t < g->p.nr_threads; t++) {
974 struct thread_data *td;
975 int task_nr;
976 int n;
977
978 task_nr = p*g->p.nr_threads + t;
979 td = g->threads + task_nr;
980
981 n = numa_node_of_cpu(td->curr_cpu);
982 if (n == node) {
983 processes++;
984 break;
985 }
986 }
987 }
988
989 return processes;
990 }
991
calc_convergence_compression(int * strong)992 static void calc_convergence_compression(int *strong)
993 {
994 unsigned int nodes_min, nodes_max;
995 int p;
996
997 nodes_min = -1;
998 nodes_max = 0;
999
1000 for (p = 0; p < g->p.nr_proc; p++) {
1001 unsigned int nodes = count_process_nodes(p);
1002
1003 if (!nodes) {
1004 *strong = 0;
1005 return;
1006 }
1007
1008 nodes_min = min(nodes, nodes_min);
1009 nodes_max = max(nodes, nodes_max);
1010 }
1011
1012 /* Strong convergence: all threads compress on a single node: */
1013 if (nodes_min == 1 && nodes_max == 1) {
1014 *strong = 1;
1015 } else {
1016 *strong = 0;
1017 tprintf(" {%d-%d}", nodes_min, nodes_max);
1018 }
1019 }
1020
calc_convergence(double runtime_ns_max,double * convergence)1021 static void calc_convergence(double runtime_ns_max, double *convergence)
1022 {
1023 unsigned int loops_done_min, loops_done_max;
1024 int process_groups;
1025 int *nodes;
1026 int distance;
1027 int nr_min;
1028 int nr_max;
1029 int strong;
1030 int sum;
1031 int nr;
1032 int node;
1033 int cpu;
1034 int t;
1035
1036 if (!g->p.show_convergence && !g->p.measure_convergence)
1037 return;
1038
1039 nodes = (int *)malloc(g->p.nr_nodes * sizeof(int));
1040 BUG_ON(!nodes);
1041 for (node = 0; node < g->p.nr_nodes; node++)
1042 nodes[node] = 0;
1043
1044 loops_done_min = -1;
1045 loops_done_max = 0;
1046
1047 for (t = 0; t < g->p.nr_tasks; t++) {
1048 struct thread_data *td = g->threads + t;
1049 unsigned int loops_done;
1050
1051 cpu = td->curr_cpu;
1052
1053 /* Not all threads have written it yet: */
1054 if (cpu < 0)
1055 continue;
1056
1057 node = numa_node_of_cpu(cpu);
1058
1059 nodes[node]++;
1060
1061 loops_done = td->loops_done;
1062 loops_done_min = min(loops_done, loops_done_min);
1063 loops_done_max = max(loops_done, loops_done_max);
1064 }
1065
1066 nr_max = 0;
1067 nr_min = g->p.nr_tasks;
1068 sum = 0;
1069
1070 for (node = 0; node < g->p.nr_nodes; node++) {
1071 if (!is_node_present(node))
1072 continue;
1073 nr = nodes[node];
1074 nr_min = min(nr, nr_min);
1075 nr_max = max(nr, nr_max);
1076 sum += nr;
1077 }
1078 BUG_ON(nr_min > nr_max);
1079
1080 BUG_ON(sum > g->p.nr_tasks);
1081
1082 if (0 && (sum < g->p.nr_tasks)) {
1083 free(nodes);
1084 return;
1085 }
1086
1087 /*
1088 * Count the number of distinct process groups present
1089 * on nodes - when we are converged this will decrease
1090 * to g->p.nr_proc:
1091 */
1092 process_groups = 0;
1093
1094 for (node = 0; node < g->p.nr_nodes; node++) {
1095 int processes;
1096
1097 if (!is_node_present(node))
1098 continue;
1099 processes = count_node_processes(node);
1100 nr = nodes[node];
1101 tprintf(" %2d/%-2d", nr, processes);
1102
1103 process_groups += processes;
1104 }
1105
1106 distance = nr_max - nr_min;
1107
1108 tprintf(" [%2d/%-2d]", distance, process_groups);
1109
1110 tprintf(" l:%3d-%-3d (%3d)",
1111 loops_done_min, loops_done_max, loops_done_max-loops_done_min);
1112
1113 if (loops_done_min && loops_done_max) {
1114 double skew = 1.0 - (double)loops_done_min/loops_done_max;
1115
1116 tprintf(" [%4.1f%%]", skew * 100.0);
1117 }
1118
1119 calc_convergence_compression(&strong);
1120
1121 if (strong && process_groups == g->p.nr_proc) {
1122 if (!*convergence) {
1123 *convergence = runtime_ns_max;
1124 tprintf(" (%6.1fs converged)\n", *convergence / NSEC_PER_SEC);
1125 if (g->p.measure_convergence) {
1126 g->all_converged = true;
1127 g->stop_work = true;
1128 }
1129 }
1130 } else {
1131 if (*convergence) {
1132 tprintf(" (%6.1fs de-converged)", runtime_ns_max / NSEC_PER_SEC);
1133 *convergence = 0;
1134 }
1135 tprintf("\n");
1136 }
1137
1138 free(nodes);
1139 }
1140
show_summary(double runtime_ns_max,int l,double * convergence)1141 static void show_summary(double runtime_ns_max, int l, double *convergence)
1142 {
1143 tprintf("\r # %5.1f%% [%.1f mins]",
1144 (double)(l+1)/g->p.nr_loops*100.0, runtime_ns_max / NSEC_PER_SEC / 60.0);
1145
1146 calc_convergence(runtime_ns_max, convergence);
1147
1148 if (g->p.show_details >= 0)
1149 fflush(stdout);
1150 }
1151
worker_thread(void * __tdata)1152 static void *worker_thread(void *__tdata)
1153 {
1154 struct thread_data *td = __tdata;
1155 struct timeval start0, start, stop, diff;
1156 int process_nr = td->process_nr;
1157 int thread_nr = td->thread_nr;
1158 unsigned long last_perturbance;
1159 int task_nr = td->task_nr;
1160 int details = g->p.show_details;
1161 int first_task, last_task;
1162 double convergence = 0;
1163 u64 val = td->val;
1164 double runtime_ns_max;
1165 u8 *global_data;
1166 u8 *process_data;
1167 u8 *thread_data;
1168 u64 bytes_done, secs;
1169 long work_done;
1170 u32 l;
1171 struct rusage rusage;
1172
1173 bind_to_cpumask(td->bind_cpumask);
1174 bind_to_memnode(td->bind_node);
1175
1176 set_taskname("thread %d/%d", process_nr, thread_nr);
1177
1178 global_data = g->data;
1179 process_data = td->process_data;
1180 thread_data = setup_private_data(g->p.bytes_thread);
1181
1182 bytes_done = 0;
1183
1184 last_task = 0;
1185 if (process_nr == g->p.nr_proc-1 && thread_nr == g->p.nr_threads-1)
1186 last_task = 1;
1187
1188 first_task = 0;
1189 if (process_nr == 0 && thread_nr == 0)
1190 first_task = 1;
1191
1192 if (details >= 2) {
1193 printf("# thread %2d / %2d global mem: %p, process mem: %p, thread mem: %p\n",
1194 process_nr, thread_nr, global_data, process_data, thread_data);
1195 }
1196
1197 if (g->p.serialize_startup) {
1198 mutex_lock(&g->startup_mutex);
1199 g->nr_tasks_started++;
1200 /* The last thread wakes the main process. */
1201 if (g->nr_tasks_started == g->p.nr_tasks)
1202 cond_signal(&g->startup_cond);
1203
1204 mutex_unlock(&g->startup_mutex);
1205
1206 /* Here we will wait for the main process to start us all at once: */
1207 mutex_lock(&g->start_work_mutex);
1208 g->start_work = false;
1209 g->nr_tasks_working++;
1210 while (!g->start_work)
1211 cond_wait(&g->start_work_cond, &g->start_work_mutex);
1212
1213 mutex_unlock(&g->start_work_mutex);
1214 }
1215
1216 gettimeofday(&start0, NULL);
1217
1218 start = stop = start0;
1219 last_perturbance = start.tv_sec;
1220
1221 for (l = 0; l < g->p.nr_loops; l++) {
1222 start = stop;
1223
1224 if (g->stop_work)
1225 break;
1226
1227 val += do_work(global_data, g->p.bytes_global, process_nr, g->p.nr_proc, l, val);
1228 val += do_work(process_data, g->p.bytes_process, thread_nr, g->p.nr_threads, l, val);
1229 val += do_work(thread_data, g->p.bytes_thread, 0, 1, l, val);
1230
1231 if (g->p.sleep_usecs) {
1232 mutex_lock(td->process_lock);
1233 usleep(g->p.sleep_usecs);
1234 mutex_unlock(td->process_lock);
1235 }
1236 /*
1237 * Amount of work to be done under a process-global lock:
1238 */
1239 if (g->p.bytes_process_locked) {
1240 mutex_lock(td->process_lock);
1241 val += do_work(process_data, g->p.bytes_process_locked, thread_nr, g->p.nr_threads, l, val);
1242 mutex_unlock(td->process_lock);
1243 }
1244
1245 work_done = g->p.bytes_global + g->p.bytes_process +
1246 g->p.bytes_process_locked + g->p.bytes_thread;
1247
1248 update_curr_cpu(task_nr, work_done);
1249 bytes_done += work_done;
1250
1251 if (details < 0 && !g->p.perturb_secs && !g->p.measure_convergence && !g->p.nr_secs)
1252 continue;
1253
1254 td->loops_done = l;
1255
1256 gettimeofday(&stop, NULL);
1257
1258 /* Check whether our max runtime timed out: */
1259 if (g->p.nr_secs) {
1260 timersub(&stop, &start0, &diff);
1261 if ((u32)diff.tv_sec >= g->p.nr_secs) {
1262 g->stop_work = true;
1263 break;
1264 }
1265 }
1266
1267 /* Update the summary at most once per second: */
1268 if (start.tv_sec == stop.tv_sec)
1269 continue;
1270
1271 /*
1272 * Perturb the first task's equilibrium every g->p.perturb_secs seconds,
1273 * by migrating to CPU#0:
1274 */
1275 if (first_task && g->p.perturb_secs && (int)(stop.tv_sec - last_perturbance) >= g->p.perturb_secs) {
1276 cpu_set_t *orig_mask;
1277 int target_cpu;
1278 int this_cpu;
1279
1280 last_perturbance = stop.tv_sec;
1281
1282 /*
1283 * Depending on where we are running, move into
1284 * the other half of the system, to create some
1285 * real disturbance:
1286 */
1287 this_cpu = g->threads[task_nr].curr_cpu;
1288 if (this_cpu < g->p.nr_cpus/2)
1289 target_cpu = g->p.nr_cpus-1;
1290 else
1291 target_cpu = 0;
1292
1293 orig_mask = bind_to_cpu(target_cpu);
1294
1295 /* Here we are running on the target CPU already */
1296 if (details >= 1)
1297 printf(" (injecting perturbalance, moved to CPU#%d)\n", target_cpu);
1298
1299 bind_to_cpumask(orig_mask);
1300 CPU_FREE(orig_mask);
1301 }
1302
1303 if (details >= 3) {
1304 timersub(&stop, &start, &diff);
1305 runtime_ns_max = diff.tv_sec * NSEC_PER_SEC;
1306 runtime_ns_max += diff.tv_usec * NSEC_PER_USEC;
1307
1308 if (details >= 0) {
1309 printf(" #%2d / %2d: %14.2lf nsecs/op [val: %016"PRIx64"]\n",
1310 process_nr, thread_nr, runtime_ns_max / bytes_done, val);
1311 }
1312 fflush(stdout);
1313 }
1314 if (!last_task)
1315 continue;
1316
1317 timersub(&stop, &start0, &diff);
1318 runtime_ns_max = diff.tv_sec * NSEC_PER_SEC;
1319 runtime_ns_max += diff.tv_usec * NSEC_PER_USEC;
1320
1321 show_summary(runtime_ns_max, l, &convergence);
1322 }
1323
1324 gettimeofday(&stop, NULL);
1325 timersub(&stop, &start0, &diff);
1326 td->runtime_ns = diff.tv_sec * NSEC_PER_SEC;
1327 td->runtime_ns += diff.tv_usec * NSEC_PER_USEC;
1328 secs = td->runtime_ns / NSEC_PER_SEC;
1329 td->speed_gbs = secs ? bytes_done / secs / 1e9 : 0;
1330
1331 getrusage(RUSAGE_THREAD, &rusage);
1332 td->system_time_ns = rusage.ru_stime.tv_sec * NSEC_PER_SEC;
1333 td->system_time_ns += rusage.ru_stime.tv_usec * NSEC_PER_USEC;
1334 td->user_time_ns = rusage.ru_utime.tv_sec * NSEC_PER_SEC;
1335 td->user_time_ns += rusage.ru_utime.tv_usec * NSEC_PER_USEC;
1336
1337 free_data(thread_data, g->p.bytes_thread);
1338
1339 mutex_lock(&g->stop_work_mutex);
1340 g->bytes_done += bytes_done;
1341 mutex_unlock(&g->stop_work_mutex);
1342
1343 return NULL;
1344 }
1345
1346 /*
1347 * A worker process starts a couple of threads:
1348 */
worker_process(int process_nr)1349 static void worker_process(int process_nr)
1350 {
1351 struct mutex process_lock;
1352 struct thread_data *td;
1353 pthread_t *pthreads;
1354 u8 *process_data;
1355 int task_nr;
1356 int ret;
1357 int t;
1358
1359 mutex_init(&process_lock);
1360 set_taskname("process %d", process_nr);
1361
1362 /*
1363 * Pick up the memory policy and the CPU binding of our first thread,
1364 * so that we initialize memory accordingly:
1365 */
1366 task_nr = process_nr*g->p.nr_threads;
1367 td = g->threads + task_nr;
1368
1369 bind_to_memnode(td->bind_node);
1370 bind_to_cpumask(td->bind_cpumask);
1371
1372 pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
1373 process_data = setup_private_data(g->p.bytes_process);
1374
1375 if (g->p.show_details >= 3) {
1376 printf(" # process %2d global mem: %p, process mem: %p\n",
1377 process_nr, g->data, process_data);
1378 }
1379
1380 for (t = 0; t < g->p.nr_threads; t++) {
1381 task_nr = process_nr*g->p.nr_threads + t;
1382 td = g->threads + task_nr;
1383
1384 td->process_data = process_data;
1385 td->process_nr = process_nr;
1386 td->thread_nr = t;
1387 td->task_nr = task_nr;
1388 td->val = rand();
1389 td->curr_cpu = -1;
1390 td->process_lock = &process_lock;
1391
1392 ret = pthread_create(pthreads + t, NULL, worker_thread, td);
1393 BUG_ON(ret);
1394 }
1395
1396 for (t = 0; t < g->p.nr_threads; t++) {
1397 ret = pthread_join(pthreads[t], NULL);
1398 BUG_ON(ret);
1399 }
1400
1401 free_data(process_data, g->p.bytes_process);
1402 free(pthreads);
1403 }
1404
print_summary(void)1405 static void print_summary(void)
1406 {
1407 if (g->p.show_details < 0)
1408 return;
1409
1410 printf("\n ###\n");
1411 printf(" # %d %s will execute (on %d nodes, %d CPUs):\n",
1412 g->p.nr_tasks, g->p.nr_tasks == 1 ? "task" : "tasks", nr_numa_nodes(), g->p.nr_cpus);
1413 printf(" # %5dx %5ldMB global shared mem operations\n",
1414 g->p.nr_loops, g->p.bytes_global/1024/1024);
1415 printf(" # %5dx %5ldMB process shared mem operations\n",
1416 g->p.nr_loops, g->p.bytes_process/1024/1024);
1417 printf(" # %5dx %5ldMB thread local mem operations\n",
1418 g->p.nr_loops, g->p.bytes_thread/1024/1024);
1419
1420 printf(" ###\n");
1421
1422 printf("\n ###\n"); fflush(stdout);
1423 }
1424
init_thread_data(void)1425 static void init_thread_data(void)
1426 {
1427 ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
1428 int t;
1429
1430 g->threads = zalloc_shared_data(size);
1431
1432 for (t = 0; t < g->p.nr_tasks; t++) {
1433 struct thread_data *td = g->threads + t;
1434 size_t cpuset_size = CPU_ALLOC_SIZE(g->p.nr_cpus);
1435 int cpu;
1436
1437 /* Allow all nodes by default: */
1438 td->bind_node = NUMA_NO_NODE;
1439
1440 /* Allow all CPUs by default: */
1441 td->bind_cpumask = CPU_ALLOC(g->p.nr_cpus);
1442 BUG_ON(!td->bind_cpumask);
1443 CPU_ZERO_S(cpuset_size, td->bind_cpumask);
1444 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
1445 CPU_SET_S(cpu, cpuset_size, td->bind_cpumask);
1446 }
1447 }
1448
deinit_thread_data(void)1449 static void deinit_thread_data(void)
1450 {
1451 ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
1452 int t;
1453
1454 /* Free the bind_cpumask allocated for thread_data */
1455 for (t = 0; t < g->p.nr_tasks; t++) {
1456 struct thread_data *td = g->threads + t;
1457 CPU_FREE(td->bind_cpumask);
1458 }
1459
1460 free_data(g->threads, size);
1461 }
1462
init(void)1463 static int init(void)
1464 {
1465 g = (void *)alloc_data(sizeof(*g), MAP_SHARED, 1, 0, 0 /* THP */, 0);
1466
1467 /* Copy over options: */
1468 g->p = p0;
1469
1470 g->p.nr_cpus = numa_num_configured_cpus();
1471
1472 g->p.nr_nodes = numa_max_node() + 1;
1473
1474 /* char array in count_process_nodes(): */
1475 BUG_ON(g->p.nr_nodes < 0);
1476
1477 if (g->p.show_quiet && !g->p.show_details)
1478 g->p.show_details = -1;
1479
1480 /* Some memory should be specified: */
1481 if (!g->p.mb_global_str && !g->p.mb_proc_str && !g->p.mb_thread_str)
1482 return -1;
1483
1484 if (g->p.mb_global_str) {
1485 g->p.mb_global = atof(g->p.mb_global_str);
1486 BUG_ON(g->p.mb_global < 0);
1487 }
1488
1489 if (g->p.mb_proc_str) {
1490 g->p.mb_proc = atof(g->p.mb_proc_str);
1491 BUG_ON(g->p.mb_proc < 0);
1492 }
1493
1494 if (g->p.mb_proc_locked_str) {
1495 g->p.mb_proc_locked = atof(g->p.mb_proc_locked_str);
1496 BUG_ON(g->p.mb_proc_locked < 0);
1497 BUG_ON(g->p.mb_proc_locked > g->p.mb_proc);
1498 }
1499
1500 if (g->p.mb_thread_str) {
1501 g->p.mb_thread = atof(g->p.mb_thread_str);
1502 BUG_ON(g->p.mb_thread < 0);
1503 }
1504
1505 BUG_ON(g->p.nr_threads <= 0);
1506 BUG_ON(g->p.nr_proc <= 0);
1507
1508 g->p.nr_tasks = g->p.nr_proc*g->p.nr_threads;
1509
1510 g->p.bytes_global = g->p.mb_global *1024L*1024L;
1511 g->p.bytes_process = g->p.mb_proc *1024L*1024L;
1512 g->p.bytes_process_locked = g->p.mb_proc_locked *1024L*1024L;
1513 g->p.bytes_thread = g->p.mb_thread *1024L*1024L;
1514
1515 g->data = setup_shared_data(g->p.bytes_global);
1516
1517 /* Startup serialization: */
1518 mutex_init_pshared(&g->start_work_mutex);
1519 cond_init_pshared(&g->start_work_cond);
1520 mutex_init_pshared(&g->startup_mutex);
1521 cond_init_pshared(&g->startup_cond);
1522 mutex_init_pshared(&g->stop_work_mutex);
1523
1524 init_thread_data();
1525
1526 tprintf("#\n");
1527 if (parse_setup_cpu_list() || parse_setup_node_list())
1528 return -1;
1529 tprintf("#\n");
1530
1531 print_summary();
1532
1533 return 0;
1534 }
1535
deinit(void)1536 static void deinit(void)
1537 {
1538 free_data(g->data, g->p.bytes_global);
1539 g->data = NULL;
1540
1541 deinit_thread_data();
1542
1543 free_data(g, sizeof(*g));
1544 g = NULL;
1545 }
1546
1547 /*
1548 * Print a short or long result, depending on the verbosity setting:
1549 */
print_res(const char * name,double val,const char * txt_unit,const char * txt_short,const char * txt_long)1550 static void print_res(const char *name, double val,
1551 const char *txt_unit, const char *txt_short, const char *txt_long)
1552 {
1553 if (!name)
1554 name = "main,";
1555
1556 if (!g->p.show_quiet)
1557 printf(" %-30s %15.3f, %-15s %s\n", name, val, txt_unit, txt_short);
1558 else
1559 printf(" %14.3f %s\n", val, txt_long);
1560 }
1561
__bench_numa(const char * name)1562 static int __bench_numa(const char *name)
1563 {
1564 struct timeval start, stop, diff;
1565 u64 runtime_ns_min, runtime_ns_sum;
1566 pid_t *pids, pid, wpid;
1567 double delta_runtime;
1568 double runtime_avg;
1569 double runtime_sec_max;
1570 double runtime_sec_min;
1571 int wait_stat;
1572 double bytes;
1573 int i, t, p;
1574
1575 if (init())
1576 return -1;
1577
1578 pids = zalloc(g->p.nr_proc * sizeof(*pids));
1579 pid = -1;
1580
1581 if (g->p.serialize_startup) {
1582 tprintf(" #\n");
1583 tprintf(" # Startup synchronization: ..."); fflush(stdout);
1584 }
1585
1586 gettimeofday(&start, NULL);
1587
1588 for (i = 0; i < g->p.nr_proc; i++) {
1589 pid = fork();
1590 dprintf(" # process %2d: PID %d\n", i, pid);
1591
1592 BUG_ON(pid < 0);
1593 if (!pid) {
1594 /* Child process: */
1595 worker_process(i);
1596
1597 exit(0);
1598 }
1599 pids[i] = pid;
1600
1601 }
1602
1603 if (g->p.serialize_startup) {
1604 bool threads_ready = false;
1605 double startup_sec;
1606
1607 /*
1608 * Wait for all the threads to start up. The last thread will
1609 * signal this process.
1610 */
1611 mutex_lock(&g->startup_mutex);
1612 while (g->nr_tasks_started != g->p.nr_tasks)
1613 cond_wait(&g->startup_cond, &g->startup_mutex);
1614
1615 mutex_unlock(&g->startup_mutex);
1616
1617 /* Wait for all threads to be at the start_work_cond. */
1618 while (!threads_ready) {
1619 mutex_lock(&g->start_work_mutex);
1620 threads_ready = (g->nr_tasks_working == g->p.nr_tasks);
1621 mutex_unlock(&g->start_work_mutex);
1622 if (!threads_ready)
1623 usleep(1);
1624 }
1625
1626 gettimeofday(&stop, NULL);
1627
1628 timersub(&stop, &start, &diff);
1629
1630 startup_sec = diff.tv_sec * NSEC_PER_SEC;
1631 startup_sec += diff.tv_usec * NSEC_PER_USEC;
1632 startup_sec /= NSEC_PER_SEC;
1633
1634 tprintf(" threads initialized in %.6f seconds.\n", startup_sec);
1635 tprintf(" #\n");
1636
1637 start = stop;
1638 /* Start all threads running. */
1639 mutex_lock(&g->start_work_mutex);
1640 g->start_work = true;
1641 mutex_unlock(&g->start_work_mutex);
1642 cond_broadcast(&g->start_work_cond);
1643 } else {
1644 gettimeofday(&start, NULL);
1645 }
1646
1647 /* Parent process: */
1648
1649
1650 for (i = 0; i < g->p.nr_proc; i++) {
1651 wpid = waitpid(pids[i], &wait_stat, 0);
1652 BUG_ON(wpid < 0);
1653 BUG_ON(!WIFEXITED(wait_stat));
1654
1655 }
1656
1657 runtime_ns_sum = 0;
1658 runtime_ns_min = -1LL;
1659
1660 for (t = 0; t < g->p.nr_tasks; t++) {
1661 u64 thread_runtime_ns = g->threads[t].runtime_ns;
1662
1663 runtime_ns_sum += thread_runtime_ns;
1664 runtime_ns_min = min(thread_runtime_ns, runtime_ns_min);
1665 }
1666
1667 gettimeofday(&stop, NULL);
1668 timersub(&stop, &start, &diff);
1669
1670 BUG_ON(bench_format != BENCH_FORMAT_DEFAULT);
1671
1672 tprintf("\n ###\n");
1673 tprintf("\n");
1674
1675 runtime_sec_max = diff.tv_sec * NSEC_PER_SEC;
1676 runtime_sec_max += diff.tv_usec * NSEC_PER_USEC;
1677 runtime_sec_max /= NSEC_PER_SEC;
1678
1679 runtime_sec_min = runtime_ns_min / NSEC_PER_SEC;
1680
1681 bytes = g->bytes_done;
1682 runtime_avg = (double)runtime_ns_sum / g->p.nr_tasks / NSEC_PER_SEC;
1683
1684 if (g->p.measure_convergence) {
1685 print_res(name, runtime_sec_max,
1686 "secs,", "NUMA-convergence-latency", "secs latency to NUMA-converge");
1687 }
1688
1689 print_res(name, runtime_sec_max,
1690 "secs,", "runtime-max/thread", "secs slowest (max) thread-runtime");
1691
1692 print_res(name, runtime_sec_min,
1693 "secs,", "runtime-min/thread", "secs fastest (min) thread-runtime");
1694
1695 print_res(name, runtime_avg,
1696 "secs,", "runtime-avg/thread", "secs average thread-runtime");
1697
1698 delta_runtime = (runtime_sec_max - runtime_sec_min)/2.0;
1699 print_res(name, delta_runtime / runtime_sec_max * 100.0,
1700 "%,", "spread-runtime/thread", "% difference between max/avg runtime");
1701
1702 print_res(name, bytes / g->p.nr_tasks / 1e9,
1703 "GB,", "data/thread", "GB data processed, per thread");
1704
1705 print_res(name, bytes / 1e9,
1706 "GB,", "data-total", "GB data processed, total");
1707
1708 print_res(name, runtime_sec_max * NSEC_PER_SEC / (bytes / g->p.nr_tasks),
1709 "nsecs,", "runtime/byte/thread","nsecs/byte/thread runtime");
1710
1711 print_res(name, bytes / g->p.nr_tasks / 1e9 / runtime_sec_max,
1712 "GB/sec,", "thread-speed", "GB/sec/thread speed");
1713
1714 print_res(name, bytes / runtime_sec_max / 1e9,
1715 "GB/sec,", "total-speed", "GB/sec total speed");
1716
1717 if (g->p.show_details >= 2) {
1718 char tname[14 + 2 * 11 + 1];
1719 struct thread_data *td;
1720 for (p = 0; p < g->p.nr_proc; p++) {
1721 for (t = 0; t < g->p.nr_threads; t++) {
1722 memset(tname, 0, sizeof(tname));
1723 td = g->threads + p*g->p.nr_threads + t;
1724 snprintf(tname, sizeof(tname), "process%d:thread%d", p, t);
1725 print_res(tname, td->speed_gbs,
1726 "GB/sec", "thread-speed", "GB/sec/thread speed");
1727 print_res(tname, td->system_time_ns / NSEC_PER_SEC,
1728 "secs", "thread-system-time", "system CPU time/thread");
1729 print_res(tname, td->user_time_ns / NSEC_PER_SEC,
1730 "secs", "thread-user-time", "user CPU time/thread");
1731 }
1732 }
1733 }
1734
1735 free(pids);
1736
1737 deinit();
1738
1739 return 0;
1740 }
1741
1742 #define MAX_ARGS 50
1743
command_size(const char ** argv)1744 static int command_size(const char **argv)
1745 {
1746 int size = 0;
1747
1748 while (*argv) {
1749 size++;
1750 argv++;
1751 }
1752
1753 BUG_ON(size >= MAX_ARGS);
1754
1755 return size;
1756 }
1757
init_params(struct params * p,const char * name,int argc,const char ** argv)1758 static void init_params(struct params *p, const char *name, int argc, const char **argv)
1759 {
1760 int i;
1761
1762 printf("\n # Running %s \"perf bench numa", name);
1763
1764 for (i = 0; i < argc; i++)
1765 printf(" %s", argv[i]);
1766
1767 printf("\"\n");
1768
1769 memset(p, 0, sizeof(*p));
1770
1771 /* Initialize nonzero defaults: */
1772
1773 p->serialize_startup = 1;
1774 p->data_reads = true;
1775 p->data_writes = true;
1776 p->data_backwards = true;
1777 p->data_rand_walk = true;
1778 p->nr_loops = -1;
1779 p->init_random = true;
1780 p->mb_global_str = "1";
1781 p->nr_proc = 1;
1782 p->nr_threads = 1;
1783 p->nr_secs = 5;
1784 p->run_all = argc == 1;
1785 }
1786
run_bench_numa(const char * name,const char ** argv)1787 static int run_bench_numa(const char *name, const char **argv)
1788 {
1789 int argc = command_size(argv);
1790
1791 init_params(&p0, name, argc, argv);
1792 argc = parse_options(argc, argv, options, bench_numa_usage, 0);
1793 if (argc)
1794 goto err;
1795
1796 if (__bench_numa(name))
1797 goto err;
1798
1799 return 0;
1800
1801 err:
1802 return -1;
1803 }
1804
1805 #define OPT_BW_RAM "-s", "20", "-zZq", "--thp", " 1", "--no-data_rand_walk"
1806 #define OPT_BW_RAM_NOTHP OPT_BW_RAM, "--thp", "-1"
1807
1808 #define OPT_CONV "-s", "100", "-zZ0qcm", "--thp", " 1"
1809 #define OPT_CONV_NOTHP OPT_CONV, "--thp", "-1"
1810
1811 #define OPT_BW "-s", "20", "-zZ0q", "--thp", " 1"
1812 #define OPT_BW_NOTHP OPT_BW, "--thp", "-1"
1813
1814 /*
1815 * The built-in test-suite executed by "perf bench numa -a".
1816 *
1817 * (A minimum of 4 nodes and 16 GB of RAM is recommended.)
1818 */
1819 static const char *tests[][MAX_ARGS] = {
1820 /* Basic single-stream NUMA bandwidth measurements: */
1821 { "RAM-bw-local,", "mem", "-p", "1", "-t", "1", "-P", "1024",
1822 "-C" , "0", "-M", "0", OPT_BW_RAM },
1823 { "RAM-bw-local-NOTHP,",
1824 "mem", "-p", "1", "-t", "1", "-P", "1024",
1825 "-C" , "0", "-M", "0", OPT_BW_RAM_NOTHP },
1826 { "RAM-bw-remote,", "mem", "-p", "1", "-t", "1", "-P", "1024",
1827 "-C" , "0", "-M", "1", OPT_BW_RAM },
1828
1829 /* 2-stream NUMA bandwidth measurements: */
1830 { "RAM-bw-local-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1831 "-C", "0,2", "-M", "0x2", OPT_BW_RAM },
1832 { "RAM-bw-remote-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1833 "-C", "0,2", "-M", "1x2", OPT_BW_RAM },
1834
1835 /* Cross-stream NUMA bandwidth measurement: */
1836 { "RAM-bw-cross,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1837 "-C", "0,8", "-M", "1,0", OPT_BW_RAM },
1838
1839 /* Convergence latency measurements: */
1840 { " 1x3-convergence,", "mem", "-p", "1", "-t", "3", "-P", "512", OPT_CONV },
1841 { " 1x4-convergence,", "mem", "-p", "1", "-t", "4", "-P", "512", OPT_CONV },
1842 { " 1x6-convergence,", "mem", "-p", "1", "-t", "6", "-P", "1020", OPT_CONV },
1843 { " 2x3-convergence,", "mem", "-p", "2", "-t", "3", "-P", "1020", OPT_CONV },
1844 { " 3x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
1845 { " 4x4-convergence,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV },
1846 { " 4x4-convergence-NOTHP,",
1847 "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
1848 { " 4x6-convergence,", "mem", "-p", "4", "-t", "6", "-P", "1020", OPT_CONV },
1849 { " 4x8-convergence,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_CONV },
1850 { " 8x4-convergence,", "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV },
1851 { " 8x4-convergence-NOTHP,",
1852 "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
1853 { " 3x1-convergence,", "mem", "-p", "3", "-t", "1", "-P", "512", OPT_CONV },
1854 { " 4x1-convergence,", "mem", "-p", "4", "-t", "1", "-P", "512", OPT_CONV },
1855 { " 8x1-convergence,", "mem", "-p", "8", "-t", "1", "-P", "512", OPT_CONV },
1856 { "16x1-convergence,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_CONV },
1857 { "32x1-convergence,", "mem", "-p", "32", "-t", "1", "-P", "128", OPT_CONV },
1858
1859 /* Various NUMA process/thread layout bandwidth measurements: */
1860 { " 2x1-bw-process,", "mem", "-p", "2", "-t", "1", "-P", "1024", OPT_BW },
1861 { " 3x1-bw-process,", "mem", "-p", "3", "-t", "1", "-P", "1024", OPT_BW },
1862 { " 4x1-bw-process,", "mem", "-p", "4", "-t", "1", "-P", "1024", OPT_BW },
1863 { " 8x1-bw-process,", "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW },
1864 { " 8x1-bw-process-NOTHP,",
1865 "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW_NOTHP },
1866 { "16x1-bw-process,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_BW },
1867
1868 { " 1x4-bw-thread,", "mem", "-p", "1", "-t", "4", "-T", "256", OPT_BW },
1869 { " 1x8-bw-thread,", "mem", "-p", "1", "-t", "8", "-T", "256", OPT_BW },
1870 { "1x16-bw-thread,", "mem", "-p", "1", "-t", "16", "-T", "128", OPT_BW },
1871 { "1x32-bw-thread,", "mem", "-p", "1", "-t", "32", "-T", "64", OPT_BW },
1872
1873 { " 2x3-bw-process,", "mem", "-p", "2", "-t", "3", "-P", "512", OPT_BW },
1874 { " 4x4-bw-process,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_BW },
1875 { " 4x6-bw-process,", "mem", "-p", "4", "-t", "6", "-P", "512", OPT_BW },
1876 { " 4x8-bw-process,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW },
1877 { " 4x8-bw-process-NOTHP,",
1878 "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW_NOTHP },
1879 { " 3x3-bw-process,", "mem", "-p", "3", "-t", "3", "-P", "512", OPT_BW },
1880 { " 5x5-bw-process,", "mem", "-p", "5", "-t", "5", "-P", "512", OPT_BW },
1881
1882 { "2x16-bw-process,", "mem", "-p", "2", "-t", "16", "-P", "512", OPT_BW },
1883 { "1x32-bw-process,", "mem", "-p", "1", "-t", "32", "-P", "2048", OPT_BW },
1884
1885 { "numa02-bw,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW },
1886 { "numa02-bw-NOTHP,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW_NOTHP },
1887 { "numa01-bw-thread,", "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW },
1888 { "numa01-bw-thread-NOTHP,",
1889 "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW_NOTHP },
1890 };
1891
bench_all(void)1892 static int bench_all(void)
1893 {
1894 int nr = ARRAY_SIZE(tests);
1895 int ret;
1896 int i;
1897
1898 ret = system("echo ' #'; echo ' # Running test on: '$(uname -a); echo ' #'");
1899 BUG_ON(ret < 0);
1900
1901 for (i = 0; i < nr; i++) {
1902 run_bench_numa(tests[i][0], tests[i] + 1);
1903 }
1904
1905 printf("\n");
1906
1907 return 0;
1908 }
1909
bench_numa(int argc,const char ** argv)1910 int bench_numa(int argc, const char **argv)
1911 {
1912 init_params(&p0, "main,", argc, argv);
1913 argc = parse_options(argc, argv, options, bench_numa_usage, 0);
1914 if (argc)
1915 goto err;
1916
1917 if (p0.run_all)
1918 return bench_all();
1919
1920 if (__bench_numa(NULL))
1921 goto err;
1922
1923 return 0;
1924
1925 err:
1926 usage_with_options(numa_usage, options);
1927 return -1;
1928 }
1929