hyptop: Keep track of string-typed columns

For structured output (i.e. JSON), some type information of the columns
is required, at least the ability to distinguish values that are strings
and possibly need to be quoted. Unfortunately at the time column values
are formatted this information is lost.

The column types are specified implicitly during the unit conversion of
the raw value. For example, online time is stored as a u64 value but is
converted into a "d:h:m" string.

Introduce a private flag per column that signifies if that column is to
be formatted as a string. This flag will be set in the appropriate unit
conversion function.

Reviewed-by: Mete Durlu <meted@linux.ibm.com>
Signed-off-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Bjoern Walk
2024-05-01 07:34:51 +02:00
committed by Jan Höppner
parent d864c55336
commit 57eef7c412
2 changed files with 11 additions and 1 deletions

View File

@@ -106,6 +106,7 @@ struct table_col_priv {
char head_char[2];
char head_last[TABLE_HEADING_SIZE];
int rsort;
int needs_quotes;
};
/*
@@ -135,6 +136,11 @@ static inline int table_col_enabled(struct table_col *col)
return col->p->enabled;
}
static inline int table_col_needs_quotes(struct table_col *col)
{
return col->p->needs_quotes;
}
/*
* Table Column Constructor Macros
*/

View File

@@ -63,7 +63,7 @@ static int l_unit_raw(struct table_col *col, struct table_entry *e)
*/
static int l_str(struct table_col *col, struct table_entry *e)
{
(void) col;
col->p->needs_quotes = 1;
return strlen(e->str);
}
@@ -226,6 +226,8 @@ static int l_unit_hm_u64(char *str, u64 v1, int negative)
static int l_unit_hm(struct table_col *col, struct table_entry *e)
{
col->p->needs_quotes = 1;
if (!e->set)
return snprintf(e->str, sizeof(e->str), L_COL_NOT_SET_STR);
@@ -272,6 +274,8 @@ static int l_unit_dhm_u64(char *str, u64 v1, int negative)
static int l_unit_dhm(struct table_col *col, struct table_entry *e)
{
col->p->needs_quotes = 1;
if (!e->set)
return snprintf(e->str, sizeof(e->str), L_COL_NOT_SET_STR);