4 * numa: Simulate NUMA-sensitive workload and measure their NUMA performance
7 /* For the CLR_() macros */
11 #include "../builtin.h"
12 #include "../util/util.h"
13 #include <subcmd/parse-options.h>
14 #include "../util/cloexec.h"
29 #include <sys/resource.h>
31 #include <sys/prctl.h>
32 #include <sys/types.h>
33 #include <linux/time64.h>
39 * Regular printout to the terminal, supressed if -q is specified:
41 #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>]",
215 static cpu_set_t bind_to_cpu(int target_cpu)
217 cpu_set_t orig_mask, mask;
220 ret = sched_getaffinity(0, sizeof(orig_mask), &orig_mask);
225 if (target_cpu == -1) {
228 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
231 BUG_ON(target_cpu < 0 || target_cpu >= g->p.nr_cpus);
232 CPU_SET(target_cpu, &mask);
235 ret = sched_setaffinity(0, sizeof(mask), &mask);
241 static cpu_set_t bind_to_node(int target_node)
243 int cpus_per_node = g->p.nr_cpus/g->p.nr_nodes;
244 cpu_set_t orig_mask, mask;
248 BUG_ON(cpus_per_node*g->p.nr_nodes != g->p.nr_cpus);
249 BUG_ON(!cpus_per_node);
251 ret = sched_getaffinity(0, sizeof(orig_mask), &orig_mask);
256 if (target_node == -1) {
257 for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
260 int cpu_start = (target_node + 0) * cpus_per_node;
261 int cpu_stop = (target_node + 1) * cpus_per_node;
263 BUG_ON(cpu_stop > g->p.nr_cpus);
265 for (cpu = cpu_start; cpu < cpu_stop; cpu++)
269 ret = sched_setaffinity(0, sizeof(mask), &mask);
275 static void bind_to_cpumask(cpu_set_t mask)
279 ret = sched_setaffinity(0, sizeof(mask), &mask);
283 static void mempol_restore(void)
287 ret = set_mempolicy(MPOL_DEFAULT, NULL, g->p.nr_nodes-1);
292 static void bind_to_memnode(int node)
294 unsigned long nodemask;
300 BUG_ON(g->p.nr_nodes > (int)sizeof(nodemask)*8);
301 nodemask = 1L << node;
303 ret = set_mempolicy(MPOL_BIND, &nodemask, sizeof(nodemask)*8);
304 dprintf("binding to node %d, mask: %016lx => %d\n", node, nodemask, ret);
309 #define HPSIZE (2*1024*1024)
311 #define set_taskname(fmt...) \
315 snprintf(name, 20, fmt); \
316 prctl(PR_SET_NAME, name); \
319 static u8 *alloc_data(ssize_t bytes0, int map_flags,
320 int init_zero, int init_cpu0, int thp, int init_random)
330 /* Allocate and initialize all memory on CPU#0: */
332 orig_mask = bind_to_node(0);
336 bytes = bytes0 + HPSIZE;
338 buf = (void *)mmap(0, bytes, PROT_READ|PROT_WRITE, MAP_ANON|map_flags, -1, 0);
339 BUG_ON(buf == (void *)-1);
341 if (map_flags == MAP_PRIVATE) {
343 ret = madvise(buf, bytes, MADV_HUGEPAGE);
344 if (ret && !g->print_once) {
346 printf("WARNING: Could not enable THP - do: 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled'\n");
350 ret = madvise(buf, bytes, MADV_NOHUGEPAGE);
351 if (ret && !g->print_once) {
353 printf("WARNING: Could not disable THP: run a CONFIG_TRANSPARENT_HUGEPAGE kernel?\n");
361 /* Initialize random contents, different in each word: */
363 u64 *wbuf = (void *)buf;
367 for (i = 0; i < bytes/8; i++)
372 /* Align to 2MB boundary: */
373 buf = (void *)(((unsigned long)buf + HPSIZE-1) & ~(HPSIZE-1));
375 /* Restore affinity: */
377 bind_to_cpumask(orig_mask);
384 static void free_data(void *data, ssize_t bytes)
391 ret = munmap(data, bytes);
396 * Create a shared memory buffer that can be shared between processes, zeroed:
398 static void * zalloc_shared_data(ssize_t bytes)
400 return alloc_data(bytes, MAP_SHARED, 1, g->p.init_cpu0, g->p.thp, g->p.init_random);
404 * Create a shared memory buffer that can be shared between processes:
406 static void * setup_shared_data(ssize_t bytes)
408 return alloc_data(bytes, MAP_SHARED, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
412 * Allocate process-local memory - this will either be shared between
413 * threads of this process, or only be accessed by this thread:
415 static void * setup_private_data(ssize_t bytes)
417 return alloc_data(bytes, MAP_PRIVATE, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
421 * Return a process-shared (global) mutex:
423 static void init_global_mutex(pthread_mutex_t *mutex)
425 pthread_mutexattr_t attr;
427 pthread_mutexattr_init(&attr);
428 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
429 pthread_mutex_init(mutex, &attr);
432 static int parse_cpu_list(const char *arg)
434 p0.cpu_list_str = strdup(arg);
436 dprintf("got CPU list: {%s}\n", p0.cpu_list_str);
441 static int parse_setup_cpu_list(void)
443 struct thread_data *td;
447 if (!g->p.cpu_list_str)
450 dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
452 str0 = str = strdup(g->p.cpu_list_str);
457 tprintf("# binding tasks to CPUs:\n");
461 int bind_cpu, bind_cpu_0, bind_cpu_1;
462 char *tok, *tok_end, *tok_step, *tok_len, *tok_mul;
467 tok = strsep(&str, ",");
471 tok_end = strstr(tok, "-");
473 dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
475 /* Single CPU specified: */
476 bind_cpu_0 = bind_cpu_1 = atol(tok);
478 /* CPU range specified (for example: "5-11"): */
479 bind_cpu_0 = atol(tok);
480 bind_cpu_1 = atol(tok_end + 1);
484 tok_step = strstr(tok, "#");
486 step = atol(tok_step + 1);
487 BUG_ON(step <= 0 || step >= g->p.nr_cpus);
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.
496 tok_len = strstr(tok, "_");
498 bind_len = atol(tok_len + 1);
499 BUG_ON(bind_len <= 0 || bind_len > g->p.nr_cpus);
502 /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
504 tok_mul = strstr(tok, "x");
506 mul = atol(tok_mul + 1);
510 dprintf("CPUs: %d_%d-%d#%dx%d\n", bind_cpu_0, bind_len, bind_cpu_1, step, mul);
512 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);
517 BUG_ON(bind_cpu_0 < 0 || bind_cpu_1 < 0);
518 BUG_ON(bind_cpu_0 > bind_cpu_1);
520 for (bind_cpu = bind_cpu_0; bind_cpu <= bind_cpu_1; bind_cpu += step) {
523 for (i = 0; i < mul; i++) {
526 if (t >= g->p.nr_tasks) {
527 printf("\n# NOTE: ignoring bind CPUs starting at CPU#%d\n #", bind_cpu);
535 tprintf("%2d/%d", bind_cpu, bind_len);
537 tprintf("%2d", bind_cpu);
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);
553 if (t < g->p.nr_tasks)
554 printf("# NOTE: %d tasks bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
560 static int parse_cpus_opt(const struct option *opt __maybe_unused,
561 const char *arg, int unset __maybe_unused)
566 return parse_cpu_list(arg);
569 static int parse_node_list(const char *arg)
571 p0.node_list_str = strdup(arg);
573 dprintf("got NODE list: {%s}\n", p0.node_list_str);
578 static int parse_setup_node_list(void)
580 struct thread_data *td;
584 if (!g->p.node_list_str)
587 dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
589 str0 = str = strdup(g->p.node_list_str);
594 tprintf("# binding tasks to NODEs:\n");
598 int bind_node, bind_node_0, bind_node_1;
599 char *tok, *tok_end, *tok_step, *tok_mul;
603 tok = strsep(&str, ",");
607 tok_end = strstr(tok, "-");
609 dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
611 /* Single NODE specified: */
612 bind_node_0 = bind_node_1 = atol(tok);
614 /* NODE range specified (for example: "5-11"): */
615 bind_node_0 = atol(tok);
616 bind_node_1 = atol(tok_end + 1);
620 tok_step = strstr(tok, "#");
622 step = atol(tok_step + 1);
623 BUG_ON(step <= 0 || step >= g->p.nr_nodes);
626 /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
628 tok_mul = strstr(tok, "x");
630 mul = atol(tok_mul + 1);
634 dprintf("NODEs: %d-%d #%d\n", bind_node_0, bind_node_1, step);
636 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);
641 BUG_ON(bind_node_0 < 0 || bind_node_1 < 0);
642 BUG_ON(bind_node_0 > bind_node_1);
644 for (bind_node = bind_node_0; bind_node <= bind_node_1; bind_node += step) {
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);
655 tprintf(" %2d", bind_node);
657 tprintf(",%2d", bind_node);
659 td->bind_node = bind_node;
668 if (t < g->p.nr_tasks)
669 printf("# NOTE: %d tasks mem-bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
675 static int parse_nodes_opt(const struct option *opt __maybe_unused,
676 const char *arg, int unset __maybe_unused)
681 return parse_node_list(arg);
686 #define BIT(x) (1ul << x)
688 static inline uint32_t lfsr_32(uint32_t lfsr)
690 const uint32_t taps = BIT(1) | BIT(5) | BIT(6) | BIT(31);
691 return (lfsr>>1) ^ ((0x0u - (lfsr & 0x1u)) & taps);
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
700 static inline u64 access_data(u64 *data __attribute__((unused)), u64 val)
704 if (g->p.data_writes)
710 * The worker process does two types of work, a forwards going
711 * loop and a backwards going loop.
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.
717 static u64 do_work(u8 *__data, long bytes, int nr, int nr_max, int loop, u64 val)
719 long words = bytes/sizeof(u64);
720 u64 *data = (void *)__data;
721 long chunk_0, chunk_1;
726 BUG_ON(!data && words);
727 BUG_ON(data && !words);
732 /* Very simple memset() work variant: */
733 if (g->p.data_zero_memset && !g->p.data_rand_walk) {
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;
746 if (g->p.data_rand_walk) {
747 u32 lfsr = nr + loop + val;
750 for (i = 0; i < words/1024; i++) {
753 lfsr = lfsr_32(lfsr);
755 start = lfsr % words;
756 end = min(start + 1024, words-1);
758 if (g->p.data_zero_memset) {
759 bzero(data + start, (end-start) * sizeof(u64));
761 for (j = start; j < end; j++)
762 val = access_data(data + j, val);
765 } else if (!g->p.data_backwards || (nr + loop) & 1) {
771 /* Process data forwards: */
773 if (unlikely(d >= d1))
775 if (unlikely(d == d0))
778 val = access_data(d, val);
783 /* Process data backwards: */
789 /* Process data forwards: */
791 if (unlikely(d < data))
793 if (unlikely(d == d0))
796 val = access_data(d, val);
805 static void update_curr_cpu(int task_nr, unsigned long bytes_worked)
809 cpu = sched_getcpu();
811 g->threads[task_nr].curr_cpu = cpu;
812 prctl(0, bytes_worked);
815 #define MAX_NR_NODES 64
818 * Count the number of nodes a process's threads
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.
825 static int count_process_nodes(int process_nr)
827 char node_present[MAX_NR_NODES] = { 0, };
831 for (t = 0; t < g->p.nr_threads; t++) {
832 struct thread_data *td;
836 task_nr = process_nr*g->p.nr_threads + t;
837 td = g->threads + task_nr;
839 node = numa_node_of_cpu(td->curr_cpu);
840 if (node < 0) /* curr_cpu was likely still -1 */
843 node_present[node] = 1;
848 for (n = 0; n < MAX_NR_NODES; n++)
849 nodes += node_present[n];
855 * Count the number of distinct process-threads a node contains.
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.
861 static int count_node_processes(int node)
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;
872 task_nr = p*g->p.nr_threads + t;
873 td = g->threads + task_nr;
875 n = numa_node_of_cpu(td->curr_cpu);
886 static void calc_convergence_compression(int *strong)
888 unsigned int nodes_min, nodes_max;
894 for (p = 0; p < g->p.nr_proc; p++) {
895 unsigned int nodes = count_process_nodes(p);
902 nodes_min = min(nodes, nodes_min);
903 nodes_max = max(nodes, nodes_max);
906 /* Strong convergence: all threads compress on a single node: */
907 if (nodes_min == 1 && nodes_max == 1) {
911 tprintf(" {%d-%d}", nodes_min, nodes_max);
915 static void calc_convergence(double runtime_ns_max, double *convergence)
917 unsigned int loops_done_min, loops_done_max;
919 int nodes[MAX_NR_NODES];
930 if (!g->p.show_convergence && !g->p.measure_convergence)
933 for (node = 0; node < g->p.nr_nodes; node++)
939 for (t = 0; t < g->p.nr_tasks; t++) {
940 struct thread_data *td = g->threads + t;
941 unsigned int loops_done;
945 /* Not all threads have written it yet: */
949 node = numa_node_of_cpu(cpu);
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);
959 nr_min = g->p.nr_tasks;
962 for (node = 0; node < g->p.nr_nodes; node++) {
964 nr_min = min(nr, nr_min);
965 nr_max = max(nr, nr_max);
968 BUG_ON(nr_min > nr_max);
970 BUG_ON(sum > g->p.nr_tasks);
972 if (0 && (sum < g->p.nr_tasks))
976 * Count the number of distinct process groups present
977 * on nodes - when we are converged this will decrease
982 for (node = 0; node < g->p.nr_nodes; node++) {
983 int processes = count_node_processes(node);
986 tprintf(" %2d/%-2d", nr, processes);
988 process_groups += processes;
991 distance = nr_max - nr_min;
993 tprintf(" [%2d/%-2d]", distance, process_groups);
995 tprintf(" l:%3d-%-3d (%3d)",
996 loops_done_min, loops_done_max, loops_done_max-loops_done_min);
998 if (loops_done_min && loops_done_max) {
999 double skew = 1.0 - (double)loops_done_min/loops_done_max;
1001 tprintf(" [%4.1f%%]", skew * 100.0);
1004 calc_convergence_compression(&strong);
1006 if (strong && process_groups == g->p.nr_proc) {
1007 if (!*convergence) {
1008 *convergence = runtime_ns_max;
1009 tprintf(" (%6.1fs converged)\n", *convergence / NSEC_PER_SEC);
1010 if (g->p.measure_convergence) {
1011 g->all_converged = true;
1012 g->stop_work = true;
1017 tprintf(" (%6.1fs de-converged)", runtime_ns_max / NSEC_PER_SEC);
1024 static void show_summary(double runtime_ns_max, int l, double *convergence)
1026 tprintf("\r # %5.1f%% [%.1f mins]",
1027 (double)(l+1)/g->p.nr_loops*100.0, runtime_ns_max / NSEC_PER_SEC / 60.0);
1029 calc_convergence(runtime_ns_max, convergence);
1031 if (g->p.show_details >= 0)
1035 static void *worker_thread(void *__tdata)
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;
1047 double runtime_ns_max;
1054 struct rusage rusage;
1056 bind_to_cpumask(td->bind_cpumask);
1057 bind_to_memnode(td->bind_node);
1059 set_taskname("thread %d/%d", process_nr, thread_nr);
1061 global_data = g->data;
1062 process_data = td->process_data;
1063 thread_data = setup_private_data(g->p.bytes_thread);
1068 if (process_nr == g->p.nr_proc-1 && thread_nr == g->p.nr_threads-1)
1072 if (process_nr == 0 && thread_nr == 0)
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);
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);
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++;
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);
1093 pthread_mutex_unlock(&g->start_work_mutex);
1096 gettimeofday(&start0, NULL);
1098 start = stop = start0;
1099 last_perturbance = start.tv_sec;
1101 for (l = 0; l < g->p.nr_loops; l++) {
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);
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);
1117 * Amount of work to be done under a process-global lock:
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);
1125 work_done = g->p.bytes_global + g->p.bytes_process +
1126 g->p.bytes_process_locked + g->p.bytes_thread;
1128 update_curr_cpu(task_nr, work_done);
1129 bytes_done += work_done;
1131 if (details < 0 && !g->p.perturb_secs && !g->p.measure_convergence && !g->p.nr_secs)
1136 gettimeofday(&stop, NULL);
1138 /* Check whether our max runtime timed out: */
1140 timersub(&stop, &start0, &diff);
1141 if ((u32)diff.tv_sec >= g->p.nr_secs) {
1142 g->stop_work = true;
1147 /* Update the summary at most once per second: */
1148 if (start.tv_sec == stop.tv_sec)
1152 * Perturb the first task's equilibrium every g->p.perturb_secs seconds,
1153 * by migrating to CPU#0:
1155 if (first_task && g->p.perturb_secs && (int)(stop.tv_sec - last_perturbance) >= g->p.perturb_secs) {
1156 cpu_set_t orig_mask;
1160 last_perturbance = stop.tv_sec;
1163 * Depending on where we are running, move into
1164 * the other half of the system, to create some
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;
1173 orig_mask = bind_to_cpu(target_cpu);
1175 /* Here we are running on the target CPU already */
1177 printf(" (injecting perturbalance, moved to CPU#%d)\n", target_cpu);
1179 bind_to_cpumask(orig_mask);
1183 timersub(&stop, &start, &diff);
1184 runtime_ns_max = diff.tv_sec * NSEC_PER_SEC;
1185 runtime_ns_max += diff.tv_usec * NSEC_PER_USEC;
1188 printf(" #%2d / %2d: %14.2lf nsecs/op [val: %016"PRIx64"]\n",
1189 process_nr, thread_nr, runtime_ns_max / bytes_done, val);
1196 timersub(&stop, &start0, &diff);
1197 runtime_ns_max = diff.tv_sec * NSEC_PER_SEC;
1198 runtime_ns_max += diff.tv_usec * NSEC_PER_USEC;
1200 show_summary(runtime_ns_max, l, &convergence);
1203 gettimeofday(&stop, NULL);
1204 timersub(&stop, &start0, &diff);
1205 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;
1209 getrusage(RUSAGE_THREAD, &rusage);
1210 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;
1215 free_data(thread_data, g->p.bytes_thread);
1217 pthread_mutex_lock(&g->stop_work_mutex);
1218 g->bytes_done += bytes_done;
1219 pthread_mutex_unlock(&g->stop_work_mutex);
1225 * A worker process starts a couple of threads:
1227 static void worker_process(int process_nr)
1229 pthread_mutex_t process_lock;
1230 struct thread_data *td;
1231 pthread_t *pthreads;
1237 pthread_mutex_init(&process_lock, NULL);
1238 set_taskname("process %d", process_nr);
1241 * Pick up the memory policy and the CPU binding of our first thread,
1242 * so that we initialize memory accordingly:
1244 task_nr = process_nr*g->p.nr_threads;
1245 td = g->threads + task_nr;
1247 bind_to_memnode(td->bind_node);
1248 bind_to_cpumask(td->bind_cpumask);
1250 pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
1251 process_data = setup_private_data(g->p.bytes_process);
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);
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;
1262 td->process_data = process_data;
1263 td->process_nr = process_nr;
1265 td->task_nr = task_nr;
1268 td->process_lock = &process_lock;
1270 ret = pthread_create(pthreads + t, NULL, worker_thread, td);
1274 for (t = 0; t < g->p.nr_threads; t++) {
1275 ret = pthread_join(pthreads[t], NULL);
1279 free_data(process_data, g->p.bytes_process);
1283 static void print_summary(void)
1285 if (g->p.show_details < 0)
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);
1300 printf("\n ###\n"); fflush(stdout);
1303 static void init_thread_data(void)
1305 ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
1308 g->threads = zalloc_shared_data(size);
1310 for (t = 0; t < g->p.nr_tasks; t++) {
1311 struct thread_data *td = g->threads + t;
1314 /* Allow all nodes by default: */
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);
1324 static void deinit_thread_data(void)
1326 ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
1328 free_data(g->threads, size);
1331 static int init(void)
1333 g = (void *)alloc_data(sizeof(*g), MAP_SHARED, 1, 0, 0 /* THP */, 0);
1335 /* Copy over options: */
1338 g->p.nr_cpus = numa_num_configured_cpus();
1340 g->p.nr_nodes = numa_max_node() + 1;
1342 /* char array in count_process_nodes(): */
1343 BUG_ON(g->p.nr_nodes > MAX_NR_NODES || g->p.nr_nodes < 0);
1345 if (g->p.show_quiet && !g->p.show_details)
1346 g->p.show_details = -1;
1348 /* Some memory should be specified: */
1349 if (!g->p.mb_global_str && !g->p.mb_proc_str && !g->p.mb_thread_str)
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);
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);
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);
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);
1373 BUG_ON(g->p.nr_threads <= 0);
1374 BUG_ON(g->p.nr_proc <= 0);
1376 g->p.nr_tasks = g->p.nr_proc*g->p.nr_threads;
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;
1383 g->data = setup_shared_data(g->p.bytes_global);
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);
1394 if (parse_setup_cpu_list() || parse_setup_node_list())
1403 static void deinit(void)
1405 free_data(g->data, g->p.bytes_global);
1408 deinit_thread_data();
1410 free_data(g, sizeof(*g));
1415 * Print a short or long result, depending on the verbosity setting:
1417 static void print_res(const char *name, double val,
1418 const char *txt_unit, const char *txt_short, const char *txt_long)
1423 if (!g->p.show_quiet)
1424 printf(" %-30s %15.3f, %-15s %s\n", name, val, txt_unit, txt_short);
1426 printf(" %14.3f %s\n", val, txt_long);
1429 static int __bench_numa(const char *name)
1431 struct timeval start, stop, diff;
1432 u64 runtime_ns_min, runtime_ns_sum;
1433 pid_t *pids, pid, wpid;
1434 double delta_runtime;
1436 double runtime_sec_max;
1437 double runtime_sec_min;
1445 pids = zalloc(g->p.nr_proc * sizeof(*pids));
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);
1451 if (g->p.serialize_startup) {
1453 tprintf(" # Startup synchronization: ..."); fflush(stdout);
1456 gettimeofday(&start, NULL);
1458 for (i = 0; i < g->p.nr_proc; i++) {
1460 dprintf(" # process %2d: PID %d\n", i, pid);
1464 /* Child process: */
1472 /* Wait for all the threads to start up: */
1473 while (g->nr_tasks_started != g->p.nr_tasks)
1474 usleep(USEC_PER_MSEC);
1476 BUG_ON(g->nr_tasks_started != g->p.nr_tasks);
1478 if (g->p.serialize_startup) {
1481 pthread_mutex_lock(&g->startup_done_mutex);
1483 /* This will start all threads: */
1484 pthread_mutex_unlock(&g->start_work_mutex);
1486 /* This mutex is locked - the last started thread will wake us: */
1487 pthread_mutex_lock(&g->startup_done_mutex);
1489 gettimeofday(&stop, NULL);
1491 timersub(&stop, &start, &diff);
1493 startup_sec = diff.tv_sec * NSEC_PER_SEC;
1494 startup_sec += diff.tv_usec * NSEC_PER_USEC;
1495 startup_sec /= NSEC_PER_SEC;
1497 tprintf(" threads initialized in %.6f seconds.\n", startup_sec);
1501 pthread_mutex_unlock(&g->startup_done_mutex);
1503 gettimeofday(&start, NULL);
1506 /* Parent process: */
1509 for (i = 0; i < g->p.nr_proc; i++) {
1510 wpid = waitpid(pids[i], &wait_stat, 0);
1512 BUG_ON(!WIFEXITED(wait_stat));
1517 runtime_ns_min = -1LL;
1519 for (t = 0; t < g->p.nr_tasks; t++) {
1520 u64 thread_runtime_ns = g->threads[t].runtime_ns;
1522 runtime_ns_sum += thread_runtime_ns;
1523 runtime_ns_min = min(thread_runtime_ns, runtime_ns_min);
1526 gettimeofday(&stop, NULL);
1527 timersub(&stop, &start, &diff);
1529 BUG_ON(bench_format != BENCH_FORMAT_DEFAULT);
1531 tprintf("\n ###\n");
1534 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;
1538 runtime_sec_min = runtime_ns_min / NSEC_PER_SEC;
1540 bytes = g->bytes_done;
1541 runtime_avg = (double)runtime_ns_sum / g->p.nr_tasks / NSEC_PER_SEC;
1543 if (g->p.measure_convergence) {
1544 print_res(name, runtime_sec_max,
1545 "secs,", "NUMA-convergence-latency", "secs latency to NUMA-converge");
1548 print_res(name, runtime_sec_max,
1549 "secs,", "runtime-max/thread", "secs slowest (max) thread-runtime");
1551 print_res(name, runtime_sec_min,
1552 "secs,", "runtime-min/thread", "secs fastest (min) thread-runtime");
1554 print_res(name, runtime_avg,
1555 "secs,", "runtime-avg/thread", "secs average thread-runtime");
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");
1561 print_res(name, bytes / g->p.nr_tasks / 1e9,
1562 "GB,", "data/thread", "GB data processed, per thread");
1564 print_res(name, bytes / 1e9,
1565 "GB,", "data-total", "GB data processed, total");
1567 print_res(name, runtime_sec_max * NSEC_PER_SEC / (bytes / g->p.nr_tasks),
1568 "nsecs,", "runtime/byte/thread","nsecs/byte/thread runtime");
1570 print_res(name, bytes / g->p.nr_tasks / 1e9 / runtime_sec_max,
1571 "GB/sec,", "thread-speed", "GB/sec/thread speed");
1573 print_res(name, bytes / runtime_sec_max / 1e9,
1574 "GB/sec,", "total-speed", "GB/sec total speed");
1576 if (g->p.show_details >= 2) {
1577 char tname[14 + 2 * 10 + 1];
1578 struct thread_data *td;
1579 for (p = 0; p < g->p.nr_proc; p++) {
1580 for (t = 0; t < g->p.nr_threads; t++) {
1581 memset(tname, 0, sizeof(tname));
1582 td = g->threads + p*g->p.nr_threads + t;
1583 snprintf(tname, sizeof(tname), "process%d:thread%d", p, t);
1584 print_res(tname, td->speed_gbs,
1585 "GB/sec", "thread-speed", "GB/sec/thread speed");
1586 print_res(tname, td->system_time_ns / NSEC_PER_SEC,
1587 "secs", "thread-system-time", "system CPU time/thread");
1588 print_res(tname, td->user_time_ns / NSEC_PER_SEC,
1589 "secs", "thread-user-time", "user CPU time/thread");
1603 static int command_size(const char **argv)
1612 BUG_ON(size >= MAX_ARGS);
1617 static void init_params(struct params *p, const char *name, int argc, const char **argv)
1621 printf("\n # Running %s \"perf bench numa", name);
1623 for (i = 0; i < argc; i++)
1624 printf(" %s", argv[i]);
1628 memset(p, 0, sizeof(*p));
1630 /* Initialize nonzero defaults: */
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;
1638 p->init_random = true;
1639 p->mb_global_str = "1";
1643 p->run_all = argc == 1;
1646 static int run_bench_numa(const char *name, const char **argv)
1648 int argc = command_size(argv);
1650 init_params(&p0, name, argc, argv);
1651 argc = parse_options(argc, argv, options, bench_numa_usage, 0);
1655 if (__bench_numa(name))
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"
1667 #define OPT_CONV "-s", "100", "-zZ0qcm", "--thp", " 1"
1668 #define OPT_CONV_NOTHP OPT_CONV, "--thp", "-1"
1670 #define OPT_BW "-s", "20", "-zZ0q", "--thp", " 1"
1671 #define OPT_BW_NOTHP OPT_BW, "--thp", "-1"
1674 * The built-in test-suite executed by "perf bench numa -a".
1676 * (A minimum of 4 nodes and 16 GB of RAM is recommended.)
1678 static 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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
1751 static int bench_all(void)
1753 int nr = ARRAY_SIZE(tests);
1757 ret = system("echo ' #'; echo ' # Running test on: '$(uname -a); echo ' #'");
1760 for (i = 0; i < nr; i++) {
1761 run_bench_numa(tests[i][0], tests[i] + 1);
1769 int bench_numa(int argc, const char **argv, const char *prefix __maybe_unused)
1771 init_params(&p0, "main,", argc, argv);
1772 argc = parse_options(argc, argv, options, bench_numa_usage, 0);
1779 if (__bench_numa(NULL))
1785 usage_with_options(numa_usage, options);