perf-record.c 8.9 KB

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