]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: Add smart-log command fahrenheit option
authorTokunori Ikegami <ikegami.t@gmail.com>
Fri, 28 Jun 2024 15:39:03 +0000 (00:39 +0900)
committerDaniel Wagner <wagi@monom.org>
Fri, 28 Jun 2024 17:58:31 +0000 (19:58 +0200)
Show temperatures in degrees fahrenheit by the option.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-stdout.c
nvme-print.c
nvme-print.h
nvme.c
nvme.h
util/types.h

index 2b9983e2d14a6cebc818191dd01283cafcc2b340..1159034c7c2af48c775bbb1beb62f52b5be147a5 100644 (file)
@@ -3881,6 +3881,7 @@ static void stdout_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
        __u16 temperature = smart->temperature[1] << 8 | smart->temperature[0];
        int i;
        bool human = stdout_print_ops.flags & VERBOSE;
+       bool fahrenheit = !!(stdout_print_ops.flags & FAHRENHEIT);
 
        printf("Smart Log for NVME device:%s namespace-id:%x\n", devname, nsid);
        printf("critical_warning                        : %#x\n",
@@ -3895,8 +3896,8 @@ static void stdout_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
                printf("      Persistent Mem. RO[5]          : %d\n", (smart->critical_warning & 0x20) >> 5);
        }
 
-       printf("temperature                             : %ld °C (%u K)\n",
-               kelvin_to_celsius(temperature), temperature);
+       printf("temperature                             : %s (%u K)\n",
+              nvme_degrees_string(temperature, fahrenheit), temperature);
        printf("available_spare                         : %u%%\n",
                smart->avail_spare);
        printf("available_spare_threshold               : %u%%\n",
@@ -3933,13 +3934,12 @@ static void stdout_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
                le32_to_cpu(smart->warning_temp_time));
        printf("Critical Composite Temperature Time     : %u\n",
                le32_to_cpu(smart->critical_comp_time));
-       for (i = 0; i < 8; i++) {
-               __s32 temp = le16_to_cpu(smart->temp_sensor[i]);
-
-               if (temp == 0)
+       for (i = 0; i < ARRAY_SIZE(smart->temp_sensor); i++) {
+               temperature = le16_to_cpu(smart->temp_sensor[i]);
+               if (!temperature)
                        continue;
-               printf("Temperature Sensor %d           : %ld °C (%u K)\n",
-                      i + 1, kelvin_to_celsius(temp), temp);
+               printf("Temperature Sensor %d                   : %s (%u K)\n", i + 1,
+                      nvme_degrees_string(temperature, fahrenheit), temperature);
        }
        printf("Thermal Management T1 Trans Count       : %u\n",
                le32_to_cpu(smart->thm_temp1_trans_count));
index faaaee411bbe367be801bbaee3ea7ea76d4056a1..5b016c8f9186c21bcd7f64be597e1ce085d97d6c 100644 (file)
@@ -767,6 +767,20 @@ void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
        nvme_print(endurance_log, flags, endurance_log, group_id, devname);
 }
 
