builtin-test.c 35 KB

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