evlist.c 35 KB

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