evlist.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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->id, x, y)
  20. void perf_evlist__init(struct perf_evlist *evlist)
  21. {
  22. int i;
  23. for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
  24. INIT_HLIST_HEAD(&evlist->heads[i]);
  25. INIT_LIST_HEAD(&evlist->entries);
  26. }
  27. struct perf_evlist *perf_evlist__new(void)
  28. {
  29. struct perf_evlist *evlist = zalloc(sizeof(*evlist));
  30. if (evlist != NULL)
  31. perf_evlist__init(evlist);
  32. return evlist;
  33. }
  34. static void perf_evlist__purge(struct perf_evlist *evlist)
  35. {
  36. struct perf_evsel *pos, *n;
  37. list_for_each_entry_safe(pos, n, &evlist->entries, node) {
  38. list_del_init(&pos->node);
  39. perf_evsel__delete(pos);
  40. }
  41. evlist->nr_entries = 0;
  42. }
  43. void perf_evlist__exit(struct perf_evlist *evlist)
  44. {
  45. free(evlist->mmap);
  46. free(evlist->pollfd);
  47. evlist->mmap = NULL;
  48. evlist->pollfd = NULL;
  49. }
  50. void perf_evlist__delete(struct perf_evlist *evlist)
  51. {
  52. perf_evlist__purge(evlist);
  53. perf_evlist__exit(evlist);
  54. free(evlist);
  55. }
  56. void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
  57. {
  58. list_add_tail(&entry->node, &evlist->entries);
  59. ++evlist->nr_entries;
  60. }
  61. int perf_evlist__add_default(struct perf_evlist *evlist)
  62. {
  63. struct perf_event_attr attr = {
  64. .type = PERF_TYPE_HARDWARE,
  65. .config = PERF_COUNT_HW_CPU_CYCLES,
  66. };
  67. struct perf_evsel *evsel = perf_evsel__new(&attr, 0);
  68. if (evsel == NULL)
  69. return -ENOMEM;
  70. perf_evlist__add(evlist, evsel);
  71. return 0;
  72. }
  73. int perf_evlist__alloc_pollfd(struct perf_evlist *evlist, int ncpus, int nthreads)
  74. {
  75. int nfds = ncpus * nthreads * evlist->nr_entries;
  76. evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
  77. return evlist->pollfd != NULL ? 0 : -ENOMEM;
  78. }
  79. void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
  80. {
  81. fcntl(fd, F_SETFL, O_NONBLOCK);
  82. evlist->pollfd[evlist->nr_fds].fd = fd;
  83. evlist->pollfd[evlist->nr_fds].events = POLLIN;
  84. evlist->nr_fds++;
  85. }
  86. static int perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel,
  87. int cpu, int thread, int fd)
  88. {
  89. struct perf_sample_id *sid;
  90. u64 read_data[4] = { 0, };
  91. int hash, id_idx = 1; /* The first entry is the counter value */
  92. if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
  93. read(fd, &read_data, sizeof(read_data)) == -1)
  94. return -1;
  95. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  96. ++id_idx;
  97. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  98. ++id_idx;
  99. sid = SID(evsel, cpu, thread);
  100. sid->id = read_data[id_idx];
  101. sid->evsel = evsel;
  102. hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
  103. hlist_add_head(&sid->node, &evlist->heads[hash]);
  104. return 0;
  105. }
  106. struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
  107. {
  108. struct hlist_head *head;
  109. struct hlist_node *pos;
  110. struct perf_sample_id *sid;
  111. int hash;
  112. if (evlist->nr_entries == 1)
  113. return list_entry(evlist->entries.next, struct perf_evsel, node);
  114. hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
  115. head = &evlist->heads[hash];
  116. hlist_for_each_entry(sid, pos, head, node)
  117. if (sid->id == id)
  118. return sid->evsel;
  119. return NULL;
  120. }
  121. union perf_event *perf_evlist__read_on_cpu(struct perf_evlist *evlist, int cpu)
  122. {
  123. /* XXX Move this to perf.c, making it generally available */
  124. unsigned int page_size = sysconf(_SC_PAGE_SIZE);
  125. struct perf_mmap *md = &evlist->mmap[cpu];
  126. unsigned int head = perf_mmap__read_head(md);
  127. unsigned int old = md->prev;
  128. unsigned char *data = md->base + page_size;
  129. union perf_event *event = NULL;
  130. if (evlist->overwrite) {
  131. /*
  132. * If we're further behind than half the buffer, there's a chance
  133. * the writer will bite our tail and mess up the samples under us.
  134. *
  135. * If we somehow ended up ahead of the head, we got messed up.
  136. *
  137. * In either case, truncate and restart at head.
  138. */
  139. int diff = head - old;
  140. if (diff > md->mask / 2 || diff < 0) {
  141. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  142. /*
  143. * head points to a known good entry, start there.
  144. */
  145. old = head;
  146. }
  147. }
  148. if (old != head) {
  149. size_t size;
  150. event = (union perf_event *)&data[old & md->mask];
  151. size = event->header.size;
  152. /*
  153. * Event straddles the mmap boundary -- header should always
  154. * be inside due to u64 alignment of output.
  155. */
  156. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  157. unsigned int offset = old;
  158. unsigned int len = min(sizeof(*event), size), cpy;
  159. void *dst = &evlist->event_copy;
  160. do {
  161. cpy = min(md->mask + 1 - (offset & md->mask), len);
  162. memcpy(dst, &data[offset & md->mask], cpy);
  163. offset += cpy;
  164. dst += cpy;
  165. len -= cpy;
  166. } while (len);
  167. event = &evlist->event_copy;
  168. }
  169. old += size;
  170. }
  171. md->prev = old;
  172. if (!evlist->overwrite)
  173. perf_mmap__write_tail(md, old);
  174. return event;
  175. }
  176. void perf_evlist__munmap(struct perf_evlist *evlist, int ncpus)
  177. {
  178. int cpu;
  179. for (cpu = 0; cpu < ncpus; cpu++) {
  180. if (evlist->mmap[cpu].base != NULL) {
  181. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  182. evlist->mmap[cpu].base = NULL;
  183. }
  184. }
  185. }
  186. int perf_evlist__alloc_mmap(struct perf_evlist *evlist, int ncpus)
  187. {
  188. evlist->mmap = zalloc(ncpus * sizeof(struct perf_mmap));
  189. return evlist->mmap != NULL ? 0 : -ENOMEM;
  190. }
  191. static int __perf_evlist__mmap(struct perf_evlist *evlist, int cpu, int prot,
  192. int mask, int fd)
  193. {
  194. evlist->mmap[cpu].prev = 0;
  195. evlist->mmap[cpu].mask = mask;
  196. evlist->mmap[cpu].base = mmap(NULL, evlist->mmap_len, prot,
  197. MAP_SHARED, fd, 0);
  198. if (evlist->mmap[cpu].base == MAP_FAILED)
  199. return -1;
  200. perf_evlist__add_pollfd(evlist, fd);
  201. return 0;
  202. }
  203. /** perf_evlist__mmap - Create per cpu maps to receive events
  204. *
  205. * @evlist - list of events
  206. * @cpus - cpu map being monitored
  207. * @threads - threads map being monitored
  208. * @pages - map length in pages
  209. * @overwrite - overwrite older events?
  210. *
  211. * If overwrite is false the user needs to signal event consuption using:
  212. *
  213. * struct perf_mmap *m = &evlist->mmap[cpu];
  214. * unsigned int head = perf_mmap__read_head(m);
  215. *
  216. * perf_mmap__write_tail(m, head)
  217. */
  218. int perf_evlist__mmap(struct perf_evlist *evlist, struct cpu_map *cpus,
  219. struct thread_map *threads, int pages, bool overwrite)
  220. {
  221. unsigned int page_size = sysconf(_SC_PAGE_SIZE);
  222. int mask = pages * page_size - 1, cpu;
  223. struct perf_evsel *first_evsel, *evsel;
  224. int thread, prot = PROT_READ | (overwrite ? 0 : PROT_WRITE);
  225. if (evlist->mmap == NULL &&
  226. perf_evlist__alloc_mmap(evlist, cpus->nr) < 0)
  227. return -ENOMEM;
  228. if (evlist->pollfd == NULL &&
  229. perf_evlist__alloc_pollfd(evlist, cpus->nr, threads->nr) < 0)
  230. return -ENOMEM;
  231. evlist->overwrite = overwrite;
  232. evlist->mmap_len = (pages + 1) * page_size;
  233. first_evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  234. list_for_each_entry(evsel, &evlist->entries, node) {
  235. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  236. evsel->id == NULL &&
  237. perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
  238. return -ENOMEM;
  239. for (cpu = 0; cpu < cpus->nr; cpu++) {
  240. for (thread = 0; thread < threads->nr; thread++) {
  241. int fd = FD(evsel, cpu, thread);
  242. if (evsel->idx || thread) {
  243. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT,
  244. FD(first_evsel, cpu, 0)) != 0)
  245. goto out_unmap;
  246. } else if (__perf_evlist__mmap(evlist, cpu, prot, mask, fd) < 0)
  247. goto out_unmap;
  248. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  249. perf_evlist__id_hash(evlist, evsel, cpu, thread, fd) < 0)
  250. goto out_unmap;
  251. }
  252. }
  253. }
  254. return 0;
  255. out_unmap:
  256. for (cpu = 0; cpu < cpus->nr; cpu++) {
  257. if (evlist->mmap[cpu].base != NULL) {
  258. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  259. evlist->mmap[cpu].base = NULL;
  260. }
  261. }
  262. return -1;
  263. }