numa.c 43 KB

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