Skip to contentSkip to conversation

printk: Add monotonic, boottime, and realtime timestamps

PATCH In review Chunyan Zhang 3 participants 4 messages Jul 29, 2020 – Aug 5, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v13 1/1 printk: Add monotonic, boottime, and realtime timestamps

From: Prarit Bhargava <[email protected]>

printk.time=1/CONFIG_PRINTK_TIME=1 adds a unmodified local hardware clock timestamp to printk messages. The local hardware clock loses time each day making it difficult to determine exactly when an issue has occurred in the kernel log, and making it difficult to determine how kernel and hardware issues relate to each other in real time.

Make printk output different timestamps by adding options for no timestamp, the local hardware clock, the monotonic clock, the boottime clock, and the real clock. Allow a user to pick one of the clocks by using the printk.time kernel parameter. Output the type of clock in /sys/module/printk/parameters/time so userspace programs can interpret the timestamp.

v2: Use peterz's suggested Kconfig options. Merge patchset together. Fix i386 !CONFIG_PRINTK builds.

v3: Fixed x86_64_defconfig. Added printk_time_type enum and printk_time_str for better output. Added BOOTTIME clock functionality.

v4: Fix messages, add additional printk.time options, and fix configs.

v5: Renaming of structures, and allow printk_time_set() to evaluate substrings of entries (eg: allow 'r', 'real', 'realtime'). From peterz, make fast functions return 0 until timekeeping is initialized (removes timekeeping_active & ktime_get_boot|real_log_ts() suggested by
tglx and adds ktime_get_real_offset()). Switch to a function pointer
for printk_get_ts() and reference fast functions. Make timestamp_sources enum match choice options for CONFIG_PRINTK_TIME (adds PRINTK_TIME_UNDEFINED).

v6: Define PRINTK_TIME_UNDEFINED for !CONFIG_PRINTK builds. Separate timekeeping changes into separate patch. Minor include file cleanup.

v7: Add default case to printk_set_timestamp() and add PRINTK_TIME_DEBUG for users that want to set timestamp to different values during runtime. Add jstultz' Kconfig to avoid defconfig churn.

v8: Add CONFIG_PRINTK_TIME_DEBUG to allow timestamp runtime switching. Rename PRINTK_TIME_DISABLE to PRINTK_TIME_DISABLED. Rename printk_set_timestamp() to printk_set_ts_func(). Separate printk_set_ts_func() and printk_get_first_ts() portions. Rename param functions. Adjust configs, enum, and timestamp_sources_str to be 0-4. Add mention realtime clock is UTC in Documentation.

v9: Fix typo. Add __ktime_get_real_fast_ns_unsafe().

v10: Remove time parameter restrictions.

v11: Rework using tglx's real time patch

v12: Reword Kconfig names. Simplify timestamp logic and remove recursive code.

v13: This patch seems have being forgotten for 3 years. Rebase it on the latest kernel v5.8, reolve conflicts and fix compiling errors. Change code to adapt new printk_time usage.
Petr's concern on printk_time is addressed by current version of kernel, too.

Cc Mark Salyzyn <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc John Stultz <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Stephen Boyd <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Deepa Dinamani <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Kees Cook <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc "Luis R. Rodriguez" <[email protected]>
Cc Nicholas Piggin <[email protected]>
Cc "Jason A. Donenfeld" <[email protected]>
Cc Olof Johansson <[email protected]>
Cc Josh Poimboeuf <[email protected]>
[jstultz: reworked Kconfig settings to avoid defconfig noise]
[orson: rebase on v5.8 and make necessary changes]
---
 Documentation/admin-guide/kernel-parameters.txt |   6 +-
 kernel/printk/printk.c                          | 127 +++++++++++++++++++++++-
 lib/Kconfig.debug                               |  48 ++++++++-
 3 files changed, 174 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +4 -2
index fb95fad..0bf5f69 100644
@@ -3874,8 +3874,10 @@
 			ratelimit - ratelimit the logging
 			Default: ratelimit
 
-	printk.time=	Show timing data prefixed to each printk message line
-			Format: <bool>  (1/Y/y=enable, 0/N/n=disable)
+	printk.time=	Show timestamp prefixed to each printk message line
+			Format: <string>
+				(0/N/n/disable, 1/Y/y/local,
+				 b/boot, m/monotonic, r/realtime (in UTC))
 
 	processor.max_cstate=	[HW,ACPI]
 			Limit processor to maximum C-state
kernel/printk/printk.c +123 -4

151 lines not on this page · raw message

lib/Kconfig.debug +47 -1

59 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw
Quoted 15 lines · Chunyan Zhang

From: Prarit Bhargava <[email protected]>

printk.time=1/CONFIG_PRINTK_TIME=1 adds a unmodified local hardware clock timestamp to printk messages. The local hardware clock loses time each day making it difficult to determine exactly when an issue has occurred in the kernel log, and making it difficult to determine how kernel and hardware issues relate to each other in real time.

Make printk output different timestamps by adding options for no timestamp, the local hardware clock, the monotonic clock, the boottime clock, and the real clock. Allow a user to pick one of the clocks by using the printk.time kernel parameter. Output the type of clock in /sys/module/printk/parameters/time so userspace programs can interpret the timestamp.

Quoted 4 lines

v13: This patch seems have being forgotten for 3 years. Rebase it on the latest kernel v5.8, reolve conflicts and fix compiling errors. Change code to adapt new printk_time usage.
Petr's concern on printk_time is addressed by current version of kernel, too.

