perf-record.c 8.0 KB

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