Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1309709

[PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]]

From Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Newsgroups linux.kernel
Subject [PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]]
Date 2016-01-14 23:30 +0100
Message-ID <qQYz0-5b5-15@gated-at.bofh.it> (permalink)
References <qQYz0-5b5-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


The user may supply the range of desired prefixes in a format %pl[From[To]],
where 'From' is letter of minimum allowed prefix, and 'To' is a maximum
correspondently.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 Documentation/printk-formats.txt |  4 ++++
 lib/test_printf.c                | 35 ++++++++++++++++++++++++++++++++---
 lib/vsprintf.c                   | 18 +++++++++++++++++-
 3 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/Documentation/printk-formats.txt b/Documentation/printk-formats.txt
index 8e8b4c0..3b41899 100644
--- a/Documentation/printk-formats.txt
+++ b/Documentation/printk-formats.txt
@@ -289,9 +289,13 @@ struct clk:
 
 unsigned long long value in human-readable form (with IEC binary prefix):
 
+	%pl[From[To]]
+
 	%pl		1023 KiB	(1047552)
 	%pl		1048575 B	(1048575)
+	%plKM		1023 KiB	(1048575)
 	%pl		1 MiB		(1048576)
+	%plG		2 TiB		(2199023255552)
 
 	For printing given unsigned long long value in human-readable form with
 	automatically choosen IEC binary prefix.
diff --git a/lib/test_printf.c b/lib/test_printf.c
index 1e078c2..ae35885 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -420,10 +420,39 @@ human_readable(void)
 		{ .v = 1048575ULL,		.r = "1048575 B" },
 		{ .v = 1047552ULL,		.r = "1023 KiB" },
 	};
-	unsigned int i;
+	struct hr_test_data htdplM[] = {
+		{ .v = 1097364144128ULL,	.r = "1022 GiB" },
+		{ .v = 1097364144125ULL,	.r = "1046527 MiB" },
+		{ .v = 1048576ULL,		.r = "1 MiB" },
+		{ .v = 1048575ULL,		.r = "0 MiB" },
+		{ .v = 1047552ULL,		.r = "0 MiB" },
+	};
+	struct hr_test_data htdplKM[] = {
+		{ .v = 1097364144128ULL,	.r = "1046528 MiB" },
+		{ .v = 1097364144125ULL,	.r = "1071644671 KiB" },
+		{ .v = 1048578ULL,		.r = "1024 KiB" },
+		{ .v = 1048576ULL,		.r = "1 MiB" },
+		{ .v = 1048575ULL,		.r = "1023 KiB" },
+		{ .v = 1047552ULL,		.r = "1023 KiB" },
+	};
+	struct hr_test_array {
+		const char *fmt;
+		struct hr_test_data *data;
+		size_t sz;
+	};
+	struct hr_test_array hta[] = {
+		{ "%pl",	htdpl,		ARRAY_SIZE(htdpl) },
+		{ "%plM",	htdplM,		ARRAY_SIZE(htdplM) },
+		{ "%plKM",	htdplKM,	ARRAY_SIZE(htdplKM) },
+		/* No reversed %pl[To[From]] order */
+		{ "%plMK",	htdplM,		ARRAY_SIZE(htdplM) },
+	};
+	unsigned int i, j;
 
-	for (i = 0; i < ARRAY_SIZE(htdpl); i++)
-		test(htdpl[i].r, "%pl", &htdpl[i].v);
+	for (j = 0; j < ARRAY_SIZE(hta); j++) {
+		for (i = 0; i < hta[j].sz; i++)
+			test(hta[j].data[i].r, hta[j].fmt, &hta[j].data[i].v);
+	}
 }
 
 static void __init
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 8288470..fbcb221 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1310,12 +1310,28 @@ char *human_readable(char *buf, char *end, const void *addr,
 		     struct printf_spec spec, const char *fmt)
 {
 	unsigned long long value = *(unsigned long long *)addr;
+	unsigned short from = 0, to = STRING_UNITS_2_NUM - 1;
 	unsigned long index;
+	unsigned int i;
+
+	/* Parse %pl[From[To]] */
+	for (i = 0; i < STRING_UNITS_2_NUM; i++)
+		if (fmt[1] == string_units_2[i][0])
+			break;
+	if (i < STRING_UNITS_2_NUM) {
+		from = i;
+		for (i = 0; i < STRING_UNITS_2_NUM; i++)
+			if (fmt[2] == string_units_2[i][0])
+				break;
+		if (i < STRING_UNITS_2_NUM && i >= from)
+			to = i;
+	}
 
 	if (value)
 		index = __ffs64(value) / 10;
 	else
 		index = 0;
+	index = clamp_t(unsigned long, index, from, to);
 
 	/* Print numeric part */
 	buf = number(buf, end, value >> (index * 10), default_dec_spec);
@@ -1459,7 +1475,7 @@ int kptr_restrict __read_mostly;
  *       Do not use this feature without some mechanism to verify the
  *       correctness of the format string and va_list arguments.
  * - 'K' For a kernel pointer that should be hidden from unprivileged users
- * - 'l' For a value in human-readable form (with IEC binary prefix)
+ * - 'l[from[to]]' For a value in human-readable form (with IEC binary prefix)
  * - 'NF' For a netdev_features_t
  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
  *            a certain separator (' ' by default):
-- 
2.6.4

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2 00/11] lib/vsprintf: refactor and introduce %pl specifier Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]] Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]] Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 22:50 +0100
  [PATCH v2 02/11] lib/vsprintf: make default_dec_spec global Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 02/11] lib/vsprintf: make default_dec_spec global Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 21:40 +0100
  [PATCH v2 11/11] pcmciamtd: print value in human-readable form via %.0plKM Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 04/11] lib/string_helpers: export string_units_{2,10} for others Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 03/11] lib/vsprintf: make default_str_spec global Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 07/11] x86/efi: print size and base in binary units in efi_print_memmap Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 10/11] cxgb4: print value in human-readable form via %.0plKM Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 05/11] lib/string_helpers: fix indentation in few places Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in human-readable form Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in  human-readable form Joe Perches <joe@perches.com> - 2016-01-15 04:10 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in human-readable form Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 22:00 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in  human-readable form "H. Peter Anvin" <hpa@zytor.com> - 2016-01-21 22:20 +0100
  [PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]] Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]] Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 23:30 +0100
  [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 21:40 +0100
  Re: [PATCH v2 00/11] lib/vsprintf: refactor and introduce %pl  specifier Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-21 14:00 +0100

csiph-web