OSDN Git Service

perf pmus: Avoid repeated sysfs scanning
authorIan Rogers <irogers@google.com>
Sat, 27 May 2023 07:22:06 +0000 (00:22 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sat, 27 May 2023 12:42:12 +0000 (09:42 -0300)
perf_pmus__scan will process every directory in sysfs to see if it is
a PMU, attempting to add it if not already in the pmus list. Add two
booleans to record whether this scanning has been done for core or all
PMUs. Skip scanning in the event that scanning has already occurred.

Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ali Saidi <alisaidi@amazon.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kang Minchul <tegongkang@gmail.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Ming Wang <wangming01@loongson.cn>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Rob Herring <robh@kernel.org>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20230527072210.2900565-31-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/pmus.c

index de7fc36..2c51234 100644 (file)
@@ -14,6 +14,8 @@
 
 static LIST_HEAD(core_pmus);
 static LIST_HEAD(other_pmus);
+static bool read_sysfs_core_pmus;
+static bool read_sysfs_all_pmus;
 
 void perf_pmus__destroy(void)
 {
@@ -29,6 +31,8 @@ void perf_pmus__destroy(void)
 
                perf_pmu__delete(pmu);
        }
+       read_sysfs_core_pmus = false;
+       read_sysfs_all_pmus = false;
 }
 
 static struct perf_pmu *pmu_find(const char *name)
@@ -53,6 +57,7 @@ struct perf_pmu *perf_pmus__find(const char *name)
 {
        struct perf_pmu *pmu;
        int dirfd;
+       bool core_pmu;
 
        /*
         * Once PMU is loaded it stays in the list,
@@ -63,8 +68,15 @@ struct perf_pmu *perf_pmus__find(const char *name)
        if (pmu)
                return pmu;
 
+       if (read_sysfs_all_pmus)
+               return NULL;
+
+       core_pmu = is_pmu_core(name);
+       if (core_pmu && read_sysfs_core_pmus)
+               return NULL;
+
        dirfd = perf_pmu__event_source_devices_fd();
-       pmu = perf_pmu__lookup(is_pmu_core(name) ? &core_pmus : &other_pmus, dirfd, name);
+       pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
        close(dirfd);
 
        return pmu;
@@ -73,6 +85,7 @@ struct perf_pmu *perf_pmus__find(const char *name)
 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
 {
        struct perf_pmu *pmu;
+       bool core_pmu;
 
        /*
         * Once PMU is loaded it stays in the list,
@@ -83,7 +96,14 @@ static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
        if (pmu)
                return pmu;
 
-       return perf_pmu__lookup(is_pmu_core(name) ? &core_pmus : &other_pmus, dirfd, name);
+       if (read_sysfs_all_pmus)
+               return NULL;
+
+       core_pmu = is_pmu_core(name);
+       if (core_pmu && read_sysfs_core_pmus)
+               return NULL;
+
+       return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
 }
 
 /* Add all pmus in sysfs to pmu list: */
@@ -93,6 +113,9 @@ static void pmu_read_sysfs(bool core_only)
        DIR *dir;
        struct dirent *dent;
 
+       if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus))
+               return;
+
        fd = perf_pmu__event_source_devices_fd();
        if (fd < 0)
                return;
@@ -111,6 +134,12 @@ static void pmu_read_sysfs(bool core_only)
        }
 
        closedir(dir);
+       if (core_only) {
+               read_sysfs_core_pmus = true;
+       } else {
+               read_sysfs_core_pmus = true;
+               read_sysfs_all_pmus = true;
+       }
 }
 
 struct perf_pmu *perf_pmus__find_by_type(unsigned int type)