Lyra,

Copying a reply I sent to Orson who sent me this patch privately this morning with some additional information.

ISTR the reason that this was dropped was because of the a problem with the way systemd read the kernel's timestamps. It got the attention of Linus, and it was then pulled from the tree.

I need to go back and review the entire thread as it's been several years since we had the discussion although ISTR someone mentioning that doing two timestamps would not be a problem for systemd.

For example,

[48551.015086]

would be

[48551.015086] m[xxxx.xxxx]

for the monotonic clock timestamp, and

[48551.015086] b[xxxx.xxxx]

for the boottime clock, etc.

P.

maintainer
#2 lore raw
Quoted 16 lines · Prarit Bhargava
Quoted 8 lines · Chunyan Zhang

From: Prarit Bhargava <[email protected]>

printk.time=1/CONFIG_PRINTK_TIME=1 adds a unmodified local hardware clock timestamp to printk messages. The local hardware clock loses time each day making it difficult to determine exactly when an issue has occurred in the kernel log, and making it difficult to determine how kernel and hardware issues relate to each other in real time.

Quoted 7 lines

Make printk output different timestamps by adding options for no timestamp, the local hardware clock, the monotonic clock, the boottime clock, and the real clock. Allow a user to pick one of the clocks by using the printk.time kernel parameter. Output the type of clock in /sys/module/printk/parameters/time so userspace programs can interpret the timestamp.

Quoted 7 lines

ISTR the reason that this was dropped was because of the a problem with the way systemd read the kernel's timestamps. It got the attention of Linus, and it was then pulled from the tree.

I need to go back and review the entire thread as it's been several years since we had the discussion although ISTR someone mentioning that doing two timestamps would not be a problem for systemd.

I guess that you talk about this thread
https://lore.kernel.org/lkml/CA+55aFwUfA__6MgMgjENnx+_RYY2ZOOLiSx2ea1AvYhSZN+78A@mail.gmail.com/

Quoted 6 lines

For example,

[48551.015086]

would be

Quoted 7 lines
[48551.015086] m[xxxx.xxxx]

for the monotonic clock timestamp, and

[48551.015086] b[xxxx.xxxx]

for the boottime clock, etc.

This approach has several drawbacks:

+ Too long prefix might make it hard to see the real messages because of shrunken/wrapped lines.

+ Too long lines are problem with slow consoles.

+ More space will be necessary to store all the time stamps.

+ Userspace tools would need/want to parse the format. We would need to maintain it forever.

Linus had an interesting idea to print all timestamps regularly. The frequency might be configurable. It might print, for example, the following line every 10 minutes or once a day:

  [48551.015086] System alive: b[xxxx.xxxx] m[xxxx.xxxx]

It might be useful in general to see when the system was still alive before it froze.

Would it be enough to sort messages printed with different clock sources?

Best Regards,
Petr

#3 lore raw
Quoted 59 lines · Petr Mladek
Quoted 16 lines · Prarit Bhargava
Quoted 8 lines · Chunyan Zhang

From: Prarit Bhargava <[email protected]>

printk.time=1/CONFIG_PRINTK_TIME=1 adds a unmodified local hardware clock timestamp to printk messages. The local hardware clock loses time each day making it difficult to determine exactly when an issue has occurred in the kernel log, and making it difficult to determine how kernel and hardware issues relate to each other in real time.

Quoted 7 lines

Make printk output different timestamps by adding options for no timestamp, the local hardware clock, the monotonic clock, the boottime clock, and the real clock. Allow a user to pick one of the clocks by using the printk.time kernel parameter. Output the type of clock in /sys/module/printk/parameters/time so userspace programs can interpret the timestamp.

Quoted 7 lines

ISTR the reason that this was dropped was because of the a problem with the way systemd read the kernel's timestamps. It got the attention of Linus, and it was then pulled from the tree.

I need to go back and review the entire thread as it's been several years since we had the discussion although ISTR someone mentioning that doing two timestamps would not be a problem for systemd.

I guess that you talk about this thread
https://lore.kernel.org/lkml/CA+55aFwUfA__6MgMgjENnx+_RYY2ZOOLiSx2ea1AvYhSZN+78A@mail.gmail.com/

Quoted 6 lines

For example,

[48551.015086]

would be

Quoted 7 lines
[48551.015086] m[xxxx.xxxx]

for the monotonic clock timestamp, and

[48551.015086] b[xxxx.xxxx]

for the boottime clock, etc.

This approach has several drawbacks:

+ Too long prefix might make it hard to see the real messages because of shrunken/wrapped lines.

+ Too long lines are problem with slow consoles.

+ More space will be necessary to store all the time stamps.

+ Userspace tools would need/want to parse the format. We would need to maintain it forever.

Linus had an interesting idea to print all timestamps regularly. The frequency might be configurable. It might print, for example, the following line every 10 minutes or once a day:

Quoted 7 lines
  [48551.015086] System alive: b[xxxx.xxxx] m[xxxx.xxxx]

It might be useful in general to see when the system was still alive before it froze.

Would it be enough to sort messages printed with different clock sources?

Hey Petr,

After reviewing the thread (it has been three years after all), I have asked Orson and Lyra to look at the suggested changes by tglx and Linus and submit those instead of "this" patch.

I should have updated this thread with that info. Sorry 'bout that.

P.