【转】内核的时间函数

因为需要在kernel tee driver中做个获取时间的测试,才有了这次搜索。

这篇比较符合需求:https://www.dingmos.com/index.php/archives/38/,感谢这位博主!

问题分析中看到的一些点:

auth_token_table.h中获取时间:

    explicit AuthTokenTable(size_t max_entries = 32, time_t (*clock_function)() = clock_gettime_raw)
        : max_entries_(max_entries), last_off_body_(clock_function()),
          clock_function_(clock_function) {}
time_t clock_gettime_raw() {
    struct timespec time;
    clock_gettime(CLOCK_MONOTONIC_RAW, &time);
return time.tv_sec;
}

首先上面的clock_gettime在kernel driver中不可用。

其次,搜索了下本地的代码,看到有地方用了do_gettimeofday,但实测下来在目前的版本中,也是不可行的。网上有看到do_gettimeofday的使用受限于内核版本。

opengrok检索了下,确实有通过版本区分的,如:

vip_uint64_t  gckvip_os_get_time(void)
{
    vip_uint64_t time = 0;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)
    struct timespec64 tv;
    /* Return the time of day in microseconds. */
    ktime_get_ts64(&tv);
    time = (vip_uint64_t)((tv.tv_sec * 1000000ULL) + (tv.tv_nsec / 1000));
#else
    struct timeval tv;
    do_gettimeofday(&tv);
    time = (vip_uint64_t)((tv.tv_sec * 1000000ULL) + tv.tv_usec);
#endif

    return time;
}

再比如:

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
unsigned long qdf_mc_timer_get_system_time(void)
{
    struct timespec64 tv;

    ktime_get_real_ts64(&tv);
    return tv.tv_sec * 1000 + tv.tv_nsec / 1000000;
}
qdf_export_symbol(qdf_mc_timer_get_system_time);

#else
unsigned long qdf_mc_timer_get_system_time(void)
{
    struct timeval tv;

    do_gettimeofday(&tv);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
qdf_export_symbol(qdf_mc_timer_get_system_time);
#endif

本地内核版本5.4,所以就不能用do_gettimeofday了。

以下函数测试正常。

 

posted @ 2023-10-09 14:58  xiululu  阅读(90)  评论(0编辑  收藏  举报