numa.c 44 KB

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