[PATCHv3 9/9] libnvme: add support for ctrl diagnostic counters
Nilay Shroff
nilay at linux.ibm.com
Tue Apr 21 07:50:30 PDT 2026
Add support for retrieving ctrl diagnostic counters such as command_
error_count, reset_count and reconnect_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 | 3 +++
libnvme/src/nvme/private.h | 3 +++
libnvme/src/nvme/tree.c | 33 +++++++++++++++++++++++++++++++++
libnvme/src/nvme/tree.h | 25 +++++++++++++++++++++++++
4 files changed, 64 insertions(+)
diff --git a/libnvme/src/libnvme.ld b/libnvme/src/libnvme.ld
index c9841e45c..68df5e2b6 100644
--- a/libnvme/src/libnvme.ld
+++ b/libnvme/src/libnvme.ld
@@ -14,6 +14,9 @@ LIBNVME_3 {
libnvme_ctrl_get_subsysnqn;
libnvme_ctrl_get_subsystem;
libnvme_ctrl_get_transport_handle;
+ libnvme_ctrl_get_command_error_count;
+ libnvme_ctrl_get_reset_count;
+ libnvme_ctrl_get_reconnect_count;
libnvme_ctrl_identify;
libnvme_ctrl_match_config;
libnvme_ctrl_next_ns;
diff --git a/libnvme/src/nvme/private.h b/libnvme/src/nvme/private.h
index 170445927..f7612c06d 100644
--- a/libnvme/src/nvme/private.h
+++ b/libnvme/src/nvme/private.h
@@ -295,6 +295,9 @@ struct libnvme_ctrl { // !generate-accessors
bool unique_discovery_ctrl;
bool discovered;
bool persistent;
+ long command_error_count; //!accessors:none
+ long reset_count; //!accessors:none
+ long reconnect_count; //!accessors:none
struct libnvme_fabrics_config cfg;
};
diff --git a/libnvme/src/nvme/tree.c b/libnvme/src/nvme/tree.c
index 085d79558..f87a6d0c3 100644
--- a/libnvme/src/nvme/tree.c
+++ b/libnvme/src/nvme/tree.c
@@ -1458,6 +1458,39 @@ __public const char *libnvme_ctrl_get_state(libnvme_ctrl_t c)
return c->state;
}
+__public long libnvme_ctrl_get_command_error_count(libnvme_ctrl_t c)
+{
+ __cleanup_free char *error_count = NULL;
+
+ error_count = libnvme_get_ctrl_attr(c, "command_error_count");
+ if (error_count)
+ sscanf(error_count, "%ld", &c->command_error_count);
+
+ return c->command_error_count;
+}
+
+__public long libnvme_ctrl_get_reset_count(libnvme_ctrl_t c)
+{
+ __cleanup_free char *reset_count = NULL;
+
+ reset_count = libnvme_get_ctrl_attr(c, "reset_count");
+ if (reset_count)
+ sscanf(reset_count, "%ld", &c->reset_count);
+
+ return c->reset_count;
+}
+
+__public long libnvme_ctrl_get_reconnect_count(libnvme_ctrl_t c)
+{
+ __cleanup_free char *reconnect_count = NULL;
+
+ reconnect_count = libnvme_get_ctrl_attr(c, "reconnect_count");
+ if (reconnect_count)
+ sscanf(reconnect_count, "%ld", &c->reconnect_count);
+
+ return c->reconnect_count;
+}
+
__public int libnvme_ctrl_identify(libnvme_ctrl_t c, struct nvme_id_ctrl *id)
{
struct libnvme_transport_handle *hdl =
diff --git a/libnvme/src/nvme/tree.h b/libnvme/src/nvme/tree.h
index 6bfc8cf98..48f1beb49 100644
--- a/libnvme/src/nvme/tree.h
+++ b/libnvme/src/nvme/tree.h
@@ -1007,6 +1007,31 @@ unsigned long long libnvme_ns_get_read_sectors(libnvme_ns_t n);
*/
unsigned long long libnvme_ns_get_write_sectors(libnvme_ns_t n);
+/**
+ * libnvme_ctrl_get_command_error_count() - Get admin command error count
+ * @c: Controller instance
+ *
+ * Return: Number of times admin command issued to controller @c failed or
+ * returned error status
+ */
+long libnvme_ctrl_get_command_error_count(libnvme_ctrl_t c);
+
+/**
+ * libnvme_ctrl_get_reset_count() - Get controller reset count
+ * @c: Controller instance
+ *
+ * Return: Number of timer controller @c is reset
+ */
+long libnvme_ctrl_get_reset_count(libnvme_ctrl_t c);
+
+/**
+ * libnvme_ctrl_get_reconnect_count() - Get controller reconnect count
+ * @c: Controller instance
+ *
+ * Return: Number of times controller has to reconnect to the target
+ */
+long libnvme_ctrl_get_reconnect_count(libnvme_ctrl_t c);
+
/**
* libnvme_ctrl_identify() - Issues an 'identify controller' command
* @c: Controller instance
--
2.53.0
More information about the Linux-nvme
mailing list