我如何在内核中打印当前时间?
How can i print current time in kernel?
我是 linux 的初学者。 (抱歉我的英语不好)
我应该打印当前时间并通过 linux 中的系统调用做一些事情。
我做了其他事情但未能打印当前时间..
我写的像
#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 和 Whosebug,
但我不知道如何在 kernel5 中打印当前时间..
有人可以帮助我吗?
是的,do_gettimeofday
由于 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)
,它将以秒和纳秒为单位为您提供时间。
struct timespec64 {
time64_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
如果你只想以纳秒为单位,你可以使用u64 ktime_get_ns(void)
。请检查上面的文档以了解适合您的目的。
您也可以查看 timekeeping.h and ktime.h 了解更多信息。
如果您想查找示例,只需使用 grep -rni <func name>
或 cscope
在内核源代码中搜索函数名称。也可以上网搜索here
我是 linux 的初学者。 (抱歉我的英语不好) 我应该打印当前时间并通过 linux 中的系统调用做一些事情。 我做了其他事情但未能打印当前时间.. 我写的像
#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 和 Whosebug, 但我不知道如何在 kernel5 中打印当前时间.. 有人可以帮助我吗?
是的,do_gettimeofday
由于 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)
,它将以秒和纳秒为单位为您提供时间。
struct timespec64 {
time64_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
如果你只想以纳秒为单位,你可以使用u64 ktime_get_ns(void)
。请检查上面的文档以了解适合您的目的。
您也可以查看 timekeeping.h and ktime.h 了解更多信息。
如果您想查找示例,只需使用 grep -rni <func name>
或 cscope
在内核源代码中搜索函数名称。也可以上网搜索here