Use of clock_getres - newbie Linux C

后端 未结 2 1931
情歌与酒
情歌与酒 2021-01-12 04:34

I\'m trying to determine the granularity of the timers on my Linux box. According to the man pages for clock_getres, I should be able to use this snippet:

#         


        
相关标签:
2条回答
  • 2021-01-12 05:10

    Unfortunately, clock_getres() POSIX function (optional "realtime" part - note REALTIME mark on POSIX page http://pubs.opengroup.org/onlinepubs/009695399/functions/clock_getres.html) does not report timers granularity in Linux. It can return only two predefined results: either 1/HZ for low resolution clocks (where HZ is value of CONFIG_HZ macro used when configuring linux kernel, typical values are 100 300 1000) or 1 ns for high resolution clocks (hrtimers).

    The file linux/include/linux/hrtimer.h in kernel has the comment about such granularity and about meaning of clock_getres() result http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/hrtimer.h

    269 /*
    270  * The resolution of the clocks. The resolution value is returned in
    271  * the clock_getres() system call to give application programmers an
    272  * idea of the (in)accuracy of timers. Timer values are rounded up to
    273  * this resolution values.
    274  */
    

    So, even if the timer source is registered as "hrtimer" (high-resolution timer), it may clocks not every nanosecond (ns). And value returned from clock_getres() will only say that this timer was not rounded (because timespec struct has nanosecond precision).

    In Linux the POSIX API is often implemented by glibc (or its derivatives like eglibc), which are linked to the all programs by default (-lc option of linker). Glibc with versions less than 2.17 separated some optional parts of POSIX into additional libraries, for example, -lpthread or -lrt. clock_getres() was defined in -lrt.

    -lrt option is not needed for glibc 2.17 and newer, according to Linux man page of clock_getres() and clock_gettime() functions, http://man7.org/linux/man-pages/man2/clock_gettime.2.html

    Link with -lrt (only for glibc versions before 2.17).

    The change was also registered in the compatibility tracker: http://upstream.rosalinux.ru/compat_reports/glibc/2.16.0_to_2.17/abi_compat_report.html

    Added Symbols ... libc-2.17.so ... clock_getres

    0 讨论(0)
  • 2021-01-12 05:33

    You need to link with with RT library (-lrt)

    0 讨论(0)
提交回复
热议问题