evsel.c 14 KB

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