numa.c 42 KB

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