evsel.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include "evsel.h"
  2. #include "evlist.h"
  3. #include "../perf.h"
  4. #include "util.h"
  5. #include "cpumap.h"
  6. #include "thread.h"
  7. #include <unistd.h>
  8. #include <sys/mman.h>
  9. #include <linux/bitops.h>
  10. #include <linux/hash.h>
  11. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  12. #define SID(e, x, y) xyarray__entry(e->id, x, y)
  13. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
  14. {
  15. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  16. if (evsel != NULL) {
  17. evsel->idx = idx;
  18. evsel->attr = *attr;
  19. INIT_LIST_HEAD(&evsel->node);
  20. }
  21. return evsel;
  22. }
  23. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  24. {
  25. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  26. return evsel->fd != NULL ? 0 : -ENOMEM;
  27. }
  28. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  29. {
  30. evsel->id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  31. return evsel->id != NULL ? 0 : -ENOMEM;
  32. }
  33. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  34. {
  35. evsel->counts = zalloc((sizeof(*evsel->counts) +
  36. (ncpus * sizeof(struct perf_counts_values))));
  37. return evsel->counts != NULL ? 0 : -ENOMEM;
  38. }
  39. void perf_evsel__free_fd(struct perf_evsel *evsel)
  40. {
  41. xyarray__delete(evsel->fd);
  42. evsel->fd = NULL;
  43. }
  44. void perf_evsel__free_id(struct perf_evsel *evsel)
  45. {
  46. xyarray__delete(evsel->id);
  47. evsel->id = NULL;
  48. }
  49. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  50. {
  51. int cpu, thread;
  52. for (cpu = 0; cpu < ncpus; cpu++)
  53. for (thread = 0; thread < nthreads; ++thread) {
  54. close(FD(evsel, cpu, thread));
  55. FD(evsel, cpu, thread) = -1;
  56. }
  57. }
  58. void perf_evlist__munmap(struct perf_evlist *evlist, int ncpus)
  59. {
  60. int cpu;
  61. for (cpu = 0; cpu < ncpus; cpu++) {
  62. if (evlist->mmap[cpu].base != NULL) {
  63. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  64. evlist->mmap[cpu].base = NULL;
  65. }
  66. }
  67. }
  68. int perf_evlist__alloc_mmap(struct perf_evlist *evlist, int ncpus)
  69. {
  70. evlist->mmap = zalloc(ncpus * sizeof(struct perf_mmap));
  71. return evlist->mmap != NULL ? 0 : -ENOMEM;
  72. }
  73. void perf_evsel__delete(struct perf_evsel *evsel)
  74. {
  75. assert(list_empty(&evsel->node));
  76. xyarray__delete(evsel->fd);
  77. xyarray__delete(evsel->id);
  78. free(evsel);
  79. }
  80. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  81. int cpu, int thread, bool scale)
  82. {
  83. struct perf_counts_values count;
  84. size_t nv = scale ? 3 : 1;
  85. if (FD(evsel, cpu, thread) < 0)
  86. return -EINVAL;
  87. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  88. return -ENOMEM;
  89. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  90. return -errno;
  91. if (scale) {
  92. if (count.run == 0)
  93. count.val = 0;
  94. else if (count.run < count.ena)
  95. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  96. } else
  97. count.ena = count.run = 0;
  98. evsel->counts->cpu[cpu] = count;
  99. return 0;
  100. }
  101. int __perf_evsel__read(struct perf_evsel *evsel,
  102. int ncpus, int nthreads, bool scale)
  103. {
  104. size_t nv = scale ? 3 : 1;
  105. int cpu, thread;
  106. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  107. aggr->val = 0;
  108. for (cpu = 0; cpu < ncpus; cpu++) {
  109. for (thread = 0; thread < nthreads; thread++) {
  110. if (FD(evsel, cpu, thread) < 0)
  111. continue;
  112. if (readn(FD(evsel, cpu, thread),
  113. &count, nv * sizeof(u64)) < 0)
  114. return -errno;
  115. aggr->val += count.val;
  116. if (scale) {
  117. aggr->ena += count.ena;
  118. aggr->run += count.run;
  119. }
  120. }
  121. }
  122. evsel->counts->scaled = 0;
  123. if (scale) {
  124. if (aggr->run == 0) {
  125. evsel->counts->scaled = -1;
  126. aggr->val = 0;
  127. return 0;
  128. }
  129. if (aggr->run < aggr->ena) {
  130. evsel->counts->scaled = 1;
  131. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  132. }
  133. } else
  134. aggr->ena = aggr->run = 0;
  135. return 0;
  136. }
  137. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  138. struct thread_map *threads, bool group, bool inherit)
  139. {
  140. int cpu, thread;
  141. if (evsel->fd == NULL &&
  142. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  143. return -1;
  144. for (cpu = 0; cpu < cpus->nr; cpu++) {
  145. int group_fd = -1;
  146. evsel->attr.inherit = (cpus->map[cpu] < 0) && inherit;
  147. for (thread = 0; thread < threads->nr; thread++) {
  148. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  149. threads->map[thread],
  150. cpus->map[cpu],
  151. group_fd, 0);
  152. if (FD(evsel, cpu, thread) < 0)
  153. goto out_close;
  154. if (group && group_fd == -1)
  155. group_fd = FD(evsel, cpu, thread);
  156. }
  157. }
  158. return 0;
  159. out_close:
  160. do {
  161. while (--thread >= 0) {
  162. close(FD(evsel, cpu, thread));
  163. FD(evsel, cpu, thread) = -1;
  164. }
  165. thread = threads->nr;
  166. } while (--cpu >= 0);
  167. return -1;
  168. }
  169. static struct {
  170. struct cpu_map map;
  171. int cpus[1];
  172. } empty_cpu_map = {
  173. .map.nr = 1,
  174. .cpus = { -1, },
  175. };
  176. static struct {
  177. struct thread_map map;
  178. int threads[1];
  179. } empty_thread_map = {
  180. .map.nr = 1,
  181. .threads = { -1, },
  182. };
  183. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  184. struct thread_map *threads, bool group, bool inherit)
  185. {
  186. if (cpus == NULL) {
  187. /* Work around old compiler warnings about strict aliasing */
  188. cpus = &empty_cpu_map.map;
  189. }
  190. if (threads == NULL)
  191. threads = &empty_thread_map.map;
  192. return __perf_evsel__open(evsel, cpus, threads, group, inherit);
  193. }
  194. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  195. struct cpu_map *cpus, bool group, bool inherit)
  196. {
  197. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit);
  198. }
  199. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  200. struct thread_map *threads, bool group, bool inherit)
  201. {
  202. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit);
  203. }
  204. static int __perf_evlist__mmap(struct perf_evlist *evlist, int cpu, int prot,
  205. int mask, int fd)
  206. {
  207. evlist->mmap[cpu].prev = 0;
  208. evlist->mmap[cpu].mask = mask;
  209. evlist->mmap[cpu].base = mmap(NULL, evlist->mmap_len, prot,
  210. MAP_SHARED, fd, 0);
  211. if (evlist->mmap[cpu].base == MAP_FAILED)
  212. return -1;
  213. perf_evlist__add_pollfd(evlist, fd);
  214. return 0;
  215. }
  216. static int perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel,
  217. int cpu, int thread, int fd)
  218. {
  219. struct perf_sample_id *sid;
  220. u64 read_data[4] = { 0, };
  221. int hash, id_idx = 1; /* The first entry is the counter value */
  222. if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
  223. read(fd, &read_data, sizeof(read_data)) == -1)
  224. return -1;
  225. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  226. ++id_idx;
  227. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  228. ++id_idx;
  229. sid = SID(evsel, cpu, thread);
  230. sid->id = read_data[id_idx];
  231. sid->evsel = evsel;
  232. hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
  233. hlist_add_head(&sid->node, &evlist->heads[hash]);
  234. return 0;
  235. }
  236. /** perf_evlist__mmap - Create per cpu maps to receive events
  237. *
  238. * @evlist - list of events
  239. * @cpus - cpu map being monitored
  240. * @threads - threads map being monitored
  241. * @pages - map length in pages
  242. * @overwrite - overwrite older events?
  243. *
  244. * If overwrite is false the user needs to signal event consuption using:
  245. *
  246. * struct perf_mmap *m = &evlist->mmap[cpu];
  247. * unsigned int head = perf_mmap__read_head(m);
  248. *
  249. * perf_mmap__write_tail(m, head)
  250. */
  251. int perf_evlist__mmap(struct perf_evlist *evlist, struct cpu_map *cpus,
  252. struct thread_map *threads, int pages, bool overwrite)
  253. {
  254. unsigned int page_size = sysconf(_SC_PAGE_SIZE);
  255. int mask = pages * page_size - 1, cpu;
  256. struct perf_evsel *first_evsel, *evsel;
  257. int thread, prot = PROT_READ | (overwrite ? 0 : PROT_WRITE);
  258. if (evlist->mmap == NULL &&
  259. perf_evlist__alloc_mmap(evlist, cpus->nr) < 0)
  260. return -ENOMEM;
  261. if (evlist->pollfd == NULL &&
  262. perf_evlist__alloc_pollfd(evlist, cpus->nr, threads->nr) < 0)
  263. return -ENOMEM;
  264. evlist->mmap_len = (pages + 1) * page_size;
  265. first_evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  266. list_for_each_entry(evsel, &evlist->entries, node) {
  267. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  268. evsel->id == NULL &&
  269. perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
  270. return -ENOMEM;
  271. for (cpu = 0; cpu < cpus->nr; cpu++) {
  272. for (thread = 0; thread < threads->nr; thread++) {
  273. int fd = FD(evsel, cpu, thread);
  274. if (evsel->idx || thread) {
  275. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT,
  276. FD(first_evsel, cpu, 0)) != 0)
  277. goto out_unmap;
  278. } else if (__perf_evlist__mmap(evlist, cpu, prot, mask, fd) < 0)
  279. goto out_unmap;
  280. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  281. perf_evlist__id_hash(evlist, evsel, cpu, thread, fd) < 0)
  282. goto out_unmap;
  283. }
  284. }
  285. }
  286. return 0;
  287. out_unmap:
  288. for (cpu = 0; cpu < cpus->nr; cpu++) {
  289. if (evlist->mmap[cpu].base != NULL) {
  290. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  291. evlist->mmap[cpu].base = NULL;
  292. }
  293. }
  294. return -1;
  295. }