From patchwork Sat Feb 24 01:12:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 205744 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp932758dyb; Fri, 23 Feb 2024 17:12:53 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV5yL1pYgGfdPOA7QNd6GNfUJ/wNCbKVilx7jr1lcp3/lvMa2Pq8gaC2iiQSP6SMoZY0oKtFNj9fpT85BpLllcteonATg== X-Google-Smtp-Source: AGHT+IHUX1ADzqM7uyYlz1rdVTXdC/r5t/mAJFSRXM6KKvsaLxPWQFxo/cCHkCy26SDKJnLtnLVE X-Received: by 2002:a2e:9448:0:b0:2d2:3998:adaf with SMTP id o8-20020a2e9448000000b002d23998adafmr343878ljh.32.1708737173288; Fri, 23 Feb 2024 17:12:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708737173; cv=pass; d=google.com; s=arc-20160816; b=dHnl0TBF/2VU1c686BTH6paSWqY2e/dQ0438DjbjYmhmssUHevzZeUD/6YhH/iNUTj jIDyo+M6oF7L6yuJgh2bLy6xvsrWKWXjVCAkwWN3x/I2ZOWdKqsWpGqreKMeCx7wyVKa aOE21QmeWFeFv8D3RYrOTXqexpRy2IgHO2hIltwqWu8hXyUK7wN0eMzkOfDgpYWopL0M MC3i6uImjJ1RUXfyAPxDqRrWtG1KxCJSFymJxYUA3tr8dCOL3MsUEWPkSiv6DDUjxjZd 7B+IBBW8HozVYDHgOj8pvXqb920rddS0miBBIzPBeFefIOMQx8HsTcV6S8pr29Cv9mxb s6Mg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:cc:to:from:subject:date:dkim-signature; bh=myVRPfz8qIEGE+Q36H1icXenFwwxagnbyVbShJHEjBo=; fh=eGotrG2wUAPCtJbO4MhMKF1jcndrbQy7pSZLLDRnPjs=; b=msA6gky7TkiHI4P8IRbcDkWCXkQ5Gmoa0h01WjfmE9sNnkArkPv6Td7ge/2t201/Y5 emL8y2PF+2i7/N31bpbnhdfzA2wXYAVdQD0qAsIdAo/2nDulIWnKzyQHRJDaHGtMPv5L zGYgy01C78S/I2dudA5u7uzQ/J3G5m9gbYtlyNqX21R7HeLNuVxb0WzCI0KMkXG00nRP eKtgb8aL4xLFlcTJ+6p8oXH8TmGh1w2kp19BneIH17i2W1hZIxD+WQuha2GW0WujXAly 8D8px1sYxuIMqv/R0xPnddySABo7S21FGWzxG5hCasYXcnWz+D99A+cod8xlioQKPMPk rmOA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oMPxm1hy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79399-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79399-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id h7-20020a056402094700b00564f1005916si71194edz.545.2024.02.23.17.12.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 17:12:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79399-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oMPxm1hy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79399-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79399-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B47C71F24D98 for ; Sat, 24 Feb 2024 01:12:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 67D8A9473; Sat, 24 Feb 2024 01:12:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oMPxm1hy" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B5D56116; Sat, 24 Feb 2024 01:12:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708737147; cv=none; b=QFbbNtwLrLqiErX+NiD6OtwJWbUtwJvkAYbSgxEcEtPXN+32046VwLEt9ttDjuSzjTzR/q+HVGh3faIB5j8jMxT0wd0JDk3JsXAjogNv2F9MGQKhYrUzChZnSwTDlXL+w7ZzWx3PvO9XhKpiCEzaSMyA6PKEfTBj0yDbACkQmmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708737147; c=relaxed/simple; bh=stCMDrWvmAsbsnRhDg01PkUkNRNpdLqZpFOiKHjm3Do=; h=Date:Subject:From:To:Cc:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fsjUaXUVknWWrD+pkMlLVbmtEddBhIk9uzL/Qvv7pPuBiBdZrcZ0+pMQLZkrOIaAQFEsW3WICVJVHCLtm2NFQAegUmPSW+aV8N0d318/0wKNoV8VgkURRdUMcrUh0Q4cK50LlR7d2QPGT6lEwjrzVw+ED/OvUpaDukdNXF/wnXo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oMPxm1hy; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6F48C433C7; Sat, 24 Feb 2024 01:12:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708737146; bh=stCMDrWvmAsbsnRhDg01PkUkNRNpdLqZpFOiKHjm3Do=; h=Date:Subject:From:To:Cc:In-Reply-To:References:From; b=oMPxm1hyVNVOKZmazZDYU7hrTHAPU1VeOJgzDSHxKgfcl0U9Suj6X4/hpwtdqhJ+o AuMh/hJC2EmUhLQS+kyvjIPMvs2VwQKZoFE4lML/V/qDpyw9sBf762Yfpy2GImdCGL cuhR+IBIb8gZGb0AqSx+EcfOQlyUbI+YH0Mog/kQTp4STdIcbg8Oye5AoaSlKQe08m dvZ+N9NAX0t4N9PYfnezGNCylK9AKLOD9jBerabtH1YsBNNAavrCAhvaQH6EoHwLEc R4j6QKRtdFtTs4PuguZYGMlPCG5xElKqI8ulJo4CBZRvb0xGpOwGcCLM4ZUyMzY0sE kj2tQ62lJxQ5w== Date: Fri, 23 Feb 2024 17:12:26 -0800 Subject: [PATCH 09/10] time_stats: report information in json format From: "Darrick J. Wong" To: akpm@linux-foundation.org, daniel@gluo.nz, kent.overstreet@linux.dev, djwong@kernel.org Cc: linux-xfs@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <170873668085.1860949.11659237532415596101.stgit@frogsfrogsfrogs> In-Reply-To: <170873667916.1860949.11027844260383646446.stgit@frogsfrogsfrogs> References: <170873667916.1860949.11027844260383646446.stgit@frogsfrogsfrogs> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791740789983565862 X-GMAIL-MSGID: 1791740789983565862 From: Darrick J. Wong Export json versions of time statistics information. Given the tabular nature of the numbers exposed, this will make it a lot easier for higher (than C) level languages (e.g. python) to import information without needing to write yet another clumsy string parser. Signed-off-by: Darrick J. Wong Signed-off-by: Kent Overstreet Reviewed-by: Darrick J. Wong --- include/linux/time_stats.h | 2 + lib/time_stats.c | 87 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/include/linux/time_stats.h b/include/linux/time_stats.h index b3c810fff963a..4e1f5485ed039 100644 --- a/include/linux/time_stats.h +++ b/include/linux/time_stats.h @@ -156,6 +156,8 @@ static inline bool track_event_change(struct time_stats *stats, bool v) struct seq_buf; void time_stats_to_seq_buf(struct seq_buf *, struct time_stats *, const char *epoch_name, unsigned int flags); +void time_stats_to_json(struct seq_buf *, struct time_stats *, + const char *epoch_name, unsigned int flags); void time_stats_exit(struct time_stats *); void time_stats_init(struct time_stats *); diff --git a/lib/time_stats.c b/lib/time_stats.c index 0fb3d854e503b..c0f209dd9f6dd 100644 --- a/lib/time_stats.c +++ b/lib/time_stats.c @@ -266,6 +266,93 @@ void time_stats_to_seq_buf(struct seq_buf *out, struct time_stats *stats, } EXPORT_SYMBOL_GPL(time_stats_to_seq_buf); +void time_stats_to_json(struct seq_buf *out, struct time_stats *stats, + const char *epoch_name, unsigned int flags) +{ + struct quantiles *quantiles = time_stats_to_quantiles(stats); + s64 f_mean = 0, d_mean = 0; + u64 f_stddev = 0, d_stddev = 0; + + if (stats->buffer) { + int cpu; + + spin_lock_irq(&stats->lock); + for_each_possible_cpu(cpu) + __time_stats_clear_buffer(stats, per_cpu_ptr(stats->buffer, cpu)); + spin_unlock_irq(&stats->lock); + } + + if (stats->freq_stats.n) { + /* avoid divide by zero */ + f_mean = mean_and_variance_get_mean(stats->freq_stats); + f_stddev = mean_and_variance_get_stddev(stats->freq_stats); + d_mean = mean_and_variance_get_mean(stats->duration_stats); + d_stddev = mean_and_variance_get_stddev(stats->duration_stats); + } else if (flags & TIME_STATS_PRINT_NO_ZEROES) { + /* unless we didn't want zeroes anyway */ + return; + } + + seq_buf_printf(out, "{\n"); + seq_buf_printf(out, " \"epoch\": \"%s\",\n", epoch_name); + seq_buf_printf(out, " \"count\": %llu,\n", stats->duration_stats.n); + + seq_buf_printf(out, " \"duration_ns\": {\n"); + seq_buf_printf(out, " \"min\": %llu,\n", stats->min_duration); + seq_buf_printf(out, " \"max\": %llu,\n", stats->max_duration); + seq_buf_printf(out, " \"total\": %llu,\n", stats->total_duration); + seq_buf_printf(out, " \"mean\": %llu,\n", d_mean); + seq_buf_printf(out, " \"stddev\": %llu\n", d_stddev); + seq_buf_printf(out, " },\n"); + + d_mean = mean_and_variance_weighted_get_mean(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT); + d_stddev = mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT); + + seq_buf_printf(out, " \"duration_ewma_ns\": {\n"); + seq_buf_printf(out, " \"mean\": %llu,\n", d_mean); + seq_buf_printf(out, " \"stddev\": %llu\n", d_stddev); + seq_buf_printf(out, " },\n"); + + seq_buf_printf(out, " \"frequency_ns\": {\n"); + seq_buf_printf(out, " \"min\": %llu,\n", stats->min_freq); + seq_buf_printf(out, " \"max\": %llu,\n", stats->max_freq); + seq_buf_printf(out, " \"mean\": %llu,\n", f_mean); + seq_buf_printf(out, " \"stddev\": %llu\n", f_stddev); + seq_buf_printf(out, " },\n"); + + f_mean = mean_and_variance_weighted_get_mean(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT); + f_stddev = mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT); + + seq_buf_printf(out, " \"frequency_ewma_ns\": {\n"); + seq_buf_printf(out, " \"mean\": %llu,\n", f_mean); + seq_buf_printf(out, " \"stddev\": %llu\n", f_stddev); + + if (quantiles) { + u64 last_q = 0; + + /* close frequency_ewma_ns but signal more items */ + seq_buf_printf(out, " },\n"); + + seq_buf_printf(out, " \"quantiles_ns\": [\n"); + eytzinger0_for_each(i, NR_QUANTILES) { + bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1; + + u64 q = max(quantiles->entries[i].m, last_q); + seq_buf_printf(out, " %llu", q); + if (!is_last) + seq_buf_printf(out, ", "); + last_q = q; + } + seq_buf_printf(out, " ]\n"); + } else { + /* close frequency_ewma_ns without dumping further */ + seq_buf_printf(out, " }\n"); + } + + seq_buf_printf(out, "}\n"); +} +EXPORT_SYMBOL_GPL(time_stats_to_json); + void time_stats_exit(struct time_stats *stats) { free_percpu(stats->buffer);