builtin-test.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. /*
  2. * builtin-test.c
  3. *
  4. * Builtin regression testing command: ever growing number of sanity tests
  5. */
  6. #include "builtin.h"
  7. #include "util/cache.h"
  8. #include "util/debug.h"
  9. #include "util/debugfs.h"
  10. #include "util/evlist.h"
  11. #include "util/parse-options.h"
  12. #include "util/parse-events.h"
  13. #include "util/symbol.h"
  14. #include "util/thread_map.h"
  15. #include "../../include/linux/hw_breakpoint.h"
  16. static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym)
  17. {
  18. bool *visited = symbol__priv(sym);
  19. *visited = true;
  20. return 0;
  21. }
  22. static int test__vmlinux_matches_kallsyms(void)
  23. {
  24. int err = -1;
  25. struct rb_node *nd;
  26. struct symbol *sym;
  27. struct map *kallsyms_map, *vmlinux_map;
  28. struct machine kallsyms, vmlinux;
  29. enum map_type type = MAP__FUNCTION;
  30. long page_size = sysconf(_SC_PAGE_SIZE);
  31. struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
  32. /*
  33. * Step 1:
  34. *
  35. * Init the machines that will hold kernel, modules obtained from
  36. * both vmlinux + .ko files and from /proc/kallsyms split by modules.
  37. */
  38. machine__init(&kallsyms, "", HOST_KERNEL_ID);
  39. machine__init(&vmlinux, "", HOST_KERNEL_ID);
  40. /*
  41. * Step 2:
  42. *
  43. * Create the kernel maps for kallsyms and the DSO where we will then
  44. * load /proc/kallsyms. Also create the modules maps from /proc/modules
  45. * and find the .ko files that match them in /lib/modules/`uname -r`/.
  46. */
  47. if (machine__create_kernel_maps(&kallsyms) < 0) {
  48. pr_debug("machine__create_kernel_maps ");
  49. return -1;
  50. }
  51. /*
  52. * Step 3:
  53. *
  54. * Load and split /proc/kallsyms into multiple maps, one per module.
  55. */
  56. if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
  57. pr_debug("dso__load_kallsyms ");
  58. goto out;
  59. }
  60. /*
  61. * Step 4:
  62. *
  63. * kallsyms will be internally on demand sorted by name so that we can
  64. * find the reference relocation * symbol, i.e. the symbol we will use
  65. * to see if the running kernel was relocated by checking if it has the
  66. * same value in the vmlinux file we load.
  67. */
  68. kallsyms_map = machine__kernel_map(&kallsyms, type);
  69. sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
  70. if (sym == NULL) {
  71. pr_debug("dso__find_symbol_by_name ");
  72. goto out;
  73. }
  74. ref_reloc_sym.addr = sym->start;
  75. /*
  76. * Step 5:
  77. *
  78. * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
  79. */
  80. if (machine__create_kernel_maps(&vmlinux) < 0) {
  81. pr_debug("machine__create_kernel_maps ");
  82. goto out;
  83. }
  84. vmlinux_map = machine__kernel_map(&vmlinux, type);
  85. map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
  86. /*
  87. * Step 6:
  88. *
  89. * Locate a vmlinux file in the vmlinux path that has a buildid that
  90. * matches the one of the running kernel.
  91. *
  92. * While doing that look if we find the ref reloc symbol, if we find it
  93. * we'll have its ref_reloc_symbol.unrelocated_addr and then
  94. * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
  95. * to fixup the symbols.
  96. */
  97. if (machine__load_vmlinux_path(&vmlinux, type,
  98. vmlinux_matches_kallsyms_filter) <= 0) {
  99. pr_debug("machine__load_vmlinux_path ");
  100. goto out;
  101. }
  102. err = 0;
  103. /*
  104. * Step 7:
  105. *
  106. * Now look at the symbols in the vmlinux DSO and check if we find all of them
  107. * in the kallsyms dso. For the ones that are in both, check its names and
  108. * end addresses too.
  109. */
  110. for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
  111. struct symbol *pair, *first_pair;
  112. bool backwards = true;
  113. sym = rb_entry(nd, struct symbol, rb_node);
  114. if (sym->start == sym->end)
  115. continue;
  116. first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
  117. pair = first_pair;
  118. if (pair && pair->start == sym->start) {
  119. next_pair:
  120. if (strcmp(sym->name, pair->name) == 0) {
  121. /*
  122. * kallsyms don't have the symbol end, so we
  123. * set that by using the next symbol start - 1,
  124. * in some cases we get this up to a page
  125. * wrong, trace_kmalloc when I was developing
  126. * this code was one such example, 2106 bytes
  127. * off the real size. More than that and we
  128. * _really_ have a problem.
  129. */
  130. s64 skew = sym->end - pair->end;
  131. if (llabs(skew) < page_size)
  132. continue;
  133. pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
  134. sym->start, sym->name, sym->end, pair->end);
  135. } else {
  136. struct rb_node *nnd;
  137. detour:
  138. nnd = backwards ? rb_prev(&pair->rb_node) :
  139. rb_next(&pair->rb_node);
  140. if (nnd) {
  141. struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
  142. if (next->start == sym->start) {
  143. pair = next;
  144. goto next_pair;
  145. }
  146. }
  147. if (backwards) {
  148. backwards = false;
  149. pair = first_pair;
  150. goto detour;
  151. }
  152. pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
  153. sym->start, sym->name, pair->name);
  154. }
  155. } else
  156. pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
  157. err = -1;
  158. }
  159. if (!verbose)
  160. goto out;
  161. pr_info("Maps only in vmlinux:\n");
  162. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  163. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  164. /*
  165. * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
  166. * the kernel will have the path for the vmlinux file being used,
  167. * so use the short name, less descriptive but the same ("[kernel]" in
  168. * both cases.
  169. */
  170. pair = map_groups__find_by_name(&kallsyms.kmaps, type,
  171. (pos->dso->kernel ?
  172. pos->dso->short_name :
  173. pos->dso->name));
  174. if (pair)
  175. pair->priv = 1;
  176. else
  177. map__fprintf(pos, stderr);
  178. }
  179. pr_info("Maps in vmlinux with a different name in kallsyms:\n");
  180. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  181. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  182. pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
  183. if (pair == NULL || pair->priv)
  184. continue;
  185. if (pair->start == pos->start) {
  186. pair->priv = 1;
  187. pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
  188. pos->start, pos->end, pos->pgoff, pos->dso->name);
  189. if (pos->pgoff != pair->pgoff || pos->end != pair->end)
  190. pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
  191. pair->start, pair->end, pair->pgoff);
  192. pr_info(" %s\n", pair->dso->name);
  193. pair->priv = 1;
  194. }
  195. }
  196. pr_info("Maps only in kallsyms:\n");
  197. for (nd = rb_first(&kallsyms.kmaps.maps[type]);
  198. nd; nd = rb_next(nd)) {
  199. struct map *pos = rb_entry(nd, struct map, rb_node);
  200. if (!pos->priv)
  201. map__fprintf(pos, stderr);
  202. }
  203. out:
  204. return err;
  205. }
  206. #include "util/cpumap.h"
  207. #include "util/evsel.h"
  208. #include <sys/types.h>
  209. static int trace_event__id(const char *evname)
  210. {
  211. char *filename;
  212. int err = -1, fd;
  213. if (asprintf(&filename,
  214. "%s/syscalls/%s/id",
  215. tracing_events_path, evname) < 0)
  216. return -1;
  217. fd = open(filename, O_RDONLY);
  218. if (fd >= 0) {
  219. char id[16];
  220. if (read(fd, id, sizeof(id)) > 0)
  221. err = atoi(id);
  222. close(fd);
  223. }
  224. free(filename);
  225. return err;
  226. }
  227. static int test__open_syscall_event(void)
  228. {
  229. int err = -1, fd;
  230. struct thread_map *threads;
  231. struct perf_evsel *evsel;
  232. struct perf_event_attr attr;
  233. unsigned int nr_open_calls = 111, i;
  234. int id = trace_event__id("sys_enter_open");
  235. if (id < 0) {
  236. pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
  237. return -1;
  238. }
  239. threads = thread_map__new(-1, getpid());
  240. if (threads == NULL) {
  241. pr_debug("thread_map__new\n");
  242. return -1;
  243. }
  244. memset(&attr, 0, sizeof(attr));
  245. attr.type = PERF_TYPE_TRACEPOINT;
  246. attr.config = id;
  247. evsel = perf_evsel__new(&attr, 0);
  248. if (evsel == NULL) {
  249. pr_debug("perf_evsel__new\n");
  250. goto out_thread_map_delete;
  251. }
  252. if (perf_evsel__open_per_thread(evsel, threads, false, NULL) < 0) {
  253. pr_debug("failed to open counter: %s, "
  254. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  255. strerror(errno));
  256. goto out_evsel_delete;
  257. }
  258. for (i = 0; i < nr_open_calls; ++i) {
  259. fd = open("/etc/passwd", O_RDONLY);
  260. close(fd);
  261. }
  262. if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
  263. pr_debug("perf_evsel__read_on_cpu\n");
  264. goto out_close_fd;
  265. }
  266. if (evsel->counts->cpu[0].val != nr_open_calls) {
  267. pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
  268. nr_open_calls, evsel->counts->cpu[0].val);
  269. goto out_close_fd;
  270. }
  271. err = 0;
  272. out_close_fd:
  273. perf_evsel__close_fd(evsel, 1, threads->nr);
  274. out_evsel_delete:
  275. perf_evsel__delete(evsel);
  276. out_thread_map_delete:
  277. thread_map__delete(threads);
  278. return err;
  279. }
  280. #include <sched.h>
  281. static int test__open_syscall_event_on_all_cpus(void)
  282. {
  283. int err = -1, fd, cpu;
  284. struct thread_map *threads;
  285. struct cpu_map *cpus;
  286. struct perf_evsel *evsel;
  287. struct perf_event_attr attr;
  288. unsigned int nr_open_calls = 111, i;
  289. cpu_set_t cpu_set;
  290. int id = trace_event__id("sys_enter_open");
  291. if (id < 0) {
  292. pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
  293. return -1;
  294. }
  295. threads = thread_map__new(-1, getpid());
  296. if (threads == NULL) {
  297. pr_debug("thread_map__new\n");
  298. return -1;
  299. }
  300. cpus = cpu_map__new(NULL);
  301. if (cpus == NULL) {
  302. pr_debug("cpu_map__new\n");
  303. goto out_thread_map_delete;
  304. }
  305. CPU_ZERO(&cpu_set);
  306. memset(&attr, 0, sizeof(attr));
  307. attr.type = PERF_TYPE_TRACEPOINT;
  308. attr.config = id;
  309. evsel = perf_evsel__new(&attr, 0);
  310. if (evsel == NULL) {
  311. pr_debug("perf_evsel__new\n");
  312. goto out_thread_map_delete;
  313. }
  314. if (perf_evsel__open(evsel, cpus, threads, false, NULL) < 0) {
  315. pr_debug("failed to open counter: %s, "
  316. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  317. strerror(errno));
  318. goto out_evsel_delete;
  319. }
  320. for (cpu = 0; cpu < cpus->nr; ++cpu) {
  321. unsigned int ncalls = nr_open_calls + cpu;
  322. /*
  323. * XXX eventually lift this restriction in a way that
  324. * keeps perf building on older glibc installations
  325. * without CPU_ALLOC. 1024 cpus in 2010 still seems
  326. * a reasonable upper limit tho :-)
  327. */
  328. if (cpus->map[cpu] >= CPU_SETSIZE) {
  329. pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
  330. continue;
  331. }
  332. CPU_SET(cpus->map[cpu], &cpu_set);
  333. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  334. pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  335. cpus->map[cpu],
  336. strerror(errno));
  337. goto out_close_fd;
  338. }
  339. for (i = 0; i < ncalls; ++i) {
  340. fd = open("/etc/passwd", O_RDONLY);
  341. close(fd);
  342. }
  343. CPU_CLR(cpus->map[cpu], &cpu_set);
  344. }
  345. /*
  346. * Here we need to explicitely preallocate the counts, as if
  347. * we use the auto allocation it will allocate just for 1 cpu,
  348. * as we start by cpu 0.
  349. */
  350. if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
  351. pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
  352. goto out_close_fd;
  353. }
  354. err = 0;
  355. for (cpu = 0; cpu < cpus->nr; ++cpu) {
  356. unsigned int expected;
  357. if (cpus->map[cpu] >= CPU_SETSIZE)
  358. continue;
  359. if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
  360. pr_debug("perf_evsel__read_on_cpu\n");
  361. err = -1;
  362. break;
  363. }
  364. expected = nr_open_calls + cpu;
  365. if (evsel->counts->cpu[cpu].val != expected) {
  366. pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
  367. expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
  368. err = -1;
  369. }
  370. }
  371. out_close_fd:
  372. perf_evsel__close_fd(evsel, 1, threads->nr);
  373. out_evsel_delete:
  374. perf_evsel__delete(evsel);
  375. out_thread_map_delete:
  376. thread_map__delete(threads);
  377. return err;
  378. }
  379. /*
  380. * This test will generate random numbers of calls to some getpid syscalls,
  381. * then establish an mmap for a group of events that are created to monitor
  382. * the syscalls.
  383. *
  384. * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
  385. * sample.id field to map back to its respective perf_evsel instance.
  386. *
  387. * Then it checks if the number of syscalls reported as perf events by
  388. * the kernel corresponds to the number of syscalls made.
  389. */
  390. static int test__basic_mmap(void)
  391. {
  392. int err = -1;
  393. union perf_event *event;
  394. struct thread_map *threads;
  395. struct cpu_map *cpus;
  396. struct perf_evlist *evlist;
  397. struct perf_event_attr attr = {
  398. .type = PERF_TYPE_TRACEPOINT,
  399. .read_format = PERF_FORMAT_ID,
  400. .sample_type = PERF_SAMPLE_ID,
  401. .watermark = 0,
  402. };
  403. cpu_set_t cpu_set;
  404. const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
  405. "getpgid", };
  406. pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
  407. (void*)getpgid };
  408. #define nsyscalls ARRAY_SIZE(syscall_names)
  409. int ids[nsyscalls];
  410. unsigned int nr_events[nsyscalls],
  411. expected_nr_events[nsyscalls], i, j;
  412. struct perf_evsel *evsels[nsyscalls], *evsel;
  413. int sample_size = __perf_evsel__sample_size(attr.sample_type);
  414. for (i = 0; i < nsyscalls; ++i) {
  415. char name[64];
  416. snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
  417. ids[i] = trace_event__id(name);
  418. if (ids[i] < 0) {
  419. pr_debug("Is debugfs mounted on /sys/kernel/debug?\n");
  420. return -1;
  421. }
  422. nr_events[i] = 0;
  423. expected_nr_events[i] = random() % 257;
  424. }
  425. threads = thread_map__new(-1, getpid());
  426. if (threads == NULL) {
  427. pr_debug("thread_map__new\n");
  428. return -1;
  429. }
  430. cpus = cpu_map__new(NULL);
  431. if (cpus == NULL) {
  432. pr_debug("cpu_map__new\n");
  433. goto out_free_threads;
  434. }
  435. CPU_ZERO(&cpu_set);
  436. CPU_SET(cpus->map[0], &cpu_set);
  437. sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
  438. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  439. pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  440. cpus->map[0], strerror(errno));
  441. goto out_free_cpus;
  442. }
  443. evlist = perf_evlist__new(cpus, threads);
  444. if (evlist == NULL) {
  445. pr_debug("perf_evlist__new\n");
  446. goto out_free_cpus;
  447. }
  448. /* anonymous union fields, can't be initialized above */
  449. attr.wakeup_events = 1;
  450. attr.sample_period = 1;
  451. for (i = 0; i < nsyscalls; ++i) {
  452. attr.config = ids[i];
  453. evsels[i] = perf_evsel__new(&attr, i);
  454. if (evsels[i] == NULL) {
  455. pr_debug("perf_evsel__new\n");
  456. goto out_free_evlist;
  457. }
  458. perf_evlist__add(evlist, evsels[i]);
  459. if (perf_evsel__open(evsels[i], cpus, threads, false, NULL) < 0) {
  460. pr_debug("failed to open counter: %s, "
  461. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  462. strerror(errno));
  463. goto out_close_fd;
  464. }
  465. }
  466. if (perf_evlist__mmap(evlist, 128, true) < 0) {
  467. pr_debug("failed to mmap events: %d (%s)\n", errno,
  468. strerror(errno));
  469. goto out_close_fd;
  470. }
  471. for (i = 0; i < nsyscalls; ++i)
  472. for (j = 0; j < expected_nr_events[i]; ++j) {
  473. int foo = syscalls[i]();
  474. ++foo;
  475. }
  476. while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
  477. struct perf_sample sample;
  478. if (event->header.type != PERF_RECORD_SAMPLE) {
  479. pr_debug("unexpected %s event\n",
  480. perf_event__name(event->header.type));
  481. goto out_munmap;
  482. }
  483. err = perf_event__parse_sample(event, attr.sample_type, sample_size,
  484. false, &sample, false);
  485. if (err) {
  486. pr_err("Can't parse sample, err = %d\n", err);
  487. goto out_munmap;
  488. }
  489. evsel = perf_evlist__id2evsel(evlist, sample.id);
  490. if (evsel == NULL) {
  491. pr_debug("event with id %" PRIu64
  492. " doesn't map to an evsel\n", sample.id);
  493. goto out_munmap;
  494. }
  495. nr_events[evsel->idx]++;
  496. }
  497. list_for_each_entry(evsel, &evlist->entries, node) {
  498. if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
  499. pr_debug("expected %d %s events, got %d\n",
  500. expected_nr_events[evsel->idx],
  501. event_name(evsel), nr_events[evsel->idx]);
  502. goto out_munmap;
  503. }
  504. }
  505. err = 0;
  506. out_munmap:
  507. perf_evlist__munmap(evlist);
  508. out_close_fd:
  509. for (i = 0; i < nsyscalls; ++i)
  510. perf_evsel__close_fd(evsels[i], 1, threads->nr);
  511. out_free_evlist:
  512. perf_evlist__delete(evlist);
  513. out_free_cpus:
  514. cpu_map__delete(cpus);
  515. out_free_threads:
  516. thread_map__delete(threads);
  517. return err;
  518. #undef nsyscalls
  519. }
  520. #define TEST_ASSERT_VAL(text, cond) \
  521. do { \
  522. if (!(cond)) { \
  523. pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
  524. return -1; \
  525. } \
  526. } while (0)
  527. static int test__checkevent_tracepoint(struct perf_evlist *evlist)
  528. {
  529. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  530. struct perf_evsel, node);
  531. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  532. TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
  533. TEST_ASSERT_VAL("wrong sample_type",
  534. (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
  535. evsel->attr.sample_type);
  536. TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
  537. return 0;
  538. }
  539. static int test__checkevent_tracepoint_multi(struct perf_evlist *evlist)
  540. {
  541. struct perf_evsel *evsel;
  542. TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
  543. list_for_each_entry(evsel, &evlist->entries, node) {
  544. TEST_ASSERT_VAL("wrong type",
  545. PERF_TYPE_TRACEPOINT == evsel->attr.type);
  546. TEST_ASSERT_VAL("wrong sample_type",
  547. (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU)
  548. == evsel->attr.sample_type);
  549. TEST_ASSERT_VAL("wrong sample_period",
  550. 1 == evsel->attr.sample_period);
  551. }
  552. return 0;
  553. }
  554. static int test__checkevent_raw(struct perf_evlist *evlist)
  555. {
  556. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  557. struct perf_evsel, node);
  558. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  559. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  560. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  561. return 0;
  562. }
  563. static int test__checkevent_numeric(struct perf_evlist *evlist)
  564. {
  565. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  566. struct perf_evsel, node);
  567. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  568. TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
  569. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  570. return 0;
  571. }
  572. static int test__checkevent_symbolic_name(struct perf_evlist *evlist)
  573. {
  574. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  575. struct perf_evsel, node);
  576. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  577. TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
  578. TEST_ASSERT_VAL("wrong config",
  579. PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
  580. return 0;
  581. }
  582. static int test__checkevent_symbolic_alias(struct perf_evlist *evlist)
  583. {
  584. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  585. struct perf_evsel, node);
  586. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  587. TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->attr.type);
  588. TEST_ASSERT_VAL("wrong config",
  589. PERF_COUNT_SW_PAGE_FAULTS == evsel->attr.config);
  590. return 0;
  591. }
  592. static int test__checkevent_genhw(struct perf_evlist *evlist)
  593. {
  594. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  595. struct perf_evsel, node);
  596. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  597. TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type);
  598. TEST_ASSERT_VAL("wrong config", (1 << 16) == evsel->attr.config);
  599. return 0;
  600. }
  601. static int test__checkevent_breakpoint(struct perf_evlist *evlist)
  602. {
  603. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  604. struct perf_evsel, node);
  605. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  606. TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
  607. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  608. TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
  609. evsel->attr.bp_type);
  610. TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
  611. evsel->attr.bp_len);
  612. return 0;
  613. }
  614. static int test__checkevent_breakpoint_x(struct perf_evlist *evlist)
  615. {
  616. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  617. struct perf_evsel, node);
  618. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  619. TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
  620. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  621. TEST_ASSERT_VAL("wrong bp_type",
  622. HW_BREAKPOINT_X == evsel->attr.bp_type);
  623. TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->attr.bp_len);
  624. return 0;
  625. }
  626. static int test__checkevent_breakpoint_r(struct perf_evlist *evlist)
  627. {
  628. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  629. struct perf_evsel, node);
  630. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  631. TEST_ASSERT_VAL("wrong type",
  632. PERF_TYPE_BREAKPOINT == evsel->attr.type);
  633. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  634. TEST_ASSERT_VAL("wrong bp_type",
  635. HW_BREAKPOINT_R == evsel->attr.bp_type);
  636. TEST_ASSERT_VAL("wrong bp_len",
  637. HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
  638. return 0;
  639. }
  640. static int test__checkevent_breakpoint_w(struct perf_evlist *evlist)
  641. {
  642. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  643. struct perf_evsel, node);
  644. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  645. TEST_ASSERT_VAL("wrong type",
  646. PERF_TYPE_BREAKPOINT == evsel->attr.type);
  647. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  648. TEST_ASSERT_VAL("wrong bp_type",
  649. HW_BREAKPOINT_W == evsel->attr.bp_type);
  650. TEST_ASSERT_VAL("wrong bp_len",
  651. HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
  652. return 0;
  653. }
  654. static int test__checkevent_tracepoint_modifier(struct perf_evlist *evlist)
  655. {
  656. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  657. struct perf_evsel, node);
  658. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  659. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  660. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  661. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  662. return test__checkevent_tracepoint(evlist);
  663. }
  664. static int
  665. test__checkevent_tracepoint_multi_modifier(struct perf_evlist *evlist)
  666. {
  667. struct perf_evsel *evsel;
  668. TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
  669. list_for_each_entry(evsel, &evlist->entries, node) {
  670. TEST_ASSERT_VAL("wrong exclude_user",
  671. !evsel->attr.exclude_user);
  672. TEST_ASSERT_VAL("wrong exclude_kernel",
  673. evsel->attr.exclude_kernel);
  674. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  675. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  676. }
  677. return test__checkevent_tracepoint_multi(evlist);
  678. }
  679. static int test__checkevent_raw_modifier(struct perf_evlist *evlist)
  680. {
  681. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  682. struct perf_evsel, node);
  683. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  684. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  685. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  686. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  687. return test__checkevent_raw(evlist);
  688. }
  689. static int test__checkevent_numeric_modifier(struct perf_evlist *evlist)
  690. {
  691. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  692. struct perf_evsel, node);
  693. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  694. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  695. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  696. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  697. return test__checkevent_numeric(evlist);
  698. }
  699. static int test__checkevent_symbolic_name_modifier(struct perf_evlist *evlist)
  700. {
  701. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  702. struct perf_evsel, node);
  703. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  704. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  705. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  706. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  707. return test__checkevent_symbolic_name(evlist);
  708. }
  709. static int test__checkevent_symbolic_alias_modifier(struct perf_evlist *evlist)
  710. {
  711. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  712. struct perf_evsel, node);
  713. TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
  714. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  715. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  716. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  717. return test__checkevent_symbolic_alias(evlist);
  718. }
  719. static int test__checkevent_genhw_modifier(struct perf_evlist *evlist)
  720. {
  721. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  722. struct perf_evsel, node);
  723. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  724. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  725. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  726. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  727. return test__checkevent_genhw(evlist);
  728. }
  729. static struct test__event_st {
  730. const char *name;
  731. __u32 type;
  732. int (*check)(struct perf_evlist *evlist);
  733. } test__events[] = {
  734. {
  735. .name = "syscalls:sys_enter_open",
  736. .check = test__checkevent_tracepoint,
  737. },
  738. {
  739. .name = "syscalls:*",
  740. .check = test__checkevent_tracepoint_multi,
  741. },
  742. {
  743. .name = "r1",
  744. .check = test__checkevent_raw,
  745. },
  746. {
  747. .name = "1:1",
  748. .check = test__checkevent_numeric,
  749. },
  750. {
  751. .name = "instructions",
  752. .check = test__checkevent_symbolic_name,
  753. },
  754. {
  755. .name = "faults",
  756. .check = test__checkevent_symbolic_alias,
  757. },
  758. {
  759. .name = "L1-dcache-load-miss",
  760. .check = test__checkevent_genhw,
  761. },
  762. {
  763. .name = "mem:0",
  764. .check = test__checkevent_breakpoint,
  765. },
  766. {
  767. .name = "mem:0:x",
  768. .check = test__checkevent_breakpoint_x,
  769. },
  770. {
  771. .name = "mem:0:r",
  772. .check = test__checkevent_breakpoint_r,
  773. },
  774. {
  775. .name = "mem:0:w",
  776. .check = test__checkevent_breakpoint_w,
  777. },
  778. {
  779. .name = "syscalls:sys_enter_open:k",
  780. .check = test__checkevent_tracepoint_modifier,
  781. },
  782. {
  783. .name = "syscalls:*:u",
  784. .check = test__checkevent_tracepoint_multi_modifier,
  785. },
  786. {
  787. .name = "r1:kp",
  788. .check = test__checkevent_raw_modifier,
  789. },
  790. {
  791. .name = "1:1:hp",
  792. .check = test__checkevent_numeric_modifier,
  793. },
  794. {
  795. .name = "instructions:h",
  796. .check = test__checkevent_symbolic_name_modifier,
  797. },
  798. {
  799. .name = "faults:u",
  800. .check = test__checkevent_symbolic_alias_modifier,
  801. },
  802. {
  803. .name = "L1-dcache-load-miss:kp",
  804. .check = test__checkevent_genhw_modifier,
  805. },
  806. };
  807. #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
  808. static int test__parse_events(void)
  809. {
  810. struct perf_evlist *evlist;
  811. u_int i;
  812. int ret = 0;
  813. for (i = 0; i < TEST__EVENTS_CNT; i++) {
  814. struct test__event_st *e = &test__events[i];
  815. evlist = perf_evlist__new(NULL, NULL);
  816. if (evlist == NULL)
  817. break;
  818. ret = parse_events(evlist, e->name, 0);
  819. if (ret) {
  820. pr_debug("failed to parse event '%s', err %d\n",
  821. e->name, ret);
  822. break;
  823. }
  824. ret = e->check(evlist);
  825. if (ret)
  826. break;
  827. perf_evlist__delete(evlist);
  828. }
  829. return ret;
  830. }
  831. static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
  832. size_t *sizep)
  833. {
  834. cpu_set_t *mask;
  835. size_t size;
  836. int i, cpu = -1, nrcpus = 1024;
  837. realloc:
  838. mask = CPU_ALLOC(nrcpus);
  839. size = CPU_ALLOC_SIZE(nrcpus);
  840. CPU_ZERO_S(size, mask);
  841. if (sched_getaffinity(pid, size, mask) == -1) {
  842. CPU_FREE(mask);
  843. if (errno == EINVAL && nrcpus < (1024 << 8)) {
  844. nrcpus = nrcpus << 2;
  845. goto realloc;
  846. }
  847. perror("sched_getaffinity");
  848. return -1;
  849. }
  850. for (i = 0; i < nrcpus; i++) {
  851. if (CPU_ISSET_S(i, size, mask)) {
  852. if (cpu == -1) {
  853. cpu = i;
  854. *maskp = mask;
  855. *sizep = size;
  856. } else
  857. CPU_CLR_S(i, size, mask);
  858. }
  859. }
  860. if (cpu == -1)
  861. CPU_FREE(mask);
  862. return cpu;
  863. }
  864. static int test__PERF_RECORD(void)
  865. {
  866. struct perf_record_opts opts = {
  867. .target_pid = -1,
  868. .target_tid = -1,
  869. .no_delay = true,
  870. .freq = 10,
  871. .mmap_pages = 256,
  872. .sample_id_all_avail = true,
  873. };
  874. cpu_set_t *cpu_mask = NULL;
  875. size_t cpu_mask_size = 0;
  876. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  877. struct perf_evsel *evsel;
  878. struct perf_sample sample;
  879. const char *cmd = "sleep";
  880. const char *argv[] = { cmd, "1", NULL, };
  881. char *bname;
  882. u64 sample_type, prev_time = 0;
  883. bool found_cmd_mmap = false,
  884. found_libc_mmap = false,
  885. found_vdso_mmap = false,
  886. found_ld_mmap = false;
  887. int err = -1, errs = 0, i, wakeups = 0, sample_size;
  888. u32 cpu;
  889. int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
  890. if (evlist == NULL || argv == NULL) {
  891. pr_debug("Not enough memory to create evlist\n");
  892. goto out;
  893. }
  894. /*
  895. * We need at least one evsel in the evlist, use the default
  896. * one: "cycles".
  897. */
  898. err = perf_evlist__add_default(evlist);
  899. if (err < 0) {
  900. pr_debug("Not enough memory to create evsel\n");
  901. goto out_delete_evlist;
  902. }
  903. /*
  904. * Create maps of threads and cpus to monitor. In this case
  905. * we start with all threads and cpus (-1, -1) but then in
  906. * perf_evlist__prepare_workload we'll fill in the only thread
  907. * we're monitoring, the one forked there.
  908. */
  909. err = perf_evlist__create_maps(evlist, opts.target_pid,
  910. opts.target_tid, opts.cpu_list);
  911. if (err < 0) {
  912. pr_debug("Not enough memory to create thread/cpu maps\n");
  913. goto out_delete_evlist;
  914. }
  915. /*
  916. * Prepare the workload in argv[] to run, it'll fork it, and then wait
  917. * for perf_evlist__start_workload() to exec it. This is done this way
  918. * so that we have time to open the evlist (calling sys_perf_event_open
  919. * on all the fds) and then mmap them.
  920. */
  921. err = perf_evlist__prepare_workload(evlist, &opts, argv);
  922. if (err < 0) {
  923. pr_debug("Couldn't run the workload!\n");
  924. goto out_delete_evlist;
  925. }
  926. /*
  927. * Config the evsels, setting attr->comm on the first one, etc.
  928. */
  929. evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  930. evsel->attr.sample_type |= PERF_SAMPLE_CPU;
  931. evsel->attr.sample_type |= PERF_SAMPLE_TID;
  932. evsel->attr.sample_type |= PERF_SAMPLE_TIME;
  933. perf_evlist__config_attrs(evlist, &opts);
  934. err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask,
  935. &cpu_mask_size);
  936. if (err < 0) {
  937. pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
  938. goto out_delete_evlist;
  939. }
  940. cpu = err;
  941. /*
  942. * So that we can check perf_sample.cpu on all the samples.
  943. */
  944. if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
  945. pr_debug("sched_setaffinity: %s\n", strerror(errno));
  946. goto out_free_cpu_mask;
  947. }
  948. /*
  949. * Call sys_perf_event_open on all the fds on all the evsels,
  950. * grouping them if asked to.
  951. */
  952. err = perf_evlist__open(evlist, opts.group);
  953. if (err < 0) {
  954. pr_debug("perf_evlist__open: %s\n", strerror(errno));
  955. goto out_delete_evlist;
  956. }
  957. /*
  958. * mmap the first fd on a given CPU and ask for events for the other
  959. * fds in the same CPU to be injected in the same mmap ring buffer
  960. * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
  961. */
  962. err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
  963. if (err < 0) {
  964. pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
  965. goto out_delete_evlist;
  966. }
  967. /*
  968. * We'll need these two to parse the PERF_SAMPLE_* fields in each
  969. * event.
  970. */
  971. sample_type = perf_evlist__sample_type(evlist);
  972. sample_size = __perf_evsel__sample_size(sample_type);
  973. /*
  974. * Now that all is properly set up, enable the events, they will
  975. * count just on workload.pid, which will start...
  976. */
  977. perf_evlist__enable(evlist);
  978. /*
  979. * Now!
  980. */
  981. perf_evlist__start_workload(evlist);
  982. while (1) {
  983. int before = total_events;
  984. for (i = 0; i < evlist->nr_mmaps; i++) {
  985. union perf_event *event;
  986. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  987. const u32 type = event->header.type;
  988. const char *name = perf_event__name(type);
  989. ++total_events;
  990. if (type < PERF_RECORD_MAX)
  991. nr_events[type]++;
  992. err = perf_event__parse_sample(event, sample_type,
  993. sample_size, true,
  994. &sample, false);
  995. if (err < 0) {
  996. if (verbose)
  997. perf_event__fprintf(event, stderr);
  998. pr_debug("Couldn't parse sample\n");
  999. goto out_err;
  1000. }
  1001. if (verbose) {
  1002. pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
  1003. perf_event__fprintf(event, stderr);
  1004. }
  1005. if (prev_time > sample.time) {
  1006. pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
  1007. name, prev_time, sample.time);
  1008. ++errs;
  1009. }
  1010. prev_time = sample.time;
  1011. if (sample.cpu != cpu) {
  1012. pr_debug("%s with unexpected cpu, expected %d, got %d\n",
  1013. name, cpu, sample.cpu);
  1014. ++errs;
  1015. }
  1016. if ((pid_t)sample.pid != evlist->workload.pid) {
  1017. pr_debug("%s with unexpected pid, expected %d, got %d\n",
  1018. name, evlist->workload.pid, sample.pid);
  1019. ++errs;
  1020. }
  1021. if ((pid_t)sample.tid != evlist->workload.pid) {
  1022. pr_debug("%s with unexpected tid, expected %d, got %d\n",
  1023. name, evlist->workload.pid, sample.tid);
  1024. ++errs;
  1025. }
  1026. if ((type == PERF_RECORD_COMM ||
  1027. type == PERF_RECORD_MMAP ||
  1028. type == PERF_RECORD_FORK ||
  1029. type == PERF_RECORD_EXIT) &&
  1030. (pid_t)event->comm.pid != evlist->workload.pid) {
  1031. pr_debug("%s with unexpected pid/tid\n", name);
  1032. ++errs;
  1033. }
  1034. if ((type == PERF_RECORD_COMM ||
  1035. type == PERF_RECORD_MMAP) &&
  1036. event->comm.pid != event->comm.tid) {
  1037. pr_debug("%s with different pid/tid!\n", name);
  1038. ++errs;
  1039. }
  1040. switch (type) {
  1041. case PERF_RECORD_COMM:
  1042. if (strcmp(event->comm.comm, cmd)) {
  1043. pr_debug("%s with unexpected comm!\n", name);
  1044. ++errs;
  1045. }
  1046. break;
  1047. case PERF_RECORD_EXIT:
  1048. goto found_exit;
  1049. case PERF_RECORD_MMAP:
  1050. bname = strrchr(event->mmap.filename, '/');
  1051. if (bname != NULL) {
  1052. if (!found_cmd_mmap)
  1053. found_cmd_mmap = !strcmp(bname + 1, cmd);
  1054. if (!found_libc_mmap)
  1055. found_libc_mmap = !strncmp(bname + 1, "libc", 4);
  1056. if (!found_ld_mmap)
  1057. found_ld_mmap = !strncmp(bname + 1, "ld", 2);
  1058. } else if (!found_vdso_mmap)
  1059. found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
  1060. break;
  1061. case PERF_RECORD_SAMPLE:
  1062. /* Just ignore samples for now */
  1063. break;
  1064. default:
  1065. pr_debug("Unexpected perf_event->header.type %d!\n",
  1066. type);
  1067. ++errs;
  1068. }
  1069. }
  1070. }
  1071. /*
  1072. * We don't use poll here because at least at 3.1 times the
  1073. * PERF_RECORD_{!SAMPLE} events don't honour
  1074. * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
  1075. */
  1076. if (total_events == before && false)
  1077. poll(evlist->pollfd, evlist->nr_fds, -1);
  1078. sleep(1);
  1079. if (++wakeups > 5) {
  1080. pr_debug("No PERF_RECORD_EXIT event!\n");
  1081. break;
  1082. }
  1083. }
  1084. found_exit:
  1085. if (nr_events[PERF_RECORD_COMM] > 1) {
  1086. pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
  1087. ++errs;
  1088. }
  1089. if (nr_events[PERF_RECORD_COMM] == 0) {
  1090. pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
  1091. ++errs;
  1092. }
  1093. if (!found_cmd_mmap) {
  1094. pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
  1095. ++errs;
  1096. }
  1097. if (!found_libc_mmap) {
  1098. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
  1099. ++errs;
  1100. }
  1101. if (!found_ld_mmap) {
  1102. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
  1103. ++errs;
  1104. }
  1105. if (!found_vdso_mmap) {
  1106. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
  1107. ++errs;
  1108. }
  1109. out_err:
  1110. perf_evlist__munmap(evlist);
  1111. out_free_cpu_mask:
  1112. CPU_FREE(cpu_mask);
  1113. out_delete_evlist:
  1114. perf_evlist__delete(evlist);
  1115. out:
  1116. return (err < 0 || errs > 0) ? -1 : 0;
  1117. }
  1118. static struct test {
  1119. const char *desc;
  1120. int (*func)(void);
  1121. } tests[] = {
  1122. {
  1123. .desc = "vmlinux symtab matches kallsyms",
  1124. .func = test__vmlinux_matches_kallsyms,
  1125. },
  1126. {
  1127. .desc = "detect open syscall event",
  1128. .func = test__open_syscall_event,
  1129. },
  1130. {
  1131. .desc = "detect open syscall event on all cpus",
  1132. .func = test__open_syscall_event_on_all_cpus,
  1133. },
  1134. {
  1135. .desc = "read samples using the mmap interface",
  1136. .func = test__basic_mmap,
  1137. },
  1138. {
  1139. .desc = "parse events tests",
  1140. .func = test__parse_events,
  1141. },
  1142. {
  1143. .desc = "Validate PERF_RECORD_* events & perf_sample fields",
  1144. .func = test__PERF_RECORD,
  1145. },
  1146. {
  1147. .func = NULL,
  1148. },
  1149. };
  1150. static bool perf_test__matches(int curr, int argc, const char *argv[])
  1151. {
  1152. int i;
  1153. if (argc == 0)
  1154. return true;
  1155. for (i = 0; i < argc; ++i) {
  1156. char *end;
  1157. long nr = strtoul(argv[i], &end, 10);
  1158. if (*end == '\0') {
  1159. if (nr == curr + 1)
  1160. return true;
  1161. continue;
  1162. }
  1163. if (strstr(tests[curr].desc, argv[i]))
  1164. return true;
  1165. }
  1166. return false;
  1167. }
  1168. static int __cmd_test(int argc, const char *argv[])
  1169. {
  1170. int i = 0;
  1171. while (tests[i].func) {
  1172. int curr = i++, err;
  1173. if (!perf_test__matches(curr, argc, argv))
  1174. continue;
  1175. pr_info("%2d: %s:", i, tests[curr].desc);
  1176. pr_debug("\n--- start ---\n");
  1177. err = tests[curr].func();
  1178. pr_debug("---- end ----\n%s:", tests[curr].desc);
  1179. pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
  1180. }
  1181. return 0;
  1182. }
  1183. static int perf_test__list(int argc, const char **argv)
  1184. {
  1185. int i = 0;
  1186. while (tests[i].func) {
  1187. int curr = i++;
  1188. if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
  1189. continue;
  1190. pr_info("%2d: %s\n", i, tests[curr].desc);
  1191. }
  1192. return 0;
  1193. }
  1194. int cmd_test(int argc, const char **argv, const char *prefix __used)
  1195. {
  1196. const char * const test_usage[] = {
  1197. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  1198. NULL,
  1199. };
  1200. const struct option test_options[] = {
  1201. OPT_INCR('v', "verbose", &verbose,
  1202. "be more verbose (show symbol address, etc)"),
  1203. OPT_END()
  1204. };
  1205. argc = parse_options(argc, argv, test_options, test_usage, 0);
  1206. if (argc >= 1 && !strcmp(argv[0], "list"))
  1207. return perf_test__list(argc, argv);
  1208. symbol_conf.priv_size = sizeof(int);
  1209. symbol_conf.sort_by_name = true;
  1210. symbol_conf.try_vmlinux_path = true;
  1211. if (symbol__init() < 0)
  1212. return -1;
  1213. setup_pager();
  1214. return __cmd_test(argc, argv);
  1215. }