内核模式clock_gettime()

时间:2014-03-22 14:52:30

标签: linux timer kernel posix

我试图在内核中使用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;
}

这些函数在<linux/posix_clock.h>中定义为posix_clock_operations结构的一部分,并且有一对函数posix_clock_register()posix_clock_unregister()。评论使人们相信这些函数将填充posix_clock_operations结构。我已经在我的初始化函数和退出函数中都实现了,希望它们的存在能够神奇地使clock_gettime()的前向声明出现,但它并没有。

有谁知道我需要做些什么来使这个功能有效?我是否真的需要将所有自己的函数定义为填充posix clock_operations

提前致谢,

皮特

2 个答案:

答案 0 :(得分:4)

似乎内核中没有clock_gettime()但是有一个名为current_kernel_time()的nsec分辨率时钟。所以重写我的计时器看起来像这样:

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();
}

它似乎运行良好,但同样适用于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;
}

感谢您的评论和指示。

皮特

答案 1 :(得分:1)

正如pjenney58在回答中指出的那样,您可以使用current_kernel_time(),但要记住它会为您提供“挂钟”时间(以HZ精度),如果您想要描述时间,则不建议这样做一些内核代码占用 - 只是因为它还会考虑在挂起模式下花费的时间。要分析内核代码中的时序,有一个内核等效的userland clock_gettime(CLOCK_MONOTONIC,...)即。 do_posix_clock_monotonic_gettime() timekeeping.h,在struct timespec中定义为宏。它将指向@echo off for /f "delims=" %%i in ('dir "d:/Database/BARC/" /b/a-d ^| find /v /c "::"') do set count=%%i echo %count% 的指针作为一个参数,其中以秒为单位返回单调时钟时间和nsec准确度。

相关问题