]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
ioctl: Add debugging feature to show command outputs
authorTokunori Ikegami <ikegami.t@gmail.com>
Tue, 12 Sep 2023 14:42:32 +0000 (23:42 +0900)
committerDaniel Wagner <wagi@monom.org>
Wed, 13 Sep 2023 07:29:31 +0000 (09:29 +0200)
Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
src/libnvme.map
src/nvme/ioctl.c
src/nvme/ioctl.h

index faabd7215f9941269dee1959ccb5e81505880dcc..1765bc65c8d223984481556548af75a46e10dd73 100644 (file)
@@ -8,6 +8,8 @@ LIBNVME_1_6 {
                nvme_root_release_fds;
                nvme_subsystem_get_iopolicy;
                nvme_subsystem_release_fds;
+               nvme_set_debug;
+               nvme_get_debug;
 };
 
 LIBNVME_1_5 {
index b9710b3d25139335876b27659ece0cfb95d086fc..ec0ac6b61db27abe2a8f3c6b26afe82e60672ba2 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <inttypes.h>
 
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 
 #include <ccan/build_assert/build_assert.h>
 #include <ccan/endian/endian.h>
@@ -23,6 +25,8 @@
 #include "ioctl.h"
 #include "util.h"
 
+static bool nvme_debug;
+
 static int nvme_verify_chr(int fd)
 {
        static struct stat nvme_stat;
@@ -86,13 +90,62 @@ static int nvme_submit_passthru64(int fd, unsigned long ioctl_cmd,
        return err;
 }
 
+static void nvme_show_command(struct nvme_passthru_cmd *cmd, int err, struct timeval start,
+                             struct timeval end)
+{
+       printf("opcode       : %02x\n", cmd->opcode);
+       printf("flags        : %02x\n", cmd->flags);
+       printf("rsvd1        : %04x\n", cmd->rsvd1);
+       printf("nsid         : %08x\n", cmd->nsid);
+       printf("cdw2         : %08x\n", cmd->cdw2);
+       printf("cdw3         : %08x\n", cmd->cdw3);
+       printf("data_len     : %08x\n", cmd->data_len);
+       printf("metadata_len : %08x\n", cmd->metadata_len);
+       printf("addr         : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->addr);
+       printf("metadata     : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->metadata);
+       printf("cdw10        : %08x\n", cmd->cdw10);
+       printf("cdw11        : %08x\n", cmd->cdw11);
+       printf("cdw12        : %08x\n", cmd->cdw12);
+       printf("cdw13        : %08x\n", cmd->cdw13);
+       printf("cdw14        : %08x\n", cmd->cdw14);
+       printf("cdw15        : %08x\n", cmd->cdw15);
+       printf("timeout_ms   : %08x\n", cmd->timeout_ms);
+       printf("result       : %08x\n", cmd->result);
+       printf("err          : %d\n", err);
+       printf("latency      : %lu us\n",
+              (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec));
+}
+
+void nvme_set_debug(bool debug)
+{
+       nvme_debug = debug;
+}
+
+bool nvme_get_debug(void)
+{
+       return nvme_debug;
+}
+
 static int nvme_submit_passthru(int fd, unsigned long ioctl_cmd,
                                struct nvme_passthru_cmd *cmd, __u32 *result)
 {
-       int err = ioctl(fd, ioctl_cmd, cmd);
+       struct timeval start;
+       struct timeval end;
+       int err;
+
+       if (nvme_get_debug())
+               gettimeofday(&start, NULL);
+
+       err = ioctl(fd, ioctl_cmd, cmd);
+
+       if (nvme_get_debug()) {
+               gettimeofday(&end, NULL);
+               nvme_show_command(cmd, err, start, end);
+       }
 
        if (err >= 0 && result)
                *result = cmd->result;
+
        return err;
 }
 
index 47d8625e435752c14d4290215890fa1d23c4f2b7..2a6c4621f4963ab089167f92f99a3a106efec4e4 100644 (file)
@@ -3855,4 +3855,16 @@ int nvme_zns_append(struct nvme_zns_append_args *args);
  */
 int nvme_dim_send(struct nvme_dim_args *args);
 
+/**
+ * nvme_set_debug - Set NVMe command debugging output
+ * @debug:     true to enable or false to disable
+ */
+void nvme_set_debug(bool debug);
+
+/**
+ * nvme_get_debug - Get NVMe command debugging output
+ *
+ * Return: false if disabled or true if enabled.
+ */
+bool nvme_get_debug(void);
 #endif /* _LIBNVME_IOCTL_H */