Skip to content

Commit

Permalink
printk: Add sleep time to timestamps
Browse files Browse the repository at this point in the history
cpu_clock() uses monotonic time, which skews when the system suspends,
making it difficult to interpret kmsg timestamps.

Add the sleep time offset to cpu_clock() in order to make kmsg timestamps
reflect the actual boot time.

Change-Id: I642cc458b4aba8c78e64dbca8febdf8190a328bc
Signed-off-by: Sultanxda <[email protected]>
Signed-off-by: Nathan Chancellor <[email protected]>
Signed-off-by: Danny Lin <[email protected]>
Signed-off-by: NotZeetaa <[email protected]>
  • Loading branch information
kerneltoast authored and TIMISONG-dev committed Dec 13, 2023
1 parent 35f0629 commit e9eb5ab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/linux/timekeeping.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,6 @@ static inline struct timespec64 get_monotonic_coarse64(void)
return ts;
}

s64 get_total_sleep_time_nsec(void);

#endif
1 change: 1 addition & 0 deletions kernel/printk/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,7 @@ static size_t print_time(u64 ts, char *buf)
if (!buf)
return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);

ts += get_total_sleep_time_nsec();
return sprintf(buf, "[%5lu.%06lu] ",
(unsigned long)ts, rem_nsec / 1000);
}
Expand Down
10 changes: 10 additions & 0 deletions kernel/time/timekeeping.c
Original file line number Diff line number Diff line change
Expand Up @@ -2417,3 +2417,13 @@ void xtime_update(unsigned long ticks)
write_sequnlock(&jiffies_lock);
update_wall_time();
}

/**
* get_total_sleep_time_nsec() - returns total sleep time in nanoseconds
*/
s64 get_total_sleep_time_nsec(void)
{
struct timekeeper *tk = &tk_core.timekeeper;

return ktime_to_ns(tk->offs_boot);
}

0 comments on commit e9eb5ab

Please sign in to comment.