perf-record.c 7.9 KB

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