Skip to contentSkip to conversation

perf pmu: Add tool-provided NVMe PMU

PATCH New Ian Rogers 2 participants 7 messages Nov 13

Subsystems (2)
lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v3 cover perf pmu: Add tool-provided NVMe PMU

This series adds a tool-provided PMU for NVMe controllers using libnvme, following the same pattern as the existing tool, hwmon, and drm PMUs. Each discovered controller is exposed as an 'nvme_<ctrl>' PMU (with alias '<ctrl>', e.g. 'nvme_nvme0' and 'nvme0') providing events from standard NVMe log pages: SMART / Health Information, Endurance Group Information, Flexible Data Placement (FDP) Statistics, Error Information, and Zoned Namespaces (ZNS) Changed Zones.

Motivation / Use Cases
======================
While standalone tools such as nvme-cli can dump individual log pages on demand, exposing NVMe controller log metrics as a perf tool PMU allows storage metrics (host/media bytes written, write amplification and FDP efficiency, read/write command counts, controller busy time, error counters, and device temperature) to be sampled on a unified timeline alongside CPU hardware counters, kernel/block events, uncore counters, and hwmon/drm sensors in 'perf stat' (including interval mode '-I', CSV '-x', JSON '-j', and perf metric formulas), without custom wrapper scripts. Discovery and log-page probing are gated behind PERF_TOOL_PMU_TYPE_NVME_MASK so commands that do not request NVMe events incur zero overhead.

Example Output
==============
Listing available NVMe events (unsupported log pages on the controller are marked deprecated and hidden from default 'perf list'):

 $ perf list nvme

List of pre-defined events (to be used in -e or -M):

nvme:
smart_crit_comp_time
[Critical composite temperature time. Unit: nvme_nvme0]
smart_ctrl_busy_time
[Controller busy time. Unit: nvme_nvme0]
smart_data_units_read
[Data units read (in 1000s of 512-byte units). Unit: nvme_nvme0]
smart_data_units_written
[Data units written (in 1000s of 512-byte units). Unit: nvme_nvme0]
smart_host_read_commands
[Host read commands. Unit: nvme_nvme0]
smart_host_write_commands
[Host write commands. Unit: nvme_nvme0]
smart_media_errors
[Media errors. Unit: nvme_nvme0]
smart_num_err_log_entries
[Number of error log entries. Unit: nvme_nvme0]
smart_power_cycles
[Power cycles. Unit: nvme_nvme0]
smart_power_on_hours
[Power on hours. Unit: nvme_nvme0]
smart_temperature
[Temperature. Unit: nvme_nvme0]
smart_unsafe_shutdowns
[Unsafe shutdowns. Unit: nvme_nvme0]
smart_warning_temp_time
[Warning temperature time. Unit: nvme_nvme0]

 $ perf list --details nvme_nvme0/smart_temperature/

List of pre-defined events (to be used in -e or -M):

nvme:
smart_temperature
[Temperature. Unit: nvme_nvme0]
nvme_nvme0/config=0x20001/

Interval 'perf stat' output (captured with mocked libnvme SMART log responses on /dev/nvme0):

 $ perf stat -a -I 500 -e smart_temperature,smart_data_units_read,\
   smart_data_units_written,smart_host_read_commands,\
   smart_host_write_commands -- sleep 1
 #           time             counts unit events
      0.500432960                311 K    smart_temperature
      0.500432960         36,864,000 B    smart_data_units_read
      0.500432960         12,288,000 B    smart_data_units_written
      0.500432960                900      smart_host_read_commands
      0.500432960                270      smart_host_write_commands
      1.001068028                311 K    smart_temperature
      1.001068028         36,864,000 B    smart_data_units_read
      1.001068028         12,288,000 B    smart_data_units_written
      1.001068028                900      smart_host_read_commands
      1.001068028                270      smart_host_write_commands

Changes in v3:
- Remove accidental internal tracking tags from commit messages (Christoph Hellwig).
- Document use cases and include actual 'perf list' and 'perf stat' example output in the cover letter and Patch 2 commit message, and document NVMe events in Documentation/perf-list.txt (Christoph Hellwig, Namhyung Kim).
- Rely on $(FEATURE_CHECK_LDFLAGS-libnvme) from pkg-config when building test-libnvme.bin instead of hardcoding -lnvme, and add test-libnvme.bin to FILES in tools/build/feature/Makefile.
- Report smart_temperature in native Kelvins ('1K') to avoid unsigned uint64_t underflow on sub-zero Celsius or 0 K readings; metrics can convert Kelvin to Celsius.
- Support advertised 'config=0x...' raw encodings, 'event=<name>', and standard event qualifiers ('name=', 'metric-id=', 'cpu='), and guard NULL term->config when formatting parse errors.
- Check xyarray__new() allocation failure (-ENOMEM), propagate initial log read errors (-EPERM / -EOPNOTSUPP) in evsel__nvme_pmu_open(), and bound incremental open error unwind by start_cpu_map_idx.
- Use an aligned union of NVMe log structures in nvme_pmu__read_val(), use valid Endurance Group ID 1 (0 is reserved in NVMe) for Endurance Group and FDP logs, fix Endurance Group data/media unit scaling to billions of bytes ('1000000000B') per NVMe Base Spec, wire up perf_pmus__find_by_type() for NVMe PMU types, and propagate nvme_pmu__check_alias() errors in perf_pmu__check_alias().
- Expand unit tests to cover alias PMUs, 'event=', 'config=0x...', 'name=' qualifiers, unit/scale checks, and invalid term rejection.

