numa.c 41 KB

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