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

From: Kent Overstreet
Date: Sat Feb 24 2024 - 00:10:55 EST


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@xxxxxxxxxx>
> >
> > 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@xxxxxxxxxx>
> > Signed-off-by: Kent Overstreet <kent.overstreet@xxxxxxxxx>
> > ---
> > 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);
> >
> >