evlist.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  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/fs.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 <subcmd/parse-options.h>
  21. #include <sys/mman.h>
  22. #include <linux/bitops.h>
  23. #include <linux/hash.h>
  24. #include <linux/log2.h>
  25. #include <linux/err.h>
  26. static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
  27. static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
  28. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  29. #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
  30. void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
  31. struct thread_map *threads)
  32. {
  33. int i;
  34. for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
  35. INIT_HLIST_HEAD(&evlist->heads[i]);
  36. INIT_LIST_HEAD(&evlist->entries);
  37. perf_evlist__set_maps(evlist, cpus, threads);
  38. fdarray__init(&evlist->pollfd, 64);
  39. evlist->workload.pid = -1;
  40. }
  41. struct perf_evlist *perf_evlist__new(void)
  42. {
  43. struct perf_evlist *evlist = zalloc(sizeof(*evlist));
  44. if (evlist != NULL)
  45. perf_evlist__init(evlist, NULL, NULL);
  46. return evlist;
  47. }
  48. struct perf_evlist *perf_evlist__new_default(void)
  49. {
  50. struct perf_evlist *evlist = perf_evlist__new();
  51. if (evlist && perf_evlist__add_default(evlist)) {
  52. perf_evlist__delete(evlist);
  53. evlist = NULL;
  54. }
  55. return evlist;
  56. }
  57. struct perf_evlist *perf_evlist__new_dummy(void)
  58. {
  59. struct perf_evlist *evlist = perf_evlist__new();
  60. if (evlist && perf_evlist__add_dummy(evlist)) {
  61. perf_evlist__delete(evlist);
  62. evlist = NULL;
  63. }
  64. return evlist;
  65. }
  66. /**
  67. * perf_evlist__set_id_pos - set the positions of event ids.
  68. * @evlist: selected event list
  69. *
  70. * Events with compatible sample types all have the same id_pos
  71. * and is_pos. For convenience, put a copy on evlist.
  72. */
  73. void perf_evlist__set_id_pos(struct perf_evlist *evlist)
  74. {
  75. struct perf_evsel *first = perf_evlist__first(evlist);
  76. evlist->id_pos = first->id_pos;
  77. evlist->is_pos = first->is_pos;
  78. }
  79. static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
  80. {
  81. struct perf_evsel *evsel;
  82. evlist__for_each(evlist, evsel)
  83. perf_evsel__calc_id_pos(evsel);
  84. perf_evlist__set_id_pos(evlist);
  85. }
  86. static void perf_evlist__purge(struct perf_evlist *evlist)
  87. {
  88. struct perf_evsel *pos, *n;
  89. evlist__for_each_safe(evlist, n, pos) {
  90. list_del_init(&pos->node);
  91. pos->evlist = NULL;
  92. perf_evsel__delete(pos);
  93. }
  94. evlist->nr_entries = 0;
  95. }
  96. void perf_evlist__exit(struct perf_evlist *evlist)
  97. {
  98. zfree(&evlist->mmap);
  99. fdarray__exit(&evlist->pollfd);
  100. }
  101. void perf_evlist__delete(struct perf_evlist *evlist)
  102. {
  103. perf_evlist__munmap(evlist);
  104. perf_evlist__close(evlist);
  105. cpu_map__put(evlist->cpus);
  106. thread_map__put(evlist->threads);
  107. evlist->cpus = NULL;
  108. evlist->threads = NULL;
  109. perf_evlist__purge(evlist);
  110. perf_evlist__exit(evlist);
  111. free(evlist);
  112. }
  113. static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
  114. struct perf_evsel *evsel)
  115. {
  116. /*
  117. * We already have cpus for evsel (via PMU sysfs) so
  118. * keep it, if there's no target cpu list defined.
  119. */
  120. if (!evsel->own_cpus || evlist->has_user_cpus) {
  121. cpu_map__put(evsel->cpus);
  122. evsel->cpus = cpu_map__get(evlist->cpus);
  123. } else if (evsel->cpus != evsel->own_cpus) {
  124. cpu_map__put(evsel->cpus);
  125. evsel->cpus = cpu_map__get(evsel->own_cpus);
  126. }
  127. thread_map__put(evsel->threads);
  128. evsel->threads = thread_map__get(evlist->threads);
  129. }
  130. static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
  131. {
  132. struct perf_evsel *evsel;
  133. evlist__for_each(evlist, evsel)
  134. __perf_evlist__propagate_maps(evlist, evsel);
  135. }
  136. void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
  137. {
  138. entry->evlist = evlist;
  139. list_add_tail(&entry->node, &evlist->entries);
  140. entry->idx = evlist->nr_entries;
  141. entry->tracking = !entry->idx;
  142. if (!evlist->nr_entries++)
  143. perf_evlist__set_id_pos(evlist);
  144. __perf_evlist__propagate_maps(evlist, entry);
  145. }
  146. void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel)
  147. {
  148. evsel->evlist = NULL;
  149. list_del_init(&evsel->node);
  150. evlist->nr_entries -= 1;
  151. }
  152. void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
  153. struct list_head *list)
  154. {
  155. struct perf_evsel *evsel, *temp;
  156. __evlist__for_each_safe(list, temp, evsel) {
  157. list_del_init(&evsel->node);
  158. perf_evlist__add(evlist, evsel);
  159. }
  160. }
  161. void __perf_evlist__set_leader(struct list_head *list)
  162. {
  163. struct perf_evsel *evsel, *leader;
  164. leader = list_entry(list->next, struct perf_evsel, node);
  165. evsel = list_entry(list->prev, struct perf_evsel, node);
  166. leader->nr_members = evsel->idx - leader->idx + 1;
  167. __evlist__for_each(list, evsel) {
  168. evsel->leader = leader;
  169. }
  170. }
  171. void perf_evlist__set_leader(struct perf_evlist *evlist)
  172. {
  173. if (evlist->nr_entries) {
  174. evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
  175. __perf_evlist__set_leader(&evlist->entries);
  176. }
  177. }
  178. void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr)
  179. {
  180. attr->precise_ip = 3;
  181. while (attr->precise_ip != 0) {
  182. int fd = sys_perf_event_open(attr, 0, -1, -1, 0);
  183. if (fd != -1) {
  184. close(fd);
  185. break;
  186. }
  187. --attr->precise_ip;
  188. }
  189. }
  190. int perf_evlist__add_default(struct perf_evlist *evlist)
  191. {
  192. struct perf_event_attr attr = {
  193. .type = PERF_TYPE_HARDWARE,
  194. .config = PERF_COUNT_HW_CPU_CYCLES,
  195. };
  196. struct perf_evsel *evsel;
  197. event_attr_init(&attr);
  198. perf_event_attr__set_max_precise_ip(&attr);
  199. evsel = perf_evsel__new(&attr);
  200. if (evsel == NULL)
  201. goto error;
  202. /* use asprintf() because free(evsel) assumes name is allocated */
  203. if (asprintf(&evsel->name, "cycles%.*s",
  204. attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
  205. goto error_free;
  206. perf_evlist__add(evlist, evsel);
  207. return 0;
  208. error_free:
  209. perf_evsel__delete(evsel);
  210. error:
  211. return -ENOMEM;
  212. }
  213. int perf_evlist__add_dummy(struct perf_evlist *evlist)
  214. {
  215. struct perf_event_attr attr = {
  216. .type = PERF_TYPE_SOFTWARE,
  217. .config = PERF_COUNT_SW_DUMMY,
  218. .size = sizeof(attr), /* to capture ABI version */
  219. };
  220. struct perf_evsel *evsel = perf_evsel__new(&attr);
  221. if (evsel == NULL)
  222. return -ENOMEM;
  223. perf_evlist__add(evlist, evsel);
  224. return 0;
  225. }
  226. static int perf_evlist__add_attrs(struct perf_evlist *evlist,
  227. struct perf_event_attr *attrs, size_t nr_attrs)
  228. {
  229. struct perf_evsel *evsel, *n;
  230. LIST_HEAD(head);
  231. size_t i;
  232. for (i = 0; i < nr_attrs; i++) {
  233. evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
  234. if (evsel == NULL)
  235. goto out_delete_partial_list;
  236. list_add_tail(&evsel->node, &head);
  237. }
  238. perf_evlist__splice_list_tail(evlist, &head);
  239. return 0;
  240. out_delete_partial_list:
  241. __evlist__for_each_safe(&head, n, evsel)
  242. perf_evsel__delete(evsel);
  243. return -1;
  244. }
  245. int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
  246. struct perf_event_attr *attrs, size_t nr_attrs)
  247. {
  248. size_t i;
  249. for (i = 0; i < nr_attrs; i++)
  250. event_attr_init(attrs + i);
  251. return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
  252. }
  253. struct perf_evsel *
  254. perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
  255. {
  256. struct perf_evsel *evsel;
  257. evlist__for_each(evlist, evsel) {
  258. if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
  259. (int)evsel->attr.config == id)
  260. return evsel;
  261. }
  262. return NULL;
  263. }
  264. struct perf_evsel *
  265. perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
  266. const char *name)
  267. {
  268. struct perf_evsel *evsel;
  269. evlist__for_each(evlist, evsel) {
  270. if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
  271. (strcmp(evsel->name, name) == 0))
  272. return evsel;
  273. }
  274. return NULL;
  275. }
  276. int perf_evlist__add_newtp(struct perf_evlist *evlist,
  277. const char *sys, const char *name, void *handler)
  278. {
  279. struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
  280. if (IS_ERR(evsel))
  281. return -1;
  282. evsel->handler = handler;
  283. perf_evlist__add(evlist, evsel);
  284. return 0;
  285. }
  286. static int perf_evlist__nr_threads(struct perf_evlist *evlist,
  287. struct perf_evsel *evsel)
  288. {
  289. if (evsel->system_wide)
  290. return 1;
  291. else
  292. return thread_map__nr(evlist->threads);
  293. }
  294. void perf_evlist__disable(struct perf_evlist *evlist)
  295. {
  296. struct perf_evsel *pos;
  297. evlist__for_each(evlist, pos) {
  298. if (!perf_evsel__is_group_leader(pos) || !pos->fd)
  299. continue;
  300. perf_evsel__disable(pos);
  301. }
  302. evlist->enabled = false;
  303. }
  304. void perf_evlist__enable(struct perf_evlist *evlist)
  305. {
  306. struct perf_evsel *pos;
  307. evlist__for_each(evlist, pos) {
  308. if (!perf_evsel__is_group_leader(pos) || !pos->fd)
  309. continue;
  310. perf_evsel__enable(pos);
  311. }
  312. evlist->enabled = true;
  313. }
  314. void perf_evlist__toggle_enable(struct perf_evlist *evlist)
  315. {
  316. (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
  317. }
  318. static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
  319. struct perf_evsel *evsel, int cpu)
  320. {
  321. int thread, err;
  322. int nr_threads = perf_evlist__nr_threads(evlist, evsel);
  323. if (!evsel->fd)
  324. return -EINVAL;
  325. for (thread = 0; thread < nr_threads; thread++) {
  326. err = ioctl(FD(evsel, cpu, thread),
  327. PERF_EVENT_IOC_ENABLE, 0);
  328. if (err)
  329. return err;
  330. }
  331. return 0;
  332. }
  333. static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
  334. struct perf_evsel *evsel,
  335. int thread)
  336. {
  337. int cpu, err;
  338. int nr_cpus = cpu_map__nr(evlist->cpus);
  339. if (!evsel->fd)
  340. return -EINVAL;
  341. for (cpu = 0; cpu < nr_cpus; cpu++) {
  342. err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
  343. if (err)
  344. return err;
  345. }
  346. return 0;
  347. }
  348. int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
  349. struct perf_evsel *evsel, int idx)
  350. {
  351. bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
  352. if (per_cpu_mmaps)
  353. return perf_evlist__enable_event_cpu(evlist, evsel, idx);
  354. else
  355. return perf_evlist__enable_event_thread(evlist, evsel, idx);
  356. }
  357. int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
  358. {
  359. int nr_cpus = cpu_map__nr(evlist->cpus);
  360. int nr_threads = thread_map__nr(evlist->threads);
  361. int nfds = 0;
  362. struct perf_evsel *evsel;
  363. evlist__for_each(evlist, evsel) {
  364. if (evsel->system_wide)
  365. nfds += nr_cpus;
  366. else
  367. nfds += nr_cpus * nr_threads;
  368. }
  369. if (fdarray__available_entries(&evlist->pollfd) < nfds &&
  370. fdarray__grow(&evlist->pollfd, nfds) < 0)
  371. return -ENOMEM;
  372. return 0;
  373. }
  374. static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
  375. {
  376. int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
  377. /*
  378. * Save the idx so that when we filter out fds POLLHUP'ed we can
  379. * close the associated evlist->mmap[] entry.
  380. */
  381. if (pos >= 0) {
  382. evlist->pollfd.priv[pos].idx = idx;
  383. fcntl(fd, F_SETFL, O_NONBLOCK);
  384. }
  385. return pos;
  386. }
  387. int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
  388. {
  389. return __perf_evlist__add_pollfd(evlist, fd, -1);
  390. }
  391. static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
  392. {
  393. struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
  394. perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
  395. }
  396. int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
  397. {
  398. return fdarray__filter(&evlist->pollfd, revents_and_mask,
  399. perf_evlist__munmap_filtered);
  400. }
  401. int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
  402. {
  403. return fdarray__poll(&evlist->pollfd, timeout);
  404. }
  405. static void perf_evlist__id_hash(struct perf_evlist *evlist,
  406. struct perf_evsel *evsel,
  407. int cpu, int thread, u64 id)
  408. {
  409. int hash;
  410. struct perf_sample_id *sid = SID(evsel, cpu, thread);
  411. sid->id = id;
  412. sid->evsel = evsel;
  413. hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
  414. hlist_add_head(&sid->node, &evlist->heads[hash]);
  415. }
  416. void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
  417. int cpu, int thread, u64 id)
  418. {
  419. perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
  420. evsel->id[evsel->ids++] = id;
  421. }
  422. int perf_evlist__id_add_fd(struct perf_evlist *evlist,
  423. struct perf_evsel *evsel,
  424. int cpu, int thread, int fd)
  425. {
  426. u64 read_data[4] = { 0, };
  427. int id_idx = 1; /* The first entry is the counter value */
  428. u64 id;
  429. int ret;
  430. ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
  431. if (!ret)
  432. goto add;
  433. if (errno != ENOTTY)
  434. return -1;
  435. /* Legacy way to get event id.. All hail to old kernels! */
  436. /*
  437. * This way does not work with group format read, so bail
  438. * out in that case.
  439. */
  440. if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
  441. return -1;
  442. if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
  443. read(fd, &read_data, sizeof(read_data)) == -1)
  444. return -1;
  445. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  446. ++id_idx;
  447. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  448. ++id_idx;
  449. id = read_data[id_idx];
  450. add:
  451. perf_evlist__id_add(evlist, evsel, cpu, thread, id);
  452. return 0;
  453. }
  454. static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
  455. struct perf_evsel *evsel, int idx, int cpu,
  456. int thread)
  457. {
  458. struct perf_sample_id *sid = SID(evsel, cpu, thread);
  459. sid->idx = idx;
  460. if (evlist->cpus && cpu >= 0)
  461. sid->cpu = evlist->cpus->map[cpu];
  462. else
  463. sid->cpu = -1;
  464. if (!evsel->system_wide && evlist->threads && thread >= 0)
  465. sid->tid = thread_map__pid(evlist->threads, thread);
  466. else
  467. sid->tid = -1;
  468. }
  469. struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
  470. {
  471. struct hlist_head *head;
  472. struct perf_sample_id *sid;
  473. int hash;
  474. hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
  475. head = &evlist->heads[hash];
  476. hlist_for_each_entry(sid, head, node)
  477. if (sid->id == id)
  478. return sid;
  479. return NULL;
  480. }
  481. struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
  482. {
  483. struct perf_sample_id *sid;
  484. if (evlist->nr_entries == 1 || !id)
  485. return perf_evlist__first(evlist);
  486. sid = perf_evlist__id2sid(evlist, id);
  487. if (sid)
  488. return sid->evsel;
  489. if (!perf_evlist__sample_id_all(evlist))
  490. return perf_evlist__first(evlist);
  491. return NULL;
  492. }
  493. struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
  494. u64 id)
  495. {
  496. struct perf_sample_id *sid;
  497. if (!id)
  498. return NULL;
  499. sid = perf_evlist__id2sid(evlist, id);
  500. if (sid)
  501. return sid->evsel;
  502. return NULL;
  503. }
  504. static int perf_evlist__event2id(struct perf_evlist *evlist,
  505. union perf_event *event, u64 *id)
  506. {
  507. const u64 *array = event->sample.array;
  508. ssize_t n;
  509. n = (event->header.size - sizeof(event->header)) >> 3;
  510. if (event->header.type == PERF_RECORD_SAMPLE) {
  511. if (evlist->id_pos >= n)
  512. return -1;
  513. *id = array[evlist->id_pos];
  514. } else {
  515. if (evlist->is_pos > n)
  516. return -1;
  517. n -= evlist->is_pos;
  518. *id = array[n];
  519. }
  520. return 0;
  521. }
  522. static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
  523. union perf_event *event)
  524. {
  525. struct perf_evsel *first = perf_evlist__first(evlist);
  526. struct hlist_head *head;
  527. struct perf_sample_id *sid;
  528. int hash;
  529. u64 id;
  530. if (evlist->nr_entries == 1)
  531. return first;
  532. if (!first->attr.sample_id_all &&
  533. event->header.type != PERF_RECORD_SAMPLE)
  534. return first;
  535. if (perf_evlist__event2id(evlist, event, &id))
  536. return NULL;
  537. /* Synthesized events have an id of zero */
  538. if (!id)
  539. return first;
  540. hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
  541. head = &evlist->heads[hash];
  542. hlist_for_each_entry(sid, head, node) {
  543. if (sid->id == id)
  544. return sid->evsel;
  545. }
  546. return NULL;
  547. }
  548. union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
  549. {
  550. struct perf_mmap *md = &evlist->mmap[idx];
  551. u64 head;
  552. u64 old = md->prev;
  553. unsigned char *data = md->base + page_size;
  554. union perf_event *event = NULL;
  555. /*
  556. * Check if event was unmapped due to a POLLHUP/POLLERR.
  557. */
  558. if (!atomic_read(&md->refcnt))
  559. return NULL;
  560. head = perf_mmap__read_head(md);
  561. if (evlist->overwrite) {
  562. /*
  563. * If we're further behind than half the buffer, there's a chance
  564. * the writer will bite our tail and mess up the samples under us.
  565. *
  566. * If we somehow ended up ahead of the head, we got messed up.
  567. *
  568. * In either case, truncate and restart at head.
  569. */
  570. int diff = head - old;
  571. if (diff > md->mask / 2 || diff < 0) {
  572. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  573. /*
  574. * head points to a known good entry, start there.
  575. */
  576. old = head;
  577. }
  578. }
  579. if (old != head) {
  580. size_t size;
  581. event = (union perf_event *)&data[old & md->mask];
  582. size = event->header.size;
  583. /*
  584. * Event straddles the mmap boundary -- header should always
  585. * be inside due to u64 alignment of output.
  586. */
  587. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  588. unsigned int offset = old;
  589. unsigned int len = min(sizeof(*event), size), cpy;
  590. void *dst = md->event_copy;
  591. do {
  592. cpy = min(md->mask + 1 - (offset & md->mask), len);
  593. memcpy(dst, &data[offset & md->mask], cpy);
  594. offset += cpy;
  595. dst += cpy;
  596. len -= cpy;
  597. } while (len);
  598. event = (union perf_event *) md->event_copy;
  599. }
  600. old += size;
  601. }
  602. md->prev = old;
  603. return event;
  604. }
  605. static bool perf_mmap__empty(struct perf_mmap *md)
  606. {
  607. return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
  608. }
  609. static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
  610. {
  611. atomic_inc(&evlist->mmap[idx].refcnt);
  612. }
  613. static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
  614. {
  615. BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
  616. if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
  617. __perf_evlist__munmap(evlist, idx);
  618. }
  619. void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
  620. {
  621. struct perf_mmap *md = &evlist->mmap[idx];
  622. if (!evlist->overwrite) {
  623. u64 old = md->prev;
  624. perf_mmap__write_tail(md, old);
  625. }
  626. if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
  627. perf_evlist__mmap_put(evlist, idx);
  628. }
  629. int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
  630. struct auxtrace_mmap_params *mp __maybe_unused,
  631. void *userpg __maybe_unused,
  632. int fd __maybe_unused)
  633. {
  634. return 0;
  635. }
  636. void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
  637. {
  638. }
  639. void __weak auxtrace_mmap_params__init(
  640. struct auxtrace_mmap_params *mp __maybe_unused,
  641. off_t auxtrace_offset __maybe_unused,
  642. unsigned int auxtrace_pages __maybe_unused,
  643. bool auxtrace_overwrite __maybe_unused)
  644. {
  645. }
  646. void __weak auxtrace_mmap_params__set_idx(
  647. struct auxtrace_mmap_params *mp __maybe_unused,
  648. struct perf_evlist *evlist __maybe_unused,
  649. int idx __maybe_unused,
  650. bool per_cpu __maybe_unused)
  651. {
  652. }
  653. static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
  654. {
  655. if (evlist->mmap[idx].base != NULL) {
  656. munmap(evlist->mmap[idx].base, evlist->mmap_len);
  657. evlist->mmap[idx].base = NULL;
  658. atomic_set(&evlist->mmap[idx].refcnt, 0);
  659. }
  660. auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
  661. }
  662. void perf_evlist__munmap(struct perf_evlist *evlist)
  663. {
  664. int i;
  665. if (evlist->mmap == NULL)
  666. return;
  667. for (i = 0; i < evlist->nr_mmaps; i++)
  668. __perf_evlist__munmap(evlist, i);
  669. zfree(&evlist->mmap);
  670. }
  671. static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
  672. {
  673. evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
  674. if (cpu_map__empty(evlist->cpus))
  675. evlist->nr_mmaps = thread_map__nr(evlist->threads);
  676. evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
  677. return evlist->mmap != NULL ? 0 : -ENOMEM;
  678. }
  679. struct mmap_params {
  680. int prot;
  681. int mask;
  682. struct auxtrace_mmap_params auxtrace_mp;
  683. };
  684. static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
  685. struct mmap_params *mp, int fd)
  686. {
  687. /*
  688. * The last one will be done at perf_evlist__mmap_consume(), so that we
  689. * make sure we don't prevent tools from consuming every last event in
  690. * the ring buffer.
  691. *
  692. * I.e. we can get the POLLHUP meaning that the fd doesn't exist
  693. * anymore, but the last events for it are still in the ring buffer,
  694. * waiting to be consumed.
  695. *
  696. * Tools can chose to ignore this at their own discretion, but the
  697. * evlist layer can't just drop it when filtering events in
  698. * perf_evlist__filter_pollfd().
  699. */
  700. atomic_set(&evlist->mmap[idx].refcnt, 2);
  701. evlist->mmap[idx].prev = 0;
  702. evlist->mmap[idx].mask = mp->mask;
  703. evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
  704. MAP_SHARED, fd, 0);
  705. if (evlist->mmap[idx].base == MAP_FAILED) {
  706. pr_debug2("failed to mmap perf event ring buffer, error %d\n",
  707. errno);
  708. evlist->mmap[idx].base = NULL;
  709. return -1;
  710. }
  711. if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap,
  712. &mp->auxtrace_mp, evlist->mmap[idx].base, fd))
  713. return -1;
  714. return 0;
  715. }
  716. static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
  717. struct mmap_params *mp, int cpu,
  718. int thread, int *output)
  719. {
  720. struct perf_evsel *evsel;
  721. evlist__for_each(evlist, evsel) {
  722. int fd;
  723. if (evsel->system_wide && thread)
  724. continue;
  725. fd = FD(evsel, cpu, thread);
  726. if (*output == -1) {
  727. *output = fd;
  728. if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
  729. return -1;
  730. } else {
  731. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
  732. return -1;
  733. perf_evlist__mmap_get(evlist, idx);
  734. }
  735. /*
  736. * The system_wide flag causes a selected event to be opened
  737. * always without a pid. Consequently it will never get a
  738. * POLLHUP, but it is used for tracking in combination with
  739. * other events, so it should not need to be polled anyway.
  740. * Therefore don't add it for polling.
  741. */
  742. if (!evsel->system_wide &&
  743. __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
  744. perf_evlist__mmap_put(evlist, idx);
  745. return -1;
  746. }
  747. if (evsel->attr.read_format & PERF_FORMAT_ID) {
  748. if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
  749. fd) < 0)
  750. return -1;
  751. perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
  752. thread);
  753. }
  754. }
  755. return 0;
  756. }
  757. static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
  758. struct mmap_params *mp)
  759. {
  760. int cpu, thread;
  761. int nr_cpus = cpu_map__nr(evlist->cpus);
  762. int nr_threads = thread_map__nr(evlist->threads);
  763. pr_debug2("perf event ring buffer mmapped per cpu\n");
  764. for (cpu = 0; cpu < nr_cpus; cpu++) {
  765. int output = -1;
  766. auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
  767. true);
  768. for (thread = 0; thread < nr_threads; thread++) {
  769. if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
  770. thread, &output))
  771. goto out_unmap;
  772. }
  773. }
  774. return 0;
  775. out_unmap:
  776. for (cpu = 0; cpu < nr_cpus; cpu++)
  777. __perf_evlist__munmap(evlist, cpu);
  778. return -1;
  779. }
  780. static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
  781. struct mmap_params *mp)
  782. {
  783. int thread;
  784. int nr_threads = thread_map__nr(evlist->threads);
  785. pr_debug2("perf event ring buffer mmapped per thread\n");
  786. for (thread = 0; thread < nr_threads; thread++) {
  787. int output = -1;
  788. auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
  789. false);
  790. if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
  791. &output))
  792. goto out_unmap;
  793. }
  794. return 0;
  795. out_unmap:
  796. for (thread = 0; thread < nr_threads; thread++)
  797. __perf_evlist__munmap(evlist, thread);
  798. return -1;
  799. }
  800. static size_t perf_evlist__mmap_size(unsigned long pages)
  801. {
  802. if (pages == UINT_MAX) {
  803. int max;
  804. if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
  805. /*
  806. * Pick a once upon a time good value, i.e. things look
  807. * strange since we can't read a sysctl value, but lets not
  808. * die yet...
  809. */
  810. max = 512;
  811. } else {
  812. max -= (page_size / 1024);
  813. }
  814. pages = (max * 1024) / page_size;
  815. if (!is_power_of_2(pages))
  816. pages = rounddown_pow_of_two(pages);
  817. } else if (!is_power_of_2(pages))
  818. return 0;
  819. return (pages + 1) * page_size;
  820. }
  821. static long parse_pages_arg(const char *str, unsigned long min,
  822. unsigned long max)
  823. {
  824. unsigned long pages, val;
  825. static struct parse_tag tags[] = {
  826. { .tag = 'B', .mult = 1 },
  827. { .tag = 'K', .mult = 1 << 10 },
  828. { .tag = 'M', .mult = 1 << 20 },
  829. { .tag = 'G', .mult = 1 << 30 },
  830. { .tag = 0 },
  831. };
  832. if (str == NULL)
  833. return -EINVAL;
  834. val = parse_tag_value(str, tags);
  835. if (val != (unsigned long) -1) {
  836. /* we got file size value */
  837. pages = PERF_ALIGN(val, page_size) / page_size;
  838. } else {
  839. /* we got pages count value */
  840. char *eptr;
  841. pages = strtoul(str, &eptr, 10);
  842. if (*eptr != '\0')
  843. return -EINVAL;
  844. }
  845. if (pages == 0 && min == 0) {
  846. /* leave number of pages at 0 */
  847. } else if (!is_power_of_2(pages)) {
  848. /* round pages up to next power of 2 */
  849. pages = roundup_pow_of_two(pages);
  850. if (!pages)
  851. return -EINVAL;
  852. pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
  853. pages * page_size, pages);
  854. }
  855. if (pages > max)
  856. return -EINVAL;
  857. return pages;
  858. }
  859. int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
  860. {
  861. unsigned long max = UINT_MAX;
  862. long pages;
  863. if (max > SIZE_MAX / page_size)
  864. max = SIZE_MAX / page_size;
  865. pages = parse_pages_arg(str, 1, max);
  866. if (pages < 0) {
  867. pr_err("Invalid argument for --mmap_pages/-m\n");
  868. return -1;
  869. }
  870. *mmap_pages = pages;
  871. return 0;
  872. }
  873. int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
  874. int unset __maybe_unused)
  875. {
  876. return __perf_evlist__parse_mmap_pages(opt->value, str);
  877. }
  878. /**
  879. * perf_evlist__mmap_ex - Create mmaps to receive events.
  880. * @evlist: list of events
  881. * @pages: map length in pages
  882. * @overwrite: overwrite older events?
  883. * @auxtrace_pages - auxtrace map length in pages
  884. * @auxtrace_overwrite - overwrite older auxtrace data?
  885. *
  886. * If @overwrite is %false the user needs to signal event consumption using
  887. * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
  888. * automatically.
  889. *
  890. * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
  891. * consumption using auxtrace_mmap__write_tail().
  892. *
  893. * Return: %0 on success, negative error code otherwise.
  894. */
  895. int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
  896. bool overwrite, unsigned int auxtrace_pages,
  897. bool auxtrace_overwrite)
  898. {
  899. struct perf_evsel *evsel;
  900. const struct cpu_map *cpus = evlist->cpus;
  901. const struct thread_map *threads = evlist->threads;
  902. struct mmap_params mp = {
  903. .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
  904. };
  905. if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
  906. return -ENOMEM;
  907. if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
  908. return -ENOMEM;
  909. evlist->overwrite = overwrite;
  910. evlist->mmap_len = perf_evlist__mmap_size(pages);
  911. pr_debug("mmap size %zuB\n", evlist->mmap_len);
  912. mp.mask = evlist->mmap_len - page_size - 1;
  913. auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
  914. auxtrace_pages, auxtrace_overwrite);
  915. evlist__for_each(evlist, evsel) {
  916. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  917. evsel->sample_id == NULL &&
  918. perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
  919. return -ENOMEM;
  920. }
  921. if (cpu_map__empty(cpus))
  922. return perf_evlist__mmap_per_thread(evlist, &mp);
  923. return perf_evlist__mmap_per_cpu(evlist, &mp);
  924. }
  925. int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
  926. bool overwrite)
  927. {
  928. return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
  929. }
  930. int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
  931. {
  932. struct cpu_map *cpus;
  933. struct thread_map *threads;
  934. threads = thread_map__new_str(target->pid, target->tid, target->uid);
  935. if (!threads)
  936. return -1;
  937. if (target__uses_dummy_map(target))
  938. cpus = cpu_map__dummy_new();
  939. else
  940. cpus = cpu_map__new(target->cpu_list);
  941. if (!cpus)
  942. goto out_delete_threads;
  943. evlist->has_user_cpus = !!target->cpu_list;
  944. perf_evlist__set_maps(evlist, cpus, threads);
  945. return 0;
  946. out_delete_threads:
  947. thread_map__put(threads);
  948. return -1;
  949. }
  950. void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
  951. struct thread_map *threads)
  952. {
  953. /*
  954. * Allow for the possibility that one or another of the maps isn't being
  955. * changed i.e. don't put it. Note we are assuming the maps that are
  956. * being applied are brand new and evlist is taking ownership of the
  957. * original reference count of 1. If that is not the case it is up to
  958. * the caller to increase the reference count.
  959. */
  960. if (cpus != evlist->cpus) {
  961. cpu_map__put(evlist->cpus);
  962. evlist->cpus = cpu_map__get(cpus);
  963. }
  964. if (threads != evlist->threads) {
  965. thread_map__put(evlist->threads);
  966. evlist->threads = thread_map__get(threads);
  967. }
  968. perf_evlist__propagate_maps(evlist);
  969. }
  970. int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
  971. {
  972. struct perf_evsel *evsel;
  973. int err = 0;
  974. const int ncpus = cpu_map__nr(evlist->cpus),
  975. nthreads = thread_map__nr(evlist->threads);
  976. evlist__for_each(evlist, evsel) {
  977. if (evsel->filter == NULL)
  978. continue;
  979. /*
  980. * filters only work for tracepoint event, which doesn't have cpu limit.
  981. * So evlist and evsel should always be same.
  982. */
  983. err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
  984. if (err) {
  985. *err_evsel = evsel;
  986. break;
  987. }
  988. }
  989. return err;
  990. }
  991. int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
  992. {
  993. struct perf_evsel *evsel;
  994. int err = 0;
  995. evlist__for_each(evlist, evsel) {
  996. if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
  997. continue;
  998. err = perf_evsel__set_filter(evsel, filter);
  999. if (err)
  1000. break;
  1001. }
  1002. return err;
  1003. }
  1004. int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
  1005. {
  1006. char *filter;
  1007. int ret = -1;
  1008. size_t i;
  1009. for (i = 0; i < npids; ++i) {
  1010. if (i == 0) {
  1011. if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
  1012. return -1;
  1013. } else {
  1014. char *tmp;
  1015. if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
  1016. goto out_free;
  1017. free(filter);
  1018. filter = tmp;
  1019. }
  1020. }
  1021. ret = perf_evlist__set_filter(evlist, filter);
  1022. out_free:
  1023. free(filter);
  1024. return ret;
  1025. }
  1026. int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
  1027. {
  1028. return perf_evlist__set_filter_pids(evlist, 1, &pid);
  1029. }
  1030. bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
  1031. {
  1032. struct perf_evsel *pos;
  1033. if (evlist->nr_entries == 1)
  1034. return true;
  1035. if (evlist->id_pos < 0 || evlist->is_pos < 0)
  1036. return false;
  1037. evlist__for_each(evlist, pos) {
  1038. if (pos->id_pos != evlist->id_pos ||
  1039. pos->is_pos != evlist->is_pos)
  1040. return false;
  1041. }
  1042. return true;
  1043. }
  1044. u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
  1045. {
  1046. struct perf_evsel *evsel;
  1047. if (evlist->combined_sample_type)
  1048. return evlist->combined_sample_type;
  1049. evlist__for_each(evlist, evsel)
  1050. evlist->combined_sample_type |= evsel->attr.sample_type;
  1051. return evlist->combined_sample_type;
  1052. }
  1053. u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
  1054. {
  1055. evlist->combined_sample_type = 0;
  1056. return __perf_evlist__combined_sample_type(evlist);
  1057. }
  1058. u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
  1059. {
  1060. struct perf_evsel *evsel;
  1061. u64 branch_type = 0;
  1062. evlist__for_each(evlist, evsel)
  1063. branch_type |= evsel->attr.branch_sample_type;
  1064. return branch_type;
  1065. }
  1066. bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
  1067. {
  1068. struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
  1069. u64 read_format = first->attr.read_format;
  1070. u64 sample_type = first->attr.sample_type;
  1071. evlist__for_each(evlist, pos) {
  1072. if (read_format != pos->attr.read_format)
  1073. return false;
  1074. }
  1075. /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
  1076. if ((sample_type & PERF_SAMPLE_READ) &&
  1077. !(read_format & PERF_FORMAT_ID)) {
  1078. return false;
  1079. }
  1080. return true;
  1081. }
  1082. u64 perf_evlist__read_format(struct perf_evlist *evlist)
  1083. {
  1084. struct perf_evsel *first = perf_evlist__first(evlist);
  1085. return first->attr.read_format;
  1086. }
  1087. u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
  1088. {
  1089. struct perf_evsel *first = perf_evlist__first(evlist);
  1090. struct perf_sample *data;
  1091. u64 sample_type;
  1092. u16 size = 0;
  1093. if (!first->attr.sample_id_all)
  1094. goto out;
  1095. sample_type = first->attr.sample_type;
  1096. if (sample_type & PERF_SAMPLE_TID)
  1097. size += sizeof(data->tid) * 2;
  1098. if (sample_type & PERF_SAMPLE_TIME)
  1099. size += sizeof(data->time);
  1100. if (sample_type & PERF_SAMPLE_ID)
  1101. size += sizeof(data->id);
  1102. if (sample_type & PERF_SAMPLE_STREAM_ID)
  1103. size += sizeof(data->stream_id);
  1104. if (sample_type & PERF_SAMPLE_CPU)
  1105. size += sizeof(data->cpu) * 2;
  1106. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  1107. size += sizeof(data->id);
  1108. out:
  1109. return size;
  1110. }
  1111. bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
  1112. {
  1113. struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
  1114. evlist__for_each_continue(evlist, pos) {
  1115. if (first->attr.sample_id_all != pos->attr.sample_id_all)
  1116. return false;
  1117. }
  1118. return true;
  1119. }
  1120. bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
  1121. {
  1122. struct perf_evsel *first = perf_evlist__first(evlist);
  1123. return first->attr.sample_id_all;
  1124. }
  1125. void perf_evlist__set_selected(struct perf_evlist *evlist,
  1126. struct perf_evsel *evsel)
  1127. {
  1128. evlist->selected = evsel;
  1129. }
  1130. void perf_evlist__close(struct perf_evlist *evlist)
  1131. {
  1132. struct perf_evsel *evsel;
  1133. int ncpus = cpu_map__nr(evlist->cpus);
  1134. int nthreads = thread_map__nr(evlist->threads);
  1135. int n;
  1136. evlist__for_each_reverse(evlist, evsel) {
  1137. n = evsel->cpus ? evsel->cpus->nr : ncpus;
  1138. perf_evsel__close(evsel, n, nthreads);
  1139. }
  1140. }
  1141. static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
  1142. {
  1143. struct cpu_map *cpus;
  1144. struct thread_map *threads;
  1145. int err = -ENOMEM;
  1146. /*
  1147. * Try reading /sys/devices/system/cpu/online to get
  1148. * an all cpus map.
  1149. *
  1150. * FIXME: -ENOMEM is the best we can do here, the cpu_map
  1151. * code needs an overhaul to properly forward the
  1152. * error, and we may not want to do that fallback to a
  1153. * default cpu identity map :-\
  1154. */
  1155. cpus = cpu_map__new(NULL);
  1156. if (!cpus)
  1157. goto out;
  1158. threads = thread_map__new_dummy();
  1159. if (!threads)
  1160. goto out_put;
  1161. perf_evlist__set_maps(evlist, cpus, threads);
  1162. out:
  1163. return err;
  1164. out_put:
  1165. cpu_map__put(cpus);
  1166. goto out;
  1167. }
  1168. int perf_evlist__open(struct perf_evlist *evlist)
  1169. {
  1170. struct perf_evsel *evsel;
  1171. int err;
  1172. /*
  1173. * Default: one fd per CPU, all threads, aka systemwide
  1174. * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
  1175. */
  1176. if (evlist->threads == NULL && evlist->cpus == NULL) {
  1177. err = perf_evlist__create_syswide_maps(evlist);
  1178. if (err < 0)
  1179. goto out_err;
  1180. }
  1181. perf_evlist__update_id_pos(evlist);
  1182. evlist__for_each(evlist, evsel) {
  1183. err = perf_evsel__open(evsel, evsel->cpus, evsel->threads);
  1184. if (err < 0)
  1185. goto out_err;
  1186. }
  1187. return 0;
  1188. out_err:
  1189. perf_evlist__close(evlist);
  1190. errno = -err;
  1191. return err;
  1192. }
  1193. int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
  1194. const char *argv[], bool pipe_output,
  1195. void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
  1196. {
  1197. int child_ready_pipe[2], go_pipe[2];
  1198. char bf;
  1199. if (pipe(child_ready_pipe) < 0) {
  1200. perror("failed to create 'ready' pipe");
  1201. return -1;
  1202. }
  1203. if (pipe(go_pipe) < 0) {
  1204. perror("failed to create 'go' pipe");
  1205. goto out_close_ready_pipe;
  1206. }
  1207. evlist->workload.pid = fork();
  1208. if (evlist->workload.pid < 0) {
  1209. perror("failed to fork");
  1210. goto out_close_pipes;
  1211. }
  1212. if (!evlist->workload.pid) {
  1213. int ret;
  1214. if (pipe_output)
  1215. dup2(2, 1);
  1216. signal(SIGTERM, SIG_DFL);
  1217. close(child_ready_pipe[0]);
  1218. close(go_pipe[1]);
  1219. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  1220. /*
  1221. * Tell the parent we're ready to go
  1222. */
  1223. close(child_ready_pipe[1]);
  1224. /*
  1225. * Wait until the parent tells us to go.
  1226. */
  1227. ret = read(go_pipe[0], &bf, 1);
  1228. /*
  1229. * The parent will ask for the execvp() to be performed by
  1230. * writing exactly one byte, in workload.cork_fd, usually via
  1231. * perf_evlist__start_workload().
  1232. *
  1233. * For cancelling the workload without actually running it,
  1234. * the parent will just close workload.cork_fd, without writing
  1235. * anything, i.e. read will return zero and we just exit()
  1236. * here.
  1237. */
  1238. if (ret != 1) {
  1239. if (ret == -1)
  1240. perror("unable to read pipe");
  1241. exit(ret);
  1242. }
  1243. execvp(argv[0], (char **)argv);
  1244. if (exec_error) {
  1245. union sigval val;
  1246. val.sival_int = errno;
  1247. if (sigqueue(getppid(), SIGUSR1, val))
  1248. perror(argv[0]);
  1249. } else
  1250. perror(argv[0]);
  1251. exit(-1);
  1252. }
  1253. if (exec_error) {
  1254. struct sigaction act = {
  1255. .sa_flags = SA_SIGINFO,
  1256. .sa_sigaction = exec_error,
  1257. };
  1258. sigaction(SIGUSR1, &act, NULL);
  1259. }
  1260. if (target__none(target)) {
  1261. if (evlist->threads == NULL) {
  1262. fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
  1263. __func__, __LINE__);
  1264. goto out_close_pipes;
  1265. }
  1266. thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
  1267. }
  1268. close(child_ready_pipe[1]);
  1269. close(go_pipe[0]);
  1270. /*
  1271. * wait for child to settle
  1272. */
  1273. if (read(child_ready_pipe[0], &bf, 1) == -1) {
  1274. perror("unable to read pipe");
  1275. goto out_close_pipes;
  1276. }
  1277. fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
  1278. evlist->workload.cork_fd = go_pipe[1];
  1279. close(child_ready_pipe[0]);
  1280. return 0;
  1281. out_close_pipes:
  1282. close(go_pipe[0]);
  1283. close(go_pipe[1]);
  1284. out_close_ready_pipe:
  1285. close(child_ready_pipe[0]);
  1286. close(child_ready_pipe[1]);
  1287. return -1;
  1288. }
  1289. int perf_evlist__start_workload(struct perf_evlist *evlist)
  1290. {
  1291. if (evlist->workload.cork_fd > 0) {
  1292. char bf = 0;
  1293. int ret;
  1294. /*
  1295. * Remove the cork, let it rip!
  1296. */
  1297. ret = write(evlist->workload.cork_fd, &bf, 1);
  1298. if (ret < 0)
  1299. perror("enable to write to pipe");
  1300. close(evlist->workload.cork_fd);
  1301. return ret;
  1302. }
  1303. return 0;
  1304. }
  1305. int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
  1306. struct perf_sample *sample)
  1307. {
  1308. struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
  1309. if (!evsel)
  1310. return -EFAULT;
  1311. return perf_evsel__parse_sample(evsel, event, sample);
  1312. }
  1313. size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
  1314. {
  1315. struct perf_evsel *evsel;
  1316. size_t printed = 0;
  1317. evlist__for_each(evlist, evsel) {
  1318. printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
  1319. perf_evsel__name(evsel));
  1320. }
  1321. return printed + fprintf(fp, "\n");
  1322. }
  1323. int perf_evlist__strerror_open(struct perf_evlist *evlist,
  1324. int err, char *buf, size_t size)
  1325. {
  1326. int printed, value;
  1327. char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
  1328. switch (err) {
  1329. case EACCES:
  1330. case EPERM:
  1331. printed = scnprintf(buf, size,
  1332. "Error:\t%s.\n"
  1333. "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
  1334. value = perf_event_paranoid();
  1335. printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
  1336. if (value >= 2) {
  1337. printed += scnprintf(buf + printed, size - printed,
  1338. "For your workloads it needs to be <= 1\nHint:\t");
  1339. }
  1340. printed += scnprintf(buf + printed, size - printed,
  1341. "For system wide tracing it needs to be set to -1.\n");
  1342. printed += scnprintf(buf + printed, size - printed,
  1343. "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
  1344. "Hint:\tThe current value is %d.", value);
  1345. break;
  1346. case EINVAL: {
  1347. struct perf_evsel *first = perf_evlist__first(evlist);
  1348. int max_freq;
  1349. if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
  1350. goto out_default;
  1351. if (first->attr.sample_freq < (u64)max_freq)
  1352. goto out_default;
  1353. printed = scnprintf(buf, size,
  1354. "Error:\t%s.\n"
  1355. "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
  1356. "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
  1357. emsg, max_freq, first->attr.sample_freq);
  1358. break;
  1359. }
  1360. default:
  1361. out_default:
  1362. scnprintf(buf, size, "%s", emsg);
  1363. break;
  1364. }
  1365. return 0;
  1366. }
  1367. int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
  1368. {
  1369. char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
  1370. int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
  1371. switch (err) {
  1372. case EPERM:
  1373. sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
  1374. printed += scnprintf(buf + printed, size - printed,
  1375. "Error:\t%s.\n"
  1376. "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
  1377. "Hint:\tTried using %zd kB.\n",
  1378. emsg, pages_max_per_user, pages_attempted);
  1379. if (pages_attempted >= pages_max_per_user) {
  1380. printed += scnprintf(buf + printed, size - printed,
  1381. "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
  1382. pages_max_per_user + pages_attempted);
  1383. }
  1384. printed += scnprintf(buf + printed, size - printed,
  1385. "Hint:\tTry using a smaller -m/--mmap-pages value.");
  1386. break;
  1387. default:
  1388. scnprintf(buf, size, "%s", emsg);
  1389. break;
  1390. }
  1391. return 0;
  1392. }
  1393. void perf_evlist__to_front(struct perf_evlist *evlist,
  1394. struct perf_evsel *move_evsel)
  1395. {
  1396. struct perf_evsel *evsel, *n;
  1397. LIST_HEAD(move);
  1398. if (move_evsel == perf_evlist__first(evlist))
  1399. return;
  1400. evlist__for_each_safe(evlist, n, evsel) {
  1401. if (evsel->leader == move_evsel->leader)
  1402. list_move_tail(&evsel->node, &move);
  1403. }
  1404. list_splice(&move, &evlist->entries);
  1405. }
  1406. void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
  1407. struct perf_evsel *tracking_evsel)
  1408. {
  1409. struct perf_evsel *evsel;
  1410. if (tracking_evsel->tracking)
  1411. return;
  1412. evlist__for_each(evlist, evsel) {
  1413. if (evsel != tracking_evsel)
  1414. evsel->tracking = false;
  1415. }
  1416. tracking_evsel->tracking = true;
  1417. }
  1418. struct perf_evsel *
  1419. perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,
  1420. const char *str)
  1421. {
  1422. struct perf_evsel *evsel;
  1423. evlist__for_each(evlist, evsel) {
  1424. if (!evsel->name)
  1425. continue;
  1426. if (strcmp(str, evsel->name) == 0)
  1427. return evsel;
  1428. }
  1429. return NULL;
  1430. }