evsel.c 8.4 KB

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