evlist.c 40 KB

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