Chinaunix首页 | 论坛 | 博客
  • 博客访问: 114035
  • 博文数量: 11
  • 博客积分: 565
  • 博客等级: 中士
  • 技术积分: 267
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-02 17:17
文章分类
文章存档

2014年(1)

2012年(2)

2011年(4)

2010年(4)

我的朋友

分类: LINUX

2011-06-16 19:32:00

1.Linux HZ

Linux核心每隔固定周期会发出timer interrupt (IRQ 0)HZ是用来定义每一秒有几次timer interrupts。举例来说,HZ1000,代表每秒有1000timer 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

TickHZ的倒数,意即timer interrupt每发生一次中断的时间。如HZ1000时,tick1毫秒(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中,首先确保两个输入参数ab的数据类型为unsigned long,然后才执行实际的比较,这是编码中应当注意的地方。


 

阅读(2694) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~