evlist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Parts came from builtin-{top,stat,record}.c, see those files for further
  5. * copyright notes.
  6. *
  7. * Released under the GPL v2. (and only v2, not any later version)
  8. */
  9. #include <poll.h>
  10. #include "cpumap.h"
  11. #include "thread_map.h"
  12. #include "evlist.h"
  13. #include "evsel.h"
  14. #include "util.h"
  15. #include <sys/mman.h>
  16. #include <linux/bitops.h>
  17. #include <linux/hash.h>
  18. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  19. #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
  20. void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
  21. struct thread_map *threads)
  22. {
  23. int i;
  24. for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
  25. INIT_HLIST_HEAD(&evlist->heads[i]);
  26. INIT_LIST_HEAD(&evlist->entries);
  27. perf_evlist__set_maps(evlist, cpus, threads);
  28. }
  29. struct perf_evlist *perf_evlist__new(struct cpu_map *cpus,
  30. struct thread_map *threads)
  31. {
  32. struct perf_evlist *evlist = zalloc(sizeof(*evlist));
  33. if (evlist != NULL)
  34. perf_evlist__init(evlist, cpus, threads);
  35. return evlist;
  36. }
  37. static void perf_evlist__purge(struct perf_evlist *evlist)
  38. {
  39. struct perf_evsel *pos, *n;
  40. list_for_each_entry_safe(pos, n, &evlist->entries, node) {
  41. list_del_init(&pos->node);
  42. perf_evsel__delete(pos);
  43. }
  44. evlist->nr_entries = 0;
  45. }
  46. void perf_evlist__exit(struct perf_evlist *evlist)
  47. {
  48. free(evlist->mmap);
  49. free(evlist->pollfd);
  50. evlist->mmap = NULL;
  51. evlist->pollfd = NULL;
  52. }
  53. void perf_evlist__delete(struct perf_evlist *evlist)
  54. {
  55. perf_evlist__purge(evlist);
  56. perf_evlist__exit(evlist);
  57. free(evlist);
  58. }
  59. void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
  60. {
  61. list_add_tail(&entry->node, &evlist->entries);
  62. ++evlist->nr_entries;
  63. }
  64. int perf_evlist__add_default(struct perf_evlist *evlist)
  65. {
  66. struct perf_event_attr attr = {
  67. .type = PERF_TYPE_HARDWARE,
  68. .config = PERF_COUNT_HW_CPU_CYCLES,
  69. };
  70. struct perf_evsel *evsel = perf_evsel__new(&attr, 0);
  71. if (evsel == NULL)
  72. return -ENOMEM;
  73. perf_evlist__add(evlist, evsel);
  74. return 0;
  75. }
  76. int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
  77. {
  78. int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries;
  79. evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
  80. return evlist->pollfd != NULL ? 0 : -ENOMEM;
  81. }
  82. void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
  83. {
  84. fcntl(fd, F_SETFL, O_NONBLOCK);
  85. evlist->pollfd[evlist->nr_fds].fd = fd;
  86. evlist->pollfd[evlist->nr_fds].events = POLLIN;
  87. evlist->nr_fds++;
  88. }
  89. static void perf_evlist__id_hash(struct perf_evlist *evlist,
  90. struct perf_evsel *evsel,
  91. int cpu, int thread, u64 id)
  92. {
  93. int hash;
  94. struct perf_sample_id *sid = SID(evsel, cpu, thread);
  95. sid->id = id;
  96. sid->evsel = evsel;
  97. hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
  98. hlist_add_head(&sid->node, &evlist->heads[hash]);
  99. }
  100. void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
  101. int cpu, int thread, u64 id)
  102. {
  103. perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
  104. evsel->id[evsel->ids++] = id;
  105. }
  106. static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
  107. struct perf_evsel *evsel,
  108. int cpu, int thread, int fd)
  109. {
  110. u64 read_data[4] = { 0, };
  111. int id_idx = 1; /* The first entry is the counter value */
  112. if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
  113. read(fd, &read_data, sizeof(read_data)) == -1)
  114. return -1;
  115. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  116. ++id_idx;
  117. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  118. ++id_idx;
  119. perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
  120. return 0;
  121. }
  122. struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
  123. {
  124. struct hlist_head *head;
  125. struct hlist_node *pos;
  126. struct perf_sample_id *sid;
  127. int hash;
  128. if (evlist->nr_entries == 1)
  129. return list_entry(evlist->entries.next, struct perf_evsel, node);
  130. hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
  131. head = &evlist->heads[hash];
  132. hlist_for_each_entry(sid, pos, head, node)
  133. if (sid->id == id)
  134. return sid->evsel;
  135. return NULL;
  136. }
  137. union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
  138. {
  139. /* XXX Move this to perf.c, making it generally available */
  140. unsigned int page_size = sysconf(_SC_PAGE_SIZE);
  141. struct perf_mmap *md = &evlist->mmap[idx];
  142. unsigned int head = perf_mmap__read_head(md);
  143. unsigned int old = md->prev;
  144. unsigned char *data = md->base + page_size;
  145. union perf_event *event = NULL;
  146. if (evlist->overwrite) {
  147. /*
  148. * If we're further behind than half the buffer, there's a chance
  149. * the writer will bite our tail and mess up the samples under us.
  150. *
  151. * If we somehow ended up ahead of the head, we got messed up.
  152. *
  153. * In either case, truncate and restart at head.
  154. */
  155. int diff = head - old;
  156. if (diff > md->mask / 2 || diff < 0) {
  157. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  158. /*
  159. * head points to a known good entry, start there.
  160. */
  161. old = head;
  162. }
  163. }
  164. if (old != head) {
  165. size_t size;
  166. event = (union perf_event *)&data[old & md->mask];
  167. size = event->header.size;
  168. /*
  169. * Event straddles the mmap boundary -- header should always
  170. * be inside due to u64 alignment of output.
  171. */
  172. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  173. unsigned int offset = old;
  174. unsigned int len = min(sizeof(*event), size), cpy;
  175. void *dst = &evlist->event_copy;
  176. do {
  177. cpy = min(md->mask + 1 - (offset & md->mask), len);
  178. memcpy(dst, &data[offset & md->mask], cpy);
  179. offset += cpy;
  180. dst += cpy;
  181. len -= cpy;
  182. } while (len);
  183. event = &evlist->event_copy;
  184. }
  185. old += size;
  186. }
  187. md->prev = old;
  188. if (!evlist->overwrite)
  189. perf_mmap__write_tail(md, old);
  190. return event;
  191. }
  192. void perf_evlist__munmap(struct perf_evlist *evlist)
  193. {
  194. int i;
  195. for (i = 0; i < evlist->nr_mmaps; i++) {
  196. if (evlist->mmap[i].base != NULL) {
  197. munmap(evlist->mmap[i].base, evlist->mmap_len);
  198. evlist->mmap[i].base = NULL;
  199. }
  200. }
  201. free(evlist->mmap);
  202. evlist->mmap = NULL;
  203. }
  204. int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
  205. {
  206. evlist->nr_mmaps = evlist->cpus->nr;
  207. if (evlist->cpus->map[0] == -1)
  208. evlist->nr_mmaps = evlist->threads->nr;
  209. evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
  210. return evlist->mmap != NULL ? 0 : -ENOMEM;
  211. }
  212. static int __perf_evlist__mmap(struct perf_evlist *evlist,
  213. int idx, int prot, int mask, int fd)
  214. {
  215. evlist->mmap[idx].prev = 0;
  216. evlist->mmap[idx].mask = mask;
  217. evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
  218. MAP_SHARED, fd, 0);
  219. if (evlist->mmap[idx].base == MAP_FAILED)
  220. return -1;
  221. perf_evlist__add_pollfd(evlist, fd);
  222. return 0;
  223. }
  224. static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
  225. {
  226. struct perf_evsel *evsel;
  227. int cpu, thread;
  228. for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
  229. int output = -1;
  230. for (thread = 0; thread < evlist->threads->nr; thread++) {
  231. list_for_each_entry(evsel, &evlist->entries, node) {
  232. int fd = FD(evsel, cpu, thread);
  233. if (output == -1) {
  234. output = fd;
  235. if (__perf_evlist__mmap(evlist, cpu,
  236. prot, mask, output) < 0)
  237. goto out_unmap;
  238. } else {
  239. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
  240. goto out_unmap;
  241. }
  242. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  243. perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
  244. goto out_unmap;
  245. }
  246. }
  247. }
  248. return 0;
  249. out_unmap:
  250. for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
  251. if (evlist->mmap[cpu].base != NULL) {
  252. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  253. evlist->mmap[cpu].base = NULL;
  254. }
  255. }
  256. return -1;
  257. }
  258. static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
  259. {
  260. struct perf_evsel *evsel;
  261. int thread;
  262. for (thread = 0; thread < evlist->threads->nr; thread++) {
  263. int output = -1;
  264. list_for_each_entry(evsel, &evlist->entries, node) {
  265. int fd = FD(evsel, 0, thread);
  266. if (output == -1) {
  267. output = fd;
  268. if (__perf_evlist__mmap(evlist, thread,
  269. prot, mask, output) < 0)
  270. goto out_unmap;
  271. } else {
  272. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
  273. goto out_unmap;
  274. }
  275. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  276. perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
  277. goto out_unmap;
  278. }
  279. }
  280. return 0;
  281. out_unmap:
  282. for (thread = 0; thread < evlist->threads->nr; thread++) {
  283. if (evlist->mmap[thread].base != NULL) {
  284. munmap(evlist->mmap[thread].base, evlist->mmap_len);
  285. evlist->mmap[thread].base = NULL;
  286. }
  287. }
  288. return -1;
  289. }
  290. /** perf_evlist__mmap - Create per cpu maps to receive events
  291. *
  292. * @evlist - list of events
  293. * @pages - map length in pages
  294. * @overwrite - overwrite older events?
  295. *
  296. * If overwrite is false the user needs to signal event consuption using:
  297. *
  298. * struct perf_mmap *m = &evlist->mmap[cpu];
  299. * unsigned int head = perf_mmap__read_head(m);
  300. *
  301. * perf_mmap__write_tail(m, head)
  302. *
  303. * Using perf_evlist__read_on_cpu does this automatically.
  304. */
  305. int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite)
  306. {
  307. unsigned int page_size = sysconf(_SC_PAGE_SIZE);
  308. int mask = pages * page_size - 1;
  309. struct perf_evsel *evsel;
  310. const struct cpu_map *cpus = evlist->cpus;
  311. const struct thread_map *threads = evlist->threads;
  312. int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE);
  313. if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
  314. return -ENOMEM;
  315. if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
  316. return -ENOMEM;
  317. evlist->overwrite = overwrite;
  318. evlist->mmap_len = (pages + 1) * page_size;
  319. list_for_each_entry(evsel, &evlist->entries, node) {
  320. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  321. evsel->sample_id == NULL &&
  322. perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
  323. return -ENOMEM;
  324. }
  325. if (evlist->cpus->map[0] == -1)
  326. return perf_evlist__mmap_per_thread(evlist, prot, mask);
  327. return perf_evlist__mmap_per_cpu(evlist, prot, mask);
  328. }
  329. int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid,
  330. pid_t target_tid, const char *cpu_list)
  331. {
  332. evlist->threads = thread_map__new(target_pid, target_tid);
  333. if (evlist->threads == NULL)
  334. return -1;
  335. if (cpu_list == NULL && target_tid != -1)
  336. evlist->cpus = cpu_map__dummy_new();
  337. else
  338. evlist->cpus = cpu_map__new(cpu_list);
  339. if (evlist->cpus == NULL)
  340. goto out_delete_threads;
  341. return 0;
  342. out_delete_threads:
  343. thread_map__delete(evlist->threads);
  344. return -1;
  345. }
  346. void perf_evlist__delete_maps(struct perf_evlist *evlist)
  347. {
  348. cpu_map__delete(evlist->cpus);
  349. thread_map__delete(evlist->threads);
  350. evlist->cpus = NULL;
  351. evlist->threads = NULL;
  352. }
  353. int perf_evlist__set_filters(struct perf_evlist *evlist)
  354. {
  355. const struct thread_map *threads = evlist->threads;
  356. const struct cpu_map *cpus = evlist->cpus;
  357. struct perf_evsel *evsel;
  358. char *filter;
  359. int thread;
  360. int cpu;
  361. int err;
  362. int fd;
  363. list_for_each_entry(evsel, &evlist->entries, node) {
  364. filter = evsel->filter;
  365. if (!filter)
  366. continue;
  367. for (cpu = 0; cpu < cpus->nr; cpu++) {
  368. for (thread = 0; thread < threads->nr; thread++) {
  369. fd = FD(evsel, cpu, thread);
  370. err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
  371. if (err)
  372. return err;
  373. }
  374. }
  375. }
  376. return 0;
  377. }
  378. bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist)
  379. {
  380. struct perf_evsel *pos, *first;
  381. pos = first = list_entry(evlist->entries.next, struct perf_evsel, node);
  382. list_for_each_entry_continue(pos, &evlist->entries, node) {
  383. if (first->attr.sample_type != pos->attr.sample_type)
  384. return false;
  385. }
  386. return true;
  387. }
  388. u64 perf_evlist__sample_type(const struct perf_evlist *evlist)
  389. {
  390. struct perf_evsel *first;
  391. first = list_entry(evlist->entries.next, struct perf_evsel, node);
  392. return first->attr.sample_type;
  393. }
  394. bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist)
  395. {
  396. struct perf_evsel *pos, *first;
  397. pos = first = list_entry(evlist->entries.next, struct perf_evsel, node);
  398. list_for_each_entry_continue(pos, &evlist->entries, node) {
  399. if (first->attr.sample_id_all != pos->attr.sample_id_all)
  400. return false;
  401. }
  402. return true;
  403. }
  404. bool perf_evlist__sample_id_all(const struct perf_evlist *evlist)
  405. {
  406. struct perf_evsel *first;
  407. first = list_entry(evlist->entries.next, struct perf_evsel, node);
  408. return first->attr.sample_id_all;
  409. }