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