内核变量——Jiffies

全局变量jiffies表示自系统启动以来系统产生的嘀嗒数。当启动时,内核初始化该变量为0。每次时钟中断就会增1,所以系统运行时间可以计算为:jiffies/HZ秒。

       jiffies变量定义如下:

extern unsigned long volatile jiffies;

jiffies转换为秒:(jiffies / HZ)。将秒换算为jiffies(seconds*HZ)

       jiffies比较相关的宏

#define time_after(unknown, known) ((long)(known) - (long)(unknown) < 0)

#define time_before(unknown, known) ((long)(unknown) - (long)(known) < 0)

#define time_after_eq(unknown, known) ((long)(unknown) - (long)(known) >= 0)

#define time_before_eq(unknown, known) ((long)(known) - (long)(unknown) >= 0)

使用例子:

unsigned long timeout = jiffies + HZ/2;

/* timeout in 0.5s */

/* ... */

 

if (time_before(jiffies, timeout)) {

 

/* we did not time out, good ... */

 

} else {

 

/* we timed out, error ... */

 

}

 

代码参考:

typedef struct {
    unsigned long jiffies;
}os_time_t;

static inline osal_result os_clock_get_time(os_time_t *time){
    time->jiffies = jiffies;
    return OSAL_SUCCESS;
}

static inline osal_result os_clock_get_time_diff_msecs( os_time_t *time,
                                                        unsigned long *msecs)
{
    *msecs = (jiffies - time->jiffies) * 1000 / HZ;
    return OSAL_SUCCESS;
}

static inline osal_result os_clock_get_time_diff_secs(  os_time_t *time,
                                                        unsigned long *secs)
{
    *secs = (jiffies - time->jiffies) / HZ;
    return OSAL_SUCCESS;
}


你可能感兴趣的:(内核变量——Jiffies)