[PATCH RFC 3/9] timekeeping: add new debugfs file to count multigrain timestamps

From: Jeff Layton
Date: Wed Oct 18 2023 - 13:41:53 EST


Add two percpu counters for tracking multigrain timestamps -- one for
coarse-grained timestamps and one for fine-grained ones. Add a new
debugfs file for summing them and outputting the result.

Signed-off-by: Jeff Layton <jlayton@xxxxxxxxxx>
---
kernel/time/timekeeping.c | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 7c20c98b1ea8..c843838cb643 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -24,6 +24,8 @@
#include <linux/compiler.h>
#include <linux/audit.h>
#include <linux/random.h>
+#include <linux/seq_file.h>
+#include <linux/debugfs.h>

#include "tick-internal.h"
#include "ntp_internal.h"
@@ -59,6 +61,9 @@ static struct {

static struct timekeeper shadow_timekeeper;

+struct percpu_counter mg_fine_ts;
+struct percpu_counter mg_coarse_ts;
+
/* flag for if timekeeping is suspended */
int __read_mostly timekeeping_suspended;

@@ -2326,6 +2331,7 @@ void ktime_get_mg_fine_ts64(struct timespec64 *ts)

ts->tv_nsec = 0;
timespec64_add_ns(ts, nsecs);
+ percpu_counter_inc(&mg_fine_ts);
}

/**
@@ -2361,6 +2367,7 @@ void ktime_get_mg_coarse_ts64(struct timespec64 *ts)
ts->tv_nsec = 0;
timespec64_add_ns(ts, nsec);
}
+ percpu_counter_inc(&mg_coarse_ts);
}

/*
@@ -2581,3 +2588,33 @@ void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts)
}
EXPORT_SYMBOL(hardpps);
#endif /* CONFIG_NTP_PPS */
+
+static int fgts_show(struct seq_file *s, void *p)
+{
+ u64 fine = percpu_counter_sum(&mg_fine_ts);
+ u64 coarse = percpu_counter_sum(&mg_coarse_ts);
+
+ seq_printf(s, "%llu %llu\n", fine, coarse);
+ return 0;
+}
+
+DEFINE_SHOW_ATTRIBUTE(fgts);
+
+static int __init tk_debugfs_init(void)
+{
+ int ret = percpu_counter_init(&mg_fine_ts, 0, GFP_KERNEL);
+
+ if (ret)
+ return ret;
+
+ ret = percpu_counter_init(&mg_coarse_ts, 0, GFP_KERNEL);
+ if (ret) {
+ percpu_counter_destroy(&mg_fine_ts);
+ return ret;
+ }
+
+ debugfs_create_file("multigrain_timestamps", S_IFREG | S_IRUGO,
+ NULL, NULL, &fgts_fops);
+ return 0;
+}
+late_initcall(tk_debugfs_init);

--
2.41.0