evlist.c 28 KB

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