Removes a lot of duplicated code moving to the common
open method.

Signed-off-by: David Ahern <dsah...@gmail.com>
Cc: Arnaldo Carvalho de Melo <a...@ghostprotocols.net>
Cc: Ingo Molnar <mi...@kernel.org>
Cc: Frederic Weisbecker <fweis...@gmail.com>
Cc: Peter Zijlstra <pet...@infradead.org>
---
 tools/perf/builtin-stat.c |  103 ++++++++++-----------------------------------
 1 file changed, 22 insertions(+), 81 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index e12002b..dc9850c 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -126,55 +126,6 @@ static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
 static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
 static struct stats walltime_nsecs_stats;
 
-static int create_perf_stat_counter(struct perf_evsel *evsel,
-                                   struct perf_evsel *first,
-                                   struct perf_record_opts *opts)
-{
-       struct perf_target *target = &opts->target;
-       struct perf_event_attr *attr = &evsel->attr;
-       bool exclude_guest_missing = false;
-       int ret;
-
-       if (scale)
-               attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
-                                   PERF_FORMAT_TOTAL_TIME_RUNNING;
-
-       attr->inherit = !opts->no_inherit;
-
-retry:
-       if (exclude_guest_missing)
-               evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
-
-       if (perf_target__has_cpu(target)) {
-               ret = perf_evsel__open_per_cpu(evsel,
-                                              perf_evsel__cpus(evsel, target));
-               if (ret)
-                       goto check_ret;
-               return 0;
-       }
-
-       if (!perf_target__has_task(target) &&
-           (!opts->group || evsel == first)) {
-               attr->disabled = 1;
-               attr->enable_on_exec = 1;
-       }
-
-       ret = perf_evsel__open_per_thread(evsel, evsel_list->threads);
-       if (!ret)
-               return 0;
-       /* fall through */
-check_ret:
-       if (ret && errno == EINVAL) {
-               if (!exclude_guest_missing &&
-                   (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
-                       pr_debug("Old kernel, cannot exclude "
-                                "guest or host samples.\n");
-                       exclude_guest_missing = true;
-                       goto retry;
-               }
-       }
-       return ret;
-}
 
 /*
  * Does the counter have nsecs as a unit?
@@ -281,6 +232,7 @@ static int __run_perf_stat(int argc __maybe_unused,
        unsigned long long t0, t1;
        struct perf_evsel *counter, *first;
        struct cpu_map *cmap;
+       struct perf_target *target = &opts->target;
        int status = 0;
        int child_ready_pipe[2], go_pipe[2];
        const bool forks = (argc > 0);
@@ -324,7 +276,7 @@ static int __run_perf_stat(int argc __maybe_unused,
                        exit(-1);
                }
 
-               if (perf_target__none(&opts->target))
+               if (perf_target__none(target))
                        evsel_list->threads->map[0] = child_pid;
 
                /*
@@ -343,38 +295,27 @@ static int __run_perf_stat(int argc __maybe_unused,
        first = perf_evlist__first(evsel_list);
 
        list_for_each_entry(counter, &evsel_list->entries, node) {
-               if (create_perf_stat_counter(counter, first, opts) < 0) {
-                       /*
-                        * PPC returns ENXIO for HW counters until 2.6.37
-                        * (behavior changed with commit b0a873e).
-                        */
-                       if (errno == EINVAL || errno == ENOSYS ||
-                           errno == ENOENT || errno == EOPNOTSUPP ||
-                           errno == ENXIO) {
-                               if (verbose)
-                                       ui__warning("%s event is not supported 
by the kernel.\n",
-                                                   perf_evsel__name(counter));
-                               counter->supported = false;
-                               continue;
-                       }
-
-                       if (errno == EPERM || errno == EACCES) {
-                               error("You may not have permission to collect 
%sstats.\n"
-                                     "\t Consider tweaking"
-                                     " /proc/sys/kernel/perf_event_paranoid or 
running as root.",
-                                     opts->target.system_wide ? "system-wide " 
: "");
-                       } else {
-                               error("open_counter returned with %d (%s). "
-                                     "/bin/dmesg may provide additional 
information.\n",
-                                      errno, strerror(errno));
-                       }
-                       if (child_pid != -1)
-                               kill(child_pid, SIGTERM);
-
-                       pr_err("Not all events could be opened.\n");
-                       return -1;
+               struct perf_event_attr *attr = &counter->attr;
+
+               if (scale)
+                       attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
+                                           PERF_FORMAT_TOTAL_TIME_RUNNING;
+
+               attr->inherit = !opts->no_inherit;
+
+               if (perf_target__none(target) &&
+                   (!opts->group || counter == first)) {
+                       attr->disabled = 1;
+                       attr->enable_on_exec = 1;
                }
-               counter->supported = true;
+       }
+
+       if (perf_evlist__open_counters(evsel_list, opts, true) != 0) {
+               if (child_pid != -1)
+                       kill(child_pid, SIGTERM);
+
+               pr_err("Not all events could be opened.\n");
+               return -1;
        }
 
        if (perf_evlist__apply_filters(evsel_list)) {
-- 
1.7.10.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to