Merge tag 'ntb-5.11' of git://github.com/jonmason/ntb
[linux/fpc-iii.git] / tools / perf / bench / numa.c
blob11726ec6285f3d3f05a92ced8c30978e4cfbf0c4
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * numa.c
5 * numa: Simulate NUMA-sensitive workload and measure their NUMA performance
6 */
8 #include <inttypes.h>
9 /* For the CLR_() macros */
10 #include <pthread.h>
12 #include <subcmd/parse-options.h>
13 #include "../util/cloexec.h"
15 #include "bench.h"
17 #include <errno.h>
18 #include <sched.h>
19 #include <stdio.h>
20 #include <assert.h>
21 #include <malloc.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/time.h>
28 #include <sys/resource.h>
29 #include <sys/wait.h>
30 #include <sys/prctl.h>
31 #include <sys/types.h>
32 #include <linux/kernel.h>
33 #include <linux/time64.h>
34 #include <linux/numa.h>
35 #include <linux/zalloc.h>
37 #include <numa.h>
38 #include <numaif.h>
40 #ifndef RUSAGE_THREAD
41 # define RUSAGE_THREAD 1
42 #endif
45 * Regular printout to the terminal, supressed if -q is specified:
47 #define tprintf(x...) do { if (g && g->p.show_details >= 0) printf(x); } while (0)
50 * Debug printf:
52 #undef dprintf
53 #define dprintf(x...) do { if (g && g->p.show_details >= 1) printf(x); } while (0)
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 pthread_mutex_t *process_lock;
72 /* Parameters set by options: */
74 struct params {
75 /* Startup synchronization: */
76 bool serialize_startup;
78 /* Task hierarchy: */
79 int nr_proc;
80 int nr_threads;
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;
88 double mb_global;
89 double mb_proc;
90 double mb_proc_locked;
91 double mb_thread;
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;
103 /* Working set initialization: */
104 bool init_zero;
105 bool init_random;
106 bool init_cpu0;
108 /* Misc options: */
109 int show_details;
110 int run_all;
111 int thp;
113 long bytes_global;
114 long bytes_process;
115 long bytes_process_locked;
116 long bytes_thread;
118 int nr_tasks;
119 bool show_quiet;
121 bool show_convergence;
122 bool measure_convergence;
124 int perturb_secs;
125 int nr_cpus;
126 int nr_nodes;
128 /* Affinity options -C and -N: */
129 char *cpu_list_str;
130 char *node_list_str;
134 /* Global, read-writable area, accessible to all processes and threads: */
136 struct global_info {
137 u8 *data;
139 pthread_mutex_t startup_mutex;
140 pthread_cond_t startup_cond;
141 int nr_tasks_started;
143 pthread_mutex_t start_work_mutex;
144 pthread_cond_t start_work_cond;
145 int nr_tasks_working;
146 bool start_work;
148 pthread_mutex_t stop_work_mutex;
149 u64 bytes_done;
151 struct thread_data *threads;
153 /* Convergence latency measurement: */
154 bool all_converged;
155 bool stop_work;
157 int print_once;
159 struct params p;
162 static struct global_info *g = NULL;
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);
167 struct params p0;
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"),
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)"),
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"),
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"),
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"),
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"),
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()
213 static const char * const bench_numa_usage[] = {
214 "perf bench numa <options>",
215 NULL
218 static const char * const numa_usage[] = {
219 "perf bench numa mem [<options>]",
220 NULL
224 * To get number of numa nodes present.
226 static int nr_numa_nodes(void)
228 int i, nr_nodes = 0;
230 for (i = 0; i < g->p.nr_nodes; i++) {
231 if (numa_bitmask_isbitset(numa_nodes_ptr, i))
232 nr_nodes++;
235 return nr_nodes;
239 * To check if given numa node is present.
241 static int is_node_present(int node)
243 return numa_bitmask_isbitset(numa_nodes_ptr, node);
247 * To check given numa node has cpus.
249 static bool node_has_cpus(int node)
251 struct bitmask *cpumask = numa_allocate_cpumask();
252 bool ret = false; /* fall back to nocpus */
253 int cpu;
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;
264 numa_free_cpumask(cpumask);
266 return ret;
269 static cpu_set_t bind_to_cpu(int target_cpu)
271 cpu_set_t orig_mask, mask;
272 int ret;
274 ret = sched_getaffinity(0, sizeof(orig_mask), &orig_mask);
275 BUG_ON(ret);
277 CPU_ZERO(&mask);
279 if (target_cpu == -1) {
280 int cpu;
282 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
283 CPU_SET(cpu, &mask);
284 } else {
285 BUG_ON(target_cpu < 0 || target_cpu >= g->p.nr_cpus);
286 CPU_SET(target_cpu, &mask);
289 ret = sched_setaffinity(0, sizeof(mask), &mask);
290 BUG_ON(ret);
292 return orig_mask;
295 static cpu_set_t bind_to_node(int target_node)
297 cpu_set_t orig_mask, mask;
298 int cpu;
299 int ret;
301 ret = sched_getaffinity(0, sizeof(orig_mask), &orig_mask);
302 BUG_ON(ret);
304 CPU_ZERO(&mask);
306 if (target_node == NUMA_NO_NODE) {
307 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
308 CPU_SET(cpu, &mask);
309 } else {
310 struct bitmask *cpumask = numa_allocate_cpumask();
312 BUG_ON(!cpumask);
313 if (!numa_node_to_cpus(target_node, cpumask)) {
314 for (cpu = 0; cpu < (int)cpumask->size; cpu++) {
315 if (numa_bitmask_isbitset(cpumask, cpu))
316 CPU_SET(cpu, &mask);
319 numa_free_cpumask(cpumask);
322 ret = sched_setaffinity(0, sizeof(mask), &mask);
323 BUG_ON(ret);
325 return orig_mask;
328 static void bind_to_cpumask(cpu_set_t mask)
330 int ret;
332 ret = sched_setaffinity(0, sizeof(mask), &mask);
333 BUG_ON(ret);
336 static void mempol_restore(void)
338 int ret;
340 ret = set_mempolicy(MPOL_DEFAULT, NULL, g->p.nr_nodes-1);
342 BUG_ON(ret);
345 static void bind_to_memnode(int node)
347 unsigned long nodemask;
348 int ret;
350 if (node == NUMA_NO_NODE)
351 return;
353 BUG_ON(g->p.nr_nodes > (int)sizeof(nodemask)*8);
354 nodemask = 1L << node;
356 ret = set_mempolicy(MPOL_BIND, &nodemask, sizeof(nodemask)*8);
357 dprintf("binding to node %d, mask: %016lx => %d\n", node, nodemask, ret);
359 BUG_ON(ret);
362 #define HPSIZE (2*1024*1024)
364 #define set_taskname(fmt...) \
365 do { \
366 char name[20]; \
368 snprintf(name, 20, fmt); \
369 prctl(PR_SET_NAME, name); \
370 } while (0)
372 static u8 *alloc_data(ssize_t bytes0, int map_flags,
373 int init_zero, int init_cpu0, int thp, int init_random)
375 cpu_set_t orig_mask;
376 ssize_t bytes;
377 u8 *buf;
378 int ret;
380 if (!bytes0)
381 return NULL;
383 /* Allocate and initialize all memory on CPU#0: */
384 if (init_cpu0) {
385 int node = numa_node_of_cpu(0);
387 orig_mask = bind_to_node(node);
388 bind_to_memnode(node);
391 bytes = bytes0 + HPSIZE;
393 buf = (void *)mmap(0, bytes, PROT_READ|PROT_WRITE, MAP_ANON|map_flags, -1, 0);
394 BUG_ON(buf == (void *)-1);
396 if (map_flags == MAP_PRIVATE) {
397 if (thp > 0) {
398 ret = madvise(buf, bytes, MADV_HUGEPAGE);
399 if (ret && !g->print_once) {
400 g->print_once = 1;
401 printf("WARNING: Could not enable THP - do: 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled'\n");
404 if (thp < 0) {
405 ret = madvise(buf, bytes, MADV_NOHUGEPAGE);
406 if (ret && !g->print_once) {
407 g->print_once = 1;
408 printf("WARNING: Could not disable THP: run a CONFIG_TRANSPARENT_HUGEPAGE kernel?\n");
413 if (init_zero) {
414 bzero(buf, bytes);
415 } else {
416 /* Initialize random contents, different in each word: */
417 if (init_random) {
418 u64 *wbuf = (void *)buf;
419 long off = rand();
420 long i;
422 for (i = 0; i < bytes/8; i++)
423 wbuf[i] = i + off;
427 /* Align to 2MB boundary: */
428 buf = (void *)(((unsigned long)buf + HPSIZE-1) & ~(HPSIZE-1));
430 /* Restore affinity: */
431 if (init_cpu0) {
432 bind_to_cpumask(orig_mask);
433 mempol_restore();
436 return buf;
439 static void free_data(void *data, ssize_t bytes)
441 int ret;
443 if (!data)
444 return;
446 ret = munmap(data, bytes);
447 BUG_ON(ret);
451 * Create a shared memory buffer that can be shared between processes, zeroed:
453 static void * zalloc_shared_data(ssize_t bytes)
455 return alloc_data(bytes, MAP_SHARED, 1, g->p.init_cpu0, g->p.thp, g->p.init_random);
459 * Create a shared memory buffer that can be shared between processes:
461 static void * setup_shared_data(ssize_t bytes)
463 return alloc_data(bytes, MAP_SHARED, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
467 * Allocate process-local memory - this will either be shared between
468 * threads of this process, or only be accessed by this thread:
470 static void * setup_private_data(ssize_t bytes)
472 return alloc_data(bytes, MAP_PRIVATE, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
476 * Return a process-shared (global) mutex:
478 static void init_global_mutex(pthread_mutex_t *mutex)
480 pthread_mutexattr_t attr;
482 pthread_mutexattr_init(&attr);
483 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
484 pthread_mutex_init(mutex, &attr);
488 * Return a process-shared (global) condition variable:
490 static void init_global_cond(pthread_cond_t *cond)
492 pthread_condattr_t attr;
494 pthread_condattr_init(&attr);
495 pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
496 pthread_cond_init(cond, &attr);
499 static int parse_cpu_list(const char *arg)
501 p0.cpu_list_str = strdup(arg);
503 dprintf("got CPU list: {%s}\n", p0.cpu_list_str);
505 return 0;
508 static int parse_setup_cpu_list(void)
510 struct thread_data *td;
511 char *str0, *str;
512 int t;
514 if (!g->p.cpu_list_str)
515 return 0;
517 dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
519 str0 = str = strdup(g->p.cpu_list_str);
520 t = 0;
522 BUG_ON(!str);
524 tprintf("# binding tasks to CPUs:\n");
525 tprintf("# ");
527 while (true) {
528 int bind_cpu, bind_cpu_0, bind_cpu_1;
529 char *tok, *tok_end, *tok_step, *tok_len, *tok_mul;
530 int bind_len;
531 int step;
532 int mul;
534 tok = strsep(&str, ",");
535 if (!tok)
536 break;
538 tok_end = strstr(tok, "-");
540 dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
541 if (!tok_end) {
542 /* Single CPU specified: */
543 bind_cpu_0 = bind_cpu_1 = atol(tok);
544 } else {
545 /* CPU range specified (for example: "5-11"): */
546 bind_cpu_0 = atol(tok);
547 bind_cpu_1 = atol(tok_end + 1);
550 step = 1;
551 tok_step = strstr(tok, "#");
552 if (tok_step) {
553 step = atol(tok_step + 1);
554 BUG_ON(step <= 0 || step >= g->p.nr_cpus);
558 * Mask length.
559 * Eg: "--cpus 8_4-16#4" means: '--cpus 8_4,12_4,16_4',
560 * where the _4 means the next 4 CPUs are allowed.
562 bind_len = 1;
563 tok_len = strstr(tok, "_");
564 if (tok_len) {
565 bind_len = atol(tok_len + 1);
566 BUG_ON(bind_len <= 0 || bind_len > g->p.nr_cpus);
569 /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
570 mul = 1;
571 tok_mul = strstr(tok, "x");
572 if (tok_mul) {
573 mul = atol(tok_mul + 1);
574 BUG_ON(mul <= 0);
577 dprintf("CPUs: %d_%d-%d#%dx%d\n", bind_cpu_0, bind_len, bind_cpu_1, step, mul);
579 if (bind_cpu_0 >= g->p.nr_cpus || bind_cpu_1 >= g->p.nr_cpus) {
580 printf("\nTest not applicable, system has only %d CPUs.\n", g->p.nr_cpus);
581 return -1;
584 BUG_ON(bind_cpu_0 < 0 || bind_cpu_1 < 0);
585 BUG_ON(bind_cpu_0 > bind_cpu_1);
587 for (bind_cpu = bind_cpu_0; bind_cpu <= bind_cpu_1; bind_cpu += step) {
588 int i;
590 for (i = 0; i < mul; i++) {
591 int cpu;
593 if (t >= g->p.nr_tasks) {
594 printf("\n# NOTE: ignoring bind CPUs starting at CPU#%d\n #", bind_cpu);
595 goto out;
597 td = g->threads + t;
599 if (t)
600 tprintf(",");
601 if (bind_len > 1) {
602 tprintf("%2d/%d", bind_cpu, bind_len);
603 } else {
604 tprintf("%2d", bind_cpu);
607 CPU_ZERO(&td->bind_cpumask);
608 for (cpu = bind_cpu; cpu < bind_cpu+bind_len; cpu++) {
609 BUG_ON(cpu < 0 || cpu >= g->p.nr_cpus);
610 CPU_SET(cpu, &td->bind_cpumask);
612 t++;
616 out:
618 tprintf("\n");
620 if (t < g->p.nr_tasks)
621 printf("# NOTE: %d tasks bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
623 free(str0);
624 return 0;
627 static int parse_cpus_opt(const struct option *opt __maybe_unused,
628 const char *arg, int unset __maybe_unused)
630 if (!arg)
631 return -1;
633 return parse_cpu_list(arg);
636 static int parse_node_list(const char *arg)
638 p0.node_list_str = strdup(arg);
640 dprintf("got NODE list: {%s}\n", p0.node_list_str);
642 return 0;
645 static int parse_setup_node_list(void)
647 struct thread_data *td;
648 char *str0, *str;
649 int t;
651 if (!g->p.node_list_str)
652 return 0;
654 dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
656 str0 = str = strdup(g->p.node_list_str);
657 t = 0;
659 BUG_ON(!str);
661 tprintf("# binding tasks to NODEs:\n");
662 tprintf("# ");
664 while (true) {
665 int bind_node, bind_node_0, bind_node_1;
666 char *tok, *tok_end, *tok_step, *tok_mul;
667 int step;
668 int mul;
670 tok = strsep(&str, ",");
671 if (!tok)
672 break;
674 tok_end = strstr(tok, "-");
676 dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
677 if (!tok_end) {
678 /* Single NODE specified: */
679 bind_node_0 = bind_node_1 = atol(tok);
680 } else {
681 /* NODE range specified (for example: "5-11"): */
682 bind_node_0 = atol(tok);
683 bind_node_1 = atol(tok_end + 1);
686 step = 1;
687 tok_step = strstr(tok, "#");
688 if (tok_step) {
689 step = atol(tok_step + 1);
690 BUG_ON(step <= 0 || step >= g->p.nr_nodes);
693 /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
694 mul = 1;
695 tok_mul = strstr(tok, "x");
696 if (tok_mul) {
697 mul = atol(tok_mul + 1);
698 BUG_ON(mul <= 0);
701 dprintf("NODEs: %d-%d #%d\n", bind_node_0, bind_node_1, step);
703 if (bind_node_0 >= g->p.nr_nodes || bind_node_1 >= g->p.nr_nodes) {
704 printf("\nTest not applicable, system has only %d nodes.\n", g->p.nr_nodes);
705 return -1;
708 BUG_ON(bind_node_0 < 0 || bind_node_1 < 0);
709 BUG_ON(bind_node_0 > bind_node_1);
711 for (bind_node = bind_node_0; bind_node <= bind_node_1; bind_node += step) {
712 int i;
714 for (i = 0; i < mul; i++) {
715 if (t >= g->p.nr_tasks || !node_has_cpus(bind_node)) {
716 printf("\n# NOTE: ignoring bind NODEs starting at NODE#%d\n", bind_node);
717 goto out;
719 td = g->threads + t;
721 if (!t)
722 tprintf(" %2d", bind_node);
723 else
724 tprintf(",%2d", bind_node);
726 td->bind_node = bind_node;
727 t++;
731 out:
733 tprintf("\n");
735 if (t < g->p.nr_tasks)
736 printf("# NOTE: %d tasks mem-bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
738 free(str0);
739 return 0;
742 static int parse_nodes_opt(const struct option *opt __maybe_unused,
743 const char *arg, int unset __maybe_unused)
745 if (!arg)
746 return -1;
748 return parse_node_list(arg);
751 #define BIT(x) (1ul << x)
753 static inline uint32_t lfsr_32(uint32_t lfsr)
755 const uint32_t taps = BIT(1) | BIT(5) | BIT(6) | BIT(31);
756 return (lfsr>>1) ^ ((0x0u - (lfsr & 0x1u)) & taps);
760 * Make sure there's real data dependency to RAM (when read
761 * accesses are enabled), so the compiler, the CPU and the
762 * kernel (KSM, zero page, etc.) cannot optimize away RAM
763 * accesses:
765 static inline u64 access_data(u64 *data, u64 val)
767 if (g->p.data_reads)
768 val += *data;
769 if (g->p.data_writes)
770 *data = val + 1;
771 return val;
775 * The worker process does two types of work, a forwards going
776 * loop and a backwards going loop.
778 * We do this so that on multiprocessor systems we do not create
779 * a 'train' of processing, with highly synchronized processes,
780 * skewing the whole benchmark.
782 static u64 do_work(u8 *__data, long bytes, int nr, int nr_max, int loop, u64 val)
784 long words = bytes/sizeof(u64);
785 u64 *data = (void *)__data;
786 long chunk_0, chunk_1;
787 u64 *d0, *d, *d1;
788 long off;
789 long i;
791 BUG_ON(!data && words);
792 BUG_ON(data && !words);
794 if (!data)
795 return val;
797 /* Very simple memset() work variant: */
798 if (g->p.data_zero_memset && !g->p.data_rand_walk) {
799 bzero(data, bytes);
800 return val;
803 /* Spread out by PID/TID nr and by loop nr: */
804 chunk_0 = words/nr_max;
805 chunk_1 = words/g->p.nr_loops;
806 off = nr*chunk_0 + loop*chunk_1;
808 while (off >= words)
809 off -= words;
811 if (g->p.data_rand_walk) {
812 u32 lfsr = nr + loop + val;
813 int j;
815 for (i = 0; i < words/1024; i++) {
816 long start, end;
818 lfsr = lfsr_32(lfsr);
820 start = lfsr % words;
821 end = min(start + 1024, words-1);
823 if (g->p.data_zero_memset) {
824 bzero(data + start, (end-start) * sizeof(u64));
825 } else {
826 for (j = start; j < end; j++)
827 val = access_data(data + j, val);
830 } else if (!g->p.data_backwards || (nr + loop) & 1) {
831 /* Process data forwards: */
833 d0 = data + off;
834 d = data + off + 1;
835 d1 = data + words;
837 for (;;) {
838 if (unlikely(d >= d1))
839 d = data;
840 if (unlikely(d == d0))
841 break;
843 val = access_data(d, val);
845 d++;
847 } else {
848 /* Process data backwards: */
850 d0 = data + off;
851 d = data + off - 1;
852 d1 = data + words;
854 for (;;) {
855 if (unlikely(d < data))
856 d = data + words-1;
857 if (unlikely(d == d0))
858 break;
860 val = access_data(d, val);
862 d--;
866 return val;
869 static void update_curr_cpu(int task_nr, unsigned long bytes_worked)
871 unsigned int cpu;
873 cpu = sched_getcpu();
875 g->threads[task_nr].curr_cpu = cpu;
876 prctl(0, bytes_worked);
879 #define MAX_NR_NODES 64
882 * Count the number of nodes a process's threads
883 * are spread out on.
885 * A count of 1 means that the process is compressed
886 * to a single node. A count of g->p.nr_nodes means it's
887 * spread out on the whole system.
889 static int count_process_nodes(int process_nr)
891 char node_present[MAX_NR_NODES] = { 0, };
892 int nodes;
893 int n, t;
895 for (t = 0; t < g->p.nr_threads; t++) {
896 struct thread_data *td;
897 int task_nr;
898 int node;
900 task_nr = process_nr*g->p.nr_threads + t;
901 td = g->threads + task_nr;
903 node = numa_node_of_cpu(td->curr_cpu);
904 if (node < 0) /* curr_cpu was likely still -1 */
905 return 0;
907 node_present[node] = 1;
910 nodes = 0;
912 for (n = 0; n < MAX_NR_NODES; n++)
913 nodes += node_present[n];
915 return nodes;
919 * Count the number of distinct process-threads a node contains.
921 * A count of 1 means that the node contains only a single
922 * process. If all nodes on the system contain at most one
923 * process then we are well-converged.
925 static int count_node_processes(int node)
927 int processes = 0;
928 int t, p;
930 for (p = 0; p < g->p.nr_proc; p++) {
931 for (t = 0; t < g->p.nr_threads; t++) {
932 struct thread_data *td;
933 int task_nr;
934 int n;
936 task_nr = p*g->p.nr_threads + t;
937 td = g->threads + task_nr;
939 n = numa_node_of_cpu(td->curr_cpu);
940 if (n == node) {
941 processes++;
942 break;
947 return processes;
950 static void calc_convergence_compression(int *strong)
952 unsigned int nodes_min, nodes_max;
953 int p;
955 nodes_min = -1;
956 nodes_max = 0;
958 for (p = 0; p < g->p.nr_proc; p++) {
959 unsigned int nodes = count_process_nodes(p);
961 if (!nodes) {
962 *strong = 0;
963 return;
966 nodes_min = min(nodes, nodes_min);
967 nodes_max = max(nodes, nodes_max);
970 /* Strong convergence: all threads compress on a single node: */
971 if (nodes_min == 1 && nodes_max == 1) {
972 *strong = 1;
973 } else {
974 *strong = 0;
975 tprintf(" {%d-%d}", nodes_min, nodes_max);
979 static void calc_convergence(double runtime_ns_max, double *convergence)
981 unsigned int loops_done_min, loops_done_max;
982 int process_groups;
983 int nodes[MAX_NR_NODES];
984 int distance;
985 int nr_min;
986 int nr_max;
987 int strong;
988 int sum;
989 int nr;
990 int node;
991 int cpu;
992 int t;
994 if (!g->p.show_convergence && !g->p.measure_convergence)
995 return;
997 for (node = 0; node < g->p.nr_nodes; node++)
998 nodes[node] = 0;
1000 loops_done_min = -1;
1001 loops_done_max = 0;
1003 for (t = 0; t < g->p.nr_tasks; t++) {
1004 struct thread_data *td = g->threads + t;
1005 unsigned int loops_done;
1007 cpu = td->curr_cpu;
1009 /* Not all threads have written it yet: */
1010 if (cpu < 0)
1011 continue;
1013 node = numa_node_of_cpu(cpu);
1015 nodes[node]++;
1017 loops_done = td->loops_done;
1018 loops_done_min = min(loops_done, loops_done_min);
1019 loops_done_max = max(loops_done, loops_done_max);
1022 nr_max = 0;
1023 nr_min = g->p.nr_tasks;
1024 sum = 0;
1026 for (node = 0; node < g->p.nr_nodes; node++) {
1027 if (!is_node_present(node))
1028 continue;
1029 nr = nodes[node];
1030 nr_min = min(nr, nr_min);
1031 nr_max = max(nr, nr_max);
1032 sum += nr;
1034 BUG_ON(nr_min > nr_max);
1036 BUG_ON(sum > g->p.nr_tasks);
1038 if (0 && (sum < g->p.nr_tasks))
1039 return;
1042 * Count the number of distinct process groups present
1043 * on nodes - when we are converged this will decrease
1044 * to g->p.nr_proc:
1046 process_groups = 0;
1048 for (node = 0; node < g->p.nr_nodes; node++) {
1049 int processes;
1051 if (!is_node_present(node))
1052 continue;
1053 processes = count_node_processes(node);
1054 nr = nodes[node];
1055 tprintf(" %2d/%-2d", nr, processes);
1057 process_groups += processes;
1060 distance = nr_max - nr_min;
1062 tprintf(" [%2d/%-2d]", distance, process_groups);
1064 tprintf(" l:%3d-%-3d (%3d)",
1065 loops_done_min, loops_done_max, loops_done_max-loops_done_min);
1067 if (loops_done_min && loops_done_max) {
1068 double skew = 1.0 - (double)loops_done_min/loops_done_max;
1070 tprintf(" [%4.1f%%]", skew * 100.0);
1073 calc_convergence_compression(&strong);
1075 if (strong && process_groups == g->p.nr_proc) {
1076 if (!*convergence) {
1077 *convergence = runtime_ns_max;
1078 tprintf(" (%6.1fs converged)\n", *convergence / NSEC_PER_SEC);
1079 if (g->p.measure_convergence) {
1080 g->all_converged = true;
1081 g->stop_work = true;
1084 } else {
1085 if (*convergence) {
1086 tprintf(" (%6.1fs de-converged)", runtime_ns_max / NSEC_PER_SEC);
1087 *convergence = 0;
1089 tprintf("\n");
1093 static void show_summary(double runtime_ns_max, int l, double *convergence)
1095 tprintf("\r # %5.1f%% [%.1f mins]",
1096 (double)(l+1)/g->p.nr_loops*100.0, runtime_ns_max / NSEC_PER_SEC / 60.0);
1098 calc_convergence(runtime_ns_max, convergence);
1100 if (g->p.show_details >= 0)
1101 fflush(stdout);
1104 static void *worker_thread(void *__tdata)
1106 struct thread_data *td = __tdata;
1107 struct timeval start0, start, stop, diff;
1108 int process_nr = td->process_nr;
1109 int thread_nr = td->thread_nr;
1110 unsigned long last_perturbance;
1111 int task_nr = td->task_nr;
1112 int details = g->p.show_details;
1113 int first_task, last_task;
1114 double convergence = 0;
1115 u64 val = td->val;
1116 double runtime_ns_max;
1117 u8 *global_data;
1118 u8 *process_data;
1119 u8 *thread_data;
1120 u64 bytes_done, secs;
1121 long work_done;
1122 u32 l;
1123 struct rusage rusage;
1125 bind_to_cpumask(td->bind_cpumask);
1126 bind_to_memnode(td->bind_node);
1128 set_taskname("thread %d/%d", process_nr, thread_nr);
1130 global_data = g->data;
1131 process_data = td->process_data;
1132 thread_data = setup_private_data(g->p.bytes_thread);
1134 bytes_done = 0;
1136 last_task = 0;
1137 if (process_nr == g->p.nr_proc-1 && thread_nr == g->p.nr_threads-1)
1138 last_task = 1;
1140 first_task = 0;
1141 if (process_nr == 0 && thread_nr == 0)
1142 first_task = 1;
1144 if (details >= 2) {
1145 printf("# thread %2d / %2d global mem: %p, process mem: %p, thread mem: %p\n",
1146 process_nr, thread_nr, global_data, process_data, thread_data);
1149 if (g->p.serialize_startup) {
1150 pthread_mutex_lock(&g->startup_mutex);
1151 g->nr_tasks_started++;
1152 /* The last thread wakes the main process. */
1153 if (g->nr_tasks_started == g->p.nr_tasks)
1154 pthread_cond_signal(&g->startup_cond);
1156 pthread_mutex_unlock(&g->startup_mutex);
1158 /* Here we will wait for the main process to start us all at once: */
1159 pthread_mutex_lock(&g->start_work_mutex);
1160 g->start_work = false;
1161 g->nr_tasks_working++;
1162 while (!g->start_work)
1163 pthread_cond_wait(&g->start_work_cond, &g->start_work_mutex);
1165 pthread_mutex_unlock(&g->start_work_mutex);
1168 gettimeofday(&start0, NULL);
1170 start = stop = start0;
1171 last_perturbance = start.tv_sec;
1173 for (l = 0; l < g->p.nr_loops; l++) {
1174 start = stop;
1176 if (g->stop_work)
1177 break;
1179 val += do_work(global_data, g->p.bytes_global, process_nr, g->p.nr_proc, l, val);
1180 val += do_work(process_data, g->p.bytes_process, thread_nr, g->p.nr_threads, l, val);
1181 val += do_work(thread_data, g->p.bytes_thread, 0, 1, l, val);
1183 if (g->p.sleep_usecs) {
1184 pthread_mutex_lock(td->process_lock);
1185 usleep(g->p.sleep_usecs);
1186 pthread_mutex_unlock(td->process_lock);
1189 * Amount of work to be done under a process-global lock:
1191 if (g->p.bytes_process_locked) {
1192 pthread_mutex_lock(td->process_lock);
1193 val += do_work(process_data, g->p.bytes_process_locked, thread_nr, g->p.nr_threads, l, val);
1194 pthread_mutex_unlock(td->process_lock);
1197 work_done = g->p.bytes_global + g->p.bytes_process +
1198 g->p.bytes_process_locked + g->p.bytes_thread;
1200 update_curr_cpu(task_nr, work_done);
1201 bytes_done += work_done;
1203 if (details < 0 && !g->p.perturb_secs && !g->p.measure_convergence && !g->p.nr_secs)
1204 continue;
1206 td->loops_done = l;
1208 gettimeofday(&stop, NULL);
1210 /* Check whether our max runtime timed out: */
1211 if (g->p.nr_secs) {
1212 timersub(&stop, &start0, &diff);
1213 if ((u32)diff.tv_sec >= g->p.nr_secs) {
1214 g->stop_work = true;
1215 break;
1219 /* Update the summary at most once per second: */
1220 if (start.tv_sec == stop.tv_sec)
1221 continue;
1224 * Perturb the first task's equilibrium every g->p.perturb_secs seconds,
1225 * by migrating to CPU#0:
1227 if (first_task && g->p.perturb_secs && (int)(stop.tv_sec - last_perturbance) >= g->p.perturb_secs) {
1228 cpu_set_t orig_mask;
1229 int target_cpu;
1230 int this_cpu;
1232 last_perturbance = stop.tv_sec;
1235 * Depending on where we are running, move into
1236 * the other half of the system, to create some
1237 * real disturbance:
1239 this_cpu = g->threads[task_nr].curr_cpu;
1240 if (this_cpu < g->p.nr_cpus/2)
1241 target_cpu = g->p.nr_cpus-1;
1242 else
1243 target_cpu = 0;
1245 orig_mask = bind_to_cpu(target_cpu);
1247 /* Here we are running on the target CPU already */
1248 if (details >= 1)
1249 printf(" (injecting perturbalance, moved to CPU#%d)\n", target_cpu);
1251 bind_to_cpumask(orig_mask);
1254 if (details >= 3) {
1255 timersub(&stop, &start, &diff);
1256 runtime_ns_max = diff.tv_sec * NSEC_PER_SEC;
1257 runtime_ns_max += diff.tv_usec * NSEC_PER_USEC;
1259 if (details >= 0) {
1260 printf(" #%2d / %2d: %14.2lf nsecs/op [val: %016"PRIx64"]\n",
1261 process_nr, thread_nr, runtime_ns_max / bytes_done, val);
1263 fflush(stdout);
1265 if (!last_task)
1266 continue;
1268 timersub(&stop, &start0, &diff);
1269 runtime_ns_max = diff.tv_sec * NSEC_PER_SEC;
1270 runtime_ns_max += diff.tv_usec * NSEC_PER_USEC;
1272 show_summary(runtime_ns_max, l, &convergence);
1275 gettimeofday(&stop, NULL);
1276 timersub(&stop, &start0, &diff);
1277 td->runtime_ns = diff.tv_sec * NSEC_PER_SEC;
1278 td->runtime_ns += diff.tv_usec * NSEC_PER_USEC;
1279 secs = td->runtime_ns / NSEC_PER_SEC;
1280 td->speed_gbs = secs ? bytes_done / secs / 1e9 : 0;
1282 getrusage(RUSAGE_THREAD, &rusage);
1283 td->system_time_ns = rusage.ru_stime.tv_sec * NSEC_PER_SEC;
1284 td->system_time_ns += rusage.ru_stime.tv_usec * NSEC_PER_USEC;
1285 td->user_time_ns = rusage.ru_utime.tv_sec * NSEC_PER_SEC;
1286 td->user_time_ns += rusage.ru_utime.tv_usec * NSEC_PER_USEC;
1288 free_data(thread_data, g->p.bytes_thread);
1290 pthread_mutex_lock(&g->stop_work_mutex);
1291 g->bytes_done += bytes_done;
1292 pthread_mutex_unlock(&g->stop_work_mutex);
1294 return NULL;
1298 * A worker process starts a couple of threads:
1300 static void worker_process(int process_nr)
1302 pthread_mutex_t process_lock;
1303 struct thread_data *td;
1304 pthread_t *pthreads;
1305 u8 *process_data;
1306 int task_nr;
1307 int ret;
1308 int t;
1310 pthread_mutex_init(&process_lock, NULL);
1311 set_taskname("process %d", process_nr);
1314 * Pick up the memory policy and the CPU binding of our first thread,
1315 * so that we initialize memory accordingly:
1317 task_nr = process_nr*g->p.nr_threads;
1318 td = g->threads + task_nr;
1320 bind_to_memnode(td->bind_node);
1321 bind_to_cpumask(td->bind_cpumask);
1323 pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
1324 process_data = setup_private_data(g->p.bytes_process);
1326 if (g->p.show_details >= 3) {
1327 printf(" # process %2d global mem: %p, process mem: %p\n",
1328 process_nr, g->data, process_data);
1331 for (t = 0; t < g->p.nr_threads; t++) {
1332 task_nr = process_nr*g->p.nr_threads + t;
1333 td = g->threads + task_nr;
1335 td->process_data = process_data;
1336 td->process_nr = process_nr;
1337 td->thread_nr = t;
1338 td->task_nr = task_nr;
1339 td->val = rand();
1340 td->curr_cpu = -1;
1341 td->process_lock = &process_lock;
1343 ret = pthread_create(pthreads + t, NULL, worker_thread, td);
1344 BUG_ON(ret);
1347 for (t = 0; t < g->p.nr_threads; t++) {
1348 ret = pthread_join(pthreads[t], NULL);
1349 BUG_ON(ret);
1352 free_data(process_data, g->p.bytes_process);
1353 free(pthreads);
1356 static void print_summary(void)
1358 if (g->p.show_details < 0)
1359 return;
1361 printf("\n ###\n");
1362 printf(" # %d %s will execute (on %d nodes, %d CPUs):\n",
1363 g->p.nr_tasks, g->p.nr_tasks == 1 ? "task" : "tasks", nr_numa_nodes(), g->p.nr_cpus);
1364 printf(" # %5dx %5ldMB global shared mem operations\n",
1365 g->p.nr_loops, g->p.bytes_global/1024/1024);
1366 printf(" # %5dx %5ldMB process shared mem operations\n",
1367 g->p.nr_loops, g->p.bytes_process/1024/1024);
1368 printf(" # %5dx %5ldMB thread local mem operations\n",
1369 g->p.nr_loops, g->p.bytes_thread/1024/1024);
1371 printf(" ###\n");
1373 printf("\n ###\n"); fflush(stdout);
1376 static void init_thread_data(void)
1378 ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
1379 int t;
1381 g->threads = zalloc_shared_data(size);
1383 for (t = 0; t < g->p.nr_tasks; t++) {
1384 struct thread_data *td = g->threads + t;
1385 int cpu;
1387 /* Allow all nodes by default: */
1388 td->bind_node = NUMA_NO_NODE;
1390 /* Allow all CPUs by default: */
1391 CPU_ZERO(&td->bind_cpumask);
1392 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
1393 CPU_SET(cpu, &td->bind_cpumask);
1397 static void deinit_thread_data(void)
1399 ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
1401 free_data(g->threads, size);
1404 static int init(void)
1406 g = (void *)alloc_data(sizeof(*g), MAP_SHARED, 1, 0, 0 /* THP */, 0);
1408 /* Copy over options: */
1409 g->p = p0;
1411 g->p.nr_cpus = numa_num_configured_cpus();
1413 g->p.nr_nodes = numa_max_node() + 1;
1415 /* char array in count_process_nodes(): */
1416 BUG_ON(g->p.nr_nodes > MAX_NR_NODES || g->p.nr_nodes < 0);
1418 if (g->p.show_quiet && !g->p.show_details)
1419 g->p.show_details = -1;
1421 /* Some memory should be specified: */
1422 if (!g->p.mb_global_str && !g->p.mb_proc_str && !g->p.mb_thread_str)
1423 return -1;
1425 if (g->p.mb_global_str) {
1426 g->p.mb_global = atof(g->p.mb_global_str);
1427 BUG_ON(g->p.mb_global < 0);
1430 if (g->p.mb_proc_str) {
1431 g->p.mb_proc = atof(g->p.mb_proc_str);
1432 BUG_ON(g->p.mb_proc < 0);
1435 if (g->p.mb_proc_locked_str) {
1436 g->p.mb_proc_locked = atof(g->p.mb_proc_locked_str);
1437 BUG_ON(g->p.mb_proc_locked < 0);
1438 BUG_ON(g->p.mb_proc_locked > g->p.mb_proc);
1441 if (g->p.mb_thread_str) {
1442 g->p.mb_thread = atof(g->p.mb_thread_str);
1443 BUG_ON(g->p.mb_thread < 0);
1446 BUG_ON(g->p.nr_threads <= 0);
1447 BUG_ON(g->p.nr_proc <= 0);
1449 g->p.nr_tasks = g->p.nr_proc*g->p.nr_threads;
1451 g->p.bytes_global = g->p.mb_global *1024L*1024L;
1452 g->p.bytes_process = g->p.mb_proc *1024L*1024L;
1453 g->p.bytes_process_locked = g->p.mb_proc_locked *1024L*1024L;
1454 g->p.bytes_thread = g->p.mb_thread *1024L*1024L;
1456 g->data = setup_shared_data(g->p.bytes_global);
1458 /* Startup serialization: */
1459 init_global_mutex(&g->start_work_mutex);
1460 init_global_cond(&g->start_work_cond);
1461 init_global_mutex(&g->startup_mutex);
1462 init_global_cond(&g->startup_cond);
1463 init_global_mutex(&g->stop_work_mutex);
1465 init_thread_data();
1467 tprintf("#\n");
1468 if (parse_setup_cpu_list() || parse_setup_node_list())
1469 return -1;
1470 tprintf("#\n");
1472 print_summary();
1474 return 0;
1477 static void deinit(void)
1479 free_data(g->data, g->p.bytes_global);
1480 g->data = NULL;
1482 deinit_thread_data();
1484 free_data(g, sizeof(*g));
1485 g = NULL;
1489 * Print a short or long result, depending on the verbosity setting:
1491 static void print_res(const char *name, double val,
1492 const char *txt_unit, const char *txt_short, const char *txt_long)
1494 if (!name)
1495 name = "main,";
1497 if (!g->p.show_quiet)
1498 printf(" %-30s %15.3f, %-15s %s\n", name, val, txt_unit, txt_short);
1499 else
1500 printf(" %14.3f %s\n", val, txt_long);
1503 static int __bench_numa(const char *name)
1505 struct timeval start, stop, diff;
1506 u64 runtime_ns_min, runtime_ns_sum;
1507 pid_t *pids, pid, wpid;
1508 double delta_runtime;
1509 double runtime_avg;
1510 double runtime_sec_max;
1511 double runtime_sec_min;
1512 int wait_stat;
1513 double bytes;
1514 int i, t, p;
1516 if (init())
1517 return -1;
1519 pids = zalloc(g->p.nr_proc * sizeof(*pids));
1520 pid = -1;
1522 if (g->p.serialize_startup) {
1523 tprintf(" #\n");
1524 tprintf(" # Startup synchronization: ..."); fflush(stdout);
1527 gettimeofday(&start, NULL);
1529 for (i = 0; i < g->p.nr_proc; i++) {
1530 pid = fork();
1531 dprintf(" # process %2d: PID %d\n", i, pid);
1533 BUG_ON(pid < 0);
1534 if (!pid) {
1535 /* Child process: */
1536 worker_process(i);
1538 exit(0);
1540 pids[i] = pid;
1544 if (g->p.serialize_startup) {
1545 bool threads_ready = false;
1546 double startup_sec;
1549 * Wait for all the threads to start up. The last thread will
1550 * signal this process.
1552 pthread_mutex_lock(&g->startup_mutex);
1553 while (g->nr_tasks_started != g->p.nr_tasks)
1554 pthread_cond_wait(&g->startup_cond, &g->startup_mutex);
1556 pthread_mutex_unlock(&g->startup_mutex);
1558 /* Wait for all threads to be at the start_work_cond. */
1559 while (!threads_ready) {
1560 pthread_mutex_lock(&g->start_work_mutex);
1561 threads_ready = (g->nr_tasks_working == g->p.nr_tasks);
1562 pthread_mutex_unlock(&g->start_work_mutex);
1563 if (!threads_ready)
1564 usleep(1);
1567 gettimeofday(&stop, NULL);
1569 timersub(&stop, &start, &diff);
1571 startup_sec = diff.tv_sec * NSEC_PER_SEC;
1572 startup_sec += diff.tv_usec * NSEC_PER_USEC;
1573 startup_sec /= NSEC_PER_SEC;
1575 tprintf(" threads initialized in %.6f seconds.\n", startup_sec);
1576 tprintf(" #\n");
1578 start = stop;
1579 /* Start all threads running. */
1580 pthread_mutex_lock(&g->start_work_mutex);
1581 g->start_work = true;
1582 pthread_mutex_unlock(&g->start_work_mutex);
1583 pthread_cond_broadcast(&g->start_work_cond);
1584 } else {
1585 gettimeofday(&start, NULL);
1588 /* Parent process: */
1591 for (i = 0; i < g->p.nr_proc; i++) {
1592 wpid = waitpid(pids[i], &wait_stat, 0);
1593 BUG_ON(wpid < 0);
1594 BUG_ON(!WIFEXITED(wait_stat));
1598 runtime_ns_sum = 0;
1599 runtime_ns_min = -1LL;
1601 for (t = 0; t < g->p.nr_tasks; t++) {
1602 u64 thread_runtime_ns = g->threads[t].runtime_ns;
1604 runtime_ns_sum += thread_runtime_ns;
1605 runtime_ns_min = min(thread_runtime_ns, runtime_ns_min);
1608 gettimeofday(&stop, NULL);
1609 timersub(&stop, &start, &diff);
1611 BUG_ON(bench_format != BENCH_FORMAT_DEFAULT);
1613 tprintf("\n ###\n");
1614 tprintf("\n");
1616 runtime_sec_max = diff.tv_sec * NSEC_PER_SEC;
1617 runtime_sec_max += diff.tv_usec * NSEC_PER_USEC;
1618 runtime_sec_max /= NSEC_PER_SEC;
1620 runtime_sec_min = runtime_ns_min / NSEC_PER_SEC;
1622 bytes = g->bytes_done;
1623 runtime_avg = (double)runtime_ns_sum / g->p.nr_tasks / NSEC_PER_SEC;
1625 if (g->p.measure_convergence) {
1626 print_res(name, runtime_sec_max,
1627 "secs,", "NUMA-convergence-latency", "secs latency to NUMA-converge");
1630 print_res(name, runtime_sec_max,
1631 "secs,", "runtime-max/thread", "secs slowest (max) thread-runtime");
1633 print_res(name, runtime_sec_min,
1634 "secs,", "runtime-min/thread", "secs fastest (min) thread-runtime");
1636 print_res(name, runtime_avg,
1637 "secs,", "runtime-avg/thread", "secs average thread-runtime");
1639 delta_runtime = (runtime_sec_max - runtime_sec_min)/2.0;
1640 print_res(name, delta_runtime / runtime_sec_max * 100.0,
1641 "%,", "spread-runtime/thread", "% difference between max/avg runtime");
1643 print_res(name, bytes / g->p.nr_tasks / 1e9,
1644 "GB,", "data/thread", "GB data processed, per thread");
1646 print_res(name, bytes / 1e9,
1647 "GB,", "data-total", "GB data processed, total");
1649 print_res(name, runtime_sec_max * NSEC_PER_SEC / (bytes / g->p.nr_tasks),
1650 "nsecs,", "runtime/byte/thread","nsecs/byte/thread runtime");
1652 print_res(name, bytes / g->p.nr_tasks / 1e9 / runtime_sec_max,
1653 "GB/sec,", "thread-speed", "GB/sec/thread speed");
1655 print_res(name, bytes / runtime_sec_max / 1e9,
1656 "GB/sec,", "total-speed", "GB/sec total speed");
1658 if (g->p.show_details >= 2) {
1659 char tname[14 + 2 * 10 + 1];
1660 struct thread_data *td;
1661 for (p = 0; p < g->p.nr_proc; p++) {
1662 for (t = 0; t < g->p.nr_threads; t++) {
1663 memset(tname, 0, sizeof(tname));
1664 td = g->threads + p*g->p.nr_threads + t;
1665 snprintf(tname, sizeof(tname), "process%d:thread%d", p, t);
1666 print_res(tname, td->speed_gbs,
1667 "GB/sec", "thread-speed", "GB/sec/thread speed");
1668 print_res(tname, td->system_time_ns / NSEC_PER_SEC,
1669 "secs", "thread-system-time", "system CPU time/thread");
1670 print_res(tname, td->user_time_ns / NSEC_PER_SEC,
1671 "secs", "thread-user-time", "user CPU time/thread");
1676 free(pids);
1678 deinit();
1680 return 0;
1683 #define MAX_ARGS 50
1685 static int command_size(const char **argv)
1687 int size = 0;
1689 while (*argv) {
1690 size++;
1691 argv++;
1694 BUG_ON(size >= MAX_ARGS);
1696 return size;
1699 static void init_params(struct params *p, const char *name, int argc, const char **argv)
1701 int i;
1703 printf("\n # Running %s \"perf bench numa", name);
1705 for (i = 0; i < argc; i++)
1706 printf(" %s", argv[i]);
1708 printf("\"\n");
1710 memset(p, 0, sizeof(*p));
1712 /* Initialize nonzero defaults: */
1714 p->serialize_startup = 1;
1715 p->data_reads = true;
1716 p->data_writes = true;
1717 p->data_backwards = true;
1718 p->data_rand_walk = true;
1719 p->nr_loops = -1;
1720 p->init_random = true;
1721 p->mb_global_str = "1";
1722 p->nr_proc = 1;
1723 p->nr_threads = 1;
1724 p->nr_secs = 5;
1725 p->run_all = argc == 1;
1728 static int run_bench_numa(const char *name, const char **argv)
1730 int argc = command_size(argv);
1732 init_params(&p0, name, argc, argv);
1733 argc = parse_options(argc, argv, options, bench_numa_usage, 0);
1734 if (argc)
1735 goto err;
1737 if (__bench_numa(name))
1738 goto err;
1740 return 0;
1742 err:
1743 return -1;
1746 #define OPT_BW_RAM "-s", "20", "-zZq", "--thp", " 1", "--no-data_rand_walk"
1747 #define OPT_BW_RAM_NOTHP OPT_BW_RAM, "--thp", "-1"
1749 #define OPT_CONV "-s", "100", "-zZ0qcm", "--thp", " 1"
1750 #define OPT_CONV_NOTHP OPT_CONV, "--thp", "-1"
1752 #define OPT_BW "-s", "20", "-zZ0q", "--thp", " 1"
1753 #define OPT_BW_NOTHP OPT_BW, "--thp", "-1"
1756 * The built-in test-suite executed by "perf bench numa -a".
1758 * (A minimum of 4 nodes and 16 GB of RAM is recommended.)
1760 static const char *tests[][MAX_ARGS] = {
1761 /* Basic single-stream NUMA bandwidth measurements: */
1762 { "RAM-bw-local,", "mem", "-p", "1", "-t", "1", "-P", "1024",
1763 "-C" , "0", "-M", "0", OPT_BW_RAM },
1764 { "RAM-bw-local-NOTHP,",
1765 "mem", "-p", "1", "-t", "1", "-P", "1024",
1766 "-C" , "0", "-M", "0", OPT_BW_RAM_NOTHP },
1767 { "RAM-bw-remote,", "mem", "-p", "1", "-t", "1", "-P", "1024",
1768 "-C" , "0", "-M", "1", OPT_BW_RAM },
1770 /* 2-stream NUMA bandwidth measurements: */
1771 { "RAM-bw-local-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1772 "-C", "0,2", "-M", "0x2", OPT_BW_RAM },
1773 { "RAM-bw-remote-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1774 "-C", "0,2", "-M", "1x2", OPT_BW_RAM },
1776 /* Cross-stream NUMA bandwidth measurement: */
1777 { "RAM-bw-cross,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1778 "-C", "0,8", "-M", "1,0", OPT_BW_RAM },
1780 /* Convergence latency measurements: */
1781 { " 1x3-convergence,", "mem", "-p", "1", "-t", "3", "-P", "512", OPT_CONV },
1782 { " 1x4-convergence,", "mem", "-p", "1", "-t", "4", "-P", "512", OPT_CONV },
1783 { " 1x6-convergence,", "mem", "-p", "1", "-t", "6", "-P", "1020", OPT_CONV },
1784 { " 2x3-convergence,", "mem", "-p", "2", "-t", "3", "-P", "1020", OPT_CONV },
1785 { " 3x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
1786 { " 4x4-convergence,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV },
1787 { " 4x4-convergence-NOTHP,",
1788 "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
1789 { " 4x6-convergence,", "mem", "-p", "4", "-t", "6", "-P", "1020", OPT_CONV },
1790 { " 4x8-convergence,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_CONV },
1791 { " 8x4-convergence,", "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV },
1792 { " 8x4-convergence-NOTHP,",
1793 "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
1794 { " 3x1-convergence,", "mem", "-p", "3", "-t", "1", "-P", "512", OPT_CONV },
1795 { " 4x1-convergence,", "mem", "-p", "4", "-t", "1", "-P", "512", OPT_CONV },
1796 { " 8x1-convergence,", "mem", "-p", "8", "-t", "1", "-P", "512", OPT_CONV },
1797 { "16x1-convergence,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_CONV },
1798 { "32x1-convergence,", "mem", "-p", "32", "-t", "1", "-P", "128", OPT_CONV },
1800 /* Various NUMA process/thread layout bandwidth measurements: */
1801 { " 2x1-bw-process,", "mem", "-p", "2", "-t", "1", "-P", "1024", OPT_BW },
1802 { " 3x1-bw-process,", "mem", "-p", "3", "-t", "1", "-P", "1024", OPT_BW },
1803 { " 4x1-bw-process,", "mem", "-p", "4", "-t", "1", "-P", "1024", OPT_BW },
1804 { " 8x1-bw-process,", "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW },
1805 { " 8x1-bw-process-NOTHP,",
1806 "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW_NOTHP },
1807 { "16x1-bw-process,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_BW },
1809 { " 1x4-bw-thread,", "mem", "-p", "1", "-t", "4", "-T", "256", OPT_BW },
1810 { " 1x8-bw-thread,", "mem", "-p", "1", "-t", "8", "-T", "256", OPT_BW },
1811 { "1x16-bw-thread,", "mem", "-p", "1", "-t", "16", "-T", "128", OPT_BW },
1812 { "1x32-bw-thread,", "mem", "-p", "1", "-t", "32", "-T", "64", OPT_BW },
1814 { " 2x3-bw-process,", "mem", "-p", "2", "-t", "3", "-P", "512", OPT_BW },
1815 { " 4x4-bw-process,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_BW },
1816 { " 4x6-bw-process,", "mem", "-p", "4", "-t", "6", "-P", "512", OPT_BW },
1817 { " 4x8-bw-process,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW },
1818 { " 4x8-bw-process-NOTHP,",
1819 "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW_NOTHP },
1820 { " 3x3-bw-process,", "mem", "-p", "3", "-t", "3", "-P", "512", OPT_BW },
1821 { " 5x5-bw-process,", "mem", "-p", "5", "-t", "5", "-P", "512", OPT_BW },
1823 { "2x16-bw-process,", "mem", "-p", "2", "-t", "16", "-P", "512", OPT_BW },
1824 { "1x32-bw-process,", "mem", "-p", "1", "-t", "32", "-P", "2048", OPT_BW },
1826 { "numa02-bw,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW },
1827 { "numa02-bw-NOTHP,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW_NOTHP },
1828 { "numa01-bw-thread,", "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW },
1829 { "numa01-bw-thread-NOTHP,",
1830 "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW_NOTHP },
1833 static int bench_all(void)
1835 int nr = ARRAY_SIZE(tests);
1836 int ret;
1837 int i;
1839 ret = system("echo ' #'; echo ' # Running test on: '$(uname -a); echo ' #'");
1840 BUG_ON(ret < 0);
1842 for (i = 0; i < nr; i++) {
1843 run_bench_numa(tests[i][0], tests[i] + 1);
1846 printf("\n");
1848 return 0;
1851 int bench_numa(int argc, const char **argv)
1853 init_params(&p0, "main,", argc, argv);
1854 argc = parse_options(argc, argv, options, bench_numa_usage, 0);
1855 if (argc)
1856 goto err;
1858 if (p0.run_all)
1859 return bench_all();
1861 if (__bench_numa(NULL))
1862 goto err;
1864 return 0;
1866 err:
1867 usage_with_options(numa_usage, options);
1868 return -1;