evlist.c 46 KB

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