evlist.c 20 KB

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