evsel.c 8.8 KB

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