Changes in v2:
https://lore.kernel.org/linux-perf-users/[email protected]/
- Include test-libnvme.c in test-all.c and add libnvme to FEATURE_PKG_CONFIG.
- Use get_unaligned_le*() helpers when reading NVMe log fields.
- Move baseline initial_values xyarray from tool-owned evsel->priv into struct evsel's tool union (evsel->nvme.initial_values) and free it in evsel__exit().

v1:
https://lore.kernel.org/linux-perf-users/[email protected]/

Ian Rogers (3):
perf build: Add libnvme feature detection
perf pmu: Implement tool-provided NVMe PMU
perf tests: Add NVMe PMU event parsing test

 tools/build/Makefile.feature           |   5 +-
 tools/build/feature/Makefile           |   4 +
 tools/build/feature/test-all.c         |   5 +
 tools/build/feature/test-libnvme.c     |  11 +
 tools/perf/Documentation/perf-list.txt |  10 +-
 tools/perf/Makefile.config             |  12 +
 tools/perf/tests/Build                 |   1 +
 tools/perf/tests/builtin-test.c        |   1 +
 tools/perf/tests/nvme_pmu.c            | 303 +++++++++++
 tools/perf/tests/tests.h               |   1 +
 tools/perf/util/Build                  |   1 +
 tools/perf/util/evsel.c                |  13 +-
 tools/perf/util/evsel.h                |   4 +
 tools/perf/util/nvme_pmu.c             | 691 +++++++++++++++++++++++++
 tools/perf/util/nvme_pmu.h             | 144 ++++++
 tools/perf/util/pmu.c                  |  17 +
 tools/perf/util/pmu.h                  |   8 +-
 tools/perf/util/pmus.c                 |  24 +
 tools/perf/util/pmus.h                 |   1 +
 19 files changed, 1250 insertions(+), 6 deletions(-)
 create mode 100644 tools/build/feature/test-libnvme.c
 create mode 100644 tools/perf/tests/nvme_pmu.c
 create mode 100644 tools/perf/util/nvme_pmu.c
 create mode 100644 tools/perf/util/nvme_pmu.h
Signature · 1 line
2.56.1.703.gf848ed807c-goog
author reviewer
#1 lore raw

PATCH v3 1/3 perf build: Add libnvme feature detection

Introduce a feature check for libnvme using pkg-config so that the tool-provided NVMe PMU can be conditionally compiled when libnvme is available.

Add libnvme to FEATURE_TESTS_BASIC, FEATURE_DISPLAY, FEATURE_PKG_CONFIG, and test-all.c so fast-path discovery and individual feature checks stay consistent with the pkg-config flags used during linking.

---
 tools/build/Makefile.feature       |  5 ++++-
 tools/build/feature/Makefile       |  4 ++++
 tools/build/feature/test-all.c     |  5 +++++
 tools/build/feature/test-libnvme.c | 11 +++++++++++
 tools/perf/Makefile.config         | 12 ++++++++++++
 5 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 tools/build/feature/test-libnvme.c
tools/build/Makefile.feature +4 -1
index 4d37d11f674c..54b51251c4c3 100644
@@ -73,6 +73,7 @@ FEATURE_TESTS_BASIC :=                  \
         libbfd                          \
 	libbfd-threadsafe		\
         libelf                          \
+        libnvme                         \
         libelf-getphdrnum               \
         libelf-gelf_getnote             \
         libelf-getshdrstrndx            \
@@ -143,6 +144,7 @@ FEATURE_DISPLAY ?=              \
          libdw                  \
          glibc                  \
          libelf                 \
+         libnvme                \
          libnuma                \
          numa_num_possible_cpus \
          python-module          \
@@ -168,7 +170,8 @@ FEATURE_GROUP_MEMBERS-libbfd = libbfd-liberty libbfd-liberty-z
 FEATURE_PKG_CONFIG ?=           \
 	 babeltrace2-ctf-writer \
          libtraceevent          \
-         libtracefs
+         libtracefs             \
+         libnvme
 
 feature_pkg_config = $(eval $(feature_pkg_config_code))
 define feature_pkg_config_code
tools/build/feature/Makefile +4 -0
index 10340a537224..851f458163f6 100644
@@ -24,6 +24,7 @@ FILES=                                          \
          test-cxa-demangle.bin                  \
          test-libcap.bin			\
          test-libelf.bin                        \
+         test-libnvme.bin                       \
          test-libelf-getphdrnum.bin             \
          test-libelf-gelf_getnote.bin           \
          test-libelf-getshdrstrndx.bin          \
@@ -147,6 +148,9 @@ $(OUTPUT)test-libcap.bin:
 $(OUTPUT)test-libelf.bin:
 	$(BUILD) -lelf
 
+$(OUTPUT)test-libnvme.bin:
+	$(BUILD) # -lnvme provided by $(FEATURE_CHECK_LDFLAGS-libnvme)
+
 $(OUTPUT)test-eventfd.bin:
 	$(BUILD)
 
tools/build/feature/test-all.c +5 -0
index 4a6890290f74..155f92efc2c8 100644
@@ -150,6 +150,10 @@
 # include "test-libopenssl.c"
 #undef main
 
+#define main main_test_libnvme
+# include "test-libnvme.c"
+#undef main
+
 int main(int argc, char *argv[])
 {
 	main_test_python_module();
@@ -183,6 +187,7 @@ int main(int argc, char *argv[])
 	main_test_libzstd();
 	main_test_libtraceevent();
 	main_test_libopenssl();
+	main_test_libnvme();
 
 	return 0;
 }
