numa.c 43 KB

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