numa.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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, secs;
  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. secs = td->runtime_ns / NSEC_PER_SEC;
  992. td->speed_gbs = secs ? bytes_done / secs / 1e9 : 0;
  993. getrusage(RUSAGE_THREAD, &rusage);
  994. td->system_time_ns = rusage.ru_stime.tv_sec * NSEC_PER_SEC;
  995. td->system_time_ns += rusage.ru_stime.tv_usec * NSEC_PER_USEC;
  996. td->user_time_ns = rusage.ru_utime.tv_sec * NSEC_PER_SEC;
  997. td->user_time_ns += rusage.ru_utime.tv_usec * NSEC_PER_USEC;
  998. free_data(thread_data, g->p.bytes_thread);
  999. pthread_mutex_lock(&g->stop_work_mutex);
  1000. g->bytes_done += bytes_done;
  1001. pthread_mutex_unlock(&g->stop_work_mutex);
  1002. return NULL;
  1003. }
  1004. /*
  1005. * A worker process starts a couple of threads:
  1006. */
  1007. static void worker_process(int process_nr)
  1008. {
  1009. pthread_mutex_t process_lock;
  1010. struct thread_data *td;
  1011. pthread_t *pthreads;
  1012. u8 *process_data;
  1013. int task_nr;
  1014. int ret;
  1015. int t;
  1016. pthread_mutex_init(&process_lock, NULL);
  1017. set_taskname("process %d", process_nr);
  1018. /*
  1019. * Pick up the memory policy and the CPU binding of our first thread,
  1020. * so that we initialize memory accordingly:
  1021. */
  1022. task_nr = process_nr*g->p.nr_threads;
  1023. td = g->threads + task_nr;
  1024. bind_to_memnode(td->bind_node);
  1025. bind_to_cpumask(td->bind_cpumask);
  1026. pthreads = zalloc(g->p.nr_threads * sizeof(pthread_t));
  1027. process_data = setup_private_data(g->p.bytes_process);
  1028. if (g->p.show_details >= 3) {
  1029. printf(" # process %2d global mem: %p, process mem: %p\n",
  1030. process_nr, g->data, process_data);
  1031. }
  1032. for (t = 0; t < g->p.nr_threads; t++) {
  1033. task_nr = process_nr*g->p.nr_threads + t;
  1034. td = g->threads + task_nr;
  1035. td->process_data = process_data;
  1036. td->process_nr = process_nr;
  1037. td->thread_nr = t;
  1038. td->task_nr = task_nr;
  1039. td->val = rand();
  1040. td->curr_cpu = -1;
  1041. td->process_lock = &process_lock;
  1042. ret = pthread_create(pthreads + t, NULL, worker_thread, td);
  1043. BUG_ON(ret);
  1044. }
  1045. for (t = 0; t < g->p.nr_threads; t++) {
  1046. ret = pthread_join(pthreads[t], NULL);
  1047. BUG_ON(ret);
  1048. }
  1049. free_data(process_data, g->p.bytes_process);
  1050. free(pthreads);
  1051. }
  1052. static void print_summary(void)
  1053. {
  1054. if (g->p.show_details < 0)
  1055. return;
  1056. printf("\n ###\n");
  1057. printf(" # %d %s will execute (on %d nodes, %d CPUs):\n",
  1058. g->p.nr_tasks, g->p.nr_tasks == 1 ? "task" : "tasks", nr_numa_nodes(), g->p.nr_cpus);
  1059. printf(" # %5dx %5ldMB global shared mem operations\n",
  1060. g->p.nr_loops, g->p.bytes_global/1024/1024);
  1061. printf(" # %5dx %5ldMB process shared mem operations\n",
  1062. g->p.nr_loops, g->p.bytes_process/1024/1024);
  1063. printf(" # %5dx %5ldMB thread local mem operations\n",
  1064. g->p.nr_loops, g->p.bytes_thread/1024/1024);
  1065. printf(" ###\n");
  1066. printf("\n ###\n"); fflush(stdout);
  1067. }
  1068. static void init_thread_data(void)
  1069. {
  1070. ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
  1071. int t;
  1072. g->threads = zalloc_shared_data(size);
  1073. for (t = 0; t < g->p.nr_tasks; t++) {
  1074. struct thread_data *td = g->threads + t;
  1075. int cpu;
  1076. /* Allow all nodes by default: */
  1077. td->bind_node = -1;
  1078. /* Allow all CPUs by default: */
  1079. CPU_ZERO(&td->bind_cpumask);
  1080. for (cpu = 0; cpu < g->p.nr_cpus; cpu++)
  1081. CPU_SET(cpu, &td->bind_cpumask);
  1082. }
  1083. }
  1084. static void deinit_thread_data(void)
  1085. {
  1086. ssize_t size = sizeof(*g->threads)*g->p.nr_tasks;
  1087. free_data(g->threads, size);
  1088. }
  1089. static int init(void)
  1090. {
  1091. g = (void *)alloc_data(sizeof(*g), MAP_SHARED, 1, 0, 0 /* THP */, 0);
  1092. /* Copy over options: */
  1093. g->p = p0;
  1094. g->p.nr_cpus = numa_num_configured_cpus();
  1095. g->p.nr_nodes = numa_max_node() + 1;
  1096. /* char array in count_process_nodes(): */
  1097. BUG_ON(g->p.nr_nodes > MAX_NR_NODES || g->p.nr_nodes < 0);
  1098. if (g->p.show_quiet && !g->p.show_details)
  1099. g->p.show_details = -1;
  1100. /* Some memory should be specified: */
  1101. if (!g->p.mb_global_str && !g->p.mb_proc_str && !g->p.mb_thread_str)
  1102. return -1;
  1103. if (g->p.mb_global_str) {
  1104. g->p.mb_global = atof(g->p.mb_global_str);
  1105. BUG_ON(g->p.mb_global < 0);
  1106. }
  1107. if (g->p.mb_proc_str) {
  1108. g->p.mb_proc = atof(g->p.mb_proc_str);
  1109. BUG_ON(g->p.mb_proc < 0);
  1110. }
  1111. if (g->p.mb_proc_locked_str) {
  1112. g->p.mb_proc_locked = atof(g->p.mb_proc_locked_str);
  1113. BUG_ON(g->p.mb_proc_locked < 0);
  1114. BUG_ON(g->p.mb_proc_locked > g->p.mb_proc);
  1115. }
  1116. if (g->p.mb_thread_str) {
  1117. g->p.mb_thread = atof(g->p.mb_thread_str);
  1118. BUG_ON(g->p.mb_thread < 0);
  1119. }
  1120. BUG_ON(g->p.nr_threads <= 0);
  1121. BUG_ON(g->p.nr_proc <= 0);
  1122. g->p.nr_tasks = g->p.nr_proc*g->p.nr_threads;
  1123. g->p.bytes_global = g->p.mb_global *1024L*1024L;
  1124. g->p.bytes_process = g->p.mb_proc *1024L*1024L;
  1125. g->p.bytes_process_locked = g->p.mb_proc_locked *1024L*1024L;
  1126. g->p.bytes_thread = g->p.mb_thread *1024L*1024L;
  1127. g->data = setup_shared_data(g->p.bytes_global);
  1128. /* Startup serialization: */
  1129. init_global_mutex(&g->start_work_mutex);
  1130. init_global_mutex(&g->startup_mutex);
  1131. init_global_mutex(&g->startup_done_mutex);
  1132. init_global_mutex(&g->stop_work_mutex);
  1133. init_thread_data();
  1134. tprintf("#\n");
  1135. if (parse_setup_cpu_list() || parse_setup_node_list())
  1136. return -1;
  1137. tprintf("#\n");
  1138. print_summary();
  1139. return 0;
  1140. }
  1141. static void deinit(void)
  1142. {
  1143. free_data(g->data, g->p.bytes_global);
  1144. g->data = NULL;
  1145. deinit_thread_data();
  1146. free_data(g, sizeof(*g));
  1147. g = NULL;
  1148. }
  1149. /*
  1150. * Print a short or long result, depending on the verbosity setting:
  1151. */
  1152. static void print_res(const char *name, double val,
  1153. const char *txt_unit, const char *txt_short, const char *txt_long)
  1154. {
  1155. if (!name)
  1156. name = "main,";
  1157. if (!g->p.show_quiet)
  1158. printf(" %-30s %15.3f, %-15s %s\n", name, val, txt_unit, txt_short);
  1159. else
  1160. printf(" %14.3f %s\n", val, txt_long);
  1161. }
  1162. static int __bench_numa(const char *name)
  1163. {
  1164. struct timeval start, stop, diff;
  1165. u64 runtime_ns_min, runtime_ns_sum;
  1166. pid_t *pids, pid, wpid;
  1167. double delta_runtime;
  1168. double runtime_avg;
  1169. double runtime_sec_max;
  1170. double runtime_sec_min;
  1171. int wait_stat;
  1172. double bytes;
  1173. int i, t, p;
  1174. if (init())
  1175. return -1;
  1176. pids = zalloc(g->p.nr_proc * sizeof(*pids));
  1177. pid = -1;
  1178. /* All threads try to acquire it, this way we can wait for them to start up: */
  1179. pthread_mutex_lock(&g->start_work_mutex);
  1180. if (g->p.serialize_startup) {
  1181. tprintf(" #\n");
  1182. tprintf(" # Startup synchronization: ..."); fflush(stdout);
  1183. }
  1184. gettimeofday(&start, NULL);
  1185. for (i = 0; i < g->p.nr_proc; i++) {
  1186. pid = fork();
  1187. dprintf(" # process %2d: PID %d\n", i, pid);
  1188. BUG_ON(pid < 0);
  1189. if (!pid) {
  1190. /* Child process: */
  1191. worker_process(i);
  1192. exit(0);
  1193. }
  1194. pids[i] = pid;
  1195. }
  1196. /* Wait for all the threads to start up: */
  1197. while (g->nr_tasks_started != g->p.nr_tasks)
  1198. usleep(USEC_PER_MSEC);
  1199. BUG_ON(g->nr_tasks_started != g->p.nr_tasks);
  1200. if (g->p.serialize_startup) {
  1201. double startup_sec;
  1202. pthread_mutex_lock(&g->startup_done_mutex);
  1203. /* This will start all threads: */
  1204. pthread_mutex_unlock(&g->start_work_mutex);
  1205. /* This mutex is locked - the last started thread will wake us: */
  1206. pthread_mutex_lock(&g->startup_done_mutex);
  1207. gettimeofday(&stop, NULL);
  1208. timersub(&stop, &start, &diff);
  1209. startup_sec = diff.tv_sec * NSEC_PER_SEC;
  1210. startup_sec += diff.tv_usec * NSEC_PER_USEC;
  1211. startup_sec /= NSEC_PER_SEC;
  1212. tprintf(" threads initialized in %.6f seconds.\n", startup_sec);
  1213. tprintf(" #\n");
  1214. start = stop;
  1215. pthread_mutex_unlock(&g->startup_done_mutex);
  1216. } else {
  1217. gettimeofday(&start, NULL);
  1218. }
  1219. /* Parent process: */
  1220. for (i = 0; i < g->p.nr_proc; i++) {
  1221. wpid = waitpid(pids[i], &wait_stat, 0);
  1222. BUG_ON(wpid < 0);
  1223. BUG_ON(!WIFEXITED(wait_stat));
  1224. }
  1225. runtime_ns_sum = 0;
  1226. runtime_ns_min = -1LL;
  1227. for (t = 0; t < g->p.nr_tasks; t++) {
  1228. u64 thread_runtime_ns = g->threads[t].runtime_ns;
  1229. runtime_ns_sum += thread_runtime_ns;
  1230. runtime_ns_min = min(thread_runtime_ns, runtime_ns_min);
  1231. }
  1232. gettimeofday(&stop, NULL);
  1233. timersub(&stop, &start, &diff);
  1234. BUG_ON(bench_format != BENCH_FORMAT_DEFAULT);
  1235. tprintf("\n ###\n");
  1236. tprintf("\n");
  1237. runtime_sec_max = diff.tv_sec * NSEC_PER_SEC;
  1238. runtime_sec_max += diff.tv_usec * NSEC_PER_USEC;
  1239. runtime_sec_max /= NSEC_PER_SEC;
  1240. runtime_sec_min = runtime_ns_min / NSEC_PER_SEC;
  1241. bytes = g->bytes_done;
  1242. runtime_avg = (double)runtime_ns_sum / g->p.nr_tasks / NSEC_PER_SEC;
  1243. if (g->p.measure_convergence) {
  1244. print_res(name, runtime_sec_max,
  1245. "secs,", "NUMA-convergence-latency", "secs latency to NUMA-converge");
  1246. }
  1247. print_res(name, runtime_sec_max,
  1248. "secs,", "runtime-max/thread", "secs slowest (max) thread-runtime");
  1249. print_res(name, runtime_sec_min,
  1250. "secs,", "runtime-min/thread", "secs fastest (min) thread-runtime");
  1251. print_res(name, runtime_avg,
  1252. "secs,", "runtime-avg/thread", "secs average thread-runtime");
  1253. delta_runtime = (runtime_sec_max - runtime_sec_min)/2.0;
  1254. print_res(name, delta_runtime / runtime_sec_max * 100.0,
  1255. "%,", "spread-runtime/thread", "% difference between max/avg runtime");
  1256. print_res(name, bytes / g->p.nr_tasks / 1e9,
  1257. "GB,", "data/thread", "GB data processed, per thread");
  1258. print_res(name, bytes / 1e9,
  1259. "GB,", "data-total", "GB data processed, total");
  1260. print_res(name, runtime_sec_max * NSEC_PER_SEC / (bytes / g->p.nr_tasks),
  1261. "nsecs,", "runtime/byte/thread","nsecs/byte/thread runtime");
  1262. print_res(name, bytes / g->p.nr_tasks / 1e9 / runtime_sec_max,
  1263. "GB/sec,", "thread-speed", "GB/sec/thread speed");
  1264. print_res(name, bytes / runtime_sec_max / 1e9,
  1265. "GB/sec,", "total-speed", "GB/sec total speed");
  1266. if (g->p.show_details >= 2) {
  1267. char tname[14 + 2 * 10 + 1];
  1268. struct thread_data *td;
  1269. for (p = 0; p < g->p.nr_proc; p++) {
  1270. for (t = 0; t < g->p.nr_threads; t++) {
  1271. memset(tname, 0, sizeof(tname));
  1272. td = g->threads + p*g->p.nr_threads + t;
  1273. snprintf(tname, sizeof(tname), "process%d:thread%d", p, t);
  1274. print_res(tname, td->speed_gbs,
  1275. "GB/sec", "thread-speed", "GB/sec/thread speed");
  1276. print_res(tname, td->system_time_ns / NSEC_PER_SEC,
  1277. "secs", "thread-system-time", "system CPU time/thread");
  1278. print_res(tname, td->user_time_ns / NSEC_PER_SEC,
  1279. "secs", "thread-user-time", "user CPU time/thread");
  1280. }
  1281. }
  1282. }
  1283. free(pids);
  1284. deinit();
  1285. return 0;
  1286. }
  1287. #define MAX_ARGS 50
  1288. static int command_size(const char **argv)
  1289. {
  1290. int size = 0;
  1291. while (*argv) {
  1292. size++;
  1293. argv++;
  1294. }
  1295. BUG_ON(size >= MAX_ARGS);
  1296. return size;
  1297. }
  1298. static void init_params(struct params *p, const char *name, int argc, const char **argv)
  1299. {
  1300. int i;
  1301. printf("\n # Running %s \"perf bench numa", name);
  1302. for (i = 0; i < argc; i++)
  1303. printf(" %s", argv[i]);
  1304. printf("\"\n");
  1305. memset(p, 0, sizeof(*p));
  1306. /* Initialize nonzero defaults: */
  1307. p->serialize_startup = 1;
  1308. p->data_reads = true;
  1309. p->data_writes = true;
  1310. p->data_backwards = true;
  1311. p->data_rand_walk = true;
  1312. p->nr_loops = -1;
  1313. p->init_random = true;
  1314. p->mb_global_str = "1";
  1315. p->nr_proc = 1;
  1316. p->nr_threads = 1;
  1317. p->nr_secs = 5;
  1318. p->run_all = argc == 1;
  1319. }
  1320. static int run_bench_numa(const char *name, const char **argv)
  1321. {
  1322. int argc = command_size(argv);
  1323. init_params(&p0, name, argc, argv);
  1324. argc = parse_options(argc, argv, options, bench_numa_usage, 0);
  1325. if (argc)
  1326. goto err;
  1327. if (__bench_numa(name))
  1328. goto err;
  1329. return 0;
  1330. err:
  1331. return -1;
  1332. }
  1333. #define OPT_BW_RAM "-s", "20", "-zZq", "--thp", " 1", "--no-data_rand_walk"
  1334. #define OPT_BW_RAM_NOTHP OPT_BW_RAM, "--thp", "-1"
  1335. #define OPT_CONV "-s", "100", "-zZ0qcm", "--thp", " 1"
  1336. #define OPT_CONV_NOTHP OPT_CONV, "--thp", "-1"
  1337. #define OPT_BW "-s", "20", "-zZ0q", "--thp", " 1"
  1338. #define OPT_BW_NOTHP OPT_BW, "--thp", "-1"
  1339. /*
  1340. * The built-in test-suite executed by "perf bench numa -a".
  1341. *
  1342. * (A minimum of 4 nodes and 16 GB of RAM is recommended.)
  1343. */
  1344. static const char *tests[][MAX_ARGS] = {
  1345. /* Basic single-stream NUMA bandwidth measurements: */
  1346. { "RAM-bw-local,", "mem", "-p", "1", "-t", "1", "-P", "1024",
  1347. "-C" , "0", "-M", "0", OPT_BW_RAM },
  1348. { "RAM-bw-local-NOTHP,",
  1349. "mem", "-p", "1", "-t", "1", "-P", "1024",
  1350. "-C" , "0", "-M", "0", OPT_BW_RAM_NOTHP },
  1351. { "RAM-bw-remote,", "mem", "-p", "1", "-t", "1", "-P", "1024",
  1352. "-C" , "0", "-M", "1", OPT_BW_RAM },
  1353. /* 2-stream NUMA bandwidth measurements: */
  1354. { "RAM-bw-local-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1355. "-C", "0,2", "-M", "0x2", OPT_BW_RAM },
  1356. { "RAM-bw-remote-2x,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1357. "-C", "0,2", "-M", "1x2", OPT_BW_RAM },
  1358. /* Cross-stream NUMA bandwidth measurement: */
  1359. { "RAM-bw-cross,", "mem", "-p", "2", "-t", "1", "-P", "1024",
  1360. "-C", "0,8", "-M", "1,0", OPT_BW_RAM },
  1361. /* Convergence latency measurements: */
  1362. { " 1x3-convergence,", "mem", "-p", "1", "-t", "3", "-P", "512", OPT_CONV },
  1363. { " 1x4-convergence,", "mem", "-p", "1", "-t", "4", "-P", "512", OPT_CONV },
  1364. { " 1x6-convergence,", "mem", "-p", "1", "-t", "6", "-P", "1020", OPT_CONV },
  1365. { " 2x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
  1366. { " 3x3-convergence,", "mem", "-p", "3", "-t", "3", "-P", "1020", OPT_CONV },
  1367. { " 4x4-convergence,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV },
  1368. { " 4x4-convergence-NOTHP,",
  1369. "mem", "-p", "4", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
  1370. { " 4x6-convergence,", "mem", "-p", "4", "-t", "6", "-P", "1020", OPT_CONV },
  1371. { " 4x8-convergence,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_CONV },
  1372. { " 8x4-convergence,", "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV },
  1373. { " 8x4-convergence-NOTHP,",
  1374. "mem", "-p", "8", "-t", "4", "-P", "512", OPT_CONV_NOTHP },
  1375. { " 3x1-convergence,", "mem", "-p", "3", "-t", "1", "-P", "512", OPT_CONV },
  1376. { " 4x1-convergence,", "mem", "-p", "4", "-t", "1", "-P", "512", OPT_CONV },
  1377. { " 8x1-convergence,", "mem", "-p", "8", "-t", "1", "-P", "512", OPT_CONV },
  1378. { "16x1-convergence,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_CONV },
  1379. { "32x1-convergence,", "mem", "-p", "32", "-t", "1", "-P", "128", OPT_CONV },
  1380. /* Various NUMA process/thread layout bandwidth measurements: */
  1381. { " 2x1-bw-process,", "mem", "-p", "2", "-t", "1", "-P", "1024", OPT_BW },
  1382. { " 3x1-bw-process,", "mem", "-p", "3", "-t", "1", "-P", "1024", OPT_BW },
  1383. { " 4x1-bw-process,", "mem", "-p", "4", "-t", "1", "-P", "1024", OPT_BW },
  1384. { " 8x1-bw-process,", "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW },
  1385. { " 8x1-bw-process-NOTHP,",
  1386. "mem", "-p", "8", "-t", "1", "-P", " 512", OPT_BW_NOTHP },
  1387. { "16x1-bw-process,", "mem", "-p", "16", "-t", "1", "-P", "256", OPT_BW },
  1388. { " 4x1-bw-thread,", "mem", "-p", "1", "-t", "4", "-T", "256", OPT_BW },
  1389. { " 8x1-bw-thread,", "mem", "-p", "1", "-t", "8", "-T", "256", OPT_BW },
  1390. { "16x1-bw-thread,", "mem", "-p", "1", "-t", "16", "-T", "128", OPT_BW },
  1391. { "32x1-bw-thread,", "mem", "-p", "1", "-t", "32", "-T", "64", OPT_BW },
  1392. { " 2x3-bw-thread,", "mem", "-p", "2", "-t", "3", "-P", "512", OPT_BW },
  1393. { " 4x4-bw-thread,", "mem", "-p", "4", "-t", "4", "-P", "512", OPT_BW },
  1394. { " 4x6-bw-thread,", "mem", "-p", "4", "-t", "6", "-P", "512", OPT_BW },
  1395. { " 4x8-bw-thread,", "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW },
  1396. { " 4x8-bw-thread-NOTHP,",
  1397. "mem", "-p", "4", "-t", "8", "-P", "512", OPT_BW_NOTHP },
  1398. { " 3x3-bw-thread,", "mem", "-p", "3", "-t", "3", "-P", "512", OPT_BW },
  1399. { " 5x5-bw-thread,", "mem", "-p", "5", "-t", "5", "-P", "512", OPT_BW },
  1400. { "2x16-bw-thread,", "mem", "-p", "2", "-t", "16", "-P", "512", OPT_BW },
  1401. { "1x32-bw-thread,", "mem", "-p", "1", "-t", "32", "-P", "2048", OPT_BW },
  1402. { "numa02-bw,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW },
  1403. { "numa02-bw-NOTHP,", "mem", "-p", "1", "-t", "32", "-T", "32", OPT_BW_NOTHP },
  1404. { "numa01-bw-thread,", "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW },
  1405. { "numa01-bw-thread-NOTHP,",
  1406. "mem", "-p", "2", "-t", "16", "-T", "192", OPT_BW_NOTHP },
  1407. };
  1408. static int bench_all(void)
  1409. {
  1410. int nr = ARRAY_SIZE(tests);
  1411. int ret;
  1412. int i;
  1413. ret = system("echo ' #'; echo ' # Running test on: '$(uname -a); echo ' #'");
  1414. BUG_ON(ret < 0);
  1415. for (i = 0; i < nr; i++) {
  1416. run_bench_numa(tests[i][0], tests[i] + 1);
  1417. }
  1418. printf("\n");
  1419. return 0;
  1420. }
  1421. int bench_numa(int argc, const char **argv)
  1422. {
  1423. init_params(&p0, "main,", argc, argv);
  1424. argc = parse_options(argc, argv, options, bench_numa_usage, 0);
  1425. if (argc)
  1426. goto err;
  1427. if (p0.run_all)
  1428. return bench_all();
  1429. if (__bench_numa(NULL))
  1430. goto err;
  1431. return 0;
  1432. err:
  1433. usage_with_options(numa_usage, options);
  1434. return -1;
  1435. }