perf-record.c 8.0 KB

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