1.Linux HZ
Linux核心每隔固定周期会发出timer interrupt (IRQ 0),HZ是用来定义每一秒有几次timer interrupts。举例来说,HZ为1000,代表每秒有1000次timer interrupts。 HZ可在编译内核时通过make menuconfig设定。HZ可设定为100、250、1000。
以内核版本2.6.25为例:(内核设置HZ为1000)
# cat .config | grep CONFIG_HZ # CONFIG_HZ_100 is not set # CONFIG_HZ_250 is not set # CONFIG_HZ_300 is not set CONFIG_HZ_1000=y CONFIG_HZ=1000 |
2.Linux tick
Tick是HZ的倒数,意即timer interrupt每发生一次中断的时间。如HZ为1000时,tick为1毫秒(millisecond)。
3.Linux jiffies
jiffies为linux内核变量(32位无符号整型,unsigned long),它被用来记录系统自开机以来,已经过多少个ticks。每发生一次timer interrupt(即每次时钟中断处理程序timer_interrupt()),jiffies变量会被加1。系统中采用jiffies来计算时间,但由于jiffies溢出可能造成时间比较的错误,因而强烈建议在编码中使用timer_after,timer_before等宏来比较时间先后关系。
/* * These inlines deal with timer wrapping correctly. You are * strongly encouraged to use them * 1. Because people otherwise forget * 2. Because if the timer wrap changes in future you won't have to * alter your driver code. * * time_after(a,b) returns true if the time a is after time b. * * Do this with "<0" and ">=0" to only test the sign of the result. A * good compiler would generate better code (and a really good compiler * wouldn't care). Gcc is currently neither. */ #define time_after(a,b) \ (typecheck(unsigned long, a) && \ typecheck(unsigned long, b) && \ ((long)(b) - (long)(a) < 0)) #define time_before(a,b) time_after(b,a)
#define time_after_eq(a,b) \ (typecheck(unsigned long, a) && \ typecheck(unsigned long, b) && \ ((long)(a) - (long)(b) >= 0)) #define time_before_eq(a,b) time_after_eq(b,a)
/* * Calculate whether a is in the range of [b, c]. */ #define time_in_range(a,b,c) \ (time_after_eq(a,b) && \ time_before_eq(a,c))
|
在宏time_after中,首先确保两个输入参数a和b的数据类型为unsigned long,然后才执行实际的比较,这是编码中应当注意的地方。
阅读(2788) | 评论(0) | 转发(0) |