From 7b3add9389143db8c3b17bdd20a141dae063958c Mon Sep 17 00:00:00 2001 From: Mete Durlu Date: Wed, 3 Dec 2025 12:48:32 +0100 Subject: [PATCH] hyptop: Use util_time helpers for time calculations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the common helpers in lib/util_time instead of using hardcoded values for time calculations. Reviewed-by: Jan Höppner Signed-off-by: Mete Durlu Signed-off-by: Steffen Eiden --- hyptop/helper.h | 1 + hyptop/hyptop.c | 2 +- hyptop/sd_cpu_items.c | 2 +- hyptop/sd_sys_items.c | 4 ++-- hyptop/table_col_unit.c | 18 ++++++++---------- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/hyptop/helper.h b/hyptop/helper.h index 34672907..43cdb0ff 100644 --- a/hyptop/helper.h +++ b/hyptop/helper.h @@ -18,6 +18,7 @@ #include "lib/util_base.h" #include "lib/util_libc.h" +#include "lib/util_time.h" #include "lib/zt_common.h" #define G0(x) MAX(0, (s64) (x)) diff --git a/hyptop/hyptop.c b/hyptop/hyptop.c index 0cd7679c..a7fe6f8f 100644 --- a/hyptop/hyptop.c +++ b/hyptop/hyptop.c @@ -112,7 +112,7 @@ static enum hyptop_win_action l_sleep(time_t time_s, long time_us) struct timespec ts; ts.tv_sec = time_s; - ts.tv_nsec = time_us * 1000; + ts.tv_nsec = util_usecs_to_nsecs(time_us); nanosleep(&ts, NULL); return WIN_KEEP; diff --git a/hyptop/sd_cpu_items.c b/hyptop/sd_cpu_items.c index 380c296d..04af9777 100644 --- a/hyptop/sd_cpu_items.c +++ b/hyptop/sd_cpu_items.c @@ -48,7 +48,7 @@ static double l_cpu_diff(struct sd_cpu_item *item, struct sd_cpu *cpu, int sign) if (online_time_diff_us == 0) return 0; - factor = ((double) online_time_diff_us) / 1000000 * cpu->cnt; + factor = util_usecs_to_secs(online_time_diff_us) * cpu->cnt; if (sign) diff_us = l_cpu_info_s64(cpu->d_cur, item->offset) - l_cpu_info_s64(cpu->d_prev, item->offset); diff --git a/hyptop/sd_sys_items.c b/hyptop/sd_sys_items.c index aa90f110..994bb0df 100644 --- a/hyptop/sd_sys_items.c +++ b/hyptop/sd_sys_items.c @@ -171,7 +171,7 @@ static double l_cpu_info_diff_u64(struct sd_sys_item *item, struct sd_cpu *cpu, diff_us = l_sub_64(l_cpu_info_u64(cpu->d_cur, item->offset), l_cpu_info_u64(cpu->d_prev, item->offset)); } - factor = ((double) online_time_diff_us) / 1000000; + factor = util_usecs_to_secs(online_time_diff_us); diff_us /= factor; return diff_us; } @@ -237,7 +237,7 @@ static double l_phys_cpu_info_diff_u64(struct sd_sys_item *item, return 0; diff_us = l_sub_64(l_cpu_info_u64(cpu->d_cur, item->offset), l_cpu_info_u64(cpu->d_prev, item->offset)); - factor = ((double)time_diff_us) / 1000000; + factor = util_usecs_to_secs(time_diff_us); diff_us /= factor; return diff_us; } diff --git a/hyptop/table_col_unit.c b/hyptop/table_col_unit.c index 0f460ab2..9c57d90d 100644 --- a/hyptop/table_col_unit.c +++ b/hyptop/table_col_unit.c @@ -153,7 +153,7 @@ struct table_col_unit table_col_unit_us = { */ static int l_unit_ms(struct table_col *col, struct table_entry *e) { - return l_unit_raw_div(col, e, 1000, L_COL_FMT_STR_2); + return l_unit_raw_div(col, e, USEC_PER_MSEC, L_COL_FMT_STR_2); } struct table_col_unit table_col_unit_ms = { @@ -167,7 +167,7 @@ struct table_col_unit table_col_unit_ms = { */ static int l_unit_perc(struct table_col *col, struct table_entry *e) { - return l_unit_raw_div(col, e, 10000, L_COL_FMT_STR_2); + return l_unit_raw_div(col, e, (USEC_PER_SEC / 100), L_COL_FMT_STR_2); } struct table_col_unit table_col_unit_perc = { @@ -182,7 +182,7 @@ struct table_col_unit table_col_unit_perc = { */ static int l_unit_s(struct table_col *col, struct table_entry *e) { - return l_unit_raw_div(col, e, 1000000, L_COL_FMT_STR_2); + return l_unit_raw_div(col, e, USEC_PER_SEC, L_COL_FMT_STR_2); } struct table_col_unit table_col_unit_s = { @@ -197,7 +197,7 @@ struct table_col_unit table_col_unit_s = { */ static int l_unit_m(struct table_col *col, struct table_entry *e) { - return l_unit_raw_div(col, e, 1000000 * 60, L_COL_FMT_STR_0); + return l_unit_raw_div(col, e, USEC_PER_SEC * 60, L_COL_FMT_STR_0); } static struct table_col_unit table_col_unit_m = { @@ -214,7 +214,7 @@ static int l_unit_hm_u64(char *str, u64 v1, int negative) { u64 time_tmp, time_h, time_m; - time_tmp = v1 / (1000000 * 60); + time_tmp = v1 / (USEC_PER_SEC * 60); time_h = time_tmp / 60; time_m = time_tmp - time_h * 60; @@ -259,7 +259,7 @@ static int l_unit_dhm_u64(char *str, u64 v1, int negative) { u64 time_tmp, time_d, time_h, time_m; - time_tmp = v1 / (1000000 * 60); + time_tmp = v1 / (USEC_PER_SEC * 60); time_d = time_tmp / (60 * 24); time_h = time_tmp / 60 - time_d * 24; time_m = time_tmp - time_h * 60 - time_d * 60 * 24; @@ -312,10 +312,8 @@ static int l_unit_vis(struct table_col *col, struct table_entry *e) assert(col->type == TABLE_COL_TYPE_U64); sprintf(e->str, "|"); - val1_perc = e->d.u64.v1; - val1_perc /= 1000000; - val2_perc = e->d.u64.v2; - val2_perc /= 1000000; + val1_perc = util_usecs_to_secs(e->d.u64.v1); + val2_perc = util_usecs_to_secs(e->d.u64.v2); val1_nr = (val1_perc * L_VISUAL_ROW_CNT) + 0.5; val2_nr = (val2_perc * L_VISUAL_ROW_CNT) + 0.5;