内核模式clock_gettime()

pje*_*y58 2 linux posix kernel timer

我正在尝试在内核中使用POSIX时钟函数,但编译器不断给我错误:错误:函数'clock_gettime'的隐式声明

long __timer_end(struct timespec start_time)
{
    struct timespec end_time;
    clock_gettime(CLOCK_REALTIME_COARSE, &end_time);
    return(end_time.tv_nsec - start_time.tv_nsec);

}

struct timespec __timer_start(void)
{
    struct timespec start_time;
    clock_gettime(CLOCK_REALTIME_COARSE, &start_time);
    return start_time;
}
Run Code Online (Sandbox Code Playgroud)

函数定义<linux/posix_clock.h>为的结构的一部分,posix_clock_operations并且有一对函数posix_clock_register()posix_clock_unregister()。这些评论使人们相信这些功能将构成该posix_clock_operations结构。我已经在init和exit函数中实现了它们,希望它们的存在会神奇地使for的声明clock_gettime()出现,但事实并非如此。

有谁知道我需要做些什么才能使这一功能起作用?我真的需要定义我自己的所有函数posix clock_operations吗?

提前致谢,

皮特

pje*_*y58 6

clock_gettime()内核中似乎没有,但是有一个称为nsec的分辨率时钟current_kernel_time()。因此,重写我的计时器如下所示:

long timer_end(struct timespec start_time)
{
    struct timespec end_time = current_kernel_time();
    return(end_time.tv_nsec - start_time.tv_nsec);
}

struct timespec timer_start(void)
{
    return current_kernel_time();
}
Run Code Online (Sandbox Code Playgroud)

似乎工作正常,但适用于ns粒度性能测试的更高性能版本看起来像这样:

long timer_end(struct timespec start_time)
{
    struct timespec end_time;
    getrawmonotonic(&end_time);
    return(end_time.tv_nsec - start_time.tv_nsec);
}

struct timespec timer_start(void)
{
    struct timespec start_time;
    getrawmonotonic(&start_time);
    return start_time;
}
Run Code Online (Sandbox Code Playgroud)

感谢您的评论和建议。

皮特