numa.c 41 KB

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