evsel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #include "evsel.h"
  2. #include "evlist.h"
  3. #include "../perf.h"
  4. #include "util.h"
  5. #include "cpumap.h"
  6. #include "thread_map.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. void perf_evsel__init(struct perf_evsel *evsel,
  14. struct perf_event_attr *attr, int idx)
  15. {
  16. evsel->idx = idx;
  17. evsel->attr = *attr;
  18. INIT_LIST_HEAD(&evsel->node);
  19. }
  20. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
  21. {
  22. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  23. if (evsel != NULL)
  24. perf_evsel__init(evsel, attr, idx);
  25. return evsel;
  26. }
  27. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  28. {
  29. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  30. return evsel->fd != NULL ? 0 : -ENOMEM;
  31. }
  32. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  33. {
  34. evsel->id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  35. return evsel->id != NULL ? 0 : -ENOMEM;
  36. }
  37. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  38. {
  39. evsel->counts = zalloc((sizeof(*evsel->counts) +
  40. (ncpus * sizeof(struct perf_counts_values))));
  41. return evsel->counts != NULL ? 0 : -ENOMEM;
  42. }
  43. void perf_evsel__free_fd(struct perf_evsel *evsel)
  44. {
  45. xyarray__delete(evsel->fd);
  46. evsel->fd = NULL;
  47. }
  48. void perf_evsel__free_id(struct perf_evsel *evsel)
  49. {
  50. xyarray__delete(evsel->id);
  51. evsel->id = NULL;
  52. }
  53. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  54. {
  55. int cpu, thread;
  56. for (cpu = 0; cpu < ncpus; cpu++)
  57. for (thread = 0; thread < nthreads; ++thread) {
  58. close(FD(evsel, cpu, thread));
  59. FD(evsel, cpu, thread) = -1;
  60. }
  61. }
  62. void perf_evlist__munmap(struct perf_evlist *evlist, int ncpus)
  63. {
  64. int cpu;
  65. for (cpu = 0; cpu < ncpus; cpu++) {
  66. if (evlist->mmap[cpu].base != NULL) {
  67. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  68. evlist->mmap[cpu].base = NULL;
  69. }
  70. }
  71. }
  72. int perf_evlist__alloc_mmap(struct perf_evlist *evlist, int ncpus)
  73. {
  74. evlist->mmap = zalloc(ncpus * sizeof(struct perf_mmap));
  75. return evlist->mmap != NULL ? 0 : -ENOMEM;
  76. }
  77. void perf_evsel__exit(struct perf_evsel *evsel)
  78. {
  79. assert(list_empty(&evsel->node));
  80. xyarray__delete(evsel->fd);
  81. xyarray__delete(evsel->id);
  82. }
  83. void perf_evsel__delete(struct perf_evsel *evsel)
  84. {
  85. perf_evsel__exit(evsel);
  86. free(evsel);
  87. }
  88. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  89. int cpu, int thread, bool scale)
  90. {
  91. struct perf_counts_values count;
  92. size_t nv = scale ? 3 : 1;
  93. if (FD(evsel, cpu, thread) < 0)
  94. return -EINVAL;
  95. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  96. return -ENOMEM;
  97. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  98. return -errno;
  99. if (scale) {
  100. if (count.run == 0)
  101. count.val = 0;
  102. else if (count.run < count.ena)
  103. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  104. } else
  105. count.ena = count.run = 0;
  106. evsel->counts->cpu[cpu] = count;
  107. return 0;
  108. }
  109. int __perf_evsel__read(struct perf_evsel *evsel,
  110. int ncpus, int nthreads, bool scale)
  111. {
  112. size_t nv = scale ? 3 : 1;
  113. int cpu, thread;
  114. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  115. aggr->val = 0;
  116. for (cpu = 0; cpu < ncpus; cpu++) {
  117. for (thread = 0; thread < nthreads; thread++) {
  118. if (FD(evsel, cpu, thread) < 0)
  119. continue;
  120. if (readn(FD(evsel, cpu, thread),
  121. &count, nv * sizeof(u64)) < 0)
  122. return -errno;
  123. aggr->val += count.val;
  124. if (scale) {
  125. aggr->ena += count.ena;
  126. aggr->run += count.run;
  127. }
  128. }
  129. }
  130. evsel->counts->scaled = 0;
  131. if (scale) {
  132. if (aggr->run == 0) {
  133. evsel->counts->scaled = -1;
  134. aggr->val = 0;
  135. return 0;
  136. }
  137. if (aggr->run < aggr->ena) {
  138. evsel->counts->scaled = 1;
  139. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  140. }
  141. } else
  142. aggr->ena = aggr->run = 0;
  143. return 0;
  144. }
  145. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  146. struct thread_map *threads, bool group, bool inherit)
  147. {
  148. int cpu, thread;
  149. if (evsel->fd == NULL &&
  150. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  151. return -1;
  152. for (cpu = 0; cpu < cpus->nr; cpu++) {
  153. int group_fd = -1;
  154. evsel->attr.inherit = (cpus->map[cpu] < 0) && inherit;
  155. for (thread = 0; thread < threads->nr; thread++) {
  156. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  157. threads->map[thread],
  158. cpus->map[cpu],
  159. group_fd, 0);
  160. if (FD(evsel, cpu, thread) < 0)
  161. goto out_close;
  162. if (group && group_fd == -1)
  163. group_fd = FD(evsel, cpu, thread);
  164. }
  165. }
  166. return 0;
  167. out_close:
  168. do {
  169. while (--thread >= 0) {
  170. close(FD(evsel, cpu, thread));
  171. FD(evsel, cpu, thread) = -1;
  172. }
  173. thread = threads->nr;
  174. } while (--cpu >= 0);
  175. return -1;
  176. }
  177. static struct {
  178. struct cpu_map map;
  179. int cpus[1];
  180. } empty_cpu_map = {
  181. .map.nr = 1,
  182. .cpus = { -1, },
  183. };
  184. static struct {
  185. struct thread_map map;
  186. int threads[1];
  187. } empty_thread_map = {
  188. .map.nr = 1,
  189. .threads = { -1, },
  190. };
  191. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  192. struct thread_map *threads, bool group, bool inherit)
  193. {
  194. if (cpus == NULL) {
  195. /* Work around old compiler warnings about strict aliasing */
  196. cpus = &empty_cpu_map.map;
  197. }
  198. if (threads == NULL)
  199. threads = &empty_thread_map.map;
  200. return __perf_evsel__open(evsel, cpus, threads, group, inherit);
  201. }
  202. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  203. struct cpu_map *cpus, bool group, bool inherit)
  204. {
  205. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit);
  206. }
  207. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  208. struct thread_map *threads, bool group, bool inherit)
  209. {
  210. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit);
  211. }
  212. static int __perf_evlist__mmap(struct perf_evlist *evlist, int cpu, int prot,
  213. int mask, int fd)
  214. {
  215. evlist->mmap[cpu].prev = 0;
  216. evlist->mmap[cpu].mask = mask;
  217. evlist->mmap[cpu].base = mmap(NULL, evlist->mmap_len, prot,
  218. MAP_SHARED, fd, 0);
  219. if (evlist->mmap[cpu].base == MAP_FAILED)
  220. return -1;
  221. perf_evlist__add_pollfd(evlist, fd);
  222. return 0;
  223. }
  224. static int perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel,
  225. int cpu, int thread, int fd)
  226. {
  227. struct perf_sample_id *sid;
  228. u64 read_data[4] = { 0, };
  229. int hash, id_idx = 1; /* The first entry is the counter value */
  230. if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
  231. read(fd, &read_data, sizeof(read_data)) == -1)
  232. return -1;
  233. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  234. ++id_idx;
  235. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  236. ++id_idx;
  237. sid = SID(evsel, cpu, thread);
  238. sid->id = read_data[id_idx];
  239. sid->evsel = evsel;
  240. hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
  241. hlist_add_head(&sid->node, &evlist->heads[hash]);
  242. return 0;
  243. }
  244. /** perf_evlist__mmap - Create per cpu maps to receive events
  245. *
  246. * @evlist - list of events
  247. * @cpus - cpu map being monitored
  248. * @threads - threads map being monitored
  249. * @pages - map length in pages
  250. * @overwrite - overwrite older events?
  251. *
  252. * If overwrite is false the user needs to signal event consuption using:
  253. *
  254. * struct perf_mmap *m = &evlist->mmap[cpu];
  255. * unsigned int head = perf_mmap__read_head(m);
  256. *
  257. * perf_mmap__write_tail(m, head)
  258. */
  259. int perf_evlist__mmap(struct perf_evlist *evlist, struct cpu_map *cpus,
  260. struct thread_map *threads, int pages, bool overwrite)
  261. {
  262. unsigned int page_size = sysconf(_SC_PAGE_SIZE);
  263. int mask = pages * page_size - 1, cpu;
  264. struct perf_evsel *first_evsel, *evsel;
  265. int thread, prot = PROT_READ | (overwrite ? 0 : PROT_WRITE);
  266. if (evlist->mmap == NULL &&
  267. perf_evlist__alloc_mmap(evlist, cpus->nr) < 0)
  268. return -ENOMEM;
  269. if (evlist->pollfd == NULL &&
  270. perf_evlist__alloc_pollfd(evlist, cpus->nr, threads->nr) < 0)
  271. return -ENOMEM;
  272. evlist->mmap_len = (pages + 1) * page_size;
  273. first_evsel = list_entry(evlist->entries.next, struct perf_evsel, node);
  274. list_for_each_entry(evsel, &evlist->entries, node) {
  275. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  276. evsel->id == NULL &&
  277. perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
  278. return -ENOMEM;
  279. for (cpu = 0; cpu < cpus->nr; cpu++) {
  280. for (thread = 0; thread < threads->nr; thread++) {
  281. int fd = FD(evsel, cpu, thread);
  282. if (evsel->idx || thread) {
  283. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT,
  284. FD(first_evsel, cpu, 0)) != 0)
  285. goto out_unmap;
  286. } else if (__perf_evlist__mmap(evlist, cpu, prot, mask, fd) < 0)
  287. goto out_unmap;
  288. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  289. perf_evlist__id_hash(evlist, evsel, cpu, thread, fd) < 0)
  290. goto out_unmap;
  291. }
  292. }
  293. }
  294. return 0;
  295. out_unmap:
  296. for (cpu = 0; cpu < cpus->nr; cpu++) {
  297. if (evlist->mmap[cpu].base != NULL) {
  298. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  299. evlist->mmap[cpu].base = NULL;
  300. }
  301. }
  302. return -1;
  303. }
  304. static int event__parse_id_sample(const event_t *event, u64 type,
  305. struct sample_data *sample)
  306. {
  307. const u64 *array = event->sample.array;
  308. array += ((event->header.size -
  309. sizeof(event->header)) / sizeof(u64)) - 1;
  310. if (type & PERF_SAMPLE_CPU) {
  311. u32 *p = (u32 *)array;
  312. sample->cpu = *p;
  313. array--;
  314. }
  315. if (type & PERF_SAMPLE_STREAM_ID) {
  316. sample->stream_id = *array;
  317. array--;
  318. }
  319. if (type & PERF_SAMPLE_ID) {
  320. sample->id = *array;
  321. array--;
  322. }
  323. if (type & PERF_SAMPLE_TIME) {
  324. sample->time = *array;
  325. array--;
  326. }
  327. if (type & PERF_SAMPLE_TID) {
  328. u32 *p = (u32 *)array;
  329. sample->pid = p[0];
  330. sample->tid = p[1];
  331. }
  332. return 0;
  333. }
  334. int event__parse_sample(const event_t *event, u64 type, bool sample_id_all,
  335. struct sample_data *data)
  336. {
  337. const u64 *array;
  338. data->cpu = data->pid = data->tid = -1;
  339. data->stream_id = data->id = data->time = -1ULL;
  340. if (event->header.type != PERF_RECORD_SAMPLE) {
  341. if (!sample_id_all)
  342. return 0;
  343. return event__parse_id_sample(event, type, data);
  344. }
  345. array = event->sample.array;
  346. if (type & PERF_SAMPLE_IP) {
  347. data->ip = event->ip.ip;
  348. array++;
  349. }
  350. if (type & PERF_SAMPLE_TID) {
  351. u32 *p = (u32 *)array;
  352. data->pid = p[0];
  353. data->tid = p[1];
  354. array++;
  355. }
  356. if (type & PERF_SAMPLE_TIME) {
  357. data->time = *array;
  358. array++;
  359. }
  360. if (type & PERF_SAMPLE_ADDR) {
  361. data->addr = *array;
  362. array++;
  363. }
  364. data->id = -1ULL;
  365. if (type & PERF_SAMPLE_ID) {
  366. data->id = *array;
  367. array++;
  368. }
  369. if (type & PERF_SAMPLE_STREAM_ID) {
  370. data->stream_id = *array;
  371. array++;
  372. }
  373. if (type & PERF_SAMPLE_CPU) {
  374. u32 *p = (u32 *)array;
  375. data->cpu = *p;
  376. array++;
  377. }
  378. if (type & PERF_SAMPLE_PERIOD) {
  379. data->period = *array;
  380. array++;
  381. }
  382. if (type & PERF_SAMPLE_READ) {
  383. fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
  384. return -1;
  385. }
  386. if (type & PERF_SAMPLE_CALLCHAIN) {
  387. data->callchain = (struct ip_callchain *)array;
  388. array += 1 + data->callchain->nr;
  389. }
  390. if (type & PERF_SAMPLE_RAW) {
  391. u32 *p = (u32 *)array;
  392. data->raw_size = *p;
  393. p++;
  394. data->raw_data = p;
  395. }
  396. return 0;
  397. }