From: Kent Overstreet <kent.overstreet@gmail.com>
To: Matthew Wilcox <willy@infradead.org>
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-fsdevel@vger.kernel.org, roman.gushchin@linux.dev,
hannes@cmpxchg.org
Subject: Re: [PATCH 1/4] lib/printbuf: New data structure for heap-allocated strings
Date: Tue, 19 Apr 2022 20:12:47 -0400 [thread overview]
Message-ID: <20220420001247.h2rfftdjivkepre6@moria.home.lan> (raw)
In-Reply-To: <Yl8llyIN0vorOu/i@casper.infradead.org>
On Tue, Apr 19, 2022 at 10:11:51PM +0100, Matthew Wilcox wrote:
> On Tue, Apr 19, 2022 at 04:31:59PM -0400, Kent Overstreet wrote:
> > +static const char si_units[] = "?kMGTPEZY";
> > +
> > +void pr_human_readable_u64(struct printbuf *buf, u64 v)
>
> The person who wrote string_get_size() spent a lot more time thinking
> about corner-cases than you did ;-)
Didn't know about that until today :)
Just commited this:
commit 3cf1cb86ee4c4a7e75bcd52082ba0df1c436d692
Author: Kent Overstreet <kent.overstreet@gmail.com>
Date: Tue Apr 19 17:29:43 2022 -0400
lib/printbuf: Switch to string_get_size()
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h
index 84a271446d..2d23506114 100644
--- a/include/linux/printbuf.h
+++ b/include/linux/printbuf.h
@@ -91,11 +91,13 @@ struct printbuf {
enum printbuf_units units:8;
u8 atomic;
bool allocation_failure:1;
+ /* SI units (10^3), or 2^10: */
+ enum string_size_units human_readable_units:1;
u8 tabstop;
u8 tabstops[4];
};
-#define PRINTBUF ((struct printbuf) { NULL })
+#define PRINTBUF ((struct printbuf) { .human_readable_units = STRING_UNITS_2 })
/**
* printbuf_exit - exit a printbuf, freeing memory it owns and poisoning it
diff --git a/lib/printbuf.c b/lib/printbuf.c
index a9d5dff81e..ecbce6670f 100644
--- a/lib/printbuf.c
+++ b/lib/printbuf.c
@@ -10,6 +10,7 @@
#include <linux/log2.h>
#include <linux/printbuf.h>
+#include <linux/string_helpers.h>
static inline size_t printbuf_remaining(struct printbuf *buf)
{
@@ -144,27 +145,11 @@ void pr_tab_rjust(struct printbuf *buf)
}
EXPORT_SYMBOL(pr_tab_rjust);
-static const char si_units[] = "?kMGTPEZY";
-
void pr_human_readable_u64(struct printbuf *buf, u64 v)
{
- int u, t = 0;
-
- for (u = 0; v >= 1024; u++) {
- t = v & ~(~0U << 10);
- v >>= 10;
- }
-
- pr_buf(buf, "%llu", v);
-
- /*
- * 103 is magic: t is in the range [-1023, 1023] and we want
- * to turn it into [-9, 9]
- */
- if (u && t && v < 100 && v > -100)
- pr_buf(buf, ".%i", t / 103);
- if (u)
- pr_char(buf, si_units[u]);
+ printbuf_realloc(buf, 10);
+ string_get_size(v, 1, buf->human_readable_units, buf->buf + buf->pos,
+ printbuf_remaining(buf));
}
EXPORT_SYMBOL(pr_human_readable_u64);
next prev parent reply other threads:[~2022-04-20 0:12 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-19 20:31 [PATCH 0/4] Printbufs & shrinker OOM reporting Kent Overstreet
2022-04-19 20:31 ` [PATCH 1/4] lib/printbuf: New data structure for heap-allocated strings Kent Overstreet
2022-04-19 21:11 ` Matthew Wilcox
2022-04-20 0:12 ` Kent Overstreet [this message]
2022-04-20 5:02 ` Christoph Hellwig
2022-04-20 5:18 ` Kent Overstreet
2022-04-19 20:32 ` [PATCH 2/4] mm: Add a .to_text() method for shrinkers Kent Overstreet
2022-04-19 20:32 ` [PATCH 3/4] mm: Centralize & improve oom reporting in show_mem.c Kent Overstreet
2022-04-20 6:58 ` Michal Hocko
2022-04-20 16:58 ` Kent Overstreet
2022-04-21 9:18 ` Michal Hocko
2022-04-21 18:42 ` Kent Overstreet
2022-04-22 8:03 ` Michal Hocko
2022-04-22 8:30 ` Kent Overstreet
2022-04-22 9:27 ` Michal Hocko
2022-04-22 9:44 ` Kent Overstreet
2022-04-22 10:51 ` Michal Hocko
2022-04-22 10:58 ` Kent Overstreet
2022-04-19 20:32 ` [PATCH 4/4] bcachefs: shrinker.to_text() methods Kent Overstreet
2022-04-21 23:48 [PATCH 0/4] Printbufs & shrinker OOM reporting Kent Overstreet
2022-04-21 23:48 ` [PATCH 1/4] lib/printbuf: New data structure for heap-allocated strings 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=20220420001247.h2rfftdjivkepre6@moria.home.lan \
--to=kent.overstreet@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=roman.gushchin@linux.dev \
--cc=willy@infradead.org \
/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