builtin-test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * builtin-test.c
  3. *
  4. * Builtin regression testing command: ever growing number of sanity tests
  5. */
  6. #include "builtin.h"
  7. #include "util/cache.h"
  8. #include "util/debug.h"
  9. #include "util/debugfs.h"
  10. #include "util/evlist.h"
  11. #include "util/parse-options.h"
  12. #include "util/parse-events.h"
  13. #include "util/symbol.h"
  14. #include "util/thread_map.h"
  15. #include "../../include/linux/hw_breakpoint.h"
  16. static long page_size;
  17. static int vmlinux_matches_kallsyms_filter(struct map *map __used, struct symbol *sym)
  18. {
  19. bool *visited = symbol__priv(sym);
  20. *visited = true;
  21. return 0;
  22. }
  23. static int test__vmlinux_matches_kallsyms(void)
  24. {
  25. int err = -1;
  26. struct rb_node *nd;
  27. struct symbol *sym;
  28. struct map *kallsyms_map, *vmlinux_map;
  29. struct machine kallsyms, vmlinux;
  30. enum map_type type = MAP__FUNCTION;
  31. struct ref_reloc_sym ref_reloc_sym = { .name = "_stext", };
  32. /*
  33. * Step 1:
  34. *
  35. * Init the machines that will hold kernel, modules obtained from
  36. * both vmlinux + .ko files and from /proc/kallsyms split by modules.
  37. */
  38. machine__init(&kallsyms, "", HOST_KERNEL_ID);
  39. machine__init(&vmlinux, "", HOST_KERNEL_ID);
  40. /*
  41. * Step 2:
  42. *
  43. * Create the kernel maps for kallsyms and the DSO where we will then
  44. * load /proc/kallsyms. Also create the modules maps from /proc/modules
  45. * and find the .ko files that match them in /lib/modules/`uname -r`/.
  46. */
  47. if (machine__create_kernel_maps(&kallsyms) < 0) {
  48. pr_debug("machine__create_kernel_maps ");
  49. return -1;
  50. }
  51. /*
  52. * Step 3:
  53. *
  54. * Load and split /proc/kallsyms into multiple maps, one per module.
  55. */
  56. if (machine__load_kallsyms(&kallsyms, "/proc/kallsyms", type, NULL) <= 0) {
  57. pr_debug("dso__load_kallsyms ");
  58. goto out;
  59. }
  60. /*
  61. * Step 4:
  62. *
  63. * kallsyms will be internally on demand sorted by name so that we can
  64. * find the reference relocation * symbol, i.e. the symbol we will use
  65. * to see if the running kernel was relocated by checking if it has the
  66. * same value in the vmlinux file we load.
  67. */
  68. kallsyms_map = machine__kernel_map(&kallsyms, type);
  69. sym = map__find_symbol_by_name(kallsyms_map, ref_reloc_sym.name, NULL);
  70. if (sym == NULL) {
  71. pr_debug("dso__find_symbol_by_name ");
  72. goto out;
  73. }
  74. ref_reloc_sym.addr = sym->start;
  75. /*
  76. * Step 5:
  77. *
  78. * Now repeat step 2, this time for the vmlinux file we'll auto-locate.
  79. */
  80. if (machine__create_kernel_maps(&vmlinux) < 0) {
  81. pr_debug("machine__create_kernel_maps ");
  82. goto out;
  83. }
  84. vmlinux_map = machine__kernel_map(&vmlinux, type);
  85. map__kmap(vmlinux_map)->ref_reloc_sym = &ref_reloc_sym;
  86. /*
  87. * Step 6:
  88. *
  89. * Locate a vmlinux file in the vmlinux path that has a buildid that
  90. * matches the one of the running kernel.
  91. *
  92. * While doing that look if we find the ref reloc symbol, if we find it
  93. * we'll have its ref_reloc_symbol.unrelocated_addr and then
  94. * maps__reloc_vmlinux will notice and set proper ->[un]map_ip routines
  95. * to fixup the symbols.
  96. */
  97. if (machine__load_vmlinux_path(&vmlinux, type,
  98. vmlinux_matches_kallsyms_filter) <= 0) {
  99. pr_debug("machine__load_vmlinux_path ");
  100. goto out;
  101. }
  102. err = 0;
  103. /*
  104. * Step 7:
  105. *
  106. * Now look at the symbols in the vmlinux DSO and check if we find all of them
  107. * in the kallsyms dso. For the ones that are in both, check its names and
  108. * end addresses too.
  109. */
  110. for (nd = rb_first(&vmlinux_map->dso->symbols[type]); nd; nd = rb_next(nd)) {
  111. struct symbol *pair, *first_pair;
  112. bool backwards = true;
  113. sym = rb_entry(nd, struct symbol, rb_node);
  114. if (sym->start == sym->end)
  115. continue;
  116. first_pair = machine__find_kernel_symbol(&kallsyms, type, sym->start, NULL, NULL);
  117. pair = first_pair;
  118. if (pair && pair->start == sym->start) {
  119. next_pair:
  120. if (strcmp(sym->name, pair->name) == 0) {
  121. /*
  122. * kallsyms don't have the symbol end, so we
  123. * set that by using the next symbol start - 1,
  124. * in some cases we get this up to a page
  125. * wrong, trace_kmalloc when I was developing
  126. * this code was one such example, 2106 bytes
  127. * off the real size. More than that and we
  128. * _really_ have a problem.
  129. */
  130. s64 skew = sym->end - pair->end;
  131. if (llabs(skew) < page_size)
  132. continue;
  133. pr_debug("%#" PRIx64 ": diff end addr for %s v: %#" PRIx64 " k: %#" PRIx64 "\n",
  134. sym->start, sym->name, sym->end, pair->end);
  135. } else {
  136. struct rb_node *nnd;
  137. detour:
  138. nnd = backwards ? rb_prev(&pair->rb_node) :
  139. rb_next(&pair->rb_node);
  140. if (nnd) {
  141. struct symbol *next = rb_entry(nnd, struct symbol, rb_node);
  142. if (next->start == sym->start) {
  143. pair = next;
  144. goto next_pair;
  145. }
  146. }
  147. if (backwards) {
  148. backwards = false;
  149. pair = first_pair;
  150. goto detour;
  151. }
  152. pr_debug("%#" PRIx64 ": diff name v: %s k: %s\n",
  153. sym->start, sym->name, pair->name);
  154. }
  155. } else
  156. pr_debug("%#" PRIx64 ": %s not on kallsyms\n", sym->start, sym->name);
  157. err = -1;
  158. }
  159. if (!verbose)
  160. goto out;
  161. pr_info("Maps only in vmlinux:\n");
  162. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  163. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  164. /*
  165. * If it is the kernel, kallsyms is always "[kernel.kallsyms]", while
  166. * the kernel will have the path for the vmlinux file being used,
  167. * so use the short name, less descriptive but the same ("[kernel]" in
  168. * both cases.
  169. */
  170. pair = map_groups__find_by_name(&kallsyms.kmaps, type,
  171. (pos->dso->kernel ?
  172. pos->dso->short_name :
  173. pos->dso->name));
  174. if (pair)
  175. pair->priv = 1;
  176. else
  177. map__fprintf(pos, stderr);
  178. }
  179. pr_info("Maps in vmlinux with a different name in kallsyms:\n");
  180. for (nd = rb_first(&vmlinux.kmaps.maps[type]); nd; nd = rb_next(nd)) {
  181. struct map *pos = rb_entry(nd, struct map, rb_node), *pair;
  182. pair = map_groups__find(&kallsyms.kmaps, type, pos->start);
  183. if (pair == NULL || pair->priv)
  184. continue;
  185. if (pair->start == pos->start) {
  186. pair->priv = 1;
  187. pr_info(" %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s in kallsyms as",
  188. pos->start, pos->end, pos->pgoff, pos->dso->name);
  189. if (pos->pgoff != pair->pgoff || pos->end != pair->end)
  190. pr_info(": \n*%" PRIx64 "-%" PRIx64 " %" PRIx64 "",
  191. pair->start, pair->end, pair->pgoff);
  192. pr_info(" %s\n", pair->dso->name);
  193. pair->priv = 1;
  194. }
  195. }
  196. pr_info("Maps only in kallsyms:\n");
  197. for (nd = rb_first(&kallsyms.kmaps.maps[type]);
  198. nd; nd = rb_next(nd)) {
  199. struct map *pos = rb_entry(nd, struct map, rb_node);
  200. if (!pos->priv)
  201. map__fprintf(pos, stderr);
  202. }
  203. out:
  204. return err;
  205. }
  206. #include "util/cpumap.h"
  207. #include "util/evsel.h"
  208. #include <sys/types.h>
  209. static int trace_event__id(const char *evname)
  210. {
  211. char *filename;
  212. int err = -1, fd;
  213. if (asprintf(&filename,
  214. "%s/syscalls/%s/id",
  215. tracing_events_path, evname) < 0)
  216. return -1;
  217. fd = open(filename, O_RDONLY);
  218. if (fd >= 0) {
  219. char id[16];
  220. if (read(fd, id, sizeof(id)) > 0)
  221. err = atoi(id);
  222. close(fd);
  223. }
  224. free(filename);
  225. return err;
  226. }
  227. static int test__open_syscall_event(void)
  228. {
  229. int err = -1, fd;
  230. struct thread_map *threads;
  231. struct perf_evsel *evsel;
  232. struct perf_event_attr attr;
  233. unsigned int nr_open_calls = 111, i;
  234. int id = trace_event__id("sys_enter_open");
  235. if (id < 0) {
  236. pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
  237. return -1;
  238. }
  239. threads = thread_map__new(-1, getpid());
  240. if (threads == NULL) {
  241. pr_debug("thread_map__new\n");
  242. return -1;
  243. }
  244. memset(&attr, 0, sizeof(attr));
  245. attr.type = PERF_TYPE_TRACEPOINT;
  246. attr.config = id;
  247. evsel = perf_evsel__new(&attr, 0);
  248. if (evsel == NULL) {
  249. pr_debug("perf_evsel__new\n");
  250. goto out_thread_map_delete;
  251. }
  252. if (perf_evsel__open_per_thread(evsel, threads, false, NULL) < 0) {
  253. pr_debug("failed to open counter: %s, "
  254. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  255. strerror(errno));
  256. goto out_evsel_delete;
  257. }
  258. for (i = 0; i < nr_open_calls; ++i) {
  259. fd = open("/etc/passwd", O_RDONLY);
  260. close(fd);
  261. }
  262. if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
  263. pr_debug("perf_evsel__read_on_cpu\n");
  264. goto out_close_fd;
  265. }
  266. if (evsel->counts->cpu[0].val != nr_open_calls) {
  267. pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
  268. nr_open_calls, evsel->counts->cpu[0].val);
  269. goto out_close_fd;
  270. }
  271. err = 0;
  272. out_close_fd:
  273. perf_evsel__close_fd(evsel, 1, threads->nr);
  274. out_evsel_delete:
  275. perf_evsel__delete(evsel);
  276. out_thread_map_delete:
  277. thread_map__delete(threads);
  278. return err;
  279. }
  280. #include <sched.h>
  281. static int test__open_syscall_event_on_all_cpus(void)
  282. {
  283. int err = -1, fd, cpu;
  284. struct thread_map *threads;
  285. struct cpu_map *cpus;
  286. struct perf_evsel *evsel;
  287. struct perf_event_attr attr;
  288. unsigned int nr_open_calls = 111, i;
  289. cpu_set_t cpu_set;
  290. int id = trace_event__id("sys_enter_open");
  291. if (id < 0) {
  292. pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
  293. return -1;
  294. }
  295. threads = thread_map__new(-1, getpid());
  296. if (threads == NULL) {
  297. pr_debug("thread_map__new\n");
  298. return -1;
  299. }
  300. cpus = cpu_map__new(NULL);
  301. if (cpus == NULL) {
  302. pr_debug("cpu_map__new\n");
  303. goto out_thread_map_delete;
  304. }
  305. CPU_ZERO(&cpu_set);
  306. memset(&attr, 0, sizeof(attr));
  307. attr.type = PERF_TYPE_TRACEPOINT;
  308. attr.config = id;
  309. evsel = perf_evsel__new(&attr, 0);
  310. if (evsel == NULL) {
  311. pr_debug("perf_evsel__new\n");
  312. goto out_thread_map_delete;
  313. }
  314. if (perf_evsel__open(evsel, cpus, threads, false, NULL) < 0) {
  315. pr_debug("failed to open counter: %s, "
  316. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  317. strerror(errno));
  318. goto out_evsel_delete;
  319. }
  320. for (cpu = 0; cpu < cpus->nr; ++cpu) {
  321. unsigned int ncalls = nr_open_calls + cpu;
  322. /*
  323. * XXX eventually lift this restriction in a way that
  324. * keeps perf building on older glibc installations
  325. * without CPU_ALLOC. 1024 cpus in 2010 still seems
  326. * a reasonable upper limit tho :-)
  327. */
  328. if (cpus->map[cpu] >= CPU_SETSIZE) {
  329. pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
  330. continue;
  331. }
  332. CPU_SET(cpus->map[cpu], &cpu_set);
  333. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  334. pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  335. cpus->map[cpu],
  336. strerror(errno));
  337. goto out_close_fd;
  338. }
  339. for (i = 0; i < ncalls; ++i) {
  340. fd = open("/etc/passwd", O_RDONLY);
  341. close(fd);
  342. }
  343. CPU_CLR(cpus->map[cpu], &cpu_set);
  344. }
  345. /*
  346. * Here we need to explicitely preallocate the counts, as if
  347. * we use the auto allocation it will allocate just for 1 cpu,
  348. * as we start by cpu 0.
  349. */
  350. if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
  351. pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
  352. goto out_close_fd;
  353. }
  354. err = 0;
  355. for (cpu = 0; cpu < cpus->nr; ++cpu) {
  356. unsigned int expected;
  357. if (cpus->map[cpu] >= CPU_SETSIZE)
  358. continue;
  359. if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
  360. pr_debug("perf_evsel__read_on_cpu\n");
  361. err = -1;
  362. break;
  363. }
  364. expected = nr_open_calls + cpu;
  365. if (evsel->counts->cpu[cpu].val != expected) {
  366. pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
  367. expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
  368. err = -1;
  369. }
  370. }
  371. out_close_fd:
  372. perf_evsel__close_fd(evsel, 1, threads->nr);
  373. out_evsel_delete:
  374. perf_evsel__delete(evsel);
  375. out_thread_map_delete:
  376. thread_map__delete(threads);
  377. return err;
  378. }
  379. /*
  380. * This test will generate random numbers of calls to some getpid syscalls,
  381. * then establish an mmap for a group of events that are created to monitor
  382. * the syscalls.
  383. *
  384. * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
  385. * sample.id field to map back to its respective perf_evsel instance.
  386. *
  387. * Then it checks if the number of syscalls reported as perf events by
  388. * the kernel corresponds to the number of syscalls made.
  389. */
  390. static int test__basic_mmap(void)
  391. {
  392. int err = -1;
  393. union perf_event *event;
  394. struct thread_map *threads;
  395. struct cpu_map *cpus;
  396. struct perf_evlist *evlist;
  397. struct perf_event_attr attr = {
  398. .type = PERF_TYPE_TRACEPOINT,
  399. .read_format = PERF_FORMAT_ID,
  400. .sample_type = PERF_SAMPLE_ID,
  401. .watermark = 0,
  402. };
  403. cpu_set_t cpu_set;
  404. const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
  405. "getpgid", };
  406. pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
  407. (void*)getpgid };
  408. #define nsyscalls ARRAY_SIZE(syscall_names)
  409. int ids[nsyscalls];
  410. unsigned int nr_events[nsyscalls],
  411. expected_nr_events[nsyscalls], i, j;
  412. struct perf_evsel *evsels[nsyscalls], *evsel;
  413. int sample_size = __perf_evsel__sample_size(attr.sample_type);
  414. for (i = 0; i < nsyscalls; ++i) {
  415. char name[64];
  416. snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
  417. ids[i] = trace_event__id(name);
  418. if (ids[i] < 0) {
  419. pr_debug("Is debugfs mounted on /sys/kernel/debug?\n");
  420. return -1;
  421. }
  422. nr_events[i] = 0;
  423. expected_nr_events[i] = random() % 257;
  424. }
  425. threads = thread_map__new(-1, getpid());
  426. if (threads == NULL) {
  427. pr_debug("thread_map__new\n");
  428. return -1;
  429. }
  430. cpus = cpu_map__new(NULL);
  431. if (cpus == NULL) {
  432. pr_debug("cpu_map__new\n");
  433. goto out_free_threads;
  434. }
  435. CPU_ZERO(&cpu_set);
  436. CPU_SET(cpus->map[0], &cpu_set);
  437. sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
  438. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  439. pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  440. cpus->map[0], strerror(errno));
  441. goto out_free_cpus;
  442. }
  443. evlist = perf_evlist__new(cpus, threads);
  444. if (evlist == NULL) {
  445. pr_debug("perf_evlist__new\n");
  446. goto out_free_cpus;
  447. }
  448. /* anonymous union fields, can't be initialized above */
  449. attr.wakeup_events = 1;
  450. attr.sample_period = 1;
  451. for (i = 0; i < nsyscalls; ++i) {
  452. attr.config = ids[i];
  453. evsels[i] = perf_evsel__new(&attr, i);
  454. if (evsels[i] == NULL) {
  455. pr_debug("perf_evsel__new\n");
  456. goto out_free_evlist;
  457. }
  458. perf_evlist__add(evlist, evsels[i]);
  459. if (perf_evsel__open(evsels[i], cpus, threads, false, NULL) < 0) {
  460. pr_debug("failed to open counter: %s, "
  461. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  462. strerror(errno));
  463. goto out_close_fd;
  464. }
  465. }
  466. if (perf_evlist__mmap(evlist, 128, true) < 0) {
  467. pr_debug("failed to mmap events: %d (%s)\n", errno,
  468. strerror(errno));
  469. goto out_close_fd;
  470. }
  471. for (i = 0; i < nsyscalls; ++i)
  472. for (j = 0; j < expected_nr_events[i]; ++j) {
  473. int foo = syscalls[i]();
  474. ++foo;
  475. }
  476. while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
  477. struct perf_sample sample;
  478. if (event->header.type != PERF_RECORD_SAMPLE) {
  479. pr_debug("unexpected %s event\n",
  480. perf_event__name(event->header.type));
  481. goto out_munmap;
  482. }
  483. err = perf_event__parse_sample(event, attr.sample_type, sample_size,
  484. false, &sample, false);
  485. if (err) {
  486. pr_err("Can't parse sample, err = %d\n", err);
  487. goto out_munmap;
  488. }
  489. evsel = perf_evlist__id2evsel(evlist, sample.id);
  490. if (evsel == NULL) {
  491. pr_debug("event with id %" PRIu64
  492. " doesn't map to an evsel\n", sample.id);
  493. goto out_munmap;
  494. }
  495. nr_events[evsel->idx]++;
  496. }
  497. list_for_each_entry(evsel, &evlist->entries, node) {
  498. if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
  499. pr_debug("expected %d %s events, got %d\n",
  500. expected_nr_events[evsel->idx],
  501. event_name(evsel), nr_events[evsel->idx]);
  502. goto out_munmap;
  503. }
  504. }
  505. err = 0;
  506. out_munmap:
  507. perf_evlist__munmap(evlist);
  508. out_close_fd:
  509. for (i = 0; i < nsyscalls; ++i)
  510. perf_evsel__close_fd(evsels[i], 1, threads->nr);
  511. out_free_evlist:
  512. perf_evlist__delete(evlist);
  513. out_free_cpus:
  514. cpu_map__delete(cpus);
  515. out_free_threads:
  516. thread_map__delete(threads);
  517. return err;
  518. #undef nsyscalls
  519. }
  520. #define TEST_ASSERT_VAL(text, cond) \
  521. do { \
  522. if (!cond) { \
  523. pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
  524. return -1; \
  525. } \
  526. } while (0)
  527. static int test__checkevent_tracepoint(struct perf_evlist *evlist)
  528. {
  529. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  530. struct perf_evsel, node);
  531. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  532. TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->attr.type);
  533. TEST_ASSERT_VAL("wrong sample_type",
  534. (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU) ==
  535. evsel->attr.sample_type);
  536. TEST_ASSERT_VAL("wrong sample_period", 1 == evsel->attr.sample_period);
  537. return 0;
  538. }
  539. static int test__checkevent_tracepoint_multi(struct perf_evlist *evlist)
  540. {
  541. struct perf_evsel *evsel;
  542. TEST_ASSERT_VAL("wrong number of entries", evlist->nr_entries > 1);
  543. list_for_each_entry(evsel, &evlist->entries, node) {
  544. TEST_ASSERT_VAL("wrong type",
  545. PERF_TYPE_TRACEPOINT == evsel->attr.type);
  546. TEST_ASSERT_VAL("wrong sample_type",
  547. (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU)
  548. == evsel->attr.sample_type);
  549. TEST_ASSERT_VAL("wrong sample_period",
  550. 1 == evsel->attr.sample_period);
  551. }
  552. return 0;
  553. }
  554. static int test__checkevent_raw(struct perf_evlist *evlist)
  555. {
  556. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  557. struct perf_evsel, node);
  558. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  559. TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->attr.type);
  560. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  561. return 0;
  562. }
  563. static int test__checkevent_numeric(struct perf_evlist *evlist)
  564. {
  565. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  566. struct perf_evsel, node);
  567. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  568. TEST_ASSERT_VAL("wrong type", 1 == evsel->attr.type);
  569. TEST_ASSERT_VAL("wrong config", 1 == evsel->attr.config);
  570. return 0;
  571. }
  572. static int test__checkevent_symbolic_name(struct perf_evlist *evlist)
  573. {
  574. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  575. struct perf_evsel, node);
  576. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  577. TEST_ASSERT_VAL("wrong type", PERF_TYPE_HARDWARE == evsel->attr.type);
  578. TEST_ASSERT_VAL("wrong config",
  579. PERF_COUNT_HW_INSTRUCTIONS == evsel->attr.config);
  580. return 0;
  581. }
  582. static int test__checkevent_symbolic_alias(struct perf_evlist *evlist)
  583. {
  584. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  585. struct perf_evsel, node);
  586. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  587. TEST_ASSERT_VAL("wrong type", PERF_TYPE_SOFTWARE == evsel->attr.type);
  588. TEST_ASSERT_VAL("wrong config",
  589. PERF_COUNT_SW_PAGE_FAULTS == evsel->attr.config);
  590. return 0;
  591. }
  592. static int test__checkevent_genhw(struct perf_evlist *evlist)
  593. {
  594. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  595. struct perf_evsel, node);
  596. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  597. TEST_ASSERT_VAL("wrong type", PERF_TYPE_HW_CACHE == evsel->attr.type);
  598. TEST_ASSERT_VAL("wrong config", (1 << 16) == evsel->attr.config);
  599. return 0;
  600. }
  601. static int test__checkevent_breakpoint(struct perf_evlist *evlist)
  602. {
  603. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  604. struct perf_evsel, node);
  605. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  606. TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
  607. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  608. TEST_ASSERT_VAL("wrong bp_type", (HW_BREAKPOINT_R | HW_BREAKPOINT_W) ==
  609. evsel->attr.bp_type);
  610. TEST_ASSERT_VAL("wrong bp_len", HW_BREAKPOINT_LEN_4 ==
  611. evsel->attr.bp_len);
  612. return 0;
  613. }
  614. static int test__checkevent_breakpoint_x(struct perf_evlist *evlist)
  615. {
  616. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  617. struct perf_evsel, node);
  618. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  619. TEST_ASSERT_VAL("wrong type", PERF_TYPE_BREAKPOINT == evsel->attr.type);
  620. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  621. TEST_ASSERT_VAL("wrong bp_type",
  622. HW_BREAKPOINT_X == evsel->attr.bp_type);
  623. TEST_ASSERT_VAL("wrong bp_len", sizeof(long) == evsel->attr.bp_len);
  624. return 0;
  625. }
  626. static int test__checkevent_breakpoint_r(struct perf_evlist *evlist)
  627. {
  628. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  629. struct perf_evsel, node);
  630. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  631. TEST_ASSERT_VAL("wrong type",
  632. PERF_TYPE_BREAKPOINT == evsel->attr.type);
  633. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  634. TEST_ASSERT_VAL("wrong bp_type",
  635. HW_BREAKPOINT_R == evsel->attr.bp_type);
  636. TEST_ASSERT_VAL("wrong bp_len",
  637. HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
  638. return 0;
  639. }
  640. static int test__checkevent_breakpoint_w(struct perf_evlist *evlist)
  641. {
  642. struct perf_evsel *evsel = list_entry(evlist->entries.next,
  643. struct perf_evsel, node);
  644. TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->nr_entries);
  645. TEST_ASSERT_VAL("wrong type",
  646. PERF_TYPE_BREAKPOINT == evsel->attr.type);
  647. TEST_ASSERT_VAL("wrong config", 0 == evsel->attr.config);
  648. TEST_ASSERT_VAL("wrong bp_type",
  649. HW_BREAKPOINT_W == evsel->attr.bp_type);
  650. TEST_ASSERT_VAL("wrong bp_len",
  651. HW_BREAKPOINT_LEN_4 == evsel->attr.bp_len);
  652. return 0;
  653. }
  654. static struct test__event_st {
  655. const char *name;
  656. __u32 type;
  657. int (*check)(struct perf_evlist *evlist);
  658. } test__events[] = {
  659. {
  660. .name = "syscalls:sys_enter_open",
  661. .check = test__checkevent_tracepoint,
  662. },
  663. {
  664. .name = "syscalls:*",
  665. .check = test__checkevent_tracepoint_multi,
  666. },
  667. {
  668. .name = "r1",
  669. .check = test__checkevent_raw,
  670. },
  671. {
  672. .name = "1:1",
  673. .check = test__checkevent_numeric,
  674. },
  675. {
  676. .name = "instructions",
  677. .check = test__checkevent_symbolic_name,
  678. },
  679. {
  680. .name = "faults",
  681. .check = test__checkevent_symbolic_alias,
  682. },
  683. {
  684. .name = "L1-dcache-load-miss",
  685. .check = test__checkevent_genhw,
  686. },
  687. {
  688. .name = "mem:0",
  689. .check = test__checkevent_breakpoint,
  690. },
  691. {
  692. .name = "mem:0:x",
  693. .check = test__checkevent_breakpoint_x,
  694. },
  695. {
  696. .name = "mem:0:r",
  697. .check = test__checkevent_breakpoint_r,
  698. },
  699. {
  700. .name = "mem:0:w",
  701. .check = test__checkevent_breakpoint_w,
  702. },
  703. };
  704. #define TEST__EVENTS_CNT (sizeof(test__events) / sizeof(struct test__event_st))
  705. static int test__parse_events(void)
  706. {
  707. struct perf_evlist *evlist;
  708. u_int i;
  709. int ret = 0;
  710. for (i = 0; i < TEST__EVENTS_CNT; i++) {
  711. struct test__event_st *e = &test__events[i];
  712. evlist = perf_evlist__new(NULL, NULL);
  713. if (evlist == NULL)
  714. break;
  715. ret = parse_events(evlist, e->name, 0);
  716. if (ret) {
  717. pr_debug("failed to parse event '%s', err %d\n",
  718. e->name, ret);
  719. break;
  720. }
  721. ret = e->check(evlist);
  722. if (ret)
  723. break;
  724. perf_evlist__delete(evlist);
  725. }
  726. return ret;
  727. }
  728. static struct test {
  729. const char *desc;
  730. int (*func)(void);
  731. } tests[] = {
  732. {
  733. .desc = "vmlinux symtab matches kallsyms",
  734. .func = test__vmlinux_matches_kallsyms,
  735. },
  736. {
  737. .desc = "detect open syscall event",
  738. .func = test__open_syscall_event,
  739. },
  740. {
  741. .desc = "detect open syscall event on all cpus",
  742. .func = test__open_syscall_event_on_all_cpus,
  743. },
  744. {
  745. .desc = "read samples using the mmap interface",
  746. .func = test__basic_mmap,
  747. },
  748. {
  749. .desc = "parse events tests",
  750. .func = test__parse_events,
  751. },
  752. {
  753. .func = NULL,
  754. },
  755. };
  756. static int __cmd_test(void)
  757. {
  758. int i = 0;
  759. page_size = sysconf(_SC_PAGE_SIZE);
  760. while (tests[i].func) {
  761. int err;
  762. pr_info("%2d: %s:", i + 1, tests[i].desc);
  763. pr_debug("\n--- start ---\n");
  764. err = tests[i].func();
  765. pr_debug("---- end ----\n%s:", tests[i].desc);
  766. pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
  767. ++i;
  768. }
  769. return 0;
  770. }
  771. static const char * const test_usage[] = {
  772. "perf test [<options>]",
  773. NULL,
  774. };
  775. static const struct option test_options[] = {
  776. OPT_INTEGER('v', "verbose", &verbose,
  777. "be more verbose (show symbol address, etc)"),
  778. OPT_END()
  779. };
  780. int cmd_test(int argc, const char **argv, const char *prefix __used)
  781. {
  782. argc = parse_options(argc, argv, test_options, test_usage, 0);
  783. if (argc)
  784. usage_with_options(test_usage, test_options);
  785. symbol_conf.priv_size = sizeof(int);
  786. symbol_conf.sort_by_name = true;
  787. symbol_conf.try_vmlinux_path = true;
  788. if (symbol__init() < 0)
  789. return -1;
  790. setup_pager();
  791. return __cmd_test();
  792. }