如何在内核中显示当前时间? [英] How can i print current time in kernel?
问题描述
我是linux的初学者.(对不起我英语不好)我应该打印当前时间,并通过linux中的系统调用来执行某些操作.我做了其他事情,但无法显示当前时间.我写得像
i'm a beginner of linux. (sorry about my poor english) I should print current time and do something through system call in linux. I did other things but failed to print current time.. I wrote like
#include<linux/kernel.h>
#include<linux/time.h>
...
asmlinkage long sys_printtime(void) {
...
struct timeval time;
struct tm tm1;
...
do_gettimeofday(&time);
local_time=(u32)(time.tv_sec -(sys_tz.tz_minuteswest * 60));
time_to_tm(local_time,(3600*9),&tm1);
printk(KERN_DEBUG "time @(%04d-%02d-%02d %02d:%02d:%02d)\n", tm1.tm_year+1900,tm1.tm_mon+1,tm1.tm_mday,tm1.tm_hour,tm1.tm_min,tm1.tm_sec);
...
return 0;
}
但它不起作用.
该错误表明我不能使用do_gettimeofday,我终于知道我不能再使用do_gettimeofday,因为kernel5不支持.我在Google和stackoverflow上进行了搜索,但我不知道如何在kernel5中打印当前时间.有人可以帮助我吗?
The error said i can not use do_gettimeofday, and i finally knew that i can not use do_gettimeofday anymore because kernel5 doesn't support. I searched on google and stackoverflow, but i don't know how to print current time in kernel5.. anybody can help me?
推荐答案
是的,由于 https://www.kernel.org/doc/html/latest/core-api/timekeeping.html .
Yes, do_gettimeofday
has been removed because of y2038 problem. Instead the kernel provides time interfaces which you can use as per your need. Check the documentation https://www.kernel.org/doc/html/latest/core-api/timekeeping.html.
例如,您有 ktime_get_ts64(struct timespec64 * ts)
,它将为您提供以秒和纳秒为单位的时间.
For example, you have ktime_get_ts64(struct timespec64 *ts)
which will provide you time in seconds and nanoseconds.
struct timespec64 {
time64_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
如果只需要几纳秒,则可以使用 u64 ktime_get_ns(void)
.请检查上面的文档以适合您的目的.您也可以检查 timekeeping.h 和 ktime.h 了解更多信息.
If you only want in nanoseconds, you can use u64 ktime_get_ns(void)
. Please check the documentation above for what suits your purpose.
Also you can check timekeeping.h and ktime.h for further information.
如果要查找示例,请使用 grep -rni< func name>
或使用 cscope
在内核源代码中搜索函数名称.您也可以在此处
If you want to find an example just search the function name in the kernel source either using grep -rni <func name>
or use cscope
. You can also search it online here
这篇关于如何在内核中显示当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!