一、检查高分辨率计时器是否可用
- 在
/boot
目录中,检查内核配置文件。它应该有一行类似CONFIG_HIGH_RES_TIMERS=y
。 - 检查
/proc/timer_list
的内容。 - 使用
clock_getres
系统调用获取时钟分辨率。
二、高分辨率计时器的用户
- 精密计时器的主要用户是利用纳米睡眠、posix-timers和间隔计时器(itimer)接口的用户空间应用程序。
- 在内核中,驱动程序和子系统等用户需要精确定时的事件(例如多媒体)。
三、High Resolution timer 核心数据结构
We need to include the <linux/hrtimer.h>
(#include <linux/hrtimer.h>
) in order to use kernel timers. Kernel timers are described by the hrtimer
structure, defined in <linux/hrtimer.h>
:
struct hrtimer {
struct rb_node node;
ktime_t expires;
int (* function) (struct hrtimer *); //pointer to the timer base (per CPU and per clock)
struct hrtimer_base * base;
};
The hrtimer
structure must be initialized by init_hrtimer_#CLOCKTYPE.
四、High Resolution timer API
There is a new type, ktime_t
, which is used to store a time value in nanoseconds. On 64-bit systems, a ktime_t
is really just a 64-bit integer value in nanoseconds. On 32-bit machines, however, it is a two-field structure: one 32-bit value holds the number of seconds, and the other holds nanoseconds.
1. ktime_set(long secs, long nanosecs); ——
used to get the ktime_t
from seconds and nanoseconds.
2.void hrtimer_init( struct hrtimer *timer, clockid_t clock_id, enum hrtimer_mode mode ); ——
Initialize High Resolution Timer