[PATCH 3/7] nvme: add support for float and double types in table_print_XXX()
Nilay Shroff
nilay at linux.ibm.com
Thu Apr 30 03:52:24 PDT 2026
The table_print_XXX() APIs do not currently support printing values of
type float or double.
Add support for float and double so that these data types can be used
with the table printing helpers. This will be later used for printing
nvme-top stat.
While at it, switch error reporting to nvme_show_error() for
consistency with the rest of the code.
Signed-off-by: Nilay Shroff <nilay at linux.ibm.com>
---
util/table.c | 22 ++++++++++++++++++++--
util/table.h | 26 ++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/util/table.c b/util/table.c
index 1a69cd39a..19ee6c1e0 100644
--- a/util/table.c
+++ b/util/table.c
@@ -20,6 +20,7 @@
#include <errno.h>
#include <string.h>
+#include "nvme-print.h"
#include "table.h"
static int table_get_value_width(struct value *v)
@@ -43,8 +44,14 @@ static int table_get_value_width(struct value *v)
case FMT_LONG:
len = snprintf(buf, sizeof(buf), "%ld", v->ld);
break;
+ case FMT_FLOAT:
+ len = snprintf(buf, sizeof(buf), "%.2f", v->f);
+ break;
+ case FMT_DOUBLE:
+ len = snprintf(buf, sizeof(buf), "%.2f", v->d);
+ break;
default:
- printf("Invalid print format!\n");
+ nvme_show_error("Invalid print format!\n");
break;
}
return len;
@@ -81,8 +88,14 @@ static void table_print_centered(struct value *val, int width, enum fmt_type typ
break;
case FMT_UNSIGNED_LONG:
printf("%lu", val->lu);
+ case FMT_FLOAT:
+ printf("%.2f", val->f);
+ break;
+ case FMT_DOUBLE:
+ printf("%.2f", val->d);
break;
default:
+ nvme_show_error("Invalid print format!\n");
break;
}
@@ -167,9 +180,14 @@ static void table_print_rows(const struct table *t)
break;
case FMT_UNSIGNED_LONG:
printf("%*lu", width, v->lu);
+ case FMT_FLOAT:
+ printf("%*.2f", width, v->f);
+ break;
+ case FMT_DOUBLE:
+ printf("%*.2f", width, v->d);
break;
default:
- fprintf(stderr, "Invalid format!\n");
+ nvme_show_error("Invalid format!\n");
break;
}
break;
diff --git a/util/table.h b/util/table.h
index a2ab2860f..045ed2439 100644
--- a/util/table.h
+++ b/util/table.h
@@ -10,6 +10,8 @@ enum fmt_type {
FMT_UNSIGNED,
FMT_LONG,
FMT_UNSIGNED_LONG,
+ FMT_FLOAT,
+ FMT_DOUBLE,
};
enum alignment {
@@ -25,6 +27,8 @@ struct value {
unsigned int u;
long ld;
unsigned long lu;
+ float f;
+ double d;
};
enum alignment align;
enum fmt_type type;
@@ -135,6 +139,28 @@ static inline void table_set_value_unsigned_long(struct table *t, int col,
v->type = FMT_UNSIGNED_LONG;
}
+static inline void table_set_value_float(struct table *t, int col,
+ int row, float f, enum alignment align)
+{
+ struct table_row *r = &t->rows[row];
+ struct value *v = &r->val[col];
+
+ v->f = f;
+ v->align = align;
+ v->type = FMT_FLOAT;
+}
+
+static inline void table_set_value_double(struct table *t, int col,
+ int row, double d, enum alignment align)
+{
+ struct table_row *r = &t->rows[row];
+ struct value *v = &r->val[col];
+
+ v->d = d;
+ v->align = align;
+ v->type = FMT_DOUBLE;
+}
+
struct table *table_create(void);
int table_add_columns(struct table *t, struct table_column *c, int num_columns);
int table_add_columns_filter(struct table *t, struct table_column *c,
--
2.53.0
More information about the Linux-nvme
mailing list