evsel.c 9.7 KB

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