perf-record.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <linux/string.h>
  5. /* For the CLR_() macros */
  6. #include <pthread.h>
  7. #include <sched.h>
  8. #include <perf/mmap.h>
  9. #include "evlist.h"
  10. #include "evsel.h"
  11. #include "debug.h"
  12. #include "record.h"
  13. #include "tests.h"
  14. #include "util/mmap.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. int test__PERF_RECORD(struct test *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 = perf_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 = perf_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. * perf_evlist__prepare_workload we'll fill in the only thread
  76. * we're monitoring, the one forked there.
  77. */
  78. err = perf_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 perf_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 = perf_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. perf_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. goto out_delete_evlist;
  107. }
  108. cpu = err;
  109. /*
  110. * So that we can check perf_sample.cpu on all the samples.
  111. */
  112. if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
  113. pr_debug("sched_setaffinity: %s\n",
  114. str_error_r(errno, sbuf, sizeof(sbuf)));
  115. goto out_delete_evlist;
  116. }
  117. /*
  118. * Call sys_perf_event_open on all the fds on all the evsels,
  119. * grouping them if asked to.
  120. */
  121. err = evlist__open(evlist);
  122. if (err < 0) {
  123. pr_debug("perf_evlist__open: %s\n",
  124. str_error_r(errno, sbuf, sizeof(sbuf)));
  125. goto out_delete_evlist;
  126. }
  127. /*
  128. * mmap the first fd on a given CPU and ask for events for the other
  129. * fds in the same CPU to be injected in the same mmap ring buffer
  130. * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
  131. */
  132. err = evlist__mmap(evlist, opts.mmap_pages);
  133. if (err < 0) {
  134. pr_debug("evlist__mmap: %s\n",
  135. str_error_r(errno, sbuf, sizeof(sbuf)));
  136. goto out_delete_evlist;
  137. }
  138. /*
  139. * Now that all is properly set up, enable the events, they will
  140. * count just on workload.pid, which will start...
  141. */
  142. evlist__enable(evlist);
  143. /*
  144. * Now!
  145. */
  146. perf_evlist__start_workload(evlist);
  147. while (1) {
  148. int before = total_events;
  149. for (i = 0; i < evlist->core.nr_mmaps; i++) {
  150. union perf_event *event;
  151. struct mmap *md;
  152. md = &evlist->mmap[i];
  153. if (perf_mmap__read_init(&md->core) < 0)
  154. continue;
  155. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  156. const u32 type = event->header.type;
  157. const char *name = perf_event__name(type);
  158. ++total_events;
  159. if (type < PERF_RECORD_MAX)
  160. nr_events[type]++;
  161. err = perf_evlist__parse_sample(evlist, event, &sample);
  162. if (err < 0) {
  163. if (verbose > 0)
  164. perf_event__fprintf(event, NULL, stderr);
  165. pr_debug("Couldn't parse sample\n");
  166. goto out_delete_evlist;
  167. }
  168. if (verbose > 0) {
  169. pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
  170. perf_event__fprintf(event, NULL, stderr);
  171. }
  172. if (prev_time > sample.time) {
  173. pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
  174. name, prev_time, sample.time);
  175. ++errs;
  176. }
  177. prev_time = sample.time;
  178. if (sample.cpu != cpu) {
  179. pr_debug("%s with unexpected cpu, expected %d, got %d\n",
  180. name, cpu, sample.cpu);
  181. ++errs;
  182. }
  183. if ((pid_t)sample.pid != evlist->workload.pid) {
  184. pr_debug("%s with unexpected pid, expected %d, got %d\n",
  185. name, evlist->workload.pid, sample.pid);
  186. ++errs;
  187. }
  188. if ((pid_t)sample.tid != evlist->workload.pid) {
  189. pr_debug("%s with unexpected tid, expected %d, got %d\n",
  190. name, evlist->workload.pid, sample.tid);
  191. ++errs;
  192. }
  193. if ((type == PERF_RECORD_COMM ||
  194. type == PERF_RECORD_MMAP ||
  195. type == PERF_RECORD_MMAP2 ||
  196. type == PERF_RECORD_FORK ||
  197. type == PERF_RECORD_EXIT) &&
  198. (pid_t)event->comm.pid != evlist->workload.pid) {
  199. pr_debug("%s with unexpected pid/tid\n", name);
  200. ++errs;
  201. }
  202. if ((type == PERF_RECORD_COMM ||
  203. type == PERF_RECORD_MMAP ||
  204. type == PERF_RECORD_MMAP2) &&
  205. event->comm.pid != event->comm.tid) {
  206. pr_debug("%s with different pid/tid!\n", name);
  207. ++errs;
  208. }
  209. switch (type) {
  210. case PERF_RECORD_COMM:
  211. if (strcmp(event->comm.comm, cmd)) {
  212. pr_debug("%s with unexpected comm!\n", name);
  213. ++errs;
  214. }
  215. break;
  216. case PERF_RECORD_EXIT:
  217. goto found_exit;
  218. case PERF_RECORD_MMAP:
  219. mmap_filename = event->mmap.filename;
  220. goto check_bname;
  221. case PERF_RECORD_MMAP2:
  222. mmap_filename = event->mmap2.filename;
  223. check_bname:
  224. bname = strrchr(mmap_filename, '/');
  225. if (bname != NULL) {
  226. if (!found_cmd_mmap)
  227. found_cmd_mmap = !strcmp(bname + 1, cmd);
  228. if (!found_coreutils_mmap)
  229. found_coreutils_mmap = !strcmp(bname + 1, "coreutils");
  230. if (!found_libc_mmap)
  231. found_libc_mmap = !strncmp(bname + 1, "libc", 4);
  232. if (!found_ld_mmap)
  233. found_ld_mmap = !strncmp(bname + 1, "ld", 2);
  234. } else if (!found_vdso_mmap)
  235. found_vdso_mmap = !strcmp(mmap_filename, "[vdso]");
  236. break;
  237. case PERF_RECORD_SAMPLE:
  238. /* Just ignore samples for now */
  239. break;
  240. default:
  241. pr_debug("Unexpected perf_event->header.type %d!\n",
  242. type);
  243. ++errs;
  244. }
  245. perf_mmap__consume(&md->core);
  246. }
  247. perf_mmap__read_done(&md->core);
  248. }
  249. /*
  250. * We don't use poll here because at least at 3.1 times the
  251. * PERF_RECORD_{!SAMPLE} events don't honour
  252. * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
  253. */
  254. if (total_events == before && false)
  255. evlist__poll(evlist, -1);
  256. sleep(1);
  257. if (++wakeups > 5) {
  258. pr_debug("No PERF_RECORD_EXIT event!\n");
  259. break;
  260. }
  261. }
  262. found_exit:
  263. if (nr_events[PERF_RECORD_COMM] > 1 + !!found_coreutils_mmap) {
  264. pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
  265. ++errs;
  266. }
  267. if (nr_events[PERF_RECORD_COMM] == 0) {
  268. pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
  269. ++errs;
  270. }
  271. if (!found_cmd_mmap && !found_coreutils_mmap) {
  272. pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
  273. ++errs;
  274. }
  275. if (!found_libc_mmap) {
  276. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
  277. ++errs;
  278. }
  279. if (!found_ld_mmap) {
  280. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
  281. ++errs;
  282. }
  283. if (!found_vdso_mmap) {
  284. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
  285. ++errs;
  286. }
  287. out_delete_evlist:
  288. evlist__delete(evlist);
  289. out:
  290. return (err < 0 || errs > 0) ? -1 : 0;
  291. }