evlist.c 34 KB

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