numa.c 46 KB

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