numa.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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 int 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 0;
  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. if (bind_cpu_0 >= g->p.nr_cpus || bind_cpu_1 >= g->p.nr_cpus) {
  395. printf("\nTest not applicable, system has only %d CPUs.\n", g->p.nr_cpus);
  396. return -1;
  397. }
  398. BUG_ON(bind_cpu_0 < 0 || bind_cpu_1 < 0);
  399. BUG_ON(bind_cpu_0 > bind_cpu_1);
  400. for (bind_cpu = bind_cpu_0; bind_cpu <= bind_cpu_1; bind_cpu += step) {
  401. int i;
  402. for (i = 0; i < mul; i++) {
  403. int cpu;
  404. if (t >= g->p.nr_tasks) {
  405. printf("\n# NOTE: ignoring bind CPUs starting at CPU#%d\n #", bind_cpu);
  406. goto out;
  407. }
  408. td = g->threads + t;
  409. if (t)
  410. tprintf(",");
  411. if (bind_len > 1) {
  412. tprintf("%2d/%d", bind_cpu, bind_len);
  413. } else {
  414. tprintf("%2d", bind_cpu);
  415. }
  416. CPU_ZERO(&td->bind_cpumask);
  417. for (cpu = bind_cpu; cpu < bind_cpu+bind_len; cpu++) {
  418. BUG_ON(cpu < 0 || cpu >= g->p.nr_cpus);
  419. CPU_SET(cpu, &td->bind_cpumask);
  420. }
  421. t++;
  422. }
  423. }
  424. }
  425. out:
  426. tprintf("\n");
  427. if (t < g->p.nr_tasks)
  428. printf("# NOTE: %d tasks bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
  429. free(str0);
  430. return 0;
  431. }
  432. static int parse_cpus_opt(const struct option *opt __maybe_unused,
  433. const char *arg, int unset __maybe_unused)
  434. {
  435. if (!arg)
  436. return -1;
  437. return parse_cpu_list(arg);
  438. }
  439. static int parse_node_list(const char *arg)
  440. {
  441. p0.node_list_str = strdup(arg);
  442. dprintf("got NODE list: {%s}\n", p0.node_list_str);
  443. return 0;
  444. }
  445. static int parse_setup_node_list(void)
  446. {
  447. struct thread_data *td;
  448. char *str0, *str;
  449. int t;
  450. if (!g->p.node_list_str)
  451. return 0;
  452. dprintf("g->p.nr_tasks: %d\n", g->p.nr_tasks);
  453. str0 = str = strdup(g->p.node_list_str);
  454. t = 0;
  455. BUG_ON(!str);
  456. tprintf("# binding tasks to NODEs:\n");
  457. tprintf("# ");
  458. while (true) {
  459. int bind_node, bind_node_0, bind_node_1;
  460. char *tok, *tok_end, *tok_step, *tok_mul;
  461. int step;
  462. int mul;
  463. tok = strsep(&str, ",");
  464. if (!tok)
  465. break;
  466. tok_end = strstr(tok, "-");
  467. dprintf("\ntoken: {%s}, end: {%s}\n", tok, tok_end);
  468. if (!tok_end) {
  469. /* Single NODE specified: */
  470. bind_node_0 = bind_node_1 = atol(tok);
  471. } else {
  472. /* NODE range specified (for example: "5-11"): */
  473. bind_node_0 = atol(tok);
  474. bind_node_1 = atol(tok_end + 1);
  475. }
  476. step = 1;
  477. tok_step = strstr(tok, "#");
  478. if (tok_step) {
  479. step = atol(tok_step + 1);
  480. BUG_ON(step <= 0 || step >= g->p.nr_nodes);
  481. }
  482. /* Multiplicator shortcut, "0x8" is a shortcut for: "0,0,0,0,0,0,0,0" */
  483. mul = 1;
  484. tok_mul = strstr(tok, "x");
  485. if (tok_mul) {
  486. mul = atol(tok_mul + 1);
  487. BUG_ON(mul <= 0);
  488. }
  489. dprintf("NODEs: %d-%d #%d\n", bind_node_0, bind_node_1, step);
  490. if (bind_node_0 >= g->p.nr_nodes || bind_node_1 >= g->p.nr_nodes) {
  491. printf("\nTest not applicable, system has only %d nodes.\n", g->p.nr_nodes);
  492. return -1;
  493. }
  494. BUG_ON(bind_node_0 < 0 || bind_node_1 < 0);
  495. BUG_ON(bind_node_0 > bind_node_1);
  496. for (bind_node = bind_node_0; bind_node <= bind_node_1; bind_node += step) {
  497. int i;
  498. for (i = 0; i < mul; i++) {
  499. if (t >= g->p.nr_tasks) {
  500. printf("\n# NOTE: ignoring bind NODEs starting at NODE#%d\n", bind_node);
  501. goto out;
  502. }
  503. td = g->threads + t;
  504. if (!t)
  505. tprintf(" %2d", bind_node);
  506. else
  507. tprintf(",%2d", bind_node);
  508. td->bind_node = bind_node;
  509. t++;
  510. }
  511. }
  512. }
  513. out:
  514. tprintf("\n");
  515. if (t < g->p.nr_tasks)
  516. printf("# NOTE: %d tasks mem-bound, %d tasks unbound\n", t, g->p.nr_tasks - t);
  517. free(str0);
  518. return 0;
  519. }
  520. static int parse_nodes_opt(const struct option *opt __maybe_unused,
  521. const char *arg, int unset __maybe_unused)
  522. {
  523. if (!arg)
  524. return -1;
  525. return parse_node_list(arg);
  526. return 0;
  527. }
  528. #define BIT(x) (1ul << x)
  529. static inline uint32_t lfsr_32(uint32_t lfsr)
  530. {
  531. const uint32_t taps = BIT(1) | BIT(5) | BIT(6) | BIT(31);
  532. return (lfsr>>1) ^ ((0x0u - (lfsr & 0x1u)) & taps);
  533. }
  534. /*
  535. * Make sure there's real data dependency to RAM (when read
  536. * accesses are enabled), so the compiler, the CPU and the
  537. * kernel (KSM, zero page, etc.) cannot optimize away RAM
  538. * accesses:
  539. */
  540. static inline u64 access_data(u64 *data __attribute__((unused)), u64 val)
  541. {
  542. if (g->p.data_reads)
  543. val += *data;
  544. if (g->p.data_writes)
  545. *data = val + 1;
  546. return val;
  547. }
  548. /*
  549. * The worker process does two types of work, a forwards going
  550. * loop and a backwards going loop.
  551. *
  552. * We do this so that on multiprocessor systems we do not create
  553. * a 'train' of processing, with highly synchronized processes,
  554. * skewing the whole benchmark.
  555. */
  556. static u64 do_work(u8 *__data, long bytes, int nr, int nr_max, int loop, u64 val)
  557. {
  558. long words = bytes/sizeof(u64);
  559. u64 *data = (void *)__data;
  560. long chunk_0, chunk_1;
  561. u64 *d0, *d, *d1;
  562. long off;
  563. long i;
  564. BUG_ON(!data && words);
  565. BUG_ON(data && !words);
  566. if (!data)
  567. return val;
  568. /* Very simple memset() work variant: */
  569. if (g->p.data_zero_memset && !g->p.data_rand_walk) {
  570. bzero(data, bytes);
  571. return val;
  572. }
  573. /* Spread out by PID/TID nr and by loop nr: */
  574. chunk_0 = words/nr_max;
  575. chunk_1 = words/g->p.nr_loops;
  576. off = nr*chunk_0 + loop*chunk_1;
  577. while (off >= words)
  578. off -= words;
  579. if (g->p.data_rand_walk) {
  580. u32 lfsr = nr + loop + val;
  581. int j;
  582. for (i = 0; i < words/1024; i++) {
  583. long start, end;
  584. lfsr = lfsr_32(lfsr);
  585. start = lfsr % words;
  586. end = min(start + 1024, words-1);
  587. if (g->p.data_zero_memset) {
  588. bzero(data + start, (end-start) * sizeof(u64));
  589. } else {
  590. for (j = start; j < end; j++)
  591. val = access_data(data + j, val);
  592. }
  593. }
  594. } else if (!g->p.data_backwards || (nr + loop) & 1) {
  595. d0 = data + off;
  596. d = data + off + 1;
  597. d1 = data + words;
  598. /* Process data forwards: */
  599. for (;;) {
  600. if (unlikely(d >= d1))
  601. d = data;
  602. if (unlikely(d == d0))
  603. break;
  604. val = access_data(d, val);
  605. d++;
  606. }
  607. } else {
  608. /* Process data backwards: */
  609. d0 = data + off;
  610. d = data + off - 1;
  611. d1 = data + words;
  612. /* Process data forwards: */
  613. for (;;) {
  614. if (unlikely(d < data))
  615. d = data + words-1;
  616. if (unlikely(d == d0))
  617. break;
  618. val = access_data(d, val);
  619. d--;
  620. }
  621. }
  622. return val;
  623. }
  624. static void update_curr_cpu(int task_nr, unsigned long bytes_worked)
  625. {
  626. unsigned int cpu;
  627. cpu = sched_getcpu();
  628. g->threads[task_nr].curr_cpu = cpu;
  629. prctl(0, bytes_worked);
  630. }
  631. #define MAX_NR_NODES 64
  632. /*
  633. * Count the number of nodes a process's threads
  634. * are spread out on.
  635. *
  636. * A count of 1 means that the process is compressed
  637. * to a single node. A count of g->p.nr_nodes means it's
  638. * spread out on the whole system.
  639. */
  640. static int count_process_nodes(int process_nr)
  641. {
  642. char node_present[MAX_NR_NODES] = { 0, };
  643. int nodes;
  644. int n, t;
  645. for (t = 0; t < g->p.nr_threads; t++) {
  646. struct thread_data *td;
  647. int task_nr;
  648. int node;
  649. task_nr = process_nr*g->p.nr_threads + t;
  650. td = g->threads + task_nr;
  651. node = numa_node_of_cpu(td->curr_cpu);
  652. node_present[node] = 1;
  653. }
  654. nodes = 0;
  655. for (n = 0; n < MAX_NR_NODES; n++)
  656. nodes += node_present[n];
  657. return nodes;
  658. }
  659. /*
  660. * Count the number of distinct process-threads a node contains.
  661. *
  662. * A count of 1 means that the node contains only a single
  663. * process. If all nodes on the system contain at most one
  664. * process then we are well-converged.
  665. */
  666. static int count_node_processes(int node)
  667. {
  668. int processes = 0;
  669. int t, p;
  670. for (p = 0; p < g->p.nr_proc; p++) {
  671. for (t = 0; t < g->p.nr_threads; t++) {
  672. struct thread_data *td;
  673. int task_nr;
  674. int n;
  675. task_nr = p*g->p.nr_threads + t;
  676. td = g->threads + task_nr;
  677. n = numa_node_of_cpu(td->curr_cpu);
  678. if (n == node) {
  679. processes++;
  680. break;
  681. }
  682. }
  683. }
  684. return processes;
  685. }
  686. static void calc_convergence_compression(int *strong)
  687. {
  688. unsigned int nodes_min, nodes_max;
  689. int p;
  690. nodes_min = -1;
  691. nodes_max = 0;
  692. for (p = 0; p < g->p.nr_proc; p++) {
  693. unsigned int nodes = count_process_nodes(p);
  694. nodes_min = min(nodes, nodes_min);
  695. nodes_max = max(nodes, nodes_max);
  696. }
  697. /* Strong convergence: all threads compress on a single node: */
  698. if (nodes_min == 1 && nodes_max == 1) {
  699. *strong = 1;
  700. } else {
  701. *strong = 0;
  702. tprintf(" {%d-%d}", nodes_min, nodes_max);
  703. }
  704. }
  705. static void calc_convergence(double runtime_ns_max, double *convergence)
  706. {
  707. unsigned int loops_done_min, loops_done_max;
  708. int process_groups;
  709. int nodes[MAX_NR_NODES];
  710. int distance;
  711. int nr_min;
  712. int nr_max;
  713. int strong;
  714. int sum;
  715. int nr;
  716. int node;
  717. int cpu;
  718. int t;
  719. if (!g->p.show_convergence && !g->p.measure_convergence)
  720. return;
  721. for (node = 0; node < g->p.nr_nodes; node++)
  722. nodes[node] = 0;
  723. loops_done_min = -1;
  724. loops_done_max = 0;
  725. for (t = 0; t < g->p.nr_tasks; t++) {
  726. struct thread_data *td = g->threads + t;
  727. unsigned int loops_done;
  728. cpu = td->curr_cpu;
  729. /* Not all threads have written it yet: */
  730. if (cpu < 0)
  731. continue;
  732. node = numa_node_of_cpu(cpu);
  733. nodes[node]++;
  734. loops_done = td->loops_done;
  735. loops_done_min = min(loops_done, loops_done_min);
  736. loops_done_max = max(loops_done, loops_done_max);
  737. }
  738. nr_max = 0;
  739. nr_min = g->p.nr_tasks;
  740. sum = 0;
  741. for (node = 0; node < g->p.nr_nodes; node++) {
  742. nr = nodes[node];
  743. nr_min = min(nr, nr_min);
  744. nr_max = max(nr, nr_max);
  745. sum += nr;
  746. }
  747. BUG_ON(nr_min > nr_max);
  748. BUG_ON(sum > g->p.nr_tasks);
  749. if (0 && (sum < g->p.nr_tasks))
  750. return;
  751. /*
  752. * Count the number of distinct process groups present
  753. * on nodes - when we are converged this will decrease
  754. * to g->p.nr_proc:
  755. */
  756. process_groups = 0;
  757. for (node = 0; node < g->p.nr_nodes; node++) {
  758. int processes = count_node_processes(node);
  759. nr = nodes[node];
  760. tprintf(" %2d/%-2d", nr, processes);
  761. process_groups += processes;
  762. }
  763. distance = nr_max - nr_min;
  764. tprintf(" [%2d/%-2d]", distance, process_groups);
  765. tprintf(" l:%3d-%-3d (%3d)",
  766. loops_done_min, loops_done_max, loops_done_max-loops_done_min);
  767. if (loops_done_min && loops_done_max) {
  768. double skew = 1.0 - (double)loops_done_min/loops_done_max;
  769. tprintf(" [%4.1f%%]", skew * 100.0);
  770. }
  771. calc_convergence_compression(&strong);
  772. if (strong && process_groups == g->p.nr_proc) {
  773. if (!*convergence) {
  774. *convergence = runtime_ns_max;
  775. tprintf(" (%6.1fs converged)\n", *convergence/1e9);
  776. if (g->p.measure_convergence) {
  777. g->all_converged = true;
  778. g->stop_work = true;
  779. }
  780. }
  781. } else {
  782. if (*convergence) {
  783. tprintf(" (%6.1fs de-converged)", runtime_ns_max/1e9);
  784. *convergence = 0;
  785. }
  786. tprintf("\n");
  787. }
  788. }
  789. static void show_summary(double runtime_ns_max, int l, double *convergence)
  790. {
  791. tprintf("\r # %5.1f%% [%.1f mins]",
  792. (double)(l+1)/g->p.nr_loops*100.0, runtime_ns_max/1e9 / 60.0);
  793. calc_convergence(runtime_ns_max, convergence);
  794. if (g->p.show_details >= 0)
  795. fflush(stdout);
  796. }
  797. static void *worker_thread(void *__tdata)
  798. {
  799. struct thread_data *td = __tdata;
  800. struct timeval start0, start, stop, diff;
  801. int process_nr = td->process_nr;
  802. int thread_nr = td->thread_nr;
  803. unsigned long last_perturbance;
  804. int task_nr = td->task_nr;
  805. int details = g->p.show_details;
  806. int first_task, last_task;
  807. double convergence = 0;
  808. u64 val = td->val;
  809. double runtime_ns_max;
  810. u8 *global_data;
  811. u8 *process_data;
  812. u8 *thread_data;
  813. u64 bytes_done;
  814. long work_done;
  815. u32 l;
  816. bind_to_cpumask(td->bind_cpumask);
  817. bind_to_memnode(td->bind_node);
  818. set_taskname("thread %d/%d", process_nr, thread_nr);
  819. global_data = g->data;
  820. process_data = td->process_data;
  821. thread_data = setup_private_data(g->p.bytes_thread);
  822. bytes_done = 0;
  823. last_task = 0;
  824. if (process_nr == g->p.nr_proc-1 && thread_nr == g->p.nr_threads-1)
  825. last_task = 1;
  826. first_task = 0;
  827. if (process_nr == 0 && thread_nr == 0)
  828. first_task = 1;
  829. if (details >= 2) {
  830. printf("# thread %2d / %2d global mem: %p, process mem: %p, thread mem: %p\n",
  831. process_nr, thread_nr, global_data, process_data, thread_data);
  832. }
  833. if (g->p.serialize_startup) {
  834. pthread_mutex_lock(&g->startup_mutex);
  835. g->nr_tasks_started++;
  836. pthread_mutex_unlock(&g->startup_mutex);
  837. /* Here we will wait for the main process to start us all at once: */
  838. pthread_mutex_lock(&g->start_work_mutex);
  839. g->nr_tasks_working++;
  840. /* Last one wake the main process: */
  841. if (g->nr_tasks_working == g->p.nr_tasks)
  842. pthread_mutex_unlock(&g->startup_done_mutex);
  843. pthread_mutex_unlock(&g->start_work_mutex);
  844. }
  845. gettimeofday(&start0, NULL);
  846. start = stop = start0;
  847. last_perturbance = start.tv_sec;
  848. for (l = 0; l < g->p.nr_loops; l++) {
  849. start = stop;
  850. if (g->stop_work)
  851. break;
  852. val += do_work(global_data, g->p.bytes_global, process_nr, g->p.nr_proc, l, val);
  853. val += do_work(process_data, g->p.bytes_process, thread_nr, g->p.nr_threads, l, val);
  854. val += do_work(thread_data, g->p.bytes_thread, 0, 1, l, val);
  855. if (g->p.sleep_usecs) {
  856. pthread_mutex_lock(td->process_lock);
  857. usleep(g->p.sleep_usecs);
  858. pthread_mutex_unlock(td->process_lock);
  859. }
  860. /*
  861. * Amount of work to be done under a process-global lock:
  862. */
  863. if (g->p.bytes_process_locked) {
  864. pthread_mutex_lock(td->process_lock);
  865. val += do_work(process_data, g->p.bytes_process_locked, thread_nr, g->p.nr_threads, l, val);
  866. pthread_mutex_unlock(td->process_lock);
  867. }
  868. work_done = g->p.bytes_global + g->p.bytes_process +
  869. g->p.bytes_process_locked + g->p.bytes_thread;
  870. update_curr_cpu(task_nr, work_done);
  871. bytes_done += work_done;
  872. if (details < 0 && !g->p.perturb_secs && !g->p.measure_convergence && !g->p.nr_secs)
  873. continue;
  874. td->loops_done = l;
  875. gettimeofday(&stop, NULL);
  876. /* Check whether our max runtime timed out: */
  877. if (g->p.nr_secs) {
  878. timersub(&stop, &start0, &diff);
  879. if ((u32)diff.tv_sec >= g->p.nr_secs) {
  880. g->stop_work = true;
  881. break;
  882. }
  883. }
  884. /* Update the summary at most once per second: */
  885. if (start.tv_sec == stop.tv_sec)
  886. continue;
  887. /*
  888. * Perturb the first task's equilibrium every g->p.perturb_secs seconds,
  889. * by migrating to CPU#0:
  890. */
  891. if (first_task && g->p.perturb_secs && (int)(stop.tv_sec - last_perturbance) >= g->p.perturb_secs) {
  892. cpu_set_t orig_mask;
  893. int target_cpu;
  894. int this_cpu;
  895. last_perturbance = stop.tv_sec;
  896. /*
  897. * Depending on where we are running, move into
  898. * the other half of the system, to create some
  899. * real disturbance:
  900. */
  901. this_cpu = g->threads[task_nr].curr_cpu;
  902. if (this_cpu < g->p.nr_cpus/2)
  903. target_cpu = g->p.nr_cpus-1;
  904. else
  905. target_cpu = 0;
  906. orig_mask = bind_to_cpu(target_cpu);
  907. /* Here we are running on the target CPU already */
  908. if (details >= 1)
  909. printf(" (injecting perturbalance, moved to CPU#%d)\n", target_cpu);
  910. bind_to_cpumask(orig_mask);
  911. }
  912. if (details >= 3) {
  913. timersub(&stop, &start, &diff);
  914. runtime_ns_max = diff.tv_sec * 1000000000;
  915. runtime_ns_max += diff.tv_usec * 1000;
  916. if (details >= 0) {
  917. printf(" #%2d / %2d: %14.2lf nsecs/op [val: %016"PRIx64"]\n",
  918. process_nr, thread_nr, runtime_ns_max / bytes_done, val);
  919. }
  920. fflush(stdout);
  921. }
  922. if (!last_task)
  923. continue;
  924. timersub(&stop, &start0, &diff);
  925. runtime_ns_max = diff.tv_sec * 1000000000ULL;
  926. runtime_ns_max += diff.tv_usec * 1000ULL;
  927. show_summary(runtime_ns_max, l, &convergence);
  928. }
  929. gettimeofday(&stop, NULL);
  930. timersub(&stop, &start0, &diff);
  931. td->runtime_ns = diff.tv_sec * 1000000000ULL;
  932. td->runtime_ns += diff.tv_usec * 1000ULL;
  933. free_data(thread_data, g->p.bytes_thread);
  934. pthread_mutex_lock(&g->stop_work_mutex);
  935. g->bytes_done += bytes_done;
  936. pthread_mutex_unlock(&g->stop_work_mutex);
  937. return NULL;
  938. }
  939. /*
  940. * A worker process starts a couple of threads:
  941. */
  942. static void worker_process(int process_nr)
  943. {
  944. pthread_mutex_t process_lock;
  945. struct thread_data *td;
  946. pthread_t *pthreads;
  947. u8 *process_data;
  948. int task_nr;
  949. int ret;
  950. int t;
  951. pthread_mutex_init(&process_lock, NULL);
  952. set_taskname("process %d", process_nr);
  953. /*
  954. * Pick up the memory policy and the CPU binding of our first thread,
  955. * so that we initialize memory accordingly:
  956. */
  957. task_nr = process_nr*g->p.nr_threads;
  958. td = g->threads + task_nr;
  959. bind_to_memnode(td->bind_node);
  960. bind_to_cpumask(td->bind_cpumask);
  961. pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
  962. process_data = setup_private_data(g->p.bytes_process);
  963. if (g->p.show_details >= 3) {
  964. printf(" # process %2d global mem: %p, process mem: %p\n",
  965. process_nr, g->data, process_data);
  966. }
  967. for (t = 0; t < g->p.nr_threads; t++) {
  968. task_nr = process_nr*g->p.nr_threads + t;
  969. td = g->threads + task_nr;
  970. td->process_data = process_data;
  971. td->process_nr = process_nr;
  972. td->thread_nr = t;
  973. td->task_nr = task_nr;
  974. td->val = rand();
  975. td->curr_cpu = -1;
  976. td->process_lock = &process_lock;
  977. ret = pthread_create(pthreads + t, NULL, worker_thread, td);
  978. BUG_ON(ret);
  979. }
  980. for (t = 0; t < g->p.nr_threads; t++) {
  981. ret = pthread_join(pthreads[t], NULL);
  982. BUG_ON(ret);
  983. }
  984. free_data(process_data, g->p.bytes_process);
  985. free(pthreads);
  986. }
  987. static void print_summary(void)
  988. {
  989. if (g->p.show_details < 0)
  990. return;
  991. printf("\n ###\n");
  992. printf(" # %d %s will execute (on %d nodes, %d CPUs):\n",
  993. g->p.nr_tasks, g->p.nr_tasks == 1 ? "task" : "tasks", g->p.nr_nodes, g->p.nr_cpus);
  994. printf(" # %5dx %5ldMB global shared mem operations\n",
  995. g->p.nr_loops, g->p.bytes_global/1024/1024);
  996. printf(" # %5dx %5ldMB process shared mem operations\n",
  997. g->p.nr_loops, g->p.bytes_process/1024/1024);
  998. printf(" # %5dx %5ldMB thread local mem operations\n",
  999. g->p.nr_loops, g->p.bytes_thread/1024/1024);
  1000. printf(" ###\n");
  1001. printf("\n ###\n"); fflush(stdout);
  1002. }
  1003. static void init_thread_data(void)
  1004. {
  1005. ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
  1006. int t;
  1007. g->threads = zalloc_shared_data(size);
  1008. for (t = 0; t < g->p.nr_tasks; t++) {
  1009. struct thread_data *td = g->threads + t;
  1010. int cpu;
  1011. /* Allow all nodes by default: */
  1012. td->bind_node = -1;
  1013. /* Allow all CPUs by default: */
  1014. CPU_ZERO(&td->bind_cpumask);
  1015. for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
  1016. CPU_SET(cpu, &td->bind_cpumask);
  1017. }
  1018. }
  1019. static void deinit_thread_data(void)
  1020. {
  1021. ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
  1022. free_data(g->threads, size);
  1023. }
  1024. static int init(void)
  1025. {
  1026. g = (void *)alloc_data(sizeof(*g), MAP_SHARED, 1, 0, 0 /* THP */, 0);
  1027. /* Copy over options: */
  1028. g->p = p0;
  1029. g->p.nr_cpus = numa_num_configured_cpus();
  1030. g->p.nr_nodes = numa_max_node() + 1;
  1031. /* char array in count_process_nodes(): */
  1032. BUG_ON(g->p.nr_nodes > MAX_NR_NODES || g->p.nr_nodes < 0);
  1033. if (g->p.show_quiet && !g->p.show_details)
  1034. g->p.show_details = -1;
  1035. /* Some memory should be specified: */
  1036. if (!g->p.mb_global_str && !g->p.mb_proc_str && !g->p.mb_thread_str)
  1037. return -1;
  1038. if (g->p.mb_global_str) {
  1039. g->p.mb_global = atof(g->p.mb_global_str);
  1040. BUG_ON(g->p.mb_global < 0);
  1041. }
  1042. if (g->p.mb_proc_str) {
  1043. g->p.mb_proc = atof(g->p.mb_proc_str);
  1044. BUG_ON(g->p.mb_proc < 0);
  1045. }
  1046. if (g->p.mb_proc_locked_str) {
  1047. g->p.mb_proc_locked = atof(g->p.mb_proc_locked_str);
  1048. BUG_ON(g->p.mb_proc_locked < 0);
  1049. BUG_ON(g->p.mb_proc_locked > g->p.mb_proc);
  1050. }
  1051. if (g->p.mb_thread_str) {
  1052. g->p.mb_thread = atof(g->p.mb_thread_str);
  1053. BUG_ON(g->p.mb_thread < 0);
  1054. }
  1055. BUG_ON(g->p.nr_threads <= 0);
  1056. BUG_ON(g->p.nr_proc <= 0);
  1057. g->p.nr_tasks = g->p.nr_proc*g->p.nr_threads;
  1058. g->p.bytes_global = g->p.mb_global *1024L*1024L;
  1059. g->p.bytes_process = g->p.mb_proc *1024L*1024L;
  1060. g->p.bytes_process_locked = g->p.mb_proc_locked *1024L*1024L;
  1061. g->p.bytes_thread = g->p.mb_thread *1024L*1024L;
  1062. g->data = setup_shared_data(g->p.bytes_global);
  1063. /* Startup serialization: */
  1064. init_global_mutex(&g->start_work_mutex);
  1065. init_global_mutex(&g->startup_mutex);
  1066. init_global_mutex(&g->startup_done_mutex);
  1067. init_global_mutex(&g->stop_work_mutex);
  1068. init_thread_data();
  1069. tprintf("#\n");
  1070. if (parse_setup_cpu_list() || parse_setup_node_list())
  1071. return -1;
  1072. tprintf("#\n");
  1073. print_summary();
  1074. return 0;
  1075. }
  1076. static void deinit(void)
  1077. {
  1078. free_data(g->data, g->p.bytes_global);
  1079. g->data = NULL;
  1080. deinit_thread_data();
  1081. free_data(g, sizeof(*g));
  1082. g = NULL;
  1083. }
  1084. /*
  1085. * Print a short or long result, depending on the verbosity setting:
  1086. */
  1087. static void print_res(const char *name, double val,
  1088. const char *txt_unit, const char *txt_short, const char *txt_long)
  1089. {
  1090. if (!name)
  1091. name = "main,";
  1092. if (g->p.show_quiet)
  1093. printf(" %-30s %15.3f, %-15s %s\n", name, val, txt_unit, txt_short);
  1094. else
  1095. printf(" %14.3f %s\n", val, txt_long);
  1096. }
  1097. static int __bench_numa(const char *name)
  1098. {
  1099. struct timeval start, stop, diff;
  1100. u64 runtime_ns_min, runtime_ns_sum;
  1101. pid_t *pids, pid, wpid;
  1102. double delta_runtime;
  1103. double runtime_avg;
  1104. double runtime_sec_max;
  1105. double runtime_sec_min;
  1106. int wait_stat;
  1107. double bytes;
  1108. int i, t;
  1109. if (init())
  1110. return -1;
  1111. pids = zalloc(g->p.nr_proc * sizeof(*pids));
  1112. pid = -1;
  1113. /* All threads try to acquire it, this way we can wait for them to start up: */
  1114. pthread_mutex_lock(&g->start_work_mutex);
  1115. if (g->p.serialize_startup) {
  1116. tprintf(" #\n");
  1117. tprintf(" # Startup synchronization: ..."); fflush(stdout);
  1118. }
  1119. gettimeofday(&start, NULL);
  1120. for (i = 0; i < g->p.nr_proc; i++) {
  1121. pid = fork();
  1122. dprintf(" # process %2d: PID %d\n", i, pid);
  1123. BUG_ON(pid < 0);
  1124. if (!pid) {
  1125. /* Child process: */
  1126. worker_process(i);
  1127. exit(0);
  1128. }
  1129. pids[i] = pid;
  1130. }
  1131. /* Wait for all the threads to start up: */
  1132. while (g->nr_tasks_started != g->p.nr_tasks)
  1133. usleep(1000);
  1134. BUG_ON(g->nr_tasks_started != g->p.nr_tasks);
  1135. if (g->p.serialize_startup) {
  1136. double startup_sec;
  1137. pthread_mutex_lock(&g->startup_done_mutex);
  1138. /* This will start all threads: */
  1139. pthread_mutex_unlock(&g->start_work_mutex);
  1140. /* This mutex is locked - the last started thread will wake us: */
  1141. pthread_mutex_lock(&g->startup_done_mutex);
  1142. gettimeofday(&stop, NULL);
  1143. timersub(&stop, &start, &diff);
  1144. startup_sec = diff.tv_sec * 1000000000.0;
  1145. startup_sec += diff.tv_usec * 1000.0;
  1146. startup_sec /= 1e9;
  1147. tprintf(" threads initialized in %.6f seconds.\n", startup_sec);
  1148. tprintf(" #\n");
  1149. start = stop;
  1150. pthread_mutex_unlock(&g->startup_done_mutex);
  1151. } else {
  1152. gettimeofday(&start, NULL);
  1153. }
  1154. /* Parent process: */
  1155. for (i = 0; i < g->p.nr_proc; i++) {
  1156. wpid = waitpid(pids[i], &wait_stat, 0);
  1157. BUG_ON(wpid < 0);
  1158. BUG_ON(!WIFEXITED(wait_stat));
  1159. }
  1160. runtime_ns_sum = 0;
  1161. runtime_ns_min = -1LL;
  1162. for (t = 0; t < g->p.nr_tasks; t++) {
  1163. u64 thread_runtime_ns = g->threads[t].runtime_ns;
  1164. runtime_ns_sum += thread_runtime_ns;
  1165. runtime_ns_min = min(thread_runtime_ns, runtime_ns_min);
  1166. }
  1167. gettimeofday(&stop, NULL);
  1168. timersub(&stop, &start, &diff);
  1169. BUG_ON(bench_format != BENCH_FORMAT_DEFAULT);
  1170. tprintf("\n ###\n");
  1171. tprintf("\n");
  1172. runtime_sec_max = diff.tv_sec * 1000000000.0;
  1173. runtime_sec_max += diff.tv_usec * 1000.0;
  1174. runtime_sec_max /= 1e9;
  1175. runtime_sec_min = runtime_ns_min/1e9;
  1176. bytes = g->bytes_done;
  1177. runtime_avg = (double)runtime_ns_sum / g->p.nr_tasks / 1e9;
  1178. if (g->p.measure_convergence) {
  1179. print_res(name, runtime_sec_max,
  1180. "secs,", "NUMA-convergence-latency", "secs latency to NUMA-converge");
  1181. }
  1182. print_res(name, runtime_sec_max,
  1183. "secs,", "runtime-max/thread", "secs slowest (max) thread-runtime");
  1184. print_res(name, runtime_sec_min,
  1185. "secs,", "runtime-min/thread", "secs fastest (min) thread-runtime");
  1186. print_res(name, runtime_avg,
  1187. "secs,", "runtime-avg/thread", "secs average thread-runtime");
  1188. delta_runtime = (runtime_sec_max - runtime_sec_min)/2.0;
  1189. print_res(name, delta_runtime / runtime_sec_max * 100.0,
  1190. "%,", "spread-runtime/thread", "% difference between max/avg runtime");
  1191. print_res(name, bytes / g->p.nr_tasks / 1e9,
  1192. "GB,", "data/thread", "GB data processed, per thread");
  1193. print_res(name, bytes / 1e9,
  1194. "GB,", "data-total", "GB data processed, total");
  1195. print_res(name, runtime_sec_max * 1e9 / (bytes / g->p.nr_tasks),
  1196. "nsecs,", "runtime/byte/thread","nsecs/byte/thread runtime");
  1197. print_res(name, bytes / g->p.nr_tasks / 1e9 / runtime_sec_max,
  1198. "GB/sec,", "thread-speed", "GB/sec/thread speed");
  1199. print_res(name, bytes / runtime_sec_max / 1e9,
  1200. "GB/sec,", "total-speed", "GB/sec total speed");
  1201. free(pids);
  1202. deinit();
  1203. return 0;
  1204. }
  1205. #define MAX_ARGS 50
  1206. static int command_size(const char **argv)
  1207. {
  1208. int size = 0;
  1209. while (*argv) {
  1210. size++;
  1211. argv++;
  1212. }
  1213. BUG_ON(size >= MAX_ARGS);
  1214. return size;
  1215. }
  1216. static void init_params(struct params *p, const char *name, int argc, const char **argv)
  1217. {
  1218. int i;
  1219. printf("\n # Running %s \"perf bench numa", name);
  1220. for (i = 0; i < argc; i++)
  1221. printf(" %s", argv[i]);
  1222. printf("\"\n");
  1223. memset(p, 0, sizeof(*p));
  1224. /* Initialize nonzero defaults: */
  1225. p->serialize_startup = 1;
  1226. p->data_reads = true;
  1227. p->data_writes = true;
  1228. p->data_backwards = true;
  1229. p->data_rand_walk = true;
  1230. p->nr_loops = -1;
  1231. p->init_random = true;
  1232. p->run_all = argc == 1;
  1233. }
  1234. static int run_bench_numa(const char *name, const char **argv)
  1235. {
  1236. int argc = command_size(argv);
  1237. init_params(&p0, name, argc, argv);
  1238. argc = parse_options(argc, argv, options, bench_numa_usage, 0);
  1239. if (argc)
  1240. goto err;
  1241. if (__bench_numa(name))
  1242. goto err;
  1243. return 0;
  1244. err:
  1245. return -1;
  1246. }
  1247. #define OPT_BW_RAM "-s", "20", "-zZq", "--thp", " 1", "--no-data_rand_walk"
  1248. #define OPT_BW_RAM_NOTHP OPT_BW_RAM, "--thp", "-1"
  1249. #define OPT_CONV "-s", "100", "-zZ0qcm", "--thp", " 1"
  1250. #define OPT_CONV_NOTHP OPT_CONV, "--thp", "-1"
  1251. #define OPT_BW "-s", "20", "-zZ0q", "--thp", " 1"
  1252. #define OPT_BW_NOTHP OPT_BW, "--thp", "-1"
  1253. /*
  1254. * The built-in test-suite executed by "perf bench numa -a".
  1255. *
  1256. * (A minimum of 4 nodes and 16 GB of RAM is recommended.)
  1257. */
  1258. static const char *tests[][MAX_ARGS] = {
  1259. /* Basic single-stream NUMA bandwidth measurements: */
  1260. { "RAM-bw-local,", "mem", "-p", "1", "-t", "1", "-P", "1024",
  1261. "-C" , "0", "-M", "0", OPT_BW_RAM },
  1262. { "RAM-bw-local-NOTHP,",
  1263. "mem", "-p", "1", "-t", "1", "-P", "1024",
  1264. "-C" , "0", "-M", "0", OPT_BW_RAM_NOTHP },
  1265. { "RAM-bw-remote,", "mem", "-p", "1", "-t", "1", "-P", "1024",
  1266. "-C" , "0", "-M", "1", OPT_BW_RAM },
  1267. /* 2-stream NUMA bandwidth measurements: */
  1268. { "RAM-bw-local-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1269. "-C", "0,2", "-M", "0x2", OPT_BW_RAM },
  1270. { "RAM-bw-remote-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1271. "-C", "0,2", "-M", "1x2", OPT_BW_RAM },
  1272. /* Cross-stream NUMA bandwidth measurement: */
  1273. { "RAM-bw-cross,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1274. "-C", "0,8", "-M", "1,0", OPT_BW_RAM },
  1275. /* Convergence latency measurements: */
  1276. { " 1x3-convergence,", "mem", "-p", "1", "-t", "3", "-P", "512", OPT_CONV },
  1277. { " 1x4-convergence,", "mem", "-p", "1", "-t", "4", "-P", "512", OPT_CONV },
  1278. { " 1x6-convergence,", "mem", "-p", "1", "-t", "6", "-P", "1020", OPT_CONV },
  1279. { " 2x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
  1280. { " 3x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
  1281. { " 4x4-convergence,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV },
  1282. { " 4x4-convergence-NOTHP,",
  1283. "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
  1284. { " 4x6-convergence,", "mem", "-p", "4", "-t", "6", "-P", "1020", OPT_CONV },
  1285. { " 4x8-convergence,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_CONV },
  1286. { " 8x4-convergence,", "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV },
  1287. { " 8x4-convergence-NOTHP,",
  1288. "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
  1289. { " 3x1-convergence,", "mem", "-p", "3", "-t", "1", "-P", "512", OPT_CONV },
  1290. { " 4x1-convergence,", "mem", "-p", "4", "-t", "1", "-P", "512", OPT_CONV },
  1291. { " 8x1-convergence,", "mem", "-p", "8", "-t", "1", "-P", "512", OPT_CONV },
  1292. { "16x1-convergence,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_CONV },
  1293. { "32x1-convergence,", "mem", "-p", "32", "-t", "1", "-P", "128", OPT_CONV },
  1294. /* Various NUMA process/thread layout bandwidth measurements: */
  1295. { " 2x1-bw-process,", "mem", "-p", "2", "-t", "1", "-P", "1024", OPT_BW },
  1296. { " 3x1-bw-process,", "mem", "-p", "3", "-t", "1", "-P", "1024", OPT_BW },
  1297. { " 4x1-bw-process,", "mem", "-p", "4", "-t", "1", "-P", "1024", OPT_BW },
  1298. { " 8x1-bw-process,", "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW },
  1299. { " 8x1-bw-process-NOTHP,",
  1300. "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW_NOTHP },
  1301. { "16x1-bw-process,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_BW },
  1302. { " 4x1-bw-thread,", "mem", "-p", "1", "-t", "4", "-T", "256", OPT_BW },
  1303. { " 8x1-bw-thread,", "mem", "-p", "1", "-t", "8", "-T", "256", OPT_BW },
  1304. { "16x1-bw-thread,", "mem", "-p", "1", "-t", "16", "-T", "128", OPT_BW },
  1305. { "32x1-bw-thread,", "mem", "-p", "1", "-t", "32", "-T", "64", OPT_BW },
  1306. { " 2x3-bw-thread,", "mem", "-p", "2", "-t", "3", "-P", "512", OPT_BW },
  1307. { " 4x4-bw-thread,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_BW },
  1308. { " 4x6-bw-thread,", "mem", "-p", "4", "-t", "6", "-P", "512", OPT_BW },
  1309. { " 4x8-bw-thread,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW },
  1310. { " 4x8-bw-thread-NOTHP,",
  1311. "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW_NOTHP },
  1312. { " 3x3-bw-thread,", "mem", "-p", "3", "-t", "3", "-P", "512", OPT_BW },
  1313. { " 5x5-bw-thread,", "mem", "-p", "5", "-t", "5", "-P", "512", OPT_BW },
  1314. { "2x16-bw-thread,", "mem", "-p", "2", "-t", "16", "-P", "512", OPT_BW },
  1315. { "1x32-bw-thread,", "mem", "-p", "1", "-t", "32", "-P", "2048", OPT_BW },
  1316. { "numa02-bw,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW },
  1317. { "numa02-bw-NOTHP,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW_NOTHP },
  1318. { "numa01-bw-thread,", "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW },
  1319. { "numa01-bw-thread-NOTHP,",
  1320. "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW_NOTHP },
  1321. };
  1322. static int bench_all(void)
  1323. {
  1324. int nr = ARRAY_SIZE(tests);
  1325. int ret;
  1326. int i;
  1327. ret = system("echo ' #'; echo ' # Running test on: '$(uname -a); echo ' #'");
  1328. BUG_ON(ret < 0);
  1329. for (i = 0; i < nr; i++) {
  1330. run_bench_numa(tests[i][0], tests[i] + 1);
  1331. }
  1332. printf("\n");
  1333. return 0;
  1334. }
  1335. int bench_numa(int argc, const char **argv, const char *prefix __maybe_unused)
  1336. {
  1337. init_params(&p0, "main,", argc, argv);
  1338. argc = parse_options(argc, argv, options, bench_numa_usage, 0);
  1339. if (argc)
  1340. goto err;
  1341. if (p0.run_all)
  1342. return bench_all();
  1343. if (__bench_numa(NULL))
  1344. goto err;
  1345. return 0;
  1346. err:
  1347. usage_with_options(numa_usage, options);
  1348. return -1;
  1349. }