evsel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 <byteswap.h>
  10. #include "asm/bug.h"
  11. #include "evsel.h"
  12. #include "evlist.h"
  13. #include "util.h"
  14. #include "cpumap.h"
  15. #include "thread_map.h"
  16. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  17. #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
  18. int __perf_evsel__sample_size(u64 sample_type)
  19. {
  20. u64 mask = sample_type & PERF_SAMPLE_MASK;
  21. int size = 0;
  22. int i;
  23. for (i = 0; i < 64; i++) {
  24. if (mask & (1ULL << i))
  25. size++;
  26. }
  27. size *= sizeof(u64);
  28. return size;
  29. }
  30. static void hists__init(struct hists *hists)
  31. {
  32. memset(hists, 0, sizeof(*hists));
  33. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  34. hists->entries_in = &hists->entries_in_array[0];
  35. hists->entries_collapsed = RB_ROOT;
  36. hists->entries = RB_ROOT;
  37. pthread_mutex_init(&hists->lock, NULL);
  38. }
  39. void perf_evsel__init(struct perf_evsel *evsel,
  40. struct perf_event_attr *attr, int idx)
  41. {
  42. evsel->idx = idx;
  43. evsel->attr = *attr;
  44. INIT_LIST_HEAD(&evsel->node);
  45. hists__init(&evsel->hists);
  46. }
  47. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
  48. {
  49. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  50. if (evsel != NULL)
  51. perf_evsel__init(evsel, attr, idx);
  52. return evsel;
  53. }
  54. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  55. {
  56. int cpu, thread;
  57. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  58. if (evsel->fd) {
  59. for (cpu = 0; cpu < ncpus; cpu++) {
  60. for (thread = 0; thread < nthreads; thread++) {
  61. FD(evsel, cpu, thread) = -1;
  62. }
  63. }
  64. }
  65. return evsel->fd != NULL ? 0 : -ENOMEM;
  66. }
  67. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  68. {
  69. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  70. if (evsel->sample_id == NULL)
  71. return -ENOMEM;
  72. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  73. if (evsel->id == NULL) {
  74. xyarray__delete(evsel->sample_id);
  75. evsel->sample_id = NULL;
  76. return -ENOMEM;
  77. }
  78. return 0;
  79. }
  80. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  81. {
  82. evsel->counts = zalloc((sizeof(*evsel->counts) +
  83. (ncpus * sizeof(struct perf_counts_values))));
  84. return evsel->counts != NULL ? 0 : -ENOMEM;
  85. }
  86. void perf_evsel__free_fd(struct perf_evsel *evsel)
  87. {
  88. xyarray__delete(evsel->fd);
  89. evsel->fd = NULL;
  90. }
  91. void perf_evsel__free_id(struct perf_evsel *evsel)
  92. {
  93. xyarray__delete(evsel->sample_id);
  94. evsel->sample_id = NULL;
  95. free(evsel->id);
  96. evsel->id = NULL;
  97. }
  98. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  99. {
  100. int cpu, thread;
  101. for (cpu = 0; cpu < ncpus; cpu++)
  102. for (thread = 0; thread < nthreads; ++thread) {
  103. close(FD(evsel, cpu, thread));
  104. FD(evsel, cpu, thread) = -1;
  105. }
  106. }
  107. void perf_evsel__exit(struct perf_evsel *evsel)
  108. {
  109. assert(list_empty(&evsel->node));
  110. xyarray__delete(evsel->fd);
  111. xyarray__delete(evsel->sample_id);
  112. free(evsel->id);
  113. }
  114. void perf_evsel__delete(struct perf_evsel *evsel)
  115. {
  116. perf_evsel__exit(evsel);
  117. close_cgroup(evsel->cgrp);
  118. free(evsel->name);
  119. free(evsel);
  120. }
  121. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  122. int cpu, int thread, bool scale)
  123. {
  124. struct perf_counts_values count;
  125. size_t nv = scale ? 3 : 1;
  126. if (FD(evsel, cpu, thread) < 0)
  127. return -EINVAL;
  128. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  129. return -ENOMEM;
  130. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  131. return -errno;
  132. if (scale) {
  133. if (count.run == 0)
  134. count.val = 0;
  135. else if (count.run < count.ena)
  136. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  137. } else
  138. count.ena = count.run = 0;
  139. evsel->counts->cpu[cpu] = count;
  140. return 0;
  141. }
  142. int __perf_evsel__read(struct perf_evsel *evsel,
  143. int ncpus, int nthreads, bool scale)
  144. {
  145. size_t nv = scale ? 3 : 1;
  146. int cpu, thread;
  147. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  148. aggr->val = aggr->ena = aggr->run = 0;
  149. for (cpu = 0; cpu < ncpus; cpu++) {
  150. for (thread = 0; thread < nthreads; thread++) {
  151. if (FD(evsel, cpu, thread) < 0)
  152. continue;
  153. if (readn(FD(evsel, cpu, thread),
  154. &count, nv * sizeof(u64)) < 0)
  155. return -errno;
  156. aggr->val += count.val;
  157. if (scale) {
  158. aggr->ena += count.ena;
  159. aggr->run += count.run;
  160. }
  161. }
  162. }
  163. evsel->counts->scaled = 0;
  164. if (scale) {
  165. if (aggr->run == 0) {
  166. evsel->counts->scaled = -1;
  167. aggr->val = 0;
  168. return 0;
  169. }
  170. if (aggr->run < aggr->ena) {
  171. evsel->counts->scaled = 1;
  172. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  173. }
  174. } else
  175. aggr->ena = aggr->run = 0;
  176. return 0;
  177. }
  178. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  179. struct thread_map *threads, bool group,
  180. struct xyarray *group_fds)
  181. {
  182. int cpu, thread;
  183. unsigned long flags = 0;
  184. int pid = -1, err;
  185. if (evsel->fd == NULL &&
  186. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  187. return -ENOMEM;
  188. if (evsel->cgrp) {
  189. flags = PERF_FLAG_PID_CGROUP;
  190. pid = evsel->cgrp->fd;
  191. }
  192. for (cpu = 0; cpu < cpus->nr; cpu++) {
  193. int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
  194. for (thread = 0; thread < threads->nr; thread++) {
  195. if (!evsel->cgrp)
  196. pid = threads->map[thread];
  197. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  198. pid,
  199. cpus->map[cpu],
  200. group_fd, flags);
  201. if (FD(evsel, cpu, thread) < 0) {
  202. err = -errno;
  203. goto out_close;
  204. }
  205. if (group && group_fd == -1)
  206. group_fd = FD(evsel, cpu, thread);
  207. }
  208. }
  209. return 0;
  210. out_close:
  211. do {
  212. while (--thread >= 0) {
  213. close(FD(evsel, cpu, thread));
  214. FD(evsel, cpu, thread) = -1;
  215. }
  216. thread = threads->nr;
  217. } while (--cpu >= 0);
  218. return err;
  219. }
  220. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  221. {
  222. if (evsel->fd == NULL)
  223. return;
  224. perf_evsel__close_fd(evsel, ncpus, nthreads);
  225. perf_evsel__free_fd(evsel);
  226. evsel->fd = NULL;
  227. }
  228. static struct {
  229. struct cpu_map map;
  230. int cpus[1];
  231. } empty_cpu_map = {
  232. .map.nr = 1,
  233. .cpus = { -1, },
  234. };
  235. static struct {
  236. struct thread_map map;
  237. int threads[1];
  238. } empty_thread_map = {
  239. .map.nr = 1,
  240. .threads = { -1, },
  241. };
  242. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  243. struct thread_map *threads, bool group,
  244. struct xyarray *group_fd)
  245. {
  246. if (cpus == NULL) {
  247. /* Work around old compiler warnings about strict aliasing */
  248. cpus = &empty_cpu_map.map;
  249. }
  250. if (threads == NULL)
  251. threads = &empty_thread_map.map;
  252. return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
  253. }
  254. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  255. struct cpu_map *cpus, bool group,
  256. struct xyarray *group_fd)
  257. {
  258. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
  259. group_fd);
  260. }
  261. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  262. struct thread_map *threads, bool group,
  263. struct xyarray *group_fd)
  264. {
  265. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
  266. group_fd);
  267. }
  268. static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
  269. struct perf_sample *sample)
  270. {
  271. const u64 *array = event->sample.array;
  272. array += ((event->header.size -
  273. sizeof(event->header)) / sizeof(u64)) - 1;
  274. if (type & PERF_SAMPLE_CPU) {
  275. u32 *p = (u32 *)array;
  276. sample->cpu = *p;
  277. array--;
  278. }
  279. if (type & PERF_SAMPLE_STREAM_ID) {
  280. sample->stream_id = *array;
  281. array--;
  282. }
  283. if (type & PERF_SAMPLE_ID) {
  284. sample->id = *array;
  285. array--;
  286. }
  287. if (type & PERF_SAMPLE_TIME) {
  288. sample->time = *array;
  289. array--;
  290. }
  291. if (type & PERF_SAMPLE_TID) {
  292. u32 *p = (u32 *)array;
  293. sample->pid = p[0];
  294. sample->tid = p[1];
  295. }
  296. return 0;
  297. }
  298. static bool sample_overlap(const union perf_event *event,
  299. const void *offset, u64 size)
  300. {
  301. const void *base = event;
  302. if (offset + size > base + event->header.size)
  303. return true;
  304. return false;
  305. }
  306. int perf_event__parse_sample(const union perf_event *event, u64 type,
  307. int sample_size, bool sample_id_all,
  308. struct perf_sample *data, bool swapped)
  309. {
  310. const u64 *array;
  311. /*
  312. * used for cross-endian analysis. See git commit 65014ab3
  313. * for why this goofiness is needed.
  314. */
  315. union {
  316. u64 val64;
  317. u32 val32[2];
  318. } u;
  319. data->cpu = data->pid = data->tid = -1;
  320. data->stream_id = data->id = data->time = -1ULL;
  321. if (event->header.type != PERF_RECORD_SAMPLE) {
  322. if (!sample_id_all)
  323. return 0;
  324. return perf_event__parse_id_sample(event, type, data);
  325. }
  326. array = event->sample.array;
  327. if (sample_size + sizeof(event->header) > event->header.size)
  328. return -EFAULT;
  329. if (type & PERF_SAMPLE_IP) {
  330. data->ip = event->ip.ip;
  331. array++;
  332. }
  333. if (type & PERF_SAMPLE_TID) {
  334. u.val64 = *array;
  335. if (swapped) {
  336. /* undo swap of u64, then swap on individual u32s */
  337. u.val64 = bswap_64(u.val64);
  338. u.val32[0] = bswap_32(u.val32[0]);
  339. u.val32[1] = bswap_32(u.val32[1]);
  340. }
  341. data->pid = u.val32[0];
  342. data->tid = u.val32[1];
  343. array++;
  344. }
  345. if (type & PERF_SAMPLE_TIME) {
  346. data->time = *array;
  347. array++;
  348. }
  349. data->addr = 0;
  350. if (type & PERF_SAMPLE_ADDR) {
  351. data->addr = *array;
  352. array++;
  353. }
  354. data->id = -1ULL;
  355. if (type & PERF_SAMPLE_ID) {
  356. data->id = *array;
  357. array++;
  358. }
  359. if (type & PERF_SAMPLE_STREAM_ID) {
  360. data->stream_id = *array;
  361. array++;
  362. }
  363. if (type & PERF_SAMPLE_CPU) {
  364. u.val64 = *array;
  365. if (swapped) {
  366. /* undo swap of u64, then swap on individual u32s */
  367. u.val64 = bswap_64(u.val64);
  368. u.val32[0] = bswap_32(u.val32[0]);
  369. }
  370. data->cpu = u.val32[0];
  371. array++;
  372. }
  373. if (type & PERF_SAMPLE_PERIOD) {
  374. data->period = *array;
  375. array++;
  376. }
  377. if (type & PERF_SAMPLE_READ) {
  378. fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
  379. return -1;
  380. }
  381. if (type & PERF_SAMPLE_CALLCHAIN) {
  382. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  383. return -EFAULT;
  384. data->callchain = (struct ip_callchain *)array;
  385. if (sample_overlap(event, array, data->callchain->nr))
  386. return -EFAULT;
  387. array += 1 + data->callchain->nr;
  388. }
  389. if (type & PERF_SAMPLE_RAW) {
  390. const u64 *pdata;
  391. u.val64 = *array;
  392. if (WARN_ONCE(swapped,
  393. "Endianness of raw data not corrected!\n")) {
  394. /* undo swap of u64, then swap on individual u32s */
  395. u.val64 = bswap_64(u.val64);
  396. u.val32[0] = bswap_32(u.val32[0]);
  397. u.val32[1] = bswap_32(u.val32[1]);
  398. }
  399. if (sample_overlap(event, array, sizeof(u32)))
  400. return -EFAULT;
  401. data->raw_size = u.val32[0];
  402. pdata = (void *) array + sizeof(u32);
  403. if (sample_overlap(event, pdata, data->raw_size))
  404. return -EFAULT;
  405. data->raw_data = (void *) pdata;
  406. }
  407. return 0;
  408. }