From: Kent Overstreet <koverstreet@google.com>
To: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org,
dm-devel@redhat.com
Cc: Kent Overstreet <koverstreet@google.com>,
tejun@google.com, agk@redhat.com, dan.j.williams@intel.com
Subject: [Bcache v14 04/16] Add human-readable units modifier to vsnprintf()
Date: Tue, 12 Jun 2012 08:39:10 -0700 [thread overview]
Message-ID: <1339515562-14638-5-git-send-email-koverstreet@google.com> (raw)
In-Reply-To: <1339515562-14638-1-git-send-email-koverstreet@google.com>
Signed-off-by: Kent Overstreet <koverstreet@google.com>
---
lib/vsprintf.c | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index c3f36d41..16149dd 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -338,6 +338,7 @@ int num_to_str(char *buf, int size, unsigned long long num)
#define LEFT 16 /* left justified */
#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
+#define HUNITS 128 /* Human readable units, i.e. k/M/G/T */
enum format_type {
FORMAT_TYPE_NONE, /* Just a string part */
@@ -377,6 +378,7 @@ char *number(char *buf, char *end, unsigned long long num,
{
/* we are called with base 8, 10 or 16, only, thus don't need "G..." */
static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
+ static const char units[] = "?kMGTPEZY";
char tmp[66];
char sign;
@@ -431,7 +433,26 @@ char *number(char *buf, char *end, unsigned long long num,
num >>= shift;
} while (num);
} else { /* base 10 */
- i = put_dec(tmp, num) - tmp;
+ if (spec.flags & HUNITS) {
+ int u, rem = 0;
+
+ for (u = 0; num >= 1024; u++) {
+ rem = num & ~(~0 << 10);
+ num >>= 10;
+ }
+
+ if (u) {
+ tmp[i++] = units[u];
+
+ if (num < 100) {
+ rem /= 100;
+ i = put_dec(tmp + i, rem) - tmp;
+ tmp[i++] = '.';
+ }
+ }
+ }
+
+ i = put_dec(tmp + i, num) - tmp;
}
/* printing 100 using %2d gives "100", not "00" */
@@ -1127,6 +1148,7 @@ int format_decode(const char *fmt, struct printf_spec *spec)
case ' ': spec->flags |= SPACE; break;
case '#': spec->flags |= SPECIAL; break;
case '0': spec->flags |= ZEROPAD; break;
+ case 'h': spec->flags |= HUNITS; break;
default: found = false;
}
--
1.7.9.3.327.g2980b
next prev parent reply other threads:[~2012-06-12 15:43 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-12 15:39 [Bcache v14 00/16] Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 01/16] Revert "rw_semaphore: remove up/down_read_non_owner" Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 02/16] Fix ratelimit macro to compile in c99 mode Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 03/16] Export get_random_int() Kent Overstreet
2012-06-12 15:39 ` Kent Overstreet [this message]
2012-06-12 15:39 ` [Bcache v14 05/16] Export blk_fill_rwbs() Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 06/16] Export __lockdep_no_validate__ Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 07/16] Closures Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 08/16] bcache: Documentation, and changes to generic code Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 09/16] bcache: Generic utility code Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 10/16] bcache: Superblock/initialization/sysfs code Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 11/16] bcache: Core btree code Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 12/16] bcache: Bset code (lookups within a btree node) Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 13/16] bcache: Journalling Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 14/16] bcache: Request, io and allocation code Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 15/16] bcache: Writeback, copying garbage collection Kent Overstreet
2012-06-12 15:39 ` [Bcache v14 16/16] bcache: Debug and tracing code Kent Overstreet
2012-06-12 16:50 ` Joe Perches
2012-06-12 17:24 ` Kent Overstreet
2012-06-12 17:35 ` Joe Perches
2012-06-12 17:45 ` Kent Overstreet
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=1339515562-14638-5-git-send-email-koverstreet@google.com \
--to=koverstreet@google.com \
--cc=agk@redhat.com \
--cc=dan.j.williams@intel.com \
--cc=dm-devel@redhat.com \
--cc=linux-bcache@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tejun@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®