numa.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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. #include <inttypes.h>
  8. /* For the CLR_() macros */
  9. #include <pthread.h>
  10. #include "../perf.h"
  11. #include "../builtin.h"
  12. #include "../util/util.h"
  13. #include <subcmd/parse-options.h>
  14. #include "../util/cloexec.h"
  15. #include "bench.h"
  16. #include <errno.h>
  17. #include <sched.h>
  18. #include <stdio.h>
  19. #include <assert.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/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/time64.h>
  33. #include <numa.h>
  34. #include <numaif.h>
  35. #ifndef RUSAGE_THREAD
  36. # define RUSAGE_THREAD 1
  37. #endif
  38. /*
  39. * Regular printout to the terminal, supressed if -q is specified:
  40. */
  41. #define tprintf(x...) do { if (g && g->p.show_details >= 0) printf(x); } while (0)
  42. /*
  43. * Debug printf:
  44. */
  45. #undef dprintf
  46. #define dprintf(x...) do { if (g && g->p.show_details >= 1) printf(x); } while (0)
  47. struct thread_data {
  48. int curr_cpu;
  49. cpu_set_t bind_cpumask;
  50. int bind_node;
  51. u8 *process_data;
  52. int process_nr;
  53. int thread_nr;
  54. int task_nr;
  55. unsigned int loops_done;
  56. u64 val;
  57. u64 runtime_ns;
  58. u64 system_time_ns;
  59. u64 user_time_ns;
  60. double speed_gbs;
  61. pthread_mutex_t *process_lock;
  62. };
  63. /* Parameters set by options: */
  64. struct params {
  65. /* Startup synchronization: */
  66. bool serialize_startup;
  67. /* Task hierarchy: */
  68. int nr_proc;
  69. int nr_threads;
  70. /* Working set sizes: */
  71. const char *mb_global_str;
  72. const char *mb_proc_str;
  73. const char *mb_proc_locked_str;
  74. const char *mb_thread_str;
  75. double mb_global;
  76. double mb_proc;
  77. double mb_proc_locked;
  78. double mb_thread;
  79. /* Access patterns to the working set: */
  80. bool data_reads;
  81. bool data_writes;
  82. bool data_backwards;
  83. bool data_zero_memset;
  84. bool data_rand_walk;
  85. u32 nr_loops;
  86. u32 nr_secs;
  87. u32 sleep_usecs;
  88. /* Working set initialization: */
  89. bool init_zero;
  90. bool init_random;
  91. bool init_cpu0;
  92. /* Misc options: */
  93. int show_details;
  94. int run_all;
  95. int thp;
  96. long bytes_global;
  97. long bytes_process;
  98. long bytes_process_locked;
  99. long bytes_thread;
  100. int nr_tasks;
  101. bool show_quiet;
  102. bool show_convergence;
  103. bool measure_convergence;
  104. int perturb_secs;
  105. int nr_cpus;
  106. int nr_nodes;
  107. /* Affinity options -C and -N: */
  108. char *cpu_list_str;
  109. char *node_list_str;
  110. };
  111. /* Global, read-writable area, accessible to all processes and threads: */
  112. struct global_info {
  113. u8 *data;
  114. pthread_mutex_t startup_mutex;
  115. int nr_tasks_started;
  116. pthread_mutex_t startup_done_mutex;
  117. pthread_mutex_t start_work_mutex;
  118. int nr_tasks_working;
  119. pthread_mutex_t stop_work_mutex;
  120. u64 bytes_done;
  121. struct thread_data *threads;
  122. /* Convergence latency measurement: */
  123. bool all_converged;
  124. bool stop_work;
  125. int print_once;
  126. struct params p;
  127. };
  128. static struct global_info *g = NULL;
  129. static int parse_cpus_opt(const struct option *opt, const char *arg, int unset);
  130. static int parse_nodes_opt(const struct option *opt, const char *arg, int unset);
  131. struct params p0;
  132. static const struct option options[] = {
  133. OPT_INTEGER('p', "nr_proc" , &p0.nr_proc, "number of processes"),
  134. OPT_INTEGER('t', "nr_threads" , &p0.nr_threads, "number of threads per process"),
  135. OPT_STRING('G', "mb_global" , &p0.mb_global_str, "MB", "global memory (MBs)"),
  136. OPT_STRING('P', "mb_proc" , &p0.mb_proc_str, "MB", "process memory (MBs)"),
  137. OPT_STRING('L', "mb_proc_locked", &p0.mb_proc_locked_str,"MB", "process serialized/locked memory access (MBs), <= process_memory"),
  138. OPT_STRING('T', "mb_thread" , &p0.mb_thread_str, "MB", "thread memory (MBs)"),
  139. OPT_UINTEGER('l', "nr_loops" , &p0.nr_loops, "max number of loops to run (default: unlimited)"),
  140. OPT_UINTEGER('s', "nr_secs" , &p0.nr_secs, "max number of seconds to run (default: 5 secs)"),
  141. OPT_UINTEGER('u', "usleep" , &p0.sleep_usecs, "usecs to sleep per loop iteration"),
  142. OPT_BOOLEAN('R', "data_reads" , &p0.data_reads, "access the data via reads (can be mixed with -W)"),
  143. OPT_BOOLEAN('W', "data_writes" , &p0.data_writes, "access the data via writes (can be mixed with -R)"),
  144. OPT_BOOLEAN('B', "data_backwards", &p0.data_backwards, "access the data backwards as well"),
  145. OPT_BOOLEAN('Z', "data_zero_memset", &p0.data_zero_memset,"access the data via glibc bzero only"),
  146. OPT_BOOLEAN('r', "data_rand_walk", &p0.data_rand_walk, "access the data with random (32bit LFSR) walk"),
  147. OPT_BOOLEAN('z', "init_zero" , &p0.init_zero, "bzero the initial allocations"),
  148. OPT_BOOLEAN('I', "init_random" , &p0.init_random, "randomize the contents of the initial allocations"),
  149. OPT_BOOLEAN('0', "init_cpu0" , &p0.init_cpu0, "do the initial allocations on CPU#0"),
  150. OPT_INTEGER('x', "perturb_secs", &p0.perturb_secs, "perturb thread 0/0 every X secs, to test convergence stability"),
  151. OPT_INCR ('d', "show_details" , &p0.show_details, "Show details"),
  152. OPT_INCR ('a', "all" , &p0.run_all, "Run all tests in the suite"),
  153. OPT_INTEGER('H', "thp" , &p0.thp, "MADV_NOHUGEPAGE < 0 < MADV_HUGEPAGE"),
  154. OPT_BOOLEAN('c', "show_convergence", &p0.show_convergence, "show convergence details, "
  155. "convergence is reached when each process (all its threads) is running on a single NUMA node."),
  156. OPT_BOOLEAN('m', "measure_convergence", &p0.measure_convergence, "measure convergence latency"),
  157. OPT_BOOLEAN('q', "quiet" , &p0.show_quiet, "quiet mode"),
  158. OPT_BOOLEAN('S', "serialize-startup", &p0.serialize_startup,"serialize thread startup"),
  159. /* Special option string parsing callbacks: */
  160. OPT_CALLBACK('C', "cpus", NULL, "cpu[,cpu2,...cpuN]",
  161. "bind the first N tasks to these specific cpus (the rest is unbound)",
  162. parse_cpus_opt),
  163. OPT_CALLBACK('M', "memnodes", NULL, "node[,node2,...nodeN]",
  164. "bind the first N tasks to these specific memory nodes (the rest is unbound)",
  165. parse_nodes_opt),
  166. OPT_END()
  167. };
  168. static const char * const bench_numa_usage[] = {
  169. "perf bench numa <options>",
  170. NULL
  171. };
  172. static const char * const numa_usage[] = {
  173. "perf bench numa mem [<options>]",
  174. NULL
  175. };
  176. /*
  177. * To get number of numa nodes present.
  178. */
  179. static int nr_numa_nodes(void)
  180. {
  181. int i, nr_nodes = 0;
  182. for (i = 0; i < g->p.nr_nodes; i++) {
  183. if (numa_bitmask_isbitset(numa_nodes_ptr, i))
  184. nr_nodes++;
  185. }
  186. return nr_nodes;
  187. }
  188. /*
  189. * To check if given numa node is present.
  190. */
  191. static int is_node_present(int node)
  192. {
  193. return numa_bitmask_isbitset(numa_nodes_ptr, node);
  194. }
  195. /*
  196. * To check given numa node has cpus.
  197. */
  198. static bool node_has_cpus(int node)
  199. {
  200. struct bitmask *cpu = numa_allocate_cpumask();
  201. unsigned int i;
  202. if (cpu && !numa_node_to_cpus(node, cpu)) {
  203. for (i = 0; i < cpu->size; i++) {
  204. if (numa_bitmask_isbitset(cpu, i))
  205. return true;
  206. }
  207. }
  208. return false; /* lets fall back to nocpus safely */
  209. }
  210. static cpu_set_t bind_to_cpu(int target_cpu)
  211. {
  212. cpu_set_t orig_mask, mask;
  213. int ret;
  214. ret = sched_getaffinity(0, sizeof(orig_mask), &orig_mask);
  215. BUG_ON(ret);
  216. CPU_ZERO(&mask);
  217. if (target_cpu == -1) {
  218. int cpu;
  219. for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
  220. CPU_SET(cpu, &mask);
  221. } else {
  222. BUG_ON(target_cpu < 0 || target_cpu >= g->p.nr_cpus);
  223. CPU_SET(target_cpu, &mask);
  224. }
  225. ret = sched_setaffinity(0, sizeof(mask), &mask);
  226. BUG_ON(ret);
  227. return orig_mask;
  228. }
  229. static cpu_set_t bind_to_node(int target_node)
  230. {
  231. int cpus_per_node = g->p.nr_cpus / nr_numa_nodes();
  232. cpu_set_t orig_mask, mask;
  233. int cpu;
  234. int ret;
  235. BUG_ON(cpus_per_node * nr_numa_nodes() != g->p.nr_cpus);
  236. BUG_ON(!cpus_per_node);
  237. ret = sched_getaffinity(0, sizeof(orig_mask), &orig_mask);
  238. BUG_ON(ret);
  239. CPU_ZERO(&mask);
  240. if (target_node == -1) {
  241. for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
  242. CPU_SET(cpu, &mask);
  243. } else {
  244. int cpu_start = (target_node + 0) * cpus_per_node;
  245. int cpu_stop = (target_node + 1) * cpus_per_node;
  246. BUG_ON(cpu_stop > g->p.nr_cpus);
  247. for (cpu = cpu_start; cpu < cpu_stop; cpu++)
  248. CPU_SET(cpu, &mask);
  249. }
  250. ret = sched_setaffinity(0, sizeof(mask), &mask);
  251. BUG_ON(ret);
  252. return orig_mask;
  253. }
  254. static void bind_to_cpumask(cpu_set_t mask)
  255. {
  256. int ret;
  257. ret = sched_setaffinity(0, sizeof(mask), &mask);
  258. BUG_ON(ret);
  259. }
  260. static void mempol_restore(void)
  261. {
  262. int ret;
  263. ret = set_mempolicy(MPOL_DEFAULT, NULL, g->p.nr_nodes-1);
  264. BUG_ON(ret);
  265. }
  266. static void bind_to_memnode(int node)
  267. {
  268. unsigned long nodemask;
  269. int ret;
  270. if (node == -1)
  271. return;
  272. BUG_ON(g->p.nr_nodes > (int)sizeof(nodemask)*8);
  273. nodemask = 1L << node;
  274. ret = set_mempolicy(MPOL_BIND, &nodemask, sizeof(nodemask)*8);
  275. dprintf("binding to node %d, mask: %016lx => %d\n", node, nodemask, ret);
  276. BUG_ON(ret);
  277. }
  278. #define HPSIZE (2*1024*1024)
  279. #define set_taskname(fmt...) \
  280. do { \
  281. char name[20]; \
  282. \
  283. snprintf(name, 20, fmt); \
  284. prctl(PR_SET_NAME, name); \
  285. } while (0)
  286. static u8 *alloc_data(ssize_t bytes0, int map_flags,
  287. int init_zero, int init_cpu0, int thp, int init_random)
  288. {
  289. cpu_set_t orig_mask;
  290. ssize_t bytes;
  291. u8 *buf;
  292. int ret;
  293. if (!bytes0)
  294. return NULL;
  295. /* Allocate and initialize all memory on CPU#0: */
  296. if (init_cpu0) {
  297. int node = numa_node_of_cpu(0);
  298. orig_mask = bind_to_node(node);
  299. bind_to_memnode(node);
  300. }
  301. bytes = bytes0 + HPSIZE;
  302. buf = (void *)mmap(0, bytes, PROT_READ|PROT_WRITE, MAP_ANON|map_flags, -1, 0);
  303. BUG_ON(buf == (void *)-1);
  304. if (map_flags == MAP_PRIVATE) {
  305. if (thp > 0) {
  306. ret = madvise(buf, bytes, MADV_HUGEPAGE);
  307. if (ret && !g->print_once) {
  308. g->print_once = 1;
  309. printf("WARNING: Could not enable THP - do: 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled'\n");
  310. }
  311. }
  312. if (thp < 0) {
  313. ret = madvise(buf, bytes, MADV_NOHUGEPAGE);
  314. if (ret && !g->print_once) {
  315. g->print_once = 1;
  316. printf("WARNING: Could not disable THP: run a CONFIG_TRANSPARENT_HUGEPAGE kernel?\n");
  317. }
  318. }
  319. }
  320. if (init_zero) {
  321. bzero(buf, bytes);
  322. } else {
  323. /* Initialize random contents, different in each word: */
  324. if (init_random) {
  325. u64 *wbuf = (void *)buf;
  326. long off = rand();
  327. long i;
  328. for (i = 0; i < bytes/8; i++)
  329. wbuf[i] = i + off;
  330. }
  331. }
  332. /* Align to 2MB boundary: */
  333. buf = (void *)(((unsigned long)buf + HPSIZE-1) & ~(HPSIZE-1));
  334. /* Restore affinity: */
  335. if (init_cpu0) {
  336. bind_to_cpumask(orig_mask);
  337. mempol_restore();
  338. }
  339. return buf;
  340. }
  341. static void free_data(void *data, ssize_t bytes)
  342. {
  343. int ret;
  344. if (!data)
  345. return;
  346. ret = munmap(data, bytes);
  347. BUG_ON(ret);
  348. }
  349. /*
  350. * Create a shared memory buffer that can be shared between processes, zeroed:
  351. */
  352. static void * zalloc_shared_data(ssize_t bytes)
  353. {
  354. return alloc_data(bytes, MAP_SHARED, 1, g->p.init_cpu0, g->p.thp, g->p.init_random);
  355. }
  356. /*
  357. * Create a shared memory buffer that can be shared between processes:
  358. */
  359. static void * setup_shared_data(ssize_t bytes)
  360. {
  361. return alloc_data(bytes, MAP_SHARED, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
  362. }
  363. /*
  364. * Allocate process-local memory - this will either be shared between
  365. * threads of this process, or only be accessed by this thread:
  366. */
  367. static void * setup_private_data(ssize_t bytes)
  368. {
  369. return alloc_data(bytes, MAP_PRIVATE, 0, g->p.init_cpu0, g->p.thp, g->p.init_random);
  370. }
  371. /*
  372. * Return a process-shared (global) mutex:
  373. */
  374. static void init_global_mutex(pthread_mutex_t *mutex)
  375. {
  376. pthread_mutexattr_t attr;
  377. pthread_mutexattr_init(&attr);
  378. pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
  379. pthread_mutex_init(mutex, &attr);
  380. }
  381. static int parse_cpu_list(const char *arg)
  382. {
  383. p0.cpu_list_str = strdup(arg);
  384. dprintf("got CPU list: {%s}\n", p0.cpu_list_str);
  385. return 0;
  386. }
  387. static int parse_setup_cpu_list(void)
  388. {
  389. struct thread_data *td;
  390. char *str0, *str;
  391. int t;
  392. if (!g->p.cpu_list_str)
  393. return 0;
  394. dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
  395. str0 = str = strdup(g->p.cpu_list_str);
  396. t = 0;
  397. BUG_ON(!str);
  398. tprintf("# binding tasks to CPUs:\n");
  399. tprintf("# ");
  400. while (true) {
  401. int bind_cpu, bind_cpu_0, bind_cpu_1;
  402. char *tok, *tok_end, *tok_step, *tok_len, *tok_mul;
  403. int bind_len;
  404. int step;
  405. int mul;
  406. tok = strsep(&str, ",");
  407. if (!tok)
  408. break;
  409. tok_end = strstr(tok, "-");
  410. dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
  411. if (!tok_end) {
  412. /* Single CPU specified: */
  413. bind_cpu_0 = bind_cpu_1 = atol(tok);
  414. } else {
  415. /* CPU range specified (for example: "5-11"): */
  416. bind_cpu_0 = atol(tok);
  417. bind_cpu_1 = atol(tok_end + 1);
  418. }
  419. step = 1;
  420. tok_step = strstr(tok, "#");
  421. if (tok_step) {
  422. step = atol(tok_step + 1);
  423. BUG_ON(step <= 0 || step >= g->p.nr_cpus);
  424. }
  425. /*
  426. * Mask length.
  427. * Eg: "--cpus 8_4-16#4" means: '--cpus 8_4,12_4,16_4',
  428. * where the _4 means the next 4 CPUs are allowed.
  429. */
  430. bind_len = 1;
  431. tok_len = strstr(tok, "_");
  432. if (tok_len) {
  433. bind_len = atol(tok_len + 1);
  434. BUG_ON(bind_len <= 0 || bind_len > g->p.nr_cpus);
  435. }
  436. /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
  437. mul = 1;
  438. tok_mul = strstr(tok, "x");
  439. if (tok_mul) {
  440. mul = atol(tok_mul + 1);
  441. BUG_ON(mul <= 0);
  442. }
  443. dprintf("CPUs: %d_%d-%d#%dx%d\n", bind_cpu_0, bind_len, bind_cpu_1, step, mul);
  444. if (bind_cpu_0 >= g->p.nr_cpus || bind_cpu_1 >= g->p.nr_cpus) {
  445. printf("\nTest not applicable, system has only %d CPUs.\n", g->p.nr_cpus);
  446. return -1;
  447. }
  448. BUG_ON(bind_cpu_0 < 0 || bind_cpu_1 < 0);
  449. BUG_ON(bind_cpu_0 > bind_cpu_1);
  450. for (bind_cpu = bind_cpu_0; bind_cpu <= bind_cpu_1; bind_cpu += step) {
  451. int i;
  452. for (i = 0; i < mul; i++) {
  453. int cpu;
  454. if (t >= g->p.nr_tasks) {
  455. printf("\n# NOTE: ignoring bind CPUs starting at CPU#%d\n #", bind_cpu);
  456. goto out;
  457. }
  458. td = g->threads + t;
  459. if (t)
  460. tprintf(",");
  461. if (bind_len > 1) {
  462. tprintf("%2d/%d", bind_cpu, bind_len);
  463. } else {
  464. tprintf("%2d", bind_cpu);
  465. }
  466. CPU_ZERO(&td->bind_cpumask);
  467. for (cpu = bind_cpu; cpu < bind_cpu+bind_len; cpu++) {
  468. BUG_ON(cpu < 0 || cpu >= g->p.nr_cpus);
  469. CPU_SET(cpu, &td->bind_cpumask);
  470. }
  471. t++;
  472. }
  473. }
  474. }
  475. out:
  476. tprintf("\n");
  477. if (t < g->p.nr_tasks)
  478. printf("# NOTE: %d tasks bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
  479. free(str0);
  480. return 0;
  481. }
  482. static int parse_cpus_opt(const struct option *opt __maybe_unused,
  483. const char *arg, int unset __maybe_unused)
  484. {
  485. if (!arg)
  486. return -1;
  487. return parse_cpu_list(arg);
  488. }
  489. static int parse_node_list(const char *arg)
  490. {
  491. p0.node_list_str = strdup(arg);
  492. dprintf("got NODE list: {%s}\n", p0.node_list_str);
  493. return 0;
  494. }
  495. static int parse_setup_node_list(void)
  496. {
  497. struct thread_data *td;
  498. char *str0, *str;
  499. int t;
  500. if (!g->p.node_list_str)
  501. return 0;
  502. dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
  503. str0 = str = strdup(g->p.node_list_str);
  504. t = 0;
  505. BUG_ON(!str);
  506. tprintf("# binding tasks to NODEs:\n");
  507. tprintf("# ");
  508. while (true) {
  509. int bind_node, bind_node_0, bind_node_1;
  510. char *tok, *tok_end, *tok_step, *tok_mul;
  511. int step;
  512. int mul;
  513. tok = strsep(&str, ",");
  514. if (!tok)
  515. break;
  516. tok_end = strstr(tok, "-");
  517. dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
  518. if (!tok_end) {
  519. /* Single NODE specified: */
  520. bind_node_0 = bind_node_1 = atol(tok);
  521. } else {
  522. /* NODE range specified (for example: "5-11"): */
  523. bind_node_0 = atol(tok);
  524. bind_node_1 = atol(tok_end + 1);
  525. }
  526. step = 1;
  527. tok_step = strstr(tok, "#");
  528. if (tok_step) {
  529. step = atol(tok_step + 1);
  530. BUG_ON(step <= 0 || step >= g->p.nr_nodes);
  531. }
  532. /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
  533. mul = 1;
  534. tok_mul = strstr(tok, "x");
  535. if (tok_mul) {
  536. mul = atol(tok_mul + 1);
  537. BUG_ON(mul <= 0);
  538. }
  539. dprintf("NODEs: %d-%d #%d\n", bind_node_0, bind_node_1, step);
  540. if (bind_node_0 >= g->p.nr_nodes || bind_node_1 >= g->p.nr_nodes) {
  541. printf("\nTest not applicable, system has only %d nodes.\n", g->p.nr_nodes);
  542. return -1;
  543. }
  544. BUG_ON(bind_node_0 < 0 || bind_node_1 < 0);
  545. BUG_ON(bind_node_0 > bind_node_1);
  546. for (bind_node = bind_node_0; bind_node <= bind_node_1; bind_node += step) {
  547. int i;
  548. for (i = 0; i < mul; i++) {
  549. if (t >= g->p.nr_tasks || !node_has_cpus(bind_node)) {
  550. printf("\n# NOTE: ignoring bind NODEs starting at NODE#%d\n", bind_node);
  551. goto out;
  552. }
  553. td = g->threads + t;
  554. if (!t)
  555. tprintf(" %2d", bind_node);
  556. else
  557. tprintf(",%2d", bind_node);
  558. td->bind_node = bind_node;
  559. t++;
  560. }
  561. }
  562. }
  563. out:
  564. tprintf("\n");
  565. if (t < g->p.nr_tasks)
  566. printf("# NOTE: %d tasks mem-bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
  567. free(str0);
  568. return 0;
  569. }
  570. static int parse_nodes_opt(const struct option *opt __maybe_unused,
  571. const char *arg, int unset __maybe_unused)
  572. {
  573. if (!arg)
  574. return -1;
  575. return parse_node_list(arg);
  576. return 0;
  577. }
  578. #define BIT(x) (1ul << x)
  579. static inline uint32_t lfsr_32(uint32_t lfsr)
  580. {
  581. const uint32_t taps = BIT(1) | BIT(5) | BIT(6) | BIT(31);
  582. return (lfsr>>1) ^ ((0x0u - (lfsr & 0x1u)) & taps);
  583. }
  584. /*
  585. * Make sure there's real data dependency to RAM (when read
  586. * accesses are enabled), so the compiler, the CPU and the
  587. * kernel (KSM, zero page, etc.) cannot optimize away RAM
  588. * accesses:
  589. */
  590. static inline u64 access_data(u64 *data, u64 val)
  591. {
  592. if (g->p.data_reads)
  593. val += *data;
  594. if (g->p.data_writes)
  595. *data = val + 1;
  596. return val;
  597. }
  598. /*
  599. * The worker process does two types of work, a forwards going
  600. * loop and a backwards going loop.
  601. *
  602. * We do this so that on multiprocessor systems we do not create
  603. * a 'train' of processing, with highly synchronized processes,
  604. * skewing the whole benchmark.
  605. */
  606. static u64 do_work(u8 *__data, long bytes, int nr, int nr_max, int loop, u64 val)
  607. {
  608. long words = bytes/sizeof(u64);
  609. u64 *data = (void *)__data;
  610. long chunk_0, chunk_1;
  611. u64 *d0, *d, *d1;
  612. long off;
  613. long i;
  614. BUG_ON(!data && words);
  615. BUG_ON(data && !words);
  616. if (!data)
  617. return val;
  618. /* Very simple memset() work variant: */
  619. if (g->p.data_zero_memset && !g->p.data_rand_walk) {
  620. bzero(data, bytes);
  621. return val;
  622. }
  623. /* Spread out by PID/TID nr and by loop nr: */
  624. chunk_0 = words/nr_max;
  625. chunk_1 = words/g->p.nr_loops;
  626. off = nr*chunk_0 + loop*chunk_1;
  627. while (off >= words)
  628. off -= words;
  629. if (g->p.data_rand_walk) {
  630. u32 lfsr = nr + loop + val;
  631. int j;
  632. for (i = 0; i < words/1024; i++) {
  633. long start, end;
  634. lfsr = lfsr_32(lfsr);
  635. start = lfsr % words;
  636. end = min(start + 1024, words-1);
  637. if (g->p.data_zero_memset) {
  638. bzero(data + start, (end-start) * sizeof(u64));
  639. } else {
  640. for (j = start; j < end; j++)
  641. val = access_data(data + j, val);
  642. }
  643. }
  644. } else if (!g->p.data_backwards || (nr + loop) & 1) {
  645. d0 = data + off;
  646. d = data + off + 1;
  647. d1 = data + words;
  648. /* Process data forwards: */
  649. for (;;) {
  650. if (unlikely(d >= d1))
  651. d = data;
  652. if (unlikely(d == d0))
  653. break;
  654. val = access_data(d, val);
  655. d++;
  656. }
  657. } else {
  658. /* Process data backwards: */
  659. d0 = data + off;
  660. d = data + off - 1;
  661. d1 = data + words;
  662. /* Process data forwards: */
  663. for (;;) {
  664. if (unlikely(d < data))
  665. d = data + words-1;
  666. if (unlikely(d == d0))
  667. break;
  668. val = access_data(d, val);
  669. d--;
  670. }
  671. }
  672. return val;
  673. }
  674. static void update_curr_cpu(int task_nr, unsigned long bytes_worked)
  675. {
  676. unsigned int cpu;
  677. cpu = sched_getcpu();
  678. g->threads[task_nr].curr_cpu = cpu;
  679. prctl(0, bytes_worked);
  680. }
  681. #define MAX_NR_NODES 64
  682. /*
  683. * Count the number of nodes a process's threads
  684. * are spread out on.
  685. *
  686. * A count of 1 means that the process is compressed
  687. * to a single node. A count of g->p.nr_nodes means it's
  688. * spread out on the whole system.
  689. */
  690. static int count_process_nodes(int process_nr)
  691. {
  692. char node_present[MAX_NR_NODES] = { 0, };
  693. int nodes;
  694. int n, t;
  695. for (t = 0; t < g->p.nr_threads; t++) {
  696. struct thread_data *td;
  697. int task_nr;
  698. int node;
  699. task_nr = process_nr*g->p.nr_threads + t;
  700. td = g->threads + task_nr;
  701. node = numa_node_of_cpu(td->curr_cpu);
  702. if (node < 0) /* curr_cpu was likely still -1 */
  703. return 0;
  704. node_present[node] = 1;
  705. }
  706. nodes = 0;
  707. for (n = 0; n < MAX_NR_NODES; n++)
  708. nodes += node_present[n];
  709. return nodes;
  710. }
  711. /*
  712. * Count the number of distinct process-threads a node contains.
  713. *
  714. * A count of 1 means that the node contains only a single
  715. * process. If all nodes on the system contain at most one
  716. * process then we are well-converged.
  717. */
  718. static int count_node_processes(int node)
  719. {
  720. int processes = 0;
  721. int t, p;
  722. for (p = 0; p < g->p.nr_proc; p++) {
  723. for (t = 0; t < g->p.nr_threads; t++) {
  724. struct thread_data *td;
  725. int task_nr;
  726. int n;
  727. task_nr = p*g->p.nr_threads + t;
  728. td = g->threads + task_nr;
  729. n = numa_node_of_cpu(td->curr_cpu);
  730. if (n == node) {
  731. processes++;
  732. break;
  733. }
  734. }
  735. }
  736. return processes;
  737. }
  738. static void calc_convergence_compression(int *strong)
  739. {
  740. unsigned int nodes_min, nodes_max;
  741. int p;
  742. nodes_min = -1;
  743. nodes_max = 0;
  744. for (p = 0; p < g->p.nr_proc; p++) {
  745. unsigned int nodes = count_process_nodes(p);
  746. if (!nodes) {
  747. *strong = 0;
  748. return;
  749. }
  750. nodes_min = min(nodes, nodes_min);
  751. nodes_max = max(nodes, nodes_max);
  752. }
  753. /* Strong convergence: all threads compress on a single node: */
  754. if (nodes_min == 1 && nodes_max == 1) {
  755. *strong = 1;
  756. } else {
  757. *strong = 0;
  758. tprintf(" {%d-%d}", nodes_min, nodes_max);
  759. }
  760. }
  761. static void calc_convergence(double runtime_ns_max, double *convergence)
  762. {
  763. unsigned int loops_done_min, loops_done_max;
  764. int process_groups;
  765. int nodes[MAX_NR_NODES];
  766. int distance;
  767. int nr_min;
  768. int nr_max;
  769. int strong;
  770. int sum;
  771. int nr;
  772. int node;
  773. int cpu;
  774. int t;
  775. if (!g->p.show_convergence && !g->p.measure_convergence)
  776. return;
  777. for (node = 0; node < g->p.nr_nodes; node++)
  778. nodes[node] = 0;
  779. loops_done_min = -1;
  780. loops_done_max = 0;
  781. for (t = 0; t < g->p.nr_tasks; t++) {
  782. struct thread_data *td = g->threads + t;
  783. unsigned int loops_done;
  784. cpu = td->curr_cpu;
  785. /* Not all threads have written it yet: */
  786. if (cpu < 0)
  787. continue;
  788. node = numa_node_of_cpu(cpu);
  789. nodes[node]++;
  790. loops_done = td->loops_done;
  791. loops_done_min = min(loops_done, loops_done_min);
  792. loops_done_max = max(loops_done, loops_done_max);
  793. }
  794. nr_max = 0;
  795. nr_min = g->p.nr_tasks;
  796. sum = 0;
  797. for (node = 0; node < g->p.nr_nodes; node++) {
  798. if (!is_node_present(node))
  799. continue;
  800. nr = nodes[node];
  801. nr_min = min(nr, nr_min);
  802. nr_max = max(nr, nr_max);
  803. sum += nr;
  804. }
  805. BUG_ON(nr_min > nr_max);
  806. BUG_ON(sum > g->p.nr_tasks);
  807. if (0 && (sum < g->p.nr_tasks))
  808. return;
  809. /*
  810. * Count the number of distinct process groups present
  811. * on nodes - when we are converged this will decrease
  812. * to g->p.nr_proc:
  813. */
  814. process_groups = 0;
  815. for (node = 0; node < g->p.nr_nodes; node++) {
  816. int processes;
  817. if (!is_node_present(node))
  818. continue;
  819. processes = count_node_processes(node);
  820. nr = nodes[node];
  821. tprintf(" %2d/%-2d", nr, processes);
  822. process_groups += processes;
  823. }
  824. distance = nr_max - nr_min;
  825. tprintf(" [%2d/%-2d]", distance, process_groups);
  826. tprintf(" l:%3d-%-3d (%3d)",
  827. loops_done_min, loops_done_max, loops_done_max-loops_done_min);
  828. if (loops_done_min && loops_done_max) {
  829. double skew = 1.0 - (double)loops_done_min/loops_done_max;
  830. tprintf(" [%4.1f%%]", skew * 100.0);
  831. }
  832. calc_convergence_compression(&strong);
  833. if (strong && process_groups == g->p.nr_proc) {
  834. if (!*convergence) {
  835. *convergence = runtime_ns_max;
  836. tprintf(" (%6.1fs converged)\n", *convergence / NSEC_PER_SEC);
  837. if (g->p.measure_convergence) {
  838. g->all_converged = true;
  839. g->stop_work = true;
  840. }
  841. }
  842. } else {
  843. if (*convergence) {
  844. tprintf(" (%6.1fs de-converged)", runtime_ns_max / NSEC_PER_SEC);
  845. *convergence = 0;
  846. }
  847. tprintf("\n");
  848. }
  849. }
  850. static void show_summary(double runtime_ns_max, int l, double *convergence)
  851. {
  852. tprintf("\r # %5.1f%% [%.1f mins]",
  853. (double)(l+1)/g->p.nr_loops*100.0, runtime_ns_max / NSEC_PER_SEC / 60.0);
  854. calc_convergence(runtime_ns_max, convergence);
  855. if (g->p.show_details >= 0)
  856. fflush(stdout);
  857. }
  858. static void *worker_thread(void *__tdata)
  859. {
  860. struct thread_data *td = __tdata;
  861. struct timeval start0, start, stop, diff;
  862. int process_nr = td->process_nr;
  863. int thread_nr = td->thread_nr;
  864. unsigned long last_perturbance;
  865. int task_nr = td->task_nr;
  866. int details = g->p.show_details;
  867. int first_task, last_task;
  868. double convergence = 0;
  869. u64 val = td->val;
  870. double runtime_ns_max;
  871. u8 *global_data;
  872. u8 *process_data;
  873. u8 *thread_data;
  874. u64 bytes_done, secs;
  875. long work_done;
  876. u32 l;
  877. struct rusage rusage;
  878. bind_to_cpumask(td->bind_cpumask);
  879. bind_to_memnode(td->bind_node);
  880. set_taskname("thread %d/%d", process_nr, thread_nr);
  881. global_data = g->data;
  882. process_data = td->process_data;
  883. thread_data = setup_private_data(g->p.bytes_thread);
  884. bytes_done = 0;
  885. last_task = 0;
  886. if (process_nr == g->p.nr_proc-1 && thread_nr == g->p.nr_threads-1)
  887. last_task = 1;
  888. first_task = 0;
  889. if (process_nr == 0 && thread_nr == 0)
  890. first_task = 1;
  891. if (details >= 2) {
  892. printf("# thread %2d / %2d global mem: %p, process mem: %p, thread mem: %p\n",
  893. process_nr, thread_nr, global_data, process_data, thread_data);
  894. }
  895. if (g->p.serialize_startup) {
  896. pthread_mutex_lock(&g->startup_mutex);
  897. g->nr_tasks_started++;
  898. pthread_mutex_unlock(&g->startup_mutex);
  899. /* Here we will wait for the main process to start us all at once: */
  900. pthread_mutex_lock(&g->start_work_mutex);
  901. g->nr_tasks_working++;
  902. /* Last one wake the main process: */
  903. if (g->nr_tasks_working == g->p.nr_tasks)
  904. pthread_mutex_unlock(&g->startup_done_mutex);
  905. pthread_mutex_unlock(&g->start_work_mutex);
  906. }
  907. gettimeofday(&start0, NULL);
  908. start = stop = start0;
  909. last_perturbance = start.tv_sec;
  910. for (l = 0; l < g->p.nr_loops; l++) {
  911. start = stop;
  912. if (g->stop_work)
  913. break;
  914. val += do_work(global_data, g->p.bytes_global, process_nr, g->p.nr_proc, l, val);
  915. val += do_work(process_data, g->p.bytes_process, thread_nr, g->p.nr_threads, l, val);
  916. val += do_work(thread_data, g->p.bytes_thread, 0, 1, l, val);
  917. if (g->p.sleep_usecs) {
  918. pthread_mutex_lock(td->process_lock);
  919. usleep(g->p.sleep_usecs);
  920. pthread_mutex_unlock(td->process_lock);
  921. }
  922. /*
  923. * Amount of work to be done under a process-global lock:
  924. */
  925. if (g->p.bytes_process_locked) {
  926. pthread_mutex_lock(td->process_lock);
  927. val += do_work(process_data, g->p.bytes_process_locked, thread_nr, g->p.nr_threads, l, val);
  928. pthread_mutex_unlock(td->process_lock);
  929. }
  930. work_done = g->p.bytes_global + g->p.bytes_process +
  931. g->p.bytes_process_locked + g->p.bytes_thread;
  932. update_curr_cpu(task_nr, work_done);
  933. bytes_done += work_done;
  934. if (details < 0 && !g->p.perturb_secs && !g->p.measure_convergence && !g->p.nr_secs)
  935. continue;
  936. td->loops_done = l;
  937. gettimeofday(&stop, NULL);
  938. /* Check whether our max runtime timed out: */
  939. if (g->p.nr_secs) {
  940. timersub(&stop, &start0, &diff);
  941. if ((u32)diff.tv_sec >= g->p.nr_secs) {
  942. g->stop_work = true;
  943. break;
  944. }
  945. }
  946. /* Update the summary at most once per second: */
  947. if (start.tv_sec == stop.tv_sec)
  948. continue;
  949. /*
  950. * Perturb the first task's equilibrium every g->p.perturb_secs seconds,
  951. * by migrating to CPU#0:
  952. */
  953. if (first_task && g->p.perturb_secs && (int)(stop.tv_sec - last_perturbance) >= g->p.perturb_secs) {
  954. cpu_set_t orig_mask;
  955. int target_cpu;
  956. int this_cpu;
  957. last_perturbance = stop.tv_sec;
  958. /*
  959. * Depending on where we are running, move into
  960. * the other half of the system, to create some
  961. * real disturbance:
  962. */
  963. this_cpu = g->threads[task_nr].curr_cpu;
  964. if (this_cpu < g->p.nr_cpus/2)
  965. target_cpu = g->p.nr_cpus-1;
  966. else
  967. target_cpu = 0;
  968. orig_mask = bind_to_cpu(target_cpu);
  969. /* Here we are running on the target CPU already */
  970. if (details >= 1)
  971. printf(" (injecting perturbalance, moved to CPU#%d)\n", target_cpu);
  972. bind_to_cpumask(orig_mask);
  973. }
  974. if (details >= 3) {
  975. timersub(&stop, &start, &diff);
  976. runtime_ns_max = diff.tv_sec * NSEC_PER_SEC;
  977. runtime_ns_max += diff.tv_usec * NSEC_PER_USEC;
  978. if (details >= 0) {
  979. printf(" #%2d / %2d: %14.2lf nsecs/op [val: %016"PRIx64"]\n",
  980. process_nr, thread_nr, runtime_ns_max / bytes_done, val);
  981. }
  982. fflush(stdout);
  983. }
  984. if (!last_task)
  985. continue;
  986. timersub(&stop, &start0, &diff);
  987. runtime_ns_max = diff.tv_sec * NSEC_PER_SEC;
  988. runtime_ns_max += diff.tv_usec * NSEC_PER_USEC;
  989. show_summary(runtime_ns_max, l, &convergence);
  990. }
  991. gettimeofday(&stop, NULL);
  992. timersub(&stop, &start0, &diff);
  993. td->runtime_ns = diff.tv_sec * NSEC_PER_SEC;
  994. td->runtime_ns += diff.tv_usec * NSEC_PER_USEC;
  995. secs = td->runtime_ns / NSEC_PER_SEC;
  996. td->speed_gbs = secs ? bytes_done / secs / 1e9 : 0;
  997. getrusage(RUSAGE_THREAD, &rusage);
  998. td->system_time_ns = rusage.ru_stime.tv_sec * NSEC_PER_SEC;
  999. td->system_time_ns += rusage.ru_stime.tv_usec * NSEC_PER_USEC;
  1000. td->user_time_ns = rusage.ru_utime.tv_sec * NSEC_PER_SEC;
  1001. td->user_time_ns += rusage.ru_utime.tv_usec * NSEC_PER_USEC;
  1002. free_data(thread_data, g->p.bytes_thread);
  1003. pthread_mutex_lock(&g->stop_work_mutex);
  1004. g->bytes_done += bytes_done;
  1005. pthread_mutex_unlock(&g->stop_work_mutex);
  1006. return NULL;
  1007. }
  1008. /*
  1009. * A worker process starts a couple of threads:
  1010. */
  1011. static void worker_process(int process_nr)
  1012. {
  1013. pthread_mutex_t process_lock;
  1014. struct thread_data *td;
  1015. pthread_t *pthreads;
  1016. u8 *process_data;
  1017. int task_nr;
  1018. int ret;
  1019. int t;
  1020. pthread_mutex_init(&process_lock, NULL);
  1021. set_taskname("process %d", process_nr);
  1022. /*
  1023. * Pick up the memory policy and the CPU binding of our first thread,
  1024. * so that we initialize memory accordingly:
  1025. */
  1026. task_nr = process_nr*g->p.nr_threads;
  1027. td = g->threads + task_nr;
  1028. bind_to_memnode(td->bind_node);
  1029. bind_to_cpumask(td->bind_cpumask);
  1030. pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
  1031. process_data = setup_private_data(g->p.bytes_process);
  1032. if (g->p.show_details >= 3) {
  1033. printf(" # process %2d global mem: %p, process mem: %p\n",
  1034. process_nr, g->data, process_data);
  1035. }
  1036. for (t = 0; t < g->p.nr_threads; t++) {
  1037. task_nr = process_nr*g->p.nr_threads + t;
  1038. td = g->threads + task_nr;
  1039. td->process_data = process_data;
  1040. td->process_nr = process_nr;
  1041. td->thread_nr = t;
  1042. td->task_nr = task_nr;
  1043. td->val = rand();
  1044. td->curr_cpu = -1;
  1045. td->process_lock = &process_lock;
  1046. ret = pthread_create(pthreads + t, NULL, worker_thread, td);
  1047. BUG_ON(ret);
  1048. }
  1049. for (t = 0; t < g->p.nr_threads; t++) {
  1050. ret = pthread_join(pthreads[t], NULL);
  1051. BUG_ON(ret);
  1052. }
  1053. free_data(process_data, g->p.bytes_process);
  1054. free(pthreads);
  1055. }
  1056. static void print_summary(void)
  1057. {
  1058. if (g->p.show_details < 0)
  1059. return;
  1060. printf("\n ###\n");
  1061. printf(" # %d %s will execute (on %d nodes, %d CPUs):\n",
  1062. g->p.nr_tasks, g->p.nr_tasks == 1 ? "task" : "tasks", nr_numa_nodes(), g->p.nr_cpus);
  1063. printf(" # %5dx %5ldMB global shared mem operations\n",
  1064. g->p.nr_loops, g->p.bytes_global/1024/1024);
  1065. printf(" # %5dx %5ldMB process shared mem operations\n",
  1066. g->p.nr_loops, g->p.bytes_process/1024/1024);
  1067. printf(" # %5dx %5ldMB thread local mem operations\n",
  1068. g->p.nr_loops, g->p.bytes_thread/1024/1024);
  1069. printf(" ###\n");
  1070. printf("\n ###\n"); fflush(stdout);
  1071. }
  1072. static void init_thread_data(void)
  1073. {
  1074. ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
  1075. int t;
  1076. g->threads = zalloc_shared_data(size);
  1077. for (t = 0; t < g->p.nr_tasks; t++) {
  1078. struct thread_data *td = g->threads + t;
  1079. int cpu;
  1080. /* Allow all nodes by default: */
  1081. td->bind_node = -1;
  1082. /* Allow all CPUs by default: */
  1083. CPU_ZERO(&td->bind_cpumask);
  1084. for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
  1085. CPU_SET(cpu, &td->bind_cpumask);
  1086. }
  1087. }
  1088. static void deinit_thread_data(void)
  1089. {
  1090. ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
  1091. free_data(g->threads, size);
  1092. }
  1093. static int init(void)
  1094. {
  1095. g = (void *)alloc_data(sizeof(*g), MAP_SHARED, 1, 0, 0 /* THP */, 0);
  1096. /* Copy over options: */
  1097. g->p = p0;
  1098. g->p.nr_cpus = numa_num_configured_cpus();
  1099. g->p.nr_nodes = numa_max_node() + 1;
  1100. /* char array in count_process_nodes(): */
  1101. BUG_ON(g->p.nr_nodes > MAX_NR_NODES || g->p.nr_nodes < 0);
  1102. if (g->p.show_quiet && !g->p.show_details)
  1103. g->p.show_details = -1;
  1104. /* Some memory should be specified: */
  1105. if (!g->p.mb_global_str && !g->p.mb_proc_str && !g->p.mb_thread_str)
  1106. return -1;
  1107. if (g->p.mb_global_str) {
  1108. g->p.mb_global = atof(g->p.mb_global_str);
  1109. BUG_ON(g->p.mb_global < 0);
  1110. }
  1111. if (g->p.mb_proc_str) {
  1112. g->p.mb_proc = atof(g->p.mb_proc_str);
  1113. BUG_ON(g->p.mb_proc < 0);
  1114. }
  1115. if (g->p.mb_proc_locked_str) {
  1116. g->p.mb_proc_locked = atof(g->p.mb_proc_locked_str);
  1117. BUG_ON(g->p.mb_proc_locked < 0);
  1118. BUG_ON(g->p.mb_proc_locked > g->p.mb_proc);
  1119. }
  1120. if (g->p.mb_thread_str) {
  1121. g->p.mb_thread = atof(g->p.mb_thread_str);
  1122. BUG_ON(g->p.mb_thread < 0);
  1123. }
  1124. BUG_ON(g->p.nr_threads <= 0);
  1125. BUG_ON(g->p.nr_proc <= 0);
  1126. g->p.nr_tasks = g->p.nr_proc*g->p.nr_threads;
  1127. g->p.bytes_global = g->p.mb_global *1024L*1024L;
  1128. g->p.bytes_process = g->p.mb_proc *1024L*1024L;
  1129. g->p.bytes_process_locked = g->p.mb_proc_locked *1024L*1024L;
  1130. g->p.bytes_thread = g->p.mb_thread *1024L*1024L;
  1131. g->data = setup_shared_data(g->p.bytes_global);
  1132. /* Startup serialization: */
  1133. init_global_mutex(&g->start_work_mutex);
  1134. init_global_mutex(&g->startup_mutex);
  1135. init_global_mutex(&g->startup_done_mutex);
  1136. init_global_mutex(&g->stop_work_mutex);
  1137. init_thread_data();
  1138. tprintf("#\n");
  1139. if (parse_setup_cpu_list() || parse_setup_node_list())
  1140. return -1;
  1141. tprintf("#\n");
  1142. print_summary();
  1143. return 0;
  1144. }
  1145. static void deinit(void)
  1146. {
  1147. free_data(g->data, g->p.bytes_global);
  1148. g->data = NULL;
  1149. deinit_thread_data();
  1150. free_data(g, sizeof(*g));
  1151. g = NULL;
  1152. }
  1153. /*
  1154. * Print a short or long result, depending on the verbosity setting:
  1155. */
  1156. static void print_res(const char *name, double val,
  1157. const char *txt_unit, const char *txt_short, const char *txt_long)
  1158. {
  1159. if (!name)
  1160. name = "main,";
  1161. if (!g->p.show_quiet)
  1162. printf(" %-30s %15.3f, %-15s %s\n", name, val, txt_unit, txt_short);
  1163. else
  1164. printf(" %14.3f %s\n", val, txt_long);
  1165. }
  1166. static int __bench_numa(const char *name)
  1167. {
  1168. struct timeval start, stop, diff;
  1169. u64 runtime_ns_min, runtime_ns_sum;
  1170. pid_t *pids, pid, wpid;
  1171. double delta_runtime;
  1172. double runtime_avg;
  1173. double runtime_sec_max;
  1174. double runtime_sec_min;
  1175. int wait_stat;
  1176. double bytes;
  1177. int i, t, p;
  1178. if (init())
  1179. return -1;
  1180. pids = zalloc(g->p.nr_proc * sizeof(*pids));
  1181. pid = -1;
  1182. /* All threads try to acquire it, this way we can wait for them to start up: */
  1183. pthread_mutex_lock(&g->start_work_mutex);
  1184. if (g->p.serialize_startup) {
  1185. tprintf(" #\n");
  1186. tprintf(" # Startup synchronization: ..."); fflush(stdout);
  1187. }
  1188. gettimeofday(&start, NULL);
  1189. for (i = 0; i < g->p.nr_proc; i++) {
  1190. pid = fork();
  1191. dprintf(" # process %2d: PID %d\n", i, pid);
  1192. BUG_ON(pid < 0);
  1193. if (!pid) {
  1194. /* Child process: */
  1195. worker_process(i);
  1196. exit(0);
  1197. }
  1198. pids[i] = pid;
  1199. }
  1200. /* Wait for all the threads to start up: */
  1201. while (g->nr_tasks_started != g->p.nr_tasks)
  1202. usleep(USEC_PER_MSEC);
  1203. BUG_ON(g->nr_tasks_started != g->p.nr_tasks);
  1204. if (g->p.serialize_startup) {
  1205. double startup_sec;
  1206. pthread_mutex_lock(&g->startup_done_mutex);
  1207. /* This will start all threads: */
  1208. pthread_mutex_unlock(&g->start_work_mutex);
  1209. /* This mutex is locked - the last started thread will wake us: */
  1210. pthread_mutex_lock(&g->startup_done_mutex);
  1211. gettimeofday(&stop, NULL);
  1212. timersub(&stop, &start, &diff);
  1213. startup_sec = diff.tv_sec * NSEC_PER_SEC;
  1214. startup_sec += diff.tv_usec * NSEC_PER_USEC;
  1215. startup_sec /= NSEC_PER_SEC;
  1216. tprintf(" threads initialized in %.6f seconds.\n", startup_sec);
  1217. tprintf(" #\n");
  1218. start = stop;
  1219. pthread_mutex_unlock(&g->startup_done_mutex);
  1220. } else {
  1221. gettimeofday(&start, NULL);
  1222. }
  1223. /* Parent process: */
  1224. for (i = 0; i < g->p.nr_proc; i++) {
  1225. wpid = waitpid(pids[i], &wait_stat, 0);
  1226. BUG_ON(wpid < 0);
  1227. BUG_ON(!WIFEXITED(wait_stat));
  1228. }
  1229. runtime_ns_sum = 0;
  1230. runtime_ns_min = -1LL;
  1231. for (t = 0; t < g->p.nr_tasks; t++) {
  1232. u64 thread_runtime_ns = g->threads[t].runtime_ns;
  1233. runtime_ns_sum += thread_runtime_ns;
  1234. runtime_ns_min = min(thread_runtime_ns, runtime_ns_min);
  1235. }
  1236. gettimeofday(&stop, NULL);
  1237. timersub(&stop, &start, &diff);
  1238. BUG_ON(bench_format != BENCH_FORMAT_DEFAULT);
  1239. tprintf("\n ###\n");
  1240. tprintf("\n");
  1241. runtime_sec_max = diff.tv_sec * NSEC_PER_SEC;
  1242. runtime_sec_max += diff.tv_usec * NSEC_PER_USEC;
  1243. runtime_sec_max /= NSEC_PER_SEC;
  1244. runtime_sec_min = runtime_ns_min / NSEC_PER_SEC;
  1245. bytes = g->bytes_done;
  1246. runtime_avg = (double)runtime_ns_sum / g->p.nr_tasks / NSEC_PER_SEC;
  1247. if (g->p.measure_convergence) {
  1248. print_res(name, runtime_sec_max,
  1249. "secs,", "NUMA-convergence-latency", "secs latency to NUMA-converge");
  1250. }
  1251. print_res(name, runtime_sec_max,
  1252. "secs,", "runtime-max/thread", "secs slowest (max) thread-runtime");
  1253. print_res(name, runtime_sec_min,
  1254. "secs,", "runtime-min/thread", "secs fastest (min) thread-runtime");
  1255. print_res(name, runtime_avg,
  1256. "secs,", "runtime-avg/thread", "secs average thread-runtime");
  1257. delta_runtime = (runtime_sec_max - runtime_sec_min)/2.0;
  1258. print_res(name, delta_runtime / runtime_sec_max * 100.0,
  1259. "%,", "spread-runtime/thread", "% difference between max/avg runtime");
  1260. print_res(name, bytes / g->p.nr_tasks / 1e9,
  1261. "GB,", "data/thread", "GB data processed, per thread");
  1262. print_res(name, bytes / 1e9,
  1263. "GB,", "data-total", "GB data processed, total");
  1264. print_res(name, runtime_sec_max * NSEC_PER_SEC / (bytes / g->p.nr_tasks),
  1265. "nsecs,", "runtime/byte/thread","nsecs/byte/thread runtime");
  1266. print_res(name, bytes / g->p.nr_tasks / 1e9 / runtime_sec_max,
  1267. "GB/sec,", "thread-speed", "GB/sec/thread speed");
  1268. print_res(name, bytes / runtime_sec_max / 1e9,
  1269. "GB/sec,", "total-speed", "GB/sec total speed");
  1270. if (g->p.show_details >= 2) {
  1271. char tname[14 + 2 * 10 + 1];
  1272. struct thread_data *td;
  1273. for (p = 0; p < g->p.nr_proc; p++) {
  1274. for (t = 0; t < g->p.nr_threads; t++) {
  1275. memset(tname, 0, sizeof(tname));
  1276. td = g->threads + p*g->p.nr_threads + t;
  1277. snprintf(tname, sizeof(tname), "process%d:thread%d", p, t);
  1278. print_res(tname, td->speed_gbs,
  1279. "GB/sec", "thread-speed", "GB/sec/thread speed");
  1280. print_res(tname, td->system_time_ns / NSEC_PER_SEC,
  1281. "secs", "thread-system-time", "system CPU time/thread");
  1282. print_res(tname, td->user_time_ns / NSEC_PER_SEC,
  1283. "secs", "thread-user-time", "user CPU time/thread");
  1284. }
  1285. }
  1286. }
  1287. free(pids);
  1288. deinit();
  1289. return 0;
  1290. }
  1291. #define MAX_ARGS 50
  1292. static int command_size(const char **argv)
  1293. {
  1294. int size = 0;
  1295. while (*argv) {
  1296. size++;
  1297. argv++;
  1298. }
  1299. BUG_ON(size >= MAX_ARGS);
  1300. return size;
  1301. }
  1302. static void init_params(struct params *p, const char *name, int argc, const char **argv)
  1303. {
  1304. int i;
  1305. printf("\n # Running %s \"perf bench numa", name);
  1306. for (i = 0; i < argc; i++)
  1307. printf(" %s", argv[i]);
  1308. printf("\"\n");
  1309. memset(p, 0, sizeof(*p));
  1310. /* Initialize nonzero defaults: */
  1311. p->serialize_startup = 1;
  1312. p->data_reads = true;
  1313. p->data_writes = true;
  1314. p->data_backwards = true;
  1315. p->data_rand_walk = true;
  1316. p->nr_loops = -1;
  1317. p->init_random = true;
  1318. p->mb_global_str = "1";
  1319. p->nr_proc = 1;
  1320. p->nr_threads = 1;
  1321. p->nr_secs = 5;
  1322. p->run_all = argc == 1;
  1323. }
  1324. static int run_bench_numa(const char *name, const char **argv)
  1325. {
  1326. int argc = command_size(argv);
  1327. init_params(&p0, name, argc, argv);
  1328. argc = parse_options(argc, argv, options, bench_numa_usage, 0);
  1329. if (argc)
  1330. goto err;
  1331. if (__bench_numa(name))
  1332. goto err;
  1333. return 0;
  1334. err:
  1335. return -1;
  1336. }
  1337. #define OPT_BW_RAM "-s", "20", "-zZq", "--thp", " 1", "--no-data_rand_walk"
  1338. #define OPT_BW_RAM_NOTHP OPT_BW_RAM, "--thp", "-1"
  1339. #define OPT_CONV "-s", "100", "-zZ0qcm", "--thp", " 1"
  1340. #define OPT_CONV_NOTHP OPT_CONV, "--thp", "-1"
  1341. #define OPT_BW "-s", "20", "-zZ0q", "--thp", " 1"
  1342. #define OPT_BW_NOTHP OPT_BW, "--thp", "-1"
  1343. /*
  1344. * The built-in test-suite executed by "perf bench numa -a".
  1345. *
  1346. * (A minimum of 4 nodes and 16 GB of RAM is recommended.)
  1347. */
  1348. static const char *tests[][MAX_ARGS] = {
  1349. /* Basic single-stream NUMA bandwidth measurements: */
  1350. { "RAM-bw-local,", "mem", "-p", "1", "-t", "1", "-P", "1024",
  1351. "-C" , "0", "-M", "0", OPT_BW_RAM },
  1352. { "RAM-bw-local-NOTHP,",
  1353. "mem", "-p", "1", "-t", "1", "-P", "1024",
  1354. "-C" , "0", "-M", "0", OPT_BW_RAM_NOTHP },
  1355. { "RAM-bw-remote,", "mem", "-p", "1", "-t", "1", "-P", "1024",
  1356. "-C" , "0", "-M", "1", OPT_BW_RAM },
  1357. /* 2-stream NUMA bandwidth measurements: */
  1358. { "RAM-bw-local-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1359. "-C", "0,2", "-M", "0x2", OPT_BW_RAM },
  1360. { "RAM-bw-remote-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1361. "-C", "0,2", "-M", "1x2", OPT_BW_RAM },
  1362. /* Cross-stream NUMA bandwidth measurement: */
  1363. { "RAM-bw-cross,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1364. "-C", "0,8", "-M", "1,0", OPT_BW_RAM },
  1365. /* Convergence latency measurements: */
  1366. { " 1x3-convergence,", "mem", "-p", "1", "-t", "3", "-P", "512", OPT_CONV },
  1367. { " 1x4-convergence,", "mem", "-p", "1", "-t", "4", "-P", "512", OPT_CONV },
  1368. { " 1x6-convergence,", "mem", "-p", "1", "-t", "6", "-P", "1020", OPT_CONV },
  1369. { " 2x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
  1370. { " 3x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
  1371. { " 4x4-convergence,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV },
  1372. { " 4x4-convergence-NOTHP,",
  1373. "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
  1374. { " 4x6-convergence,", "mem", "-p", "4", "-t", "6", "-P", "1020", OPT_CONV },
  1375. { " 4x8-convergence,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_CONV },
  1376. { " 8x4-convergence,", "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV },
  1377. { " 8x4-convergence-NOTHP,",
  1378. "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
  1379. { " 3x1-convergence,", "mem", "-p", "3", "-t", "1", "-P", "512", OPT_CONV },
  1380. { " 4x1-convergence,", "mem", "-p", "4", "-t", "1", "-P", "512", OPT_CONV },
  1381. { " 8x1-convergence,", "mem", "-p", "8", "-t", "1", "-P", "512", OPT_CONV },
  1382. { "16x1-convergence,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_CONV },
  1383. { "32x1-convergence,", "mem", "-p", "32", "-t", "1", "-P", "128", OPT_CONV },
  1384. /* Various NUMA process/thread layout bandwidth measurements: */
  1385. { " 2x1-bw-process,", "mem", "-p", "2", "-t", "1", "-P", "1024", OPT_BW },
  1386. { " 3x1-bw-process,", "mem", "-p", "3", "-t", "1", "-P", "1024", OPT_BW },
  1387. { " 4x1-bw-process,", "mem", "-p", "4", "-t", "1", "-P", "1024", OPT_BW },
  1388. { " 8x1-bw-process,", "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW },
  1389. { " 8x1-bw-process-NOTHP,",
  1390. "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW_NOTHP },
  1391. { "16x1-bw-process,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_BW },
  1392. { " 4x1-bw-thread,", "mem", "-p", "1", "-t", "4", "-T", "256", OPT_BW },
  1393. { " 8x1-bw-thread,", "mem", "-p", "1", "-t", "8", "-T", "256", OPT_BW },
  1394. { "16x1-bw-thread,", "mem", "-p", "1", "-t", "16", "-T", "128", OPT_BW },
  1395. { "32x1-bw-thread,", "mem", "-p", "1", "-t", "32", "-T", "64", OPT_BW },
  1396. { " 2x3-bw-thread,", "mem", "-p", "2", "-t", "3", "-P", "512", OPT_BW },
  1397. { " 4x4-bw-thread,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_BW },
  1398. { " 4x6-bw-thread,", "mem", "-p", "4", "-t", "6", "-P", "512", OPT_BW },
  1399. { " 4x8-bw-thread,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW },
  1400. { " 4x8-bw-thread-NOTHP,",
  1401. "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW_NOTHP },
  1402. { " 3x3-bw-thread,", "mem", "-p", "3", "-t", "3", "-P", "512", OPT_BW },
  1403. { " 5x5-bw-thread,", "mem", "-p", "5", "-t", "5", "-P", "512", OPT_BW },
  1404. { "2x16-bw-thread,", "mem", "-p", "2", "-t", "16", "-P", "512", OPT_BW },
  1405. { "1x32-bw-thread,", "mem", "-p", "1", "-t", "32", "-P", "2048", OPT_BW },
  1406. { "numa02-bw,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW },
  1407. { "numa02-bw-NOTHP,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW_NOTHP },
  1408. { "numa01-bw-thread,", "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW },
  1409. { "numa01-bw-thread-NOTHP,",
  1410. "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW_NOTHP },
  1411. };
  1412. static int bench_all(void)
  1413. {
  1414. int nr = ARRAY_SIZE(tests);
  1415. int ret;
  1416. int i;
  1417. ret = system("echo ' #'; echo ' # Running test on: '$(uname -a); echo ' #'");
  1418. BUG_ON(ret < 0);
  1419. for (i = 0; i < nr; i++) {
  1420. run_bench_numa(tests[i][0], tests[i] + 1);
  1421. }
  1422. printf("\n");
  1423. return 0;
  1424. }
  1425. int bench_numa(int argc, const char **argv)
  1426. {
  1427. init_params(&p0, "main,", argc, argv);
  1428. argc = parse_options(argc, argv, options, bench_numa_usage, 0);
  1429. if (argc)
  1430. goto err;
  1431. if (p0.run_all)
  1432. return bench_all();
  1433. if (__bench_numa(NULL))
  1434. goto err;
  1435. return 0;
  1436. err:
  1437. usage_with_options(numa_usage, options);
  1438. return -1;
  1439. }