From 3f19715e45e8a64c4f6830d99e22daa1f10252c0 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 29 Aug 2025 11:36:10 +0800 Subject: [PATCH] add function bytes_to_human_str --- HISTORY | 3 ++- src/common_define.h | 7 +++++ src/shared_func.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ src/shared_func.h | 2 ++ 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/HISTORY b/HISTORY index 8a629f7..b2e7121 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ -Version 1.79 2025-08-20 +Version 1.79 2025-08-29 * logger.h export function log_it_ex3 + * shared_func.[hc]: add function bytes_to_human_str Version 1.78 2025-08-07 * getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6 diff --git a/src/common_define.h b/src/common_define.h index 2016254..0913390 100644 --- a/src/common_define.h +++ b/src/common_define.h @@ -112,6 +112,13 @@ extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind); #define SYNC_LOG_BUFF_DEF_INTERVAL 10 #define TIME_NONE -1 +#define FC_BYTES_ONE_KB ( 1 << 10) +#define FC_BYTES_ONE_MB ( 1 << 20) +#define FC_BYTES_ONE_GB ( 1 << 30) +#define FC_BYTES_ONE_TB (1LL << 40) +#define FC_BYTES_ONE_PB (1LL << 50) +#define FC_BYTES_ONE_EB (1LL << 60) + #if defined(IOV_MAX) && IOV_MAX > 256 #define FC_IOV_BATCH_SIZE 256 #else diff --git a/src/shared_func.c b/src/shared_func.c index c27282f..e5d4f65 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -3251,6 +3251,72 @@ const char *long2str(const int64_t n, char *buff, const bool thousands_separator return buff; } +const char *bytes_to_human_str(const int64_t bytes, char *buff) +{ + char *p; + + if (bytes < FC_BYTES_ONE_TB) + { + if (bytes < FC_BYTES_ONE_MB) + { + if (bytes < FC_BYTES_ONE_KB) + { + p = buff + fc_itoa(bytes, buff); + *p++ = ' '; + } + else + { + p = buff + fc_itoa(bytes / FC_BYTES_ONE_KB, buff); + *p++ = ' '; + *p++ = 'K'; + } + } + else + { + if (bytes < FC_BYTES_ONE_GB) + { + p = buff + fc_itoa(bytes / FC_BYTES_ONE_MB, buff); + *p++ = ' '; + *p++ = 'M'; + } + else + { + p = buff + fc_itoa(bytes / FC_BYTES_ONE_GB, buff); + *p++ = ' '; + *p++ = 'G'; + } + } + } + else + { + if (bytes < FC_BYTES_ONE_EB) + { + if (bytes < FC_BYTES_ONE_PB) + { + p = buff + fc_itoa(bytes / FC_BYTES_ONE_TB, buff); + *p++ = ' '; + *p++ = 'T'; + } + else + { + p = buff + fc_itoa(bytes / FC_BYTES_ONE_PB, buff); + *p++ = ' '; + *p++ = 'P'; + } + } + else + { + p = buff + fc_itoa(bytes / FC_BYTES_ONE_EB, buff); + *p++ = ' '; + *p++ = 'E'; + } + } + *p++ = 'B'; + *p = '\0'; + + return buff; +} + bool starts_with(const char *str, const char *needle) { int str_len; diff --git a/src/shared_func.h b/src/shared_func.h index 77ecb01..a2df779 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -1191,6 +1191,8 @@ static inline const char *long_to_comma_str(const int64_t n, char *buff) return long2str(n, buff, true); } +const char *bytes_to_human_str(const int64_t bytes, char *buff); + /** if the string starts with the needle string * parameters: * str: the string to detect