From patchwork Mon Jun 20 00:42:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 12886900 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 070ACC433EF for ; Mon, 20 Jun 2022 00:42:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A7656B0080; Sun, 19 Jun 2022 20:42:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9593C6B0081; Sun, 19 Jun 2022 20:42:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75B2F6B0082; Sun, 19 Jun 2022 20:42:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 68FC86B0080 for ; Sun, 19 Jun 2022 20:42:54 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3E58E6CC for ; Mon, 20 Jun 2022 00:42:54 +0000 (UTC) X-FDA: 79596764268.21.85BAFE2 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) by imf20.hostedemail.com (Postfix) with ESMTP id 8A0D61C00A4 for ; Mon, 20 Jun 2022 00:42:53 +0000 (UTC) Received: by mail-qk1-f178.google.com with SMTP id n197so6890814qke.1 for ; Sun, 19 Jun 2022 17:42:53 -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=diqEEblAkuwawlbX1s7fEiGLZApz+ZFOnP0b0ltgYqNgA052Gipbc9uKwJ40VMj81l rgJ0bCFMpK36FfJY/Vry6iChjR+sq39wweoXVBArRGara3DJiwwrlrtGBPXOLgnzNfyY hATDS8UvAcxRKRiy0g2P4FiPpEUrbRFEcMUpDV/QTyYS+JBNv7TcrReQ5uLa5iXjXXBE f5VYMTqD6rCe1J0MRW/6r9qUZVvCmlR+muDdG7tnuVCGkwX6lYI9mFadbeimctE7cXYT 2ruBsd3ArehoYMpmQWHb7lbhHFCMnnvyHaUBj7hHhHtY87ClUihTXVzCWcgFtn6+Mm2P p8Tw== X-Gm-Message-State: AJIora+yDD8Q1kxG15YSks3ZSJzeRsASjDxAFr+sCzA3idKYDBI/Ppqo T+YdiGe2cwxQk46uAEru9A== X-Google-Smtp-Source: AGRyM1tBmrxf1indi/uvJgmAPcm0cBP3mfXeFK9mluLQyQy1jfh9ZCHnRGgEBFB8u9JlPE8+z6ipmw== 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655685773; a=rsa-sha256; cv=none; b=8MiS5osxxkqTy5XaYVOOSIA5fg5G4oElTmpckIWWLu4fdFvxO3viDHe/3bphhT+i9I7w3S tDKHDLsHfD2YsclUA7NguNZbXm9wAUIeC40xyrl8p4uAUxKqmOUd5RFLwl0QcQvb98JHQk kh8WKd0hnbGyhBqG+LwQPzoGD95K4Mc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=FS+p1cBl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.222.178 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655685773; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fmnZsBS7ICfhtPbFVJE00Af049vTUvEMSfkA9G33Z+Y=; b=yXspjcDnUrWb8mwx+KUELAUu3QhcISrWcd+s1amizFjF5ZLVWPoXEQGiW364ohrKubbJgR o37GRcV5VyU184JsCBmwJVG63T/d0ozUzL6lmwtnl7KV6XQASygQOla3IzWKgHycXkIaKy 6A6i1qNLztweVFuQ8ne5MmVwhCLSJII= Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=FS+p1cBl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of kent.overstreet@gmail.com designates 209.85.222.178 as permitted sender) smtp.mailfrom=kent.overstreet@gmail.com X-Stat-Signature: 3g3o3kqjoq5a371ymdha8od8zffkcsh5 X-Rspamd-Queue-Id: 8A0D61C00A4 X-Rspam-User: X-Rspamd-Server: rspam03 X-HE-Tag: 1655685773-632944 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: 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);