tools/build/feature/test-libnvme.c new file +11 -0
new file mode 100644
index 000000000000..3fefad70a680
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+#include <libnvme.h>
+
+int main(void)
+{
+	nvme_root_t r = nvme_scan(NULL);
+
+	if (r)
+		nvme_free_tree(r);
+	return 0;
+}
tools/perf/Makefile.config +12 -0
index b4764f69f47f..d82273dadf39 100644
@@ -1065,6 +1065,18 @@ ifndef NO_CAPSTONE
   endif
 endif
 
+ifndef NO_LIBNVME
+  ifeq ($(feature-libnvme), 1)
+    CFLAGS += -DHAVE_LIBNVME_SUPPORT $(shell $(PKG_CONFIG) --cflags libnvme)
+    LDFLAGS += $(shell $(PKG_CONFIG) --libs-only-L libnvme)
+    EXTLIBS += $(shell $(PKG_CONFIG) --libs-only-l libnvme)
+    $(call detected,CONFIG_LIBNVME)
+  else
+    $(warning No libnvme found, disables NVMe PMU support, please install libnvme-dev/libnvme-devel)
+    NO_LIBNVME := 1
+  endif
+endif
+
 ifdef EXTRA_TESTS
     $(call detected,CONFIG_EXTRA_TESTS)
     CFLAGS += -DHAVE_EXTRA_TESTS
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#2 lore raw

Re: [PATCH v3 1/3] perf build: Add libnvme feature detection

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

Introduce a feature check for libnvme using pkg-config so that the tool-provided NVMe PMU can be conditionally compiled when libnvme is available.

Add libnvme to FEATURE_TESTS_BASIC, FEATURE_DISPLAY, FEATURE_PKG_CONFIG, and test-all.c so fast-path discovery and individual feature checks stay consistent with the pkg-config flags used during linking.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw

PATCH v3 2/3 perf pmu: Implement tool-provided NVMe PMU

In reply to Ian Rogers

Add a tool-provided PMU for NVMe controllers using libnvme, similar to the existing hwmon and drm tool PMUs. Each discovered controller is exposed as an 'nvme_<ctrl>' PMU (with alias '<ctrl>', e.g. 'nvme_nvme0' and 'nvme0') providing events from standard NVMe log pages:
- SMART / Health Information (data units read/written, host read/write commands, controller busy time, power cycles, power on hours, unsafe shutdowns, media errors, error log entries, temperature, warning and critical temperature times)
- Endurance Group Information
- Flexible Data Placement (FDP) Statistics (host/media bytes written, media bytes erased)
- Error Information
- Zoned Namespaces (ZNS) Changed Zones

Exposing these controller log metrics as a perf tool PMU allows storage activity, write amplification / FDP efficiency, error counters, and device thermals to be measured on a unified timeline alongside CPU hardware counters, block/kernel events, uncore counters, and hwmon sensors in 'perf stat' (including interval mode '-I', CSV '-x', JSON '-j', and perf metric formulas), without separate polling scripts. As with hwmon_pmu and drm_pmu, NVMe controller discovery and log probing are gated behind PERF_TOOL_PMU_TYPE_NVME_MASK so there is no overhead unless NVMe events or wildcards are requested.

Features:
- Generic configuration encoding (log type, field size, and byte offset packed into attr.config), supporting symbolic event names, 'event=' terms, and raw 'config=0x...' encodings.
- Dynamic probing of supported log pages on /dev/nvmeX. Unsupported log events are marked deprecated and hidden from 'perf list' by default.
- Baseline offset snapshots stored in struct evsel's tool union (evsel->nvme.initial_values) so cumulative device counters report interval deltas while gauges (temperature, percent_used, zns_nrzid) report instantaneous readings.
- Temperature is reported in native Kelvins ('K') matching the NVMe SMART log specification, avoiding unsigned counter underflow on sub-zero Celsius values while allowing metrics to convert to Celsius.

Example 'perf list' output (when only the SMART log is supported):

 $ perf list nvme

List of pre-defined events (to be used in -e or -M):

nvme:
smart_crit_comp_time
[Critical composite temperature time. Unit: nvme_nvme0]
smart_ctrl_busy_time
[Controller busy time. Unit: nvme_nvme0]
smart_data_units_read
[Data units read (in 1000s of 512-byte units). Unit: nvme_nvme0]
smart_data_units_written
[Data units written (in 1000s of 512-byte units). Unit: nvme_nvme0]
smart_host_read_commands
[Host read commands. Unit: nvme_nvme0]
smart_host_write_commands
[Host write commands. Unit: nvme_nvme0]
smart_media_errors
[Media errors. Unit: nvme_nvme0]
smart_num_err_log_entries
[Number of error log entries. Unit: nvme_nvme0]
smart_power_cycles
[Power cycles. Unit: nvme_nvme0]
smart_power_on_hours
[Power on hours. Unit: nvme_nvme0]
smart_temperature
[Temperature. Unit: nvme_nvme0]
smart_unsafe_shutdowns
[Unsafe shutdowns. Unit: nvme_nvme0]
smart_warning_temp_time
[Warning temperature time. Unit: nvme_nvme0]

Example 'perf stat' interval output (captured with mocked libnvme SMART log responses on /dev/nvme0):

 $ perf stat -a -I 500 -e smart_temperature,smart_data_units_read,\
   smart_data_units_written,smart_host_read_commands,\
   smart_host_write_commands -- sleep 1
 #           time             counts unit events
      0.500432960                311 K    smart_temperature
      0.500432960         36,864,000 B    smart_data_units_read
      0.500432960         12,288,000 B    smart_data_units_written
      0.500432960                900      smart_host_read_commands
      0.500432960                270      smart_host_write_commands
      1.001068028                311 K    smart_temperature
      1.001068028         36,864,000 B    smart_data_units_read
      1.001068028         12,288,000 B    smart_data_units_written
      1.001068028                900      smart_host_read_commands
      1.001068028                270      smart_host_write_commands
