evsel.c 8.6 KB

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