perf-record.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <linux/string.h>
  5. #include <sched.h>
  6. #include <perf/mmap.h>
  7. #include "event.h"
  8. #include "evlist.h"
  9. #include "evsel.h"
  10. #include "debug.h"
  11. #include "record.h"
  12. #include "tests.h"
  13. #include "util/mmap.h"
  14. #include "util/sample.h"
  15. #include "util/cpumap.h"
  16. static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
  17. {
  18. int i, cpu = -1;
  19. int nrcpus = cpu__max_cpu().cpu;
  20. size_t size = CPU_ALLOC_SIZE(nrcpus);
  21. realloc:
  22. CPU_ZERO_S(size, maskp);
  23. if (sched_getaffinity(pid, size, maskp) == -1) {
  24. if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) {
  25. nrcpus = nrcpus << 2;
  26. goto realloc;
  27. }
  28. perror("sched_getaffinity");
  29. return -1;
  30. }
  31. for (i = 0; i < nrcpus; i++) {
  32. if (CPU_ISSET_S(i, size, maskp)) {
  33. if (cpu == -1)
  34. cpu = i;
  35. else
  36. CPU_CLR_S(i, size, maskp);
  37. }
  38. }
  39. return cpu;
  40. }
  41. static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  42. {
  43. struct record_opts opts = {
  44. .target = {
  45. .uses_mmap = true,
  46. },
  47. .no_buffering = true,
  48. .mmap_pages = 256,
  49. };
  50. int nrcpus = cpu__max_cpu().cpu;
  51. cpu_set_t *cpu_mask;
  52. size_t cpu_mask_size;
  53. struct evlist *evlist = evlist__new_dummy();
  54. struct evsel *evsel;
  55. struct perf_sample sample;
  56. const char *cmd = "sleep";
  57. const char *argv[] = { cmd, "1", NULL, };
  58. char *bname, *mmap_filename;
  59. u64 prev_time = 0;
  60. bool found_cmd_mmap = false,
  61. found_coreutils_mmap = false,
  62. found_libc_mmap = false,
  63. found_vdso_mmap = false,
  64. found_ld_mmap = false;
  65. int err = -1, errs = 0, i, wakeups = 0;
  66. u32 cpu;
  67. int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
  68. char sbuf[STRERR_BUFSIZE];
  69. cpu_mask = CPU_ALLOC(nrcpus);
  70. if (!cpu_mask) {
  71. pr_debug("failed to create cpumask\n");
  72. goto out;
  73. }
  74. cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
  75. CPU_ZERO_S(cpu_mask_size, cpu_mask);
  76. perf_sample__init(&sample, /*all=*/false);
  77. if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */
  78. evlist = evlist__new_default();
  79. if (evlist == NULL) {
  80. pr_debug("Not enough memory to create evlist\n");
  81. CPU_FREE(cpu_mask);
  82. goto out;
  83. }
  84. /*
  85. * Create maps of threads and cpus to monitor. In this case
  86. * we start with all threads and cpus (-1, -1) but then in
  87. * evlist__prepare_workload we'll fill in the only thread
  88. * we're monitoring, the one forked there.
  89. */
  90. err = evlist__create_maps(evlist, &opts.target);
  91. if (err < 0) {
  92. pr_debug("Not enough memory to create thread/cpu maps\n");
  93. goto out_delete_evlist;
  94. }
  95. /*
  96. * Prepare the workload in argv[] to run, it'll fork it, and then wait
  97. * for evlist__start_workload() to exec it. This is done this way
  98. * so that we have time to open the evlist (calling sys_perf_event_open
  99. * on all the fds) and then mmap them.
  100. */
  101. err = evlist__prepare_workload(evlist, &opts.target, argv, false, NULL);
  102. if (err < 0) {
  103. pr_debug("Couldn't run the workload!\n");
  104. goto out_delete_evlist;
  105. }
  106. /*
  107. * Config the evsels, setting attr->comm on the first one, etc.
  108. */
  109. evsel = evlist__first(evlist);
  110. evsel__set_sample_bit(evsel, CPU);
  111. evsel__set_sample_bit(evsel, TID);
  112. evsel__set_sample_bit(evsel, TIME);
  113. evlist__config(evlist, &opts, NULL);
  114. err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
  115. if (err < 0) {
  116. pr_debug("sched__get_first_possible_cpu: %s\n",
  117. str_error_r(errno, sbuf, sizeof(sbuf)));
  118. evlist__cancel_workload(evlist);
  119. goto out_delete_evlist;
  120. }
  121. cpu = err;
  122. /*
  123. * So that we can check perf_sample.cpu on all the samples.
  124. */
  125. if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
  126. pr_debug("sched_setaffinity: %s\n",
  127. str_error_r(errno, sbuf, sizeof(sbuf)));
  128. evlist__cancel_workload(evlist);
  129. goto out_delete_evlist;
  130. }
  131. /*
  132. * Call sys_perf_event_open on all the fds on all the evsels,
  133. * grouping them if asked to.
  134. */
  135. err = evlist__open(evlist);
  136. if (err < 0) {
  137. pr_debug("perf_evlist__open: %s\n",
  138. str_error_r(errno, sbuf, sizeof(sbuf)));
  139. evlist__cancel_workload(evlist);
  140. goto out_delete_evlist;
  141. }
  142. /*
  143. * mmap the first fd on a given CPU and ask for events for the other
  144. * fds in the same CPU to be injected in the same mmap ring buffer
  145. * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
  146. */
  147. err = evlist__mmap(evlist, opts.mmap_pages);
  148. if (err < 0) {
  149. pr_debug("evlist__mmap: %s\n",
  150. str_error_r(errno, sbuf, sizeof(sbuf)));
  151. evlist__cancel_workload(evlist);
  152. goto out_delete_evlist;
  153. }
  154. /*
  155. * Now that all is properly set up, enable the events, they will
  156. * count just on workload.pid, which will start...
  157. */
  158. evlist__enable(evlist);
  159. /*
  160. * Now!
  161. */
  162. evlist__start_workload(evlist);
  163. while (1) {
  164. int before = total_events;
  165. for (i = 0; i < evlist->core.nr_mmaps; i++) {
  166. union perf_event *event;
  167. struct mmap *md;
  168. md = &evlist->mmap[i];
  169. if (perf_mmap__read_init(&md->core) < 0)
  170. continue;
  171. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  172. const u32 type = event->header.type;
  173. const char *name = perf_event__name(type);
  174. ++total_events;
  175. if (type < PERF_RECORD_MAX)
  176. nr_events[type]++;
  177. err = evlist__parse_sample(evlist, event, &sample);
  178. if (err < 0) {
  179. if (verbose > 0)
  180. perf_event__fprintf(event, NULL, stderr);
  181. pr_debug("Couldn't parse sample\n");
  182. goto out_delete_evlist;
  183. }
  184. if (verbose > 0) {
  185. pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
  186. perf_event__fprintf(event, NULL, stderr);
  187. }
  188. if (prev_time > sample.time) {
  189. pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
  190. name, prev_time, sample.time);
  191. ++errs;
  192. }
  193. prev_time = sample.time;
  194. if (sample.cpu != cpu) {
  195. pr_debug("%s with unexpected cpu, expected %d, got %d\n",
  196. name, cpu, sample.cpu);
  197. ++errs;
  198. }
  199. if ((pid_t)sample.pid != evlist->workload.pid) {
  200. pr_debug("%s with unexpected pid, expected %d, got %d\n",
  201. name, evlist->workload.pid, sample.pid);
  202. ++errs;
  203. }
  204. if ((pid_t)sample.tid != evlist->workload.pid) {
  205. pr_debug("%s with unexpected tid, expected %d, got %d\n",
  206. name, evlist->workload.pid, sample.tid);
  207. ++errs;
  208. }
  209. if ((type == PERF_RECORD_COMM ||
  210. type == PERF_RECORD_MMAP ||
  211. type == PERF_RECORD_MMAP2 ||
  212. type == PERF_RECORD_FORK ||
  213. type == PERF_RECORD_EXIT) &&
  214. (pid_t)event->comm.pid != evlist->workload.pid) {
  215. pr_debug("%s with unexpected pid/tid\n", name);
  216. ++errs;
  217. }
  218. if ((type == PERF_RECORD_COMM ||
  219. type == PERF_RECORD_MMAP ||
  220. type == PERF_RECORD_MMAP2) &&
  221. event->comm.pid != event->comm.tid) {
  222. pr_debug("%s with different pid/tid!\n", name);
  223. ++errs;
  224. }
  225. switch (type) {
  226. case PERF_RECORD_COMM:
  227. if (strcmp(event->comm.comm, cmd)) {
  228. pr_debug("%s with unexpected comm!\n", name);
  229. ++errs;
  230. }
  231. break;
  232. case PERF_RECORD_EXIT:
  233. goto found_exit;
  234. case PERF_RECORD_MMAP:
  235. mmap_filename = event->mmap.filename;
  236. goto check_bname;
  237. case PERF_RECORD_MMAP2:
  238. mmap_filename = event->mmap2.filename;
  239. check_bname:
  240. bname = strrchr(mmap_filename, '/');
  241. if (bname != NULL) {
  242. if (!found_cmd_mmap)
  243. found_cmd_mmap = !strcmp(bname + 1, cmd);
  244. if (!found_coreutils_mmap)
  245. found_coreutils_mmap = !strcmp(bname + 1, "coreutils");
  246. if (!found_libc_mmap)
  247. found_libc_mmap = !strncmp(bname + 1, "libc", 4);
  248. if (!found_ld_mmap)
  249. found_ld_mmap = !strncmp(bname + 1, "ld", 2);
  250. } else if (!found_vdso_mmap)
  251. found_vdso_mmap = !strcmp(mmap_filename, "[vdso]");
  252. break;
  253. case PERF_RECORD_SAMPLE:
  254. /* Just ignore samples for now */
  255. break;
  256. default:
  257. pr_debug("Unexpected perf_event->header.type %d!\n",
  258. type);
  259. ++errs;
  260. }
  261. perf_mmap__consume(&md->core);
  262. }
  263. perf_mmap__read_done(&md->core);
  264. }
  265. /*
  266. * We don't use poll here because at least at 3.1 times the
  267. * PERF_RECORD_{!SAMPLE} events don't honour
  268. * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
  269. */
  270. if (total_events == before && false)
  271. evlist__poll(evlist, -1);
  272. sleep(1);
  273. if (++wakeups > 5) {
  274. pr_debug("No PERF_RECORD_EXIT event!\n");
  275. break;
  276. }
  277. }
  278. found_exit:
  279. if (nr_events[PERF_RECORD_COMM] > 1 + !!found_coreutils_mmap) {
  280. pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
  281. ++errs;
  282. }
  283. if (nr_events[PERF_RECORD_COMM] == 0) {
  284. pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
  285. ++errs;
  286. }
  287. if (!found_cmd_mmap && !found_coreutils_mmap) {
  288. pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
  289. ++errs;
  290. }
  291. if (!found_libc_mmap) {
  292. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
  293. ++errs;
  294. }
  295. if (!found_ld_mmap) {
  296. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
  297. ++errs;
  298. }
  299. if (!found_vdso_mmap) {
  300. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
  301. ++errs;
  302. }
  303. out_delete_evlist:
  304. CPU_FREE(cpu_mask);
  305. evlist__delete(evlist);
  306. out:
  307. perf_sample__exit(&sample);
  308. if (err == -EACCES)
  309. return TEST_SKIP;
  310. if (err < 0 || errs != 0)
  311. return TEST_FAIL;
  312. return TEST_OK;
  313. }
  314. static struct test_case tests__PERF_RECORD[] = {
  315. TEST_CASE_REASON("PERF_RECORD_* events & perf_sample fields",
  316. PERF_RECORD,
  317. "permissions"),
  318. { .name = NULL, }
  319. };
  320. struct test_suite suite__PERF_RECORD = {
  321. .desc = "PERF_RECORD_* events & perf_sample fields",
  322. .test_cases = tests__PERF_RECORD,
  323. };