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