evlist.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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 "util.h"
  10. #include <api/fs/debugfs.h>
  11. #include <poll.h>
  12. #include "cpumap.h"
  13. #include "thread_map.h"
  14. #include "target.h"
  15. #include "evlist.h"
  16. #include "evsel.h"
  17. #include "debug.h"
  18. #include <unistd.h>
  19. #include "parse-events.h"
  20. #include "parse-options.h"
  21. #include <sys/mman.h>
  22. #include <linux/bitops.h>
  23. #include <linux/hash.h>
  24. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  25. #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
  26. void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
  27. struct thread_map *threads)
  28. {
  29. int i;
  30. for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
  31. INIT_HLIST_HEAD(&evlist->heads[i]);
  32. INIT_LIST_HEAD(&evlist->entries);
  33. perf_evlist__set_maps(evlist, cpus, threads);
  34. evlist->workload.pid = -1;
  35. }
  36. struct perf_evlist *perf_evlist__new(void)
  37. {
  38. struct perf_evlist *evlist = zalloc(sizeof(*evlist));
  39. if (evlist != NULL)
  40. perf_evlist__init(evlist, NULL, NULL);
  41. return evlist;
  42. }
  43. struct perf_evlist *perf_evlist__new_default(void)
  44. {
  45. struct perf_evlist *evlist = perf_evlist__new();
  46. if (evlist && perf_evlist__add_default(evlist)) {
  47. perf_evlist__delete(evlist);
  48. evlist = NULL;
  49. }
  50. return evlist;
  51. }
  52. /**
  53. * perf_evlist__set_id_pos - set the positions of event ids.
  54. * @evlist: selected event list
  55. *
  56. * Events with compatible sample types all have the same id_pos
  57. * and is_pos. For convenience, put a copy on evlist.
  58. */
  59. void perf_evlist__set_id_pos(struct perf_evlist *evlist)
  60. {
  61. struct perf_evsel *first = perf_evlist__first(evlist);
  62. evlist->id_pos = first->id_pos;
  63. evlist->is_pos = first->is_pos;
  64. }
  65. static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
  66. {
  67. struct perf_evsel *evsel;
  68. evlist__for_each(evlist, evsel)
  69. perf_evsel__calc_id_pos(evsel);
  70. perf_evlist__set_id_pos(evlist);
  71. }
  72. static void perf_evlist__purge(struct perf_evlist *evlist)
  73. {
  74. struct perf_evsel *pos, *n;
  75. evlist__for_each_safe(evlist, n, pos) {
  76. list_del_init(&pos->node);
  77. perf_evsel__delete(pos);
  78. }
  79. evlist->nr_entries = 0;
  80. }
  81. void perf_evlist__exit(struct perf_evlist *evlist)
  82. {
  83. zfree(&evlist->mmap);
  84. zfree(&evlist->pollfd);
  85. }
  86. void perf_evlist__delete(struct perf_evlist *evlist)
  87. {
  88. perf_evlist__munmap(evlist);
  89. perf_evlist__close(evlist);
  90. cpu_map__delete(evlist->cpus);
  91. thread_map__delete(evlist->threads);
  92. evlist->cpus = NULL;
  93. evlist->threads = NULL;
  94. perf_evlist__purge(evlist);
  95. perf_evlist__exit(evlist);
  96. free(evlist);
  97. }
  98. void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
  99. {
  100. list_add_tail(&entry->node, &evlist->entries);
  101. entry->idx = evlist->nr_entries;
  102. if (!evlist->nr_entries++)
  103. perf_evlist__set_id_pos(evlist);
  104. }
  105. void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
  106. struct list_head *list,
  107. int nr_entries)
  108. {
  109. bool set_id_pos = !evlist->nr_entries;
  110. list_splice_tail(list, &evlist->entries);
  111. evlist->nr_entries += nr_entries;
  112. if (set_id_pos)
  113. perf_evlist__set_id_pos(evlist);
  114. }
  115. void __perf_evlist__set_leader(struct list_head *list)
  116. {
  117. struct perf_evsel *evsel, *leader;
  118. leader = list_entry(list->next, struct perf_evsel, node);
  119. evsel = list_entry(list->prev, struct perf_evsel, node);
  120. leader->nr_members = evsel->idx - leader->idx + 1;
  121. __evlist__for_each(list, evsel) {
  122. evsel->leader = leader;
  123. }
  124. }
  125. void perf_evlist__set_leader(struct perf_evlist *evlist)
  126. {
  127. if (evlist->nr_entries) {
  128. evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
  129. __perf_evlist__set_leader(&evlist->entries);
  130. }
  131. }
  132. int perf_evlist__add_default(struct perf_evlist *evlist)
  133. {
  134. struct perf_event_attr attr = {
  135. .type = PERF_TYPE_HARDWARE,
  136. .config = PERF_COUNT_HW_CPU_CYCLES,
  137. };
  138. struct perf_evsel *evsel;
  139. event_attr_init(&attr);
  140. evsel = perf_evsel__new(&attr);
  141. if (evsel == NULL)
  142. goto error;
  143. /* use strdup() because free(evsel) assumes name is allocated */
  144. evsel->name = strdup("cycles");
  145. if (!evsel->name)
  146. goto error_free;
  147. perf_evlist__add(evlist, evsel);
  148. return 0;
  149. error_free:
  150. perf_evsel__delete(evsel);
  151. error:
  152. return -ENOMEM;
  153. }
  154. static int perf_evlist__add_attrs(struct perf_evlist *evlist,
  155. struct perf_event_attr *attrs, size_t nr_attrs)
  156. {
  157. struct perf_evsel *evsel, *n;
  158. LIST_HEAD(head);
  159. size_t i;
  160. for (i = 0; i < nr_attrs; i++) {
  161. evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
  162. if (evsel == NULL)
  163. goto out_delete_partial_list;
  164. list_add_tail(&evsel->node, &head);
  165. }
  166. perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
  167. return 0;
  168. out_delete_partial_list:
  169. __evlist__for_each_safe(&head, n, evsel)
  170. perf_evsel__delete(evsel);
  171. return -1;
  172. }
  173. int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
  174. struct perf_event_attr *attrs, size_t nr_attrs)
  175. {
  176. size_t i;
  177. for (i = 0; i < nr_attrs; i++)
  178. event_attr_init(attrs + i);
  179. return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
  180. }
  181. struct perf_evsel *
  182. perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
  183. {
  184. struct perf_evsel *evsel;
  185. evlist__for_each(evlist, evsel) {
  186. if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
  187. (int)evsel->attr.config == id)
  188. return evsel;
  189. }
  190. return NULL;
  191. }
  192. struct perf_evsel *
  193. perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
  194. const char *name)
  195. {
  196. struct perf_evsel *evsel;
  197. evlist__for_each(evlist, evsel) {
  198. if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
  199. (strcmp(evsel->name, name) == 0))
  200. return evsel;
  201. }
  202. return NULL;
  203. }
  204. int perf_evlist__add_newtp(struct perf_evlist *evlist,
  205. const char *sys, const char *name, void *handler)
  206. {
  207. struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
  208. if (evsel == NULL)
  209. return -1;
  210. evsel->handler = handler;
  211. perf_evlist__add(evlist, evsel);
  212. return 0;
  213. }
  214. void perf_evlist__disable(struct perf_evlist *evlist)
  215. {
  216. int cpu, thread;
  217. struct perf_evsel *pos;
  218. int nr_cpus = cpu_map__nr(evlist->cpus);
  219. int nr_threads = thread_map__nr(evlist->threads);
  220. for (cpu = 0; cpu < nr_cpus; cpu++) {
  221. evlist__for_each(evlist, pos) {
  222. if (!perf_evsel__is_group_leader(pos) || !pos->fd)
  223. continue;
  224. for (thread = 0; thread < nr_threads; thread++)
  225. ioctl(FD(pos, cpu, thread),
  226. PERF_EVENT_IOC_DISABLE, 0);
  227. }
  228. }
  229. }
  230. void perf_evlist__enable(struct perf_evlist *evlist)
  231. {
  232. int cpu, thread;
  233. struct perf_evsel *pos;
  234. int nr_cpus = cpu_map__nr(evlist->cpus);
  235. int nr_threads = thread_map__nr(evlist->threads);
  236. for (cpu = 0; cpu < nr_cpus; cpu++) {
  237. evlist__for_each(evlist, pos) {
  238. if (!perf_evsel__is_group_leader(pos) || !pos->fd)
  239. continue;
  240. for (thread = 0; thread < nr_threads; thread++)
  241. ioctl(FD(pos, cpu, thread),
  242. PERF_EVENT_IOC_ENABLE, 0);
  243. }
  244. }
  245. }
  246. int perf_evlist__disable_event(struct perf_evlist *evlist,
  247. struct perf_evsel *evsel)
  248. {
  249. int cpu, thread, err;
  250. if (!evsel->fd)
  251. return 0;
  252. for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
  253. for (thread = 0; thread < evlist->threads->nr; thread++) {
  254. err = ioctl(FD(evsel, cpu, thread),
  255. PERF_EVENT_IOC_DISABLE, 0);
  256. if (err)
  257. return err;
  258. }
  259. }
  260. return 0;
  261. }
  262. int perf_evlist__enable_event(struct perf_evlist *evlist,
  263. struct perf_evsel *evsel)
  264. {
  265. int cpu, thread, err;
  266. if (!evsel->fd)
  267. return -EINVAL;
  268. for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
  269. for (thread = 0; thread < evlist->threads->nr; thread++) {
  270. err = ioctl(FD(evsel, cpu, thread),
  271. PERF_EVENT_IOC_ENABLE, 0);
  272. if (err)
  273. return err;
  274. }
  275. }
  276. return 0;
  277. }
  278. static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
  279. {
  280. int nr_cpus = cpu_map__nr(evlist->cpus);
  281. int nr_threads = thread_map__nr(evlist->threads);
  282. int nfds = nr_cpus * nr_threads * evlist->nr_entries;
  283. evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
  284. return evlist->pollfd != NULL ? 0 : -ENOMEM;
  285. }
  286. void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
  287. {
  288. fcntl(fd, F_SETFL, O_NONBLOCK);
  289. evlist->pollfd[evlist->nr_fds].fd = fd;
  290. evlist->pollfd[evlist->nr_fds].events = POLLIN;
  291. evlist->nr_fds++;
  292. }
  293. static void perf_evlist__id_hash(struct perf_evlist *evlist,
  294. struct perf_evsel *evsel,
  295. int cpu, int thread, u64 id)
  296. {
  297. int hash;
  298. struct perf_sample_id *sid = SID(evsel, cpu, thread);
  299. sid->id = id;
  300. sid->evsel = evsel;
  301. hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
  302. hlist_add_head(&sid->node, &evlist->heads[hash]);
  303. }
  304. void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
  305. int cpu, int thread, u64 id)
  306. {
  307. perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
  308. evsel->id[evsel->ids++] = id;
  309. }
  310. static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
  311. struct perf_evsel *evsel,
  312. int cpu, int thread, int fd)
  313. {
  314. u64 read_data[4] = { 0, };
  315. int id_idx = 1; /* The first entry is the counter value */
  316. u64 id;
  317. int ret;
  318. ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
  319. if (!ret)
  320. goto add;
  321. if (errno != ENOTTY)
  322. return -1;
  323. /* Legacy way to get event id.. All hail to old kernels! */
  324. /*
  325. * This way does not work with group format read, so bail
  326. * out in that case.
  327. */
  328. if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
  329. return -1;
  330. if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
  331. read(fd, &read_data, sizeof(read_data)) == -1)
  332. return -1;
  333. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  334. ++id_idx;
  335. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  336. ++id_idx;
  337. id = read_data[id_idx];
  338. add:
  339. perf_evlist__id_add(evlist, evsel, cpu, thread, id);
  340. return 0;
  341. }
  342. struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
  343. {
  344. struct hlist_head *head;
  345. struct perf_sample_id *sid;
  346. int hash;
  347. hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
  348. head = &evlist->heads[hash];
  349. hlist_for_each_entry(sid, head, node)
  350. if (sid->id == id)
  351. return sid;
  352. return NULL;
  353. }
  354. struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
  355. {
  356. struct perf_sample_id *sid;
  357. if (evlist->nr_entries == 1)
  358. return perf_evlist__first(evlist);
  359. sid = perf_evlist__id2sid(evlist, id);
  360. if (sid)
  361. return sid->evsel;
  362. if (!perf_evlist__sample_id_all(evlist))
  363. return perf_evlist__first(evlist);
  364. return NULL;
  365. }
  366. static int perf_evlist__event2id(struct perf_evlist *evlist,
  367. union perf_event *event, u64 *id)
  368. {
  369. const u64 *array = event->sample.array;
  370. ssize_t n;
  371. n = (event->header.size - sizeof(event->header)) >> 3;
  372. if (event->header.type == PERF_RECORD_SAMPLE) {
  373. if (evlist->id_pos >= n)
  374. return -1;
  375. *id = array[evlist->id_pos];
  376. } else {
  377. if (evlist->is_pos > n)
  378. return -1;
  379. n -= evlist->is_pos;
  380. *id = array[n];
  381. }
  382. return 0;
  383. }
  384. static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
  385. union perf_event *event)
  386. {
  387. struct perf_evsel *first = perf_evlist__first(evlist);
  388. struct hlist_head *head;
  389. struct perf_sample_id *sid;
  390. int hash;
  391. u64 id;
  392. if (evlist->nr_entries == 1)
  393. return first;
  394. if (!first->attr.sample_id_all &&
  395. event->header.type != PERF_RECORD_SAMPLE)
  396. return first;
  397. if (perf_evlist__event2id(evlist, event, &id))
  398. return NULL;
  399. /* Synthesized events have an id of zero */
  400. if (!id)
  401. return first;
  402. hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
  403. head = &evlist->heads[hash];
  404. hlist_for_each_entry(sid, head, node) {
  405. if (sid->id == id)
  406. return sid->evsel;
  407. }
  408. return NULL;
  409. }
  410. union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
  411. {
  412. struct perf_mmap *md = &evlist->mmap[idx];
  413. unsigned int head = perf_mmap__read_head(md);
  414. unsigned int old = md->prev;
  415. unsigned char *data = md->base + page_size;
  416. union perf_event *event = NULL;
  417. if (evlist->overwrite) {
  418. /*
  419. * If we're further behind than half the buffer, there's a chance
  420. * the writer will bite our tail and mess up the samples under us.
  421. *
  422. * If we somehow ended up ahead of the head, we got messed up.
  423. *
  424. * In either case, truncate and restart at head.
  425. */
  426. int diff = head - old;
  427. if (diff > md->mask / 2 || diff < 0) {
  428. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  429. /*
  430. * head points to a known good entry, start there.
  431. */
  432. old = head;
  433. }
  434. }
  435. if (old != head) {
  436. size_t size;
  437. event = (union perf_event *)&data[old & md->mask];
  438. size = event->header.size;
  439. /*
  440. * Event straddles the mmap boundary -- header should always
  441. * be inside due to u64 alignment of output.
  442. */
  443. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  444. unsigned int offset = old;
  445. unsigned int len = min(sizeof(*event), size), cpy;
  446. void *dst = md->event_copy;
  447. do {
  448. cpy = min(md->mask + 1 - (offset & md->mask), len);
  449. memcpy(dst, &data[offset & md->mask], cpy);
  450. offset += cpy;
  451. dst += cpy;
  452. len -= cpy;
  453. } while (len);
  454. event = (union perf_event *) md->event_copy;
  455. }
  456. old += size;
  457. }
  458. md->prev = old;
  459. return event;
  460. }
  461. void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
  462. {
  463. if (!evlist->overwrite) {
  464. struct perf_mmap *md = &evlist->mmap[idx];
  465. unsigned int old = md->prev;
  466. perf_mmap__write_tail(md, old);
  467. }
  468. }
  469. static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
  470. {
  471. if (evlist->mmap[idx].base != NULL) {
  472. munmap(evlist->mmap[idx].base, evlist->mmap_len);
  473. evlist->mmap[idx].base = NULL;
  474. }
  475. }
  476. void perf_evlist__munmap(struct perf_evlist *evlist)
  477. {
  478. int i;
  479. if (evlist->mmap == NULL)
  480. return;
  481. for (i = 0; i < evlist->nr_mmaps; i++)
  482. __perf_evlist__munmap(evlist, i);
  483. zfree(&evlist->mmap);
  484. }
  485. static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
  486. {
  487. evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
  488. if (cpu_map__empty(evlist->cpus))
  489. evlist->nr_mmaps = thread_map__nr(evlist->threads);
  490. evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
  491. return evlist->mmap != NULL ? 0 : -ENOMEM;
  492. }
  493. struct mmap_params {
  494. int prot;
  495. int mask;
  496. };
  497. static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
  498. struct mmap_params *mp, int fd)
  499. {
  500. evlist->mmap[idx].prev = 0;
  501. evlist->mmap[idx].mask = mp->mask;
  502. evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
  503. MAP_SHARED, fd, 0);
  504. if (evlist->mmap[idx].base == MAP_FAILED) {
  505. pr_debug2("failed to mmap perf event ring buffer, error %d\n",
  506. errno);
  507. evlist->mmap[idx].base = NULL;
  508. return -1;
  509. }
  510. perf_evlist__add_pollfd(evlist, fd);
  511. return 0;
  512. }
  513. static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
  514. struct mmap_params *mp, int cpu,
  515. int thread, int *output)
  516. {
  517. struct perf_evsel *evsel;
  518. evlist__for_each(evlist, evsel) {
  519. int fd = FD(evsel, cpu, thread);
  520. if (*output == -1) {
  521. *output = fd;
  522. if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
  523. return -1;
  524. } else {
  525. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
  526. return -1;
  527. }
  528. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  529. perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
  530. return -1;
  531. }
  532. return 0;
  533. }
  534. static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
  535. struct mmap_params *mp)
  536. {
  537. int cpu, thread;
  538. int nr_cpus = cpu_map__nr(evlist->cpus);
  539. int nr_threads = thread_map__nr(evlist->threads);
  540. pr_debug2("perf event ring buffer mmapped per cpu\n");
  541. for (cpu = 0; cpu < nr_cpus; cpu++) {
  542. int output = -1;
  543. for (thread = 0; thread < nr_threads; thread++) {
  544. if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
  545. thread, &output))
  546. goto out_unmap;
  547. }
  548. }
  549. return 0;
  550. out_unmap:
  551. for (cpu = 0; cpu < nr_cpus; cpu++)
  552. __perf_evlist__munmap(evlist, cpu);
  553. return -1;
  554. }
  555. static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
  556. struct mmap_params *mp)
  557. {
  558. int thread;
  559. int nr_threads = thread_map__nr(evlist->threads);
  560. pr_debug2("perf event ring buffer mmapped per thread\n");
  561. for (thread = 0; thread < nr_threads; thread++) {
  562. int output = -1;
  563. if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
  564. &output))
  565. goto out_unmap;
  566. }
  567. return 0;
  568. out_unmap:
  569. for (thread = 0; thread < nr_threads; thread++)
  570. __perf_evlist__munmap(evlist, thread);
  571. return -1;
  572. }
  573. static size_t perf_evlist__mmap_size(unsigned long pages)
  574. {
  575. /* 512 kiB: default amount of unprivileged mlocked memory */
  576. if (pages == UINT_MAX)
  577. pages = (512 * 1024) / page_size;
  578. else if (!is_power_of_2(pages))
  579. return 0;
  580. return (pages + 1) * page_size;
  581. }
  582. static long parse_pages_arg(const char *str, unsigned long min,
  583. unsigned long max)
  584. {
  585. unsigned long pages, val;
  586. static struct parse_tag tags[] = {
  587. { .tag = 'B', .mult = 1 },
  588. { .tag = 'K', .mult = 1 << 10 },
  589. { .tag = 'M', .mult = 1 << 20 },
  590. { .tag = 'G', .mult = 1 << 30 },
  591. { .tag = 0 },
  592. };
  593. if (str == NULL)
  594. return -EINVAL;
  595. val = parse_tag_value(str, tags);
  596. if (val != (unsigned long) -1) {
  597. /* we got file size value */
  598. pages = PERF_ALIGN(val, page_size) / page_size;
  599. } else {
  600. /* we got pages count value */
  601. char *eptr;
  602. pages = strtoul(str, &eptr, 10);
  603. if (*eptr != '\0')
  604. return -EINVAL;
  605. }
  606. if (pages == 0 && min == 0) {
  607. /* leave number of pages at 0 */
  608. } else if (!is_power_of_2(pages)) {
  609. /* round pages up to next power of 2 */
  610. pages = next_pow2_l(pages);
  611. if (!pages)
  612. return -EINVAL;
  613. pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
  614. pages * page_size, pages);
  615. }
  616. if (pages > max)
  617. return -EINVAL;
  618. return pages;
  619. }
  620. int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
  621. int unset __maybe_unused)
  622. {
  623. unsigned int *mmap_pages = opt->value;
  624. unsigned long max = UINT_MAX;
  625. long pages;
  626. if (max > SIZE_MAX / page_size)
  627. max = SIZE_MAX / page_size;
  628. pages = parse_pages_arg(str, 1, max);
  629. if (pages < 0) {
  630. pr_err("Invalid argument for --mmap_pages/-m\n");
  631. return -1;
  632. }
  633. *mmap_pages = pages;
  634. return 0;
  635. }
  636. /**
  637. * perf_evlist__mmap - Create mmaps to receive events.
  638. * @evlist: list of events
  639. * @pages: map length in pages
  640. * @overwrite: overwrite older events?
  641. *
  642. * If @overwrite is %false the user needs to signal event consumption using
  643. * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
  644. * automatically.
  645. *
  646. * Return: %0 on success, negative error code otherwise.
  647. */
  648. int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
  649. bool overwrite)
  650. {
  651. struct perf_evsel *evsel;
  652. const struct cpu_map *cpus = evlist->cpus;
  653. const struct thread_map *threads = evlist->threads;
  654. struct mmap_params mp = {
  655. .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
  656. };
  657. if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
  658. return -ENOMEM;
  659. if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
  660. return -ENOMEM;
  661. evlist->overwrite = overwrite;
  662. evlist->mmap_len = perf_evlist__mmap_size(pages);
  663. pr_debug("mmap size %zuB\n", evlist->mmap_len);
  664. mp.mask = evlist->mmap_len - page_size - 1;
  665. evlist__for_each(evlist, evsel) {
  666. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  667. evsel->sample_id == NULL &&
  668. perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
  669. return -ENOMEM;
  670. }
  671. if (cpu_map__empty(cpus))
  672. return perf_evlist__mmap_per_thread(evlist, &mp);
  673. return perf_evlist__mmap_per_cpu(evlist, &mp);
  674. }
  675. int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
  676. {
  677. evlist->threads = thread_map__new_str(target->pid, target->tid,
  678. target->uid);
  679. if (evlist->threads == NULL)
  680. return -1;
  681. if (target__uses_dummy_map(target))
  682. evlist->cpus = cpu_map__dummy_new();
  683. else
  684. evlist->cpus = cpu_map__new(target->cpu_list);
  685. if (evlist->cpus == NULL)
  686. goto out_delete_threads;
  687. return 0;
  688. out_delete_threads:
  689. thread_map__delete(evlist->threads);
  690. return -1;
  691. }
  692. int perf_evlist__apply_filters(struct perf_evlist *evlist)
  693. {
  694. struct perf_evsel *evsel;
  695. int err = 0;
  696. const int ncpus = cpu_map__nr(evlist->cpus),
  697. nthreads = thread_map__nr(evlist->threads);
  698. evlist__for_each(evlist, evsel) {
  699. if (evsel->filter == NULL)
  700. continue;
  701. err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
  702. if (err)
  703. break;
  704. }
  705. return err;
  706. }
  707. int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
  708. {
  709. struct perf_evsel *evsel;
  710. int err = 0;
  711. const int ncpus = cpu_map__nr(evlist->cpus),
  712. nthreads = thread_map__nr(evlist->threads);
  713. evlist__for_each(evlist, evsel) {
  714. err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
  715. if (err)
  716. break;
  717. }
  718. return err;
  719. }
  720. bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
  721. {
  722. struct perf_evsel *pos;
  723. if (evlist->nr_entries == 1)
  724. return true;
  725. if (evlist->id_pos < 0 || evlist->is_pos < 0)
  726. return false;
  727. evlist__for_each(evlist, pos) {
  728. if (pos->id_pos != evlist->id_pos ||
  729. pos->is_pos != evlist->is_pos)
  730. return false;
  731. }
  732. return true;
  733. }
  734. u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
  735. {
  736. struct perf_evsel *evsel;
  737. if (evlist->combined_sample_type)
  738. return evlist->combined_sample_type;
  739. evlist__for_each(evlist, evsel)
  740. evlist->combined_sample_type |= evsel->attr.sample_type;
  741. return evlist->combined_sample_type;
  742. }
  743. u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
  744. {
  745. evlist->combined_sample_type = 0;
  746. return __perf_evlist__combined_sample_type(evlist);
  747. }
  748. bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
  749. {
  750. struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
  751. u64 read_format = first->attr.read_format;
  752. u64 sample_type = first->attr.sample_type;
  753. evlist__for_each(evlist, pos) {
  754. if (read_format != pos->attr.read_format)
  755. return false;
  756. }
  757. /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
  758. if ((sample_type & PERF_SAMPLE_READ) &&
  759. !(read_format & PERF_FORMAT_ID)) {
  760. return false;
  761. }
  762. return true;
  763. }
  764. u64 perf_evlist__read_format(struct perf_evlist *evlist)
  765. {
  766. struct perf_evsel *first = perf_evlist__first(evlist);
  767. return first->attr.read_format;
  768. }
  769. u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
  770. {
  771. struct perf_evsel *first = perf_evlist__first(evlist);
  772. struct perf_sample *data;
  773. u64 sample_type;
  774. u16 size = 0;
  775. if (!first->attr.sample_id_all)
  776. goto out;
  777. sample_type = first->attr.sample_type;
  778. if (sample_type & PERF_SAMPLE_TID)
  779. size += sizeof(data->tid) * 2;
  780. if (sample_type & PERF_SAMPLE_TIME)
  781. size += sizeof(data->time);
  782. if (sample_type & PERF_SAMPLE_ID)
  783. size += sizeof(data->id);
  784. if (sample_type & PERF_SAMPLE_STREAM_ID)
  785. size += sizeof(data->stream_id);
  786. if (sample_type & PERF_SAMPLE_CPU)
  787. size += sizeof(data->cpu) * 2;
  788. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  789. size += sizeof(data->id);
  790. out:
  791. return size;
  792. }
  793. bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
  794. {
  795. struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
  796. evlist__for_each_continue(evlist, pos) {
  797. if (first->attr.sample_id_all != pos->attr.sample_id_all)
  798. return false;
  799. }
  800. return true;
  801. }
  802. bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
  803. {
  804. struct perf_evsel *first = perf_evlist__first(evlist);
  805. return first->attr.sample_id_all;
  806. }
  807. void perf_evlist__set_selected(struct perf_evlist *evlist,
  808. struct perf_evsel *evsel)
  809. {
  810. evlist->selected = evsel;
  811. }
  812. void perf_evlist__close(struct perf_evlist *evlist)
  813. {
  814. struct perf_evsel *evsel;
  815. int ncpus = cpu_map__nr(evlist->cpus);
  816. int nthreads = thread_map__nr(evlist->threads);
  817. int n;
  818. evlist__for_each_reverse(evlist, evsel) {
  819. n = evsel->cpus ? evsel->cpus->nr : ncpus;
  820. perf_evsel__close(evsel, n, nthreads);
  821. }
  822. }
  823. int perf_evlist__open(struct perf_evlist *evlist)
  824. {
  825. struct perf_evsel *evsel;
  826. int err;
  827. perf_evlist__update_id_pos(evlist);
  828. evlist__for_each(evlist, evsel) {
  829. err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
  830. if (err < 0)
  831. goto out_err;
  832. }
  833. return 0;
  834. out_err:
  835. perf_evlist__close(evlist);
  836. errno = -err;
  837. return err;
  838. }
  839. int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
  840. const char *argv[], bool pipe_output,
  841. void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
  842. {
  843. int child_ready_pipe[2], go_pipe[2];
  844. char bf;
  845. if (pipe(child_ready_pipe) < 0) {
  846. perror("failed to create 'ready' pipe");
  847. return -1;
  848. }
  849. if (pipe(go_pipe) < 0) {
  850. perror("failed to create 'go' pipe");
  851. goto out_close_ready_pipe;
  852. }
  853. evlist->workload.pid = fork();
  854. if (evlist->workload.pid < 0) {
  855. perror("failed to fork");
  856. goto out_close_pipes;
  857. }
  858. if (!evlist->workload.pid) {
  859. if (pipe_output)
  860. dup2(2, 1);
  861. signal(SIGTERM, SIG_DFL);
  862. close(child_ready_pipe[0]);
  863. close(go_pipe[1]);
  864. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  865. /*
  866. * Tell the parent we're ready to go
  867. */
  868. close(child_ready_pipe[1]);
  869. /*
  870. * Wait until the parent tells us to go.
  871. */
  872. if (read(go_pipe[0], &bf, 1) == -1)
  873. perror("unable to read pipe");
  874. execvp(argv[0], (char **)argv);
  875. if (exec_error) {
  876. union sigval val;
  877. val.sival_int = errno;
  878. if (sigqueue(getppid(), SIGUSR1, val))
  879. perror(argv[0]);
  880. } else
  881. perror(argv[0]);
  882. exit(-1);
  883. }
  884. if (exec_error) {
  885. struct sigaction act = {
  886. .sa_flags = SA_SIGINFO,
  887. .sa_sigaction = exec_error,
  888. };
  889. sigaction(SIGUSR1, &act, NULL);
  890. }
  891. if (target__none(target))
  892. evlist->threads->map[0] = evlist->workload.pid;
  893. close(child_ready_pipe[1]);
  894. close(go_pipe[0]);
  895. /*
  896. * wait for child to settle
  897. */
  898. if (read(child_ready_pipe[0], &bf, 1) == -1) {
  899. perror("unable to read pipe");
  900. goto out_close_pipes;
  901. }
  902. fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
  903. evlist->workload.cork_fd = go_pipe[1];
  904. close(child_ready_pipe[0]);
  905. return 0;
  906. out_close_pipes:
  907. close(go_pipe[0]);
  908. close(go_pipe[1]);
  909. out_close_ready_pipe:
  910. close(child_ready_pipe[0]);
  911. close(child_ready_pipe[1]);
  912. return -1;
  913. }
  914. int perf_evlist__start_workload(struct perf_evlist *evlist)
  915. {
  916. if (evlist->workload.cork_fd > 0) {
  917. char bf = 0;
  918. int ret;
  919. /*
  920. * Remove the cork, let it rip!
  921. */
  922. ret = write(evlist->workload.cork_fd, &bf, 1);
  923. if (ret < 0)
  924. perror("enable to write to pipe");
  925. close(evlist->workload.cork_fd);
  926. return ret;
  927. }
  928. return 0;
  929. }
  930. int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
  931. struct perf_sample *sample)
  932. {
  933. struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
  934. if (!evsel)
  935. return -EFAULT;
  936. return perf_evsel__parse_sample(evsel, event, sample);
  937. }
  938. size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
  939. {
  940. struct perf_evsel *evsel;
  941. size_t printed = 0;
  942. evlist__for_each(evlist, evsel) {
  943. printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
  944. perf_evsel__name(evsel));
  945. }
  946. return printed + fprintf(fp, "\n");
  947. }
  948. int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
  949. int err, char *buf, size_t size)
  950. {
  951. char sbuf[128];
  952. switch (err) {
  953. case ENOENT:
  954. scnprintf(buf, size, "%s",
  955. "Error:\tUnable to find debugfs\n"
  956. "Hint:\tWas your kernel was compiled with debugfs support?\n"
  957. "Hint:\tIs the debugfs filesystem mounted?\n"
  958. "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
  959. break;
  960. case EACCES:
  961. scnprintf(buf, size,
  962. "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
  963. "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
  964. debugfs_mountpoint, debugfs_mountpoint);
  965. break;
  966. default:
  967. scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
  968. break;
  969. }
  970. return 0;
  971. }
  972. int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
  973. int err, char *buf, size_t size)
  974. {
  975. int printed, value;
  976. char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
  977. switch (err) {
  978. case EACCES:
  979. case EPERM:
  980. printed = scnprintf(buf, size,
  981. "Error:\t%s.\n"
  982. "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
  983. value = perf_event_paranoid();
  984. printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
  985. if (value >= 2) {
  986. printed += scnprintf(buf + printed, size - printed,
  987. "For your workloads it needs to be <= 1\nHint:\t");
  988. }
  989. printed += scnprintf(buf + printed, size - printed,
  990. "For system wide tracing it needs to be set to -1.\n");
  991. printed += scnprintf(buf + printed, size - printed,
  992. "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
  993. "Hint:\tThe current value is %d.", value);
  994. break;
  995. default:
  996. scnprintf(buf, size, "%s", emsg);
  997. break;
  998. }
  999. return 0;
  1000. }
  1001. void perf_evlist__to_front(struct perf_evlist *evlist,
  1002. struct perf_evsel *move_evsel)
  1003. {
  1004. struct perf_evsel *evsel, *n;
  1005. LIST_HEAD(move);
  1006. if (move_evsel == perf_evlist__first(evlist))
  1007. return;
  1008. evlist__for_each_safe(evlist, n, evsel) {
  1009. if (evsel->leader == move_evsel->leader)
  1010. list_move_tail(&evsel->node, &move);
  1011. }
  1012. list_splice(&move, &evlist->entries);
  1013. }