evsel.c 12 KB

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