Chinaunix首页 | 论坛 | 博客
  • 博客访问: 158375
  • 博文数量: 40
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 355
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-27 18:05
文章分类
文章存档

2011年(1)

2010年(9)

2009年(16)

2008年(14)

我的朋友

分类: LINUX

2008-12-05 11:22:42

7.1. 测量时间流失
1.HZ 值定义在linux/param.h
2.jiffies_64计数器64位:在系统启动时初始化为 0, 代表从最后一次启动以来的时钟嘀哒的数目,只读
3.jiffies变量unsigned long,不同平台下,可能是jiffies_64(64位平台下两者相同)或者
     jiffies_64的低有效位,驱动编写首选,只读
7.1.1. 使用 jiffies 计数器

1.linux/jiffies.h

2.时间表示

     j = jiffies; /* read the current value */
     stamp_1 = j + HZ; /* 1 second in the future */
     stamp_half = j + HZ/2; /* half a second */
     stamp_n = j + n * HZ / 1000; /* n milliseconds */

     在64位机器上得到值:

      #include
      u64 get_jiffies_64(void);

3.时间比较

#include
int time_after(unsigned long a, unsigned long b);
int time_before(unsigned long a, unsigned long b);
int time_after_eq(unsigned long a, unsigned long b);
int time_before_eq(unsigned long a, unsigned long b);
 diff = (long)t2 - (long)t1;.

以上转换为 signed long, 相减, 得出结果


4.与用户空间时间转换

linux/time.h

结构体

    旧的struct timeval{ seconds xx;microseconds yy;}

    新的struct timespec {seconds xx; nanoseconds yy;}

函数

 

#include <linux/time.h>
unsigned long timespec_to_jiffies(struct timespec *value);
void jiffies_to_timespec(unsigned long jiffies, struct timespec *value);
unsigned long timeval_to_jiffies(struct timeval *value);
void jiffies_to_timeval(unsigned long jiffies, struct timeval *value);

7.1.2. 处理器特定的寄存器

1.若对时间要求严格,可依赖硬件,大部分现代处理器包含一个计数器寄存器,它在每个时钟周期固定地递增一次

 

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