evsel.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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, bool inherit)
  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. /*
  161. * Don't allow mmap() of inherited per-task counters. This
  162. * would create a performance issue due to all children writing
  163. * to the same buffer.
  164. *
  165. * FIXME:
  166. * Proper fix is not to pass 'inherit' to perf_evsel__open*,
  167. * but a 'flags' parameter, with 'group' folded there as well,
  168. * then introduce a PERF_O_{MMAP,GROUP,INHERIT} enum, and if
  169. * O_MMAP is set, emit a warning if cpu < 0 and O_INHERIT is
  170. * set. Lets go for the minimal fix first tho.
  171. */
  172. evsel->attr.inherit = (cpus->map[cpu] >= 0) && inherit;
  173. for (thread = 0; thread < threads->nr; thread++) {
  174. if (!evsel->cgrp)
  175. pid = threads->map[thread];
  176. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  177. pid,
  178. cpus->map[cpu],
  179. group_fd, flags);
  180. if (FD(evsel, cpu, thread) < 0)
  181. goto out_close;
  182. if (group && group_fd == -1)
  183. group_fd = FD(evsel, cpu, thread);
  184. }
  185. }
  186. return 0;
  187. out_close:
  188. do {
  189. while (--thread >= 0) {
  190. close(FD(evsel, cpu, thread));
  191. FD(evsel, cpu, thread) = -1;
  192. }
  193. thread = threads->nr;
  194. } while (--cpu >= 0);
  195. return -1;
  196. }
  197. static struct {
  198. struct cpu_map map;
  199. int cpus[1];
  200. } empty_cpu_map = {
  201. .map.nr = 1,
  202. .cpus = { -1, },
  203. };
  204. static struct {
  205. struct thread_map map;
  206. int threads[1];
  207. } empty_thread_map = {
  208. .map.nr = 1,
  209. .threads = { -1, },
  210. };
  211. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  212. struct thread_map *threads, bool group, bool inherit)
  213. {
  214. if (cpus == NULL) {
  215. /* Work around old compiler warnings about strict aliasing */
  216. cpus = &empty_cpu_map.map;
  217. }
  218. if (threads == NULL)
  219. threads = &empty_thread_map.map;
  220. return __perf_evsel__open(evsel, cpus, threads, group, inherit);
  221. }
  222. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  223. struct cpu_map *cpus, bool group, bool inherit)
  224. {
  225. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit);
  226. }
  227. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  228. struct thread_map *threads, bool group, bool inherit)
  229. {
  230. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit);
  231. }
  232. static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
  233. struct perf_sample *sample)
  234. {
  235. const u64 *array = event->sample.array;
  236. array += ((event->header.size -
  237. sizeof(event->header)) / sizeof(u64)) - 1;
  238. if (type & PERF_SAMPLE_CPU) {
  239. u32 *p = (u32 *)array;
  240. sample->cpu = *p;
  241. array--;
  242. }
  243. if (type & PERF_SAMPLE_STREAM_ID) {
  244. sample->stream_id = *array;
  245. array--;
  246. }
  247. if (type & PERF_SAMPLE_ID) {
  248. sample->id = *array;
  249. array--;
  250. }
  251. if (type & PERF_SAMPLE_TIME) {
  252. sample->time = *array;
  253. array--;
  254. }
  255. if (type & PERF_SAMPLE_TID) {
  256. u32 *p = (u32 *)array;
  257. sample->pid = p[0];
  258. sample->tid = p[1];
  259. }
  260. return 0;
  261. }
  262. int perf_event__parse_sample(const union perf_event *event, u64 type,
  263. bool sample_id_all, struct perf_sample *data)
  264. {
  265. const u64 *array;
  266. data->cpu = data->pid = data->tid = -1;
  267. data->stream_id = data->id = data->time = -1ULL;
  268. if (event->header.type != PERF_RECORD_SAMPLE) {
  269. if (!sample_id_all)
  270. return 0;
  271. return perf_event__parse_id_sample(event, type, data);
  272. }
  273. array = event->sample.array;
  274. if (type & PERF_SAMPLE_IP) {
  275. data->ip = event->ip.ip;
  276. array++;
  277. }
  278. if (type & PERF_SAMPLE_TID) {
  279. u32 *p = (u32 *)array;
  280. data->pid = p[0];
  281. data->tid = p[1];
  282. array++;
  283. }
  284. if (type & PERF_SAMPLE_TIME) {
  285. data->time = *array;
  286. array++;
  287. }
  288. if (type & PERF_SAMPLE_ADDR) {
  289. data->addr = *array;
  290. array++;
  291. }
  292. data->id = -1ULL;
  293. if (type & PERF_SAMPLE_ID) {
  294. data->id = *array;
  295. array++;
  296. }
  297. if (type & PERF_SAMPLE_STREAM_ID) {
  298. data->stream_id = *array;
  299. array++;
  300. }
  301. if (type & PERF_SAMPLE_CPU) {
  302. u32 *p = (u32 *)array;
  303. data->cpu = *p;
  304. array++;
  305. }
  306. if (type & PERF_SAMPLE_PERIOD) {
  307. data->period = *array;
  308. array++;
  309. }
  310. if (type & PERF_SAMPLE_READ) {
  311. fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
  312. return -1;
  313. }
  314. if (type & PERF_SAMPLE_CALLCHAIN) {
  315. data->callchain = (struct ip_callchain *)array;
  316. array += 1 + data->callchain->nr;
  317. }
  318. if (type & PERF_SAMPLE_RAW) {
  319. u32 *p = (u32 *)array;
  320. data->raw_size = *p;
  321. p++;
  322. data->raw_data = p;
  323. }
  324. return 0;
  325. }