diff mbox series

[09/10] time_stats: report information in json format

Message ID 170873668085.1860949.11659237532415596101.stgit@frogsfrogsfrogs (mailing list archive)
State Deferred, archived
Headers show
Series [01/10] time_stats: report lifetime of the stats object | expand

Commit Message

Darrick J. Wong Feb. 24, 2024, 1:12 a.m. UTC
From: Darrick J. Wong <djwong@kernel.org>

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 <djwong@kernel.org>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 include/linux/time_stats.h |    2 +
 lib/time_stats.c           |   87 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)

Comments

Darrick J. Wong Feb. 24, 2024, 4:15 a.m. UTC | #1
On Fri, Feb 23, 2024 at 05:12:26PM -0800, Darrick J. Wong wrote:
> From: Darrick J. Wong <djwong@kernel.org>
> 
> 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 <djwong@kernel.org>
> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
> ---
>  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");

I took the variable names too literally here; these labels really ought
to be "between_ns" and "between_ewma_ns" to maintain consistency with
the labels in the table format.

> +	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 */

(also this comment)

> +		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 */

(this one too)

Kent, would you mind making that edit the next time you reflow your
branch?

--D

> +		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);
> 
>
Kent Overstreet Feb. 24, 2024, 5:10 a.m. UTC | #2
On Fri, Feb 23, 2024 at 08:15:45PM -0800, Darrick J. Wong wrote:
> On Fri, Feb 23, 2024 at 05:12:26PM -0800, Darrick J. Wong wrote:
> > From: Darrick J. Wong <djwong@kernel.org>
> > 
> > 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 <djwong@kernel.org>
> > Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
> > ---
> >  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");
> 
> I took the variable names too literally here; these labels really ought
> to be "between_ns" and "between_ewma_ns" to maintain consistency with
> the labels in the table format.
> 
> > +	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 */
> 
> (also this comment)
> 
> > +		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 */
> 
> (this one too)
> 
> Kent, would you mind making that edit the next time you reflow your
> branch?
> 
> --D
> 
> > +		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);
> > 
> > 


From 5885a65fa5a0aace7bdf1a8fa58ac2bca3b15900 Mon Sep 17 00:00:00 2001
From: Kent Overstreet <kent.overstreet@linux.dev>
Date: Sat, 24 Feb 2024 00:10:06 -0500
Subject: [PATCH] fixup! time_stats: report information in json format


diff --git a/lib/time_stats.c b/lib/time_stats.c
index 0b90c80cba9f..d7dd64baebb8 100644
--- a/lib/time_stats.c
+++ b/lib/time_stats.c
@@ -313,7 +313,7 @@ void time_stats_to_json(struct seq_buf *out, struct time_stats *stats,
 	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, "  \"between_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);
@@ -323,14 +323,14 @@ void time_stats_to_json(struct seq_buf *out, struct time_stats *stats,
 	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, "  \"between_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 */
+		/* close between_ewma_ns but signal more items */
 		seq_buf_printf(out, "  },\n");
 
 		seq_buf_printf(out, "  \"quantiles_ns\": [\n");
@@ -345,7 +345,7 @@ void time_stats_to_json(struct seq_buf *out, struct time_stats *stats,
 		}
 		seq_buf_printf(out, "  ]\n");
 	} else {
-		/* close frequency_ewma_ns without dumping further */
+		/* close between_ewma_ns without dumping further */
 		seq_buf_printf(out, "  }\n");
 	}
Darrick J. Wong Feb. 24, 2024, 6:02 a.m. UTC | #3
On Sat, Feb 24, 2024 at 12:10:33AM -0500, Kent Overstreet wrote:
> On Fri, Feb 23, 2024 at 08:15:45PM -0800, Darrick J. Wong wrote:
> > On Fri, Feb 23, 2024 at 05:12:26PM -0800, Darrick J. Wong wrote:
> > > From: Darrick J. Wong <djwong@kernel.org>
> > > 
> > > 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 <djwong@kernel.org>
> > > Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
> > > ---
> > >  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");
> > 
> > I took the variable names too literally here; these labels really ought
> > to be "between_ns" and "between_ewma_ns" to maintain consistency with
> > the labels in the table format.
> > 
> > > +	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 */
> > 
> > (also this comment)
> > 
> > > +		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 */
> > 
> > (this one too)
> > 
> > Kent, would you mind making that edit the next time you reflow your
> > branch?
> > 
> > --D
> > 
> > > +		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);
> > > 
> > > 
> 
> 
> From 5885a65fa5a0aace7bdf1a8fa58ac2bca3b15900 Mon Sep 17 00:00:00 2001
> From: Kent Overstreet <kent.overstreet@linux.dev>
> Date: Sat, 24 Feb 2024 00:10:06 -0500
> Subject: [PATCH] fixup! time_stats: report information in json format
> 
> 
> diff --git a/lib/time_stats.c b/lib/time_stats.c
> index 0b90c80cba9f..d7dd64baebb8 100644
> --- a/lib/time_stats.c
> +++ b/lib/time_stats.c
> @@ -313,7 +313,7 @@ void time_stats_to_json(struct seq_buf *out, struct time_stats *stats,
>  	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, "  \"between_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);
> @@ -323,14 +323,14 @@ void time_stats_to_json(struct seq_buf *out, struct time_stats *stats,
>  	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, "  \"between_ewma_ns\": {\n");

Looks good to me,
Reviewed-by: Darrick J. Wong <djwong@kernel.org>

--D

>  	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 */
> +		/* close between_ewma_ns but signal more items */
>  		seq_buf_printf(out, "  },\n");
>  
>  		seq_buf_printf(out, "  \"quantiles_ns\": [\n");
> @@ -345,7 +345,7 @@ void time_stats_to_json(struct seq_buf *out, struct time_stats *stats,
>  		}
>  		seq_buf_printf(out, "  ]\n");
>  	} else {
> -		/* close frequency_ewma_ns without dumping further */
> +		/* close between_ewma_ns without dumping further */
>  		seq_buf_printf(out, "  }\n");
>  	}
>
diff mbox series

Patch

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);