include/linux/trace_clock.h | 2 ++ kernel/trace/trace.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+)
From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Record trace_clock information in the trace_scratch area and recover
the trace_clock when boot, so that reader can docode the timestamp
correctly.
Note that since most trace_clocks records the timestamp in nano-
seconds, this is not a bug. But some trace_clock, like counter and
tsc will record the counter value. Only for those trace_clock user
needs this information.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
Changes in v2:
- instead of exposing it via last_boot_info, set the current
trace_clock as the same clock we used in the last boot.
---
include/linux/trace_clock.h | 2 ++
kernel/trace/trace.c | 17 +++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/include/linux/trace_clock.h b/include/linux/trace_clock.h
index 00e8f98c940f..2245848270d2 100644
--- a/include/linux/trace_clock.h
+++ b/include/linux/trace_clock.h
@@ -15,6 +15,8 @@
#include <asm/trace_clock.h>
+#define TRACE_CLOCK_NAME_MAX 16
+
extern u64 notrace trace_clock_local(void);
extern u64 notrace trace_clock(void);
extern u64 notrace trace_clock_jiffies(void);
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 8ddf6b17215c..6ce5d05ec64d 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -6004,6 +6004,7 @@ struct trace_mod_entry {
};
struct trace_scratch {
+ char clock[TRACE_CLOCK_NAME_MAX];
unsigned long text_addr;
unsigned long nr_entries;
struct trace_mod_entry entries[];
@@ -6114,6 +6115,8 @@ static void update_last_data(struct trace_array *tr)
if (tr->scratch) {
struct trace_scratch *tscratch = tr->scratch;
+ strscpy(tscratch->clock, trace_clocks[tr->clock_id].name,
+ TRACE_CLOCK_NAME_MAX);
memset(tscratch->entries, 0,
flex_array_size(tscratch, entries, tscratch->nr_entries));
tscratch->nr_entries = 0;
@@ -7289,6 +7292,12 @@ int tracing_set_clock(struct trace_array *tr, const char *clockstr)
tracing_reset_online_cpus(&tr->max_buffer);
#endif
+ if (tr->scratch && !(tr->flags & TRACE_ARRAY_FL_LAST_BOOT)) {
+ struct trace_scratch *tscratch = tr->scratch;
+
+ strscpy(tscratch->clock, trace_clocks[i].name, TRACE_CLOCK_NAME_MAX);
+ }
+
mutex_unlock(&trace_types_lock);
return 0;
@@ -9514,6 +9523,14 @@ static void setup_trace_scratch(struct trace_array *tr,
/* Scan modules to make text delta for modules. */
module_for_each_mod(make_mod_delta, tr);
+
+ /* Set trace_clock as the same of the previous boot. */
+ if (tscratch->clock[0]) {
+ if (tracing_set_clock(tr, tscratch->clock) < 0) {
+ pr_info("the previous trace_clock info is not valid.");
+ goto reset;
+ }
+ }
return;
reset:
/* Invalid trace modules */
On Thu, 17 Apr 2025 14:17:29 +0900
"Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -6004,6 +6004,7 @@ struct trace_mod_entry {
> };
>
> struct trace_scratch {
> + char clock[TRACE_CLOCK_NAME_MAX];
> unsigned long text_addr;
> unsigned long nr_entries;
> struct trace_mod_entry entries[];
> @@ -6114,6 +6115,8 @@ static void update_last_data(struct trace_array *tr)
> if (tr->scratch) {
> struct trace_scratch *tscratch = tr->scratch;
>
> + strscpy(tscratch->clock, trace_clocks[tr->clock_id].name,
Why copy the name and not the clock_id?
The clock ids should not change between kernels.
-- Steve
> + TRACE_CLOCK_NAME_MAX);
> memset(tscratch->entries, 0,
> flex_array_size(tscratch, entries, tscratch->nr_entries));
> tscratch->nr_entries = 0;
On Thu, 17 Apr 2025 10:18:04 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:
> On Thu, 17 Apr 2025 14:17:29 +0900
> "Masami Hiramatsu (Google)" <mhiramat@kernel.org> wrote:
>
> > --- a/kernel/trace/trace.c
> > +++ b/kernel/trace/trace.c
> > @@ -6004,6 +6004,7 @@ struct trace_mod_entry {
> > };
> >
> > struct trace_scratch {
> > + char clock[TRACE_CLOCK_NAME_MAX];
> > unsigned long text_addr;
> > unsigned long nr_entries;
> > struct trace_mod_entry entries[];
> > @@ -6114,6 +6115,8 @@ static void update_last_data(struct trace_array *tr)
> > if (tr->scratch) {
> > struct trace_scratch *tscratch = tr->scratch;
> >
> > + strscpy(tscratch->clock, trace_clocks[tr->clock_id].name,
>
> Why copy the name and not the clock_id?
The name is more robust if we rebooted in the different kernel.
>
> The clock ids should not change between kernels.
Hmm, I thought it could be changed because we didn't define it.
----
static struct {
u64 (*func)(void);
const char *name;
int in_ns; /* is this clock in nanoseconds? */
} trace_clocks[] = {
{ trace_clock_local, "local", 1 },
{ trace_clock_global, "global", 1 },
{ trace_clock_counter, "counter", 0 },
{ trace_clock_jiffies, "uptime", 0 },
{ trace_clock, "perf", 1 },
{ ktime_get_mono_fast_ns, "mono", 1 },
{ ktime_get_raw_fast_ns, "mono_raw", 1 },
{ ktime_get_boot_fast_ns, "boot", 1 },
{ ktime_get_tai_fast_ns, "tai", 1 },
ARCH_TRACE_CLOCKS
};
----
So the clock id is just an index of this array. That can be changed
easily between the kernel version. If we discard the previous boot
data in that case, I agree with using clock ids.
(Another reason for the implementation is that tracing_set_clock()
only accepts a name, but this is just due to laziness on my part. :-( )
Thank you,
>
> -- Steve
>
> > + TRACE_CLOCK_NAME_MAX);
> > memset(tscratch->entries, 0,
> > flex_array_size(tscratch, entries, tscratch->nr_entries));
> > tscratch->nr_entries = 0;
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
© 2016 - 2025 Red Hat, Inc.