perf-record.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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(void)
  31. {
  32. struct perf_record_opts opts = {
  33. .target = {
  34. .uid = UINT_MAX,
  35. .uses_mmap = true,
  36. },
  37. .no_delay = true,
  38. .freq = 10,
  39. .mmap_pages = 256,
  40. };
  41. cpu_set_t cpu_mask;
  42. size_t cpu_mask_size = sizeof(cpu_mask);
  43. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  44. struct perf_evsel *evsel;
  45. struct perf_sample sample;
  46. const char *cmd = "sleep";
  47. const char *argv[] = { cmd, "1", NULL, };
  48. char *bname;
  49. u64 prev_time = 0;
  50. bool found_cmd_mmap = false,
  51. found_libc_mmap = false,
  52. found_vdso_mmap = false,
  53. found_ld_mmap = false;
  54. int err = -1, errs = 0, i, wakeups = 0;
  55. u32 cpu;
  56. int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
  57. if (evlist == NULL || argv == NULL) {
  58. pr_debug("Not enough memory to create evlist\n");
  59. goto out;
  60. }
  61. /*
  62. * We need at least one evsel in the evlist, use the default
  63. * one: "cycles".
  64. */
  65. err = perf_evlist__add_default(evlist);
  66. if (err < 0) {
  67. pr_debug("Not enough memory to create evsel\n");
  68. goto out_delete_evlist;
  69. }
  70. /*
  71. * Create maps of threads and cpus to monitor. In this case
  72. * we start with all threads and cpus (-1, -1) but then in
  73. * perf_evlist__prepare_workload we'll fill in the only thread
  74. * we're monitoring, the one forked there.
  75. */
  76. err = perf_evlist__create_maps(evlist, &opts.target);
  77. if (err < 0) {
  78. pr_debug("Not enough memory to create thread/cpu maps\n");
  79. goto out_delete_evlist;
  80. }
  81. /*
  82. * Prepare the workload in argv[] to run, it'll fork it, and then wait
  83. * for perf_evlist__start_workload() to exec it. This is done this way
  84. * so that we have time to open the evlist (calling sys_perf_event_open
  85. * on all the fds) and then mmap them.
  86. */
  87. err = perf_evlist__prepare_workload(evlist, &opts, argv);
  88. if (err < 0) {
  89. pr_debug("Couldn't run the workload!\n");
  90. goto out_delete_evlist;
  91. }
  92. /*
  93. * Config the evsels, setting attr->comm on the first one, etc.
  94. */
  95. evsel = perf_evlist__first(evlist);
  96. evsel->attr.sample_type |= PERF_SAMPLE_CPU;
  97. evsel->attr.sample_type |= PERF_SAMPLE_TID;
  98. evsel->attr.sample_type |= PERF_SAMPLE_TIME;
  99. perf_evlist__config_attrs(evlist, &opts);
  100. err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
  101. if (err < 0) {
  102. pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
  103. goto out_delete_evlist;
  104. }
  105. cpu = err;
  106. /*
  107. * So that we can check perf_sample.cpu on all the samples.
  108. */
  109. if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
  110. pr_debug("sched_setaffinity: %s\n", strerror(errno));
  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", strerror(errno));
  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", strerror(errno));
  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)
  154. perf_event__fprintf(event, stderr);
  155. pr_debug("Couldn't parse sample\n");
  156. goto out_err;
  157. }
  158. if (verbose) {
  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_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. event->comm.pid != event->comm.tid) {
  194. pr_debug("%s with different pid/tid!\n", name);
  195. ++errs;
  196. }
  197. switch (type) {
  198. case PERF_RECORD_COMM:
  199. if (strcmp(event->comm.comm, cmd)) {
  200. pr_debug("%s with unexpected comm!\n", name);
  201. ++errs;
  202. }
  203. break;
  204. case PERF_RECORD_EXIT:
  205. goto found_exit;
  206. case PERF_RECORD_MMAP:
  207. bname = strrchr(event->mmap.filename, '/');
  208. if (bname != NULL) {
  209. if (!found_cmd_mmap)
  210. found_cmd_mmap = !strcmp(bname + 1, cmd);
  211. if (!found_libc_mmap)
  212. found_libc_mmap = !strncmp(bname + 1, "libc", 4);
  213. if (!found_ld_mmap)
  214. found_ld_mmap = !strncmp(bname + 1, "ld", 2);
  215. } else if (!found_vdso_mmap)
  216. found_vdso_mmap = !strcmp(event->mmap.filename, "[vdso]");
  217. break;
  218. case PERF_RECORD_SAMPLE:
  219. /* Just ignore samples for now */
  220. break;
  221. default:
  222. pr_debug("Unexpected perf_event->header.type %d!\n",
  223. type);
  224. ++errs;
  225. }
  226. }
  227. }
  228. /*
  229. * We don't use poll here because at least at 3.1 times the
  230. * PERF_RECORD_{!SAMPLE} events don't honour
  231. * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
  232. */
  233. if (total_events == before && false)
  234. poll(evlist->pollfd, evlist->nr_fds, -1);
  235. sleep(1);
  236. if (++wakeups > 5) {
  237. pr_debug("No PERF_RECORD_EXIT event!\n");
  238. break;
  239. }
  240. }
  241. found_exit:
  242. if (nr_events[PERF_RECORD_COMM] > 1) {
  243. pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
  244. ++errs;
  245. }
  246. if (nr_events[PERF_RECORD_COMM] == 0) {
  247. pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
  248. ++errs;
  249. }
  250. if (!found_cmd_mmap) {
  251. pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
  252. ++errs;
  253. }
  254. if (!found_libc_mmap) {
  255. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
  256. ++errs;
  257. }
  258. if (!found_ld_mmap) {
  259. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
  260. ++errs;
  261. }
  262. if (!found_vdso_mmap) {
  263. pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
  264. ++errs;
  265. }
  266. out_err:
  267. perf_evlist__munmap(evlist);
  268. out_delete_evlist:
  269. perf_evlist__delete(evlist);
  270. out:
  271. return (err < 0 || errs > 0) ? -1 : 0;
  272. }