From e9eb5ab11537ed9e2ca6d09fd2906b412c641a6f Mon Sep 17 00:00:00 2001 From: Sultanxda Date: Sat, 5 Nov 2016 00:53:22 -0700 Subject: [PATCH] printk: Add sleep time to timestamps 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 Signed-off-by: Nathan Chancellor Signed-off-by: Danny Lin Signed-off-by: NotZeetaa --- include/linux/timekeeping.h | 2 ++ kernel/printk/printk.c | 1 + kernel/time/timekeeping.c | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index 992fa3dc1cd5..dd2495ced04c 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -316,4 +316,6 @@ static inline struct timespec64 get_monotonic_coarse64(void) return ts; } +s64 get_total_sleep_time_nsec(void); + #endif diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index fafac7dd380d..0852fbccc6b6 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -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); } diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 6b305788d0d2..79a23c6806d2 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -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); +}