evlist.c 20 KB

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