evlist.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. struct perf_mmap *md = &evlist->mmap[idx];
  265. unsigned int head = perf_mmap__read_head(md);
  266. unsigned int old = md->prev;
  267. unsigned char *data = md->base + page_size;
  268. union perf_event *event = NULL;
  269. if (evlist->overwrite) {
  270. /*
  271. * If we're further behind than half the buffer, there's a chance
  272. * the writer will bite our tail and mess up the samples under us.
  273. *
  274. * If we somehow ended up ahead of the head, we got messed up.
  275. *
  276. * In either case, truncate and restart at head.
  277. */
  278. int diff = head - old;
  279. if (diff > md->mask / 2 || diff < 0) {
  280. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  281. /*
  282. * head points to a known good entry, start there.
  283. */
  284. old = head;
  285. }
  286. }
  287. if (old != head) {
  288. size_t size;
  289. event = (union perf_event *)&data[old & md->mask];
  290. size = event->header.size;
  291. /*
  292. * Event straddles the mmap boundary -- header should always
  293. * be inside due to u64 alignment of output.
  294. */
  295. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  296. unsigned int offset = old;
  297. unsigned int len = min(sizeof(*event), size), cpy;
  298. void *dst = &evlist->event_copy;
  299. do {
  300. cpy = min(md->mask + 1 - (offset & md->mask), len);
  301. memcpy(dst, &data[offset & md->mask], cpy);
  302. offset += cpy;
  303. dst += cpy;
  304. len -= cpy;
  305. } while (len);
  306. event = &evlist->event_copy;
  307. }
  308. old += size;
  309. }
  310. md->prev = old;
  311. if (!evlist->overwrite)
  312. perf_mmap__write_tail(md, old);
  313. return event;
  314. }
  315. void perf_evlist__munmap(struct perf_evlist *evlist)
  316. {
  317. int i;
  318. for (i = 0; i < evlist->nr_mmaps; i++) {
  319. if (evlist->mmap[i].base != NULL) {
  320. munmap(evlist->mmap[i].base, evlist->mmap_len);
  321. evlist->mmap[i].base = NULL;
  322. }
  323. }
  324. free(evlist->mmap);
  325. evlist->mmap = NULL;
  326. }
  327. static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
  328. {
  329. evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
  330. if (cpu_map__all(evlist->cpus))
  331. evlist->nr_mmaps = evlist->threads->nr;
  332. evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
  333. return evlist->mmap != NULL ? 0 : -ENOMEM;
  334. }
  335. static int __perf_evlist__mmap(struct perf_evlist *evlist,
  336. int idx, int prot, int mask, int fd)
  337. {
  338. evlist->mmap[idx].prev = 0;
  339. evlist->mmap[idx].mask = mask;
  340. evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
  341. MAP_SHARED, fd, 0);
  342. if (evlist->mmap[idx].base == MAP_FAILED) {
  343. evlist->mmap[idx].base = NULL;
  344. return -1;
  345. }
  346. perf_evlist__add_pollfd(evlist, fd);
  347. return 0;
  348. }
  349. static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
  350. {
  351. struct perf_evsel *evsel;
  352. int cpu, thread;
  353. for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
  354. int output = -1;
  355. for (thread = 0; thread < evlist->threads->nr; thread++) {
  356. list_for_each_entry(evsel, &evlist->entries, node) {
  357. int fd = FD(evsel, cpu, thread);
  358. if (output == -1) {
  359. output = fd;
  360. if (__perf_evlist__mmap(evlist, cpu,
  361. prot, mask, output) < 0)
  362. goto out_unmap;
  363. } else {
  364. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
  365. goto out_unmap;
  366. }
  367. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  368. perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
  369. goto out_unmap;
  370. }
  371. }
  372. }
  373. return 0;
  374. out_unmap:
  375. for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
  376. if (evlist->mmap[cpu].base != NULL) {
  377. munmap(evlist->mmap[cpu].base, evlist->mmap_len);
  378. evlist->mmap[cpu].base = NULL;
  379. }
  380. }
  381. return -1;
  382. }
  383. static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
  384. {
  385. struct perf_evsel *evsel;
  386. int thread;
  387. for (thread = 0; thread < evlist->threads->nr; thread++) {
  388. int output = -1;
  389. list_for_each_entry(evsel, &evlist->entries, node) {
  390. int fd = FD(evsel, 0, thread);
  391. if (output == -1) {
  392. output = fd;
  393. if (__perf_evlist__mmap(evlist, thread,
  394. prot, mask, output) < 0)
  395. goto out_unmap;
  396. } else {
  397. if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
  398. goto out_unmap;
  399. }
  400. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  401. perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
  402. goto out_unmap;
  403. }
  404. }
  405. return 0;
  406. out_unmap:
  407. for (thread = 0; thread < evlist->threads->nr; thread++) {
  408. if (evlist->mmap[thread].base != NULL) {
  409. munmap(evlist->mmap[thread].base, evlist->mmap_len);
  410. evlist->mmap[thread].base = NULL;
  411. }
  412. }
  413. return -1;
  414. }
  415. /** perf_evlist__mmap - Create per cpu maps to receive events
  416. *
  417. * @evlist - list of events
  418. * @pages - map length in pages
  419. * @overwrite - overwrite older events?
  420. *
  421. * If overwrite is false the user needs to signal event consuption using:
  422. *
  423. * struct perf_mmap *m = &evlist->mmap[cpu];
  424. * unsigned int head = perf_mmap__read_head(m);
  425. *
  426. * perf_mmap__write_tail(m, head)
  427. *
  428. * Using perf_evlist__read_on_cpu does this automatically.
  429. */
  430. int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
  431. bool overwrite)
  432. {
  433. struct perf_evsel *evsel;
  434. const struct cpu_map *cpus = evlist->cpus;
  435. const struct thread_map *threads = evlist->threads;
  436. int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
  437. /* 512 kiB: default amount of unprivileged mlocked memory */
  438. if (pages == UINT_MAX)
  439. pages = (512 * 1024) / page_size;
  440. else if (!is_power_of_2(pages))
  441. return -EINVAL;
  442. mask = pages * page_size - 1;
  443. if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
  444. return -ENOMEM;
  445. if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
  446. return -ENOMEM;
  447. evlist->overwrite = overwrite;
  448. evlist->mmap_len = (pages + 1) * page_size;
  449. list_for_each_entry(evsel, &evlist->entries, node) {
  450. if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
  451. evsel->sample_id == NULL &&
  452. perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
  453. return -ENOMEM;
  454. }
  455. if (cpu_map__all(cpus))
  456. return perf_evlist__mmap_per_thread(evlist, prot, mask);
  457. return perf_evlist__mmap_per_cpu(evlist, prot, mask);
  458. }
  459. int perf_evlist__create_maps(struct perf_evlist *evlist,
  460. struct perf_target *target)
  461. {
  462. evlist->threads = thread_map__new_str(target->pid, target->tid,
  463. target->uid);
  464. if (evlist->threads == NULL)
  465. return -1;
  466. if (perf_target__has_task(target))
  467. evlist->cpus = cpu_map__dummy_new();
  468. else if (!perf_target__has_cpu(target) && !target->uses_mmap)
  469. evlist->cpus = cpu_map__dummy_new();
  470. else
  471. evlist->cpus = cpu_map__new(target->cpu_list);
  472. if (evlist->cpus == NULL)
  473. goto out_delete_threads;
  474. return 0;
  475. out_delete_threads:
  476. thread_map__delete(evlist->threads);
  477. return -1;
  478. }
  479. void perf_evlist__delete_maps(struct perf_evlist *evlist)
  480. {
  481. cpu_map__delete(evlist->cpus);
  482. thread_map__delete(evlist->threads);
  483. evlist->cpus = NULL;
  484. evlist->threads = NULL;
  485. }
  486. int perf_evlist__apply_filters(struct perf_evlist *evlist)
  487. {
  488. struct perf_evsel *evsel;
  489. int err = 0;
  490. const int ncpus = cpu_map__nr(evlist->cpus),
  491. nthreads = evlist->threads->nr;
  492. list_for_each_entry(evsel, &evlist->entries, node) {
  493. if (evsel->filter == NULL)
  494. continue;
  495. err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
  496. if (err)
  497. break;
  498. }
  499. return err;
  500. }
  501. int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
  502. {
  503. struct perf_evsel *evsel;
  504. int err = 0;
  505. const int ncpus = cpu_map__nr(evlist->cpus),
  506. nthreads = evlist->threads->nr;
  507. list_for_each_entry(evsel, &evlist->entries, node) {
  508. err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
  509. if (err)
  510. break;
  511. }
  512. return err;
  513. }
  514. bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
  515. {
  516. struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
  517. list_for_each_entry_continue(pos, &evlist->entries, node) {
  518. if (first->attr.sample_type != pos->attr.sample_type)
  519. return false;
  520. }
  521. return true;
  522. }
  523. u64 perf_evlist__sample_type(struct perf_evlist *evlist)
  524. {
  525. struct perf_evsel *first = perf_evlist__first(evlist);
  526. return first->attr.sample_type;
  527. }
  528. u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
  529. {
  530. struct perf_evsel *first = perf_evlist__first(evlist);
  531. struct perf_sample *data;
  532. u64 sample_type;
  533. u16 size = 0;
  534. if (!first->attr.sample_id_all)
  535. goto out;
  536. sample_type = first->attr.sample_type;
  537. if (sample_type & PERF_SAMPLE_TID)
  538. size += sizeof(data->tid) * 2;
  539. if (sample_type & PERF_SAMPLE_TIME)
  540. size += sizeof(data->time);
  541. if (sample_type & PERF_SAMPLE_ID)
  542. size += sizeof(data->id);
  543. if (sample_type & PERF_SAMPLE_STREAM_ID)
  544. size += sizeof(data->stream_id);
  545. if (sample_type & PERF_SAMPLE_CPU)
  546. size += sizeof(data->cpu) * 2;
  547. out:
  548. return size;
  549. }
  550. bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
  551. {
  552. struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
  553. list_for_each_entry_continue(pos, &evlist->entries, node) {
  554. if (first->attr.sample_id_all != pos->attr.sample_id_all)
  555. return false;
  556. }
  557. return true;
  558. }
  559. bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
  560. {
  561. struct perf_evsel *first = perf_evlist__first(evlist);
  562. return first->attr.sample_id_all;
  563. }
  564. void perf_evlist__set_selected(struct perf_evlist *evlist,
  565. struct perf_evsel *evsel)
  566. {
  567. evlist->selected = evsel;
  568. }
  569. int perf_evlist__open(struct perf_evlist *evlist)
  570. {
  571. struct perf_evsel *evsel;
  572. int err, ncpus, nthreads;
  573. list_for_each_entry(evsel, &evlist->entries, node) {
  574. err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
  575. if (err < 0)
  576. goto out_err;
  577. }
  578. return 0;
  579. out_err:
  580. ncpus = evlist->cpus ? evlist->cpus->nr : 1;
  581. nthreads = evlist->threads ? evlist->threads->nr : 1;
  582. list_for_each_entry_reverse(evsel, &evlist->entries, node)
  583. perf_evsel__close(evsel, ncpus, nthreads);
  584. errno = -err;
  585. return err;
  586. }
  587. int perf_evlist__prepare_workload(struct perf_evlist *evlist,
  588. struct perf_record_opts *opts,
  589. const char *argv[])
  590. {
  591. int child_ready_pipe[2], go_pipe[2];
  592. char bf;
  593. if (pipe(child_ready_pipe) < 0) {
  594. perror("failed to create 'ready' pipe");
  595. return -1;
  596. }
  597. if (pipe(go_pipe) < 0) {
  598. perror("failed to create 'go' pipe");
  599. goto out_close_ready_pipe;
  600. }
  601. evlist->workload.pid = fork();
  602. if (evlist->workload.pid < 0) {
  603. perror("failed to fork");
  604. goto out_close_pipes;
  605. }
  606. if (!evlist->workload.pid) {
  607. if (opts->pipe_output)
  608. dup2(2, 1);
  609. close(child_ready_pipe[0]);
  610. close(go_pipe[1]);
  611. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  612. /*
  613. * Do a dummy execvp to get the PLT entry resolved,
  614. * so we avoid the resolver overhead on the real
  615. * execvp call.
  616. */
  617. execvp("", (char **)argv);
  618. /*
  619. * Tell the parent we're ready to go
  620. */
  621. close(child_ready_pipe[1]);
  622. /*
  623. * Wait until the parent tells us to go.
  624. */
  625. if (read(go_pipe[0], &bf, 1) == -1)
  626. perror("unable to read pipe");
  627. execvp(argv[0], (char **)argv);
  628. perror(argv[0]);
  629. kill(getppid(), SIGUSR1);
  630. exit(-1);
  631. }
  632. if (perf_target__none(&opts->target))
  633. evlist->threads->map[0] = evlist->workload.pid;
  634. close(child_ready_pipe[1]);
  635. close(go_pipe[0]);
  636. /*
  637. * wait for child to settle
  638. */
  639. if (read(child_ready_pipe[0], &bf, 1) == -1) {
  640. perror("unable to read pipe");
  641. goto out_close_pipes;
  642. }
  643. evlist->workload.cork_fd = go_pipe[1];
  644. close(child_ready_pipe[0]);
  645. return 0;
  646. out_close_pipes:
  647. close(go_pipe[0]);
  648. close(go_pipe[1]);
  649. out_close_ready_pipe:
  650. close(child_ready_pipe[0]);
  651. close(child_ready_pipe[1]);
  652. return -1;
  653. }
  654. int perf_evlist__start_workload(struct perf_evlist *evlist)
  655. {
  656. if (evlist->workload.cork_fd > 0) {
  657. /*
  658. * Remove the cork, let it rip!
  659. */
  660. return close(evlist->workload.cork_fd);
  661. }
  662. return 0;
  663. }
  664. int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
  665. struct perf_sample *sample)
  666. {
  667. struct perf_evsel *evsel = perf_evlist__first(evlist);
  668. return perf_evsel__parse_sample(evsel, event, sample);
  669. }
  670. size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
  671. {
  672. struct perf_evsel *evsel;
  673. size_t printed = 0;
  674. list_for_each_entry(evsel, &evlist->entries, node) {
  675. printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
  676. perf_evsel__name(evsel));
  677. }
  678. return printed + fprintf(fp, "\n");;
  679. }