evsel.c 8.2 KB

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