perf-record.c 8.0 KB

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