evlist.c 41 KB

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