Linux 如何同步内核时间和logcat时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6329648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sync Kernel time and logcat time?
提问by Pavan Manjunath
I am working on an Android phone based on the Linux Kernel. I'm using kmsg
for kernel logs and adb logcat -v time
for platform logs. The problem is Kernel logs shows time from 0.000000
and logcat is showing from the start of system time ( For example, if time on the phone is 10.43.00, it'll show the first log from this time )
我正在开发基于 Linux 内核的 Android 手机。我kmsg
用于内核日志和adb logcat -v time
平台日志。问题是内核日志显示时间从0.000000
系统时间开始,而 logcat 显示从系统时间开始(例如,如果手机上的时间是 10.43.00,它将显示从这个时间开始的第一个日志)
Now I am unable to compare events from these 2 logs as the time base ( reference) is different. Can anyone kindly point out how to sync these 2 times?
现在我无法比较这 2 个日志中的事件,因为时基(参考)不同。谁能指出如何同步这两次?
采纳答案by Will Tate
Pavan,
帕万,
Perhaps with you can tag your logcat prints with the time since last boot? This should be closer to the time shown in kmsg.
也许你可以用自上次启动以来的时间标记你的 logcat 打印?这应该更接近 kmsg 中显示的时间。
Time since last boot can be retrieved with elapsedRealtime()
.
可以使用 检索自上次启动以来的时间elapsedRealtime()
。
回答by jaiprakashgogi
you can create a single file containing both kernel and platform logs as follows:
您可以创建一个包含内核和平台日志的文件,如下所示:
$adb shell
$logcat -v time -f /dev/kmsg | cat /proc/kmsg > /data/klog_plog_log.txt
you can differentiate both the logs using time stamps in the platform logs. And then pull the file to your local drive using
您可以使用平台日志中的时间戳区分这两个日志。然后使用
adb pull /data/klog_plog_log.txt > sample.txt
Refer http://jai-tech.blogspot.com/2012/01/taking-kernel-and-platform-logs-of.html. Hope this helps.
请参阅http://jai-tech.blogspot.com/2012/01/taking-kernel-and-platform-logs-of.html。希望这可以帮助。
Regards, JP
问候, JP
回答by Jerry.CUi
I think it can use like this:
我认为它可以像这样使用:
adb shell logcat -v time -f /dev/kmsg | adb shell cat /proc/kmsg >sample.txt
回答by smichak
The single file method described above is nice but may not always be useful as the input from logcat and kmsg might be delayed due to buffering. So you will most likely see a block of kmsg entries and then a block of logcat entries which might still be interleaved in real time.
上面描述的单文件方法很好,但可能并不总是有用,因为来自 logcat 和 kmsg 的输入可能会由于缓冲而延迟。因此,您很可能会看到一组 kmsg 条目,然后是一组可能仍实时交错的 logcat 条目。
That said, you might be able to sync kmsg time and logcat time by looking for device suspend messages in kmsg (grep UTC):
也就是说,您可以通过在 kmsg (grep UTC) 中查找设备挂起消息来同步 kmsg 时间和 logcat 时间:
<6>[249485.550811] suspend: exit suspend, ret = 0 (2012-12-27 16:16:46.300872527 UTC)
<6>[249485.550811] 暂停:退出暂停,ret = 0 (2012-12-27 16:16:46.300872527 UTC)
As you can see those entries in kmsg report the current system wallclock time which can then be synchronized with the kmsg time value. Note however that the kmsg time value does not increase when the device is asleep while the wallclock time obviously does. To that end you will have to resynchronize on the wallclock time on every suspend entry and exit in order to get the correct wallclock time.
如您所见,kmsg 中的这些条目报告当前系统挂钟时间,然后可以与 kmsg 时间值同步。但是请注意,当设备处于睡眠状态时,kmsg 时间值不会增加,而挂钟时间显然会增加。为此,您必须在每次暂停进入和退出时重新同步挂钟时间,以获得正确的挂钟时间。
回答by Purdea Andrei
Another solution would be similar to jpg's answer, but in the other direction, redirect the kernel messages into logcat. This is better, because too many logcat messages might overload the serial console (if you have it active).
另一种解决方案类似于 jpg 的答案,但在另一个方向上,将内核消息重定向到 logcat。这更好,因为太多的 logcat 消息可能会使串行控制台过载(如果它处于活动状态)。
you can run this in an android shell:
您可以在 android shell 中运行它:
cat /proc/kmsg | while read LINE; do echo 'kerneladb shell '(cat /proc/kmsg | while read LINE; do echo \06kernel\0$LINE\0 > /dev/log/main; done)'
'$LINE'adb shell '(logcat & cat /proc/kmsg) > /path/to/log/file'
' > /dev/log/main; done
or this in a host shell:
或者在主机外壳中:
##代码##The first time you start the command you will see all of the current dmesg messages in one place, but any further messages will be interleaved, when they appear.
第一次启动该命令时,您将在一个地方看到所有当前的 dmesg 消息,但任何进一步的消息在出现时都会交错排列。
and then examine logcat in a different shell. If you examine logcat with -v time, then the kernel messages will contain both the logcat and the kernel timestamps. Of course there may be delays between the two.
然后在不同的 shell 中检查 logcat。如果您使用 -v time 检查 logcat,则内核消息将同时包含 logcat 和内核时间戳。当然,两者之间可能会有延迟。
Another, even simpler way to see messages interleaved would be:
查看交错消息的另一种更简单的方法是:
##代码##But in this case it's a little harder to identify messages coming from the kernel, and you can't tell how kernel timestamps relate to logcat timestamps.
但在这种情况下,识别来自内核的消息有点困难,而且您无法判断内核时间戳与 logcat 时间戳之间的关系。