numa.c 43 KB

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