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