4 * numa: Simulate NUMA-sensitive workload and measure their NUMA performance
8 #include "../builtin.h"
9 #include "../util/util.h"
10 #include "../util/parse-options.h"
11 #include "../util/cloexec.h"
27 #include <sys/resource.h>
29 #include <sys/prctl.h>
30 #include <sys/types.h>
36 # define RUSAGE_THREAD 1
40 * Regular printout to the terminal, supressed if -q is specified:
42 #define tprintf(x...) do { if (g && g->p.show_details >= 0) printf(x); } while (0)
47 #define dprintf(x...) do { if (g && g->p.show_details >= 1) printf(x); } while (0)
51 cpu_set_t bind_cpumask;
57 unsigned int loops_done;
63 pthread_mutex_t *process_lock;
66 /* Parameters set by options: */
69 /* Startup synchronization: */
70 bool serialize_startup;
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;
84 double mb_proc_locked;
87 /* Access patterns to the working set: */
91 bool data_zero_memset;
97 /* Working set initialization: */
109 long bytes_process_locked;
115 bool show_convergence;
116 bool measure_convergence;
122 /* Affinity options -C and -N: */
128 /* Global, read-writable area, accessible to all processes and threads: */
133 pthread_mutex_t startup_mutex;
134 int nr_tasks_started;
136 pthread_mutex_t startup_done_mutex;
138 pthread_mutex_t start_work_mutex;
139 int nr_tasks_working;
141 pthread_mutex_t stop_work_mutex;
144 struct thread_data *threads;
146 /* Convergence latency measurement: */
155 static struct global_info *g = NULL;
157 static int parse_cpus_opt(const struct option *opt, const char *arg, int unset);
158 static int parse_nodes_opt(const struct option *opt, const char *arg, int unset);
162 static 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"),
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)"),
171 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)"),
173 OPT_UINTEGER('u', "usleep" , &p0.sleep_usecs, "usecs to sleep per loop iteration"),
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"),
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"),
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"),
192 OPT_BOOLEAN('q', "quiet" , &p0.show_quiet, "quiet mode"),
193 OPT_BOOLEAN('S', "serialize-startup", &p0.serialize_startup,"serialize thread startup"),
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)",
199 OPT_CALLBACK('M', "memnodes", NULL, "node[,node2,...nodeN]",
200 "bind the first N tasks to these specific memory nodes (the rest is unbound)",
205 static const char * const bench_numa_usage[] = {
206 "perf bench numa <options>",
210 static const char * const numa_usage[] = {
211 "perf bench numa mem [<options>]",
216 * To get number of numa nodes present.
218 static int nr_numa_nodes(void)
222 for (i = 0; i < g->p.nr_nodes; i++) {
223 if (numa_bitmask_isbitset(numa_nodes_ptr, i))
231 * To check if given numa node is present.
233 static int is_node_present(int node)
235 return numa_bitmask_isbitset(numa_nodes_ptr, node);
239 * To check given numa node has cpus.
241 static bool node_has_cpus(int node)
243 struct bitmask *cpu = numa_allocate_cpumask();
246 if (cpu && !numa_node_to_cpus(node, cpu)) {
247 for (i = 0; i < cpu->size; i++) {
248 if (numa_bitmask_isbitset(cpu, i))
253 return false; /* lets fall back to nocpus safely */
256 static cpu_set_t bind_to_cpu(int target_cpu)
258 cpu_set_t orig_mask, mask;
261 ret = sched_getaffinity(0, sizeof(orig_mask), &orig_mask);
266 if (target_cpu == -1) {
269 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
272 BUG_ON(target_cpu < 0 || target_cpu >= g->p.nr_cpus);
273 CPU_SET(target_cpu, &mask);
276 ret = sched_setaffinity(0, sizeof(mask), &mask);
282 static cpu_set_t bind_to_node(int target_node)
284 int cpus_per_node = g->p.nr_cpus / nr_numa_nodes();
285 cpu_set_t orig_mask, mask;
289 BUG_ON(cpus_per_node * nr_numa_nodes() != g->p.nr_cpus);
290 BUG_ON(!cpus_per_node);
292 ret = sched_getaffinity(0, sizeof(orig_mask), &orig_mask);
297 if (target_node == -1) {
298 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
301 int cpu_start = (target_node + 0) * cpus_per_node;
302 int cpu_stop = (target_node + 1) * cpus_per_node;
304 BUG_ON(cpu_stop > g->p.nr_cpus);
306 for (cpu = cpu_start; cpu < cpu_stop; cpu++)
310 ret = sched_setaffinity(0, sizeof(mask), &mask);
316 static void bind_to_cpumask(cpu_set_t mask)
320 ret = sched_setaffinity(0, sizeof(mask), &mask);
324 static void mempol_restore(void)
328 ret = set_mempolicy(MPOL_DEFAULT, NULL, g->p.nr_nodes-1);
333 static void bind_to_memnode(int node)
335 unsigned long nodemask;
341 BUG_ON(g->p.nr_nodes > (int)sizeof(nodemask));
342 nodemask = 1L << node;
344 ret = set_mempolicy(MPOL_BIND, &nodemask, sizeof(nodemask)*8);
345 dprintf("binding to node %d, mask: %016lx => %d\n", node, nodemask, ret);
350 #define HPSIZE (2*1024*1024)
352 #define set_taskname(fmt...) \
356 snprintf(name, 20, fmt); \
357 prctl(PR_SET_NAME, name); \
360 static u8 *alloc_data(ssize_t bytes0, int map_flags,
361 int init_zero, int init_cpu0, int thp, int init_random)
371 /* Allocate and initialize all memory on CPU#0: */
373 int node = numa_node_of_cpu(0);
375 orig_mask = bind_to_node(node);
376 bind_to_memnode(node);
379 bytes = bytes0 + HPSIZE;
381 buf = (void *)mmap(0, bytes, PROT_READ|PROT_WRITE, MAP_ANON|map_flags, -1, 0);
382 BUG_ON(buf == (void *)-1);
384 if (map_flags == MAP_PRIVATE) {
386 ret = madvise(buf, bytes, MADV_HUGEPAGE);
387 if (ret && !g->print_once) {
389 printf("WARNING: Could not enable THP - do: 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled'\n");
393 ret = madvise(buf, bytes, MADV_NOHUGEPAGE);
394 if (ret && !g->print_once) {
396 printf("WARNING: Could not disable THP: run a CONFIG_TRANSPARENT_HUGEPAGE kernel?\n");
404 /* Initialize random contents, different in each word: */
406 u64 *wbuf = (void *)buf;
410 for (i = 0; i < bytes/8; i++)
415 /* Align to 2MB boundary: */
416 buf = (void *)(((unsigned long)buf + HPSIZE-1) & ~(HPSIZE-1));
418 /* Restore affinity: */
420 bind_to_cpumask(orig_mask);
427 static void free_data(void *data, ssize_t bytes)
434 ret = munmap(data, bytes);
439 * Create a shared memory buffer that can be shared between processes, zeroed:
441 static void * zalloc_shared_data(ssize_t bytes)
443 return alloc_data(bytes, MAP_SHARED, 1, g->p.init_cpu0, g->p.thp, g->p.init_random);
447 * Create a shared memory buffer that can be shared between processes:
449 static void * setup_shared_data(ssize_t bytes)
451 return alloc_data(bytes, MAP_SHARED, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
455 * Allocate process-local memory - this will either be shared between
456 * threads of this process, or only be accessed by this thread:
458 static void * setup_private_data(ssize_t bytes)
460 return alloc_data(bytes, MAP_PRIVATE, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
464 * Return a process-shared (global) mutex:
466 static void init_global_mutex(pthread_mutex_t *mutex)
468 pthread_mutexattr_t attr;
470 pthread_mutexattr_init(&attr);
471 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
472 pthread_mutex_init(mutex, &attr);
475 static int parse_cpu_list(const char *arg)
477 p0.cpu_list_str = strdup(arg);
479 dprintf("got CPU list: {%s}\n", p0.cpu_list_str);
484 static int parse_setup_cpu_list(void)
486 struct thread_data *td;
490 if (!g->p.cpu_list_str)
493 dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
495 str0 = str = strdup(g->p.cpu_list_str);
500 tprintf("# binding tasks to CPUs:\n");
504 int bind_cpu, bind_cpu_0, bind_cpu_1;
505 char *tok, *tok_end, *tok_step, *tok_len, *tok_mul;
510 tok = strsep(&str, ",");
514 tok_end = strstr(tok, "-");
516 dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
518 /* Single CPU specified: */
519 bind_cpu_0 = bind_cpu_1 = atol(tok);
521 /* CPU range specified (for example: "5-11"): */
522 bind_cpu_0 = atol(tok);
523 bind_cpu_1 = atol(tok_end + 1);
527 tok_step = strstr(tok, "#");
529 step = atol(tok_step + 1);
530 BUG_ON(step <= 0 || step >= g->p.nr_cpus);
535 * Eg: "--cpus 8_4-16#4" means: '--cpus 8_4,12_4,16_4',
536 * where the _4 means the next 4 CPUs are allowed.
539 tok_len = strstr(tok, "_");
541 bind_len = atol(tok_len + 1);
542 BUG_ON(bind_len <= 0 || bind_len > g->p.nr_cpus);
545 /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
547 tok_mul = strstr(tok, "x");
549 mul = atol(tok_mul + 1);
553 dprintf("CPUs: %d_%d-%d#%dx%d\n", bind_cpu_0, bind_len, bind_cpu_1, step, mul);
555 if (bind_cpu_0 >= g->p.nr_cpus || bind_cpu_1 >= g->p.nr_cpus) {
556 printf("\nTest not applicable, system has only %d CPUs.\n", g->p.nr_cpus);
560 BUG_ON(bind_cpu_0 < 0 || bind_cpu_1 < 0);
561 BUG_ON(bind_cpu_0 > bind_cpu_1);
563 for (bind_cpu = bind_cpu_0; bind_cpu <= bind_cpu_1; bind_cpu += step) {
566 for (i = 0; i < mul; i++) {
569 if (t >= g->p.nr_tasks) {
570 printf("\n# NOTE: ignoring bind CPUs starting at CPU#%d\n #", bind_cpu);
578 tprintf("%2d/%d", bind_cpu, bind_len);
580 tprintf("%2d", bind_cpu);
583 CPU_ZERO(&td->bind_cpumask);
584 for (cpu = bind_cpu; cpu < bind_cpu+bind_len; cpu++) {
585 BUG_ON(cpu < 0 || cpu >= g->p.nr_cpus);
586 CPU_SET(cpu, &td->bind_cpumask);
596 if (t < g->p.nr_tasks)
597 printf("# NOTE: %d tasks bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
603 static int parse_cpus_opt(const struct option *opt __maybe_unused,
604 const char *arg, int unset __maybe_unused)
609 return parse_cpu_list(arg);
612 static int parse_node_list(const char *arg)
614 p0.node_list_str = strdup(arg);
616 dprintf("got NODE list: {%s}\n", p0.node_list_str);
621 static int parse_setup_node_list(void)
623 struct thread_data *td;
627 if (!g->p.node_list_str)
630 dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
632 str0 = str = strdup(g->p.node_list_str);
637 tprintf("# binding tasks to NODEs:\n");
641 int bind_node, bind_node_0, bind_node_1;
642 char *tok, *tok_end, *tok_step, *tok_mul;
646 tok = strsep(&str, ",");
650 tok_end = strstr(tok, "-");
652 dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
654 /* Single NODE specified: */
655 bind_node_0 = bind_node_1 = atol(tok);
657 /* NODE range specified (for example: "5-11"): */
658 bind_node_0 = atol(tok);
659 bind_node_1 = atol(tok_end + 1);
663 tok_step = strstr(tok, "#");
665 step = atol(tok_step + 1);
666 BUG_ON(step <= 0 || step >= g->p.nr_nodes);
669 /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
671 tok_mul = strstr(tok, "x");
673 mul = atol(tok_mul + 1);
677 dprintf("NODEs: %d-%d #%d\n", bind_node_0, bind_node_1, step);
679 if (bind_node_0 >= g->p.nr_nodes || bind_node_1 >= g->p.nr_nodes) {
680 printf("\nTest not applicable, system has only %d nodes.\n", g->p.nr_nodes);
684 BUG_ON(bind_node_0 < 0 || bind_node_1 < 0);
685 BUG_ON(bind_node_0 > bind_node_1);
687 for (bind_node = bind_node_0; bind_node <= bind_node_1; bind_node += step) {
690 for (i = 0; i < mul; i++) {
691 if (t >= g->p.nr_tasks || !node_has_cpus(bind_node)) {
692 printf("\n# NOTE: ignoring bind NODEs starting at NODE#%d\n", bind_node);
698 tprintf(" %2d", bind_node);
700 tprintf(",%2d", bind_node);
702 td->bind_node = bind_node;
711 if (t < g->p.nr_tasks)
712 printf("# NOTE: %d tasks mem-bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
718 static int parse_nodes_opt(const struct option *opt __maybe_unused,
719 const char *arg, int unset __maybe_unused)
724 return parse_node_list(arg);
729 #define BIT(x) (1ul << x)
731 static inline uint32_t lfsr_32(uint32_t lfsr)
733 const uint32_t taps = BIT(1) | BIT(5) | BIT(6) | BIT(31);
734 return (lfsr>>1) ^ ((0x0u - (lfsr & 0x1u)) & taps);
738 * Make sure there's real data dependency to RAM (when read
739 * accesses are enabled), so the compiler, the CPU and the
740 * kernel (KSM, zero page, etc.) cannot optimize away RAM
743 static inline u64 access_data(u64 *data __attribute__((unused)), u64 val)
747 if (g->p.data_writes)
753 * The worker process does two types of work, a forwards going
754 * loop and a backwards going loop.
756 * We do this so that on multiprocessor systems we do not create
757 * a 'train' of processing, with highly synchronized processes,
758 * skewing the whole benchmark.
760 static u64 do_work(u8 *__data, long bytes, int nr, int nr_max, int loop, u64 val)
762 long words = bytes/sizeof(u64);
763 u64 *data = (void *)__data;
764 long chunk_0, chunk_1;
769 BUG_ON(!data && words);
770 BUG_ON(data && !words);
775 /* Very simple memset() work variant: */
776 if (g->p.data_zero_memset && !g->p.data_rand_walk) {
781 /* Spread out by PID/TID nr and by loop nr: */
782 chunk_0 = words/nr_max;
783 chunk_1 = words/g->p.nr_loops;
784 off = nr*chunk_0 + loop*chunk_1;
789 if (g->p.data_rand_walk) {
790 u32 lfsr = nr + loop + val;
793 for (i = 0; i < words/1024; i++) {
796 lfsr = lfsr_32(lfsr);
798 start = lfsr % words;
799 end = min(start + 1024, words-1);
801 if (g->p.data_zero_memset) {
802 bzero(data + start, (end-start) * sizeof(u64));
804 for (j = start; j < end; j++)
805 val = access_data(data + j, val);
808 } else if (!g->p.data_backwards || (nr + loop) & 1) {
814 /* Process data forwards: */
816 if (unlikely(d >= d1))
818 if (unlikely(d == d0))
821 val = access_data(d, val);
826 /* Process data backwards: */
832 /* Process data forwards: */
834 if (unlikely(d < data))
836 if (unlikely(d == d0))
839 val = access_data(d, val);
848 static void update_curr_cpu(int task_nr, unsigned long bytes_worked)
852 cpu = sched_getcpu();
854 g->threads[task_nr].curr_cpu = cpu;
855 prctl(0, bytes_worked);
858 #define MAX_NR_NODES 64
861 * Count the number of nodes a process's threads
864 * A count of 1 means that the process is compressed
865 * to a single node. A count of g->p.nr_nodes means it's
866 * spread out on the whole system.
868 static int count_process_nodes(int process_nr)
870 char node_present[MAX_NR_NODES] = { 0, };
874 for (t = 0; t < g->p.nr_threads; t++) {
875 struct thread_data *td;
879 task_nr = process_nr*g->p.nr_threads + t;
880 td = g->threads + task_nr;
882 node = numa_node_of_cpu(td->curr_cpu);
883 if (node < 0) /* curr_cpu was likely still -1 */
886 node_present[node] = 1;
891 for (n = 0; n < MAX_NR_NODES; n++)
892 nodes += node_present[n];
898 * Count the number of distinct process-threads a node contains.
900 * A count of 1 means that the node contains only a single
901 * process. If all nodes on the system contain at most one
902 * process then we are well-converged.
904 static int count_node_processes(int node)
909 for (p = 0; p < g->p.nr_proc; p++) {
910 for (t = 0; t < g->p.nr_threads; t++) {
911 struct thread_data *td;
915 task_nr = p*g->p.nr_threads + t;
916 td = g->threads + task_nr;
918 n = numa_node_of_cpu(td->curr_cpu);
929 static void calc_convergence_compression(int *strong)
931 unsigned int nodes_min, nodes_max;
937 for (p = 0; p < g->p.nr_proc; p++) {
938 unsigned int nodes = count_process_nodes(p);
945 nodes_min = min(nodes, nodes_min);
946 nodes_max = max(nodes, nodes_max);
949 /* Strong convergence: all threads compress on a single node: */
950 if (nodes_min == 1 && nodes_max == 1) {
954 tprintf(" {%d-%d}", nodes_min, nodes_max);
958 static void calc_convergence(double runtime_ns_max, double *convergence)
960 unsigned int loops_done_min, loops_done_max;
962 int nodes[MAX_NR_NODES];
973 if (!g->p.show_convergence && !g->p.measure_convergence)
976 for (node = 0; node < g->p.nr_nodes; node++)
982 for (t = 0; t < g->p.nr_tasks; t++) {
983 struct thread_data *td = g->threads + t;
984 unsigned int loops_done;
988 /* Not all threads have written it yet: */
992 node = numa_node_of_cpu(cpu);
996 loops_done = td->loops_done;
997 loops_done_min = min(loops_done, loops_done_min);
998 loops_done_max = max(loops_done, loops_done_max);
1002 nr_min = g->p.nr_tasks;
1005 for (node = 0; node < g->p.nr_nodes; node++) {
1006 if (!is_node_present(node))
1009 nr_min = min(nr, nr_min);
1010 nr_max = max(nr, nr_max);
1013 BUG_ON(nr_min > nr_max);
1015 BUG_ON(sum > g->p.nr_tasks);
1017 if (0 && (sum < g->p.nr_tasks))
1021 * Count the number of distinct process groups present
1022 * on nodes - when we are converged this will decrease
1027 for (node = 0; node < g->p.nr_nodes; node++) {
1030 if (!is_node_present(node))
1032 processes = count_node_processes(node);
1034 tprintf(" %2d/%-2d", nr, processes);
1036 process_groups += processes;
1039 distance = nr_max - nr_min;
1041 tprintf(" [%2d/%-2d]", distance, process_groups);
1043 tprintf(" l:%3d-%-3d (%3d)",
1044 loops_done_min, loops_done_max, loops_done_max-loops_done_min);
1046 if (loops_done_min && loops_done_max) {
1047 double skew = 1.0 - (double)loops_done_min/loops_done_max;
1049 tprintf(" [%4.1f%%]", skew * 100.0);
1052 calc_convergence_compression(&strong);
1054 if (strong && process_groups == g->p.nr_proc) {
1055 if (!*convergence) {
1056 *convergence = runtime_ns_max;
1057 tprintf(" (%6.1fs converged)\n", *convergence/1e9);
1058 if (g->p.measure_convergence) {
1059 g->all_converged = true;
1060 g->stop_work = true;
1065 tprintf(" (%6.1fs de-converged)", runtime_ns_max/1e9);
1072 static void show_summary(double runtime_ns_max, int l, double *convergence)
1074 tprintf("\r # %5.1f%% [%.1f mins]",
1075 (double)(l+1)/g->p.nr_loops*100.0, runtime_ns_max/1e9 / 60.0);
1077 calc_convergence(runtime_ns_max, convergence);
1079 if (g->p.show_details >= 0)
1083 static void *worker_thread(void *__tdata)
1085 struct thread_data *td = __tdata;
1086 struct timeval start0, start, stop, diff;
1087 int process_nr = td->process_nr;
1088 int thread_nr = td->thread_nr;
1089 unsigned long last_perturbance;
1090 int task_nr = td->task_nr;
1091 int details = g->p.show_details;
1092 int first_task, last_task;
1093 double convergence = 0;
1095 double runtime_ns_max;
1102 struct rusage rusage;
1104 bind_to_cpumask(td->bind_cpumask);
1105 bind_to_memnode(td->bind_node);
1107 set_taskname("thread %d/%d", process_nr, thread_nr);
1109 global_data = g->data;
1110 process_data = td->process_data;
1111 thread_data = setup_private_data(g->p.bytes_thread);
1116 if (process_nr == g->p.nr_proc-1 && thread_nr == g->p.nr_threads-1)
1120 if (process_nr == 0 && thread_nr == 0)
1124 printf("# thread %2d / %2d global mem: %p, process mem: %p, thread mem: %p\n",
1125 process_nr, thread_nr, global_data, process_data, thread_data);
1128 if (g->p.serialize_startup) {
1129 pthread_mutex_lock(&g->startup_mutex);
1130 g->nr_tasks_started++;
1131 pthread_mutex_unlock(&g->startup_mutex);
1133 /* Here we will wait for the main process to start us all at once: */
1134 pthread_mutex_lock(&g->start_work_mutex);
1135 g->nr_tasks_working++;
1137 /* Last one wake the main process: */
1138 if (g->nr_tasks_working == g->p.nr_tasks)
1139 pthread_mutex_unlock(&g->startup_done_mutex);
1141 pthread_mutex_unlock(&g->start_work_mutex);
1144 gettimeofday(&start0, NULL);
1146 start = stop = start0;
1147 last_perturbance = start.tv_sec;
1149 for (l = 0; l < g->p.nr_loops; l++) {
1155 val += do_work(global_data, g->p.bytes_global, process_nr, g->p.nr_proc, l, val);
1156 val += do_work(process_data, g->p.bytes_process, thread_nr, g->p.nr_threads, l, val);
1157 val += do_work(thread_data, g->p.bytes_thread, 0, 1, l, val);
1159 if (g->p.sleep_usecs) {
1160 pthread_mutex_lock(td->process_lock);
1161 usleep(g->p.sleep_usecs);
1162 pthread_mutex_unlock(td->process_lock);
1165 * Amount of work to be done under a process-global lock:
1167 if (g->p.bytes_process_locked) {
1168 pthread_mutex_lock(td->process_lock);
1169 val += do_work(process_data, g->p.bytes_process_locked, thread_nr, g->p.nr_threads, l, val);
1170 pthread_mutex_unlock(td->process_lock);
1173 work_done = g->p.bytes_global + g->p.bytes_process +
1174 g->p.bytes_process_locked + g->p.bytes_thread;
1176 update_curr_cpu(task_nr, work_done);
1177 bytes_done += work_done;
1179 if (details < 0 && !g->p.perturb_secs && !g->p.measure_convergence && !g->p.nr_secs)
1184 gettimeofday(&stop, NULL);
1186 /* Check whether our max runtime timed out: */
1188 timersub(&stop, &start0, &diff);
1189 if ((u32)diff.tv_sec >= g->p.nr_secs) {
1190 g->stop_work = true;
1195 /* Update the summary at most once per second: */
1196 if (start.tv_sec == stop.tv_sec)
1200 * Perturb the first task's equilibrium every g->p.perturb_secs seconds,
1201 * by migrating to CPU#0:
1203 if (first_task && g->p.perturb_secs && (int)(stop.tv_sec - last_perturbance) >= g->p.perturb_secs) {
1204 cpu_set_t orig_mask;
1208 last_perturbance = stop.tv_sec;
1211 * Depending on where we are running, move into
1212 * the other half of the system, to create some
1215 this_cpu = g->threads[task_nr].curr_cpu;
1216 if (this_cpu < g->p.nr_cpus/2)
1217 target_cpu = g->p.nr_cpus-1;
1221 orig_mask = bind_to_cpu(target_cpu);
1223 /* Here we are running on the target CPU already */
1225 printf(" (injecting perturbalance, moved to CPU#%d)\n", target_cpu);
1227 bind_to_cpumask(orig_mask);
1231 timersub(&stop, &start, &diff);
1232 runtime_ns_max = diff.tv_sec * 1000000000;
1233 runtime_ns_max += diff.tv_usec * 1000;
1236 printf(" #%2d / %2d: %14.2lf nsecs/op [val: %016"PRIx64"]\n",
1237 process_nr, thread_nr, runtime_ns_max / bytes_done, val);
1244 timersub(&stop, &start0, &diff);
1245 runtime_ns_max = diff.tv_sec * 1000000000ULL;
1246 runtime_ns_max += diff.tv_usec * 1000ULL;
1248 show_summary(runtime_ns_max, l, &convergence);
1251 gettimeofday(&stop, NULL);
1252 timersub(&stop, &start0, &diff);
1253 td->runtime_ns = diff.tv_sec * 1000000000ULL;
1254 td->runtime_ns += diff.tv_usec * 1000ULL;
1255 td->speed_gbs = bytes_done / (td->runtime_ns / 1e9) / 1e9;
1257 getrusage(RUSAGE_THREAD, &rusage);
1258 td->system_time_ns = rusage.ru_stime.tv_sec * 1000000000ULL;
1259 td->system_time_ns += rusage.ru_stime.tv_usec * 1000ULL;
1260 td->user_time_ns = rusage.ru_utime.tv_sec * 1000000000ULL;
1261 td->user_time_ns += rusage.ru_utime.tv_usec * 1000ULL;
1263 free_data(thread_data, g->p.bytes_thread);
1265 pthread_mutex_lock(&g->stop_work_mutex);
1266 g->bytes_done += bytes_done;
1267 pthread_mutex_unlock(&g->stop_work_mutex);
1273 * A worker process starts a couple of threads:
1275 static void worker_process(int process_nr)
1277 pthread_mutex_t process_lock;
1278 struct thread_data *td;
1279 pthread_t *pthreads;
1285 pthread_mutex_init(&process_lock, NULL);
1286 set_taskname("process %d", process_nr);
1289 * Pick up the memory policy and the CPU binding of our first thread,
1290 * so that we initialize memory accordingly:
1292 task_nr = process_nr*g->p.nr_threads;
1293 td = g->threads + task_nr;
1295 bind_to_memnode(td->bind_node);
1296 bind_to_cpumask(td->bind_cpumask);
1298 pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
1299 process_data = setup_private_data(g->p.bytes_process);
1301 if (g->p.show_details >= 3) {
1302 printf(" # process %2d global mem: %p, process mem: %p\n",
1303 process_nr, g->data, process_data);
1306 for (t = 0; t < g->p.nr_threads; t++) {
1307 task_nr = process_nr*g->p.nr_threads + t;
1308 td = g->threads + task_nr;
1310 td->process_data = process_data;
1311 td->process_nr = process_nr;
1313 td->task_nr = task_nr;
1316 td->process_lock = &process_lock;
1318 ret = pthread_create(pthreads + t, NULL, worker_thread, td);
1322 for (t = 0; t < g->p.nr_threads; t++) {
1323 ret = pthread_join(pthreads[t], NULL);
1327 free_data(process_data, g->p.bytes_process);
1331 static void print_summary(void)
1333 if (g->p.show_details < 0)
1337 printf(" # %d %s will execute (on %d nodes, %d CPUs):\n",
1338 g->p.nr_tasks, g->p.nr_tasks == 1 ? "task" : "tasks", nr_numa_nodes(), g->p.nr_cpus);
1339 printf(" # %5dx %5ldMB global shared mem operations\n",
1340 g->p.nr_loops, g->p.bytes_global/1024/1024);
1341 printf(" # %5dx %5ldMB process shared mem operations\n",
1342 g->p.nr_loops, g->p.bytes_process/1024/1024);
1343 printf(" # %5dx %5ldMB thread local mem operations\n",
1344 g->p.nr_loops, g->p.bytes_thread/1024/1024);
1348 printf("\n ###\n"); fflush(stdout);
1351 static void init_thread_data(void)
1353 ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
1356 g->threads = zalloc_shared_data(size);
1358 for (t = 0; t < g->p.nr_tasks; t++) {
1359 struct thread_data *td = g->threads + t;
1362 /* Allow all nodes by default: */
1365 /* Allow all CPUs by default: */
1366 CPU_ZERO(&td->bind_cpumask);
1367 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
1368 CPU_SET(cpu, &td->bind_cpumask);
1372 static void deinit_thread_data(void)
1374 ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
1376 free_data(g->threads, size);
1379 static int init(void)
1381 g = (void *)alloc_data(sizeof(*g), MAP_SHARED, 1, 0, 0 /* THP */, 0);
1383 /* Copy over options: */
1386 g->p.nr_cpus = numa_num_configured_cpus();
1388 g->p.nr_nodes = numa_max_node() + 1;
1390 /* char array in count_process_nodes(): */
1391 BUG_ON(g->p.nr_nodes > MAX_NR_NODES || g->p.nr_nodes < 0);
1393 if (g->p.show_quiet && !g->p.show_details)
1394 g->p.show_details = -1;
1396 /* Some memory should be specified: */
1397 if (!g->p.mb_global_str && !g->p.mb_proc_str && !g->p.mb_thread_str)
1400 if (g->p.mb_global_str) {
1401 g->p.mb_global = atof(g->p.mb_global_str);
1402 BUG_ON(g->p.mb_global < 0);
1405 if (g->p.mb_proc_str) {
1406 g->p.mb_proc = atof(g->p.mb_proc_str);
1407 BUG_ON(g->p.mb_proc < 0);
1410 if (g->p.mb_proc_locked_str) {
1411 g->p.mb_proc_locked = atof(g->p.mb_proc_locked_str);
1412 BUG_ON(g->p.mb_proc_locked < 0);
1413 BUG_ON(g->p.mb_proc_locked > g->p.mb_proc);
1416 if (g->p.mb_thread_str) {
1417 g->p.mb_thread = atof(g->p.mb_thread_str);
1418 BUG_ON(g->p.mb_thread < 0);
1421 BUG_ON(g->p.nr_threads <= 0);
1422 BUG_ON(g->p.nr_proc <= 0);
1424 g->p.nr_tasks = g->p.nr_proc*g->p.nr_threads;
1426 g->p.bytes_global = g->p.mb_global *1024L*1024L;
1427 g->p.bytes_process = g->p.mb_proc *1024L*1024L;
1428 g->p.bytes_process_locked = g->p.mb_proc_locked *1024L*1024L;
1429 g->p.bytes_thread = g->p.mb_thread *1024L*1024L;
1431 g->data = setup_shared_data(g->p.bytes_global);
1433 /* Startup serialization: */
1434 init_global_mutex(&g->start_work_mutex);
1435 init_global_mutex(&g->startup_mutex);
1436 init_global_mutex(&g->startup_done_mutex);
1437 init_global_mutex(&g->stop_work_mutex);
1442 if (parse_setup_cpu_list() || parse_setup_node_list())
1451 static void deinit(void)
1453 free_data(g->data, g->p.bytes_global);
1456 deinit_thread_data();
1458 free_data(g, sizeof(*g));
1463 * Print a short or long result, depending on the verbosity setting:
1465 static void print_res(const char *name, double val,
1466 const char *txt_unit, const char *txt_short, const char *txt_long)
1471 if (!g->p.show_quiet)
1472 printf(" %-30s %15.3f, %-15s %s\n", name, val, txt_unit, txt_short);
1474 printf(" %14.3f %s\n", val, txt_long);
1477 static int __bench_numa(const char *name)
1479 struct timeval start, stop, diff;
1480 u64 runtime_ns_min, runtime_ns_sum;
1481 pid_t *pids, pid, wpid;
1482 double delta_runtime;
1484 double runtime_sec_max;
1485 double runtime_sec_min;
1493 pids = zalloc(g->p.nr_proc * sizeof(*pids));
1496 /* All threads try to acquire it, this way we can wait for them to start up: */
1497 pthread_mutex_lock(&g->start_work_mutex);
1499 if (g->p.serialize_startup) {
1501 tprintf(" # Startup synchronization: ..."); fflush(stdout);
1504 gettimeofday(&start, NULL);
1506 for (i = 0; i < g->p.nr_proc; i++) {
1508 dprintf(" # process %2d: PID %d\n", i, pid);
1512 /* Child process: */
1520 /* Wait for all the threads to start up: */
1521 while (g->nr_tasks_started != g->p.nr_tasks)
1524 BUG_ON(g->nr_tasks_started != g->p.nr_tasks);
1526 if (g->p.serialize_startup) {
1529 pthread_mutex_lock(&g->startup_done_mutex);
1531 /* This will start all threads: */
1532 pthread_mutex_unlock(&g->start_work_mutex);
1534 /* This mutex is locked - the last started thread will wake us: */
1535 pthread_mutex_lock(&g->startup_done_mutex);
1537 gettimeofday(&stop, NULL);
1539 timersub(&stop, &start, &diff);
1541 startup_sec = diff.tv_sec * 1000000000.0;
1542 startup_sec += diff.tv_usec * 1000.0;
1545 tprintf(" threads initialized in %.6f seconds.\n", startup_sec);
1549 pthread_mutex_unlock(&g->startup_done_mutex);
1551 gettimeofday(&start, NULL);
1554 /* Parent process: */
1557 for (i = 0; i < g->p.nr_proc; i++) {
1558 wpid = waitpid(pids[i], &wait_stat, 0);
1560 BUG_ON(!WIFEXITED(wait_stat));
1565 runtime_ns_min = -1LL;
1567 for (t = 0; t < g->p.nr_tasks; t++) {
1568 u64 thread_runtime_ns = g->threads[t].runtime_ns;
1570 runtime_ns_sum += thread_runtime_ns;
1571 runtime_ns_min = min(thread_runtime_ns, runtime_ns_min);
1574 gettimeofday(&stop, NULL);
1575 timersub(&stop, &start, &diff);
1577 BUG_ON(bench_format != BENCH_FORMAT_DEFAULT);
1579 tprintf("\n ###\n");
1582 runtime_sec_max = diff.tv_sec * 1000000000.0;
1583 runtime_sec_max += diff.tv_usec * 1000.0;
1584 runtime_sec_max /= 1e9;
1586 runtime_sec_min = runtime_ns_min/1e9;
1588 bytes = g->bytes_done;
1589 runtime_avg = (double)runtime_ns_sum / g->p.nr_tasks / 1e9;
1591 if (g->p.measure_convergence) {
1592 print_res(name, runtime_sec_max,
1593 "secs,", "NUMA-convergence-latency", "secs latency to NUMA-converge");
1596 print_res(name, runtime_sec_max,
1597 "secs,", "runtime-max/thread", "secs slowest (max) thread-runtime");
1599 print_res(name, runtime_sec_min,
1600 "secs,", "runtime-min/thread", "secs fastest (min) thread-runtime");
1602 print_res(name, runtime_avg,
1603 "secs,", "runtime-avg/thread", "secs average thread-runtime");
1605 delta_runtime = (runtime_sec_max - runtime_sec_min)/2.0;
1606 print_res(name, delta_runtime / runtime_sec_max * 100.0,
1607 "%,", "spread-runtime/thread", "% difference between max/avg runtime");
1609 print_res(name, bytes / g->p.nr_tasks / 1e9,
1610 "GB,", "data/thread", "GB data processed, per thread");
1612 print_res(name, bytes / 1e9,
1613 "GB,", "data-total", "GB data processed, total");
1615 print_res(name, runtime_sec_max * 1e9 / (bytes / g->p.nr_tasks),
1616 "nsecs,", "runtime/byte/thread","nsecs/byte/thread runtime");
1618 print_res(name, bytes / g->p.nr_tasks / 1e9 / runtime_sec_max,
1619 "GB/sec,", "thread-speed", "GB/sec/thread speed");
1621 print_res(name, bytes / runtime_sec_max / 1e9,
1622 "GB/sec,", "total-speed", "GB/sec total speed");
1624 if (g->p.show_details >= 2) {
1625 char tname[14 + 2 * 10 + 1];
1626 struct thread_data *td;
1627 for (p = 0; p < g->p.nr_proc; p++) {
1628 for (t = 0; t < g->p.nr_threads; t++) {
1629 memset(tname, 0, sizeof(tname));
1630 td = g->threads + p*g->p.nr_threads + t;
1631 snprintf(tname, sizeof(tname), "process%d:thread%d", p, t);
1632 print_res(tname, td->speed_gbs,
1633 "GB/sec", "thread-speed", "GB/sec/thread speed");
1634 print_res(tname, td->system_time_ns / 1e9,
1635 "secs", "thread-system-time", "system CPU time/thread");
1636 print_res(tname, td->user_time_ns / 1e9,
1637 "secs", "thread-user-time", "user CPU time/thread");
1651 static int command_size(const char **argv)
1660 BUG_ON(size >= MAX_ARGS);
1665 static void init_params(struct params *p, const char *name, int argc, const char **argv)
1669 printf("\n # Running %s \"perf bench numa", name);
1671 for (i = 0; i < argc; i++)
1672 printf(" %s", argv[i]);
1676 memset(p, 0, sizeof(*p));
1678 /* Initialize nonzero defaults: */
1680 p->serialize_startup = 1;
1681 p->data_reads = true;
1682 p->data_writes = true;
1683 p->data_backwards = true;
1684 p->data_rand_walk = true;
1686 p->init_random = true;
1687 p->mb_global_str = "1";
1691 p->run_all = argc == 1;
1694 static int run_bench_numa(const char *name, const char **argv)
1696 int argc = command_size(argv);
1698 init_params(&p0, name, argc, argv);
1699 argc = parse_options(argc, argv, options, bench_numa_usage, 0);
1703 if (__bench_numa(name))
1712 #define OPT_BW_RAM "-s", "20", "-zZq", "--thp", " 1", "--no-data_rand_walk"
1713 #define OPT_BW_RAM_NOTHP OPT_BW_RAM, "--thp", "-1"
1715 #define OPT_CONV "-s", "100", "-zZ0qcm", "--thp", " 1"
1716 #define OPT_CONV_NOTHP OPT_CONV, "--thp", "-1"
1718 #define OPT_BW "-s", "20", "-zZ0q", "--thp", " 1"
1719 #define OPT_BW_NOTHP OPT_BW, "--thp", "-1"
1722 * The built-in test-suite executed by "perf bench numa -a".
1724 * (A minimum of 4 nodes and 16 GB of RAM is recommended.)
1726 static const char *tests[][MAX_ARGS] = {
1727 /* Basic single-stream NUMA bandwidth measurements: */
1728 { "RAM-bw-local,", "mem", "-p", "1", "-t", "1", "-P", "1024",
1729 "-C" , "0", "-M", "0", OPT_BW_RAM },
1730 { "RAM-bw-local-NOTHP,",
1731 "mem", "-p", "1", "-t", "1", "-P", "1024",
1732 "-C" , "0", "-M", "0", OPT_BW_RAM_NOTHP },
1733 { "RAM-bw-remote,", "mem", "-p", "1", "-t", "1", "-P", "1024",
1734 "-C" , "0", "-M", "1", OPT_BW_RAM },
1736 /* 2-stream NUMA bandwidth measurements: */
1737 { "RAM-bw-local-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1738 "-C", "0,2", "-M", "0x2", OPT_BW_RAM },
1739 { "RAM-bw-remote-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1740 "-C", "0,2", "-M", "1x2", OPT_BW_RAM },
1742 /* Cross-stream NUMA bandwidth measurement: */
1743 { "RAM-bw-cross,", "mem", "-p", "2", "-t", "1", "-P", "1024",
1744 "-C", "0,8", "-M", "1,0", OPT_BW_RAM },
1746 /* Convergence latency measurements: */
1747 { " 1x3-convergence,", "mem", "-p", "1", "-t", "3", "-P", "512", OPT_CONV },
1748 { " 1x4-convergence,", "mem", "-p", "1", "-t", "4", "-P", "512", OPT_CONV },
1749 { " 1x6-convergence,", "mem", "-p", "1", "-t", "6", "-P", "1020", OPT_CONV },
1750 { " 2x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
1751 { " 3x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
1752 { " 4x4-convergence,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV },
1753 { " 4x4-convergence-NOTHP,",
1754 "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
1755 { " 4x6-convergence,", "mem", "-p", "4", "-t", "6", "-P", "1020", OPT_CONV },
1756 { " 4x8-convergence,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_CONV },
1757 { " 8x4-convergence,", "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV },
1758 { " 8x4-convergence-NOTHP,",
1759 "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
1760 { " 3x1-convergence,", "mem", "-p", "3", "-t", "1", "-P", "512", OPT_CONV },
1761 { " 4x1-convergence,", "mem", "-p", "4", "-t", "1", "-P", "512", OPT_CONV },
1762 { " 8x1-convergence,", "mem", "-p", "8", "-t", "1", "-P", "512", OPT_CONV },
1763 { "16x1-convergence,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_CONV },
1764 { "32x1-convergence,", "mem", "-p", "32", "-t", "1", "-P", "128", OPT_CONV },
1766 /* Various NUMA process/thread layout bandwidth measurements: */
1767 { " 2x1-bw-process,", "mem", "-p", "2", "-t", "1", "-P", "1024", OPT_BW },
1768 { " 3x1-bw-process,", "mem", "-p", "3", "-t", "1", "-P", "1024", OPT_BW },
1769 { " 4x1-bw-process,", "mem", "-p", "4", "-t", "1", "-P", "1024", OPT_BW },
1770 { " 8x1-bw-process,", "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW },
1771 { " 8x1-bw-process-NOTHP,",
1772 "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW_NOTHP },
1773 { "16x1-bw-process,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_BW },
1775 { " 4x1-bw-thread,", "mem", "-p", "1", "-t", "4", "-T", "256", OPT_BW },
1776 { " 8x1-bw-thread,", "mem", "-p", "1", "-t", "8", "-T", "256", OPT_BW },
1777 { "16x1-bw-thread,", "mem", "-p", "1", "-t", "16", "-T", "128", OPT_BW },
1778 { "32x1-bw-thread,", "mem", "-p", "1", "-t", "32", "-T", "64", OPT_BW },
1780 { " 2x3-bw-thread,", "mem", "-p", "2", "-t", "3", "-P", "512", OPT_BW },
1781 { " 4x4-bw-thread,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_BW },
1782 { " 4x6-bw-thread,", "mem", "-p", "4", "-t", "6", "-P", "512", OPT_BW },
1783 { " 4x8-bw-thread,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW },
1784 { " 4x8-bw-thread-NOTHP,",
1785 "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW_NOTHP },
1786 { " 3x3-bw-thread,", "mem", "-p", "3", "-t", "3", "-P", "512", OPT_BW },
1787 { " 5x5-bw-thread,", "mem", "-p", "5", "-t", "5", "-P", "512", OPT_BW },
1789 { "2x16-bw-thread,", "mem", "-p", "2", "-t", "16", "-P", "512", OPT_BW },
1790 { "1x32-bw-thread,", "mem", "-p", "1", "-t", "32", "-P", "2048", OPT_BW },
1792 { "numa02-bw,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW },
1793 { "numa02-bw-NOTHP,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW_NOTHP },
1794 { "numa01-bw-thread,", "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW },
1795 { "numa01-bw-thread-NOTHP,",
1796 "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW_NOTHP },
1799 static int bench_all(void)
1801 int nr = ARRAY_SIZE(tests);
1805 ret = system("echo ' #'; echo ' # Running test on: '$(uname -a); echo ' #'");
1808 for (i = 0; i < nr; i++) {
1809 run_bench_numa(tests[i][0], tests[i] + 1);
1817 int bench_numa(int argc, const char **argv, const char *prefix __maybe_unused)
1819 init_params(&p0, "main,", argc, argv);
1820 argc = parse_options(argc, argv, options, bench_numa_usage, 0);
1827 if (__bench_numa(NULL))
1833 usage_with_options(numa_usage, options);