From: Andre Muezerie <andremue@linux.microsoft.com>
To: andremue@linux.microsoft.com
Cc: bruce.richardson@intel.com, dev@dpdk.org,
sameh.gobriel@intel.com, vladimir.medvedkin@intel.com,
yipeng1.wang@intel.com
Subject: [PATCH v5 1/3] eal: add function rte_size_to_str
Date: Tue, 8 Apr 2025 18:20:56 -0700 [thread overview]
Message-ID: <1744161658-7874-2-git-send-email-andremue@linux.microsoft.com> (raw)
In-Reply-To: <1744161658-7874-1-git-send-email-andremue@linux.microsoft.com>
It's common to use %' in the printf format specifier to make large numbers
more easily readable by having the thousands grouped. However, this
grouping does not work on Windows. Therefore, a function is needed to make
uint64_t numbers more easily readable. There are at least two tests that
can benefit from this new function.
Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
lib/eal/common/eal_common_string_fns.c | 54 ++++++++++++++++++++++++++
lib/eal/include/rte_common.h | 34 ++++++++++++++++
2 files changed, 88 insertions(+)
diff --git a/lib/eal/common/eal_common_string_fns.c b/lib/eal/common/eal_common_string_fns.c
index 020603fe8b..ec17382487 100644
--- a/lib/eal/common/eal_common_string_fns.c
+++ b/lib/eal/common/eal_common_string_fns.c
@@ -4,6 +4,7 @@
#include <ctype.h>
#include <errno.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
@@ -91,6 +92,12 @@ rte_str_to_size(const char *str)
endptr++; /* allow 1 space gap */
switch (*endptr) {
+ case 'E': case 'e':
+ size *= 1024; /* fall-through */
+ case 'P': case 'p':
+ size *= 1024; /* fall-through */
+ case 'T': case 't':
+ size *= 1024; /* fall-through */
case 'G': case 'g':
size *= 1024; /* fall-through */
case 'M': case 'm':
@@ -102,3 +109,50 @@ rte_str_to_size(const char *str)
}
return size;
}
+
+RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_size_to_str, 25.07)
+char*
+rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const char *unit)
+{
+ /* https://en.wikipedia.org/wiki/International_System_of_Units */
+ const char *prefix = "kMGTPE";
+ const unsigned int base = use_iec ? 1024 : 1000;
+ uint64_t powi = 1;
+ uint16_t powj = 1;
+ uint8_t precision = 2;
+ int result;
+
+ if (count < base) {
+ if (unit != NULL && *unit != '\0')
+ result = snprintf(buf, buf_size, "%"PRIu64" %s", count, unit);
+ else
+ result = snprintf(buf, buf_size, "%"PRIu64, count);
+
+ return result < buf_size ? buf : NULL;
+ }
+
+ /* increase value by a factor of 1000/1024 and store
+ * if result is something a human can read
+ */
+ for (;;) {
+ powi *= base;
+ if (count / powi < base)
+ break;
+
+ if (prefix[1] == '\0')
+ break;
+ ++prefix;
+ }
+
+ /* try to guess a good number of digits for precision */
+ for (; precision > 0; precision--) {
+ powj *= 10;
+ if (count / powi < powj)
+ break;
+ }
+
+ result = snprintf(buf, buf_size, "%.*f %c%s%s", precision,
+ (double)count / powi, *prefix, use_iec ? "i" : "",
+ (unit != NULL) ? unit : "");
+ return result < buf_size ? buf : NULL;
+}
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index 386f11ae40..a6085dce27 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -14,9 +14,11 @@
#include <assert.h>
#include <limits.h>
+#include <stdbool.h>
#include <stdint.h>
#include <stdalign.h>
+#include <rte_compat.h>
#include <rte_config.h>
/* OS specific include */
@@ -919,6 +921,38 @@ __extension__ typedef uint64_t RTE_MARKER64[0];
uint64_t
rte_str_to_size(const char *str);
+/**
+ * Converts the uint64_t value provided to a human-readable string.
+ * It null-terminates the string, truncating the data if needed.
+ * An optional unit (like "B") can be provided as a string. It will be
+ * appended to the number, and a space will be inserted before the unit if needed.
+ *
+ * Sample outputs: (1) "use_iec" disabled, (2) "use_iec" enabled,
+ * (3) "use_iec" enabled and "B" as unit.
+ * 0 : "0", "0", "0 B"
+ * 700 : "700", "700", "700 B"
+ * 1000 : "1.00 k", "1000", "1000 B"
+ * 1024 : "1.02 k", "1.00 ki", "1.00 kiB"
+ * 21474836480 : "21.5 G", "20.0 Gi", "20.0 GiB"
+ * 109951162777600 : "110 T", "100 Ti", "100 TiB"
+ *
+ * @param buf
+ * Buffer to write the string to.
+ * @param buf_size
+ * Size of the buffer.
+ * @param count
+ * Number to convert.
+ * @param use_iec
+ * If true, use IEC units (1024-based), otherwise use SI units (1000-based).
+ * @param unit
+ * Unit to append to the string (Like "B" for bytes). Can be NULL.
+ * @return
+ * buf on success, NULL if the buffer is too small.
+ */
+__rte_experimental
+char *
+rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const char *unit);
+
/**
* Function to terminate the application immediately, printing an error
* message and returning the exit_code back to the shell.
--
2.49.0.vfs.0.0
next prev parent reply other threads:[~2025-04-09 1:21 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-06 20:03 [PATCH] hash_readwrite_autotest: fix printf parameters Andre Muezerie
2025-03-07 9:01 ` Bruce Richardson
2025-03-07 22:34 ` Andre Muezerie
2025-03-10 10:51 ` Bruce Richardson
2025-03-10 15:36 ` Stephen Hemminger
2025-03-11 14:39 ` Andre Muezerie
2025-03-11 15:01 ` Morten Brørup
2025-03-11 15:33 ` [PATCH v2 1/3] eal: add function rte_size_to_str Andre Muezerie
2025-03-11 15:33 ` [PATCH v2 2/3] hash_multiwriter_autotest: fix printf parameters Andre Muezerie
2025-03-11 15:33 ` [PATCH v2 3/3] hash_readwrite_autotest: " Andre Muezerie
2025-03-11 15:49 ` [PATCH v2 1/3] eal: add function rte_size_to_str Stephen Hemminger
2025-03-11 15:51 ` Bruce Richardson
2025-03-11 16:21 ` Morten Brørup
2025-03-12 19:28 ` [PATCH v3 0/3] fix how large numbers are printed by hash tests Andre Muezerie
2025-03-12 19:28 ` [PATCH v3 1/3] eal: add function rte_size_to_str Andre Muezerie
2025-03-13 9:09 ` Bruce Richardson
2025-03-13 10:07 ` Morten Brørup
2025-03-13 10:35 ` Bruce Richardson
2025-03-14 11:56 ` Mattias Rönnblom
2025-03-13 10:41 ` Bruce Richardson
2025-03-13 14:06 ` Andre Muezerie
2025-03-12 19:28 ` [PATCH v3 2/3] hash_multiwriter_autotest: fix printf parameters Andre Muezerie
2025-03-12 19:28 ` [PATCH v3 3/3] hash_readwrite_autotest: " Andre Muezerie
2025-03-13 16:51 ` [PATCH v4 0/3] fix how large numbers are printed by hash tests Andre Muezerie
2025-03-13 16:51 ` [PATCH v4 1/3] eal: add function rte_size_to_str Andre Muezerie
2025-03-13 16:59 ` Morten Brørup
2025-03-13 16:51 ` [PATCH v4 2/3] hash_multiwriter_autotest: fix printf parameters Andre Muezerie
2025-03-13 16:51 ` [PATCH v4 3/3] hash_readwrite_autotest: " Andre Muezerie
2025-03-13 17:33 ` [PATCH v4 0/3] fix how large numbers are printed by hash tests Bruce Richardson
2025-04-09 1:20 ` [PATCH v5 " Andre Muezerie
2025-04-09 1:20 ` Andre Muezerie [this message]
2025-04-09 1:20 ` [PATCH v5 2/3] hash_multiwriter_autotest: fix printf parameters Andre Muezerie
2025-04-09 1:20 ` [PATCH v5 3/3] hash_readwrite_autotest: " Andre Muezerie
2025-04-10 11:34 ` [PATCH v5 0/3] fix how large numbers are printed by hash tests David Marchand
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1744161658-7874-2-git-send-email-andremue@linux.microsoft.com \
--to=andremue@linux.microsoft.com \
--cc=bruce.richardson@intel.com \
--cc=dev@dpdk.org \
--cc=sameh.gobriel@intel.com \
--cc=vladimir.medvedkin@intel.com \
--cc=yipeng1.wang@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.