From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>, Jiri Olsa <jolsa@kernel.org>
Cc: Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
linux-perf-users@vger.kernel.org,
Kan Liang <kan.liang@linux.intel.com>,
Leo Yan <leo.yan@linaro.org>
Subject: [PATCH 8/9] perf pmu: Add perf_pmu__{open,scan}_file_at()
Date: Fri, 31 Mar 2023 13:29:48 -0700 [thread overview]
Message-ID: <20230331202949.810326-9-namhyung@kernel.org> (raw)
In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org>
These two helpers will also use openat() to reduce the overhead with
relative pathnames. Convert other functions in pmu_lookup() to use
the new helpers.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
tools/perf/util/pmu.c | 57 ++++++++++++++++++++++++++++++++++---------
tools/perf/util/pmu.h | 6 ++++-
2 files changed, 50 insertions(+), 13 deletions(-)
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 0c1d87f10b23..78a407b42ad1 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -567,7 +567,7 @@ static void pmu_read_sysfs(void)
* Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
* may have a "cpus" file.
*/
-static struct perf_cpu_map *pmu_cpumask(const char *name)
+static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name)
{
struct perf_cpu_map *cpus;
const char *templates[] = {
@@ -582,10 +582,11 @@ static struct perf_cpu_map *pmu_cpumask(const char *name)
strlcpy(pmu_name, name, sizeof(pmu_name));
for (template = templates; *template; template++) {
- file = perf_pmu__open_file(&pmu, *template);
+ file = perf_pmu__open_file_at(&pmu, dirfd, *template);
if (!file)
continue;
cpus = perf_cpu_map__read(file);
+ fclose(file);
if (cpus)
return cpus;
}
@@ -593,15 +594,19 @@ static struct perf_cpu_map *pmu_cpumask(const char *name)
return NULL;
}
-static bool pmu_is_uncore(const char *name)
+static bool pmu_is_uncore(int dirfd, const char *name)
{
- char path[PATH_MAX];
+ int fd;
if (perf_pmu__hybrid_mounted(name))
return false;
- perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpumask");
- return file_available(path);
+ fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
+ if (fd < 0)
+ return false;
+
+ close(fd);
+ return true;
}
static char *pmu_id(const char *name)
@@ -853,11 +858,11 @@ pmu_find_alias_name(const char *name __maybe_unused)
return NULL;
}
-static int pmu_max_precise(struct perf_pmu *pmu)
+static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
{
int max_precise = -1;
- perf_pmu__scan_file(pmu, "caps/max_precise", "%d", &max_precise);
+ perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
return max_precise;
}
@@ -895,14 +900,14 @@ static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name)
if (!pmu)
return NULL;
- pmu->cpus = pmu_cpumask(name);
+ pmu->cpus = pmu_cpumask(dirfd, name);
pmu->name = strdup(name);
if (!pmu->name)
goto err;
/* Read type, and ensure that type value is successfully assigned (return 1) */
- if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1)
+ if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
goto err;
alias_name = pmu_find_alias_name(name);
@@ -913,10 +918,10 @@ static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name)
}
pmu->type = type;
- pmu->is_uncore = pmu_is_uncore(name);
+ pmu->is_uncore = pmu_is_uncore(dirfd, name);
if (pmu->is_uncore)
pmu->id = pmu_id(name);
- pmu->max_precise = pmu_max_precise(pmu);
+ pmu->max_precise = pmu_max_precise(dirfd, pmu);
pmu_add_cpu_aliases(&aliases, pmu);
pmu_add_sys_aliases(&aliases, pmu);
@@ -1730,6 +1735,17 @@ FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
return fopen(path, "r");
}
+FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
+{
+ int fd;
+
+ fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
+ if (fd < 0)
+ return NULL;
+
+ return fdopen(fd, "r");
+}
+
int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
...)
{
@@ -1747,6 +1763,23 @@ int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
return ret;
}
+int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
+ const char *fmt, ...)
+{
+ va_list args;
+ FILE *file;
+ int ret = EOF;
+
+ va_start(args, fmt);
+ file = perf_pmu__open_file_at(pmu, dirfd, name);
+ if (file) {
+ ret = vfscanf(file, fmt, args);
+ fclose(file);
+ }
+ va_end(args);
+ return ret;
+}
+
bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
{
char path[PATH_MAX];
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 751c7016e7b6..32c3a75bca0e 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -220,7 +220,12 @@ bool is_pmu_core(const char *name);
void print_pmu_events(const struct print_callbacks *print_cb, void *print_state);
bool pmu_have_event(const char *pname, const char *name);
+FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name);
+FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name);
+
int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, ...) __scanf(3, 4);
+int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
+ const char *fmt, ...) __scanf(4, 5);
bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name);
@@ -259,7 +264,6 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size,
const char *pmu_name, const char *filename);
int perf_pmu__event_source_devices_fd(void);
int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags);
-FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name);
void perf_pmu__destroy(void);
--
2.40.0.348.gf938b09366-goog
next prev parent reply other threads:[~2023-03-31 20:30 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-31 20:29 [PATCHSET 0/9] perf tools: Update pmu scan using openat() (v1) Namhyung Kim
2023-03-31 20:29 ` [PATCH 1/9] perf list: Use relative path for tracepoint scan Namhyung Kim
2023-04-03 21:59 ` Arnaldo Carvalho de Melo
2023-04-03 22:00 ` Arnaldo Carvalho de Melo
2023-04-04 14:10 ` Arnaldo Carvalho de Melo
2023-04-04 22:08 ` Namhyung Kim
2023-04-04 22:19 ` Arnaldo Carvalho de Melo
2023-03-31 20:29 ` [PATCH 2/9] perf tools: Fix a asan issue in parse_events_multi_pmu_add() Namhyung Kim
2023-03-31 20:29 ` [PATCH 3/9] perf pmu: Add perf_pmu__destroy() function Namhyung Kim
2023-03-31 20:29 ` [PATCH 4/9] perf bench: Add pmu-scan benchmark Namhyung Kim
2023-03-31 20:29 ` [PATCH 5/9] perf pmu: Use relative path for sysfs scan Namhyung Kim
2023-04-03 17:23 ` Ian Rogers
2023-03-31 20:29 ` [PATCH 6/9] perf pmu: Use relative path in perf_pmu__caps_parse() Namhyung Kim
2023-03-31 20:29 ` [PATCH 7/9] perf pmu: Use relative path in setup_pmu_alias_list() Namhyung Kim
2023-03-31 20:29 ` Namhyung Kim [this message]
2023-03-31 20:29 ` [PATCH 9/9] perf intel-pt: Use perf_pmu__scan_file_at() if possible Namhyung Kim
2023-04-03 4:51 ` Adrian Hunter
2023-04-03 17:28 ` [PATCHSET 0/9] perf tools: Update pmu scan using openat() (v1) Ian Rogers
2023-04-03 20:25 ` Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230331202949.810326-9-namhyung@kernel.org \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=leo.yan@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®