perf-record.c 8.2 KB

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