---
 tools/perf/Documentation/perf-list.txt |  10 +-
 tools/perf/util/Build                  |   1 +
 tools/perf/util/evsel.c                |  13 +-
 tools/perf/util/evsel.h                |   4 +
 tools/perf/util/nvme_pmu.c             | 691 +++++++++++++++++++++++++
 tools/perf/util/nvme_pmu.h             | 144 ++++++
 tools/perf/util/pmu.c                  |  17 +
 tools/perf/util/pmu.h                  |   8 +-
 tools/perf/util/pmus.c                 |  19 +
 9 files changed, 902 insertions(+), 5 deletions(-)
 create mode 100644 tools/perf/util/nvme_pmu.c
 create mode 100644 tools/perf/util/nvme_pmu.h
tools/perf/Documentation/perf-list.txt +8 -2
index 17e582998474..520e08a6efc9 100644
@@ -254,8 +254,8 @@ For accessing trace point events perf needs to have read access to
 /sys/kernel/tracing, even when perf_event_paranoid is in a relaxed
 setting.
 
-TOOL/HWMON EVENTS
------------------
+TOOL/HWMON/NVME EVENTS
+----------------------
 
 Some events don't have an associated PMU instead reading values
 available to software without perf_event_open. As these events don't
@@ -268,6 +268,12 @@ Hwmon events provide easy access to hwmon sysfs data typically in
 /sys/class/hwmon. This information includes temperatures, fan speeds
 and energy usage.
 
+NVMe events (when built with libnvme) expose controller log page
+metrics for /dev/nvmeX devices under nvme_nvmeX PMUs. This includes
+SMART health counters and temperature (in Kelvin), endurance group
+counters, Flexible Data Placement (FDP) statistics, error counts and
+Zoned Namespace (ZNS) changed zones.
+
 
 TRACING
 -------
tools/perf/util/Build +1 -0
index 2c1f880c4c47..d7740aa06249 100644
@@ -23,6 +23,7 @@ perf-util-y += disasm.o
 perf-util-y += env.o
 perf-util-y += event.o
 perf-util-y += evlist.o
+perf-util-y += nvme_pmu.o
 perf-util-y += sideband_evlist.o
 perf-util-y += evsel.o
 perf-util-y += evsel_fprintf.o
tools/perf/util/evsel.c +11 -2
index 6d69f63b7fb1..9c98478692dc 100644
@@ -63,6 +63,7 @@
 #include "hashmap.h"
 #include "hist.h"
 #include "hwmon_pmu.h"
+#include "nvme_pmu.h"
 #include "intel-tpebs.h"
 #include "memswap.h"
 #include "off_cpu.h"
@@ -2104,6 +2105,8 @@ static void evsel__exit(struct evsel *evsel)
 		xyarray__delete(evsel->process_time.start_times);
 		xyarray__delete(evsel->process_time.accumulated_times);
 	}
+	if (evsel__is_nvme(evsel))
+		xyarray__delete(evsel->nvme.initial_values);
 }
 
 void evsel__put(struct evsel *evsel)
@@ -2308,6 +2311,8 @@ int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread)
 
 	if (evsel__is_hwmon(evsel))
 		return evsel__hwmon_pmu_read(evsel, cpu_map_idx, thread);
+	if (evsel__is_nvme(evsel))
+		return evsel__nvme_pmu_read(evsel, cpu_map_idx, thread);
 
 	if (evsel__is_drm(evsel))
 		return evsel__drm_pmu_read(evsel, cpu_map_idx, thread);
@@ -3067,8 +3072,12 @@ static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus,
 	}
 	if (evsel__is_hwmon(evsel)) {
 		err = evsel__hwmon_pmu_open(evsel, threads,
-					    start_cpu_map_idx,
-					    end_cpu_map_idx);
+					    start_cpu_map_idx, end_cpu_map_idx);
+		goto out;
+	}
+	if (evsel__is_nvme(evsel)) {
+		err = evsel__nvme_pmu_open(evsel, threads,
+					   start_cpu_map_idx, end_cpu_map_idx);
 		goto out;
 	}
 	if (evsel__is_drm(evsel)) {
tools/perf/util/evsel.h +4 -0
index 53f3641b66e1..b30b5b6251af 100644
@@ -208,6 +208,10 @@ struct evsel {
 			struct xyarray *start_times;
 			struct xyarray *accumulated_times;
 		} process_time;
+		/* For NVMe events */
+		struct {
+			struct xyarray *initial_values;
+		} nvme;
 	};
 	/* Is the tool's fd for /proc/pid/stat or /proc/stat. */
 	bool pid_stat;
tools/perf/util/nvme_pmu.c new file +691 -0

691 lines not on this page · raw message

tools/perf/util/nvme_pmu.h new file +144 -0

144 lines not on this page · raw message

tools/perf/util/pmu.c +17 -0
index 01753d5c69d1..d33a313f5604 100644
@@ -25,6 +25,7 @@
 #include "pmu.h"
 #include "drm_pmu.h"
 #include "hwmon_pmu.h"
+#include "nvme_pmu.h"
 #include "pmus.h"
 #include "tool_pmu.h"
 #include "tp_pmu.h"
