[PATCHv3 7/9] libnvme: add support for namespace diagnostic counters

Nilay Shroff nilay at linux.ibm.com
Tue Apr 21 07:50:28 PDT 2026


Add support for retrieving namespace diagnostic counters such as
command_retry_count and command_error_count. These counters improve
visibility and can be useful for tools such as nvme-top to display
real-time statistics.

Unlike other sysfs attributes, these counters can change dynamically.
Annotate them with "!accessors:none" and provide custom implementations
to always retrieve the latest (non-cached) values.

Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
 libnvme/src/libnvme.ld     |  2 ++
 libnvme/src/nvme/private.h |  3 +++
 libnvme/src/nvme/tree.c    | 22 ++++++++++++++++++++++
 libnvme/src/nvme/tree.h    | 17 +++++++++++++++++
 4 files changed, 44 insertions(+)

diff --git a/libnvme/src/libnvme.ld b/libnvme/src/libnvme.ld
index 85ff00e55..2fafd333c 100644
--- a/libnvme/src/libnvme.ld
+++ b/libnvme/src/libnvme.ld
@@ -140,6 +140,8 @@ LIBNVME_3 {
 		libnvme_ns_get_write_sectors;
 		libnvme_ns_get_inflights;
 		libnvme_ns_get_io_ticks;
+		libnvme_ns_get_command_retry_count;
+		libnvme_ns_get_command_error_count;
 		libnvme_ns_identify;
 		libnvme_ns_read;
 		libnvme_ns_verify;
diff --git a/libnvme/src/nvme/private.h b/libnvme/src/nvme/private.h
index 7f01f8a7d..ec0e1e848 100644
--- a/libnvme/src/nvme/private.h
+++ b/libnvme/src/nvme/private.h
@@ -251,6 +251,9 @@ struct libnvme_ns { // !generate-accessors
 	uint8_t nguid[16];
 	unsigned char uuid[NVME_UUID_LEN];
 	enum nvme_csi csi;
+
+	long command_retry_count; //!accessors:none
+	long command_error_count; //!accessors:none
 };
 
 struct libnvme_ctrl { // !generate-accessors
diff --git a/libnvme/src/nvme/tree.c b/libnvme/src/nvme/tree.c
index a603c2fde..f1a1311b7 100644
--- a/libnvme/src/nvme/tree.c
+++ b/libnvme/src/nvme/tree.c
@@ -2611,6 +2611,28 @@ __public void libnvme_ns_get_uuid(libnvme_ns_t n,
 	memcpy(out, n->uuid, NVME_UUID_LEN);
 }
 
+__public long libnvme_ns_get_command_retry_count(libnvme_ns_t n)
+{
+	__cleanup_free char *retry_count = NULL;
+
+	retry_count = libnvme_get_ns_attr(n, "command_retry_count");
+	if (retry_count)
+		sscanf(retry_count, "%ld", &n->command_retry_count);
+
+	return n->command_retry_count;
+}
+
+__public long libnvme_ns_get_command_error_count(libnvme_ns_t n)
+{
+	__cleanup_free char *error_count = NULL;
+
+	error_count = libnvme_get_ns_attr(n, "command_error_count");
+	if (error_count)
+		sscanf(error_count, "%ld", &n->command_error_count);
+
+	return n->command_error_count;
+}
+
 __public int libnvme_ns_identify(libnvme_ns_t n, struct nvme_id_ns *ns)
 {
 	struct libnvme_transport_handle *hdl;
diff --git a/libnvme/src/nvme/tree.h b/libnvme/src/nvme/tree.h
index 2eb4cea0e..63cd20838 100644
--- a/libnvme/src/nvme/tree.h
+++ b/libnvme/src/nvme/tree.h
@@ -510,6 +510,23 @@ const uint8_t *libnvme_ns_get_nguid(libnvme_ns_t n);
  */
 void libnvme_ns_get_uuid(libnvme_ns_t n, unsigned char out[NVME_UUID_LEN]);
 
+/**
+ * libnvme_ns_get_command_retry_count() - Get command retry count
+ * @n: &libnvme_ns_t object
+ *
+ * Return: Number of times any command issued to namespace @n has to be retried
+ */
+long libnvme_ns_get_command_retry_count(libnvme_ns_t n);
+
+/**
+ * libnvme_ns_get_command_error_count() - Get command error count
+ * @n: &libnvme_ns_t object
+ *
+ * Return: Number of times command issued to namespace @n returns non-zero
+ * status or error
+ */
+long libnvme_ns_get_command_error_count(libnvme_ns_t n);
+
 /**
  * libnvme_ns_get_generic_name() - Returns name of generic namespace chardev.
  * @n:	Namespace instance
-- 
2.53.0




More information about the Linux-nvme mailing list