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