@@ -1696,6 +1697,8 @@ int perf_pmu__config_terms(const struct perf_pmu *pmu,
 
 	if (perf_pmu__is_hwmon(pmu))
 		return hwmon_pmu__config_terms(pmu, attr, terms, err);
+	if (perf_pmu__is_nvme(pmu))
+		return nvme_pmu__config_terms(pmu, attr, terms, err);
 	if (perf_pmu__is_drm(pmu))
 		return drm_pmu__config_terms(pmu, attr, terms, err);
 
@@ -1860,6 +1863,12 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_
 		ret = hwmon_pmu__check_alias(head_terms, info, err);
 		goto out;
 	}
+	if (perf_pmu__is_nvme(pmu)) {
+		ret = nvme_pmu__check_alias(head_terms, info, err);
+		if (ret)
+			return ret;
+		goto out;
+	}
 	if (perf_pmu__is_drm(pmu)) {
 		ret = drm_pmu__check_alias(pmu, head_terms, info, err);
 		goto out;
@@ -2121,6 +2130,8 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
 		return tp_pmu__have_event(pmu, name);
 	if (perf_pmu__is_hwmon(pmu))
 		return hwmon_pmu__have_event(pmu, name);
+	if (perf_pmu__is_nvme(pmu))
+		return nvme_pmu__have_event(pmu, name);
 	if (perf_pmu__is_drm(pmu))
 		return drm_pmu__have_event(pmu, name);
 	if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
@@ -2142,6 +2153,8 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
 		return tp_pmu__num_events(pmu);
 	if (perf_pmu__is_hwmon(pmu))
 		return hwmon_pmu__num_events(pmu);
+	if (perf_pmu__is_nvme(pmu))
+		return nvme_pmu__num_events(pmu);
 	if (perf_pmu__is_drm(pmu))
 		return drm_pmu__num_events(pmu);
 
@@ -2278,6 +2291,8 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
 		return tp_pmu__for_each_event(pmu, state, cb);
 	if (perf_pmu__is_hwmon(pmu))
 		return hwmon_pmu__for_each_event(pmu, state, cb);
+	if (perf_pmu__is_nvme(pmu))
+		return nvme_pmu__for_each_event(pmu, state, cb);
 	if (perf_pmu__is_drm(pmu))
 		return drm_pmu__for_each_event(pmu, state, cb);
 
@@ -2786,6 +2801,8 @@ void perf_pmu__delete(struct perf_pmu *pmu)
 
 	if (perf_pmu__is_hwmon(pmu))
 		hwmon_pmu__exit(pmu);
+	else if (perf_pmu__is_nvme(pmu))
+		nvme_pmu__exit(pmu);
 	else if (perf_pmu__is_drm(pmu))
 		drm_pmu__exit(pmu);
 
tools/perf/util/pmu.h +7 -1
index 2c8916ad073e..fd33835da4de 100644
@@ -46,6 +46,8 @@ enum pmu_kind {
 	PERF_PMU_KIND_DRM,
 	/* A perf tool provided HWMON PMU. */
 	PERF_PMU_KIND_HWMON,
+	/* A perf tool provided NVME PMU. */
+	PERF_PMU_KIND_NVME,
 	/* Perf tool provided PMU for tool events like time. */
 	PERF_PMU_KIND_TOOL,
 	/* A testing PMU kind. */
@@ -54,7 +56,9 @@ enum pmu_kind {
 
 enum {
 	PERF_PMU_TYPE_PE_START    = 0,
-	PERF_PMU_TYPE_PE_END      = 0xFFFDFFFF,
+	PERF_PMU_TYPE_PE_END      = 0xFFFCFFFF,
+	PERF_PMU_TYPE_NVME_START  = 0xFFFD0000,
+	PERF_PMU_TYPE_NVME_END    = 0xFFFDFFFF,
 	PERF_PMU_TYPE_DRM_START   = 0xFFFE0000,
 	PERF_PMU_TYPE_DRM_END     = 0xFFFEFFFF,
 	PERF_PMU_TYPE_HWMON_START = 0xFFFF0000,
@@ -366,6 +370,8 @@ static inline enum pmu_kind perf_pmu__kind(const struct perf_pmu *pmu)
 	type = pmu->type;
 	if (type <= PERF_PMU_TYPE_PE_END)
 		return PERF_PMU_KIND_PE;
+	if (type <= PERF_PMU_TYPE_NVME_END)
+		return PERF_PMU_KIND_NVME;
 	if (type <= PERF_PMU_TYPE_DRM_END)
 		return PERF_PMU_KIND_DRM;
 	if (type <= PERF_PMU_TYPE_HWMON_END)
tools/perf/util/pmus.c +19 -0
index 1355b317f3ed..980035b640dc 100644
@@ -18,6 +18,7 @@
 #include "pmus.h"
 #include "pmu.h"
 #include "hwmon_pmu.h"
+#include "nvme_pmu.h"
 #include "tool_pmu.h"
 #include "print-events.h"
 #include "strbuf.h"
@@ -45,18 +46,21 @@ enum perf_tool_pmu_type {
 	PERF_TOOL_PMU_TYPE_PE_OTHER,
 	PERF_TOOL_PMU_TYPE_TOOL,
 	PERF_TOOL_PMU_TYPE_HWMON,
+	PERF_TOOL_PMU_TYPE_NVME,
 	PERF_TOOL_PMU_TYPE_DRM,
 
 #define PERF_TOOL_PMU_TYPE_PE_CORE_MASK (1 << PERF_TOOL_PMU_TYPE_PE_CORE)
 #define PERF_TOOL_PMU_TYPE_PE_OTHER_MASK (1 << PERF_TOOL_PMU_TYPE_PE_OTHER)
 #define PERF_TOOL_PMU_TYPE_TOOL_MASK (1 << PERF_TOOL_PMU_TYPE_TOOL)
 #define PERF_TOOL_PMU_TYPE_HWMON_MASK (1 << PERF_TOOL_PMU_TYPE_HWMON)
+#define PERF_TOOL_PMU_TYPE_NVME_MASK (1 << PERF_TOOL_PMU_TYPE_NVME)
 #define PERF_TOOL_PMU_TYPE_DRM_MASK (1 << PERF_TOOL_PMU_TYPE_DRM)
 
 #define PERF_TOOL_PMU_TYPE_ALL_MASK (PERF_TOOL_PMU_TYPE_PE_CORE_MASK |	\
 					PERF_TOOL_PMU_TYPE_PE_OTHER_MASK | \
 					PERF_TOOL_PMU_TYPE_TOOL_MASK |	\
 					PERF_TOOL_PMU_TYPE_HWMON_MASK | \
+					PERF_TOOL_PMU_TYPE_NVME_MASK | \
 					PERF_TOOL_PMU_TYPE_DRM_MASK)
 };
 static unsigned int read_pmu_types;
@@ -180,6 +184,8 @@ struct perf_pmu *perf_pmus__find(const char *name)
 	/* Looking up an individual perf event PMU failed, check if a tool PMU should be read. */
 	if (!strncmp(name, "hwmon_", 6))
 		to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK;
+	else if (!strncmp(name, "nvme", 4))
+		to_read_pmus |= PERF_TOOL_PMU_TYPE_NVME_MASK;
 	else if (!strncmp(name, "drm_", 4))
 		to_read_pmus |= PERF_TOOL_PMU_TYPE_DRM_MASK;
 	else if (!strcmp(name, "tool"))
@@ -282,6 +288,10 @@ static void pmu_read_sysfs(unsigned int to_read_types)
 	    (read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) == 0)
 		perf_pmus__read_hwmon_pmus(&other_pmus);
 
+	if ((to_read_types & PERF_TOOL_PMU_TYPE_NVME_MASK) != 0 &&
+	    (read_pmu_types & PERF_TOOL_PMU_TYPE_NVME_MASK) == 0)
+		perf_pmus__read_nvme_pmus(&other_pmus);
+
 	if ((to_read_types & PERF_TOOL_PMU_TYPE_DRM_MASK) != 0 &&
 	    (read_pmu_types & PERF_TOOL_PMU_TYPE_DRM_MASK) == 0)
 		perf_pmus__read_drm_pmus(&other_pmus);
@@ -318,6 +328,8 @@ struct perf_pmu *perf_pmus__find_by_type(unsigned int type)
 	if (type >= PERF_PMU_TYPE_PE_START && type <= PERF_PMU_TYPE_PE_END) {
 		to_read_pmus = PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
 			PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+	} else if (type >= PERF_PMU_TYPE_NVME_START && type <= PERF_PMU_TYPE_NVME_END) {
+		to_read_pmus = PERF_TOOL_PMU_TYPE_NVME_MASK;
 	} else if (type >= PERF_PMU_TYPE_DRM_START && type <= PERF_PMU_TYPE_DRM_END) {
 		to_read_pmus = PERF_TOOL_PMU_TYPE_DRM_MASK;
 	} else if (type >= PERF_PMU_TYPE_HWMON_START && type <= PERF_PMU_TYPE_HWMON_END) {
@@ -390,6 +402,10 @@ struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *eve
 		if (strlen(event) > 4 && strncmp("drm-", event, 4) == 0)
 			to_read_pmus |= PERF_TOOL_PMU_TYPE_DRM_MASK;
 
+		/* Could the event be an nvme event? */
+		if (nvme_pmu__have_event(NULL, event))
+			to_read_pmus |= PERF_TOOL_PMU_TYPE_NVME_MASK;
+
 		pmu_read_sysfs(to_read_pmus);
 		pmu = list_prepare_entry(pmu, &core_pmus, list);
 	}
@@ -427,11 +443,14 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
 		 */
 		if (strisglob(wildcard)) {
 			to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK |
+				PERF_TOOL_PMU_TYPE_NVME_MASK |
 				PERF_TOOL_PMU_TYPE_DRM_MASK;
 		} else if (strlen(wildcard) >= 4 && strncmp("drm_", wildcard, 4) == 0) {
 			to_read_pmus |= PERF_TOOL_PMU_TYPE_DRM_MASK;
 		} else if (strlen(wildcard) >= 5 && strncmp("hwmon", wildcard, 5) == 0) {
 			to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK;
+		} else if (strlen(wildcard) >= 4 && strncmp("nvme", wildcard, 4) == 0) {
+			to_read_pmus |= PERF_TOOL_PMU_TYPE_NVME_MASK;
 		}
 
 		pmu_read_sysfs(to_read_pmus);
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#4 lore raw

Re: [PATCH v3 2/3] perf pmu: Implement tool-provided NVMe PMU

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] perf pmu: missing errno and err_thread handling in evsel__nvme_pmu_open() - [Medium] perf evsel…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf pmu: missing errno and err_thread handling in evsel__nvme_pmu_open()
- [Medium] perf evsel: PERF_EVENT_IOC_ENABLE/DISABLE ioctls issued on NVMe device fds
--

commit 1cca3c688611b8ef7867f412d383cb3ae7a315e1
Author: Ian Rogers <[email protected]>

perf pmu: Implement tool-provided NVMe PMU

This commit adds a tool-provided PMU for NVMe controllers using libnvme, exposing metrics from standard NVMe log pages under nvme_<ctrl> PMUs.

Quoted 5 lines
tools/perf/util/nvme_pmu.c new file +0 -0
new file mode 100644
index 0000000000000..3ee967505dec6

[ ... ]

Quoted 2 lines
tools/perf/util/nvme_pmu.c new file +2 -0
+static int nvme_pmu__read_val(int fd, uint64_t config, uint64_t *val)
+{

[ ... ]

Quoted 4 lines
tools/perf/util/nvme_pmu.c new file +4 -0
+	if (ret < 0)
+		return -errno ?: -EIO;
+	if (ret > 0)
+		return -EOPNOTSUPP;

[ ... ]

Quoted 13 lines
tools/perf/util/nvme_pmu.c new file +13 -0
+int evsel__nvme_pmu_open(struct evsel *evsel,
+			 struct perf_thread_map *threads,
+			 int start_cpu_map_idx, int end_cpu_map_idx)
+{
+	struct nvme_pmu *nvm = container_of(evsel->pmu, struct nvme_pmu, pmu);
+	int idx = start_cpu_map_idx, thread = 0, nthreads, err = 0;
+	int max_cpus, max_threads;
+	char path[PATH_MAX];
+
+	if (evsel->core.attr.sample_period || !evsel->core.fd)
+		return -EINVAL;
+
+	snprintf(path, sizeof(path), "/dev/%s", nvm->dev_name);

[ ... ]

tools/perf/util/nvme_pmu.c line 611

Quoted 6 lines
tools/perf/util/nvme_pmu.c new file +6 -0
+	for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) {
+		for (thread = 0; thread < nthreads; thread++) {
+			uint64_t *initial_val;
+			int fd = open(path, O_RDONLY | O_CLOEXEC);
+
+			FD(evsel, idx, thread) = fd;

[Severity: Medium]
Does storing a /dev/nvmeX file descriptor in evsel->core.fd cause evsel__enable_cpu(), evsel__enable(), evsel__disable_cpu() and evsel__disable() to fail with -ENOTTY?

Those functions only special-case evsel__is_tool(evsel) and otherwise call
into libperf:

tools/perf/util/evsel.c:evsel__enable_cpu() {
	if (evsel__is_tool(evsel))
		err = evsel__tool_pmu_enable_cpu(evsel, cpu_map_idx);
	else
		err = perf_evsel__enable_cpu(&evsel->core, cpu_map_idx);

	if (!err && evsel__is_group_leader(evsel)) {
    ...
}

perf_evsel__enable_cpu() and its enable/disable counterparts issue PERF_EVENT_IOC_ENABLE and PERF_EVENT_IOC_DISABLE on every fd in evsel->core.fd, which the NVMe character device rejects with -ENOTTY. That leaves evsel->disabled stale and aborts the group loop before userspace members such as duration_time, user_time or system_time are enabled or disabled in mixed groups.

Quoted 18 lines
tools/perf/util/nvme_pmu.c new file +18 -0
+			if (fd < 0) {
+				err = -errno;
+				goto out_close;
+			}
+			initial_val = xyarray__entry(evsel->nvme.initial_values, idx, thread);
+			err = nvme_pmu__read_val(fd, evsel->core.attr.config, initial_val);
+			if (err) {
+				close(fd);
+				FD(evsel, idx, thread) = -1;
+				goto out_close;
+			}
+		}
+	}
+	return 0;
+out_close:
+	if (err)
+		threads->err_thread = thread;
+

tools/perf/util/nvme_pmu.c line 639

Quoted 10 lines
tools/perf/util/nvme_pmu.c new file +10 -0
+	do {
+		while (--thread >= 0) {
+			if (FD(evsel, idx, thread) >= 0)
+				close(FD(evsel, idx, thread));
+			FD(evsel, idx, thread) = -1;
+		}
+		thread = nthreads;
+	} while (--idx >= start_cpu_map_idx);
+	return err;
+}

[Severity: Medium]
Does evsel__nvme_pmu_open() leave errno unset or clobbered on failure while also setting threads->err_thread for a controller-wide device?

Callers of evsel__open() and evsel__open_per_cpu_and_thread() such as __run_perf_stat(), record__open() and __cmd_top() read errno after a negative return. When a controller does not support an optional log page (such as FDP, ZNS or Endurance Group), libnvme returns a positive NVMe status code without setting errno, and nvme_pmu__read_val() returns -EOPNOTSUPP with errno still 0 (or clobbered by close()). The -EINVAL and !HAVE_LIBNVME_SUPPORT error returns likewise do not set errno.

In addition, setting threads->err_thread = thread causes stat_handle_error() in perf stat --per-thread mode to treat a controller open or log error as a dead thread, removing every thread from the thread map in a retry loop until nthreads reaches 0 and evsel__nvme_pmu_open() returns 0 with no descriptors opened.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#5 lore raw

PATCH v3 3/3 perf tests: Add NVMe PMU event parsing test

In reply to Ian Rogers

Introduce a unit test suite for the NVMe tool PMU event parser. The test registers a mock test NVMe PMU ('nvme_a_test_nvme_pmu' with alias 'nvme1234') and verifies:
- Event parsing without a PMU prefix and with both PMU name and alias.
- Event config encoding, unit, and scale across SMART, Endurance, FDP, Error, and ZNS log events (including Kelvin temperature scaling).
- Support for 'event=<name>', raw 'config=0x...', and 'name=' qualifiers.
- Clean rejection of unknown event names, invalid raw configs, duplicate event terms, and unsupported string terms where term->config is NULL.

---
 tools/perf/tests/Build          |   1 +
 tools/perf/tests/builtin-test.c |   1 +
 tools/perf/tests/nvme_pmu.c     | 303 ++++++++++++++++++++++++++++++++
 tools/perf/tests/tests.h        |   1 +
 tools/perf/util/pmus.c          |   5 +
 tools/perf/util/pmus.h          |   1 +
 6 files changed, 312 insertions(+)
 create mode 100644 tools/perf/tests/nvme_pmu.c
tools/perf/tests/Build +1 -0
index 5eebb2ec5dde..336bad77c8f4 100644
@@ -71,6 +71,7 @@ perf-test-y += hybrid-merge.o
 perf-test-y += symbols.o
 perf-test-y += util.o
 perf-test-y += hwmon_pmu.o
+perf-test-y += nvme_pmu.o
 perf-test-y += tool_pmu.o
 perf-test-y += subcmd-help.o
 perf-test-y += kallsyms-split.o
tools/perf/tests/builtin-test.c +1 -0
index 327483f96401..0b88018c7036 100644
@@ -124,6 +124,7 @@ static struct test_suite *generic_tests[] = {
 	&suite__pmu,
 	&suite__pmu_events,
 	&suite__hwmon_pmu,
+	&suite__nvme_pmu,
 	&suite__tool_pmu,
 	&suite__dso_data,
 	&suite__perf_evsel__roundtrip_name_test,
tools/perf/tests/nvme_pmu.c new file +303 -0

303 lines not on this page · raw message

tools/perf/tests/tests.h +1 -0
index af822885890d..5a77bc5ecf7a 100644
@@ -133,6 +133,7 @@ DECLARE_SUITE(syscall_openat_tp_fields);
 DECLARE_SUITE(pmu);
 DECLARE_SUITE(pmu_events);
 DECLARE_SUITE(hwmon_pmu);
+DECLARE_SUITE(nvme_pmu);
 DECLARE_SUITE(tool_pmu);
 DECLARE_SUITE(attr);
 DECLARE_SUITE(dso_data);
tools/perf/util/pmus.c +5 -0
index 980035b640dc..66626ce3494b 100644
@@ -925,6 +925,11 @@ struct perf_pmu *perf_pmus__add_test_hwmon_pmu(const char *hwmon_dir,
 	return hwmon_pmu__new(&other_pmus, hwmon_dir, sysfs_name, name);
 }
 
+struct perf_pmu *perf_pmus__add_test_nvme_pmu(const char *sysfs_name, const char *name)
+{
+	return nvme_pmu__new(&other_pmus, sysfs_name, name);
+}
+
 struct perf_pmu *perf_pmus__fake_pmu(void)
 {
 	static struct perf_pmu fake = {
tools/perf/util/pmus.h +1 -0
index 0d55edb3f2fc..2045d4cf44d1 100644
@@ -37,6 +37,7 @@ struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char *name)
 struct perf_pmu *perf_pmus__add_test_hwmon_pmu(const char *hwmon_dir,
 					       const char *sysfs_name,
 					       const char *name);
+struct perf_pmu *perf_pmus__add_test_nvme_pmu(const char *sysfs_name, const char *name);
 struct perf_pmu *perf_pmus__fake_pmu(void);
 struct perf_pmu *perf_pmus__find_core_pmu(void);
 
Signature · 1 line
2.56.1.703.gf848ed807c-goog
#6 lore raw

Re: [PATCH v3 3/3] perf tests: Add NVMe PMU event parsing test

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] perf tests: suite__nvme_pmu has NULL test_cases when libnvme is disabled --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] perf tests: suite__nvme_pmu has NULL test_cases when libnvme is disabled
--

commit 606f3f12b454f7dd2fdaeea08ae54a0e801abbdb
Author: Ian Rogers <[email protected]>

perf tests: Add NVMe PMU event parsing test

This commit adds a unit test suite for the NVMe tool PMU event parser and registers a helper to create a test NVMe PMU.

Quoted 17 lines
tools/perf/tests/builtin-test.c +1 -0
index 0b8897fd42cac..fbcdcbf92aded 100644
@@ -110,6 +110,7 @@ static struct test_suite *generic_tests[] = {
 	&suite__pmu,
 	&suite__pmu_events,
 	&suite__hwmon_pmu,
+	&suite__nvme_pmu,
 	&suite__tool_pmu,
 	&suite__dso_data,
 	&suite__perf_evsel__roundtrip_name_test,
tools/perf/tests/nvme_pmu.c new file +0 -0
new file mode 100644
index 0000000000000..c3c17ccc9ea0b

[ ... ]

tools/perf/tests/nvme_pmu.c line 303

Quoted 8 lines
tools/perf/tests/nvme_pmu.c new file +8 -0
+#else
+
+struct test_suite suite__nvme_pmu = {
+	.desc = "NVMe PMU",
+	.test_cases = NULL,
+};
+
+#endif

[Severity: Low]
When HAVE_LIBNVME_SUPPORT is not defined, does setting suite__nvme_pmu.test_cases to NULL cause __cmd_test() to silently drop the suite during execution?

generic_tests[] registers suite__nvme_pmu unconditionally, so perf test list assigns it a suite number, but __cmd_test() only invokes start_test() and print_test_result() inside test_suite__for_each_test_case(), which runs zero iterations when test_cases is NULL. On builds without libnvme, perf test skips over that suite number without printing a Skip line, counting it in the skipped summary, or emitting it in JUnit output. Providing a stub test case that returns TEST_SKIP with a TEST_CASE_REASON of "not compiled in" (like tools/perf/tests/pfm.c), or guarding &suite__nvme_pmu in generic_tests[] with HAVE_LIBNVME_SUPPORT, would keep the suite list and test runner in sync.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3