本文系itspy原创,复制/转载请尽量标明原出处http://blog.csdn.net/yyplc/article/details/7065722,谢谢!
Linux系统定时器,在内核中扮演着重要角色。内核的许多重要实现如任务调度,工作队列等均以系统定时器关系密切。系统定时器能以可编程的频率中断处理,这一中断叫做软中断。此频率即为每秒的定时器节拍数HZ。HZ的越大,说明定时器节拍越小,线程调度的准确性会越高。但HZ设得过大,对一个系统来说并不好,会导CPU开销过大,反而造成任务调度效率降低。滴答jiffies 变量记录系统启动以来,系统定时器已经触发的次数。也就是说每过一秒jiffies的增量为HZ,一般HZ=100,HZ是可以配置的,在S3C2440 arm linux中配置为200.
下面基于Linux2.6.30.4源码来探讨其实现原理及其过程。
要理解系统定时器实现原理,先来看看关系到系统定时器的各种数据结构,其具体的描述参数。
结构体structtimer_list来描述timer的参数,其数据结构如下:
struct timer_list {
structlist_head entry; //timer双向链表
unsignedlong expires; //timer超时变量
void(*function)(unsigned long); //timer超时回调函数
unsignedlong data; //传递给回调函数的数据,也就是定时器数据
struct tvec_base *base; //timer base向量表用于timer_list的挂载和链表管理
//timer的一些扩展参数
#ifdef CONFIG_TIMER_STATS
void*start_site;
charstart_comm[16];
intstart_pid;
#endif
#ifdef CONFIG_LOCKDEP
structlockdep_map lockdep_map;
#endif
};
其中:
list_entry结构:
struct list_head {
structlist_head *next, *prev;
};
tevc_base的结构:
struct tvec_base {
spinlock_tlock; //自旋锁lock
structtimer_list *running_timer; //指向已经挂载进来的timer_list
unsignedlong timer_jiffies; //timer jiffies用于记录定时器当前jiffies
structtvec_root tv1; //5组tvec_base,从tv1~tv5,成员数各不相同
structtvec tv2; //其成员数TVR_SIZE,TVN_SIZE决定
structtvec tv3;
structtvec tv4;
structtvec tv5;
} ____cacheline_aligned;
#define TVN_BITS (CONFIG_BASE_SMALL ? 4 :6)
#define TVR_BITS (CONFIG_BASE_SMALL ? 6 :8)
#define TVN_SIZE (1 << TVN_BITS)
#define TVR_SIZE (1 << TVR_BITS)
#define TVN_MASK (TVN_SIZE - 1)
#define TVR_MASK (TVR_SIZE - 1)
struct tvec {
structlist_head vec[TVN_SIZE]; // tv2~t5个