builtin-test.c 23 KB

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