Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2428826iog; Sun, 19 Jun 2022 17:45:18 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uRtsaz/bdYxzdiWr9pO889XHxBChkQqHJN3GkqFYYO9nS+QbpbNSzN2I7VkIm5ogJOMYGT X-Received: by 2002:aa7:cb95:0:b0:434:ef34:664f with SMTP id r21-20020aa7cb95000000b00434ef34664fmr25827318edt.236.1655685918189; Sun, 19 Jun 2022 17:45:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655685918; cv=none; d=google.com; s=arc-20160816; b=AcfaO9DCTbedn3LQXKoptzyCD0+oUgj4nAJy816qr+YzTQ9x/olTU0W6zkpIgJyfyE B71MMEsczNLYr2eSZ7vkfo/C71R0g+SvueYZ7ydWAQCWz86fdnQaC5x55Go+1ySPuL4U fspTRNdjNHWt5scRV8aU3NbrvHuLs08w5EeiECRiF/NGQQKxW4YUuQo0uAMZkjIAZaCH lyWalrWOqWzPpkSgiMwnf7OlsyGBxS9hktIWH9r72F1StlqTpHW6IyOPTR7ciLooixz/ ZecmCFpvU20pSh+eMM0S2ITASVorHicTyIOjvNa8jAkE6/dE32couD6lL8A5lXjMky1I GU3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fmnZsBS7ICfhtPbFVJE00Af049vTUvEMSfkA9G33Z+Y=; b=fe/C0Z5jZYeeYyb7xeZKKLeKjpmOScDV2yTAN9X4JO/eg6/AdKEfeD9JTRBOzphbtw 4MKAstrkVBbmAIE1w8TZ8pUEynYAHX7+cdwatrfZktqQFaGs0IPeuITS3Y60g38Bh96o n1LXc4L+NgtIBuz+Tjxmu6kdtD3j7qRKgMFCk1Yz8KSwQokFkLpVoHCpHOuOzDYhPo4K bvAJU2Te0y4O/6rUIkKCv3KtWaaANJG8gWr9FaEVE+MHXDUs4RUrTNADAMH8Hv4BOOsy AhMerUuf5LXC2yoVSl8osQJ5BYWWMz8HFGcBAb+69NZfDteQW8vpU5QKSKxIKBD38y+s hoyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=FS+p1cBl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hp40-20020a1709073e2800b0071200c979b1si9254257ejc.644.2022.06.19.17.44.53; Sun, 19 Jun 2022 17:45:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=FS+p1cBl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237963AbiFTAnV (ORCPT + 99 others); Sun, 19 Jun 2022 20:43:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237747AbiFTAnI (ORCPT ); Sun, 19 Jun 2022 20:43:08 -0400 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F1466585 for ; Sun, 19 Jun 2022 17:42:54 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id d128so6880176qkg.8 for ; Sun, 19 Jun 2022 17:42:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fmnZsBS7ICfhtPbFVJE00Af049vTUvEMSfkA9G33Z+Y=; b=FS+p1cBlMgrvZr0p4b+tva3wEIao0KwdEJw4GNGD0MkRDG+Q78eE9bMbYmNKtz1DFW GYcGcOEivpoUJV4yz/3AMofzVI9M8D5IHkPwmR43g0hZNzL2RqObGCtM+s3hyXdZq783 xLNOzqLaOM6J7c3kv6aoM5vBqMw0zgVGzD2LoWbaO7xkjPaZXtnuAlFvKDfkD8uYtTZd ZRCJVXDTZy/0Ev8v0CWU9Eb7XNDiYE0PjEzKuUhKWCrILL/AKr1H5WNdMMggHtDpzHM/ clj7iSjtJR+pHdqJp9SNzOJ7LOOnh768E6/lULh9I5d3iDT6/zzudxsdIJHoxuM45QLq OktA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fmnZsBS7ICfhtPbFVJE00Af049vTUvEMSfkA9G33Z+Y=; b=tttIGVoLdvN6moR2cU9O/Uyey8/Vx9xnmIBxY5KMiAWYszEfOq7TmUereCavKq0c5V HbZMPMTRY2uZTO/BjBdz2VqjhASSIRgfqS4pzF1Acw8rLr+sC/TFRCf2LXnWKjDYLpu3 Fpi20Yzo1mMBHQCL2XwPj2qN7HXEPfY6SUSibUy2idxk8C+V6PRRBJSl88bmTxLoJZQr DnxtPS/B3FRNO3gUp70bKPUKs1rEoJIutiNojNBsANnjlntw3TEv2I6eGnBkMa5n61jt UMl8UUNjulT4vJTTLsw+/DZWOGXAwHc4/tyGuUsmAAnbwgIOg4i8Gjxq5DGOeVsNhxb+ oHQg== X-Gm-Message-State: AJIora/wMfd6uOj+IEwhJMxMnBE/rqLV+7rZoaSwk5tkhREARFaI/wYx YKPAe+5ohGCu93wPdRVnjJCt9tYmMZ3Vae8= X-Received: by 2002:a05:620a:d94:b0:6a6:6c9c:c7ec with SMTP id q20-20020a05620a0d9400b006a66c9cc7ecmr14807435qkl.221.1655685772861; Sun, 19 Jun 2022 17:42:52 -0700 (PDT) Received: from localhost (c-73-219-103-14.hsd1.vt.comcast.net. [73.219.103.14]) by smtp.gmail.com with ESMTPSA id i21-20020a05620a405500b0069fe1dfbeffsm11316870qko.92.2022.06.19.17.42.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 17:42:52 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, pmladek@suse.com Cc: Kent Overstreet , rostedt@goodmis.org, enozhatsky@chromium.org, linux@rasmusvillemoes.dk, willy@infradead.org Subject: [PATCH v4 09/34] lib/printbuf: Unit specifiers Date: Sun, 19 Jun 2022 20:42:08 -0400 Message-Id: <20220620004233.3805-10-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620004233.3805-1-kent.overstreet@gmail.com> References: <20220620004233.3805-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds options to printbuf for specifying whether units should be printed raw (default) or with human readable units, and for controlling whether human-readable units should be base 2 (default), or base 10. This also adds new helpers that obey these options: - pr_human_readable_u64 - pr_human_readable_s64 These obey printbuf->si_units - pr_units_u64 - pr_units_s64 These obey both printbuf-human_readable_units and printbuf->si_units Signed-off-by: Kent Overstreet --- include/linux/printbuf.h | 15 +++++++++++ lib/printbuf.c | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h index 1e43c47891..5100287eed 100644 --- a/include/linux/printbuf.h +++ b/include/linux/printbuf.h @@ -53,11 +53,20 @@ * * Make sure you use prt_newline() instead of \n in the format string for indent * level and tabstops to work corretly. + * + * Output units: printbuf->units exists to tell pretty-printers how to output + * numbers: a raw value (e.g. directly from a superblock field), as bytes, or as + * human readable bytes. prt_units() obeys it. */ #include #include +enum printbuf_si { + PRINTBUF_UNITS_2, /* use binary powers of 2^10 */ + PRINTBUF_UNITS_10, /* use powers of 10^3 (standard SI) */ +}; + struct printbuf { char *buf; unsigned size; @@ -71,6 +80,8 @@ struct printbuf { u8 atomic; bool allocation_failure:1; bool heap_allocated:1; + enum printbuf_si si_units:1; + bool human_readable_units:1; u8 tabstop; u8 tabstops[4]; }; @@ -84,6 +95,10 @@ void printbuf_indent_add(struct printbuf *, unsigned); void printbuf_indent_sub(struct printbuf *, unsigned); void prt_tab(struct printbuf *); void prt_tab_rjust(struct printbuf *); +void prt_human_readable_u64(struct printbuf *, u64); +void prt_human_readable_s64(struct printbuf *, s64); +void prt_units_u64(struct printbuf *, u64); +void prt_units_s64(struct printbuf *, s64); /* Initializer for a heap allocated printbuf: */ #define PRINTBUF ((struct printbuf) { .heap_allocated = true }) diff --git a/lib/printbuf.c b/lib/printbuf.c index a7f80f63ca..553f89ebc1 100644 --- a/lib/printbuf.c +++ b/lib/printbuf.c @@ -10,6 +10,7 @@ #include #include +#include #include static inline size_t printbuf_linelen(struct printbuf *buf) @@ -194,3 +195,59 @@ void prt_tab_rjust(struct printbuf *buf) buf->tabstop++; } EXPORT_SYMBOL(prt_tab_rjust); + +/** + * prt_human_readable_u64 - Print out a u64 in human readable units + * + * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units + */ +void prt_human_readable_u64(struct printbuf *buf, u64 v) +{ + printbuf_make_room(buf, 10); + buf->pos += string_get_size(v, 1, !buf->si_units, + buf->buf + buf->pos, + printbuf_remaining_size(buf)); +} +EXPORT_SYMBOL(prt_human_readable_u64); + +/** + * prt_human_readable_s64 - Print out a s64 in human readable units + * + * Units of 2^10 (default) or 10^3 are controlled via @buf->si_units + */ +void prt_human_readable_s64(struct printbuf *buf, s64 v) +{ + if (v < 0) + prt_char(buf, '-'); + prt_human_readable_u64(buf, abs(v)); +} +EXPORT_SYMBOL(prt_human_readable_s64); + +/** + * prt_units_u64 - Print out a u64 according to printbuf unit options + * + * Units are either raw (default), or human reabable units (controlled via + * @buf->human_readable_units) + */ +void prt_units_u64(struct printbuf *out, u64 v) +{ + if (out->human_readable_units) + prt_human_readable_u64(out, v); + else + prt_printf(out, "%llu", v); +} +EXPORT_SYMBOL(prt_units_u64); + +/** + * prt_units_s64 - Print out a s64 according to printbuf unit options + * + * Units are either raw (default), or human reabable units (controlled via + * @buf->human_readable_units) + */ +void prt_units_s64(struct printbuf *out, s64 v) +{ + if (v < 0) + prt_char(out, '-'); + prt_units_u64(out, abs(v)); +} +EXPORT_SYMBOL(prt_units_s64); -- 2.36.1