evsel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 <byteswap.h>
  10. #include "asm/bug.h"
  11. #include "evsel.h"
  12. #include "evlist.h"
  13. #include "util.h"
  14. #include "cpumap.h"
  15. #include "thread_map.h"
  16. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  17. #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
  18. int __perf_evsel__sample_size(u64 sample_type)
  19. {
  20. u64 mask = sample_type & PERF_SAMPLE_MASK;
  21. int size = 0;
  22. int i;
  23. for (i = 0; i < 64; i++) {
  24. if (mask & (1ULL << i))
  25. size++;
  26. }
  27. size *= sizeof(u64);
  28. return size;
  29. }
  30. static void hists__init(struct hists *hists)
  31. {
  32. memset(hists, 0, sizeof(*hists));
  33. hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
  34. hists->entries_in = &hists->entries_in_array[0];
  35. hists->entries_collapsed = RB_ROOT;
  36. hists->entries = RB_ROOT;
  37. pthread_mutex_init(&hists->lock, NULL);
  38. }
  39. void perf_evsel__init(struct perf_evsel *evsel,
  40. struct perf_event_attr *attr, int idx)
  41. {
  42. evsel->idx = idx;
  43. evsel->attr = *attr;
  44. INIT_LIST_HEAD(&evsel->node);
  45. hists__init(&evsel->hists);
  46. }
  47. struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
  48. {
  49. struct perf_evsel *evsel = zalloc(sizeof(*evsel));
  50. if (evsel != NULL)
  51. perf_evsel__init(evsel, attr, idx);
  52. return evsel;
  53. }
  54. void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts)
  55. {
  56. struct perf_event_attr *attr = &evsel->attr;
  57. int track = !evsel->idx; /* only the first counter needs these */
  58. attr->sample_id_all = opts->sample_id_all_avail ? 1 : 0;
  59. attr->inherit = !opts->no_inherit;
  60. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  61. PERF_FORMAT_TOTAL_TIME_RUNNING |
  62. PERF_FORMAT_ID;
  63. attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  64. /*
  65. * We default some events to a 1 default interval. But keep
  66. * it a weak assumption overridable by the user.
  67. */
  68. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  69. opts->user_interval != ULLONG_MAX)) {
  70. if (opts->freq) {
  71. attr->sample_type |= PERF_SAMPLE_PERIOD;
  72. attr->freq = 1;
  73. attr->sample_freq = opts->freq;
  74. } else {
  75. attr->sample_period = opts->default_interval;
  76. }
  77. }
  78. if (opts->no_samples)
  79. attr->sample_freq = 0;
  80. if (opts->inherit_stat)
  81. attr->inherit_stat = 1;
  82. if (opts->sample_address) {
  83. attr->sample_type |= PERF_SAMPLE_ADDR;
  84. attr->mmap_data = track;
  85. }
  86. if (opts->call_graph)
  87. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  88. if (opts->system_wide)
  89. attr->sample_type |= PERF_SAMPLE_CPU;
  90. if (opts->sample_id_all_avail &&
  91. (opts->sample_time || opts->system_wide ||
  92. !opts->no_inherit || opts->cpu_list))
  93. attr->sample_type |= PERF_SAMPLE_TIME;
  94. if (opts->raw_samples) {
  95. attr->sample_type |= PERF_SAMPLE_TIME;
  96. attr->sample_type |= PERF_SAMPLE_RAW;
  97. attr->sample_type |= PERF_SAMPLE_CPU;
  98. }
  99. if (opts->no_delay) {
  100. attr->watermark = 0;
  101. attr->wakeup_events = 1;
  102. }
  103. attr->mmap = track;
  104. attr->comm = track;
  105. if (opts->target_pid == -1 && opts->target_tid == -1 && !opts->system_wide) {
  106. attr->disabled = 1;
  107. attr->enable_on_exec = 1;
  108. }
  109. }
  110. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  111. {
  112. int cpu, thread;
  113. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  114. if (evsel->fd) {
  115. for (cpu = 0; cpu < ncpus; cpu++) {
  116. for (thread = 0; thread < nthreads; thread++) {
  117. FD(evsel, cpu, thread) = -1;
  118. }
  119. }
  120. }
  121. return evsel->fd != NULL ? 0 : -ENOMEM;
  122. }
  123. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  124. {
  125. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  126. if (evsel->sample_id == NULL)
  127. return -ENOMEM;
  128. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  129. if (evsel->id == NULL) {
  130. xyarray__delete(evsel->sample_id);
  131. evsel->sample_id = NULL;
  132. return -ENOMEM;
  133. }
  134. return 0;
  135. }
  136. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  137. {
  138. evsel->counts = zalloc((sizeof(*evsel->counts) +
  139. (ncpus * sizeof(struct perf_counts_values))));
  140. return evsel->counts != NULL ? 0 : -ENOMEM;
  141. }
  142. void perf_evsel__free_fd(struct perf_evsel *evsel)
  143. {
  144. xyarray__delete(evsel->fd);
  145. evsel->fd = NULL;
  146. }
  147. void perf_evsel__free_id(struct perf_evsel *evsel)
  148. {
  149. xyarray__delete(evsel->sample_id);
  150. evsel->sample_id = NULL;
  151. free(evsel->id);
  152. evsel->id = NULL;
  153. }
  154. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  155. {
  156. int cpu, thread;
  157. for (cpu = 0; cpu < ncpus; cpu++)
  158. for (thread = 0; thread < nthreads; ++thread) {
  159. close(FD(evsel, cpu, thread));
  160. FD(evsel, cpu, thread) = -1;
  161. }
  162. }
  163. void perf_evsel__exit(struct perf_evsel *evsel)
  164. {
  165. assert(list_empty(&evsel->node));
  166. xyarray__delete(evsel->fd);
  167. xyarray__delete(evsel->sample_id);
  168. free(evsel->id);
  169. }
  170. void perf_evsel__delete(struct perf_evsel *evsel)
  171. {
  172. perf_evsel__exit(evsel);
  173. close_cgroup(evsel->cgrp);
  174. free(evsel->name);
  175. free(evsel);
  176. }
  177. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  178. int cpu, int thread, bool scale)
  179. {
  180. struct perf_counts_values count;
  181. size_t nv = scale ? 3 : 1;
  182. if (FD(evsel, cpu, thread) < 0)
  183. return -EINVAL;
  184. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  185. return -ENOMEM;
  186. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  187. return -errno;
  188. if (scale) {
  189. if (count.run == 0)
  190. count.val = 0;
  191. else if (count.run < count.ena)
  192. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  193. } else
  194. count.ena = count.run = 0;
  195. evsel->counts->cpu[cpu] = count;
  196. return 0;
  197. }
  198. int __perf_evsel__read(struct perf_evsel *evsel,
  199. int ncpus, int nthreads, bool scale)
  200. {
  201. size_t nv = scale ? 3 : 1;
  202. int cpu, thread;
  203. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  204. aggr->val = aggr->ena = aggr->run = 0;
  205. for (cpu = 0; cpu < ncpus; cpu++) {
  206. for (thread = 0; thread < nthreads; thread++) {
  207. if (FD(evsel, cpu, thread) < 0)
  208. continue;
  209. if (readn(FD(evsel, cpu, thread),
  210. &count, nv * sizeof(u64)) < 0)
  211. return -errno;
  212. aggr->val += count.val;
  213. if (scale) {
  214. aggr->ena += count.ena;
  215. aggr->run += count.run;
  216. }
  217. }
  218. }
  219. evsel->counts->scaled = 0;
  220. if (scale) {
  221. if (aggr->run == 0) {
  222. evsel->counts->scaled = -1;
  223. aggr->val = 0;
  224. return 0;
  225. }
  226. if (aggr->run < aggr->ena) {
  227. evsel->counts->scaled = 1;
  228. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  229. }
  230. } else
  231. aggr->ena = aggr->run = 0;
  232. return 0;
  233. }
  234. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  235. struct thread_map *threads, bool group,
  236. struct xyarray *group_fds)
  237. {
  238. int cpu, thread;
  239. unsigned long flags = 0;
  240. int pid = -1, err;
  241. if (evsel->fd == NULL &&
  242. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  243. return -ENOMEM;
  244. if (evsel->cgrp) {
  245. flags = PERF_FLAG_PID_CGROUP;
  246. pid = evsel->cgrp->fd;
  247. }
  248. for (cpu = 0; cpu < cpus->nr; cpu++) {
  249. int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
  250. for (thread = 0; thread < threads->nr; thread++) {
  251. if (!evsel->cgrp)
  252. pid = threads->map[thread];
  253. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  254. pid,
  255. cpus->map[cpu],
  256. group_fd, flags);
  257. if (FD(evsel, cpu, thread) < 0) {
  258. err = -errno;
  259. goto out_close;
  260. }
  261. if (group && group_fd == -1)
  262. group_fd = FD(evsel, cpu, thread);
  263. }
  264. }
  265. return 0;
  266. out_close:
  267. do {
  268. while (--thread >= 0) {
  269. close(FD(evsel, cpu, thread));
  270. FD(evsel, cpu, thread) = -1;
  271. }
  272. thread = threads->nr;
  273. } while (--cpu >= 0);
  274. return err;
  275. }
  276. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  277. {
  278. if (evsel->fd == NULL)
  279. return;
  280. perf_evsel__close_fd(evsel, ncpus, nthreads);
  281. perf_evsel__free_fd(evsel);
  282. evsel->fd = NULL;
  283. }
  284. static struct {
  285. struct cpu_map map;
  286. int cpus[1];
  287. } empty_cpu_map = {
  288. .map.nr = 1,
  289. .cpus = { -1, },
  290. };
  291. static struct {
  292. struct thread_map map;
  293. int threads[1];
  294. } empty_thread_map = {
  295. .map.nr = 1,
  296. .threads = { -1, },
  297. };
  298. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  299. struct thread_map *threads, bool group,
  300. struct xyarray *group_fd)
  301. {
  302. if (cpus == NULL) {
  303. /* Work around old compiler warnings about strict aliasing */
  304. cpus = &empty_cpu_map.map;
  305. }
  306. if (threads == NULL)
  307. threads = &empty_thread_map.map;
  308. return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
  309. }
  310. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  311. struct cpu_map *cpus, bool group,
  312. struct xyarray *group_fd)
  313. {
  314. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
  315. group_fd);
  316. }
  317. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  318. struct thread_map *threads, bool group,
  319. struct xyarray *group_fd)
  320. {
  321. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
  322. group_fd);
  323. }
  324. static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
  325. struct perf_sample *sample)
  326. {
  327. const u64 *array = event->sample.array;
  328. array += ((event->header.size -
  329. sizeof(event->header)) / sizeof(u64)) - 1;
  330. if (type & PERF_SAMPLE_CPU) {
  331. u32 *p = (u32 *)array;
  332. sample->cpu = *p;
  333. array--;
  334. }
  335. if (type & PERF_SAMPLE_STREAM_ID) {
  336. sample->stream_id = *array;
  337. array--;
  338. }
  339. if (type & PERF_SAMPLE_ID) {
  340. sample->id = *array;
  341. array--;
  342. }
  343. if (type & PERF_SAMPLE_TIME) {
  344. sample->time = *array;
  345. array--;
  346. }
  347. if (type & PERF_SAMPLE_TID) {
  348. u32 *p = (u32 *)array;
  349. sample->pid = p[0];
  350. sample->tid = p[1];
  351. }
  352. return 0;
  353. }
  354. static bool sample_overlap(const union perf_event *event,
  355. const void *offset, u64 size)
  356. {
  357. const void *base = event;
  358. if (offset + size > base + event->header.size)
  359. return true;
  360. return false;
  361. }
  362. int perf_event__parse_sample(const union perf_event *event, u64 type,
  363. int sample_size, bool sample_id_all,
  364. struct perf_sample *data, bool swapped)
  365. {
  366. const u64 *array;
  367. /*
  368. * used for cross-endian analysis. See git commit 65014ab3
  369. * for why this goofiness is needed.
  370. */
  371. union {
  372. u64 val64;
  373. u32 val32[2];
  374. } u;
  375. data->cpu = data->pid = data->tid = -1;
  376. data->stream_id = data->id = data->time = -1ULL;
  377. if (event->header.type != PERF_RECORD_SAMPLE) {
  378. if (!sample_id_all)
  379. return 0;
  380. return perf_event__parse_id_sample(event, type, data);
  381. }
  382. array = event->sample.array;
  383. if (sample_size + sizeof(event->header) > event->header.size)
  384. return -EFAULT;
  385. if (type & PERF_SAMPLE_IP) {
  386. data->ip = event->ip.ip;
  387. array++;
  388. }
  389. if (type & PERF_SAMPLE_TID) {
  390. u.val64 = *array;
  391. if (swapped) {
  392. /* undo swap of u64, then swap on individual u32s */
  393. u.val64 = bswap_64(u.val64);
  394. u.val32[0] = bswap_32(u.val32[0]);
  395. u.val32[1] = bswap_32(u.val32[1]);
  396. }
  397. data->pid = u.val32[0];
  398. data->tid = u.val32[1];
  399. array++;
  400. }
  401. if (type & PERF_SAMPLE_TIME) {
  402. data->time = *array;
  403. array++;
  404. }
  405. data->addr = 0;
  406. if (type & PERF_SAMPLE_ADDR) {
  407. data->addr = *array;
  408. array++;
  409. }
  410. data->id = -1ULL;
  411. if (type & PERF_SAMPLE_ID) {
  412. data->id = *array;
  413. array++;
  414. }
  415. if (type & PERF_SAMPLE_STREAM_ID) {
  416. data->stream_id = *array;
  417. array++;
  418. }
  419. if (type & PERF_SAMPLE_CPU) {
  420. u.val64 = *array;
  421. if (swapped) {
  422. /* undo swap of u64, then swap on individual u32s */
  423. u.val64 = bswap_64(u.val64);
  424. u.val32[0] = bswap_32(u.val32[0]);
  425. }
  426. data->cpu = u.val32[0];
  427. array++;
  428. }
  429. if (type & PERF_SAMPLE_PERIOD) {
  430. data->period = *array;
  431. array++;
  432. }
  433. if (type & PERF_SAMPLE_READ) {
  434. fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
  435. return -1;
  436. }
  437. if (type & PERF_SAMPLE_CALLCHAIN) {
  438. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  439. return -EFAULT;
  440. data->callchain = (struct ip_callchain *)array;
  441. if (sample_overlap(event, array, data->callchain->nr))
  442. return -EFAULT;
  443. array += 1 + data->callchain->nr;
  444. }
  445. if (type & PERF_SAMPLE_RAW) {
  446. const u64 *pdata;
  447. u.val64 = *array;
  448. if (WARN_ONCE(swapped,
  449. "Endianness of raw data not corrected!\n")) {
  450. /* undo swap of u64, then swap on individual u32s */
  451. u.val64 = bswap_64(u.val64);
  452. u.val32[0] = bswap_32(u.val32[0]);
  453. u.val32[1] = bswap_32(u.val32[1]);
  454. }
  455. if (sample_overlap(event, array, sizeof(u32)))
  456. return -EFAULT;
  457. data->raw_size = u.val32[0];
  458. pdata = (void *) array + sizeof(u32);
  459. if (sample_overlap(event, pdata, data->raw_size))
  460. return -EFAULT;
  461. data->raw_data = (void *) pdata;
  462. }
  463. return 0;
  464. }