Linux2.6中实现了一种新的定时器hrtimer。与传统定时器使用时间轮算法不同,hrtimer使用了红黑树算法。hrtimer本身可以配置成高精度和普通精度两种,在单CPU系统和多CPU系统中的实现也有区别。这里先分析最简单的配置成普通精度、单CPU的情况。配置成高精度的情况见后续文章。
1. 时钟源的定义为了实现hrtimer,Linux为系统中每一个CPU定义了一个hrtimer_cpu_base,这个结构体的定义如下:
struct hrtimer_cpu_base { raw_spinlock_t lock; struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES]; //时钟源 #define HRTIMER_MAX_CLOCK_BASES 2 #ifdef CONFIG_HIGH_RES_TIMERS ktime_t expires_next; int hres_active; int hang_detected; unsigned long nr_events; unsigned long nr_retries; unsigned long nr_hangs; ktime_t max_hang_time; #endif }; <p> </p><pre class="cpp" name="code">struct hrtimer_clock_base { struct hrtimer_cpu_base *cpu_base; clockid_t index; struct rb_root active; struct rb_node *first; ktime_t resolution; ktime_t (*get_time)(void); ktime_t softirq_time; #ifdef CONFIG_HIGH_RES_TIMERS ktime_t offset; #endif };
在hrtimer.c中,有为每个CPU具体定义hrtimer_cpu_base的代码:
DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = { .clock_base = { { .index = CLOCK_REALTIME, .get_time = &ktime_get_real, .resolution = KTIME_LOW_RES, }, { .index = CLOCK_MONOTONIC, .get_time = &ktime_get, .resolution = KTIME_LOW_RES, }, } };
1.1 时钟源类型