builtin-test.c 46 KB

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