evlist.c 19 KB

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