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: [PATCHSET 0/9] perf tools: Update pmu scan using openat() (v1)
Date: Fri, 31 Mar 2023 13:29:40 -0700 [thread overview]
Message-ID: <20230331202949.810326-1-namhyung@kernel.org> (raw)
Hello,
This patchset changes PMU info scanning on sysfs using openat()
basically. I got reports of occasional contention on the
opening files in sysfs. While the root cause was a separate
issue, I discovered some inefficiencies in the perf code.
To scan PMUs, it roughly does something like below:
dir = opendir("/sys/bus/event_source/devices");
while (dentry = readdir(dir)) {
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "%s/%s",
"/sys/bus/event_source/devices", dentry->d_name);
fd = open(buf, O_RDONLY);
...
}
But this is not good since it needs to copy the string to build the
absolute pathname, and it makes redundant pathname walk (from the /sys)
in the kernel unnecessarily. We can use openat(2) to open the file in
the given directory.
Add a couple of new helper to return the file descriptor of PMU
directory so that it can use it with relative paths.
* perf_pmu__event_source_devices_fd()
- returns a fd for the PMU root ("/sys/bus/event_source/devices")
* perf_pmu__pathname_fd()
- returns a fd for "<pmu>/<file>" under the PMU root
Now the above code can be converted something like below:
dirfd = perf_pmu__event_source_devices_fd();
dir = fdopendir(dirfd);
while (dentry = readdir(dir)) {
fd = openat(dirfd, dentry->d_name, O_RDONLY);
...
}
I added a benchmark for pmu-scan and it showed a slight speedup
in the normal case too.
$ ./perf.old bench internals pmu-scan
# Running 'internals/pmu-scan' benchmark:
Computing performance of sysfs PMU event scan for 100 times
Average PMU scanning took: 6670.970 usec (+- 13.022 usec)
$ ./perf.new bench internals pmu-scan
# Running 'internals/pmu-scan' benchmark:
Computing performance of sysfs PMU event scan for 100 times
Average PMU scanning took: 6296.980 usec (+- 14.891 usec)
The 5~6% of improvement might be small but it may have bigger impact
when the system is contended.
You can get the code from 'perf/pmu-scan-v1' branch in
git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git
Thanks,
Namhyung
Namhyung Kim (9):
perf list: Use relative path for tracepoint scan
perf tools: Fix a asan issue in parse_events_multi_pmu_add()
perf pmu: Add perf_pmu__destroy() function
perf bench: Add pmu-scan benchmark
perf pmu: Use relative path for sysfs scan
perf pmu: Use relative path in perf_pmu__caps_parse()
perf pmu: Use relative path in setup_pmu_alias_list()
perf pmu: Add perf_pmu__{open,scan}_file_at()
perf intel-pt: Use perf_pmu__scan_file_at() if possible
tools/perf/arch/x86/util/intel-pt.c | 52 ++++--
tools/perf/arch/x86/util/pmu.c | 13 +-
tools/perf/bench/Build | 1 +
tools/perf/bench/bench.h | 1 +
tools/perf/bench/pmu-scan.c | 184 ++++++++++++++++++
tools/perf/builtin-bench.c | 1 +
tools/perf/tests/pmu.c | 9 +-
tools/perf/util/parse-events.c | 2 +-
tools/perf/util/pmu.c | 278 ++++++++++++++++++++--------
tools/perf/util/pmu.h | 12 +-
tools/perf/util/print-events.c | 26 ++-
11 files changed, 466 insertions(+), 113 deletions(-)
create mode 100644 tools/perf/bench/pmu-scan.c
base-commit: 417c6adfb155f906f0441cc1034827f6e2b3c372
--
2.40.0.348.gf938b09366-goog
next reply other threads:[~2023-03-31 20:29 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-31 20:29 Namhyung Kim [this message]
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 ` [PATCH 8/9] perf pmu: Add perf_pmu__{open,scan}_file_at() Namhyung Kim
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-1-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®