Skip to content

Commit 8e7d8a2

Browse files
captain5050acmel
authored andcommitted
perf pmus: Avoid repeated sysfs scanning
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>
1 parent 9d6a1df commit 8e7d8a2

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

tools/perf/util/pmus.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
static LIST_HEAD(core_pmus);
1616
static LIST_HEAD(other_pmus);
17+
static bool read_sysfs_core_pmus;
18+
static bool read_sysfs_all_pmus;
1719

1820
void perf_pmus__destroy(void)
1921
{
@@ -29,6 +31,8 @@ void perf_pmus__destroy(void)
2931

3032
perf_pmu__delete(pmu);
3133
}
34+
read_sysfs_core_pmus = false;
35+
read_sysfs_all_pmus = false;
3236
}
3337

3438
static struct perf_pmu *pmu_find(const char *name)
@@ -53,6 +57,7 @@ struct perf_pmu *perf_pmus__find(const char *name)
5357
{
5458
struct perf_pmu *pmu;
5559
int dirfd;
60+
bool core_pmu;
5661

5762
/*
5863
* Once PMU is loaded it stays in the list,
@@ -63,8 +68,15 @@ struct perf_pmu *perf_pmus__find(const char *name)
6368
if (pmu)
6469
return pmu;
6570

71+
if (read_sysfs_all_pmus)
72+
return NULL;
73+
74+
core_pmu = is_pmu_core(name);
75+
if (core_pmu && read_sysfs_core_pmus)
76+
return NULL;
77+
6678
dirfd = perf_pmu__event_source_devices_fd();
67-
pmu = perf_pmu__lookup(is_pmu_core(name) ? &core_pmus : &other_pmus, dirfd, name);
79+
pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
6880
close(dirfd);
6981

7082
return pmu;
@@ -73,6 +85,7 @@ struct perf_pmu *perf_pmus__find(const char *name)
7385
static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
7486
{
7587
struct perf_pmu *pmu;
88+
bool core_pmu;
7689

7790
/*
7891
* 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)
8396
if (pmu)
8497
return pmu;
8598

86-
return perf_pmu__lookup(is_pmu_core(name) ? &core_pmus : &other_pmus, dirfd, name);
99+
if (read_sysfs_all_pmus)
100+
return NULL;
101+
102+
core_pmu = is_pmu_core(name);
103+
if (core_pmu && read_sysfs_core_pmus)
104+
return NULL;
105+
106+
return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
87107
}
88108

89109
/* Add all pmus in sysfs to pmu list: */
@@ -93,6 +113,9 @@ static void pmu_read_sysfs(bool core_only)
93113
DIR *dir;
94114
struct dirent *dent;
95115

116+
if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus))
117+
return;
118+
96119
fd = perf_pmu__event_source_devices_fd();
97120
if (fd < 0)
98121
return;
@@ -111,6 +134,12 @@ static void pmu_read_sysfs(bool core_only)
111134
}
112135

113136
closedir(dir);
137+
if (core_only) {
138+
read_sysfs_core_pmus = true;
139+
} else {
140+
read_sysfs_core_pmus = true;
141+
read_sysfs_all_pmus = true;
142+
}
114143
}
115144

116145
struct perf_pmu *perf_pmus__find_by_type(unsigned int type)

0 commit comments

Comments
 (0)