evsel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. 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. struct perf_evsel *first)
  56. {
  57. struct perf_event_attr *attr = &evsel->attr;
  58. int track = !evsel->idx; /* only the first counter needs these */
  59. attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
  60. attr->inherit = !opts->no_inherit;
  61. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  62. PERF_FORMAT_TOTAL_TIME_RUNNING |
  63. PERF_FORMAT_ID;
  64. attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  65. /*
  66. * We default some events to a 1 default interval. But keep
  67. * it a weak assumption overridable by the user.
  68. */
  69. if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
  70. opts->user_interval != ULLONG_MAX)) {
  71. if (opts->freq) {
  72. attr->sample_type |= PERF_SAMPLE_PERIOD;
  73. attr->freq = 1;
  74. attr->sample_freq = opts->freq;
  75. } else {
  76. attr->sample_period = opts->default_interval;
  77. }
  78. }
  79. if (opts->no_samples)
  80. attr->sample_freq = 0;
  81. if (opts->inherit_stat)
  82. attr->inherit_stat = 1;
  83. if (opts->sample_address) {
  84. attr->sample_type |= PERF_SAMPLE_ADDR;
  85. attr->mmap_data = track;
  86. }
  87. if (opts->call_graph)
  88. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  89. if (opts->system_wide)
  90. attr->sample_type |= PERF_SAMPLE_CPU;
  91. if (opts->period)
  92. attr->sample_type |= PERF_SAMPLE_PERIOD;
  93. if (!opts->sample_id_all_missing &&
  94. (opts->sample_time || opts->system_wide ||
  95. !opts->no_inherit || opts->cpu_list))
  96. attr->sample_type |= PERF_SAMPLE_TIME;
  97. if (opts->raw_samples) {
  98. attr->sample_type |= PERF_SAMPLE_TIME;
  99. attr->sample_type |= PERF_SAMPLE_RAW;
  100. attr->sample_type |= PERF_SAMPLE_CPU;
  101. }
  102. if (opts->no_delay) {
  103. attr->watermark = 0;
  104. attr->wakeup_events = 1;
  105. }
  106. if (opts->branch_stack) {
  107. attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
  108. attr->branch_sample_type = opts->branch_stack;
  109. }
  110. attr->mmap = track;
  111. attr->comm = track;
  112. if (!opts->target_pid && !opts->target_tid && !opts->system_wide &&
  113. (!opts->group || evsel == first)) {
  114. attr->disabled = 1;
  115. attr->enable_on_exec = 1;
  116. }
  117. }
  118. int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  119. {
  120. int cpu, thread;
  121. evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
  122. if (evsel->fd) {
  123. for (cpu = 0; cpu < ncpus; cpu++) {
  124. for (thread = 0; thread < nthreads; thread++) {
  125. FD(evsel, cpu, thread) = -1;
  126. }
  127. }
  128. }
  129. return evsel->fd != NULL ? 0 : -ENOMEM;
  130. }
  131. int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
  132. {
  133. evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
  134. if (evsel->sample_id == NULL)
  135. return -ENOMEM;
  136. evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
  137. if (evsel->id == NULL) {
  138. xyarray__delete(evsel->sample_id);
  139. evsel->sample_id = NULL;
  140. return -ENOMEM;
  141. }
  142. return 0;
  143. }
  144. int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
  145. {
  146. evsel->counts = zalloc((sizeof(*evsel->counts) +
  147. (ncpus * sizeof(struct perf_counts_values))));
  148. return evsel->counts != NULL ? 0 : -ENOMEM;
  149. }
  150. void perf_evsel__free_fd(struct perf_evsel *evsel)
  151. {
  152. xyarray__delete(evsel->fd);
  153. evsel->fd = NULL;
  154. }
  155. void perf_evsel__free_id(struct perf_evsel *evsel)
  156. {
  157. xyarray__delete(evsel->sample_id);
  158. evsel->sample_id = NULL;
  159. free(evsel->id);
  160. evsel->id = NULL;
  161. }
  162. void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
  163. {
  164. int cpu, thread;
  165. for (cpu = 0; cpu < ncpus; cpu++)
  166. for (thread = 0; thread < nthreads; ++thread) {
  167. close(FD(evsel, cpu, thread));
  168. FD(evsel, cpu, thread) = -1;
  169. }
  170. }
  171. void perf_evsel__exit(struct perf_evsel *evsel)
  172. {
  173. assert(list_empty(&evsel->node));
  174. xyarray__delete(evsel->fd);
  175. xyarray__delete(evsel->sample_id);
  176. free(evsel->id);
  177. }
  178. void perf_evsel__delete(struct perf_evsel *evsel)
  179. {
  180. perf_evsel__exit(evsel);
  181. close_cgroup(evsel->cgrp);
  182. free(evsel->name);
  183. free(evsel);
  184. }
  185. int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
  186. int cpu, int thread, bool scale)
  187. {
  188. struct perf_counts_values count;
  189. size_t nv = scale ? 3 : 1;
  190. if (FD(evsel, cpu, thread) < 0)
  191. return -EINVAL;
  192. if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
  193. return -ENOMEM;
  194. if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
  195. return -errno;
  196. if (scale) {
  197. if (count.run == 0)
  198. count.val = 0;
  199. else if (count.run < count.ena)
  200. count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
  201. } else
  202. count.ena = count.run = 0;
  203. evsel->counts->cpu[cpu] = count;
  204. return 0;
  205. }
  206. int __perf_evsel__read(struct perf_evsel *evsel,
  207. int ncpus, int nthreads, bool scale)
  208. {
  209. size_t nv = scale ? 3 : 1;
  210. int cpu, thread;
  211. struct perf_counts_values *aggr = &evsel->counts->aggr, count;
  212. aggr->val = aggr->ena = aggr->run = 0;
  213. for (cpu = 0; cpu < ncpus; cpu++) {
  214. for (thread = 0; thread < nthreads; thread++) {
  215. if (FD(evsel, cpu, thread) < 0)
  216. continue;
  217. if (readn(FD(evsel, cpu, thread),
  218. &count, nv * sizeof(u64)) < 0)
  219. return -errno;
  220. aggr->val += count.val;
  221. if (scale) {
  222. aggr->ena += count.ena;
  223. aggr->run += count.run;
  224. }
  225. }
  226. }
  227. evsel->counts->scaled = 0;
  228. if (scale) {
  229. if (aggr->run == 0) {
  230. evsel->counts->scaled = -1;
  231. aggr->val = 0;
  232. return 0;
  233. }
  234. if (aggr->run < aggr->ena) {
  235. evsel->counts->scaled = 1;
  236. aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
  237. }
  238. } else
  239. aggr->ena = aggr->run = 0;
  240. return 0;
  241. }
  242. static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  243. struct thread_map *threads, bool group,
  244. struct xyarray *group_fds)
  245. {
  246. int cpu, thread;
  247. unsigned long flags = 0;
  248. int pid = -1, err;
  249. if (evsel->fd == NULL &&
  250. perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
  251. return -ENOMEM;
  252. if (evsel->cgrp) {
  253. flags = PERF_FLAG_PID_CGROUP;
  254. pid = evsel->cgrp->fd;
  255. }
  256. for (cpu = 0; cpu < cpus->nr; cpu++) {
  257. int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
  258. for (thread = 0; thread < threads->nr; thread++) {
  259. if (!evsel->cgrp)
  260. pid = threads->map[thread];
  261. FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
  262. pid,
  263. cpus->map[cpu],
  264. group_fd, flags);
  265. if (FD(evsel, cpu, thread) < 0) {
  266. err = -errno;
  267. goto out_close;
  268. }
  269. if (group && group_fd == -1)
  270. group_fd = FD(evsel, cpu, thread);
  271. }
  272. }
  273. return 0;
  274. out_close:
  275. do {
  276. while (--thread >= 0) {
  277. close(FD(evsel, cpu, thread));
  278. FD(evsel, cpu, thread) = -1;
  279. }
  280. thread = threads->nr;
  281. } while (--cpu >= 0);
  282. return err;
  283. }
  284. void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
  285. {
  286. if (evsel->fd == NULL)
  287. return;
  288. perf_evsel__close_fd(evsel, ncpus, nthreads);
  289. perf_evsel__free_fd(evsel);
  290. evsel->fd = NULL;
  291. }
  292. static struct {
  293. struct cpu_map map;
  294. int cpus[1];
  295. } empty_cpu_map = {
  296. .map.nr = 1,
  297. .cpus = { -1, },
  298. };
  299. static struct {
  300. struct thread_map map;
  301. int threads[1];
  302. } empty_thread_map = {
  303. .map.nr = 1,
  304. .threads = { -1, },
  305. };
  306. int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
  307. struct thread_map *threads, bool group,
  308. struct xyarray *group_fd)
  309. {
  310. if (cpus == NULL) {
  311. /* Work around old compiler warnings about strict aliasing */
  312. cpus = &empty_cpu_map.map;
  313. }
  314. if (threads == NULL)
  315. threads = &empty_thread_map.map;
  316. return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
  317. }
  318. int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
  319. struct cpu_map *cpus, bool group,
  320. struct xyarray *group_fd)
  321. {
  322. return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
  323. group_fd);
  324. }
  325. int perf_evsel__open_per_thread(struct perf_evsel *evsel,
  326. struct thread_map *threads, bool group,
  327. struct xyarray *group_fd)
  328. {
  329. return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
  330. group_fd);
  331. }
  332. static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
  333. struct perf_sample *sample)
  334. {
  335. const u64 *array = event->sample.array;
  336. array += ((event->header.size -
  337. sizeof(event->header)) / sizeof(u64)) - 1;
  338. if (type & PERF_SAMPLE_CPU) {
  339. u32 *p = (u32 *)array;
  340. sample->cpu = *p;
  341. array--;
  342. }
  343. if (type & PERF_SAMPLE_STREAM_ID) {
  344. sample->stream_id = *array;
  345. array--;
  346. }
  347. if (type & PERF_SAMPLE_ID) {
  348. sample->id = *array;
  349. array--;
  350. }
  351. if (type & PERF_SAMPLE_TIME) {
  352. sample->time = *array;
  353. array--;
  354. }
  355. if (type & PERF_SAMPLE_TID) {
  356. u32 *p = (u32 *)array;
  357. sample->pid = p[0];
  358. sample->tid = p[1];
  359. }
  360. return 0;
  361. }
  362. static bool sample_overlap(const union perf_event *event,
  363. const void *offset, u64 size)
  364. {
  365. const void *base = event;
  366. if (offset + size > base + event->header.size)
  367. return true;
  368. return false;
  369. }
  370. int perf_event__parse_sample(const union perf_event *event, u64 type,
  371. int sample_size, bool sample_id_all,
  372. struct perf_sample *data, bool swapped)
  373. {
  374. const u64 *array;
  375. /*
  376. * used for cross-endian analysis. See git commit 65014ab3
  377. * for why this goofiness is needed.
  378. */
  379. union {
  380. u64 val64;
  381. u32 val32[2];
  382. } u;
  383. memset(data, 0, sizeof(*data));
  384. data->cpu = data->pid = data->tid = -1;
  385. data->stream_id = data->id = data->time = -1ULL;
  386. data->period = 1;
  387. if (event->header.type != PERF_RECORD_SAMPLE) {
  388. if (!sample_id_all)
  389. return 0;
  390. return perf_event__parse_id_sample(event, type, data);
  391. }
  392. array = event->sample.array;
  393. if (sample_size + sizeof(event->header) > event->header.size)
  394. return -EFAULT;
  395. if (type & PERF_SAMPLE_IP) {
  396. data->ip = event->ip.ip;
  397. array++;
  398. }
  399. if (type & PERF_SAMPLE_TID) {
  400. u.val64 = *array;
  401. if (swapped) {
  402. /* undo swap of u64, then swap on individual u32s */
  403. u.val64 = bswap_64(u.val64);
  404. u.val32[0] = bswap_32(u.val32[0]);
  405. u.val32[1] = bswap_32(u.val32[1]);
  406. }
  407. data->pid = u.val32[0];
  408. data->tid = u.val32[1];
  409. array++;
  410. }
  411. if (type & PERF_SAMPLE_TIME) {
  412. data->time = *array;
  413. array++;
  414. }
  415. data->addr = 0;
  416. if (type & PERF_SAMPLE_ADDR) {
  417. data->addr = *array;
  418. array++;
  419. }
  420. data->id = -1ULL;
  421. if (type & PERF_SAMPLE_ID) {
  422. data->id = *array;
  423. array++;
  424. }
  425. if (type & PERF_SAMPLE_STREAM_ID) {
  426. data->stream_id = *array;
  427. array++;
  428. }
  429. if (type & PERF_SAMPLE_CPU) {
  430. u.val64 = *array;
  431. if (swapped) {
  432. /* undo swap of u64, then swap on individual u32s */
  433. u.val64 = bswap_64(u.val64);
  434. u.val32[0] = bswap_32(u.val32[0]);
  435. }
  436. data->cpu = u.val32[0];
  437. array++;
  438. }
  439. if (type & PERF_SAMPLE_PERIOD) {
  440. data->period = *array;
  441. array++;
  442. }
  443. if (type & PERF_SAMPLE_READ) {
  444. fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
  445. return -1;
  446. }
  447. if (type & PERF_SAMPLE_CALLCHAIN) {
  448. if (sample_overlap(event, array, sizeof(data->callchain->nr)))
  449. return -EFAULT;
  450. data->callchain = (struct ip_callchain *)array;
  451. if (sample_overlap(event, array, data->callchain->nr))
  452. return -EFAULT;
  453. array += 1 + data->callchain->nr;
  454. }
  455. if (type & PERF_SAMPLE_RAW) {
  456. const u64 *pdata;
  457. u.val64 = *array;
  458. if (WARN_ONCE(swapped,
  459. "Endianness of raw data not corrected!\n")) {
  460. /* undo swap of u64, then swap on individual u32s */
  461. u.val64 = bswap_64(u.val64);
  462. u.val32[0] = bswap_32(u.val32[0]);
  463. u.val32[1] = bswap_32(u.val32[1]);
  464. }
  465. if (sample_overlap(event, array, sizeof(u32)))
  466. return -EFAULT;
  467. data->raw_size = u.val32[0];
  468. pdata = (void *) array + sizeof(u32);
  469. if (sample_overlap(event, pdata, data->raw_size))
  470. return -EFAULT;
  471. data->raw_data = (void *) pdata;
  472. array = (void *)array + data->raw_size + sizeof(u32);
  473. }
  474. if (type & PERF_SAMPLE_BRANCH_STACK) {
  475. u64 sz;
  476. data->branch_stack = (struct branch_stack *)array;
  477. array++; /* nr */
  478. sz = data->branch_stack->nr * sizeof(struct branch_entry);
  479. sz /= sizeof(u64);
  480. array += sz;
  481. }
  482. return 0;
  483. }
  484. int perf_event__synthesize_sample(union perf_event *event, u64 type,
  485. const struct perf_sample *sample,
  486. bool swapped)
  487. {
  488. u64 *array;
  489. /*
  490. * used for cross-endian analysis. See git commit 65014ab3
  491. * for why this goofiness is needed.
  492. */
  493. union {
  494. u64 val64;
  495. u32 val32[2];
  496. } u;
  497. array = event->sample.array;
  498. if (type & PERF_SAMPLE_IP) {
  499. event->ip.ip = sample->ip;
  500. array++;
  501. }
  502. if (type & PERF_SAMPLE_TID) {
  503. u.val32[0] = sample->pid;
  504. u.val32[1] = sample->tid;
  505. if (swapped) {
  506. /*
  507. * Inverse of what is done in perf_event__parse_sample
  508. */
  509. u.val32[0] = bswap_32(u.val32[0]);
  510. u.val32[1] = bswap_32(u.val32[1]);
  511. u.val64 = bswap_64(u.val64);
  512. }
  513. *array = u.val64;
  514. array++;
  515. }
  516. if (type & PERF_SAMPLE_TIME) {
  517. *array = sample->time;
  518. array++;
  519. }
  520. if (type & PERF_SAMPLE_ADDR) {
  521. *array = sample->addr;
  522. array++;
  523. }
  524. if (type & PERF_SAMPLE_ID) {
  525. *array = sample->id;
  526. array++;
  527. }
  528. if (type & PERF_SAMPLE_STREAM_ID) {
  529. *array = sample->stream_id;
  530. array++;
  531. }
  532. if (type & PERF_SAMPLE_CPU) {
  533. u.val32[0] = sample->cpu;
  534. if (swapped) {
  535. /*
  536. * Inverse of what is done in perf_event__parse_sample
  537. */
  538. u.val32[0] = bswap_32(u.val32[0]);
  539. u.val64 = bswap_64(u.val64);
  540. }
  541. *array = u.val64;
  542. array++;
  543. }
  544. if (type & PERF_SAMPLE_PERIOD) {
  545. *array = sample->period;
  546. array++;
  547. }
  548. return 0;
  549. }