191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f8a95309SArnaldo Carvalho de Melo /*
3f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4f8a95309SArnaldo Carvalho de Melo *
5f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further
6f8a95309SArnaldo Carvalho de Melo * copyright notes.
7f8a95309SArnaldo Carvalho de Melo */
8956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h>
9a43783aeSArnaldo Carvalho de Melo #include <errno.h>
10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
115c581041SArnaldo Carvalho de Melo #include <poll.h>
12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
13e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1512864b31SNamhyung Kim #include "target.h"
16361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
17361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
186657a099SAdrian Hunter #include "record.h"
19e3e1a54fSAdrian Hunter #include "debug.h"
2058db1d6eSArnaldo Carvalho de Melo #include "units.h"
215508c9daSSong Liu #include "bpf_counter.h"
2220f2be1dSJiri Olsa #include <internal/lib.h> // page_size
237736627bSAndi Kleen #include "affinity.h"
24c1a604dfSArnaldo Carvalho de Melo #include "../perf.h"
2554cc54deSWang Nan #include "asm/bug.h"
26657ee553SSong Liu #include "bpf-event.h"
272e5a738aSArnaldo Carvalho de Melo #include "util/event.h"
28da949f50SArnaldo Carvalho de Melo #include "util/string2.h"
2940c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
30142544a9SJiri Olsa #include "util/evsel_fprintf.h"
311d3351e6SJin Yao #include "util/pmu.h"
329823147dSArnaldo Carvalho de Melo #include "util/sample.h"
33d180aa56SNamhyung Kim #include "util/bpf-filter.h"
34ac873ac3SIan Rogers #include "util/stat.h"
35ea0c5239SIan Rogers #include "util/util.h"
369607ad3aSArnaldo Carvalho de Melo #include <signal.h>
3735b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
38b397f846SArnaldo Carvalho de Melo #include <sched.h>
39f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
40361c99a6SArnaldo Carvalho de Melo
4150d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
424b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
4350d08e47SArnaldo Carvalho de Melo
44bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
4586a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
46f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
47a7672d1dSArnaldo Carvalho de Melo #include <sys/prctl.h>
486657a099SAdrian Hunter #include <sys/timerfd.h>
49f8a95309SArnaldo Carvalho de Melo
5070db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
5170db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
520389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
538dd2a131SJiri Olsa #include <linux/err.h>
548520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
556657a099SAdrian Hunter #include <linux/time64.h>
567f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
574562a739SJiri Olsa #include <perf/evlist.h>
5888761fa1SJiri Olsa #include <perf/evsel.h>
599c3516d1SJiri Olsa #include <perf/cpumap.h>
607728fa0cSJiri Olsa #include <perf/mmap.h>
6170db7533SArnaldo Carvalho de Melo
62e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
63e14e5497SArnaldo Carvalho de Melo
64748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
65748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
66748fe088SArnaldo Carvalho de Melo #endif
67748fe088SArnaldo Carvalho de Melo
689dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
698cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
70f8a95309SArnaldo Carvalho de Melo
evlist__init(struct evlist * evlist,struct perf_cpu_map * cpus,struct perf_thread_map * threads)7152c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
729749b90eSJiri Olsa struct perf_thread_map *threads)
73361c99a6SArnaldo Carvalho de Melo {
744562a739SJiri Olsa perf_evlist__init(&evlist->core);
75453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads);
7635b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1;
7754cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
788ab705b5SAlexey Budankov evlist->ctl_fd.fd = -1;
798ab705b5SAlexey Budankov evlist->ctl_fd.ack = -1;
808ab705b5SAlexey Budankov evlist->ctl_fd.pos = -1;
81361c99a6SArnaldo Carvalho de Melo }
82361c99a6SArnaldo Carvalho de Melo
evlist__new(void)830f98b11cSJiri Olsa struct evlist *evlist__new(void)
84ef1d1af2SArnaldo Carvalho de Melo {
8563503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist));
86ef1d1af2SArnaldo Carvalho de Melo
87ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL)
8852c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL);
89ef1d1af2SArnaldo Carvalho de Melo
90361c99a6SArnaldo Carvalho de Melo return evlist;
91361c99a6SArnaldo Carvalho de Melo }
92361c99a6SArnaldo Carvalho de Melo
evlist__new_default(void)93606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void)
94b22d54b0SJiri Olsa {
950f98b11cSJiri Olsa struct evlist *evlist = evlist__new();
967b100989SIan Rogers bool can_profile_kernel;
977b100989SIan Rogers int err;
98b22d54b0SJiri Olsa
997b100989SIan Rogers if (!evlist)
1007b100989SIan Rogers return NULL;
1017b100989SIan Rogers
1027b100989SIan Rogers can_profile_kernel = perf_event_paranoid_check(1);
1037b100989SIan Rogers err = parse_event(evlist, can_profile_kernel ? "cycles:P" : "cycles:Pu");
1047b100989SIan Rogers if (err) {
105c12995a5SJiri Olsa evlist__delete(evlist);
106f7799ecfSJames Clark return NULL;
107f7799ecfSJames Clark }
108f7799ecfSJames Clark
109f7799ecfSJames Clark if (evlist->core.nr_entries > 1) {
110f7799ecfSJames Clark struct evsel *evsel;
111f7799ecfSJames Clark
112f7799ecfSJames Clark evlist__for_each_entry(evlist, evsel)
113f7799ecfSJames Clark evsel__set_sample_id(evsel, /*can_sample_identifier=*/false);
114b22d54b0SJiri Olsa }
115b22d54b0SJiri Olsa
116b22d54b0SJiri Olsa return evlist;
117b22d54b0SJiri Olsa }
118b22d54b0SJiri Olsa
evlist__new_dummy(void)119606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void)
1205bae0250SArnaldo Carvalho de Melo {
1210f98b11cSJiri Olsa struct evlist *evlist = evlist__new();
1225bae0250SArnaldo Carvalho de Melo
123e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_dummy(evlist)) {
124c12995a5SJiri Olsa evlist__delete(evlist);
1255bae0250SArnaldo Carvalho de Melo evlist = NULL;
1265bae0250SArnaldo Carvalho de Melo }
1275bae0250SArnaldo Carvalho de Melo
1285bae0250SArnaldo Carvalho de Melo return evlist;
1295bae0250SArnaldo Carvalho de Melo }
1305bae0250SArnaldo Carvalho de Melo
13175562573SAdrian Hunter /**
1320a7e7ec9SArnaldo Carvalho de Melo * evlist__set_id_pos - set the positions of event ids.
13375562573SAdrian Hunter * @evlist: selected event list
13475562573SAdrian Hunter *
13575562573SAdrian Hunter * Events with compatible sample types all have the same id_pos
13675562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist.
13775562573SAdrian Hunter */
evlist__set_id_pos(struct evlist * evlist)1380a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist)
13975562573SAdrian Hunter {
140515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist);
14175562573SAdrian Hunter
14275562573SAdrian Hunter evlist->id_pos = first->id_pos;
14375562573SAdrian Hunter evlist->is_pos = first->is_pos;
14475562573SAdrian Hunter }
14575562573SAdrian Hunter
evlist__update_id_pos(struct evlist * evlist)1460a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist)
147733cd2feSAdrian Hunter {
14832dcd021SJiri Olsa struct evsel *evsel;
149733cd2feSAdrian Hunter
150e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel)
1514b5e87b7SArnaldo Carvalho de Melo evsel__calc_id_pos(evsel);
152733cd2feSAdrian Hunter
1530a7e7ec9SArnaldo Carvalho de Melo evlist__set_id_pos(evlist);
154733cd2feSAdrian Hunter }
155733cd2feSAdrian Hunter
evlist__purge(struct evlist * evlist)156e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
157361c99a6SArnaldo Carvalho de Melo {
15832dcd021SJiri Olsa struct evsel *pos, *n;
159361c99a6SArnaldo Carvalho de Melo
160e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) {
161b27c4eceSJiri Olsa list_del_init(&pos->core.node);
162d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL;
1635eb2dd2aSJiri Olsa evsel__delete(pos);
164361c99a6SArnaldo Carvalho de Melo }
165361c99a6SArnaldo Carvalho de Melo
1666484d2f9SJiri Olsa evlist->core.nr_entries = 0;
167361c99a6SArnaldo Carvalho de Melo }
168361c99a6SArnaldo Carvalho de Melo
evlist__exit(struct evlist * evlist)169470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
170ef1d1af2SArnaldo Carvalho de Melo {
1716657a099SAdrian Hunter event_enable_timer__exit(&evlist->eet);
17204662523SArnaldo Carvalho de Melo zfree(&evlist->mmap);
1730b72d69aSWang Nan zfree(&evlist->overwrite_mmap);
17493dd6e28SJiri Olsa perf_evlist__exit(&evlist->core);
175ef1d1af2SArnaldo Carvalho de Melo }
176ef1d1af2SArnaldo Carvalho de Melo
evlist__delete(struct evlist * evlist)177c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
178361c99a6SArnaldo Carvalho de Melo {
1790b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL)
1800b04b3dcSArnaldo Carvalho de Melo return;
1810b04b3dcSArnaldo Carvalho de Melo
182ac873ac3SIan Rogers evlist__free_stats(evlist);
183db6b7b13SJiri Olsa evlist__munmap(evlist);
184750b4edeSJiri Olsa evlist__close(evlist);
185e6b1878dSJiri Olsa evlist__purge(evlist);
186470579b0SJiri Olsa evlist__exit(evlist);
187361c99a6SArnaldo Carvalho de Melo free(evlist);
188361c99a6SArnaldo Carvalho de Melo }
189361c99a6SArnaldo Carvalho de Melo
evlist__add(struct evlist * evlist,struct evsel * entry)190a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
191361c99a6SArnaldo Carvalho de Melo {
1926484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core);
19338fe0e01SJiri Olsa entry->evlist = evlist;
19438fe0e01SJiri Olsa entry->tracking = !entry->core.idx;
1956484d2f9SJiri Olsa
1966484d2f9SJiri Olsa if (evlist->core.nr_entries == 1)
1970a7e7ec9SArnaldo Carvalho de Melo evlist__set_id_pos(evlist);
198361c99a6SArnaldo Carvalho de Melo }
199361c99a6SArnaldo Carvalho de Melo
evlist__remove(struct evlist * evlist,struct evsel * evsel)20016251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
2014768230aSAdrian Hunter {
2024768230aSAdrian Hunter evsel->evlist = NULL;
20352e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core);
2044768230aSAdrian Hunter }
2054768230aSAdrian Hunter
evlist__splice_list_tail(struct evlist * evlist,struct list_head * list)206e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
20750d08e47SArnaldo Carvalho de Melo {
2086d2783feSJohn Garry while (!list_empty(list)) {
2096d2783feSJohn Garry struct evsel *evsel, *temp, *leader = NULL;
21075562573SAdrian Hunter
211e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) {
212b27c4eceSJiri Olsa list_del_init(&evsel->core.node);
213a1cf3a75SJiri Olsa evlist__add(evlist, evsel);
2146d2783feSJohn Garry leader = evsel;
2156d2783feSJohn Garry break;
2166d2783feSJohn Garry }
2176d2783feSJohn Garry
2186d2783feSJohn Garry __evlist__for_each_entry_safe(list, temp, evsel) {
219fba7c866SJiri Olsa if (evsel__has_leader(evsel, leader)) {
2206d2783feSJohn Garry list_del_init(&evsel->core.node);
2216d2783feSJohn Garry evlist__add(evlist, evsel);
2226d2783feSJohn Garry }
2236d2783feSJohn Garry }
224f114d6efSAdrian Hunter }
22550d08e47SArnaldo Carvalho de Melo }
22650d08e47SArnaldo Carvalho de Melo
__evlist__set_tracepoints_handlers(struct evlist * evlist,const struct evsel_str_handler * assocs,size_t nr_assocs)227c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
228c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs)
229c0e53476SArnaldo Carvalho de Melo {
230c0e53476SArnaldo Carvalho de Melo size_t i;
231c0e53476SArnaldo Carvalho de Melo int err;
232c0e53476SArnaldo Carvalho de Melo
233c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) {
234c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it.
235b02736f7SArnaldo Carvalho de Melo struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
236c0e53476SArnaldo Carvalho de Melo if (evsel == NULL)
237c0e53476SArnaldo Carvalho de Melo continue;
238c0e53476SArnaldo Carvalho de Melo
239c0e53476SArnaldo Carvalho de Melo err = -EEXIST;
240c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL)
241c0e53476SArnaldo Carvalho de Melo goto out;
242c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler;
243c0e53476SArnaldo Carvalho de Melo }
244c0e53476SArnaldo Carvalho de Melo
245c0e53476SArnaldo Carvalho de Melo err = 0;
246c0e53476SArnaldo Carvalho de Melo out:
247c0e53476SArnaldo Carvalho de Melo return err;
248c0e53476SArnaldo Carvalho de Melo }
249c0e53476SArnaldo Carvalho de Melo
evlist__set_leader(struct evlist * evlist)2505f8f9567SIan Rogers static void evlist__set_leader(struct evlist *evlist)
2516a4bb04cSJiri Olsa {
2522e6263abSJiri Olsa perf_evlist__set_leader(&evlist->core);
25397f63e4aSNamhyung Kim }
2546a4bb04cSJiri Olsa
evlist__dummy_event(struct evlist * evlist)25582944899SAdrian Hunter static struct evsel *evlist__dummy_event(struct evlist *evlist)
2565bae0250SArnaldo Carvalho de Melo {
2575bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = {
2585bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE,
2595bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY,
2605bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */
26167a21d9bSIan Rogers /* Avoid frequency mode for dummy events to avoid associated timers. */
26267a21d9bSIan Rogers .freq = 0,
26367a21d9bSIan Rogers .sample_period = 1,
2645bae0250SArnaldo Carvalho de Melo };
26582944899SAdrian Hunter
26682944899SAdrian Hunter return evsel__new_idx(&attr, evlist->core.nr_entries);
26782944899SAdrian Hunter }
26882944899SAdrian Hunter
evlist__add_dummy(struct evlist * evlist)26982944899SAdrian Hunter int evlist__add_dummy(struct evlist *evlist)
27082944899SAdrian Hunter {
27182944899SAdrian Hunter struct evsel *evsel = evlist__dummy_event(evlist);
2725bae0250SArnaldo Carvalho de Melo
2735bae0250SArnaldo Carvalho de Melo if (evsel == NULL)
2745bae0250SArnaldo Carvalho de Melo return -ENOMEM;
2755bae0250SArnaldo Carvalho de Melo
276a1cf3a75SJiri Olsa evlist__add(evlist, evsel);
2775bae0250SArnaldo Carvalho de Melo return 0;
2785bae0250SArnaldo Carvalho de Melo }
2795bae0250SArnaldo Carvalho de Melo
evlist__add_aux_dummy(struct evlist * evlist,bool system_wide)280126d68fdSAdrian Hunter struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
281126d68fdSAdrian Hunter {
282126d68fdSAdrian Hunter struct evsel *evsel = evlist__dummy_event(evlist);
283126d68fdSAdrian Hunter
284126d68fdSAdrian Hunter if (!evsel)
285126d68fdSAdrian Hunter return NULL;
286126d68fdSAdrian Hunter
287126d68fdSAdrian Hunter evsel->core.attr.exclude_kernel = 1;
288126d68fdSAdrian Hunter evsel->core.attr.exclude_guest = 1;
289126d68fdSAdrian Hunter evsel->core.attr.exclude_hv = 1;
29060ea006fSNamhyung Kim evsel->core.system_wide = system_wide;
291126d68fdSAdrian Hunter evsel->no_aux_samples = true;
292126d68fdSAdrian Hunter evsel->name = strdup("dummy:u");
293126d68fdSAdrian Hunter
294126d68fdSAdrian Hunter evlist__add(evlist, evsel);
295126d68fdSAdrian Hunter return evsel;
296126d68fdSAdrian Hunter }
297126d68fdSAdrian Hunter
298378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
evlist__add_sched_switch(struct evlist * evlist,bool system_wide)299182bb594SNamhyung Kim struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
300182bb594SNamhyung Kim {
301182bb594SNamhyung Kim struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
302182bb594SNamhyung Kim
303182bb594SNamhyung Kim if (IS_ERR(evsel))
304182bb594SNamhyung Kim return evsel;
305182bb594SNamhyung Kim
306182bb594SNamhyung Kim evsel__set_sample_bit(evsel, CPU);
307182bb594SNamhyung Kim evsel__set_sample_bit(evsel, TIME);
308182bb594SNamhyung Kim
309182bb594SNamhyung Kim evsel->core.system_wide = system_wide;
310182bb594SNamhyung Kim evsel->no_aux_samples = true;
311182bb594SNamhyung Kim
312182bb594SNamhyung Kim evlist__add(evlist, evsel);
313182bb594SNamhyung Kim return evsel;
314378ef0f5SIan Rogers }
315378ef0f5SIan Rogers #endif
316182bb594SNamhyung Kim
evlist__add_attrs(struct evlist * evlist,struct perf_event_attr * attrs,size_t nr_attrs)317cdb204adSKan Liang int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
31850d08e47SArnaldo Carvalho de Melo {
31932dcd021SJiri Olsa struct evsel *evsel, *n;
32050d08e47SArnaldo Carvalho de Melo LIST_HEAD(head);
32150d08e47SArnaldo Carvalho de Melo size_t i;
32250d08e47SArnaldo Carvalho de Melo
32350d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) {
3248f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
32550d08e47SArnaldo Carvalho de Melo if (evsel == NULL)
32650d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list;
327b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head);
32850d08e47SArnaldo Carvalho de Melo }
32950d08e47SArnaldo Carvalho de Melo
330e414fd1aSArnaldo Carvalho de Melo evlist__splice_list_tail(evlist, &head);
33150d08e47SArnaldo Carvalho de Melo
33250d08e47SArnaldo Carvalho de Melo return 0;
33350d08e47SArnaldo Carvalho de Melo
33450d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
335e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel)
3365eb2dd2aSJiri Olsa evsel__delete(evsel);
33750d08e47SArnaldo Carvalho de Melo return -1;
33850d08e47SArnaldo Carvalho de Melo }
33950d08e47SArnaldo Carvalho de Melo
__evlist__add_default_attrs(struct evlist * evlist,struct perf_event_attr * attrs,size_t nr_attrs)340e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
34179695e1bSArnaldo Carvalho de Melo {
34279695e1bSArnaldo Carvalho de Melo size_t i;
34379695e1bSArnaldo Carvalho de Melo
34479695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++)
34579695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i);
34679695e1bSArnaldo Carvalho de Melo
347a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs);
34879695e1bSArnaldo Carvalho de Melo }
34979695e1bSArnaldo Carvalho de Melo
arch_evlist__add_default_attrs(struct evlist * evlist,struct perf_event_attr * attrs,size_t nr_attrs)350a9c1ecdaSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist,
351a9c1ecdaSKan Liang struct perf_event_attr *attrs,
352a9c1ecdaSKan Liang size_t nr_attrs)
35342641d6fSKan Liang {
354a9c1ecdaSKan Liang if (!nr_attrs)
35542641d6fSKan Liang return 0;
356a9c1ecdaSKan Liang
357a9c1ecdaSKan Liang return __evlist__add_default_attrs(evlist, attrs, nr_attrs);
35842641d6fSKan Liang }
35942641d6fSKan Liang
evlist__find_tracepoint_by_id(struct evlist * evlist,int id)360b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
361ee29be62SArnaldo Carvalho de Melo {
36232dcd021SJiri Olsa struct evsel *evsel;
363ee29be62SArnaldo Carvalho de Melo
364e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
3651fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
3661fc632ceSJiri Olsa (int)evsel->core.attr.config == id)
367ee29be62SArnaldo Carvalho de Melo return evsel;
368ee29be62SArnaldo Carvalho de Melo }
369ee29be62SArnaldo Carvalho de Melo
370ee29be62SArnaldo Carvalho de Melo return NULL;
371ee29be62SArnaldo Carvalho de Melo }
372ee29be62SArnaldo Carvalho de Melo
evlist__find_tracepoint_by_name(struct evlist * evlist,const char * name)373b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
374a2f2804aSDavid Ahern {
37532dcd021SJiri Olsa struct evsel *evsel;
376a2f2804aSDavid Ahern
377e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
3781fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
379a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0))
380a2f2804aSDavid Ahern return evsel;
381a2f2804aSDavid Ahern }
382a2f2804aSDavid Ahern
383a2f2804aSDavid Ahern return NULL;
384a2f2804aSDavid Ahern }
385a2f2804aSDavid Ahern
386378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
evlist__add_newtp(struct evlist * evlist,const char * sys,const char * name,void * handler)387e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
38839876e7dSArnaldo Carvalho de Melo {
3898f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp(sys, name);
39039876e7dSArnaldo Carvalho de Melo
3918dd2a131SJiri Olsa if (IS_ERR(evsel))
39239876e7dSArnaldo Carvalho de Melo return -1;
39339876e7dSArnaldo Carvalho de Melo
394744a9719SArnaldo Carvalho de Melo evsel->handler = handler;
395a1cf3a75SJiri Olsa evlist__add(evlist, evsel);
39639876e7dSArnaldo Carvalho de Melo return 0;
39739876e7dSArnaldo Carvalho de Melo }
398378ef0f5SIan Rogers #endif
39939876e7dSArnaldo Carvalho de Melo
evlist__cpu_begin(struct evlist * evlist,struct affinity * affinity)400472832d2SIan Rogers struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
401a8cbe40fSAndi Kleen {
402472832d2SIan Rogers struct evlist_cpu_iterator itr = {
403472832d2SIan Rogers .container = evlist,
4048b464eacSIan Rogers .evsel = NULL,
405472832d2SIan Rogers .cpu_map_idx = 0,
406472832d2SIan Rogers .evlist_cpu_map_idx = 0,
407472832d2SIan Rogers .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
4086d18804bSIan Rogers .cpu = (struct perf_cpu){ .cpu = -1},
409472832d2SIan Rogers .affinity = affinity,
410472832d2SIan Rogers };
411a8cbe40fSAndi Kleen
4128b464eacSIan Rogers if (evlist__empty(evlist)) {
4138b464eacSIan Rogers /* Ensure the empty list doesn't iterate. */
4148b464eacSIan Rogers itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
4158b464eacSIan Rogers } else {
4168b464eacSIan Rogers itr.evsel = evlist__first(evlist);
417472832d2SIan Rogers if (itr.affinity) {
418472832d2SIan Rogers itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
4196d18804bSIan Rogers affinity__set(itr.affinity, itr.cpu.cpu);
420472832d2SIan Rogers itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
421a8cbe40fSAndi Kleen /*
4228b464eacSIan Rogers * If this CPU isn't in the evsel's cpu map then advance
4238b464eacSIan Rogers * through the list.
424a8cbe40fSAndi Kleen */
425472832d2SIan Rogers if (itr.cpu_map_idx == -1)
426472832d2SIan Rogers evlist_cpu_iterator__next(&itr);
427472832d2SIan Rogers }
4288b464eacSIan Rogers }
429472832d2SIan Rogers return itr;
430a8cbe40fSAndi Kleen }
431a8cbe40fSAndi Kleen
evlist_cpu_iterator__next(struct evlist_cpu_iterator * evlist_cpu_itr)432472832d2SIan Rogers void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
433a8cbe40fSAndi Kleen {
434472832d2SIan Rogers while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
435472832d2SIan Rogers evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
436472832d2SIan Rogers evlist_cpu_itr->cpu_map_idx =
437472832d2SIan Rogers perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
438472832d2SIan Rogers evlist_cpu_itr->cpu);
439472832d2SIan Rogers if (evlist_cpu_itr->cpu_map_idx != -1)
440472832d2SIan Rogers return;
441472832d2SIan Rogers }
442472832d2SIan Rogers evlist_cpu_itr->evlist_cpu_map_idx++;
443472832d2SIan Rogers if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
444472832d2SIan Rogers evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
445472832d2SIan Rogers evlist_cpu_itr->cpu =
446472832d2SIan Rogers perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
447472832d2SIan Rogers evlist_cpu_itr->evlist_cpu_map_idx);
448472832d2SIan Rogers if (evlist_cpu_itr->affinity)
4496d18804bSIan Rogers affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
450472832d2SIan Rogers evlist_cpu_itr->cpu_map_idx =
451472832d2SIan Rogers perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
452472832d2SIan Rogers evlist_cpu_itr->cpu);
453472832d2SIan Rogers /*
454472832d2SIan Rogers * If this CPU isn't in the evsel's cpu map then advance through
455472832d2SIan Rogers * the list.
456472832d2SIan Rogers */
457472832d2SIan Rogers if (evlist_cpu_itr->cpu_map_idx == -1)
458472832d2SIan Rogers evlist_cpu_iterator__next(evlist_cpu_itr);
459472832d2SIan Rogers }
460a8cbe40fSAndi Kleen }
461a8cbe40fSAndi Kleen
evlist_cpu_iterator__end(const struct evlist_cpu_iterator * evlist_cpu_itr)462472832d2SIan Rogers bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
463a8cbe40fSAndi Kleen {
464472832d2SIan Rogers return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
465a8cbe40fSAndi Kleen }
466a8cbe40fSAndi Kleen
evsel__strcmp(struct evsel * pos,char * evsel_name)4677cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
4687cfcd1e0SJiri Olsa {
4697cfcd1e0SJiri Olsa if (!evsel_name)
4707cfcd1e0SJiri Olsa return 0;
4717cfcd1e0SJiri Olsa if (evsel__is_dummy_event(pos))
4727cfcd1e0SJiri Olsa return 1;
473ce1d3bc2SArnaldo Carvalho de Melo return !evsel__name_is(pos, evsel_name);
4747cfcd1e0SJiri Olsa }
4757cfcd1e0SJiri Olsa
evlist__is_enabled(struct evlist * evlist)4767cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
4777cfcd1e0SJiri Olsa {
4787cfcd1e0SJiri Olsa struct evsel *pos;
4797cfcd1e0SJiri Olsa
4807cfcd1e0SJiri Olsa evlist__for_each_entry(evlist, pos) {
4817cfcd1e0SJiri Olsa if (!evsel__is_group_leader(pos) || !pos->core.fd)
4827cfcd1e0SJiri Olsa continue;
4837cfcd1e0SJiri Olsa /* If at least one event is enabled, evlist is enabled. */
4847cfcd1e0SJiri Olsa if (!pos->disabled)
4857cfcd1e0SJiri Olsa return true;
4867cfcd1e0SJiri Olsa }
4877cfcd1e0SJiri Olsa return false;
4887cfcd1e0SJiri Olsa }
4897cfcd1e0SJiri Olsa
__evlist__disable(struct evlist * evlist,char * evsel_name,bool excl_dummy)490329725d5SAdrian Hunter static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
4914152ab37SArnaldo Carvalho de Melo {
49232dcd021SJiri Olsa struct evsel *pos;
493472832d2SIan Rogers struct evlist_cpu_iterator evlist_cpu_itr;
4940d3d2376SArnaldo Carvalho de Melo struct affinity saved_affinity, *affinity = NULL;
49587cf8360SAdrian Hunter bool has_imm = false;
496704e2f5bSAndi Kleen
4970d3d2376SArnaldo Carvalho de Melo // See explanation in evlist__close()
4980df6ade7SIan Rogers if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
4990d3d2376SArnaldo Carvalho de Melo if (affinity__setup(&saved_affinity) < 0)
500704e2f5bSAndi Kleen return;
5010d3d2376SArnaldo Carvalho de Melo affinity = &saved_affinity;
5020d3d2376SArnaldo Carvalho de Melo }
503704e2f5bSAndi Kleen
50487cf8360SAdrian Hunter /* Disable 'immediate' events last */
505472832d2SIan Rogers for (int imm = 0; imm <= 1; imm++) {
5060d3d2376SArnaldo Carvalho de Melo evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
507472832d2SIan Rogers pos = evlist_cpu_itr.evsel;
5087cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name))
5097cfcd1e0SJiri Olsa continue;
510c754c382SArnaldo Carvalho de Melo if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
5113fe4430dSJiri Olsa continue;
512329725d5SAdrian Hunter if (excl_dummy && evsel__is_dummy_event(pos))
513329725d5SAdrian Hunter continue;
51487cf8360SAdrian Hunter if (pos->immediate)
51587cf8360SAdrian Hunter has_imm = true;
51687cf8360SAdrian Hunter if (pos->immediate != imm)
51787cf8360SAdrian Hunter continue;
518472832d2SIan Rogers evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
519704e2f5bSAndi Kleen }
52087cf8360SAdrian Hunter if (!has_imm)
52187cf8360SAdrian Hunter break;
52287cf8360SAdrian Hunter }
52387cf8360SAdrian Hunter
5240d3d2376SArnaldo Carvalho de Melo affinity__cleanup(affinity);
525704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) {
5267cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name))
5277cfcd1e0SJiri Olsa continue;
528c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd)
529704e2f5bSAndi Kleen continue;
530329725d5SAdrian Hunter if (excl_dummy && evsel__is_dummy_event(pos))
531329725d5SAdrian Hunter continue;
532704e2f5bSAndi Kleen pos->disabled = true;
5334152ab37SArnaldo Carvalho de Melo }
5342b56bcfbSArnaldo Carvalho de Melo
5357cfcd1e0SJiri Olsa /*
5367cfcd1e0SJiri Olsa * If we disabled only single event, we need to check
5377cfcd1e0SJiri Olsa * the enabled state of the evlist manually.
5387cfcd1e0SJiri Olsa */
5397cfcd1e0SJiri Olsa if (evsel_name)
5407cfcd1e0SJiri Olsa evlist->enabled = evlist__is_enabled(evlist);
5417cfcd1e0SJiri Olsa else
5422b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false;
5434152ab37SArnaldo Carvalho de Melo }
5444152ab37SArnaldo Carvalho de Melo
evlist__disable(struct evlist * evlist)5457cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
5467cfcd1e0SJiri Olsa {
547329725d5SAdrian Hunter __evlist__disable(evlist, NULL, false);
548329725d5SAdrian Hunter }
549329725d5SAdrian Hunter
evlist__disable_non_dummy(struct evlist * evlist)550329725d5SAdrian Hunter void evlist__disable_non_dummy(struct evlist *evlist)
551329725d5SAdrian Hunter {
552329725d5SAdrian Hunter __evlist__disable(evlist, NULL, true);
5537cfcd1e0SJiri Olsa }
5547cfcd1e0SJiri Olsa
evlist__disable_evsel(struct evlist * evlist,char * evsel_name)5557cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
5567cfcd1e0SJiri Olsa {
557329725d5SAdrian Hunter __evlist__disable(evlist, evsel_name, false);
5587cfcd1e0SJiri Olsa }
5597cfcd1e0SJiri Olsa
__evlist__enable(struct evlist * evlist,char * evsel_name,bool excl_dummy)560329725d5SAdrian Hunter static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
561764e16a3SDavid Ahern {
56232dcd021SJiri Olsa struct evsel *pos;
563472832d2SIan Rogers struct evlist_cpu_iterator evlist_cpu_itr;
564f350ee95SArnaldo Carvalho de Melo struct affinity saved_affinity, *affinity = NULL;
565764e16a3SDavid Ahern
566f350ee95SArnaldo Carvalho de Melo // See explanation in evlist__close()
5670df6ade7SIan Rogers if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
568f350ee95SArnaldo Carvalho de Melo if (affinity__setup(&saved_affinity) < 0)
569704e2f5bSAndi Kleen return;
570f350ee95SArnaldo Carvalho de Melo affinity = &saved_affinity;
571f350ee95SArnaldo Carvalho de Melo }
572704e2f5bSAndi Kleen
573f350ee95SArnaldo Carvalho de Melo evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
574472832d2SIan Rogers pos = evlist_cpu_itr.evsel;
5757cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name))
5767cfcd1e0SJiri Olsa continue;
577c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd)
578704e2f5bSAndi Kleen continue;
579329725d5SAdrian Hunter if (excl_dummy && evsel__is_dummy_event(pos))
580329725d5SAdrian Hunter continue;
581472832d2SIan Rogers evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
582704e2f5bSAndi Kleen }
583f350ee95SArnaldo Carvalho de Melo affinity__cleanup(affinity);
584e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) {
5857cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name))
5867cfcd1e0SJiri Olsa continue;
587c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd)
5883fe4430dSJiri Olsa continue;
589329725d5SAdrian Hunter if (excl_dummy && evsel__is_dummy_event(pos))
590329725d5SAdrian Hunter continue;
591704e2f5bSAndi Kleen pos->disabled = false;
592764e16a3SDavid Ahern }
5932b56bcfbSArnaldo Carvalho de Melo
5947cfcd1e0SJiri Olsa /*
5957cfcd1e0SJiri Olsa * Even single event sets the 'enabled' for evlist,
5967cfcd1e0SJiri Olsa * so the toggle can work properly and toggle to
5977cfcd1e0SJiri Olsa * 'disabled' state.
5987cfcd1e0SJiri Olsa */
5992b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true;
6002b56bcfbSArnaldo Carvalho de Melo }
6012b56bcfbSArnaldo Carvalho de Melo
evlist__enable(struct evlist * evlist)6027cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
6037cfcd1e0SJiri Olsa {
604329725d5SAdrian Hunter __evlist__enable(evlist, NULL, false);
605329725d5SAdrian Hunter }
606329725d5SAdrian Hunter
evlist__enable_non_dummy(struct evlist * evlist)607329725d5SAdrian Hunter void evlist__enable_non_dummy(struct evlist *evlist)
608329725d5SAdrian Hunter {
609329725d5SAdrian Hunter __evlist__enable(evlist, NULL, true);
6107cfcd1e0SJiri Olsa }
6117cfcd1e0SJiri Olsa
evlist__enable_evsel(struct evlist * evlist,char * evsel_name)6127cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
6137cfcd1e0SJiri Olsa {
614329725d5SAdrian Hunter __evlist__enable(evlist, evsel_name, false);
6157cfcd1e0SJiri Olsa }
6167cfcd1e0SJiri Olsa
evlist__toggle_enable(struct evlist * evlist)617ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
6182b56bcfbSArnaldo Carvalho de Melo {
619e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
620764e16a3SDavid Ahern }
621764e16a3SDavid Ahern
evlist__add_pollfd(struct evlist * evlist,int fd)622f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
623e4b356b5SArnaldo Carvalho de Melo {
624ab4c1f9fSAlexey Budankov return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
625e4b356b5SArnaldo Carvalho de Melo }
6261b85337dSArnaldo Carvalho de Melo
evlist__filter_pollfd(struct evlist * evlist,short revents_and_mask)627f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
6281ddec7f0SArnaldo Carvalho de Melo {
62984227cb1SJiri Olsa return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
6301ddec7f0SArnaldo Carvalho de Melo }
6311ddec7f0SArnaldo Carvalho de Melo
632e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
evlist__add_wakeup_eventfd(struct evlist * evlist,int fd)633e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
634e16c2ce7SYang Jihong {
635e16c2ce7SYang Jihong return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
636a032ad87SAdrian Hunter fdarray_flag__nonfilterable |
637a032ad87SAdrian Hunter fdarray_flag__non_perf_event);
638e16c2ce7SYang Jihong }
639e16c2ce7SYang Jihong #endif
640e16c2ce7SYang Jihong
evlist__poll(struct evlist * evlist,int timeout)64180ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
642f66a889dSArnaldo Carvalho de Melo {
64380ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout);
644f66a889dSArnaldo Carvalho de Melo }
645f66a889dSArnaldo Carvalho de Melo
evlist__id2sid(struct evlist * evlist,u64 id)6463ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
64770db7533SArnaldo Carvalho de Melo {
64870db7533SArnaldo Carvalho de Melo struct hlist_head *head;
64970db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid;
65070db7533SArnaldo Carvalho de Melo int hash;
65170db7533SArnaldo Carvalho de Melo
65270db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6531d5af02dSJiri Olsa head = &evlist->core.heads[hash];
65470db7533SArnaldo Carvalho de Melo
655b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node)
65670db7533SArnaldo Carvalho de Melo if (sid->id == id)
657932a3594SJiri Olsa return sid;
658932a3594SJiri Olsa
659932a3594SJiri Olsa return NULL;
660932a3594SJiri Olsa }
661932a3594SJiri Olsa
evlist__id2evsel(struct evlist * evlist,u64 id)6623ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
663932a3594SJiri Olsa {
664932a3594SJiri Olsa struct perf_sample_id *sid;
665932a3594SJiri Olsa
6666484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id)
667515dbe48SJiri Olsa return evlist__first(evlist);
668932a3594SJiri Olsa
6693ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id);
670932a3594SJiri Olsa if (sid)
67170c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core);
67230e68bccSNamhyung Kim
6738cedf3a5SArnaldo Carvalho de Melo if (!evlist__sample_id_all(evlist))
674515dbe48SJiri Olsa return evlist__first(evlist);
67530e68bccSNamhyung Kim
67670db7533SArnaldo Carvalho de Melo return NULL;
67770db7533SArnaldo Carvalho de Melo }
67804391debSArnaldo Carvalho de Melo
evlist__id2evsel_strict(struct evlist * evlist,u64 id)6793ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
680dddcf6abSAdrian Hunter {
681dddcf6abSAdrian Hunter struct perf_sample_id *sid;
682dddcf6abSAdrian Hunter
683dddcf6abSAdrian Hunter if (!id)
684dddcf6abSAdrian Hunter return NULL;
685dddcf6abSAdrian Hunter
6863ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id);
687dddcf6abSAdrian Hunter if (sid)
68870c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core);
689dddcf6abSAdrian Hunter
690dddcf6abSAdrian Hunter return NULL;
691dddcf6abSAdrian Hunter }
692dddcf6abSAdrian Hunter
evlist__event2id(struct evlist * evlist,union perf_event * event,u64 * id)6933ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
69475562573SAdrian Hunter {
695b1fcd190SJiri Olsa const __u64 *array = event->sample.array;
69675562573SAdrian Hunter ssize_t n;
69775562573SAdrian Hunter
69875562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3;
69975562573SAdrian Hunter
70075562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) {
70175562573SAdrian Hunter if (evlist->id_pos >= n)
70275562573SAdrian Hunter return -1;
70375562573SAdrian Hunter *id = array[evlist->id_pos];
70475562573SAdrian Hunter } else {
70575562573SAdrian Hunter if (evlist->is_pos > n)
70675562573SAdrian Hunter return -1;
70775562573SAdrian Hunter n -= evlist->is_pos;
70875562573SAdrian Hunter *id = array[n];
70975562573SAdrian Hunter }
71075562573SAdrian Hunter return 0;
71175562573SAdrian Hunter }
71275562573SAdrian Hunter
evlist__event2evsel(struct evlist * evlist,union perf_event * event)7133ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
71475562573SAdrian Hunter {
715515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist);
71675562573SAdrian Hunter struct hlist_head *head;
71775562573SAdrian Hunter struct perf_sample_id *sid;
71875562573SAdrian Hunter int hash;
71975562573SAdrian Hunter u64 id;
72075562573SAdrian Hunter
7216484d2f9SJiri Olsa if (evlist->core.nr_entries == 1)
72298be6966SAdrian Hunter return first;
72398be6966SAdrian Hunter
7241fc632ceSJiri Olsa if (!first->core.attr.sample_id_all &&
72598be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE)
72698be6966SAdrian Hunter return first;
72775562573SAdrian Hunter
7283ccf8a7bSArnaldo Carvalho de Melo if (evlist__event2id(evlist, event, &id))
72975562573SAdrian Hunter return NULL;
73075562573SAdrian Hunter
73175562573SAdrian Hunter /* Synthesized events have an id of zero */
73275562573SAdrian Hunter if (!id)
73398be6966SAdrian Hunter return first;
73475562573SAdrian Hunter
73575562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
7361d5af02dSJiri Olsa head = &evlist->core.heads[hash];
73775562573SAdrian Hunter
73875562573SAdrian Hunter hlist_for_each_entry(sid, head, node) {
73975562573SAdrian Hunter if (sid->id == id)
74070c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core);
74175562573SAdrian Hunter }
74275562573SAdrian Hunter return NULL;
74375562573SAdrian Hunter }
74475562573SAdrian Hunter
evlist__set_paused(struct evlist * evlist,bool value)7450a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
74665aea233SWang Nan {
74765aea233SWang Nan int i;
74865aea233SWang Nan
7490b72d69aSWang Nan if (!evlist->overwrite_mmap)
750078c3386SWang Nan return 0;
751078c3386SWang Nan
752c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) {
7532cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd;
75465aea233SWang Nan int err;
75565aea233SWang Nan
75665aea233SWang Nan if (fd < 0)
75765aea233SWang Nan continue;
75865aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
75965aea233SWang Nan if (err)
76065aea233SWang Nan return err;
76165aea233SWang Nan }
76265aea233SWang Nan return 0;
76365aea233SWang Nan }
76465aea233SWang Nan
evlist__pause(struct evlist * evlist)7650a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
76665aea233SWang Nan {
7670a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, true);
76865aea233SWang Nan }
76965aea233SWang Nan
evlist__resume(struct evlist * evlist)7700a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
77165aea233SWang Nan {
7720a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, false);
77365aea233SWang Nan }
77465aea233SWang Nan
evlist__munmap_nofree(struct evlist * evlist)775db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
776f8a95309SArnaldo Carvalho de Melo {
777aece948fSArnaldo Carvalho de Melo int i;
778f8a95309SArnaldo Carvalho de Melo
779b2cb615dSWang Nan if (evlist->mmap)
780c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++)
78180e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core);
782b2cb615dSWang Nan
7830b72d69aSWang Nan if (evlist->overwrite_mmap)
784c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++)
78580e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
786a1f72618SWang Nan }
787aece948fSArnaldo Carvalho de Melo
evlist__munmap(struct evlist * evlist)788db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
789a1f72618SWang Nan {
790db6b7b13SJiri Olsa evlist__munmap_nofree(evlist);
79104662523SArnaldo Carvalho de Melo zfree(&evlist->mmap);
7920b72d69aSWang Nan zfree(&evlist->overwrite_mmap);
793f8a95309SArnaldo Carvalho de Melo }
794f8a95309SArnaldo Carvalho de Melo
perf_mmap__unmap_cb(struct perf_mmap * map)79580e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
79680e53d11SJiri Olsa {
79780e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core);
79880e53d11SJiri Olsa
79980e53d11SJiri Olsa mmap__munmap(m);
80080e53d11SJiri Olsa }
80180e53d11SJiri Olsa
evlist__alloc_mmap(struct evlist * evlist,bool overwrite)802d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
8032c5f6d87SKan Liang bool overwrite)
804f8a95309SArnaldo Carvalho de Melo {
805d4c6fb36SWang Nan int i;
806a5830532SJiri Olsa struct mmap *map;
807d4c6fb36SWang Nan
808c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
8098db6d6b1SWang Nan if (!map)
8108db6d6b1SWang Nan return NULL;
811946ae1d4SWang Nan
812c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) {
8136eb65f7aSJiri Olsa struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
8146eb65f7aSJiri Olsa
8154738ca30SArnaldo Carvalho de Melo /*
8164738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus
8176afad54dSKan Liang * one extra to let perf_mmap__consume() get the last
8184738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are
8194738ca30SArnaldo Carvalho de Melo * dropped.
8204738ca30SArnaldo Carvalho de Melo *
8214738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
8224738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it.
8234738ca30SArnaldo Carvalho de Melo */
8246eb65f7aSJiri Olsa perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
8254738ca30SArnaldo Carvalho de Melo }
826353120b4SJiri Olsa
8278db6d6b1SWang Nan return map;
828f8a95309SArnaldo Carvalho de Melo }
829f8a95309SArnaldo Carvalho de Melo
8309abd2ab2SJiri Olsa static void
perf_evlist__mmap_cb_idx(struct perf_evlist * _evlist,struct perf_evsel * _evsel,struct perf_mmap_param * _mp,int idx)8319abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
832d01508f2SAdrian Hunter struct perf_evsel *_evsel,
8339abd2ab2SJiri Olsa struct perf_mmap_param *_mp,
8346a7b8a5aSAdrian Hunter int idx)
8359abd2ab2SJiri Olsa {
8369abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core);
8379abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
838d01508f2SAdrian Hunter struct evsel *evsel = container_of(_evsel, struct evsel, core);
8399abd2ab2SJiri Olsa
84084bd5abaSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
8419abd2ab2SJiri Olsa }
8429abd2ab2SJiri Olsa
843bb1b1885SJiri Olsa static struct perf_mmap*
perf_evlist__mmap_cb_get(struct perf_evlist * _evlist,bool overwrite,int idx)844bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
845bb1b1885SJiri Olsa {
846bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core);
8473805e4f3SJiri Olsa struct mmap *maps;
848bb1b1885SJiri Olsa
8493805e4f3SJiri Olsa maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
850bb1b1885SJiri Olsa
851bb1b1885SJiri Olsa if (!maps) {
8523805e4f3SJiri Olsa maps = evlist__alloc_mmap(evlist, overwrite);
853bb1b1885SJiri Olsa if (!maps)
854bb1b1885SJiri Olsa return NULL;
855bb1b1885SJiri Olsa
8563805e4f3SJiri Olsa if (overwrite) {
857bb1b1885SJiri Olsa evlist->overwrite_mmap = maps;
858bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
859ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
8603805e4f3SJiri Olsa } else {
8613805e4f3SJiri Olsa evlist->mmap = maps;
862bb1b1885SJiri Olsa }
863bb1b1885SJiri Olsa }
864bb1b1885SJiri Olsa
865bb1b1885SJiri Olsa return &maps[idx].core;
866bb1b1885SJiri Olsa }
867bb1b1885SJiri Olsa
868b80132b1SJiri Olsa static int
perf_evlist__mmap_cb_mmap(struct perf_mmap * _map,struct perf_mmap_param * _mp,int output,struct perf_cpu cpu)869b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
8706d18804bSIan Rogers int output, struct perf_cpu cpu)
871b80132b1SJiri Olsa {
872b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core);
873b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
874b80132b1SJiri Olsa
875b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu);
876b80132b1SJiri Olsa }
877b80132b1SJiri Olsa
perf_event_mlock_kb_in_pages(void)878f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
879994a1f78SJiri Olsa {
880f5e7150cSArnaldo Carvalho de Melo unsigned long pages;
8818185e881SArnaldo Carvalho de Melo int max;
8828185e881SArnaldo Carvalho de Melo
8838185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8848185e881SArnaldo Carvalho de Melo /*
8858185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look
8868185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not
8878185e881SArnaldo Carvalho de Melo * die yet...
8888185e881SArnaldo Carvalho de Melo */
8898185e881SArnaldo Carvalho de Melo max = 512;
8908185e881SArnaldo Carvalho de Melo } else {
8918185e881SArnaldo Carvalho de Melo max -= (page_size / 1024);
8928185e881SArnaldo Carvalho de Melo }
8938185e881SArnaldo Carvalho de Melo
8948185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size;
8951be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages))
8961be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages);
897f5e7150cSArnaldo Carvalho de Melo
898f5e7150cSArnaldo Carvalho de Melo return pages;
899f5e7150cSArnaldo Carvalho de Melo }
900f5e7150cSArnaldo Carvalho de Melo
evlist__mmap_size(unsigned long pages)9019521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
902f5e7150cSArnaldo Carvalho de Melo {
903f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX)
904f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages();
905f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages))
906994a1f78SJiri Olsa return 0;
907994a1f78SJiri Olsa
908994a1f78SJiri Olsa return (pages + 1) * page_size;
909994a1f78SJiri Olsa }
910994a1f78SJiri Olsa
parse_pages_arg(const char * str,unsigned long min,unsigned long max)91133c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
91233c2dcfdSDavid Ahern unsigned long max)
913994a1f78SJiri Olsa {
9142fbe4abeSAdrian Hunter unsigned long pages, val;
91527050f53SJiri Olsa static struct parse_tag tags[] = {
91627050f53SJiri Olsa { .tag = 'B', .mult = 1 },
91727050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 },
91827050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 },
91927050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 },
92027050f53SJiri Olsa { .tag = 0 },
92127050f53SJiri Olsa };
922994a1f78SJiri Olsa
9238973504bSDavid Ahern if (str == NULL)
92433c2dcfdSDavid Ahern return -EINVAL;
9258973504bSDavid Ahern
92627050f53SJiri Olsa val = parse_tag_value(str, tags);
9272fbe4abeSAdrian Hunter if (val != (unsigned long) -1) {
92827050f53SJiri Olsa /* we got file size value */
92927050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size;
93027050f53SJiri Olsa } else {
93127050f53SJiri Olsa /* we got pages count value */
93227050f53SJiri Olsa char *eptr;
933994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10);
93433c2dcfdSDavid Ahern if (*eptr != '\0')
93533c2dcfdSDavid Ahern return -EINVAL;
93627050f53SJiri Olsa }
937994a1f78SJiri Olsa
9382bcab6c1SAdrian Hunter if (pages == 0 && min == 0) {
93933c2dcfdSDavid Ahern /* leave number of pages at 0 */
9401dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) {
9419808143bSJiri Olsa char buf[100];
9429808143bSJiri Olsa
94333c2dcfdSDavid Ahern /* round pages up to next power of 2 */
94491529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages);
9451dbfa938SAdrian Hunter if (!pages)
9461dbfa938SAdrian Hunter return -EINVAL;
9479808143bSJiri Olsa
9489808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
9499808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n",
9509808143bSJiri Olsa buf, pages);
9512fbe4abeSAdrian Hunter }
9522fbe4abeSAdrian Hunter
95333c2dcfdSDavid Ahern if (pages > max)
95433c2dcfdSDavid Ahern return -EINVAL;
95533c2dcfdSDavid Ahern
95633c2dcfdSDavid Ahern return pages;
95733c2dcfdSDavid Ahern }
95833c2dcfdSDavid Ahern
__evlist__parse_mmap_pages(unsigned int * mmap_pages,const char * str)95925f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
96033c2dcfdSDavid Ahern {
96133c2dcfdSDavid Ahern unsigned long max = UINT_MAX;
96233c2dcfdSDavid Ahern long pages;
96333c2dcfdSDavid Ahern
964f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size)
96533c2dcfdSDavid Ahern max = SIZE_MAX / page_size;
96633c2dcfdSDavid Ahern
96733c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max);
96833c2dcfdSDavid Ahern if (pages < 0) {
96933c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n");
970994a1f78SJiri Olsa return -1;
971994a1f78SJiri Olsa }
972994a1f78SJiri Olsa
973994a1f78SJiri Olsa *mmap_pages = pages;
974994a1f78SJiri Olsa return 0;
975994a1f78SJiri Olsa }
976994a1f78SJiri Olsa
evlist__parse_mmap_pages(const struct option * opt,const char * str,int unset __maybe_unused)97725f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
978e9db1310SAdrian Hunter {
97925f84702SArnaldo Carvalho de Melo return __evlist__parse_mmap_pages(opt->value, str);
980e9db1310SAdrian Hunter }
981e9db1310SAdrian Hunter
982c83fa7f2SAdrian Hunter /**
9839521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events.
984c83fa7f2SAdrian Hunter * @evlist: list of events
985c83fa7f2SAdrian Hunter * @pages: map length in pages
986c83fa7f2SAdrian Hunter * @overwrite: overwrite older events?
987718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages
988718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data?
989f8a95309SArnaldo Carvalho de Melo *
990c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using
9919521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this
992c83fa7f2SAdrian Hunter * automatically.
993f8a95309SArnaldo Carvalho de Melo *
994718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
995718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail().
996718c602dSAdrian Hunter *
997c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise.
998f8a95309SArnaldo Carvalho de Melo */
evlist__mmap_ex(struct evlist * evlist,unsigned int pages,unsigned int auxtrace_pages,bool auxtrace_overwrite,int nr_cblocks,int affinity,int flush,int comp_level)9999521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
10007a276ff6SWang Nan unsigned int auxtrace_pages,
100151255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
100251255a8aSAlexey Budankov int comp_level)
1003f8a95309SArnaldo Carvalho de Melo {
100471f566a3SWang Nan /*
100571f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap.
100671f566a3SWang Nan * Its value is decided by evsel's write_backward.
100771f566a3SWang Nan * So &mp should not be passed through const pointer.
100871f566a3SWang Nan */
1009e440979fSJiri Olsa struct mmap_params mp = {
1010e440979fSJiri Olsa .nr_cblocks = nr_cblocks,
1011e440979fSJiri Olsa .affinity = affinity,
1012e440979fSJiri Olsa .flush = flush,
1013e440979fSJiri Olsa .comp_level = comp_level
1014e440979fSJiri Olsa };
1015923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = {
10169abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx,
1017bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get,
1018b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap,
10199abd2ab2SJiri Olsa };
102050a682ceSArnaldo Carvalho de Melo
1021f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages);
1022f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
1023f8a95309SArnaldo Carvalho de Melo
1024f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
1025718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite);
1026718c602dSAdrian Hunter
1027923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
1028f8a95309SArnaldo Carvalho de Melo }
10297e2ed097SArnaldo Carvalho de Melo
evlist__mmap(struct evlist * evlist,unsigned int pages)10309521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
1031718c602dSAdrian Hunter {
10329521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1033718c602dSAdrian Hunter }
1034718c602dSAdrian Hunter
evlist__create_maps(struct evlist * evlist,struct target * target)10357748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
10367e2ed097SArnaldo Carvalho de Melo {
1037147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide);
1038f854839bSJiri Olsa struct perf_cpu_map *cpus;
10399749b90eSJiri Olsa struct perf_thread_map *threads;
10407e2ed097SArnaldo Carvalho de Melo
1041147c508fSJin Yao /*
1042147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record
1043147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and
1044147c508fSJin Yao * target->system_wide = true.
1045147c508fSJin Yao *
1046147c508fSJin Yao * If specify '--per-thread' only to perf record,
1047147c508fSJin Yao * target->per_thread = true and target->system_wide = false.
1048147c508fSJin Yao *
1049147c508fSJin Yao * So target->per_thread && target->system_wide is false.
1050147c508fSJin Yao * For perf record, thread_map__new_str doesn't call
1051147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's
1052147c508fSJin Yao * current behavior.
1053147c508fSJin Yao *
1054147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and
1055147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide
1056147c508fSJin Yao * per-thread data. thread_map__new_str will call
1057147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads.
1058147c508fSJin Yao */
105973c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid,
1060147c508fSJin Yao all_threads);
106174bfd2b2SAdrian Hunter
106274bfd2b2SAdrian Hunter if (!threads)
10637e2ed097SArnaldo Carvalho de Melo return -1;
10647e2ed097SArnaldo Carvalho de Melo
10659c105fbcSDongsheng Yang if (target__uses_dummy_map(target))
1066397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new();
1067879d77d0SNamhyung Kim else
10689c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list);
10697e2ed097SArnaldo Carvalho de Melo
107074bfd2b2SAdrian Hunter if (!cpus)
10717e2ed097SArnaldo Carvalho de Melo goto out_delete_threads;
10727e2ed097SArnaldo Carvalho de Melo
107342249160SIan Rogers evlist->core.has_user_cpus = !!target->cpu_list;
1074ec9a77a7SAdrian Hunter
1075453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads);
1076d5bc056eSAdrian Hunter
1077bfd1b83dSNamhyung Kim /* as evlist now has references, put count here */
1078bfd1b83dSNamhyung Kim perf_cpu_map__put(cpus);
1079bfd1b83dSNamhyung Kim perf_thread_map__put(threads);
1080bfd1b83dSNamhyung Kim
1081d5bc056eSAdrian Hunter return 0;
10827e2ed097SArnaldo Carvalho de Melo
10837e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10847836e52eSJiri Olsa perf_thread_map__put(threads);
10857e2ed097SArnaldo Carvalho de Melo return -1;
10867e2ed097SArnaldo Carvalho de Melo }
10877e2ed097SArnaldo Carvalho de Melo
evlist__apply_filters(struct evlist * evlist,struct evsel ** err_evsel)108824bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10890a102479SFrederic Weisbecker {
109032dcd021SJiri Olsa struct evsel *evsel;
1091745cefc5SArnaldo Carvalho de Melo int err = 0;
10920a102479SFrederic Weisbecker
1093e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
1094d988d5eeSKan Liang /*
1095d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit.
1096d988d5eeSKan Liang * So evlist and evsel should always be same.
1097d988d5eeSKan Liang */
1098d180aa56SNamhyung Kim if (evsel->filter) {
1099a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
110023d4aad4SArnaldo Carvalho de Melo if (err) {
110123d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel;
1102745cefc5SArnaldo Carvalho de Melo break;
11030a102479SFrederic Weisbecker }
110423d4aad4SArnaldo Carvalho de Melo }
11050a102479SFrederic Weisbecker
1106d180aa56SNamhyung Kim /*
1107d180aa56SNamhyung Kim * non-tracepoint events can have BPF filters.
1108d180aa56SNamhyung Kim */
1109d180aa56SNamhyung Kim if (!list_empty(&evsel->bpf_filters)) {
1110d180aa56SNamhyung Kim err = perf_bpf_filter__prepare(evsel);
1111d180aa56SNamhyung Kim if (err) {
1112d180aa56SNamhyung Kim *err_evsel = evsel;
1113d180aa56SNamhyung Kim break;
1114d180aa56SNamhyung Kim }
1115d180aa56SNamhyung Kim }
1116d180aa56SNamhyung Kim }
1117d180aa56SNamhyung Kim
1118745cefc5SArnaldo Carvalho de Melo return err;
1119745cefc5SArnaldo Carvalho de Melo }
1120745cefc5SArnaldo Carvalho de Melo
evlist__set_tp_filter(struct evlist * evlist,const char * filter)112124bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1122745cefc5SArnaldo Carvalho de Melo {
112332dcd021SJiri Olsa struct evsel *evsel;
1124745cefc5SArnaldo Carvalho de Melo int err = 0;
1125745cefc5SArnaldo Carvalho de Melo
112605cea449SArnaldo Carvalho de Melo if (filter == NULL)
112705cea449SArnaldo Carvalho de Melo return -1;
112805cea449SArnaldo Carvalho de Melo
1129e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
11301fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1131fdf14720SWang Nan continue;
1132fdf14720SWang Nan
1133ad681adfSArnaldo Carvalho de Melo err = evsel__set_filter(evsel, filter);
1134745cefc5SArnaldo Carvalho de Melo if (err)
1135745cefc5SArnaldo Carvalho de Melo break;
1136745cefc5SArnaldo Carvalho de Melo }
1137745cefc5SArnaldo Carvalho de Melo
1138745cefc5SArnaldo Carvalho de Melo return err;
11390a102479SFrederic Weisbecker }
114074429964SFrederic Weisbecker
evlist__append_tp_filter(struct evlist * evlist,const char * filter)114124bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
114253c92f73SArnaldo Carvalho de Melo {
114353c92f73SArnaldo Carvalho de Melo struct evsel *evsel;
114453c92f73SArnaldo Carvalho de Melo int err = 0;
114553c92f73SArnaldo Carvalho de Melo
114653c92f73SArnaldo Carvalho de Melo if (filter == NULL)
114753c92f73SArnaldo Carvalho de Melo return -1;
114853c92f73SArnaldo Carvalho de Melo
114953c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
115053c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
115153c92f73SArnaldo Carvalho de Melo continue;
115253c92f73SArnaldo Carvalho de Melo
1153ad681adfSArnaldo Carvalho de Melo err = evsel__append_tp_filter(evsel, filter);
115453c92f73SArnaldo Carvalho de Melo if (err)
115553c92f73SArnaldo Carvalho de Melo break;
115653c92f73SArnaldo Carvalho de Melo }
115753c92f73SArnaldo Carvalho de Melo
115853c92f73SArnaldo Carvalho de Melo return err;
115953c92f73SArnaldo Carvalho de Melo }
116053c92f73SArnaldo Carvalho de Melo
asprintf__tp_filter_pids(size_t npids,pid_t * pids)1161da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1162cfd70a26SArnaldo Carvalho de Melo {
1163cfd70a26SArnaldo Carvalho de Melo char *filter;
1164be199adaSArnaldo Carvalho de Melo size_t i;
1165cfd70a26SArnaldo Carvalho de Melo
1166be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) {
1167be199adaSArnaldo Carvalho de Melo if (i == 0) {
1168be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
116905cea449SArnaldo Carvalho de Melo return NULL;
1170be199adaSArnaldo Carvalho de Melo } else {
1171be199adaSArnaldo Carvalho de Melo char *tmp;
1172be199adaSArnaldo Carvalho de Melo
1173be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1174be199adaSArnaldo Carvalho de Melo goto out_free;
1175be199adaSArnaldo Carvalho de Melo
1176be199adaSArnaldo Carvalho de Melo free(filter);
1177be199adaSArnaldo Carvalho de Melo filter = tmp;
1178be199adaSArnaldo Carvalho de Melo }
1179be199adaSArnaldo Carvalho de Melo }
1180cfd70a26SArnaldo Carvalho de Melo
118105cea449SArnaldo Carvalho de Melo return filter;
1182be199adaSArnaldo Carvalho de Melo out_free:
1183cfd70a26SArnaldo Carvalho de Melo free(filter);
118405cea449SArnaldo Carvalho de Melo return NULL;
118505cea449SArnaldo Carvalho de Melo }
118605cea449SArnaldo Carvalho de Melo
evlist__set_tp_filter_pids(struct evlist * evlist,size_t npids,pid_t * pids)118724bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
118805cea449SArnaldo Carvalho de Melo {
118905cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids);
119024bf91a7SArnaldo Carvalho de Melo int ret = evlist__set_tp_filter(evlist, filter);
119105cea449SArnaldo Carvalho de Melo
119205cea449SArnaldo Carvalho de Melo free(filter);
1193cfd70a26SArnaldo Carvalho de Melo return ret;
1194cfd70a26SArnaldo Carvalho de Melo }
1195cfd70a26SArnaldo Carvalho de Melo
evlist__set_tp_filter_pid(struct evlist * evlist,pid_t pid)119624bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1197be199adaSArnaldo Carvalho de Melo {
119824bf91a7SArnaldo Carvalho de Melo return evlist__set_tp_filter_pids(evlist, 1, &pid);
1199be199adaSArnaldo Carvalho de Melo }
1200be199adaSArnaldo Carvalho de Melo
evlist__append_tp_filter_pids(struct evlist * evlist,size_t npids,pid_t * pids)120124bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
12021827ab5bSArnaldo Carvalho de Melo {
12031827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids);
120424bf91a7SArnaldo Carvalho de Melo int ret = evlist__append_tp_filter(evlist, filter);
12051827ab5bSArnaldo Carvalho de Melo
12061827ab5bSArnaldo Carvalho de Melo free(filter);
12071827ab5bSArnaldo Carvalho de Melo return ret;
12081827ab5bSArnaldo Carvalho de Melo }
12091827ab5bSArnaldo Carvalho de Melo
evlist__append_tp_filter_pid(struct evlist * evlist,pid_t pid)121024bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
12111827ab5bSArnaldo Carvalho de Melo {
121224bf91a7SArnaldo Carvalho de Melo return evlist__append_tp_filter_pids(evlist, 1, &pid);
12131827ab5bSArnaldo Carvalho de Melo }
12141827ab5bSArnaldo Carvalho de Melo
evlist__valid_sample_type(struct evlist * evlist)1215b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
121674429964SFrederic Weisbecker {
121732dcd021SJiri Olsa struct evsel *pos;
1218c2a70653SArnaldo Carvalho de Melo
12196484d2f9SJiri Olsa if (evlist->core.nr_entries == 1)
122075562573SAdrian Hunter return true;
122175562573SAdrian Hunter
122275562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0)
122375562573SAdrian Hunter return false;
122475562573SAdrian Hunter
1225e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) {
122675562573SAdrian Hunter if (pos->id_pos != evlist->id_pos ||
122775562573SAdrian Hunter pos->is_pos != evlist->is_pos)
1228c2a70653SArnaldo Carvalho de Melo return false;
122974429964SFrederic Weisbecker }
123074429964SFrederic Weisbecker
1231c2a70653SArnaldo Carvalho de Melo return true;
1232c2a70653SArnaldo Carvalho de Melo }
1233c2a70653SArnaldo Carvalho de Melo
__evlist__combined_sample_type(struct evlist * evlist)1234b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1235c2a70653SArnaldo Carvalho de Melo {
123632dcd021SJiri Olsa struct evsel *evsel;
123775562573SAdrian Hunter
123875562573SAdrian Hunter if (evlist->combined_sample_type)
123975562573SAdrian Hunter return evlist->combined_sample_type;
124075562573SAdrian Hunter
1241e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel)
12421fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type;
124375562573SAdrian Hunter
124475562573SAdrian Hunter return evlist->combined_sample_type;
124575562573SAdrian Hunter }
124675562573SAdrian Hunter
evlist__combined_sample_type(struct evlist * evlist)1247b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
124875562573SAdrian Hunter {
124975562573SAdrian Hunter evlist->combined_sample_type = 0;
1250b3c2cc2bSArnaldo Carvalho de Melo return __evlist__combined_sample_type(evlist);
1251c2a70653SArnaldo Carvalho de Melo }
1252c2a70653SArnaldo Carvalho de Melo
evlist__combined_branch_type(struct evlist * evlist)125392c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
125498df858eSAndi Kleen {
125532dcd021SJiri Olsa struct evsel *evsel;
125698df858eSAndi Kleen u64 branch_type = 0;
125798df858eSAndi Kleen
1258e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel)
12591fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type;
126098df858eSAndi Kleen return branch_type;
126198df858eSAndi Kleen }
126298df858eSAndi Kleen
evlist__valid_read_format(struct evlist * evlist)126378e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
12649ede473cSJiri Olsa {
1265515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first;
12661fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format;
12671fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type;
12689ede473cSJiri Olsa
1269e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) {
127094d3820fSAdrian Hunter if (read_format != pos->core.attr.read_format) {
127194d3820fSAdrian Hunter pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
127294d3820fSAdrian Hunter read_format, (u64)pos->core.attr.read_format);
127394d3820fSAdrian Hunter }
12749ede473cSJiri Olsa }
12759ede473cSJiri Olsa
12764d39c89fSIngo Molnar /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
12779ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) &&
12789ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) {
12799ede473cSJiri Olsa return false;
12809ede473cSJiri Olsa }
12819ede473cSJiri Olsa
12829ede473cSJiri Olsa return true;
12839ede473cSJiri Olsa }
12849ede473cSJiri Olsa
evlist__id_hdr_size(struct evlist * evlist)12851420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
128681e36bffSArnaldo Carvalho de Melo {
1287515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist);
128881e36bffSArnaldo Carvalho de Melo
12890a64de04SAdrian Hunter return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
129081e36bffSArnaldo Carvalho de Melo }
129181e36bffSArnaldo Carvalho de Melo
evlist__valid_sample_id_all(struct evlist * evlist)12928cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1293c2a70653SArnaldo Carvalho de Melo {
1294515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first;
1295c2a70653SArnaldo Carvalho de Melo
1296e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) {
12971fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1298c2a70653SArnaldo Carvalho de Melo return false;
1299c2a70653SArnaldo Carvalho de Melo }
1300c2a70653SArnaldo Carvalho de Melo
1301c2a70653SArnaldo Carvalho de Melo return true;
130274429964SFrederic Weisbecker }
130374429964SFrederic Weisbecker
evlist__sample_id_all(struct evlist * evlist)13048cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
130574429964SFrederic Weisbecker {
1306515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist);
13071fc632ceSJiri Olsa return first->core.attr.sample_id_all;
130874429964SFrederic Weisbecker }
130981cce8deSArnaldo Carvalho de Melo
evlist__set_selected(struct evlist * evlist,struct evsel * evsel)1310900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
131181cce8deSArnaldo Carvalho de Melo {
131281cce8deSArnaldo Carvalho de Melo evlist->selected = evsel;
131381cce8deSArnaldo Carvalho de Melo }
1314727ab04eSArnaldo Carvalho de Melo
evlist__close(struct evlist * evlist)1315750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1316a74b4b66SNamhyung Kim {
131732dcd021SJiri Olsa struct evsel *evsel;
1318472832d2SIan Rogers struct evlist_cpu_iterator evlist_cpu_itr;
13197736627bSAndi Kleen struct affinity affinity;
1320a74b4b66SNamhyung Kim
13217736627bSAndi Kleen /*
13220df6ade7SIan Rogers * With perf record core.user_requested_cpus is usually NULL.
13237736627bSAndi Kleen * Use the old method to handle this for now.
13247736627bSAndi Kleen */
13250df6ade7SIan Rogers if (!evlist->core.user_requested_cpus ||
13260df6ade7SIan Rogers cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1327475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel)
132888761fa1SJiri Olsa evsel__close(evsel);
13297736627bSAndi Kleen return;
13307736627bSAndi Kleen }
13317736627bSAndi Kleen
13327736627bSAndi Kleen if (affinity__setup(&affinity) < 0)
13337736627bSAndi Kleen return;
13347736627bSAndi Kleen
1335472832d2SIan Rogers evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1336472832d2SIan Rogers perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1337472832d2SIan Rogers evlist_cpu_itr.cpu_map_idx);
13387736627bSAndi Kleen }
1339472832d2SIan Rogers
13407736627bSAndi Kleen affinity__cleanup(&affinity);
13417736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) {
13427736627bSAndi Kleen perf_evsel__free_fd(&evsel->core);
13437736627bSAndi Kleen perf_evsel__free_id(&evsel->core);
13447736627bSAndi Kleen }
1345513068f2SNamhyung Kim perf_evlist__reset_id_hash(&evlist->core);
1346a74b4b66SNamhyung Kim }
1347a74b4b66SNamhyung Kim
evlist__create_syswide_maps(struct evlist * evlist)13487748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
13494112eb18SArnaldo Carvalho de Melo {
1350f854839bSJiri Olsa struct perf_cpu_map *cpus;
13519749b90eSJiri Olsa struct perf_thread_map *threads;
13524112eb18SArnaldo Carvalho de Melo
13534112eb18SArnaldo Carvalho de Melo /*
13544112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get
13554112eb18SArnaldo Carvalho de Melo * an all cpus map.
13564112eb18SArnaldo Carvalho de Melo *
13574112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map
13584112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the
13594112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a
13604112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\
13614112eb18SArnaldo Carvalho de Melo */
13629c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL);
13638c0498b6SAdrian Hunter if (!cpus)
13644112eb18SArnaldo Carvalho de Melo goto out;
13654112eb18SArnaldo Carvalho de Melo
13664b49cce2SJiri Olsa threads = perf_thread_map__new_dummy();
13678c0498b6SAdrian Hunter if (!threads)
13688c0498b6SAdrian Hunter goto out_put;
13694112eb18SArnaldo Carvalho de Melo
1370453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads);
1371bfd1b83dSNamhyung Kim
1372bfd1b83dSNamhyung Kim perf_thread_map__put(threads);
13738c0498b6SAdrian Hunter out_put:
137438f01d8dSJiri Olsa perf_cpu_map__put(cpus);
1375bfd1b83dSNamhyung Kim out:
1376f717d89aSHaowen Bai return -ENOMEM;
13774112eb18SArnaldo Carvalho de Melo }
13784112eb18SArnaldo Carvalho de Melo
evlist__open(struct evlist * evlist)1379474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1380727ab04eSArnaldo Carvalho de Melo {
138132dcd021SJiri Olsa struct evsel *evsel;
1382a74b4b66SNamhyung Kim int err;
1383727ab04eSArnaldo Carvalho de Melo
13844112eb18SArnaldo Carvalho de Melo /*
13854112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide
13864112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13874112eb18SArnaldo Carvalho de Melo */
13880df6ade7SIan Rogers if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
13897748bb71SArnaldo Carvalho de Melo err = evlist__create_syswide_maps(evlist);
13904112eb18SArnaldo Carvalho de Melo if (err < 0)
13914112eb18SArnaldo Carvalho de Melo goto out_err;
13924112eb18SArnaldo Carvalho de Melo }
13934112eb18SArnaldo Carvalho de Melo
13940a7e7ec9SArnaldo Carvalho de Melo evlist__update_id_pos(evlist);
1395733cd2feSAdrian Hunter
1396e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
1397af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1398727ab04eSArnaldo Carvalho de Melo if (err < 0)
1399727ab04eSArnaldo Carvalho de Melo goto out_err;
1400727ab04eSArnaldo Carvalho de Melo }
1401727ab04eSArnaldo Carvalho de Melo
1402727ab04eSArnaldo Carvalho de Melo return 0;
1403727ab04eSArnaldo Carvalho de Melo out_err:
1404750b4edeSJiri Olsa evlist__close(evlist);
140541c21a68SNamhyung Kim errno = -err;
1406727ab04eSArnaldo Carvalho de Melo return err;
1407727ab04eSArnaldo Carvalho de Melo }
140835b9d88eSArnaldo Carvalho de Melo
evlist__prepare_workload(struct evlist * evlist,struct target * target,const char * argv[],bool pipe_output,void (* exec_error)(int signo,siginfo_t * info,void * ucontext))14097b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
14107b392ef0SArnaldo Carvalho de Melo bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
141135b9d88eSArnaldo Carvalho de Melo {
141235b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2];
141335b9d88eSArnaldo Carvalho de Melo char bf;
141435b9d88eSArnaldo Carvalho de Melo
141535b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) {
141635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe");
141735b9d88eSArnaldo Carvalho de Melo return -1;
141835b9d88eSArnaldo Carvalho de Melo }
141935b9d88eSArnaldo Carvalho de Melo
142035b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) {
142135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe");
142235b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe;
142335b9d88eSArnaldo Carvalho de Melo }
142435b9d88eSArnaldo Carvalho de Melo
142535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork();
142635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) {
142735b9d88eSArnaldo Carvalho de Melo perror("failed to fork");
142835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes;
142935b9d88eSArnaldo Carvalho de Melo }
143035b9d88eSArnaldo Carvalho de Melo
143135b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) {
14325f1c4225SArnaldo Carvalho de Melo int ret;
14335f1c4225SArnaldo Carvalho de Melo
1434119fa3c9SNamhyung Kim if (pipe_output)
143535b9d88eSArnaldo Carvalho de Melo dup2(2, 1);
143635b9d88eSArnaldo Carvalho de Melo
14370817df08SDavid Ahern signal(SIGTERM, SIG_DFL);
14380817df08SDavid Ahern
143935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]);
144035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]);
144135b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
144235b9d88eSArnaldo Carvalho de Melo
144335b9d88eSArnaldo Carvalho de Melo /*
1444a7672d1dSArnaldo Carvalho de Melo * Change the name of this process not to confuse --exclude-perf users
1445a7672d1dSArnaldo Carvalho de Melo * that sees 'perf' in the window up to the execvp() and thinks that
1446a7672d1dSArnaldo Carvalho de Melo * perf samples are not being excluded.
1447a7672d1dSArnaldo Carvalho de Melo */
1448a7672d1dSArnaldo Carvalho de Melo prctl(PR_SET_NAME, "perf-exec");
1449a7672d1dSArnaldo Carvalho de Melo
1450a7672d1dSArnaldo Carvalho de Melo /*
145135b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go
145235b9d88eSArnaldo Carvalho de Melo */
145335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]);
145435b9d88eSArnaldo Carvalho de Melo
145535b9d88eSArnaldo Carvalho de Melo /*
145635b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go.
145735b9d88eSArnaldo Carvalho de Melo */
14585f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1);
14595f1c4225SArnaldo Carvalho de Melo /*
14605f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by
14615f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via
14627b392ef0SArnaldo Carvalho de Melo * evlist__start_workload().
14635f1c4225SArnaldo Carvalho de Melo *
146420f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it,
14655f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing
14665f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit()
14675f1c4225SArnaldo Carvalho de Melo * here.
14685f1c4225SArnaldo Carvalho de Melo */
14695f1c4225SArnaldo Carvalho de Melo if (ret != 1) {
14705f1c4225SArnaldo Carvalho de Melo if (ret == -1)
147135b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe");
14725f1c4225SArnaldo Carvalho de Melo exit(ret);
14735f1c4225SArnaldo Carvalho de Melo }
147435b9d88eSArnaldo Carvalho de Melo
147535b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv);
147635b9d88eSArnaldo Carvalho de Melo
1477735f7e0bSArnaldo Carvalho de Melo if (exec_error) {
1478f33cbe72SArnaldo Carvalho de Melo union sigval val;
1479f33cbe72SArnaldo Carvalho de Melo
1480f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno;
1481f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val))
148235b9d88eSArnaldo Carvalho de Melo perror(argv[0]);
1483f33cbe72SArnaldo Carvalho de Melo } else
1484f33cbe72SArnaldo Carvalho de Melo perror(argv[0]);
148535b9d88eSArnaldo Carvalho de Melo exit(-1);
148635b9d88eSArnaldo Carvalho de Melo }
148735b9d88eSArnaldo Carvalho de Melo
1488735f7e0bSArnaldo Carvalho de Melo if (exec_error) {
1489735f7e0bSArnaldo Carvalho de Melo struct sigaction act = {
1490735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO,
1491735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error,
1492735f7e0bSArnaldo Carvalho de Melo };
1493735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL);
1494735f7e0bSArnaldo Carvalho de Melo }
1495735f7e0bSArnaldo Carvalho de Melo
14961aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) {
149703617c22SJiri Olsa if (evlist->core.threads == NULL) {
14981aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14991aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__);
15001aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes;
15011aaf63b1SArnaldo Carvalho de Melo }
150203617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
15031aaf63b1SArnaldo Carvalho de Melo }
150435b9d88eSArnaldo Carvalho de Melo
150535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]);
150635b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]);
150735b9d88eSArnaldo Carvalho de Melo /*
150835b9d88eSArnaldo Carvalho de Melo * wait for child to settle
150935b9d88eSArnaldo Carvalho de Melo */
151035b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) {
151135b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe");
151235b9d88eSArnaldo Carvalho de Melo goto out_close_pipes;
151335b9d88eSArnaldo Carvalho de Melo }
151435b9d88eSArnaldo Carvalho de Melo
1515bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
151635b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1];
151735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]);
151835b9d88eSArnaldo Carvalho de Melo return 0;
151935b9d88eSArnaldo Carvalho de Melo
152035b9d88eSArnaldo Carvalho de Melo out_close_pipes:
152135b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]);
152235b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]);
152335b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
152435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]);
152535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]);
152635b9d88eSArnaldo Carvalho de Melo return -1;
152735b9d88eSArnaldo Carvalho de Melo }
152835b9d88eSArnaldo Carvalho de Melo
evlist__start_workload(struct evlist * evlist)15297b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
153035b9d88eSArnaldo Carvalho de Melo {
153135b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) {
1532b3824404SDavid Ahern char bf = 0;
1533bcf3145fSNamhyung Kim int ret;
153435b9d88eSArnaldo Carvalho de Melo /*
153535b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip!
153635b9d88eSArnaldo Carvalho de Melo */
1537bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1);
1538bcf3145fSNamhyung Kim if (ret < 0)
1539e978be9eSSoramichi Akiyama perror("unable to write to pipe");
1540bcf3145fSNamhyung Kim
1541bcf3145fSNamhyung Kim close(evlist->workload.cork_fd);
1542bcf3145fSNamhyung Kim return ret;
154335b9d88eSArnaldo Carvalho de Melo }
154435b9d88eSArnaldo Carvalho de Melo
154535b9d88eSArnaldo Carvalho de Melo return 0;
154635b9d88eSArnaldo Carvalho de Melo }
1547cb0b29e0SArnaldo Carvalho de Melo
evlist__parse_sample(struct evlist * evlist,union perf_event * event,struct perf_sample * sample)15482a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1549cb0b29e0SArnaldo Carvalho de Melo {
15503ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event);
15513461b65dSAdrian Hunter int ret;
155275562573SAdrian Hunter
155375562573SAdrian Hunter if (!evsel)
155475562573SAdrian Hunter return -EFAULT;
15553461b65dSAdrian Hunter ret = evsel__parse_sample(evsel, event, sample);
15563461b65dSAdrian Hunter if (ret)
15573461b65dSAdrian Hunter return ret;
15583461b65dSAdrian Hunter if (perf_guest && sample->id) {
15593461b65dSAdrian Hunter struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
15603461b65dSAdrian Hunter
15613461b65dSAdrian Hunter if (sid) {
15623461b65dSAdrian Hunter sample->machine_pid = sid->machine_pid;
15633461b65dSAdrian Hunter sample->vcpu = sid->vcpu.cpu;
15643461b65dSAdrian Hunter }
15653461b65dSAdrian Hunter }
15663461b65dSAdrian Hunter return 0;
1567cb0b29e0SArnaldo Carvalho de Melo }
156878f067b3SArnaldo Carvalho de Melo
evlist__parse_sample_timestamp(struct evlist * evlist,union perf_event * event,u64 * timestamp)15692a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
157001468120SJiri Olsa {
15713ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event);
157201468120SJiri Olsa
157301468120SJiri Olsa if (!evsel)
157401468120SJiri Olsa return -EFAULT;
15756b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample_timestamp(evsel, event, timestamp);
157601468120SJiri Olsa }
157701468120SJiri Olsa
evlist__strerror_open(struct evlist * evlist,int err,char * buf,size_t size)1578d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1579a8f23d8fSArnaldo Carvalho de Melo {
1580a8f23d8fSArnaldo Carvalho de Melo int printed, value;
1581c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1582a8f23d8fSArnaldo Carvalho de Melo
1583a8f23d8fSArnaldo Carvalho de Melo switch (err) {
1584a8f23d8fSArnaldo Carvalho de Melo case EACCES:
1585a8f23d8fSArnaldo Carvalho de Melo case EPERM:
1586a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size,
1587a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n"
1588a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1589a8f23d8fSArnaldo Carvalho de Melo
15901a47245dSAdrian Hunter value = perf_event_paranoid();
1591a8f23d8fSArnaldo Carvalho de Melo
1592a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1593a8f23d8fSArnaldo Carvalho de Melo
1594a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) {
1595a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed,
1596a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t");
1597a8f23d8fSArnaldo Carvalho de Melo }
1598a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed,
15995229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n");
1600a8f23d8fSArnaldo Carvalho de Melo
1601a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed,
16025229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
16035229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value);
1604a8f23d8fSArnaldo Carvalho de Melo break;
1605d9aade7fSArnaldo Carvalho de Melo case EINVAL: {
1606515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist);
1607d9aade7fSArnaldo Carvalho de Melo int max_freq;
1608d9aade7fSArnaldo Carvalho de Melo
1609d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1610d9aade7fSArnaldo Carvalho de Melo goto out_default;
1611d9aade7fSArnaldo Carvalho de Melo
16121fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq)
1613d9aade7fSArnaldo Carvalho de Melo goto out_default;
1614d9aade7fSArnaldo Carvalho de Melo
1615d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size,
1616d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n"
1617d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1618d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
16191fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq);
1620d9aade7fSArnaldo Carvalho de Melo break;
1621d9aade7fSArnaldo Carvalho de Melo }
1622a8f23d8fSArnaldo Carvalho de Melo default:
1623d9aade7fSArnaldo Carvalho de Melo out_default:
1624a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg);
1625a8f23d8fSArnaldo Carvalho de Melo break;
1626a8f23d8fSArnaldo Carvalho de Melo }
1627a8f23d8fSArnaldo Carvalho de Melo
1628a8f23d8fSArnaldo Carvalho de Melo return 0;
1629a8f23d8fSArnaldo Carvalho de Melo }
1630a025e4f0SAdrian Hunter
evlist__strerror_mmap(struct evlist * evlist,int err,char * buf,size_t size)1631d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1632956fa571SArnaldo Carvalho de Melo {
1633c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1634f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1635956fa571SArnaldo Carvalho de Melo
1636956fa571SArnaldo Carvalho de Melo switch (err) {
1637956fa571SArnaldo Carvalho de Melo case EPERM:
1638e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1639e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed,
1640e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n"
1641956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1642e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n",
1643e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted);
1644e965bea1SArnaldo Carvalho de Melo
1645e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) {
1646e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed,
1647e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1648e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted);
1649e965bea1SArnaldo Carvalho de Melo }
1650e965bea1SArnaldo Carvalho de Melo
1651e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed,
1652e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value.");
1653956fa571SArnaldo Carvalho de Melo break;
1654956fa571SArnaldo Carvalho de Melo default:
1655956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg);
1656956fa571SArnaldo Carvalho de Melo break;
1657956fa571SArnaldo Carvalho de Melo }
1658956fa571SArnaldo Carvalho de Melo
1659956fa571SArnaldo Carvalho de Melo return 0;
1660956fa571SArnaldo Carvalho de Melo }
1661956fa571SArnaldo Carvalho de Melo
evlist__to_front(struct evlist * evlist,struct evsel * move_evsel)1662e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1663a025e4f0SAdrian Hunter {
166432dcd021SJiri Olsa struct evsel *evsel, *n;
1665a025e4f0SAdrian Hunter LIST_HEAD(move);
1666a025e4f0SAdrian Hunter
1667515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist))
1668a025e4f0SAdrian Hunter return;
1669a025e4f0SAdrian Hunter
1670e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) {
1671fba7c866SJiri Olsa if (evsel__leader(evsel) == evsel__leader(move_evsel))
1672b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move);
1673a025e4f0SAdrian Hunter }
1674a025e4f0SAdrian Hunter
1675ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries);
1676a025e4f0SAdrian Hunter }
167760b0896cSAdrian Hunter
evlist__get_tracking_event(struct evlist * evlist)1678e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1679442ad225SAdrian Hunter {
1680442ad225SAdrian Hunter struct evsel *evsel;
1681442ad225SAdrian Hunter
1682442ad225SAdrian Hunter evlist__for_each_entry(evlist, evsel) {
1683442ad225SAdrian Hunter if (evsel->tracking)
1684442ad225SAdrian Hunter return evsel;
1685442ad225SAdrian Hunter }
1686442ad225SAdrian Hunter
1687442ad225SAdrian Hunter return evlist__first(evlist);
1688442ad225SAdrian Hunter }
1689442ad225SAdrian Hunter
evlist__set_tracking_event(struct evlist * evlist,struct evsel * tracking_evsel)1690e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
169160b0896cSAdrian Hunter {
169232dcd021SJiri Olsa struct evsel *evsel;
169360b0896cSAdrian Hunter
169460b0896cSAdrian Hunter if (tracking_evsel->tracking)
169560b0896cSAdrian Hunter return;
169660b0896cSAdrian Hunter
1697e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
169860b0896cSAdrian Hunter if (evsel != tracking_evsel)
169960b0896cSAdrian Hunter evsel->tracking = false;
170060b0896cSAdrian Hunter }
170160b0896cSAdrian Hunter
170260b0896cSAdrian Hunter tracking_evsel->tracking = true;
170360b0896cSAdrian Hunter }
17047630b3e2SWang Nan
evlist__findnew_tracking_event(struct evlist * evlist,bool system_wide)1705e024e98eSYang Jihong struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide)
1706e024e98eSYang Jihong {
1707e024e98eSYang Jihong struct evsel *evsel;
1708e024e98eSYang Jihong
1709e024e98eSYang Jihong evsel = evlist__get_tracking_event(evlist);
1710e024e98eSYang Jihong if (!evsel__is_dummy_event(evsel)) {
1711e024e98eSYang Jihong evsel = evlist__add_aux_dummy(evlist, system_wide);
1712e024e98eSYang Jihong if (!evsel)
1713e024e98eSYang Jihong return NULL;
1714e024e98eSYang Jihong
1715e024e98eSYang Jihong evlist__set_tracking_event(evlist, evsel);
1716e024e98eSYang Jihong } else if (system_wide) {
1717e024e98eSYang Jihong perf_evlist__go_system_wide(&evlist->core, &evsel->core);
1718e024e98eSYang Jihong }
1719e024e98eSYang Jihong
1720e024e98eSYang Jihong return evsel;
1721e024e98eSYang Jihong }
1722e024e98eSYang Jihong
evlist__find_evsel_by_str(struct evlist * evlist,const char * str)1723b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
17247630b3e2SWang Nan {
172532dcd021SJiri Olsa struct evsel *evsel;
17267630b3e2SWang Nan
1727e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
17287630b3e2SWang Nan if (!evsel->name)
17297630b3e2SWang Nan continue;
1730ce1d3bc2SArnaldo Carvalho de Melo if (evsel__name_is(evsel, str))
17317630b3e2SWang Nan return evsel;
17327630b3e2SWang Nan }
17337630b3e2SWang Nan
17347630b3e2SWang Nan return NULL;
17357630b3e2SWang Nan }
173654cc54deSWang Nan
evlist__toggle_bkw_mmap(struct evlist * evlist,enum bkw_mmap_state state)1737ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
173854cc54deSWang Nan {
173954cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
174054cc54deSWang Nan enum action {
174154cc54deSWang Nan NONE,
174254cc54deSWang Nan PAUSE,
174354cc54deSWang Nan RESUME,
174454cc54deSWang Nan } action = NONE;
174554cc54deSWang Nan
17460b72d69aSWang Nan if (!evlist->overwrite_mmap)
174754cc54deSWang Nan return;
174854cc54deSWang Nan
174954cc54deSWang Nan switch (old_state) {
175054cc54deSWang Nan case BKW_MMAP_NOTREADY: {
175154cc54deSWang Nan if (state != BKW_MMAP_RUNNING)
1752dd8bd53aSLuis de Bethencourt goto state_err;
175354cc54deSWang Nan break;
175454cc54deSWang Nan }
175554cc54deSWang Nan case BKW_MMAP_RUNNING: {
175654cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING)
175754cc54deSWang Nan goto state_err;
175854cc54deSWang Nan action = PAUSE;
175954cc54deSWang Nan break;
176054cc54deSWang Nan }
176154cc54deSWang Nan case BKW_MMAP_DATA_PENDING: {
176254cc54deSWang Nan if (state != BKW_MMAP_EMPTY)
176354cc54deSWang Nan goto state_err;
176454cc54deSWang Nan break;
176554cc54deSWang Nan }
176654cc54deSWang Nan case BKW_MMAP_EMPTY: {
176754cc54deSWang Nan if (state != BKW_MMAP_RUNNING)
176854cc54deSWang Nan goto state_err;
176954cc54deSWang Nan action = RESUME;
177054cc54deSWang Nan break;
177154cc54deSWang Nan }
177254cc54deSWang Nan default:
177354cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n");
177454cc54deSWang Nan }
177554cc54deSWang Nan
177654cc54deSWang Nan evlist->bkw_mmap_state = state;
177754cc54deSWang Nan
177854cc54deSWang Nan switch (action) {
177954cc54deSWang Nan case PAUSE:
17800a60b339SArnaldo Carvalho de Melo evlist__pause(evlist);
178154cc54deSWang Nan break;
178254cc54deSWang Nan case RESUME:
17830a60b339SArnaldo Carvalho de Melo evlist__resume(evlist);
178454cc54deSWang Nan break;
178554cc54deSWang Nan case NONE:
178654cc54deSWang Nan default:
178754cc54deSWang Nan break;
178854cc54deSWang Nan }
178954cc54deSWang Nan
179054cc54deSWang Nan state_err:
179154cc54deSWang Nan return;
179254cc54deSWang Nan }
179307d6f446SArnaldo Carvalho de Melo
evlist__exclude_kernel(struct evlist * evlist)179478e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
179507d6f446SArnaldo Carvalho de Melo {
179632dcd021SJiri Olsa struct evsel *evsel;
179707d6f446SArnaldo Carvalho de Melo
179807d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
17991fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel)
180007d6f446SArnaldo Carvalho de Melo return false;
180107d6f446SArnaldo Carvalho de Melo }
180207d6f446SArnaldo Carvalho de Melo
180307d6f446SArnaldo Carvalho de Melo return true;
180407d6f446SArnaldo Carvalho de Melo }
1805e2bdbe80SJin Yao
1806e2bdbe80SJin Yao /*
1807e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want
1808e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's
1809e2bdbe80SJin Yao * forced_leader flag to notify the display code.
1810e2bdbe80SJin Yao */
evlist__force_leader(struct evlist * evlist)181164b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1812e2bdbe80SJin Yao {
18139d2dc632SIan Rogers if (evlist__nr_groups(evlist) == 0) {
1814515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist);
1815e2bdbe80SJin Yao
1816a622eafaSArnaldo Carvalho de Melo evlist__set_leader(evlist);
1817e2bdbe80SJin Yao leader->forced_leader = true;
1818e2bdbe80SJin Yao }
1819e2bdbe80SJin Yao }
1820c3537fc2SAndi Kleen
evlist__reset_weak_group(struct evlist * evsel_list,struct evsel * evsel,bool close)182164b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1822c3537fc2SAndi Kleen {
182332dcd021SJiri Olsa struct evsel *c2, *leader;
1824c3537fc2SAndi Kleen bool is_open = true;
1825c3537fc2SAndi Kleen
1826fba7c866SJiri Olsa leader = evsel__leader(evsel);
1827fba7c866SJiri Olsa
1828c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n",
18295643b1a5SJiri Olsa leader->name, leader->core.nr_members);
1830c3537fc2SAndi Kleen
1831c3537fc2SAndi Kleen /*
1832c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't
1833c3537fc2SAndi Kleen * include the first entry.
1834c3537fc2SAndi Kleen */
1835c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) {
1836c3537fc2SAndi Kleen if (c2 == evsel)
1837c3537fc2SAndi Kleen is_open = false;
1838fba7c866SJiri Olsa if (evsel__has_leader(c2, leader)) {
18394804e011SAndi Kleen if (is_open && close)
18405a40e199SAndi Kleen perf_evsel__close(&c2->core);
1841d98079c0SIan Rogers /*
1842d98079c0SIan Rogers * We want to close all members of the group and reopen
1843d98079c0SIan Rogers * them. Some events, like Intel topdown, require being
1844d98079c0SIan Rogers * in a group and so keep these in the group.
1845d98079c0SIan Rogers */
1846e8f4f794SKan Liang evsel__remove_from_group(c2, leader);
1847d98079c0SIan Rogers
18484804e011SAndi Kleen /*
18494804e011SAndi Kleen * Set this for all former members of the group
18504804e011SAndi Kleen * to indicate they get reopened.
18514804e011SAndi Kleen */
18524804e011SAndi Kleen c2->reset_group = true;
1853c3537fc2SAndi Kleen }
1854c3537fc2SAndi Kleen }
1855d98079c0SIan Rogers /* Reset the leader count if all entries were removed. */
1856d98079c0SIan Rogers if (leader->core.nr_members == 1)
1857d98079c0SIan Rogers leader->core.nr_members = 0;
1858c3537fc2SAndi Kleen return leader;
1859c3537fc2SAndi Kleen }
1860ec886bf5SAlexey Budankov
evlist__parse_control_fifo(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1861a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1862a8fcbd26SAdrian Hunter {
1863a8fcbd26SAdrian Hunter char *s, *p;
1864a8fcbd26SAdrian Hunter int ret = 0, fd;
1865a8fcbd26SAdrian Hunter
1866a8fcbd26SAdrian Hunter if (strncmp(str, "fifo:", 5))
1867a8fcbd26SAdrian Hunter return -EINVAL;
1868a8fcbd26SAdrian Hunter
1869a8fcbd26SAdrian Hunter str += 5;
1870a8fcbd26SAdrian Hunter if (!*str || *str == ',')
1871a8fcbd26SAdrian Hunter return -EINVAL;
1872a8fcbd26SAdrian Hunter
1873a8fcbd26SAdrian Hunter s = strdup(str);
1874a8fcbd26SAdrian Hunter if (!s)
1875a8fcbd26SAdrian Hunter return -ENOMEM;
1876a8fcbd26SAdrian Hunter
1877a8fcbd26SAdrian Hunter p = strchr(s, ',');
1878a8fcbd26SAdrian Hunter if (p)
1879a8fcbd26SAdrian Hunter *p = '\0';
1880a8fcbd26SAdrian Hunter
1881a8fcbd26SAdrian Hunter /*
1882a8fcbd26SAdrian Hunter * O_RDWR avoids POLLHUPs which is necessary to allow the other
1883a8fcbd26SAdrian Hunter * end of a FIFO to be repeatedly opened and closed.
1884a8fcbd26SAdrian Hunter */
1885a8fcbd26SAdrian Hunter fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1886a8fcbd26SAdrian Hunter if (fd < 0) {
1887a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", s);
1888a8fcbd26SAdrian Hunter ret = -errno;
1889a8fcbd26SAdrian Hunter goto out_free;
1890a8fcbd26SAdrian Hunter }
1891a8fcbd26SAdrian Hunter *ctl_fd = fd;
1892a8fcbd26SAdrian Hunter *ctl_fd_close = true;
1893a8fcbd26SAdrian Hunter
1894a8fcbd26SAdrian Hunter if (p && *++p) {
1895a8fcbd26SAdrian Hunter /* O_RDWR | O_NONBLOCK means the other end need not be open */
1896a8fcbd26SAdrian Hunter fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1897a8fcbd26SAdrian Hunter if (fd < 0) {
1898a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", p);
1899a8fcbd26SAdrian Hunter ret = -errno;
1900a8fcbd26SAdrian Hunter goto out_free;
1901a8fcbd26SAdrian Hunter }
1902a8fcbd26SAdrian Hunter *ctl_fd_ack = fd;
1903a8fcbd26SAdrian Hunter }
1904a8fcbd26SAdrian Hunter
1905a8fcbd26SAdrian Hunter out_free:
1906a8fcbd26SAdrian Hunter free(s);
1907a8fcbd26SAdrian Hunter return ret;
1908a8fcbd26SAdrian Hunter }
1909a8fcbd26SAdrian Hunter
evlist__parse_control(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1910a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
19119864a66dSAdrian Hunter {
19129864a66dSAdrian Hunter char *comma = NULL, *endptr = NULL;
19139864a66dSAdrian Hunter
1914a8fcbd26SAdrian Hunter *ctl_fd_close = false;
1915a8fcbd26SAdrian Hunter
19169864a66dSAdrian Hunter if (strncmp(str, "fd:", 3))
1917a8fcbd26SAdrian Hunter return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
19189864a66dSAdrian Hunter
19199864a66dSAdrian Hunter *ctl_fd = strtoul(&str[3], &endptr, 0);
19209864a66dSAdrian Hunter if (endptr == &str[3])
19219864a66dSAdrian Hunter return -EINVAL;
19229864a66dSAdrian Hunter
19239864a66dSAdrian Hunter comma = strchr(str, ',');
19249864a66dSAdrian Hunter if (comma) {
19259864a66dSAdrian Hunter if (endptr != comma)
19269864a66dSAdrian Hunter return -EINVAL;
19279864a66dSAdrian Hunter
19289864a66dSAdrian Hunter *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
19299864a66dSAdrian Hunter if (endptr == comma + 1 || *endptr != '\0')
19309864a66dSAdrian Hunter return -EINVAL;
19319864a66dSAdrian Hunter }
19329864a66dSAdrian Hunter
19339864a66dSAdrian Hunter return 0;
19349864a66dSAdrian Hunter }
19359864a66dSAdrian Hunter
evlist__close_control(int ctl_fd,int ctl_fd_ack,bool * ctl_fd_close)1936ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1937ee7fe31eSAdrian Hunter {
1938ee7fe31eSAdrian Hunter if (*ctl_fd_close) {
1939ee7fe31eSAdrian Hunter *ctl_fd_close = false;
1940ee7fe31eSAdrian Hunter close(ctl_fd);
1941ee7fe31eSAdrian Hunter if (ctl_fd_ack >= 0)
1942ee7fe31eSAdrian Hunter close(ctl_fd_ack);
1943ee7fe31eSAdrian Hunter }
1944ee7fe31eSAdrian Hunter }
1945ee7fe31eSAdrian Hunter
evlist__initialize_ctlfd(struct evlist * evlist,int fd,int ack)1946ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1947ec886bf5SAlexey Budankov {
1948ec886bf5SAlexey Budankov if (fd == -1) {
1949ec886bf5SAlexey Budankov pr_debug("Control descriptor is not initialized\n");
1950ec886bf5SAlexey Budankov return 0;
1951ec886bf5SAlexey Budankov }
1952ec886bf5SAlexey Budankov
1953ec886bf5SAlexey Budankov evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1954feff0b61SAdrian Hunter fdarray_flag__nonfilterable |
1955feff0b61SAdrian Hunter fdarray_flag__non_perf_event);
1956ec886bf5SAlexey Budankov if (evlist->ctl_fd.pos < 0) {
1957ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1;
1958ec886bf5SAlexey Budankov pr_err("Failed to add ctl fd entry: %m\n");
1959ec886bf5SAlexey Budankov return -1;
1960ec886bf5SAlexey Budankov }
1961ec886bf5SAlexey Budankov
1962ec886bf5SAlexey Budankov evlist->ctl_fd.fd = fd;
1963ec886bf5SAlexey Budankov evlist->ctl_fd.ack = ack;
1964ec886bf5SAlexey Budankov
1965ec886bf5SAlexey Budankov return 0;
1966ec886bf5SAlexey Budankov }
1967ec886bf5SAlexey Budankov
evlist__ctlfd_initialized(struct evlist * evlist)1968ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1969ec886bf5SAlexey Budankov {
1970ec886bf5SAlexey Budankov return evlist->ctl_fd.pos >= 0;
1971ec886bf5SAlexey Budankov }
1972ec886bf5SAlexey Budankov
evlist__finalize_ctlfd(struct evlist * evlist)1973ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1974ec886bf5SAlexey Budankov {
1975ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries;
1976ec886bf5SAlexey Budankov
1977ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist))
1978ec886bf5SAlexey Budankov return 0;
1979ec886bf5SAlexey Budankov
1980ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].fd = -1;
1981ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].events = 0;
1982ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].revents = 0;
1983ec886bf5SAlexey Budankov
1984ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1;
1985ec886bf5SAlexey Budankov evlist->ctl_fd.ack = -1;
1986ec886bf5SAlexey Budankov evlist->ctl_fd.fd = -1;
1987ec886bf5SAlexey Budankov
1988ec886bf5SAlexey Budankov return 0;
1989ec886bf5SAlexey Budankov }
1990ec886bf5SAlexey Budankov
evlist__ctlfd_recv(struct evlist * evlist,enum evlist_ctl_cmd * cmd,char * cmd_data,size_t data_size)1991ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1992ec886bf5SAlexey Budankov char *cmd_data, size_t data_size)
1993ec886bf5SAlexey Budankov {
1994ec886bf5SAlexey Budankov int err;
1995ec886bf5SAlexey Budankov char c;
1996ec886bf5SAlexey Budankov size_t bytes_read = 0;
1997ec886bf5SAlexey Budankov
199840db8ff5SAdrian Hunter *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1999ec886bf5SAlexey Budankov memset(cmd_data, 0, data_size);
2000ec886bf5SAlexey Budankov data_size--;
2001ec886bf5SAlexey Budankov
2002ec886bf5SAlexey Budankov do {
2003ec886bf5SAlexey Budankov err = read(evlist->ctl_fd.fd, &c, 1);
2004ec886bf5SAlexey Budankov if (err > 0) {
2005ec886bf5SAlexey Budankov if (c == '\n' || c == '\0')
2006ec886bf5SAlexey Budankov break;
2007ec886bf5SAlexey Budankov cmd_data[bytes_read++] = c;
2008ec886bf5SAlexey Budankov if (bytes_read == data_size)
2009ec886bf5SAlexey Budankov break;
201040db8ff5SAdrian Hunter continue;
201140db8ff5SAdrian Hunter } else if (err == -1) {
201240db8ff5SAdrian Hunter if (errno == EINTR)
201340db8ff5SAdrian Hunter continue;
201440db8ff5SAdrian Hunter if (errno == EAGAIN || errno == EWOULDBLOCK)
201540db8ff5SAdrian Hunter err = 0;
201640db8ff5SAdrian Hunter else
2017ec886bf5SAlexey Budankov pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
2018ec886bf5SAlexey Budankov }
201940db8ff5SAdrian Hunter break;
2020ec886bf5SAlexey Budankov } while (1);
2021ec886bf5SAlexey Budankov
2022ec886bf5SAlexey Budankov pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
2023ec886bf5SAlexey Budankov bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
2024ec886bf5SAlexey Budankov
202540db8ff5SAdrian Hunter if (bytes_read > 0) {
2026ec886bf5SAlexey Budankov if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
2027ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
2028ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_ENABLE;
2029ec886bf5SAlexey Budankov } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
2030ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
2031ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_DISABLE;
2032d20aff15SAdrian Hunter } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2033d20aff15SAdrian Hunter (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2034d20aff15SAdrian Hunter *cmd = EVLIST_CTL_CMD_SNAPSHOT;
2035d20aff15SAdrian Hunter pr_debug("is snapshot\n");
2036142544a9SJiri Olsa } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2037142544a9SJiri Olsa (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2038142544a9SJiri Olsa *cmd = EVLIST_CTL_CMD_EVLIST;
2039f186cd61SJiri Olsa } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2040f186cd61SJiri Olsa (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2041f186cd61SJiri Olsa *cmd = EVLIST_CTL_CMD_STOP;
204247fddcb4SJiri Olsa } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
204347fddcb4SJiri Olsa (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
204447fddcb4SJiri Olsa *cmd = EVLIST_CTL_CMD_PING;
2045ec886bf5SAlexey Budankov }
2046ec886bf5SAlexey Budankov }
2047ec886bf5SAlexey Budankov
204840db8ff5SAdrian Hunter return bytes_read ? (int)bytes_read : err;
2049ec886bf5SAlexey Budankov }
2050ec886bf5SAlexey Budankov
evlist__ctlfd_ack(struct evlist * evlist)2051d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
2052ec886bf5SAlexey Budankov {
2053ec886bf5SAlexey Budankov int err;
2054ec886bf5SAlexey Budankov
2055ec886bf5SAlexey Budankov if (evlist->ctl_fd.ack == -1)
2056ec886bf5SAlexey Budankov return 0;
2057ec886bf5SAlexey Budankov
2058ec886bf5SAlexey Budankov err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2059ec886bf5SAlexey Budankov sizeof(EVLIST_CTL_CMD_ACK_TAG));
2060ec886bf5SAlexey Budankov if (err == -1)
2061ec886bf5SAlexey Budankov pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2062ec886bf5SAlexey Budankov
2063ec886bf5SAlexey Budankov return err;
2064ec886bf5SAlexey Budankov }
2065ec886bf5SAlexey Budankov
get_cmd_arg(char * cmd_data,size_t cmd_size,char ** arg)2066991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2067991ae4ebSJiri Olsa {
2068991ae4ebSJiri Olsa char *data = cmd_data + cmd_size;
2069991ae4ebSJiri Olsa
2070991ae4ebSJiri Olsa /* no argument */
2071991ae4ebSJiri Olsa if (!*data)
2072991ae4ebSJiri Olsa return 0;
2073991ae4ebSJiri Olsa
2074991ae4ebSJiri Olsa /* there's argument */
2075991ae4ebSJiri Olsa if (*data == ' ') {
2076991ae4ebSJiri Olsa *arg = data + 1;
2077991ae4ebSJiri Olsa return 1;
2078991ae4ebSJiri Olsa }
2079991ae4ebSJiri Olsa
2080991ae4ebSJiri Olsa /* malformed */
2081991ae4ebSJiri Olsa return -1;
2082991ae4ebSJiri Olsa }
2083991ae4ebSJiri Olsa
evlist__ctlfd_enable(struct evlist * evlist,char * cmd_data,bool enable)2084991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2085991ae4ebSJiri Olsa {
2086991ae4ebSJiri Olsa struct evsel *evsel;
2087991ae4ebSJiri Olsa char *name;
2088991ae4ebSJiri Olsa int err;
2089991ae4ebSJiri Olsa
2090991ae4ebSJiri Olsa err = get_cmd_arg(cmd_data,
2091991ae4ebSJiri Olsa enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2092991ae4ebSJiri Olsa sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2093991ae4ebSJiri Olsa &name);
2094991ae4ebSJiri Olsa if (err < 0) {
2095991ae4ebSJiri Olsa pr_info("failed: wrong command\n");
2096991ae4ebSJiri Olsa return -1;
2097991ae4ebSJiri Olsa }
2098991ae4ebSJiri Olsa
2099991ae4ebSJiri Olsa if (err) {
2100991ae4ebSJiri Olsa evsel = evlist__find_evsel_by_str(evlist, name);
2101991ae4ebSJiri Olsa if (evsel) {
2102991ae4ebSJiri Olsa if (enable)
2103991ae4ebSJiri Olsa evlist__enable_evsel(evlist, name);
2104991ae4ebSJiri Olsa else
2105991ae4ebSJiri Olsa evlist__disable_evsel(evlist, name);
2106991ae4ebSJiri Olsa pr_info("Event %s %s\n", evsel->name,
2107991ae4ebSJiri Olsa enable ? "enabled" : "disabled");
2108991ae4ebSJiri Olsa } else {
2109991ae4ebSJiri Olsa pr_info("failed: can't find '%s' event\n", name);
2110991ae4ebSJiri Olsa }
2111991ae4ebSJiri Olsa } else {
2112991ae4ebSJiri Olsa if (enable) {
2113991ae4ebSJiri Olsa evlist__enable(evlist);
2114991ae4ebSJiri Olsa pr_info(EVLIST_ENABLED_MSG);
2115991ae4ebSJiri Olsa } else {
2116991ae4ebSJiri Olsa evlist__disable(evlist);
2117991ae4ebSJiri Olsa pr_info(EVLIST_DISABLED_MSG);
2118991ae4ebSJiri Olsa }
2119991ae4ebSJiri Olsa }
2120991ae4ebSJiri Olsa
2121991ae4ebSJiri Olsa return 0;
2122991ae4ebSJiri Olsa }
2123991ae4ebSJiri Olsa
evlist__ctlfd_list(struct evlist * evlist,char * cmd_data)2124142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2125142544a9SJiri Olsa {
2126142544a9SJiri Olsa struct perf_attr_details details = { .verbose = false, };
2127142544a9SJiri Olsa struct evsel *evsel;
2128142544a9SJiri Olsa char *arg;
2129142544a9SJiri Olsa int err;
2130142544a9SJiri Olsa
2131142544a9SJiri Olsa err = get_cmd_arg(cmd_data,
2132142544a9SJiri Olsa sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2133142544a9SJiri Olsa &arg);
2134142544a9SJiri Olsa if (err < 0) {
2135142544a9SJiri Olsa pr_info("failed: wrong command\n");
2136142544a9SJiri Olsa return -1;
2137142544a9SJiri Olsa }
2138142544a9SJiri Olsa
2139142544a9SJiri Olsa if (err) {
2140142544a9SJiri Olsa if (!strcmp(arg, "-v")) {
2141142544a9SJiri Olsa details.verbose = true;
2142142544a9SJiri Olsa } else if (!strcmp(arg, "-g")) {
2143142544a9SJiri Olsa details.event_group = true;
2144142544a9SJiri Olsa } else if (!strcmp(arg, "-F")) {
2145142544a9SJiri Olsa details.freq = true;
2146142544a9SJiri Olsa } else {
2147142544a9SJiri Olsa pr_info("failed: wrong command\n");
2148142544a9SJiri Olsa return -1;
2149142544a9SJiri Olsa }
2150142544a9SJiri Olsa }
2151142544a9SJiri Olsa
2152142544a9SJiri Olsa evlist__for_each_entry(evlist, evsel)
2153142544a9SJiri Olsa evsel__fprintf(evsel, &details, stderr);
2154142544a9SJiri Olsa
2155142544a9SJiri Olsa return 0;
2156142544a9SJiri Olsa }
2157142544a9SJiri Olsa
evlist__ctlfd_process(struct evlist * evlist,enum evlist_ctl_cmd * cmd)2158ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2159ec886bf5SAlexey Budankov {
2160ec886bf5SAlexey Budankov int err = 0;
2161ec886bf5SAlexey Budankov char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2162ec886bf5SAlexey Budankov int ctlfd_pos = evlist->ctl_fd.pos;
2163ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries;
2164ec886bf5SAlexey Budankov
2165ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2166ec886bf5SAlexey Budankov return 0;
2167ec886bf5SAlexey Budankov
2168ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & POLLIN) {
2169ec886bf5SAlexey Budankov err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2170ec886bf5SAlexey Budankov EVLIST_CTL_CMD_MAX_LEN);
2171ec886bf5SAlexey Budankov if (err > 0) {
2172ec886bf5SAlexey Budankov switch (*cmd) {
2173ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ENABLE:
2174ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_DISABLE:
2175991ae4ebSJiri Olsa err = evlist__ctlfd_enable(evlist, cmd_data,
2176991ae4ebSJiri Olsa *cmd == EVLIST_CTL_CMD_ENABLE);
2177ec886bf5SAlexey Budankov break;
2178142544a9SJiri Olsa case EVLIST_CTL_CMD_EVLIST:
2179142544a9SJiri Olsa err = evlist__ctlfd_list(evlist, cmd_data);
2180142544a9SJiri Olsa break;
2181d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT:
2182f186cd61SJiri Olsa case EVLIST_CTL_CMD_STOP:
218347fddcb4SJiri Olsa case EVLIST_CTL_CMD_PING:
2184d20aff15SAdrian Hunter break;
2185ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ACK:
2186ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED:
2187ec886bf5SAlexey Budankov default:
2188ec886bf5SAlexey Budankov pr_debug("ctlfd: unsupported %d\n", *cmd);
2189ec886bf5SAlexey Budankov break;
2190ec886bf5SAlexey Budankov }
2191d20aff15SAdrian Hunter if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2192d20aff15SAdrian Hunter *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2193ec886bf5SAlexey Budankov evlist__ctlfd_ack(evlist);
2194ec886bf5SAlexey Budankov }
2195ec886bf5SAlexey Budankov }
2196ec886bf5SAlexey Budankov
2197ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2198ec886bf5SAlexey Budankov evlist__finalize_ctlfd(evlist);
2199ec886bf5SAlexey Budankov else
2200ec886bf5SAlexey Budankov entries[ctlfd_pos].revents = 0;
2201ec886bf5SAlexey Budankov
2202ec886bf5SAlexey Budankov return err;
2203ec886bf5SAlexey Budankov }
2204b214ba8cSNamhyung Kim
22056657a099SAdrian Hunter /**
22066657a099SAdrian Hunter * struct event_enable_time - perf record -D/--delay single time range.
22076657a099SAdrian Hunter * @start: start of time range to enable events in milliseconds
22086657a099SAdrian Hunter * @end: end of time range to enable events in milliseconds
22096657a099SAdrian Hunter *
22106657a099SAdrian Hunter * N.B. this structure is also accessed as an array of int.
22116657a099SAdrian Hunter */
22126657a099SAdrian Hunter struct event_enable_time {
22136657a099SAdrian Hunter int start;
22146657a099SAdrian Hunter int end;
22156657a099SAdrian Hunter };
22166657a099SAdrian Hunter
parse_event_enable_time(const char * str,struct event_enable_time * range,bool first)22176657a099SAdrian Hunter static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
22186657a099SAdrian Hunter {
22196657a099SAdrian Hunter const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
22206657a099SAdrian Hunter int ret, start, end, n;
22216657a099SAdrian Hunter
22226657a099SAdrian Hunter ret = sscanf(str, fmt, &start, &end, &n);
22236657a099SAdrian Hunter if (ret != 2 || end <= start)
22246657a099SAdrian Hunter return -EINVAL;
22256657a099SAdrian Hunter if (range) {
22266657a099SAdrian Hunter range->start = start;
22276657a099SAdrian Hunter range->end = end;
22286657a099SAdrian Hunter }
22296657a099SAdrian Hunter return n;
22306657a099SAdrian Hunter }
22316657a099SAdrian Hunter
parse_event_enable_times(const char * str,struct event_enable_time * range)22326657a099SAdrian Hunter static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
22336657a099SAdrian Hunter {
22346657a099SAdrian Hunter int incr = !!range;
22356657a099SAdrian Hunter bool first = true;
22366657a099SAdrian Hunter ssize_t ret, cnt;
22376657a099SAdrian Hunter
22386657a099SAdrian Hunter for (cnt = 0; *str; cnt++) {
22396657a099SAdrian Hunter ret = parse_event_enable_time(str, range, first);
22406657a099SAdrian Hunter if (ret < 0)
22416657a099SAdrian Hunter return ret;
22426657a099SAdrian Hunter /* Check no overlap */
22436657a099SAdrian Hunter if (!first && range && range->start <= range[-1].end)
22446657a099SAdrian Hunter return -EINVAL;
22456657a099SAdrian Hunter str += ret;
22466657a099SAdrian Hunter range += incr;
22476657a099SAdrian Hunter first = false;
22486657a099SAdrian Hunter }
22496657a099SAdrian Hunter return cnt;
22506657a099SAdrian Hunter }
22516657a099SAdrian Hunter
22526657a099SAdrian Hunter /**
22536657a099SAdrian Hunter * struct event_enable_timer - control structure for perf record -D/--delay.
22546657a099SAdrian Hunter * @evlist: event list
22556657a099SAdrian Hunter * @times: time ranges that events are enabled (N.B. this is also accessed as an
22566657a099SAdrian Hunter * array of int)
22576657a099SAdrian Hunter * @times_cnt: number of time ranges
22586657a099SAdrian Hunter * @timerfd: timer file descriptor
22596657a099SAdrian Hunter * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
22606657a099SAdrian Hunter * @times_step: current position in (int *)@times)[],
22616657a099SAdrian Hunter * refer event_enable_timer__process()
22626657a099SAdrian Hunter *
22636657a099SAdrian Hunter * Note, this structure is only used when there are time ranges, not when there
22646657a099SAdrian Hunter * is only an initial delay.
22656657a099SAdrian Hunter */
22666657a099SAdrian Hunter struct event_enable_timer {
22676657a099SAdrian Hunter struct evlist *evlist;
22686657a099SAdrian Hunter struct event_enable_time *times;
22696657a099SAdrian Hunter size_t times_cnt;
22706657a099SAdrian Hunter int timerfd;
22716657a099SAdrian Hunter int pollfd_pos;
22726657a099SAdrian Hunter size_t times_step;
22736657a099SAdrian Hunter };
22746657a099SAdrian Hunter
str_to_delay(const char * str)22756657a099SAdrian Hunter static int str_to_delay(const char *str)
22766657a099SAdrian Hunter {
22776657a099SAdrian Hunter char *endptr;
22786657a099SAdrian Hunter long d;
22796657a099SAdrian Hunter
22806657a099SAdrian Hunter d = strtol(str, &endptr, 10);
22816657a099SAdrian Hunter if (*endptr || d > INT_MAX || d < -1)
22826657a099SAdrian Hunter return 0;
22836657a099SAdrian Hunter return d;
22846657a099SAdrian Hunter }
22856657a099SAdrian Hunter
evlist__parse_event_enable_time(struct evlist * evlist,struct record_opts * opts,const char * str,int unset)22866657a099SAdrian Hunter int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
22876657a099SAdrian Hunter const char *str, int unset)
22886657a099SAdrian Hunter {
22896657a099SAdrian Hunter enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
22906657a099SAdrian Hunter struct event_enable_timer *eet;
22916657a099SAdrian Hunter ssize_t times_cnt;
22926657a099SAdrian Hunter ssize_t ret;
22936657a099SAdrian Hunter int err;
22946657a099SAdrian Hunter
22956657a099SAdrian Hunter if (unset)
22966657a099SAdrian Hunter return 0;
22976657a099SAdrian Hunter
2298cb4b9e68SChangbin Du opts->target.initial_delay = str_to_delay(str);
2299cb4b9e68SChangbin Du if (opts->target.initial_delay)
23006657a099SAdrian Hunter return 0;
23016657a099SAdrian Hunter
23026657a099SAdrian Hunter ret = parse_event_enable_times(str, NULL);
23036657a099SAdrian Hunter if (ret < 0)
23046657a099SAdrian Hunter return ret;
23056657a099SAdrian Hunter
23066657a099SAdrian Hunter times_cnt = ret;
23076657a099SAdrian Hunter if (times_cnt == 0)
23086657a099SAdrian Hunter return -EINVAL;
23096657a099SAdrian Hunter
23106657a099SAdrian Hunter eet = zalloc(sizeof(*eet));
23116657a099SAdrian Hunter if (!eet)
23126657a099SAdrian Hunter return -ENOMEM;
23136657a099SAdrian Hunter
23146657a099SAdrian Hunter eet->times = calloc(times_cnt, sizeof(*eet->times));
23156657a099SAdrian Hunter if (!eet->times) {
23166657a099SAdrian Hunter err = -ENOMEM;
23176657a099SAdrian Hunter goto free_eet;
23186657a099SAdrian Hunter }
23196657a099SAdrian Hunter
23206657a099SAdrian Hunter if (parse_event_enable_times(str, eet->times) != times_cnt) {
23216657a099SAdrian Hunter err = -EINVAL;
23226657a099SAdrian Hunter goto free_eet_times;
23236657a099SAdrian Hunter }
23246657a099SAdrian Hunter
23256657a099SAdrian Hunter eet->times_cnt = times_cnt;
23266657a099SAdrian Hunter
23276657a099SAdrian Hunter eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
23286657a099SAdrian Hunter if (eet->timerfd == -1) {
23296657a099SAdrian Hunter err = -errno;
23306657a099SAdrian Hunter pr_err("timerfd_create failed: %s\n", strerror(errno));
23316657a099SAdrian Hunter goto free_eet_times;
23326657a099SAdrian Hunter }
23336657a099SAdrian Hunter
23346657a099SAdrian Hunter eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
23356657a099SAdrian Hunter if (eet->pollfd_pos < 0) {
23366657a099SAdrian Hunter err = eet->pollfd_pos;
23376657a099SAdrian Hunter goto close_timerfd;
23386657a099SAdrian Hunter }
23396657a099SAdrian Hunter
23406657a099SAdrian Hunter eet->evlist = evlist;
23416657a099SAdrian Hunter evlist->eet = eet;
2342cb4b9e68SChangbin Du opts->target.initial_delay = eet->times[0].start;
23436657a099SAdrian Hunter
23446657a099SAdrian Hunter return 0;
23456657a099SAdrian Hunter
23466657a099SAdrian Hunter close_timerfd:
23476657a099SAdrian Hunter close(eet->timerfd);
23486657a099SAdrian Hunter free_eet_times:
23492bfc8134SArnaldo Carvalho de Melo zfree(&eet->times);
23506657a099SAdrian Hunter free_eet:
23516657a099SAdrian Hunter free(eet);
23526657a099SAdrian Hunter return err;
23536657a099SAdrian Hunter }
23546657a099SAdrian Hunter
event_enable_timer__set_timer(struct event_enable_timer * eet,int ms)23556657a099SAdrian Hunter static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
23566657a099SAdrian Hunter {
23576657a099SAdrian Hunter struct itimerspec its = {
23586657a099SAdrian Hunter .it_value.tv_sec = ms / MSEC_PER_SEC,
23596657a099SAdrian Hunter .it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
23606657a099SAdrian Hunter };
23616657a099SAdrian Hunter int err = 0;
23626657a099SAdrian Hunter
23636657a099SAdrian Hunter if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
23646657a099SAdrian Hunter err = -errno;
23656657a099SAdrian Hunter pr_err("timerfd_settime failed: %s\n", strerror(errno));
23666657a099SAdrian Hunter }
23676657a099SAdrian Hunter return err;
23686657a099SAdrian Hunter }
23696657a099SAdrian Hunter
event_enable_timer__start(struct event_enable_timer * eet)23706657a099SAdrian Hunter int event_enable_timer__start(struct event_enable_timer *eet)
23716657a099SAdrian Hunter {
23726657a099SAdrian Hunter int ms;
23736657a099SAdrian Hunter
23746657a099SAdrian Hunter if (!eet)
23756657a099SAdrian Hunter return 0;
23766657a099SAdrian Hunter
23776657a099SAdrian Hunter ms = eet->times[0].end - eet->times[0].start;
23786657a099SAdrian Hunter eet->times_step = 1;
23796657a099SAdrian Hunter
23806657a099SAdrian Hunter return event_enable_timer__set_timer(eet, ms);
23816657a099SAdrian Hunter }
23826657a099SAdrian Hunter
event_enable_timer__process(struct event_enable_timer * eet)23836657a099SAdrian Hunter int event_enable_timer__process(struct event_enable_timer *eet)
23846657a099SAdrian Hunter {
23856657a099SAdrian Hunter struct pollfd *entries;
23866657a099SAdrian Hunter short revents;
23876657a099SAdrian Hunter
23886657a099SAdrian Hunter if (!eet)
23896657a099SAdrian Hunter return 0;
23906657a099SAdrian Hunter
23916657a099SAdrian Hunter entries = eet->evlist->core.pollfd.entries;
23926657a099SAdrian Hunter revents = entries[eet->pollfd_pos].revents;
23936657a099SAdrian Hunter entries[eet->pollfd_pos].revents = 0;
23946657a099SAdrian Hunter
23956657a099SAdrian Hunter if (revents & POLLIN) {
23966657a099SAdrian Hunter size_t step = eet->times_step;
23976657a099SAdrian Hunter size_t pos = step / 2;
23986657a099SAdrian Hunter
23996657a099SAdrian Hunter if (step & 1) {
24006657a099SAdrian Hunter evlist__disable_non_dummy(eet->evlist);
24016657a099SAdrian Hunter pr_info(EVLIST_DISABLED_MSG);
24026657a099SAdrian Hunter if (pos >= eet->times_cnt - 1) {
24036657a099SAdrian Hunter /* Disarm timer */
24046657a099SAdrian Hunter event_enable_timer__set_timer(eet, 0);
24056657a099SAdrian Hunter return 1; /* Stop */
24066657a099SAdrian Hunter }
24076657a099SAdrian Hunter } else {
24086657a099SAdrian Hunter evlist__enable_non_dummy(eet->evlist);
24096657a099SAdrian Hunter pr_info(EVLIST_ENABLED_MSG);
24106657a099SAdrian Hunter }
24116657a099SAdrian Hunter
24126657a099SAdrian Hunter step += 1;
24136657a099SAdrian Hunter pos = step / 2;
24146657a099SAdrian Hunter
24156657a099SAdrian Hunter if (pos < eet->times_cnt) {
24166657a099SAdrian Hunter int *times = (int *)eet->times; /* Accessing 'times' as array of int */
24176657a099SAdrian Hunter int ms = times[step] - times[step - 1];
24186657a099SAdrian Hunter
24196657a099SAdrian Hunter eet->times_step = step;
24206657a099SAdrian Hunter return event_enable_timer__set_timer(eet, ms);
24216657a099SAdrian Hunter }
24226657a099SAdrian Hunter }
24236657a099SAdrian Hunter
24246657a099SAdrian Hunter return 0;
24256657a099SAdrian Hunter }
24266657a099SAdrian Hunter
event_enable_timer__exit(struct event_enable_timer ** ep)24276657a099SAdrian Hunter void event_enable_timer__exit(struct event_enable_timer **ep)
24286657a099SAdrian Hunter {
24296657a099SAdrian Hunter if (!ep || !*ep)
24306657a099SAdrian Hunter return;
24312bfc8134SArnaldo Carvalho de Melo zfree(&(*ep)->times);
24326657a099SAdrian Hunter zfree(ep);
24336657a099SAdrian Hunter }
24346657a099SAdrian Hunter
evlist__find_evsel(struct evlist * evlist,int idx)2435b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2436b214ba8cSNamhyung Kim {
2437b214ba8cSNamhyung Kim struct evsel *evsel;
2438b214ba8cSNamhyung Kim
2439b214ba8cSNamhyung Kim evlist__for_each_entry(evlist, evsel) {
244038fe0e01SJiri Olsa if (evsel->core.idx == idx)
2441b214ba8cSNamhyung Kim return evsel;
2442b214ba8cSNamhyung Kim }
2443b214ba8cSNamhyung Kim return NULL;
2444b214ba8cSNamhyung Kim }
24459865ea8aSArnaldo Carvalho de Melo
evlist__scnprintf_evsels(struct evlist * evlist,size_t size,char * bf)24469865ea8aSArnaldo Carvalho de Melo int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
24479865ea8aSArnaldo Carvalho de Melo {
24489865ea8aSArnaldo Carvalho de Melo struct evsel *evsel;
24499865ea8aSArnaldo Carvalho de Melo int printed = 0;
24509865ea8aSArnaldo Carvalho de Melo
24519865ea8aSArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) {
24529865ea8aSArnaldo Carvalho de Melo if (evsel__is_dummy_event(evsel))
24539865ea8aSArnaldo Carvalho de Melo continue;
24549865ea8aSArnaldo Carvalho de Melo if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
24559865ea8aSArnaldo Carvalho de Melo printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
24569865ea8aSArnaldo Carvalho de Melo } else {
24579865ea8aSArnaldo Carvalho de Melo printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
24589865ea8aSArnaldo Carvalho de Melo break;
24599865ea8aSArnaldo Carvalho de Melo }
24609865ea8aSArnaldo Carvalho de Melo }
24619865ea8aSArnaldo Carvalho de Melo
24629865ea8aSArnaldo Carvalho de Melo return printed;
24639865ea8aSArnaldo Carvalho de Melo }
2464d5a8bd0fSJin Yao
evlist__check_mem_load_aux(struct evlist * evlist)2465d5a8bd0fSJin Yao void evlist__check_mem_load_aux(struct evlist *evlist)
2466d5a8bd0fSJin Yao {
2467d5a8bd0fSJin Yao struct evsel *leader, *evsel, *pos;
2468d5a8bd0fSJin Yao
2469d5a8bd0fSJin Yao /*
2470d5a8bd0fSJin Yao * For some platforms, the 'mem-loads' event is required to use
2471d5a8bd0fSJin Yao * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2472d5a8bd0fSJin Yao * must be the group leader. Now we disable this group before reporting
2473d5a8bd0fSJin Yao * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2474d5a8bd0fSJin Yao * any valid memory load information.
2475d5a8bd0fSJin Yao */
2476d5a8bd0fSJin Yao evlist__for_each_entry(evlist, evsel) {
2477fba7c866SJiri Olsa leader = evsel__leader(evsel);
2478d5a8bd0fSJin Yao if (leader == evsel)
2479d5a8bd0fSJin Yao continue;
2480d5a8bd0fSJin Yao
2481d5a8bd0fSJin Yao if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2482d5a8bd0fSJin Yao for_each_group_evsel(pos, leader) {
2483fba7c866SJiri Olsa evsel__set_leader(pos, pos);
2484d5a8bd0fSJin Yao pos->core.nr_members = 0;
2485d5a8bd0fSJin Yao }
2486d5a8bd0fSJin Yao }
2487d5a8bd0fSJin Yao }
2488d5a8bd0fSJin Yao }
24895ac72634SIan Rogers
24905ac72634SIan Rogers /**
24915ac72634SIan Rogers * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs
24925ac72634SIan Rogers * and warn if the user CPU list is inapplicable for the event's PMU's
24935ac72634SIan Rogers * CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a
24945ac72634SIan Rogers * user requested CPU and so any online CPU is applicable. Core PMUs handle
24955ac72634SIan Rogers * events on the CPUs in their list and otherwise the event isn't supported.
24965ac72634SIan Rogers * @evlist: The list of events being checked.
24975ac72634SIan Rogers * @cpu_list: The user provided list of CPUs.
24985ac72634SIan Rogers */
evlist__warn_user_requested_cpus(struct evlist * evlist,const char * cpu_list)24995ac72634SIan Rogers void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list)
25005ac72634SIan Rogers {
25015ac72634SIan Rogers struct perf_cpu_map *user_requested_cpus;
25025ac72634SIan Rogers struct evsel *pos;
25035ac72634SIan Rogers
25045ac72634SIan Rogers if (!cpu_list)
25055ac72634SIan Rogers return;
25065ac72634SIan Rogers
25075ac72634SIan Rogers user_requested_cpus = perf_cpu_map__new(cpu_list);
25085ac72634SIan Rogers if (!user_requested_cpus)
25095ac72634SIan Rogers return;
25105ac72634SIan Rogers
25115ac72634SIan Rogers evlist__for_each_entry(evlist, pos) {
25125ac72634SIan Rogers struct perf_cpu_map *intersect, *to_test;
25135ac72634SIan Rogers const struct perf_pmu *pmu = evsel__find_pmu(pos);
25145ac72634SIan Rogers
25155ac72634SIan Rogers to_test = pmu && pmu->is_core ? pmu->cpus : cpu_map__online();
25165ac72634SIan Rogers intersect = perf_cpu_map__intersect(to_test, user_requested_cpus);
25175ac72634SIan Rogers if (!perf_cpu_map__equal(intersect, user_requested_cpus)) {
25185ac72634SIan Rogers char buf[128];
25195ac72634SIan Rogers
25205ac72634SIan Rogers cpu_map__snprint(to_test, buf, sizeof(buf));
25215ac72634SIan Rogers pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s' (CPUs %s) for event '%s'\n",
25225ac72634SIan Rogers cpu_list, pmu ? pmu->name : "cpu", buf, evsel__name(pos));
25235ac72634SIan Rogers }
25245ac72634SIan Rogers perf_cpu_map__put(intersect);
25255ac72634SIan Rogers }
25265ac72634SIan Rogers perf_cpu_map__put(user_requested_cpus);
25275ac72634SIan Rogers }
25281f7b3c80SKan Liang
evlist__uniquify_name(struct evlist * evlist)25291f7b3c80SKan Liang void evlist__uniquify_name(struct evlist *evlist)
25301f7b3c80SKan Liang {
25311f7b3c80SKan Liang struct evsel *pos;
25321f7b3c80SKan Liang char *new_name;
25331f7b3c80SKan Liang int ret;
25341f7b3c80SKan Liang
25351f7b3c80SKan Liang if (perf_pmus__num_core_pmus() == 1)
25361f7b3c80SKan Liang return;
25371f7b3c80SKan Liang
25381f7b3c80SKan Liang evlist__for_each_entry(evlist, pos) {
25391f7b3c80SKan Liang if (!evsel__is_hybrid(pos))
25401f7b3c80SKan Liang continue;
25411f7b3c80SKan Liang
25421f7b3c80SKan Liang if (strchr(pos->name, '/'))
25431f7b3c80SKan Liang continue;
25441f7b3c80SKan Liang
25451f7b3c80SKan Liang ret = asprintf(&new_name, "%s/%s/",
25461f7b3c80SKan Liang pos->pmu_name, pos->name);
25471f7b3c80SKan Liang if (ret) {
25481f7b3c80SKan Liang free(pos->name);
25491f7b3c80SKan Liang pos->name = new_name;
25501f7b3c80SKan Liang }
25511f7b3c80SKan Liang }
25521f7b3c80SKan Liang }
2553