builtin-test.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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_symbolic_alias_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_user", !evsel->attr.exclude_user);
  732. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  733. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  734. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  735. return test__checkevent_symbolic_alias(evlist);
  736. }
  737. static int test__checkevent_genhw_modifier(struct perf_evlist *evlist)
  738. {
  739. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  740. struct perf_evsel, node);
  741. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  742. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  743. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  744. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  745. return test__checkevent_genhw(evlist);
  746. }
  747. static int test__checkevent_breakpoint_modifier(struct perf_evlist *evlist)
  748. {
  749. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  750. struct perf_evsel, node);
  751. TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
  752. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  753. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  754. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  755. return test__checkevent_breakpoint(evlist);
  756. }
  757. static int test__checkevent_breakpoint_x_modifier(struct perf_evlist *evlist)
  758. {
  759. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  760. struct perf_evsel, node);
  761. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  762. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  763. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  764. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  765. return test__checkevent_breakpoint_x(evlist);
  766. }
  767. static int test__checkevent_breakpoint_r_modifier(struct perf_evlist *evlist)
  768. {
  769. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  770. struct perf_evsel, node);
  771. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  772. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  773. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  774. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  775. return test__checkevent_breakpoint_r(evlist);
  776. }
  777. static int test__checkevent_breakpoint_w_modifier(struct perf_evlist *evlist)
  778. {
  779. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  780. struct perf_evsel, node);
  781. TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
  782. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  783. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  784. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  785. return test__checkevent_breakpoint_w(evlist);
  786. }
  787. static int test__checkevent_pmu(struct perf_evlist *evlist)
  788. {
  789. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  790. struct perf_evsel, node);
  791. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  792. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  793. TEST_ASSERT_VAL("wrong config", 10 == evsel->attr.config);
  794. TEST_ASSERT_VAL("wrong config1", 1 == evsel->attr.config1);
  795. TEST_ASSERT_VAL("wrong config2", 3 == evsel->attr.config2);
  796. TEST_ASSERT_VAL("wrong period", 1000 == evsel->attr.sample_period);
  797. return 0;
  798. }
  799. static int test__checkevent_list(struct perf_evlist *evlist)
  800. {
  801. struct perf_evsel *evsel;
  802. TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->nr_entries);
  803. /* r1 */
  804. evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  805. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  806. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  807. TEST_ASSERT_VAL("wrong config1", 0 == evsel->attr.config1);
  808. TEST_ASSERT_VAL("wrong config2", 0 == evsel->attr.config2);
  809. TEST_ASSERT_VAL("wrong exclude_user", !evsel->attr.exclude_user);
  810. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  811. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  812. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  813. /* syscalls:sys_enter_open:k */
  814. evsel = list_entry(evsel->node.next, struct perf_evsel, node);
  815. TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
  816. TEST_ASSERT_VAL("wrong sample_type",
  817. (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
  818. evsel->attr.sample_type);
  819. TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
  820. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  821. TEST_ASSERT_VAL("wrong exclude_kernel", !evsel->attr.exclude_kernel);
  822. TEST_ASSERT_VAL("wrong exclude_hv", evsel->attr.exclude_hv);
  823. TEST_ASSERT_VAL("wrong precise_ip", !evsel->attr.precise_ip);
  824. /* 1:1:hp */
  825. evsel = list_entry(evsel->node.next, struct perf_evsel, node);
  826. TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
  827. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  828. TEST_ASSERT_VAL("wrong exclude_user", evsel->attr.exclude_user);
  829. TEST_ASSERT_VAL("wrong exclude_kernel", evsel->attr.exclude_kernel);
  830. TEST_ASSERT_VAL("wrong exclude_hv", !evsel->attr.exclude_hv);
  831. TEST_ASSERT_VAL("wrong precise_ip", evsel->attr.precise_ip);
  832. return 0;
  833. }
  834. static struct test__event_st {
  835. const char *name;
  836. __u32 type;
  837. int (*check)(struct perf_evlist *evlist);
  838. } test__events[] = {
  839. {
  840. .name = "syscalls:sys_enter_open",
  841. .check = test__checkevent_tracepoint,
  842. },
  843. {
  844. .name = "syscalls:*",
  845. .check = test__checkevent_tracepoint_multi,
  846. },
  847. {
  848. .name = "r1a",
  849. .check = test__checkevent_raw,
  850. },
  851. {
  852. .name = "1:1",
  853. .check = test__checkevent_numeric,
  854. },
  855. {
  856. .name = "instructions",
  857. .check = test__checkevent_symbolic_name,
  858. },
  859. {
  860. .name = "cycles/period=100000,config2/",
  861. .check = test__checkevent_symbolic_name_config,
  862. },
  863. {
  864. .name = "faults",
  865. .check = test__checkevent_symbolic_alias,
  866. },
  867. {
  868. .name = "L1-dcache-load-miss",
  869. .check = test__checkevent_genhw,
  870. },
  871. {
  872. .name = "mem:0",
  873. .check = test__checkevent_breakpoint,
  874. },
  875. {
  876. .name = "mem:0:x",
  877. .check = test__checkevent_breakpoint_x,
  878. },
  879. {
  880. .name = "mem:0:r",
  881. .check = test__checkevent_breakpoint_r,
  882. },
  883. {
  884. .name = "mem:0:w",
  885. .check = test__checkevent_breakpoint_w,
  886. },
  887. {
  888. .name = "syscalls:sys_enter_open:k",
  889. .check = test__checkevent_tracepoint_modifier,
  890. },
  891. {
  892. .name = "syscalls:*:u",
  893. .check = test__checkevent_tracepoint_multi_modifier,
  894. },
  895. {
  896. .name = "r1a:kp",
  897. .check = test__checkevent_raw_modifier,
  898. },
  899. {
  900. .name = "1:1:hp",
  901. .check = test__checkevent_numeric_modifier,
  902. },
  903. {
  904. .name = "instructions:h",
  905. .check = test__checkevent_symbolic_name_modifier,
  906. },
  907. {
  908. .name = "faults:u",
  909. .check = test__checkevent_symbolic_alias_modifier,
  910. },
  911. {
  912. .name = "L1-dcache-load-miss:kp",
  913. .check = test__checkevent_genhw_modifier,
  914. },
  915. {
  916. .name = "mem:0:u",
  917. .check = test__checkevent_breakpoint_modifier,
  918. },
  919. {
  920. .name = "mem:0:x:k",
  921. .check = test__checkevent_breakpoint_x_modifier,
  922. },
  923. {
  924. .name = "mem:0:r:hp",
  925. .check = test__checkevent_breakpoint_r_modifier,
  926. },
  927. {
  928. .name = "mem:0:w:up",
  929. .check = test__checkevent_breakpoint_w_modifier,
  930. },
  931. {
  932. .name = "cpu/config=10,config1,config2=3,period=1000/u",
  933. .check = test__checkevent_pmu,
  934. },
  935. {
  936. .name = "r1,syscalls:sys_enter_open:k,1:1:hp",
  937. .check = test__checkevent_list,
  938. },
  939. };
  940. #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
  941. static int test__parse_events(void)
  942. {
  943. struct perf_evlist *evlist;
  944. u_int i;
  945. int ret = 0;
  946. for (i = 0; i < TEST__EVENTS_CNT; i++) {
  947. struct test__event_st *e = &test__events[i];
  948. evlist = perf_evlist__new(NULL, NULL);
  949. if (evlist == NULL)
  950. break;
  951. ret = parse_events(evlist, e->name, 0);
  952. if (ret) {
  953. pr_debug("failed to parse event '%s', err %d\n",
  954. e->name, ret);
  955. break;
  956. }
  957. ret = e->check(evlist);
  958. perf_evlist__delete(evlist);
  959. if (ret)
  960. break;
  961. }
  962. return ret;
  963. }
  964. static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
  965. size_t *sizep)
  966. {
  967. cpu_set_t *mask;
  968. size_t size;
  969. int i, cpu = -1, nrcpus = 1024;
  970. realloc:
  971. mask = CPU_ALLOC(nrcpus);
  972. size = CPU_ALLOC_SIZE(nrcpus);
  973. CPU_ZERO_S(size, mask);
  974. if (sched_getaffinity(pid, size, mask) == -1) {
  975. CPU_FREE(mask);
  976. if (errno == EINVAL && nrcpus < (1024 << 8)) {
  977. nrcpus = nrcpus << 2;
  978. goto realloc;
  979. }
  980. perror("sched_getaffinity");
  981. return -1;
  982. }
  983. for (i = 0; i < nrcpus; i++) {
  984. if (CPU_ISSET_S(i, size, mask)) {
  985. if (cpu == -1) {
  986. cpu = i;
  987. *maskp = mask;
  988. *sizep = size;
  989. } else
  990. CPU_CLR_S(i, size, mask);
  991. }
  992. }
  993. if (cpu == -1)
  994. CPU_FREE(mask);
  995. return cpu;
  996. }
  997. static int test__PERF_RECORD(void)
  998. {
  999. struct perf_record_opts opts = {
  1000. .no_delay = true,
  1001. .freq = 10,
  1002. .mmap_pages = 256,
  1003. };
  1004. cpu_set_t *cpu_mask = NULL;
  1005. size_t cpu_mask_size = 0;
  1006. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  1007. struct perf_evsel *evsel;
  1008. struct perf_sample sample;
  1009. const char *cmd = "sleep";
  1010. const char *argv[] = { cmd, "1", NULL, };
  1011. char *bname;
  1012. u64 sample_type, prev_time = 0;
  1013. bool found_cmd_mmap = false,
  1014. found_libc_mmap = false,
  1015. found_vdso_mmap = false,
  1016. found_ld_mmap = false;
  1017. int err = -1, errs = 0, i, wakeups = 0, sample_size;
  1018. u32 cpu;
  1019. int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
  1020. if (evlist == NULL || argv == NULL) {
  1021. pr_debug("Not enough memory to create evlist\n");
  1022. goto out;
  1023. }
  1024. /*
  1025. * We need at least one evsel in the evlist, use the default
  1026. * one: "cycles".
  1027. */
  1028. err = perf_evlist__add_default(evlist);
  1029. if (err < 0) {
  1030. pr_debug("Not enough memory to create evsel\n");
  1031. goto out_delete_evlist;
  1032. }
  1033. /*
  1034. * Create maps of threads and cpus to monitor. In this case
  1035. * we start with all threads and cpus (-1, -1) but then in
  1036. * perf_evlist__prepare_workload we'll fill in the only thread
  1037. * we're monitoring, the one forked there.
  1038. */
  1039. err = perf_evlist__create_maps(evlist, opts.target_pid,
  1040. opts.target_tid, UINT_MAX, opts.cpu_list);
  1041. if (err < 0) {
  1042. pr_debug("Not enough memory to create thread/cpu maps\n");
  1043. goto out_delete_evlist;
  1044. }
  1045. /*
  1046. * Prepare the workload in argv[] to run, it'll fork it, and then wait
  1047. * for perf_evlist__start_workload() to exec it. This is done this way
  1048. * so that we have time to open the evlist (calling sys_perf_event_open
  1049. * on all the fds) and then mmap them.
  1050. */
  1051. err = perf_evlist__prepare_workload(evlist, &opts, argv);
  1052. if (err < 0) {
  1053. pr_debug("Couldn't run the workload!\n");
  1054. goto out_delete_evlist;
  1055. }
  1056. /*
  1057. * Config the evsels, setting attr->comm on the first one, etc.
  1058. */
  1059. evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  1060. evsel->attr.sample_type |= PERF_SAMPLE_CPU;
  1061. evsel->attr.sample_type |= PERF_SAMPLE_TID;
  1062. evsel->attr.sample_type |= PERF_SAMPLE_TIME;
  1063. perf_evlist__config_attrs(evlist, &opts);
  1064. err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask,
  1065. &cpu_mask_size);
  1066. if (err < 0) {
  1067. pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
  1068. goto out_delete_evlist;
  1069. }
  1070. cpu = err;
  1071. /*
  1072. * So that we can check perf_sample.cpu on all the samples.
  1073. */
  1074. if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
  1075. pr_debug("sched_setaffinity: %s\n", strerror(errno));
  1076. goto out_free_cpu_mask;
  1077. }
  1078. /*
  1079. * Call sys_perf_event_open on all the fds on all the evsels,
  1080. * grouping them if asked to.
  1081. */
  1082. err = perf_evlist__open(evlist, opts.group);
  1083. if (err < 0) {
  1084. pr_debug("perf_evlist__open: %s\n", strerror(errno));
  1085. goto out_delete_evlist;
  1086. }
  1087. /*
  1088. * mmap the first fd on a given CPU and ask for events for the other
  1089. * fds in the same CPU to be injected in the same mmap ring buffer
  1090. * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
  1091. */
  1092. err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
  1093. if (err < 0) {
  1094. pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
  1095. goto out_delete_evlist;
  1096. }
  1097. /*
  1098. * We'll need these two to parse the PERF_SAMPLE_* fields in each
  1099. * event.
  1100. */
  1101. sample_type = perf_evlist__sample_type(evlist);
  1102. sample_size = __perf_evsel__sample_size(sample_type);
  1103. /*
  1104. * Now that all is properly set up, enable the events, they will
  1105. * count just on workload.pid, which will start...
  1106. */
  1107. perf_evlist__enable(evlist);
  1108. /*
  1109. * Now!
  1110. */
  1111. perf_evlist__start_workload(evlist);
  1112. while (1) {
  1113. int before = total_events;
  1114. for (i = 0; i < evlist->nr_mmaps; i++) {
  1115. union perf_event *event;
  1116. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  1117. const u32 type = event->header.type;
  1118. const char *name = perf_event__name(type);
  1119. ++total_events;
  1120. if (type < PERF_RECORD_MAX)
  1121. nr_events[type]++;
  1122. err = perf_event__parse_sample(event, sample_type,
  1123. sample_size, true,
  1124. &sample, false);
  1125. if (err < 0) {
  1126. if (verbose)
  1127. perf_event__fprintf(event, stderr);
  1128. pr_debug("Couldn't parse sample\n");
  1129. goto out_err;
  1130. }
  1131. if (verbose) {
  1132. pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
  1133. perf_event__fprintf(event, stderr);
  1134. }
  1135. if (prev_time > sample.time) {
  1136. pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
  1137. name, prev_time, sample.time);
  1138. ++errs;
  1139. }
  1140. prev_time = sample.time;
  1141. if (sample.cpu != cpu) {
  1142. pr_debug("%s with unexpected cpu, expected %d, got %d\n",
  1143. name, cpu, sample.cpu);
  1144. ++errs;
  1145. }
  1146. if ((pid_t)sample.pid != evlist->workload.pid) {
  1147. pr_debug("%s with unexpected pid, expected %d, got %d\n",
  1148. name, evlist->workload.pid, sample.pid);
  1149. ++errs;
  1150. }
  1151. if ((pid_t)sample.tid != evlist->workload.pid) {
  1152. pr_debug("%s with unexpected tid, expected %d, got %d\n",
  1153. name, evlist->workload.pid, sample.tid);
  1154. ++errs;
  1155. }
  1156. if ((type == PERF_RECORD_COMM ||
  1157. type == PERF_RECORD_MMAP ||
  1158. type == PERF_RECORD_FORK ||
  1159. type == PERF_RECORD_EXIT) &&
  1160. (pid_t)event->comm.pid != evlist->workload.pid) {
  1161. pr_debug("%s with unexpected pid/tid\n", name);
  1162. ++errs;
  1163. }
  1164. if ((type == PERF_RECORD_COMM ||
  1165. type == PERF_RECORD_MMAP) &&
  1166. event->comm.pid != event->comm.tid) {
  1167. pr_debug("%s with different pid/tid!\n", name);
  1168. ++errs;
  1169. }
  1170. switch (type) {
  1171. case PERF_RECORD_COMM:
  1172. if (strcmp(event->comm.comm, cmd)) {
  1173. pr_debug("%s with unexpected comm!\n", name);
  1174. ++errs;
  1175. }
  1176. break;
  1177. case PERF_RECORD_EXIT:
  1178. goto found_exit;
  1179. case PERF_RECORD_MMAP:
  1180. bname = strrchr(event->mmap.filename, '/');
  1181. if (bname != NULL) {
  1182. if (!found_cmd_mmap)
  1183. found_cmd_mmap = !strcmp(bname + 1, cmd);
  1184. if (!found_libc_mmap)
  1185. found_libc_mmap = !strncmp(bname + 1, "libc", 4);
  1186. if (!found_ld_mmap)
  1187. found_ld_mmap = !strncmp(bname + 1, "ld", 2);
  1188. } else if (!found_vdso_mmap)
  1189. found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
  1190. break;
  1191. case PERF_RECORD_SAMPLE:
  1192. /* Just ignore samples for now */
  1193. break;
  1194. default:
  1195. pr_debug("Unexpected perf_event->header.type %d!\n",
  1196. type);
  1197. ++errs;
  1198. }
  1199. }
  1200. }
  1201. /*
  1202. * We don't use poll here because at least at 3.1 times the
  1203. * PERF_RECORD_{!SAMPLE} events don't honour
  1204. * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
  1205. */
  1206. if (total_events == before && false)
  1207. poll(evlist->pollfd, evlist->nr_fds, -1);
  1208. sleep(1);
  1209. if (++wakeups > 5) {
  1210. pr_debug("No PERF_RECORD_EXIT event!\n");
  1211. break;
  1212. }
  1213. }
  1214. found_exit:
  1215. if (nr_events[PERF_RECORD_COMM] > 1) {
  1216. pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
  1217. ++errs;
  1218. }
  1219. if (nr_events[PERF_RECORD_COMM] == 0) {
  1220. pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
  1221. ++errs;
  1222. }
  1223. if (!found_cmd_mmap) {
  1224. pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
  1225. ++errs;
  1226. }
  1227. if (!found_libc_mmap) {
  1228. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
  1229. ++errs;
  1230. }
  1231. if (!found_ld_mmap) {
  1232. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
  1233. ++errs;
  1234. }
  1235. if (!found_vdso_mmap) {
  1236. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
  1237. ++errs;
  1238. }
  1239. out_err:
  1240. perf_evlist__munmap(evlist);
  1241. out_free_cpu_mask:
  1242. CPU_FREE(cpu_mask);
  1243. out_delete_evlist:
  1244. perf_evlist__delete(evlist);
  1245. out:
  1246. return (err < 0 || errs > 0) ? -1 : 0;
  1247. }
  1248. #if defined(__x86_64__) || defined(__i386__)
  1249. #define barrier() asm volatile("" ::: "memory")
  1250. static u64 rdpmc(unsigned int counter)
  1251. {
  1252. unsigned int low, high;
  1253. asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
  1254. return low | ((u64)high) << 32;
  1255. }
  1256. static u64 rdtsc(void)
  1257. {
  1258. unsigned int low, high;
  1259. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  1260. return low | ((u64)high) << 32;
  1261. }
  1262. static u64 mmap_read_self(void *addr)
  1263. {
  1264. struct perf_event_mmap_page *pc = addr;
  1265. u32 seq, idx, time_mult = 0, time_shift = 0;
  1266. u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
  1267. do {
  1268. seq = pc->lock;
  1269. barrier();
  1270. enabled = pc->time_enabled;
  1271. running = pc->time_running;
  1272. if (enabled != running) {
  1273. cyc = rdtsc();
  1274. time_mult = pc->time_mult;
  1275. time_shift = pc->time_shift;
  1276. time_offset = pc->time_offset;
  1277. }
  1278. idx = pc->index;
  1279. count = pc->offset;
  1280. if (idx)
  1281. count += rdpmc(idx - 1);
  1282. barrier();
  1283. } while (pc->lock != seq);
  1284. if (enabled != running) {
  1285. u64 quot, rem;
  1286. quot = (cyc >> time_shift);
  1287. rem = cyc & ((1 << time_shift) - 1);
  1288. delta = time_offset + quot * time_mult +
  1289. ((rem * time_mult) >> time_shift);
  1290. enabled += delta;
  1291. if (idx)
  1292. running += delta;
  1293. quot = count / running;
  1294. rem = count % running;
  1295. count = quot * enabled + (rem * enabled) / running;
  1296. }
  1297. return count;
  1298. }
  1299. /*
  1300. * If the RDPMC instruction faults then signal this back to the test parent task:
  1301. */
  1302. static void segfault_handler(int sig __used, siginfo_t *info __used, void *uc __used)
  1303. {
  1304. exit(-1);
  1305. }
  1306. static int __test__rdpmc(void)
  1307. {
  1308. long page_size = sysconf(_SC_PAGE_SIZE);
  1309. volatile int tmp = 0;
  1310. u64 i, loops = 1000;
  1311. int n;
  1312. int fd;
  1313. void *addr;
  1314. struct perf_event_attr attr = {
  1315. .type = PERF_TYPE_HARDWARE,
  1316. .config = PERF_COUNT_HW_INSTRUCTIONS,
  1317. .exclude_kernel = 1,
  1318. };
  1319. u64 delta_sum = 0;
  1320. struct sigaction sa;
  1321. sigfillset(&sa.sa_mask);
  1322. sa.sa_sigaction = segfault_handler;
  1323. sigaction(SIGSEGV, &sa, NULL);
  1324. fprintf(stderr, "\n\n");
  1325. fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
  1326. if (fd < 0) {
  1327. die("Error: sys_perf_event_open() syscall returned "
  1328. "with %d (%s)\n", fd, strerror(errno));
  1329. }
  1330. addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
  1331. if (addr == (void *)(-1)) {
  1332. die("Error: mmap() syscall returned "
  1333. "with (%s)\n", strerror(errno));
  1334. }
  1335. for (n = 0; n < 6; n++) {
  1336. u64 stamp, now, delta;
  1337. stamp = mmap_read_self(addr);
  1338. for (i = 0; i < loops; i++)
  1339. tmp++;
  1340. now = mmap_read_self(addr);
  1341. loops *= 10;
  1342. delta = now - stamp;
  1343. fprintf(stderr, "%14d: %14Lu\n", n, (long long)delta);
  1344. delta_sum += delta;
  1345. }
  1346. munmap(addr, page_size);
  1347. close(fd);
  1348. fprintf(stderr, " ");
  1349. if (!delta_sum)
  1350. return -1;
  1351. return 0;
  1352. }
  1353. static int test__rdpmc(void)
  1354. {
  1355. int status = 0;
  1356. int wret = 0;
  1357. int ret;
  1358. int pid;
  1359. pid = fork();
  1360. if (pid < 0)
  1361. return -1;
  1362. if (!pid) {
  1363. ret = __test__rdpmc();
  1364. exit(ret);
  1365. }
  1366. wret = waitpid(pid, &status, 0);
  1367. if (wret < 0 || status)
  1368. return -1;
  1369. return 0;
  1370. }
  1371. #endif
  1372. static int test__perf_pmu(void)
  1373. {
  1374. return perf_pmu__test();
  1375. }
  1376. static struct test {
  1377. const char *desc;
  1378. int (*func)(void);
  1379. } tests[] = {
  1380. {
  1381. .desc = "vmlinux symtab matches kallsyms",
  1382. .func = test__vmlinux_matches_kallsyms,
  1383. },
  1384. {
  1385. .desc = "detect open syscall event",
  1386. .func = test__open_syscall_event,
  1387. },
  1388. {
  1389. .desc = "detect open syscall event on all cpus",
  1390. .func = test__open_syscall_event_on_all_cpus,
  1391. },
  1392. {
  1393. .desc = "read samples using the mmap interface",
  1394. .func = test__basic_mmap,
  1395. },
  1396. {
  1397. .desc = "parse events tests",
  1398. .func = test__parse_events,
  1399. },
  1400. #if defined(__x86_64__) || defined(__i386__)
  1401. {
  1402. .desc = "x86 rdpmc test",
  1403. .func = test__rdpmc,
  1404. },
  1405. #endif
  1406. {
  1407. .desc = "Validate PERF_RECORD_* events & perf_sample fields",
  1408. .func = test__PERF_RECORD,
  1409. },
  1410. {
  1411. .desc = "Test perf pmu format parsing",
  1412. .func = test__perf_pmu,
  1413. },
  1414. {
  1415. .func = NULL,
  1416. },
  1417. };
  1418. static bool perf_test__matches(int curr, int argc, const char *argv[])
  1419. {
  1420. int i;
  1421. if (argc == 0)
  1422. return true;
  1423. for (i = 0; i < argc; ++i) {
  1424. char *end;
  1425. long nr = strtoul(argv[i], &end, 10);
  1426. if (*end == '\0') {
  1427. if (nr == curr + 1)
  1428. return true;
  1429. continue;
  1430. }
  1431. if (strstr(tests[curr].desc, argv[i]))
  1432. return true;
  1433. }
  1434. return false;
  1435. }
  1436. static int __cmd_test(int argc, const char *argv[])
  1437. {
  1438. int i = 0;
  1439. while (tests[i].func) {
  1440. int curr = i++, err;
  1441. if (!perf_test__matches(curr, argc, argv))
  1442. continue;
  1443. pr_info("%2d: %s:", i, tests[curr].desc);
  1444. pr_debug("\n--- start ---\n");
  1445. err = tests[curr].func();
  1446. pr_debug("---- end ----\n%s:", tests[curr].desc);
  1447. pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
  1448. }
  1449. return 0;
  1450. }
  1451. static int perf_test__list(int argc, const char **argv)
  1452. {
  1453. int i = 0;
  1454. while (tests[i].func) {
  1455. int curr = i++;
  1456. if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
  1457. continue;
  1458. pr_info("%2d: %s\n", i, tests[curr].desc);
  1459. }
  1460. return 0;
  1461. }
  1462. int cmd_test(int argc, const char **argv, const char *prefix __used)
  1463. {
  1464. const char * const test_usage[] = {
  1465. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  1466. NULL,
  1467. };
  1468. const struct option test_options[] = {
  1469. OPT_INCR('v', "verbose", &verbose,
  1470. "be more verbose (show symbol address, etc)"),
  1471. OPT_END()
  1472. };
  1473. argc = parse_options(argc, argv, test_options, test_usage, 0);
  1474. if (argc >= 1 && !strcmp(argv[0], "list"))
  1475. return perf_test__list(argc, argv);
  1476. symbol_conf.priv_size = sizeof(int);
  1477. symbol_conf.sort_by_name = true;
  1478. symbol_conf.try_vmlinux_path = true;
  1479. if (symbol__init() < 0)
  1480. return -1;
  1481. return __cmd_test(argc, argv);
  1482. }