+const char *nvme_degrees_string(long t, bool fahrenheit)
+{
+       static char str[STR_LEN];
+       long val = kelvin_to_celsius(t);
+
+       if (fahrenheit)
+               val = kelvin_to_fahrenheit(t);
+
+       sprintf(str, "%ld °%s", val, fahrenheit ? "F" : "C");
+
+
+       return str;
+}
+
 void nvme_show_smart_log(struct nvme_smart_log *smart, unsigned int nsid,
                         const char *devname, enum nvme_print_flags flags)
 {
index 64234fcecc90a3e0be48d8ee598e711a28d3b3ca..cbb9a5e67bc730ce321c92ff41dad7fade210b27 100644 (file)
@@ -322,4 +322,5 @@ void nvme_show_finish(void);
 bool nvme_is_fabrics_reg(int offset);
 bool nvme_registers_cmbloc_support(__u32 cmbsz);
 bool nvme_registers_pmrctl_ready(__u32 pmrctl);
+const char *nvme_degrees_string(long t, bool fahrenheit);
 #endif /* NVME_PRINT_H */
diff --git a/nvme.c b/nvme.c
index 29829d1057322c04fb6c2b59ac20a6e687466766..16dd57b586fbf0a0debd9f0ca3e75c9e7d48cf6b 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -216,6 +216,7 @@ static const char *doper = "directive operation";
 static const char *dry = "show command instead of sending";
 static const char *dspec_w_dtype = "directive specification associated with directive type";
 static const char *dtype = "directive type";
+static const char *fahrenheit = "show temperatures in degrees fahrenheit";
 static const char *force_unit_access = "force device to commit data before command completes";
 static const char *human_readable_directive = "show directive in readable format";
 static const char *human_readable_identify = "show identify in readable format";
@@ -549,6 +550,7 @@ static int get_smart_log(int argc, char **argv, struct command *cmd, struct plug
                __u32   namespace_id;
                bool    raw_binary;
                bool    human_readable;
+               bool    fahrenheit;
        };
 
        struct config cfg = {
@@ -560,7 +562,8 @@ static int get_smart_log(int argc, char **argv, struct command *cmd, struct plug
        NVME_ARGS(opts,
                  OPT_UINT("namespace-id",   'n', &cfg.namespace_id,   namespace),
                  OPT_FLAG("raw-binary",     'b', &cfg.raw_binary,     raw_output),
-                 OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_info));
+                 OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable_info),
+                 OPT_FLAG("fahrenheit",     'f', &cfg.fahrenheit,     fahrenheit));
 
        err = parse_and_open(&dev, argc, argv, desc, opts);
        if (err)
@@ -578,6 +581,9 @@ static int get_smart_log(int argc, char **argv, struct command *cmd, struct plug
        if (cfg.human_readable)
                flags |= VERBOSE;
 
+       if (cfg.fahrenheit)
+               flags |= FAHRENHEIT;
+
        smart_log = nvme_alloc(sizeof(*smart_log));
        if (!smart_log)
                return -ENOMEM;
diff --git a/nvme.h b/nvme.h
index 7e86afc5d0abff9ed0bf7f403e6e942b8f4da2fa..66b544fc9fff4c9125c3e1a5dbe2d824320dddf7 100644 (file)
--- a/nvme.h
+++ b/nvme.h
 #include "util/cleanup.h"
 
 enum nvme_print_flags {
-       NORMAL  = 0,
-       VERBOSE = 1 << 0,       /* verbosely decode complex values for humans */
-       JSON    = 1 << 1,       /* display in json format */
-       VS      = 1 << 2,       /* hex dump vendor specific data areas */
-       BINARY  = 1 << 3,       /* binary dump raw bytes */
+       NORMAL          = 0,
+       VERBOSE         = 1 << 0,       /* verbosely decode complex values for humans */
+       JSON            = 1 << 1,       /* display in json format */
+       VS              = 1 << 2,       /* hex dump vendor specific data areas */
+       BINARY          = 1 << 3,       /* binary dump raw bytes */
+       FAHRENHEIT      = 1 << 4,       /* show temperatures in degrees fahrenheit */
 };
 
 enum nvme_cli_topo_ranking {
index 595958bb7c01572863a5fe9bd35ff32530cf1a0a..9e0806c2d292188b248a4fb0c5b7a6cea544cbc9 100644 (file)
@@ -16,6 +16,16 @@ static inline long kelvin_to_celsius(long t)
        return t + ABSOLUTE_ZERO_CELSIUS;
 }
 
+static inline long celsius_to_fahrenheit(long t)
+{
+       return t * 9 / 5 + 32;
+}
+
+static inline long kelvin_to_fahrenheit(long t)
+{
+       return celsius_to_fahrenheit(kelvin_to_celsius(t));
+}
+
 /* uint128_t is not always available, define our own. */
 union nvme_uint128 {
         __u8  bytes[16];