[PATCHv2 9/9] libnvme: add support for ctrl diagnostic counters
Nilay Shroff
nilay at linux.ibm.com
Sat Apr 4 03:14:59 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 13bd3995c..52611f38b 100644
--- a/libnvme/src/libnvme.ld
+++ b/libnvme/src/libnvme.ld
@@ -16,6 +16,9 @@ LIBNVME_3 {
nvme_ctrl_get_subsysnqn;
nvme_ctrl_get_subsystem;
nvme_ctrl_get_transport_handle;
+ nvme_ctrl_get_command_error_count;
+ nvme_ctrl_get_reset_count;
+ nvme_ctrl_get_reconnect_count;
nvme_ctrl_identify;
nvme_ctrl_match_config;
nvme_ctrl_next_ns;
diff --git a/libnvme/src/nvme/private.h b/libnvme/src/nvme/private.h
index ae0b8507e..2009ac3c0 100644
--- a/libnvme/src/nvme/private.h
+++ b/libnvme/src/nvme/private.h
@@ -255,6 +255,9 @@ struct nvme_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 nvme_fabrics_config cfg;
};
diff --git a/libnvme/src/nvme/tree.c b/libnvme/src/nvme/tree.c
index 77aed7eee..210952785 100644
--- a/libnvme/src/nvme/tree.c
+++ b/libnvme/src/nvme/tree.c
@@ -1414,6 +1414,39 @@ __public const char *nvme_ctrl_get_state(nvme_ctrl_t c)
return c->state;
}
+__public long nvme_ctrl_get_command_error_count(nvme_ctrl_t c)
+{
+ _cleanup_free_ char *error_count = NULL;
+
+ error_count = nvme_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 nvme_ctrl_get_reset_count(nvme_ctrl_t c)
+{
+ _cleanup_free_ char *reset_count = NULL;
+
+ reset_count = nvme_get_ctrl_attr(c, "reset_count");
+ if (reset_count)
+ sscanf(reset_count, "%ld", &c->reset_count);
+
+ return c->reset_count;
+}
+
+__public long nvme_ctrl_get_reconnect_count(nvme_ctrl_t c)
+{
+ _cleanup_free_ char *reconnect_count = NULL;
+
+ reconnect_count = nvme_get_ctrl_attr(c, "reconnect_count");
+ if (reconnect_count)
+ sscanf(reconnect_count, "%ld", &c->reconnect_count);
+
+ return c->reconnect_count;
+}
+
__public struct nvme_fabrics_config *nvme_ctrl_get_config(nvme_ctrl_t c)
{
return &c->cfg;
diff --git a/libnvme/src/nvme/tree.h b/libnvme/src/nvme/tree.h
index e6a26efb1..3a86208ef 100644
--- a/libnvme/src/nvme/tree.h
+++ b/libnvme/src/nvme/tree.h
@@ -1030,6 +1030,31 @@ unsigned long long nvme_ns_get_write_sectors(nvme_ns_t n);
*/
struct nvme_fabrics_config *nvme_ctrl_get_config(nvme_ctrl_t c);
+/**
+ * nvme_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 nvme_ctrl_get_command_error_count(nvme_ctrl_t c);
+
+/**
+ * nvme_ctrl_get_reset_count() - Get controller reset count
+ * @c: Controller instance
+ *
+ * Return: Number of timer controller @c is reset
+ */
+long nvme_ctrl_get_reset_count(nvme_ctrl_t c);
+
+/**
+ * nvme_ctrl_get_reconnect_count() - Get controller reconnect count
+ * @c: Controller instance
+ *
+ * Return: Number of times controller has to reconnect to the target
+ */
+long nvme_ctrl_get_reconnect_count(nvme_ctrl_t c);
+
/**
* nvme_ctrl_identify() - Issues an 'identify controller' command
* @c: Controller instance
--
2.53.0
More information about the Linux-nvme
mailing list