diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c index 1c28c7cc06caac79e555c4241e39a8d5b4a7b2d7..742ae0a7f28eabcb9c1410bee07eb12f809140e7 100644 --- a/tools/lib/perf/evsel.c +++ b/tools/lib/perf/evsel.c @@ -204,11 +204,15 @@ static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ioc, void *arg, int cpu) { - int thread; + int thread, err; for (thread = 0; thread < xyarray__max_y(evsel->fd); thread++) { - int fd = FD(evsel, cpu, thread), - err = ioctl(fd, ioc, arg); + int fd = FD(evsel, cpu, thread); + + if (fd < 0) + return -1; + + err = ioctl(fd, ioc, arg); if (err) return err; diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 5782ca279fc439b0be23453d3f67672fe4037b51..7c16cbf7f95ab48a0e72f6dcd4648612c4927cb7 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -2090,6 +2090,7 @@ int cmd_stat(int argc, const char **argv) FILE *output = stderr; unsigned int interval, timeout; const char * const stat_subcommands[] = { "record", "report" }; + struct evsel *evsel; setlocale(LC_ALL, ""); @@ -2376,6 +2377,10 @@ int cmd_stat(int argc, const char **argv) if (evlist__initialize_ctlfd(evsel_list, stat_config.ctl_fd, stat_config.ctl_fd_ack)) goto out; + /* Enable ignoring missing threads when -p option is defined. */ + evlist__for_each_entry(evsel_list, evsel) { + evsel->ignore_missing_thread = target.pid; + } status = 0; for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) { if (stat_config.run_count != 1 && verbose > 0) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 073682703fcc82dd462844de320f79e9c6a4cc87..7cf22ae199b85840490f2b3eb964a8339739d361 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1605,9 +1605,11 @@ static int get_group_fd(struct evsel *evsel, int cpu, int thread) static void evsel__remove_fd(struct evsel *pos, int nr_cpus, int nr_threads, int thread_idx) { - for (int cpu = 0; cpu < nr_cpus; cpu++) + for (int cpu = 0; cpu < nr_cpus; cpu++) { for (int thread = thread_idx; thread < nr_threads - 1; thread++) FD(pos, cpu, thread) = FD(pos, cpu, thread + 1); + FD(pos, cpu, nr_threads - 1) = -1; + } } static int update_fds(struct evsel *evsel, @@ -2820,6 +2822,8 @@ static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist) for (thread = 0; thread < xyarray__max_y(evsel->core.fd); thread++) { int fd = FD(evsel, cpu, thread); + if (fd < 0) + continue; if (perf_evlist__id_add_fd(&evlist->core, &evsel->core, cpu, thread, fd) < 0)