evsel.c 10 KB

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