evlist.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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 <lk/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 <unistd.h>
  18. #include "parse-events.h"
  19. #include <sys/mman.h>
  20. #include <linux/bitops.h>
  21. #include <linux/hash.h>
  22. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  23. #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
  24. void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
  25. struct thread_map *threads)
  26. {
  27. int i;
  28. for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
  29. INIT_HLIST_HEAD(&evlist->heads[i]);
  30. INIT_LIST_HEAD(&evlist->entries);
  31. perf_evlist__set_maps(evlist, cpus, threads);
  32. evlist->workload.pid = -1;
  33. }
  34. struct perf_evlist *perf_evlist__new(void)
  35. {
  36. struct perf_evlist *evlist = zalloc(sizeof(*evlist));
  37. if (evlist != NULL)
  38. perf_evlist__init(evlist, NULL, NULL);
  39. return evlist;
  40. }
  41. void perf_evlist__config(struct perf_evlist *evlist,
  42. struct perf_record_opts *opts)
  43. {
  44. struct perf_evsel *evsel;
  45. /*
  46. * Set the evsel leader links before we configure attributes,
  47. * since some might depend on this info.
  48. */
  49. if (opts->group)
  50. perf_evlist__set_leader(evlist);
  51. if (evlist->cpus->map[0] < 0)
  52. opts->no_inherit = true;
  53. list_for_each_entry(evsel, &evlist->entries, node) {
  54. perf_evsel__config(evsel, opts);
  55. if (evlist->nr_entries > 1)
  56. perf_evsel__set_sample_id(evsel);
  57. }
  58. }
  59. static void perf_evlist__purge(struct perf_evlist *evlist)
  60. {
  61. struct perf_evsel *pos, *n;
  62. list_for_each_entry_safe(pos, n, &evlist->entries, node) {
  63. list_del_init(&pos->node);
  64. perf_evsel__delete(pos);
  65. }
  66. evlist->nr_entries = 0;
  67. }
  68. void perf_evlist__exit(struct perf_evlist *evlist)
  69. {
  70. free(evlist->mmap);
  71. free(evlist->pollfd);
  72. evlist->mmap = NULL;
  73. evlist->pollfd = NULL;
  74. }
  75. void perf_evlist__delete(struct perf_evlist *evlist)
  76. {
  77. perf_evlist__purge(evlist);
  78. perf_evlist__exit(evlist);
  79. free(evlist);
  80. }
  81. void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
  82. {
  83. list_add_tail(&entry->node, &evlist->entries);
  84. ++evlist->nr_entries;
  85. }
  86. void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
  87. struct list_head *list,
  88. int nr_entries)
  89. {
  90. list_splice_tail(list, &evlist->entries);
  91. evlist->nr_entries += nr_entries;
  92. }
  93. void __perf_evlist__set_leader(struct list_head *list)
  94. {
  95. struct perf_evsel *evsel, *leader;
  96. leader = list_entry(list->next, struct perf_evsel, node);
  97. evsel = list_entry(list->prev, struct perf_evsel, node);
  98. leader->nr_members = evsel->idx - leader->idx + 1;
  99. list_for_each_entry(evsel, list, node) {
  100. evsel->leader = leader;
  101. }
  102. }
  103. void perf_evlist__set_leader(struct perf_evlist *evlist)
  104. {
  105. if (evlist->nr_entries) {
  106. evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
  107. __perf_evlist__set_leader(&evlist->entries);
  108. }
  109. }
  110. int perf_evlist__add_default(struct perf_evlist *evlist)
  111. {
  112. struct perf_event_attr attr = {
  113. .type = PERF_TYPE_HARDWARE,
  114. .config = PERF_COUNT_HW_CPU_CYCLES,
  115. };
  116. struct perf_evsel *evsel;
  117. event_attr_init(&attr);
  118. evsel = perf_evsel__new(&attr, 0);
  119. if (evsel == NULL)
  120. goto error;
  121. /* use strdup() because free(evsel) assumes name is allocated */
  122. evsel->name = strdup("cycles");
  123. if (!evsel->name)
  124. goto error_free;
  125. perf_evlist__add(evlist, evsel);
  126. return 0;
  127. error_free:
  128. perf_evsel__delete(evsel);
  129. error:
  130. return -ENOMEM;
  131. }
  132. static int perf_evlist__add_attrs(struct perf_evlist *evlist,
  133. struct perf_event_attr *attrs, size_t nr_attrs)
  134. {
  135. struct perf_evsel *evsel, *n;
  136. LIST_HEAD(head);
  137. size_t i;
  138. for (i = 0; i < nr_attrs; i++) {
  139. evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
  140. if (evsel == NULL)
  141. goto out_delete_partial_list;
  142. list_add_tail(&evsel->node, &head);
  143. }
  144. perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
  145. return 0;
  146. out_delete_partial_list:
  147. list_for_each_entry_safe(evsel, n, &head, node)
  148. perf_evsel__delete(evsel);
  149. return -1;
  150. }
  151. int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
  152. struct perf_event_attr *attrs, size_t nr_attrs)
  153. {
  154. size_t i;
  155. for (i = 0; i < nr_attrs; i++)
  156. event_attr_init(attrs + i);
  157. return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
  158. }
  159. struct perf_evsel *
  160. perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
  161. {
  162. struct perf_evsel *evsel;
  163. list_for_each_entry(evsel, &evlist->entries, node) {
  164. if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
  165. (int)evsel->attr.config == id)
  166. return evsel;
  167. }
  168. return NULL;
  169. }
  170. int perf_evlist__add_newtp(struct perf_evlist *evlist,
  171. const char *sys, const char *name, void *handler)
  172. {
  173. struct perf_evsel *evsel;
  174. evsel = perf_evsel__newtp(sys, name, evlist->nr_entries);
  175. if (evsel == NULL)
  176. return -1;
  177. evsel->handler.func = handler;
  178. perf_evlist__add(evlist, evsel);
  179. return 0;
  180. }
  181. void perf_evlist__disable(struct perf_evlist *evlist)
  182. {
  183. int cpu, thread;
  184. struct perf_evsel *pos;
  185. int nr_cpus = cpu_map__nr(evlist->cpus);
  186. int nr_threads = thread_map__nr(evlist->threads);
  187. for (cpu = 0; cpu < nr_cpus; cpu++) {
  188. list_for_each_entry(pos, &evlist->entries, node) {
  189. if (!perf_evsel__is_group_leader(pos))
  190. continue;
  191. for (thread = 0; thread < nr_threads; thread++)
  192. ioctl(FD(pos, cpu, thread),
  193. PERF_EVENT_IOC_DISABLE, 0);
  194. }
  195. }
  196. }
  197. void perf_evlist__enable(struct perf_evlist *evlist)
  198. {
  199. int cpu, thread;
  200. struct perf_evsel *pos;
  201. int nr_cpus = cpu_map__nr(evlist->cpus);
  202. int nr_threads = thread_map__nr(evlist->threads);
  203. for (cpu = 0; cpu < nr_cpus; cpu++) {
  204. list_for_each_entry(pos, &evlist->entries, node) {
  205. if (!perf_evsel__is_group_leader(pos))
  206. continue;
  207. for (thread = 0; thread < nr_threads; thread++)
  208. ioctl(FD(pos, cpu, thread),
  209. PERF_EVENT_IOC_ENABLE, 0);
  210. }
  211. }
  212. }
  213. static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
  214. {
  215. int nr_cpus = cpu_map__nr(evlist->cpus);
  216. int nr_threads = thread_map__nr(evlist->threads);
  217. int nfds = nr_cpus * nr_threads * evlist->nr_entries;
  218. evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
  219. return evlist->pollfd != NULL ? 0 : -ENOMEM;
  220. }
  221. void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
  222. {
  223. fcntl(fd, F_SETFL, O_NONBLOCK);
  224. evlist->pollfd[evlist->nr_fds].fd = fd;
  225. evlist->pollfd[evlist->nr_fds].events = POLLIN;
  226. evlist->nr_fds++;
  227. }
  228. static void perf_evlist__id_hash(struct perf_evlist *evlist,
  229. struct perf_evsel *evsel,
  230. int cpu, int thread, u64 id)
  231. {
  232. int hash;
  233. struct perf_sample_id *sid = SID(evsel, cpu, thread);
  234. sid->id = id;
  235. sid->evsel = evsel;
  236. hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
  237. hlist_add_head(&sid->node, &evlist->heads[hash]);
  238. }
  239. void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
  240. int cpu, int thread, u64 id)
  241. {
  242. perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
  243. evsel->id[evsel->ids++] = id;
  244. }
  245. static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
  246. struct perf_evsel *evsel,
  247. int cpu, int thread, int fd)
  248. {
  249. u64 read_data[4] = { 0, };
  250. int id_idx = 1; /* The first entry is the counter value */
  251. if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
  252. read(fd, &read_data, sizeof(read_data)) == -1)
  253. return -1;
  254. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  255. ++id_idx;
  256. if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  257. ++id_idx;
  258. perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
  259. return 0;
  260. }
  261. struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
  262. {
  263. struct hlist_head *head;
  264. struct hlist_node *pos;
  265. struct perf_sample_id *sid;
  266. int hash;
  267. if (evlist->nr_entries == 1)
  268. return perf_evlist__first(evlist);
  269. hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
  270. head = &evlist->heads[hash];
  271. hlist_for_each_entry(sid, pos, head, node)
  272. if (sid->id == id)
  273. return sid->evsel;
  274. if (!perf_evlist__sample_id_all(evlist))
  275. return perf_evlist__first(evlist);
  276. return NULL;
  277. }
  278. union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
  279. {
  280. struct perf_mmap *md = &evlist->mmap[idx];
  281. unsigned int head = perf_mmap__read_head(md);
  282. unsigned int old = md->prev;
  283. unsigned char *data = md->base + page_size;
  284. union perf_event *event = NULL;
  285. if (evlist->overwrite) {
  286. /*
  287. * If we're further behind than half the buffer, there's a chance
  288. * the writer will bite our tail and mess up the samples under us.
  289. *
  290. * If we somehow ended up ahead of the head, we got messed up.
  291. *
  292. * In either case, truncate and restart at head.
  293. */
  294. int diff = head - old;
  295. if (diff > md->mask / 2 || diff < 0) {
  296. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  297. /*
  298. * head points to a known good entry, start there.
  299. */
  300. old = head;
  301. }
  302. }
  303. if (old != head) {
  304. size_t size;
  305. event = (union perf_event *)&data[old & md->mask];
  306. size = event->header.size;
  307. /*
  308. * Event straddles the mmap boundary -- header should always
  309. * be inside due to u64 alignment of output.
  310. */
  311. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  312. unsigned int offset = old;
  313. unsigned int len = min(sizeof(*event), size), cpy;
  314. void *dst = &md->event_copy;
  315. do {
  316. cpy = min(md->mask + 1 - (offset & md->mask), len);
  317. memcpy(dst, &data[offset & md->mask], cpy);
  318. offset += cpy;
  319. dst += cpy;
  320. len -= cpy;
  321. } while (len);
  322. event = &md->event_copy;
  323. }
  324. old += size;
  325. }
  326. md->prev = old;
  327. if (!evlist->overwrite)
  328. perf_mmap__write_tail(md, old);
  329. return event;
  330. }
  331. void perf_evlist__munmap(struct perf_evlist *evlist)
  332. {
  333. int i;
  334. for (i = 0; i < evlist->nr_mmaps; i++) {
  335. if (evlist->mmap[i].base != NULL) {
  336. munmap(evlist->mmap[i].base, evlist->mmap_len);
  337. evlist->mmap[i].base = NULL;
  338. }
  339. }
  340. free(evlist->mmap);
  341. evlist->mmap = NULL;
  342. }
  343. static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
  344. {
  345. evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
  346. if (cpu_map__all(evlist->cpus))
  347. evlist->nr_mmaps = thread_map__nr(evlist->threads);
  348. evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
  349. return evlist->mmap != NULL ? 0 : -ENOMEM;
  350. }
  351. static int __perf_evlist__mmap(struct perf_evlist *evlist,
  352. int idx, int prot, int mask, int fd)
  353. {
  354. evlist->mmap[idx].prev = 0;
  355. evlist->mmap[idx].mask = mask;
  356. evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
  357. MAP_SHARED, fd, 0);
  358. if (evlist->mmap[idx].base == MAP_FAILED) {
  359. evlist->mmap[idx].base = NULL;
  360. return -1;
  361. }
  362. perf_evlist__add_pollfd(evlist, fd);
  363. return 0;
  364. }
  365. static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
  366. {
  367. struct perf_evsel *evsel;
  368. int cpu, thread;
  369. int nr_cpus = cpu_map__nr(evlist->cpus);
  370. int nr_threads = thread_map__nr(evlist->threads);
  371. for (cpu = 0; cpu < nr_cpus; cpu++) {
  372. int output = -1;
  373. for (thread = 0; thread < nr_threads; thread++) {
  374. list_for_each_entry(evsel, &evlist->entries, node) {
  375. int fd = FD(evsel, cpu, thread);
  376. if (output == -1) {
  377. output = fd;
  378. if (__perf_evlist__mmap(evlist, cpu,
  379. prot, mask, output) < 0)
  380. goto out_unmap;
  381. } else {
  382. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
  383. goto out_unmap;
  384. }
  385. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  386. perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
  387. goto out_unmap;
  388. }
  389. }
  390. }
  391. return 0;
  392. out_unmap:
  393. for (cpu = 0; cpu < nr_cpus; cpu++) {
  394. if (evlist->mmap[cpu].base != NULL) {
  395. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  396. evlist->mmap[cpu].base = NULL;
  397. }
  398. }
  399. return -1;
  400. }
  401. static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
  402. {
  403. struct perf_evsel *evsel;
  404. int thread;
  405. int nr_threads = thread_map__nr(evlist->threads);
  406. for (thread = 0; thread < nr_threads; thread++) {
  407. int output = -1;
  408. list_for_each_entry(evsel, &evlist->entries, node) {
  409. int fd = FD(evsel, 0, thread);
  410. if (output == -1) {
  411. output = fd;
  412. if (__perf_evlist__mmap(evlist, thread,
  413. prot, mask, output) < 0)
  414. goto out_unmap;
  415. } else {
  416. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
  417. goto out_unmap;
  418. }
  419. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  420. perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
  421. goto out_unmap;
  422. }
  423. }
  424. return 0;
  425. out_unmap:
  426. for (thread = 0; thread < nr_threads; thread++) {
  427. if (evlist->mmap[thread].base != NULL) {
  428. munmap(evlist->mmap[thread].base, evlist->mmap_len);
  429. evlist->mmap[thread].base = NULL;
  430. }
  431. }
  432. return -1;
  433. }
  434. /** perf_evlist__mmap - Create per cpu maps to receive events
  435. *
  436. * @evlist - list of events
  437. * @pages - map length in pages
  438. * @overwrite - overwrite older events?
  439. *
  440. * If overwrite is false the user needs to signal event consuption using:
  441. *
  442. * struct perf_mmap *m = &evlist->mmap[cpu];
  443. * unsigned int head = perf_mmap__read_head(m);
  444. *
  445. * perf_mmap__write_tail(m, head)
  446. *
  447. * Using perf_evlist__read_on_cpu does this automatically.
  448. */
  449. int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
  450. bool overwrite)
  451. {
  452. struct perf_evsel *evsel;
  453. const struct cpu_map *cpus = evlist->cpus;
  454. const struct thread_map *threads = evlist->threads;
  455. int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
  456. /* 512 kiB: default amount of unprivileged mlocked memory */
  457. if (pages == UINT_MAX)
  458. pages = (512 * 1024) / page_size;
  459. else if (!is_power_of_2(pages))
  460. return -EINVAL;
  461. mask = pages * page_size - 1;
  462. if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
  463. return -ENOMEM;
  464. if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
  465. return -ENOMEM;
  466. evlist->overwrite = overwrite;
  467. evlist->mmap_len = (pages + 1) * page_size;
  468. list_for_each_entry(evsel, &evlist->entries, node) {
  469. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  470. evsel->sample_id == NULL &&
  471. perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
  472. return -ENOMEM;
  473. }
  474. if (cpu_map__all(cpus))
  475. return perf_evlist__mmap_per_thread(evlist, prot, mask);
  476. return perf_evlist__mmap_per_cpu(evlist, prot, mask);
  477. }
  478. int perf_evlist__create_maps(struct perf_evlist *evlist,
  479. struct perf_target *target)
  480. {
  481. evlist->threads = thread_map__new_str(target->pid, target->tid,
  482. target->uid);
  483. if (evlist->threads == NULL)
  484. return -1;
  485. if (perf_target__has_task(target))
  486. evlist->cpus = cpu_map__dummy_new();
  487. else if (!perf_target__has_cpu(target) && !target->uses_mmap)
  488. evlist->cpus = cpu_map__dummy_new();
  489. else
  490. evlist->cpus = cpu_map__new(target->cpu_list);
  491. if (evlist->cpus == NULL)
  492. goto out_delete_threads;
  493. return 0;
  494. out_delete_threads:
  495. thread_map__delete(evlist->threads);
  496. return -1;
  497. }
  498. void perf_evlist__delete_maps(struct perf_evlist *evlist)
  499. {
  500. cpu_map__delete(evlist->cpus);
  501. thread_map__delete(evlist->threads);
  502. evlist->cpus = NULL;
  503. evlist->threads = NULL;
  504. }
  505. int perf_evlist__apply_filters(struct perf_evlist *evlist)
  506. {
  507. struct perf_evsel *evsel;
  508. int err = 0;
  509. const int ncpus = cpu_map__nr(evlist->cpus),
  510. nthreads = thread_map__nr(evlist->threads);
  511. list_for_each_entry(evsel, &evlist->entries, node) {
  512. if (evsel->filter == NULL)
  513. continue;
  514. err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
  515. if (err)
  516. break;
  517. }
  518. return err;
  519. }
  520. int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
  521. {
  522. struct perf_evsel *evsel;
  523. int err = 0;
  524. const int ncpus = cpu_map__nr(evlist->cpus),
  525. nthreads = thread_map__nr(evlist->threads);
  526. list_for_each_entry(evsel, &evlist->entries, node) {
  527. err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
  528. if (err)
  529. break;
  530. }
  531. return err;
  532. }
  533. bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
  534. {
  535. struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
  536. list_for_each_entry_continue(pos, &evlist->entries, node) {
  537. if (first->attr.sample_type != pos->attr.sample_type)
  538. return false;
  539. }
  540. return true;
  541. }
  542. u64 perf_evlist__sample_type(struct perf_evlist *evlist)
  543. {
  544. struct perf_evsel *first = perf_evlist__first(evlist);
  545. return first->attr.sample_type;
  546. }
  547. u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
  548. {
  549. struct perf_evsel *first = perf_evlist__first(evlist);
  550. struct perf_sample *data;
  551. u64 sample_type;
  552. u16 size = 0;
  553. if (!first->attr.sample_id_all)
  554. goto out;
  555. sample_type = first->attr.sample_type;
  556. if (sample_type & PERF_SAMPLE_TID)
  557. size += sizeof(data->tid) * 2;
  558. if (sample_type & PERF_SAMPLE_TIME)
  559. size += sizeof(data->time);
  560. if (sample_type & PERF_SAMPLE_ID)
  561. size += sizeof(data->id);
  562. if (sample_type & PERF_SAMPLE_STREAM_ID)
  563. size += sizeof(data->stream_id);
  564. if (sample_type & PERF_SAMPLE_CPU)
  565. size += sizeof(data->cpu) * 2;
  566. out:
  567. return size;
  568. }
  569. bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
  570. {
  571. struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
  572. list_for_each_entry_continue(pos, &evlist->entries, node) {
  573. if (first->attr.sample_id_all != pos->attr.sample_id_all)
  574. return false;
  575. }
  576. return true;
  577. }
  578. bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
  579. {
  580. struct perf_evsel *first = perf_evlist__first(evlist);
  581. return first->attr.sample_id_all;
  582. }
  583. void perf_evlist__set_selected(struct perf_evlist *evlist,
  584. struct perf_evsel *evsel)
  585. {
  586. evlist->selected = evsel;
  587. }
  588. int perf_evlist__open(struct perf_evlist *evlist)
  589. {
  590. struct perf_evsel *evsel;
  591. int err, ncpus, nthreads;
  592. list_for_each_entry(evsel, &evlist->entries, node) {
  593. err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
  594. if (err < 0)
  595. goto out_err;
  596. }
  597. return 0;
  598. out_err:
  599. ncpus = cpu_map__nr(evlist->cpus);
  600. nthreads = thread_map__nr(evlist->threads);
  601. list_for_each_entry_reverse(evsel, &evlist->entries, node)
  602. perf_evsel__close(evsel, ncpus, nthreads);
  603. errno = -err;
  604. return err;
  605. }
  606. int perf_evlist__prepare_workload(struct perf_evlist *evlist,
  607. struct perf_target *target,
  608. struct perf_record_opts *opts,
  609. const char *argv[])
  610. {
  611. int child_ready_pipe[2], go_pipe[2];
  612. char bf;
  613. if (pipe(child_ready_pipe) < 0) {
  614. perror("failed to create 'ready' pipe");
  615. return -1;
  616. }
  617. if (pipe(go_pipe) < 0) {
  618. perror("failed to create 'go' pipe");
  619. goto out_close_ready_pipe;
  620. }
  621. evlist->workload.pid = fork();
  622. if (evlist->workload.pid < 0) {
  623. perror("failed to fork");
  624. goto out_close_pipes;
  625. }
  626. if (!evlist->workload.pid) {
  627. if (opts->pipe_output)
  628. dup2(2, 1);
  629. close(child_ready_pipe[0]);
  630. close(go_pipe[1]);
  631. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  632. /*
  633. * Do a dummy execvp to get the PLT entry resolved,
  634. * so we avoid the resolver overhead on the real
  635. * execvp call.
  636. */
  637. execvp("", (char **)argv);
  638. /*
  639. * Tell the parent we're ready to go
  640. */
  641. close(child_ready_pipe[1]);
  642. /*
  643. * Wait until the parent tells us to go.
  644. */
  645. if (read(go_pipe[0], &bf, 1) == -1)
  646. perror("unable to read pipe");
  647. execvp(argv[0], (char **)argv);
  648. perror(argv[0]);
  649. kill(getppid(), SIGUSR1);
  650. exit(-1);
  651. }
  652. if (perf_target__none(target))
  653. evlist->threads->map[0] = evlist->workload.pid;
  654. close(child_ready_pipe[1]);
  655. close(go_pipe[0]);
  656. /*
  657. * wait for child to settle
  658. */
  659. if (read(child_ready_pipe[0], &bf, 1) == -1) {
  660. perror("unable to read pipe");
  661. goto out_close_pipes;
  662. }
  663. evlist->workload.cork_fd = go_pipe[1];
  664. close(child_ready_pipe[0]);
  665. return 0;
  666. out_close_pipes:
  667. close(go_pipe[0]);
  668. close(go_pipe[1]);
  669. out_close_ready_pipe:
  670. close(child_ready_pipe[0]);
  671. close(child_ready_pipe[1]);
  672. return -1;
  673. }
  674. int perf_evlist__start_workload(struct perf_evlist *evlist)
  675. {
  676. if (evlist->workload.cork_fd > 0) {
  677. /*
  678. * Remove the cork, let it rip!
  679. */
  680. return close(evlist->workload.cork_fd);
  681. }
  682. return 0;
  683. }
  684. int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
  685. struct perf_sample *sample)
  686. {
  687. struct perf_evsel *evsel = perf_evlist__first(evlist);
  688. return perf_evsel__parse_sample(evsel, event, sample);
  689. }
  690. size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
  691. {
  692. struct perf_evsel *evsel;
  693. size_t printed = 0;
  694. list_for_each_entry(evsel, &evlist->entries, node) {
  695. printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
  696. perf_evsel__name(evsel));
  697. }
  698. return printed + fprintf(fp, "\n");;
  699. }