evsel.c 9.7 KB

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