如何获取当前时间,因为 dmesg 会打印它

问题描述 投票:0回答:0

AFAIU,

dmesg
使用内核环形缓冲区中打印的时间戳。

我想要的是:当前时间,因为它会显示刚刚打印的内核日志消息。

必须考虑到系统可能已暂停并随后恢复。

一些资源提到这将是

/proc/uptime
的第一个字段。
AFAIU,这并不是因为它使用了
ktime_get_boottime_ts64()
,因此最终
CLOCK_BOOTTIME

我以为它会使用

CLOCK_MONOTONIC
,它可以从
/proc/timer_list
获得,但它似乎也没有这样做。

我有一个系统,它暂停了一段时间然后恢复,当我现在这样做时,例如(作为根):

# echo 3 >| /proc/sys/vm/drop_caches ; dmesg | tail -n 1 ; cat /proc/uptime ; grep now /proc/timer_list
[224529.744667] bash (5248): drop_caches: 3
386212.79 716849.32
now at 222216601953051 nsecs

人们清楚地看到

/proc/uptime
相差甚远,但
/proc/timer_list
仍然相差 2313 秒。

我尝试查看

printk()
的来源,除非我弄错了,否则时间在这里: https://github.com/torvalds/linux/blob/52b1853b080a082ec3749c3a9577f6c71b1d4a90/kernel/printk/printk.c#L2171-L2176

但我无法真正找出它给出的内容,或者如何从 shell 中将其打印为当前时间。

谢谢, 克里斯.

linux linux-kernel printk dmesg
© www.soinside.com 2019 - 2024. All rights reserved.