builtin-test.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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) < 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) < 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. for (i = 0; i < nsyscalls; ++i) {
  416. char name[64];
  417. snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
  418. ids[i] = trace_event__id(name);
  419. if (ids[i] < 0) {
  420. pr_debug("Is debugfs mounted on /sys/kernel/debug?\n");
  421. return -1;
  422. }
  423. nr_events[i] = 0;
  424. expected_nr_events[i] = random() % 257;
  425. }
  426. threads = thread_map__new(-1, getpid(), UINT_MAX);
  427. if (threads == NULL) {
  428. pr_debug("thread_map__new\n");
  429. return -1;
  430. }
  431. cpus = cpu_map__new(NULL);
  432. if (cpus == NULL) {
  433. pr_debug("cpu_map__new\n");
  434. goto out_free_threads;
  435. }
  436. CPU_ZERO(&cpu_set);
  437. CPU_SET(cpus->map[0], &cpu_set);
  438. sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
  439. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  440. pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  441. cpus->map[0], strerror(errno));
  442. goto out_free_cpus;
  443. }
  444. evlist = perf_evlist__new(cpus, threads);
  445. if (evlist == NULL) {
  446. pr_debug("perf_evlist__new\n");
  447. goto out_free_cpus;
  448. }
  449. /* anonymous union fields, can't be initialized above */
  450. attr.wakeup_events = 1;
  451. attr.sample_period = 1;
  452. for (i = 0; i < nsyscalls; ++i) {
  453. attr.config = ids[i];
  454. evsels[i] = perf_evsel__new(&attr, i);
  455. if (evsels[i] == NULL) {
  456. pr_debug("perf_evsel__new\n");
  457. goto out_free_evlist;
  458. }
  459. perf_evlist__add(evlist, evsels[i]);
  460. if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
  461. pr_debug("failed to open counter: %s, "
  462. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  463. strerror(errno));
  464. goto out_close_fd;
  465. }
  466. }
  467. if (perf_evlist__mmap(evlist, 128, true) < 0) {
  468. pr_debug("failed to mmap events: %d (%s)\n", errno,
  469. strerror(errno));
  470. goto out_close_fd;
  471. }
  472. for (i = 0; i < nsyscalls; ++i)
  473. for (j = 0; j < expected_nr_events[i]; ++j) {
  474. int foo = syscalls[i]();
  475. ++foo;
  476. }
  477. while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
  478. struct perf_sample sample;
  479. if (event->header.type != PERF_RECORD_SAMPLE) {
  480. pr_debug("unexpected %s event\n",
  481. perf_event__name(event->header.type));
  482. goto out_munmap;
  483. }
  484. err = perf_evlist__parse_sample(evlist, event, &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. perf_evsel__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. static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
  521. size_t *sizep)
  522. {
  523. cpu_set_t *mask;
  524. size_t size;
  525. int i, cpu = -1, nrcpus = 1024;
  526. realloc:
  527. mask = CPU_ALLOC(nrcpus);
  528. size = CPU_ALLOC_SIZE(nrcpus);
  529. CPU_ZERO_S(size, mask);
  530. if (sched_getaffinity(pid, size, mask) == -1) {
  531. CPU_FREE(mask);
  532. if (errno == EINVAL && nrcpus < (1024 << 8)) {
  533. nrcpus = nrcpus << 2;
  534. goto realloc;
  535. }
  536. perror("sched_getaffinity");
  537. return -1;
  538. }
  539. for (i = 0; i < nrcpus; i++) {
  540. if (CPU_ISSET_S(i, size, mask)) {
  541. if (cpu == -1) {
  542. cpu = i;
  543. *maskp = mask;
  544. *sizep = size;
  545. } else
  546. CPU_CLR_S(i, size, mask);
  547. }
  548. }
  549. if (cpu == -1)
  550. CPU_FREE(mask);
  551. return cpu;
  552. }
  553. static int test__PERF_RECORD(void)
  554. {
  555. struct perf_record_opts opts = {
  556. .target = {
  557. .uid = UINT_MAX,
  558. .uses_mmap = true,
  559. },
  560. .no_delay = true,
  561. .freq = 10,
  562. .mmap_pages = 256,
  563. };
  564. cpu_set_t *cpu_mask = NULL;
  565. size_t cpu_mask_size = 0;
  566. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  567. struct perf_evsel *evsel;
  568. struct perf_sample sample;
  569. const char *cmd = "sleep";
  570. const char *argv[] = { cmd, "1", NULL, };
  571. char *bname;
  572. u64 prev_time = 0;
  573. bool found_cmd_mmap = false,
  574. found_libc_mmap = false,
  575. found_vdso_mmap = false,
  576. found_ld_mmap = false;
  577. int err = -1, errs = 0, i, wakeups = 0;
  578. u32 cpu;
  579. int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
  580. if (evlist == NULL || argv == NULL) {
  581. pr_debug("Not enough memory to create evlist\n");
  582. goto out;
  583. }
  584. /*
  585. * We need at least one evsel in the evlist, use the default
  586. * one: "cycles".
  587. */
  588. err = perf_evlist__add_default(evlist);
  589. if (err < 0) {
  590. pr_debug("Not enough memory to create evsel\n");
  591. goto out_delete_evlist;
  592. }
  593. /*
  594. * Create maps of threads and cpus to monitor. In this case
  595. * we start with all threads and cpus (-1, -1) but then in
  596. * perf_evlist__prepare_workload we'll fill in the only thread
  597. * we're monitoring, the one forked there.
  598. */
  599. err = perf_evlist__create_maps(evlist, &opts.target);
  600. if (err < 0) {
  601. pr_debug("Not enough memory to create thread/cpu maps\n");
  602. goto out_delete_evlist;
  603. }
  604. /*
  605. * Prepare the workload in argv[] to run, it'll fork it, and then wait
  606. * for perf_evlist__start_workload() to exec it. This is done this way
  607. * so that we have time to open the evlist (calling sys_perf_event_open
  608. * on all the fds) and then mmap them.
  609. */
  610. err = perf_evlist__prepare_workload(evlist, &opts, argv);
  611. if (err < 0) {
  612. pr_debug("Couldn't run the workload!\n");
  613. goto out_delete_evlist;
  614. }
  615. /*
  616. * Config the evsels, setting attr->comm on the first one, etc.
  617. */
  618. evsel = perf_evlist__first(evlist);
  619. evsel->attr.sample_type |= PERF_SAMPLE_CPU;
  620. evsel->attr.sample_type |= PERF_SAMPLE_TID;
  621. evsel->attr.sample_type |= PERF_SAMPLE_TIME;
  622. perf_evlist__config_attrs(evlist, &opts);
  623. err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask,
  624. &cpu_mask_size);
  625. if (err < 0) {
  626. pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
  627. goto out_delete_evlist;
  628. }
  629. cpu = err;
  630. /*
  631. * So that we can check perf_sample.cpu on all the samples.
  632. */
  633. if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
  634. pr_debug("sched_setaffinity: %s\n", strerror(errno));
  635. goto out_free_cpu_mask;
  636. }
  637. /*
  638. * Call sys_perf_event_open on all the fds on all the evsels,
  639. * grouping them if asked to.
  640. */
  641. err = perf_evlist__open(evlist);
  642. if (err < 0) {
  643. pr_debug("perf_evlist__open: %s\n", strerror(errno));
  644. goto out_delete_evlist;
  645. }
  646. /*
  647. * mmap the first fd on a given CPU and ask for events for the other
  648. * fds in the same CPU to be injected in the same mmap ring buffer
  649. * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
  650. */
  651. err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
  652. if (err < 0) {
  653. pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
  654. goto out_delete_evlist;
  655. }
  656. /*
  657. * Now that all is properly set up, enable the events, they will
  658. * count just on workload.pid, which will start...
  659. */
  660. perf_evlist__enable(evlist);
  661. /*
  662. * Now!
  663. */
  664. perf_evlist__start_workload(evlist);
  665. while (1) {
  666. int before = total_events;
  667. for (i = 0; i < evlist->nr_mmaps; i++) {
  668. union perf_event *event;
  669. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  670. const u32 type = event->header.type;
  671. const char *name = perf_event__name(type);
  672. ++total_events;
  673. if (type < PERF_RECORD_MAX)
  674. nr_events[type]++;
  675. err = perf_evlist__parse_sample(evlist, event, &sample, false);
  676. if (err < 0) {
  677. if (verbose)
  678. perf_event__fprintf(event, stderr);
  679. pr_debug("Couldn't parse sample\n");
  680. goto out_err;
  681. }
  682. if (verbose) {
  683. pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
  684. perf_event__fprintf(event, stderr);
  685. }
  686. if (prev_time > sample.time) {
  687. pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
  688. name, prev_time, sample.time);
  689. ++errs;
  690. }
  691. prev_time = sample.time;
  692. if (sample.cpu != cpu) {
  693. pr_debug("%s with unexpected cpu, expected %d, got %d\n",
  694. name, cpu, sample.cpu);
  695. ++errs;
  696. }
  697. if ((pid_t)sample.pid != evlist->workload.pid) {
  698. pr_debug("%s with unexpected pid, expected %d, got %d\n",
  699. name, evlist->workload.pid, sample.pid);
  700. ++errs;
  701. }
  702. if ((pid_t)sample.tid != evlist->workload.pid) {
  703. pr_debug("%s with unexpected tid, expected %d, got %d\n",
  704. name, evlist->workload.pid, sample.tid);
  705. ++errs;
  706. }
  707. if ((type == PERF_RECORD_COMM ||
  708. type == PERF_RECORD_MMAP ||
  709. type == PERF_RECORD_FORK ||
  710. type == PERF_RECORD_EXIT) &&
  711. (pid_t)event->comm.pid != evlist->workload.pid) {
  712. pr_debug("%s with unexpected pid/tid\n", name);
  713. ++errs;
  714. }
  715. if ((type == PERF_RECORD_COMM ||
  716. type == PERF_RECORD_MMAP) &&
  717. event->comm.pid != event->comm.tid) {
  718. pr_debug("%s with different pid/tid!\n", name);
  719. ++errs;
  720. }
  721. switch (type) {
  722. case PERF_RECORD_COMM:
  723. if (strcmp(event->comm.comm, cmd)) {
  724. pr_debug("%s with unexpected comm!\n", name);
  725. ++errs;
  726. }
  727. break;
  728. case PERF_RECORD_EXIT:
  729. goto found_exit;
  730. case PERF_RECORD_MMAP:
  731. bname = strrchr(event->mmap.filename, '/');
  732. if (bname != NULL) {
  733. if (!found_cmd_mmap)
  734. found_cmd_mmap = !strcmp(bname + 1, cmd);
  735. if (!found_libc_mmap)
  736. found_libc_mmap = !strncmp(bname + 1, "libc", 4);
  737. if (!found_ld_mmap)
  738. found_ld_mmap = !strncmp(bname + 1, "ld", 2);
  739. } else if (!found_vdso_mmap)
  740. found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
  741. break;
  742. case PERF_RECORD_SAMPLE:
  743. /* Just ignore samples for now */
  744. break;
  745. default:
  746. pr_debug("Unexpected perf_event->header.type %d!\n",
  747. type);
  748. ++errs;
  749. }
  750. }
  751. }
  752. /*
  753. * We don't use poll here because at least at 3.1 times the
  754. * PERF_RECORD_{!SAMPLE} events don't honour
  755. * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
  756. */
  757. if (total_events == before && false)
  758. poll(evlist->pollfd, evlist->nr_fds, -1);
  759. sleep(1);
  760. if (++wakeups > 5) {
  761. pr_debug("No PERF_RECORD_EXIT event!\n");
  762. break;
  763. }
  764. }
  765. found_exit:
  766. if (nr_events[PERF_RECORD_COMM] > 1) {
  767. pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
  768. ++errs;
  769. }
  770. if (nr_events[PERF_RECORD_COMM] == 0) {
  771. pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
  772. ++errs;
  773. }
  774. if (!found_cmd_mmap) {
  775. pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
  776. ++errs;
  777. }
  778. if (!found_libc_mmap) {
  779. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
  780. ++errs;
  781. }
  782. if (!found_ld_mmap) {
  783. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
  784. ++errs;
  785. }
  786. if (!found_vdso_mmap) {
  787. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
  788. ++errs;
  789. }
  790. out_err:
  791. perf_evlist__munmap(evlist);
  792. out_free_cpu_mask:
  793. CPU_FREE(cpu_mask);
  794. out_delete_evlist:
  795. perf_evlist__delete(evlist);
  796. out:
  797. return (err < 0 || errs > 0) ? -1 : 0;
  798. }
  799. #if defined(__x86_64__) || defined(__i386__)
  800. #define barrier() asm volatile("" ::: "memory")
  801. static u64 rdpmc(unsigned int counter)
  802. {
  803. unsigned int low, high;
  804. asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter));
  805. return low | ((u64)high) << 32;
  806. }
  807. static u64 rdtsc(void)
  808. {
  809. unsigned int low, high;
  810. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  811. return low | ((u64)high) << 32;
  812. }
  813. static u64 mmap_read_self(void *addr)
  814. {
  815. struct perf_event_mmap_page *pc = addr;
  816. u32 seq, idx, time_mult = 0, time_shift = 0;
  817. u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
  818. do {
  819. seq = pc->lock;
  820. barrier();
  821. enabled = pc->time_enabled;
  822. running = pc->time_running;
  823. if (enabled != running) {
  824. cyc = rdtsc();
  825. time_mult = pc->time_mult;
  826. time_shift = pc->time_shift;
  827. time_offset = pc->time_offset;
  828. }
  829. idx = pc->index;
  830. count = pc->offset;
  831. if (idx)
  832. count += rdpmc(idx - 1);
  833. barrier();
  834. } while (pc->lock != seq);
  835. if (enabled != running) {
  836. u64 quot, rem;
  837. quot = (cyc >> time_shift);
  838. rem = cyc & ((1 << time_shift) - 1);
  839. delta = time_offset + quot * time_mult +
  840. ((rem * time_mult) >> time_shift);
  841. enabled += delta;
  842. if (idx)
  843. running += delta;
  844. quot = count / running;
  845. rem = count % running;
  846. count = quot * enabled + (rem * enabled) / running;
  847. }
  848. return count;
  849. }
  850. /*
  851. * If the RDPMC instruction faults then signal this back to the test parent task:
  852. */
  853. static void segfault_handler(int sig __used, siginfo_t *info __used, void *uc __used)
  854. {
  855. exit(-1);
  856. }
  857. static int __test__rdpmc(void)
  858. {
  859. long page_size = sysconf(_SC_PAGE_SIZE);
  860. volatile int tmp = 0;
  861. u64 i, loops = 1000;
  862. int n;
  863. int fd;
  864. void *addr;
  865. struct perf_event_attr attr = {
  866. .type = PERF_TYPE_HARDWARE,
  867. .config = PERF_COUNT_HW_INSTRUCTIONS,
  868. .exclude_kernel = 1,
  869. };
  870. u64 delta_sum = 0;
  871. struct sigaction sa;
  872. sigfillset(&sa.sa_mask);
  873. sa.sa_sigaction = segfault_handler;
  874. sigaction(SIGSEGV, &sa, NULL);
  875. fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
  876. if (fd < 0) {
  877. pr_debug("Error: sys_perf_event_open() syscall returned "
  878. "with %d (%s)\n", fd, strerror(errno));
  879. return -1;
  880. }
  881. addr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
  882. if (addr == (void *)(-1)) {
  883. pr_debug("Error: mmap() syscall returned with (%s)\n",
  884. strerror(errno));
  885. goto out_close;
  886. }
  887. for (n = 0; n < 6; n++) {
  888. u64 stamp, now, delta;
  889. stamp = mmap_read_self(addr);
  890. for (i = 0; i < loops; i++)
  891. tmp++;
  892. now = mmap_read_self(addr);
  893. loops *= 10;
  894. delta = now - stamp;
  895. pr_debug("%14d: %14Lu\n", n, (long long)delta);
  896. delta_sum += delta;
  897. }
  898. munmap(addr, page_size);
  899. pr_debug(" ");
  900. out_close:
  901. close(fd);
  902. if (!delta_sum)
  903. return -1;
  904. return 0;
  905. }
  906. static int test__rdpmc(void)
  907. {
  908. int status = 0;
  909. int wret = 0;
  910. int ret;
  911. int pid;
  912. pid = fork();
  913. if (pid < 0)
  914. return -1;
  915. if (!pid) {
  916. ret = __test__rdpmc();
  917. exit(ret);
  918. }
  919. wret = waitpid(pid, &status, 0);
  920. if (wret < 0 || status)
  921. return -1;
  922. return 0;
  923. }
  924. #endif
  925. static int test__perf_pmu(void)
  926. {
  927. return perf_pmu__test();
  928. }
  929. static int perf_evsel__roundtrip_cache_name_test(void)
  930. {
  931. char name[128];
  932. int type, op, err = 0, ret = 0, i, idx;
  933. struct perf_evsel *evsel;
  934. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  935. if (evlist == NULL)
  936. return -ENOMEM;
  937. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  938. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  939. /* skip invalid cache type */
  940. if (!perf_evsel__is_cache_op_valid(type, op))
  941. continue;
  942. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  943. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  944. name, sizeof(name));
  945. err = parse_events(evlist, name, 0);
  946. if (err)
  947. ret = err;
  948. }
  949. }
  950. }
  951. idx = 0;
  952. evsel = perf_evlist__first(evlist);
  953. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  954. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  955. /* skip invalid cache type */
  956. if (!perf_evsel__is_cache_op_valid(type, op))
  957. continue;
  958. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  959. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  960. name, sizeof(name));
  961. if (evsel->idx != idx)
  962. continue;
  963. ++idx;
  964. if (strcmp(perf_evsel__name(evsel), name)) {
  965. pr_debug("%s != %s\n", perf_evsel__name(evsel), name);
  966. ret = -1;
  967. }
  968. evsel = perf_evsel__next(evsel);
  969. }
  970. }
  971. }
  972. perf_evlist__delete(evlist);
  973. return ret;
  974. }
  975. static int __perf_evsel__name_array_test(const char *names[], int nr_names)
  976. {
  977. int i, err;
  978. struct perf_evsel *evsel;
  979. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  980. if (evlist == NULL)
  981. return -ENOMEM;
  982. for (i = 0; i < nr_names; ++i) {
  983. err = parse_events(evlist, names[i], 0);
  984. if (err) {
  985. pr_debug("failed to parse event '%s', err %d\n",
  986. names[i], err);
  987. goto out_delete_evlist;
  988. }
  989. }
  990. err = 0;
  991. list_for_each_entry(evsel, &evlist->entries, node) {
  992. if (strcmp(perf_evsel__name(evsel), names[evsel->idx])) {
  993. --err;
  994. pr_debug("%s != %s\n", perf_evsel__name(evsel), names[evsel->idx]);
  995. }
  996. }
  997. out_delete_evlist:
  998. perf_evlist__delete(evlist);
  999. return err;
  1000. }
  1001. #define perf_evsel__name_array_test(names) \
  1002. __perf_evsel__name_array_test(names, ARRAY_SIZE(names))
  1003. static int perf_evsel__roundtrip_name_test(void)
  1004. {
  1005. int err = 0, ret = 0;
  1006. err = perf_evsel__name_array_test(perf_evsel__hw_names);
  1007. if (err)
  1008. ret = err;
  1009. err = perf_evsel__name_array_test(perf_evsel__sw_names);
  1010. if (err)
  1011. ret = err;
  1012. err = perf_evsel__roundtrip_cache_name_test();
  1013. if (err)
  1014. ret = err;
  1015. return ret;
  1016. }
  1017. static struct test {
  1018. const char *desc;
  1019. int (*func)(void);
  1020. } tests[] = {
  1021. {
  1022. .desc = "vmlinux symtab matches kallsyms",
  1023. .func = test__vmlinux_matches_kallsyms,
  1024. },
  1025. {
  1026. .desc = "detect open syscall event",
  1027. .func = test__open_syscall_event,
  1028. },
  1029. {
  1030. .desc = "detect open syscall event on all cpus",
  1031. .func = test__open_syscall_event_on_all_cpus,
  1032. },
  1033. {
  1034. .desc = "read samples using the mmap interface",
  1035. .func = test__basic_mmap,
  1036. },
  1037. {
  1038. .desc = "parse events tests",
  1039. .func = parse_events__test,
  1040. },
  1041. #if defined(__x86_64__) || defined(__i386__)
  1042. {
  1043. .desc = "x86 rdpmc test",
  1044. .func = test__rdpmc,
  1045. },
  1046. #endif
  1047. {
  1048. .desc = "Validate PERF_RECORD_* events & perf_sample fields",
  1049. .func = test__PERF_RECORD,
  1050. },
  1051. {
  1052. .desc = "Test perf pmu format parsing",
  1053. .func = test__perf_pmu,
  1054. },
  1055. {
  1056. .desc = "Test dso data interface",
  1057. .func = dso__test_data,
  1058. },
  1059. {
  1060. .desc = "roundtrip evsel->name check",
  1061. .func = perf_evsel__roundtrip_name_test,
  1062. },
  1063. {
  1064. .func = NULL,
  1065. },
  1066. };
  1067. static bool perf_test__matches(int curr, int argc, const char *argv[])
  1068. {
  1069. int i;
  1070. if (argc == 0)
  1071. return true;
  1072. for (i = 0; i < argc; ++i) {
  1073. char *end;
  1074. long nr = strtoul(argv[i], &end, 10);
  1075. if (*end == '\0') {
  1076. if (nr == curr + 1)
  1077. return true;
  1078. continue;
  1079. }
  1080. if (strstr(tests[curr].desc, argv[i]))
  1081. return true;
  1082. }
  1083. return false;
  1084. }
  1085. static int __cmd_test(int argc, const char *argv[])
  1086. {
  1087. int i = 0;
  1088. while (tests[i].func) {
  1089. int curr = i++, err;
  1090. if (!perf_test__matches(curr, argc, argv))
  1091. continue;
  1092. pr_info("%2d: %s:", i, tests[curr].desc);
  1093. pr_debug("\n--- start ---\n");
  1094. err = tests[curr].func();
  1095. pr_debug("---- end ----\n%s:", tests[curr].desc);
  1096. pr_info(" %s\n", err ? "FAILED!\n" : "Ok");
  1097. }
  1098. return 0;
  1099. }
  1100. static int perf_test__list(int argc, const char **argv)
  1101. {
  1102. int i = 0;
  1103. while (tests[i].func) {
  1104. int curr = i++;
  1105. if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
  1106. continue;
  1107. pr_info("%2d: %s\n", i, tests[curr].desc);
  1108. }
  1109. return 0;
  1110. }
  1111. int cmd_test(int argc, const char **argv, const char *prefix __used)
  1112. {
  1113. const char * const test_usage[] = {
  1114. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  1115. NULL,
  1116. };
  1117. const struct option test_options[] = {
  1118. OPT_INCR('v', "verbose", &verbose,
  1119. "be more verbose (show symbol address, etc)"),
  1120. OPT_END()
  1121. };
  1122. argc = parse_options(argc, argv, test_options, test_usage, 0);
  1123. if (argc >= 1 && !strcmp(argv[0], "list"))
  1124. return perf_test__list(argc, argv);
  1125. symbol_conf.priv_size = sizeof(int);
  1126. symbol_conf.sort_by_name = true;
  1127. symbol_conf.try_vmlinux_path = true;
  1128. if (symbol__init() < 0)
  1129. return -1;
  1130. return __cmd_test(argc, argv);
  1131. }