Linux内核中的时间函数

ktime时间函数

  • 基于ktime_t格式的时间
ktime_t ktime_get(void);

获取基于CLOCK_MONOTONIC的当前时间,返回ktime_t格式的数据类型。

ktime_t ktime_get_boottime(void);

获取基于CLOCK_BOOTTIME的当前时间,返回ktime_t格式的数据类型。

ktime_t ktime_get_real(void);

获取基于CLOCK_REALTIME的当前时间,也就是我们常说的墙上时间xtime,返回ktime_t格式的数据类型。

ktime_t ktime_get_raw(void);

获取基于CLOCK_MONOTONIC_RAW的当前时间,返回ktime_t格式的数据类型。

  • 基于struct timespec格式的时间
void ktime_get_ts64(struct timespec64 *ts);       //CLOCK_MONOTONIC
void ktime_get_real_ts64(struct timespec64 *);    //CLOCK_REALTIME
void ktime_get_boottime_ts64(struct timespec64 *);//CLOCK_BOOTTIME
void ktime_get_raw_ts64(struct timespec64 *);     //CLOCK_MONOTONIC_RAW

逐步过时接口

以下接口是内核逐步过时的时间接口,但是一些第三方的驱动可能依然在使用,因此内核也会做兼容,建议在可选时尽量使用上面的ktime系列接口来获取时间。

  • 基于struct timespec格式的时间
static inline void get_monotonic_boottime(struct timespec *ts)
{
    *ts = ktime_to_timespec(ktime_get_boottime());
}

static inline void get_monotonic_boottime64(struct timespec64 *ts)
{
    *ts = ktime_to_timespec64(ktime_get_boottime());
}

获取基于CLOCK_BOOTTIME的当前时间,返回struct timespec格式的数据类型。建议使用 ktime_get_boottime() 接口来进行替代。

struct timespec64 current_kernel_time64(void);

static inline struct timespec current_kernel_time(void)
{
    struct timespec64 now = current_kernel_time64();

    return timespec64_to_timespec(now);
}

获取基于CLOCK_REALTIME(xtime)的时间,返回struct timespec类型的数据类型。更新版本的内核开始使用ktime_get_coarse_real_ts64()/ktime_get_coarse_ts64()来替代。

  • 基于struct timeval格式的时间
void do_gettimeofday(struct timeval *tv);

获取基于xtime的时间,返回struct timeval类型的数据类型。新版内核开始使用ktime_get_real_ts64来替代。

参考:
kernel-4.14 source code
https://www.kernel.org/doc/html/latest/core-api/timekeeping.html

发布了234 篇原创文章 · 获赞 78 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/rikeyone/article/details/98498526