evsel.c 14 KB

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