evsel.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. free(evsel);
  74. }
  75. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  76. int cpu, int thread, bool scale)
  77. {
  78. struct perf_counts_values count;
  79. size_t nv = scale ? 3 : 1;
  80. if (FD(evsel, cpu, thread) < 0)
  81. return -EINVAL;
  82. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  83. return -ENOMEM;
  84. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  85. return -errno;
  86. if (scale) {
  87. if (count.run == 0)
  88. count.val = 0;
  89. else if (count.run < count.ena)
  90. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  91. } else
  92. count.ena = count.run = 0;
  93. evsel->counts->cpu[cpu] = count;
  94. return 0;
  95. }
  96. int __perf_evsel__read(struct perf_evsel *evsel,
  97. int ncpus, int nthreads, bool scale)
  98. {
  99. size_t nv = scale ? 3 : 1;
  100. int cpu, thread;
  101. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  102. aggr->val = 0;
  103. for (cpu = 0; cpu < ncpus; cpu++) {
  104. for (thread = 0; thread < nthreads; thread++) {
  105. if (FD(evsel, cpu, thread) < 0)
  106. continue;
  107. if (readn(FD(evsel, cpu, thread),
  108. &count, nv * sizeof(u64)) < 0)
  109. return -errno;
  110. aggr->val += count.val;
  111. if (scale) {
  112. aggr->ena += count.ena;
  113. aggr->run += count.run;
  114. }
  115. }
  116. }
  117. evsel->counts->scaled = 0;
  118. if (scale) {
  119. if (aggr->run == 0) {
  120. evsel->counts->scaled = -1;
  121. aggr->val = 0;
  122. return 0;
  123. }
  124. if (aggr->run < aggr->ena) {
  125. evsel->counts->scaled = 1;
  126. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  127. }
  128. } else
  129. aggr->ena = aggr->run = 0;
  130. return 0;
  131. }
  132. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  133. struct thread_map *threads, bool group, bool inherit)
  134. {
  135. int cpu, thread;
  136. if (evsel->fd == NULL &&
  137. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  138. return -1;
  139. for (cpu = 0; cpu < cpus->nr; cpu++) {
  140. int group_fd = -1;
  141. evsel->attr.inherit = (cpus->map[cpu] < 0) && inherit;
  142. for (thread = 0; thread < threads->nr; thread++) {
  143. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  144. threads->map[thread],
  145. cpus->map[cpu],
  146. group_fd, 0);
  147. if (FD(evsel, cpu, thread) < 0)
  148. goto out_close;
  149. if (group && group_fd == -1)
  150. group_fd = FD(evsel, cpu, thread);
  151. }
  152. }
  153. return 0;
  154. out_close:
  155. do {
  156. while (--thread >= 0) {
  157. close(FD(evsel, cpu, thread));
  158. FD(evsel, cpu, thread) = -1;
  159. }
  160. thread = threads->nr;
  161. } while (--cpu >= 0);
  162. return -1;
  163. }
  164. static struct {
  165. struct cpu_map map;
  166. int cpus[1];
  167. } empty_cpu_map = {
  168. .map.nr = 1,
  169. .cpus = { -1, },
  170. };
  171. static struct {
  172. struct thread_map map;
  173. int threads[1];
  174. } empty_thread_map = {
  175. .map.nr = 1,
  176. .threads = { -1, },
  177. };
  178. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  179. struct thread_map *threads, bool group, bool inherit)
  180. {
  181. if (cpus == NULL) {
  182. /* Work around old compiler warnings about strict aliasing */
  183. cpus = &empty_cpu_map.map;
  184. }
  185. if (threads == NULL)
  186. threads = &empty_thread_map.map;
  187. return __perf_evsel__open(evsel, cpus, threads, group, inherit);
  188. }
  189. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  190. struct cpu_map *cpus, bool group, bool inherit)
  191. {
  192. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit);
  193. }
  194. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  195. struct thread_map *threads, bool group, bool inherit)
  196. {
  197. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit);
  198. }
  199. static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
  200. struct perf_sample *sample)
  201. {
  202. const u64 *array = event->sample.array;
  203. array += ((event->header.size -
  204. sizeof(event->header)) / sizeof(u64)) - 1;
  205. if (type & PERF_SAMPLE_CPU) {
  206. u32 *p = (u32 *)array;
  207. sample->cpu = *p;
  208. array--;
  209. }
  210. if (type & PERF_SAMPLE_STREAM_ID) {
  211. sample->stream_id = *array;
  212. array--;
  213. }
  214. if (type & PERF_SAMPLE_ID) {
  215. sample->id = *array;
  216. array--;
  217. }
  218. if (type & PERF_SAMPLE_TIME) {
  219. sample->time = *array;
  220. array--;
  221. }
  222. if (type & PERF_SAMPLE_TID) {
  223. u32 *p = (u32 *)array;
  224. sample->pid = p[0];
  225. sample->tid = p[1];
  226. }
  227. return 0;
  228. }
  229. int perf_event__parse_sample(const union perf_event *event, u64 type,
  230. bool sample_id_all, struct perf_sample *data)
  231. {
  232. const u64 *array;
  233. data->cpu = data->pid = data->tid = -1;
  234. data->stream_id = data->id = data->time = -1ULL;
  235. if (event->header.type != PERF_RECORD_SAMPLE) {
  236. if (!sample_id_all)
  237. return 0;
  238. return perf_event__parse_id_sample(event, type, data);
  239. }
  240. array = event->sample.array;
  241. if (type & PERF_SAMPLE_IP) {
  242. data->ip = event->ip.ip;
  243. array++;
  244. }
  245. if (type & PERF_SAMPLE_TID) {
  246. u32 *p = (u32 *)array;
  247. data->pid = p[0];
  248. data->tid = p[1];
  249. array++;
  250. }
  251. if (type & PERF_SAMPLE_TIME) {
  252. data->time = *array;
  253. array++;
  254. }
  255. if (type & PERF_SAMPLE_ADDR) {
  256. data->addr = *array;
  257. array++;
  258. }
  259. data->id = -1ULL;
  260. if (type & PERF_SAMPLE_ID) {
  261. data->id = *array;
  262. array++;
  263. }
  264. if (type & PERF_SAMPLE_STREAM_ID) {
  265. data->stream_id = *array;
  266. array++;
  267. }
  268. if (type & PERF_SAMPLE_CPU) {
  269. u32 *p = (u32 *)array;
  270. data->cpu = *p;
  271. array++;
  272. }
  273. if (type & PERF_SAMPLE_PERIOD) {
  274. data->period = *array;
  275. array++;
  276. }
  277. if (type & PERF_SAMPLE_READ) {
  278. fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
  279. return -1;
  280. }
  281. if (type & PERF_SAMPLE_CALLCHAIN) {
  282. data->callchain = (struct ip_callchain *)array;
  283. array += 1 + data->callchain->nr;
  284. }
  285. if (type & PERF_SAMPLE_RAW) {
  286. u32 *p = (u32 *)array;
  287. data->raw_size = *p;
  288. p++;
  289. data->raw_data = p;
  290. }
  291. return 0;
  292. }