evlist.c 40 KB

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