From patchwork Thu May 28 23:52:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 11577273 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 592311391 for ; Thu, 28 May 2020 23:52:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 17E0E2078C for ; Thu, 28 May 2020 23:52:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IRNgeI7k" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 17E0E2078C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 23363800B7; Thu, 28 May 2020 19:52:58 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 20B6080010; Thu, 28 May 2020 19:52:58 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12087800B7; Thu, 28 May 2020 19:52:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0138.hostedemail.com [216.40.44.138]) by kanga.kvack.org (Postfix) with ESMTP id EDC1380010 for ; Thu, 28 May 2020 19:52:57 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id B0AF92C84 for ; Thu, 28 May 2020 23:52:57 +0000 (UTC) X-FDA: 76867780794.20.copy78_8e350d6e68520 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 96055180C07AB for ; Thu, 28 May 2020 23:52:57 +0000 (UTC) X-Spam-Summary: 2,0,0,301d4a5cc5795433,d41d8cd98f00b204,32e7qxg0kclmtqxektlfnllxgzhhzex.vhfebgnq-ffdotvd.hkz@flex--axelrasmussen.bounces.google.com,,RULES_HIT:41:152:355:379:541:800:960:966:973:988:989:1260:1277:1313:1314:1345:1431:1437:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:2899:3138:3139:3140:3141:3142:3152:3165:3355:3608:3865:3866:3867:3868:3870:3871:4119:4250:4321:4385:4605:5007:6261:6653:6737:6742:6743:7875:7903:9969:10004:10400:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12555:12683:12895:12986:14181:14394:14659:14721:14877:21080:21433:21444:21451:21627:21990:30012:30034:30054:30070,0,RBL:209.85.219.201:@flex--axelrasmussen.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: copy78_8e350d6e68520 X-Filterd-Recvd-Size: 8875 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf12.hostedemail.com (Postfix) with ESMTP for ; Thu, 28 May 2020 23:52:57 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id f130so927140yba.9 for ; Thu, 28 May 2020 16:52:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=uXM6Fov/4A7+5lOy4XDu2Yode4WUxdldKubf/JJk+Ys=; b=IRNgeI7k5mpd9pI0t6QJQPWME/3vAvlzp62U1uKbtD54d8xsgeYrdDPC5Ao4i/FZ1b ItsxLbIf0twoil5ukifuA0+3Wj9dVqePe3ZJ6fN0HMhlIIgnvhXRU4ditFe1xW22kExt ud6Dka7HnVgfy/jaXPCtVjQAK+QyUJ3yAGJmaKkJm2W5UiDptVkATKVJoSF/PJV+dNHs B2nffPSoYsra5frvS6ZZBHWgQTAtNuyzg4sYYGRT67FHeOyaLt1JKXaCdEaUmEWafhsI 47uFQn5Rx3xApoG0HR3XrZ7z/pDViWWwYa3Y3DlMlI5vvOssqt2ZljAByC8H5XKNyOcV SynA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=uXM6Fov/4A7+5lOy4XDu2Yode4WUxdldKubf/JJk+Ys=; b=D078f52tT8vomiijcTmmDvbbx7p1MiDrTG85JJUDLGKoDyTIWDkbsD9gIy1hGTRVic EGAK12lCfH9PbbJfKWcycnSfN9YU0rNI6pSpZSk2wP2npwIYFpe/MyjZwgKY8ZyNMVVe 7c51SAXmP6WT1mh2M3RPI9B6/jxUiUyiP5pgD9UcvqvEe1tXX/yna1+w5JcOPuY2Grxc wIL6zamsrs53f3ybiXVqFbMw4aoDQsifbHrrfFWkVseoy5m+h89J8Xy+OvGVZ4nhQJuM 5hxtEbV+/toNl6IiZ3GrmihBQ+aQ1LuyRdp6YnjeOKPryGwBkZqe05aUsjt0IgQEwYP7 SXPQ== X-Gm-Message-State: AOAM532NZwThWv26fFeaLWQr0VlKepavkyNTO048lW0xoAObgwV1H7v0 0QIXgBozcM5q1fiwBM6lKOKV12Fv7ctvUqbbszW6 X-Google-Smtp-Source: ABdhPJy/rfhM6KHn/G1HvFjIKBvVeSeuXQmfxsW2OduPIrlSFrsDtZtuxGzg0YGt33xYLF0SlmHehM1GOL3M3O2rtawO X-Received: by 2002:a25:8b02:: with SMTP id i2mr9363896ybl.283.1590709976338; Thu, 28 May 2020 16:52:56 -0700 (PDT) Date: Thu, 28 May 2020 16:52:53 -0700 Message-Id: <20200528235253.75570-1-axelrasmussen@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.27.0.rc0.183.gde8f92d652-goog Subject: [PATCH v2 2/7] histogram: add helper function to expose histograms to userspace From: Axel Rasmussen To: Andrew Morton , David Rientjes , Davidlohr Bueso , Ingo Molnar , Ingo Molnar , Jerome Glisse , Laurent Dufour , "Liam R . Howlett" , Matthew Wilcox , Michel Lespinasse , Peter Zijlstra , Vlastimil Babka , Will Deacon Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, AKASHI Takahiro , Aleksa Sarai , Alexander Potapenko , Alexey Dobriyan , Al Viro , Andrei Vagin , Ard Biesheuvel , Brendan Higgins , chenqiwu , Christian Brauner , Christian Kellner , Corentin Labbe , Daniel Jordan , Dan Williams , David Gow , "David S. Miller" , "Dmitry V. Levin" , "Eric W. Biederman" , Eugene Syromiatnikov , Jamie Liu , Jason Gunthorpe , John Garry , John Hubbard , Jonathan Adams , Junaid Shahid , Kees Cook , "Kirill A. Shutemov" , Konstantin Khlebnikov , Krzysztof Kozlowski , Mark Rutland , Masahiro Yamada , Masami Hiramatsu , Mathieu Desnoyers , Michal Hocko , Mikhail Zaslonko , Petr Mladek , Ralph Campbell , Randy Dunlap , Roman Gushchin , Shakeel Butt , Steven Rostedt , Tal Gilboa , Thomas Gleixner , " =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= " , Vincenzo Frascino , Yang Shi , Yu Zhao , Axel Rasmussen X-Rspamd-Queue-Id: 96055180C07AB X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 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 change introduces the histogram_print_buckets_rcu(). This API is used for implementing e.g. procfs or sysfs files exposing histograms. print_buckets can be combined with e.g. seq_file to provide a read interface for such a file. This is a squashed, refactored, and modified version of a previously-internal implementation. Thanks to the following individuals for portions of the implementation: Junaid Shahid - Original implementation Yu Zhao - Simplification Signed-off-by: Axel Rasmussen --- include/linux/histogram.h | 23 ++++++++++++++++ lib/histogram.c | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/include/linux/histogram.h b/include/linux/histogram.h index 137930ca933f..7a1dc33fff5e 100644 --- a/include/linux/histogram.h +++ b/include/linux/histogram.h @@ -65,6 +65,12 @@ * histogram_destroy_rcu(&hrcu); */ +/* + * Max number of digits in decimal needed to represent U64_MAX threshold, + * plus one space. + */ +#define HISTO_MAX_CHARS_PER_THRESHOLD (20 + 1) + struct histogram { struct rcu_head rcu; u64 __percpu *buckets; @@ -189,6 +195,23 @@ static inline void histogram_record_rcu(struct histogram_rcu *hrcu, u64 val, ssize_t histogram_read_rcu(const struct histogram_rcu *hrcu, u64 *buckets, u64 *thresholds, size_t nr_thresholds); +/** + * histogram_print_buckets_rcu() - helper function to print histogram buckets + * @hrcu: histogram + * @buffer: output buffer to fill + * @len: length of the output buffer + * + * Reads buckets by calling histogram_read_rcu(), then fills the output buffer. + * + * Context: Performs allocation with GFP_ATOMIC. + * + * Returns: The number of characters written to @buffer, or a negative error + * code on failure. If the buffer isn't large enough to contain the output, + * -EINVAL is returned. + */ +int histogram_print_buckets_rcu(struct histogram_rcu *hrcu, char *buffer, + int len); + /** * histogram_set_thresholds_rcu() - set RCU-protected histogram thresholds * @hrcu: histogram diff --git a/lib/histogram.c b/lib/histogram.c index b68334275a46..1a23590fc6e9 100644 --- a/lib/histogram.c +++ b/lib/histogram.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include #include @@ -133,6 +134,60 @@ ssize_t histogram_read_rcu(const struct histogram_rcu *hrcu, u64 *buckets, } EXPORT_SYMBOL_GPL(histogram_read_rcu); +int histogram_print_buckets_rcu(struct histogram_rcu *hrcu, char *buffer, + int len) +{ + const struct histogram *hist; + u64 *buckets = NULL; + u64 lower = 0; + size_t nr_buckets; + size_t i; + int ret; + int remaining = len; + + rcu_read_lock_sched(); + hist = rcu_dereference_sched(hrcu->hist); + + nr_buckets = hist->nr_thresholds; + if (!nr_buckets) { + ret = -EINVAL; + goto out; + } + + buckets = kmalloc_array(nr_buckets, sizeof(*hist->buckets), GFP_ATOMIC); + if (!buckets) { + ret = -ENOMEM; + goto out; + } + + histogram_read_buckets(hist, buckets); + + for (i = 0; i < nr_buckets; i++) { + if (i == nr_buckets - 1) + ret = snprintf(buffer, remaining, "%llu-inf %llu\n", + lower, buckets[i]); + else + ret = snprintf(buffer, remaining, "%llu-%llu %llu\n", + lower, hist->thresholds[i], buckets[i]); + if (ret >= remaining) { + ret = -EINVAL; + goto out; + } + + buffer += ret; + remaining -= ret; + + lower = hist->thresholds[i] + 1; + } + + ret = len - remaining; +out: + rcu_read_unlock_sched(); + kfree(buckets); + return ret; +} +EXPORT_SYMBOL_GPL(histogram_print_buckets_rcu); + int histogram_set_thresholds_rcu(struct histogram_rcu *hrcu, const u64 *thresholds, size_t nr_thresholds) {