From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7823C433F5 for ; Wed, 20 Apr 2022 00:12:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14E986B0071; Tue, 19 Apr 2022 20:12:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0FF4D6B0072; Tue, 19 Apr 2022 20:12:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE2906B0074; Tue, 19 Apr 2022 20:12:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id DB2076B0071 for ; Tue, 19 Apr 2022 20:12:51 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id BE71082691 for ; Wed, 20 Apr 2022 00:12:51 +0000 (UTC) X-FDA: 79375331742.21.7580994 Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) by imf25.hostedemail.com (Postfix) with ESMTP id 67750A000F for ; Wed, 20 Apr 2022 00:12:49 +0000 (UTC) Received: by mail-qv1-f54.google.com with SMTP id a5so258564qvx.1 for ; Tue, 19 Apr 2022 17:12:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=+kZ8fsQMxa2G5tR/zWDa2wKSXSj5woL4fX7zC3ln+ug=; b=OWrrgdJJgG5OOmpoVOK+m7hnGruCyhof13q3Wdclz86DnV5uhieIWUtQacHyVpr5i/ Pja/HaaFgIyNPkuH8P2a5iKEse9Dt6T7So2XS3vHxyZi/0x4QtTXLo7PmNi+tQrp4lsM YykNUGfHwc+aECSmtx1pga0PWj0+OBK0KXW501WwC+hsMXmtvyXu85P4zIBspF+bC0mX AttBw4tTaxFyB4r5YP6JtnsKJpGd8OSzLOZi0RVSvMyNwFvZ2pA0kY9XEyHvhUMK5iZm qqvJeyN/b9p9y/a8AviA6wqpcIWCSmSdqrHaI5ubslaDYIAzSn1ipGb5qEUCEOVpl/0V yQfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=+kZ8fsQMxa2G5tR/zWDa2wKSXSj5woL4fX7zC3ln+ug=; b=Lvz6/iXmTPI3itud4qnbhEL5Ww9tFrqxCIE//lY4ffj0aJIBe+8hWFrHz6QRlpYlv+ LScuFxJCD4YvZTcgiiw/Ao83p7DXFxgy1YuXNs+t8aGFGxOCsSnD0nnrdguyeqnz6bMN euN4WkQ3x3bdzetOlTOMojcCBOuYuDkORT1w/s17EZun9P35Lds76BVtmVAHkeigS5d8 c/EQh6zpHr4zd0n4OuZfSXH7q1I7puTr9p0q1B1y6jihm3S2EBhs+aFUEV4eNIywCR9W Nt+rogAxkges4iUDFEsamMTns+iKvoWuA5gTwOGGPWbZxgOqUNKyLJJNVaxkztaDFPkp y5ww== X-Gm-Message-State: AOAM5309uG1xSawjsgpTDd/zhQvQ3aeIfTP1EHQiJOxVjFE//qZ2Nbmc WDw2/ERnq6B3okOqK4bnFw== X-Google-Smtp-Source: ABdhPJy0xATPQzogWwq9m+v+V1IUbI4cTaytqPhueLSR+e3gB8k3SxrBUTaT8oGvP/vU1CPQ4H0yUg== X-Received: by 2002:a05:6214:23ce:b0:441:8296:a11e with SMTP id hr14-20020a05621423ce00b004418296a11emr13432542qvb.16.1650413570709; Tue, 19 Apr 2022 17:12:50 -0700 (PDT) Received: from moria.home.lan (c-73-219-103-14.hsd1.vt.comcast.net. [73.219.103.14]) by smtp.gmail.com with ESMTPSA id 15-20020ac8594f000000b002f200ea2518sm960364qtz.59.2022.04.19.17.12.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Apr 2022 17:12:49 -0700 (PDT) Date: Tue, 19 Apr 2022 20:12:47 -0400 From: Kent Overstreet To: Matthew Wilcox 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 Message-ID: <20220420001247.h2rfftdjivkepre6@moria.home.lan> References: <20220419203202.2670193-1-kent.overstreet@gmail.com> <20220419203202.2670193-2-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 67750A000F X-Rspam-User: Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=OWrrgdJJ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.219.54 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com X-Stat-Signature: woofjmpf6jpcg6inndqck64owt1bpymh X-HE-Tag: 1650413569-481809 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Date: Tue Apr 19 17:29:43 2022 -0400 lib/printbuf: Switch to string_get_size() Signed-off-by: Kent Overstreet 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 #include +#include 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);