Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233894
  • 博文数量: 82
  • 博客积分: 30
  • 博客等级: 民兵
  • 技术积分: 505
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 14:59
文章分类

全部博文(82)

文章存档

2015年(81)

2011年(1)

我的朋友

分类: 嵌入式

2015-03-06 15:08:50

全局变量jiffies用来记录自系统启动以来产生的节拍的总数。启动时,内核将该变量初始化为0,此后,每次时钟中断处理程序都会增加该变量的值。一秒内时钟中断的次数等于Hz,所以jiffies一秒内增加的值也就是Hz。

   系统运行时间以秒为单位,等于jiffies/Hz。

注意,jiffies类型为无符号长整型(unsigned long),其他任何类型存放它都不正确。

将以秒为单位的时间转化为jiffies:

seconds * Hz

将jiffies转化为以秒为单位的时间:

jiffies / Hz

相比之下,内核中将秒转换为jiffies用的多些。

  • jiffies的内部表示

   jiffies定义于文件中:

  1. /*
  2. * The 64-bit value is not atomic - you MUST NOT read it
  3. * without sampling the sequence number in xtime_lock.
  4. * get_jiffies_64() will do this for you as appropriate.
  5. */
  6. extern u64 __jiffy_data jiffies_64;
  7. extern unsigned long volatile __jiffy_data jiffies;

ld(1)脚本用于连接主内核映像(在x86上位于arch/i386/kernel/vmlinux.lds.S中),然后用jiffies_64变量的初值覆盖jiffies变量。因此jiffies取整个jiffies_64变量的低32位。

  访问jiffies的代码只会读取jiffies_64的低32位,通过get_jiffies_64()函数就可以读取整个64位的值。在64位体系结构上,jiffies_64和jiffies指的是同一个变量。

  1. #if (BITS_PER_LONG < 64)
  2. u64 get_jiffies_64(void);
  3. #else
  4. static inline u64 get_jiffies_64(void)
  5. {
  6. return (u64)jiffies;
  7. }
  8. #endif
  1. #if (BITS_PER_LONG < 64)
  2. u64 get_jiffies_64(void)
  3. {
  4.     unsigned long seq;
  5.     u64 ret;
  6. do {
  7.         seq = read_seqbegin(&xtime_lock);
  8.         ret = jiffies_64;
  9.     } while (read_seqretry(&xtime_lock, seq));
  10. return ret;
  11. }
  • jiffies的回绕wrap around

  当jiffies的值超过它的最大存放范围后就会发生溢出。对于32位无符号长整型,最大取值为(2^32)-1,即429496795。如果节拍计数达到了最大值后还要继续增加,它的值就会回绕到0。

  内核提供了四个宏来帮助比较节拍计数,它们能正确的处理节拍计数回绕的问题:

  1. /*
  2. *  These inlines deal with timer wrapping correctly. You are
  3. *  strongly encouraged to use them
  4. *  1. Because people otherwise forget
  5. *  2. Because if the timer wrap changes in future you won't have to
  6. *     alter your driver code.
  7. *
  8. * time_after(a,b) returns true if the time a is after time b.
  9. *
  10. * Do this with "<0" and ">=0" to only test the sign of the result. A
  11. * good compiler would generate better code (and a really good compiler
  12. * wouldn't care). Gcc is currently neither.
  13. */
  14. #define time_after(a,b)     \
  15.     (typecheck(unsigned long, a) && \
  16.      typecheck(unsigned long, b) && \
  17.      ((long)(b) - (long)(a) < 0))
  18. #define time_before(a,b)    time_after(b,a)
  19. #define time_after_eq(a,b)  \
  20.     (typecheck(unsigned long, a) && \
  21.      typecheck(unsigned long, b) && \
  22.      ((long)(a) - (long)(b) >= 0))
  23. #define time_before_eq(a,b) time_after_eq(b,a)
  24. /* Same as above, but does so with platform independent 64bit types.
  25. * These must be used when utilizing jiffies_64 (i.e. return value of
  26. * get_jiffies_64() */
  27. #define time_after64(a,b)   \
  28.     (typecheck(__u64, a) && \
  29.      typecheck(__u64, b) && \
  30.      ((__s64)(b) - (__s64)(a) < 0))
  31. #define time_before64(a,b)  time_after64(b,a)
  32. #define time_after_eq64(a,b)    \
  33.     (typecheck(__u64, a) && \
  34.      typecheck(__u64, b) && \
  35.      ((__s64)(a) - (__s64)(b) >= 0))
  36. #define time_before_eq64(a,b)   time_after_eq64(b,a)
  • 用户空间和HZ

  问题提出:

  在2.6以前的内核中,如果改变内核中的HZ值会给用户空间中某些程序造成异常结果。因为内核是以节拍数/秒的形式给用户空间导出这个值的,应用程序便依赖这个特定的HZ值。如果在内核中改变了HZ的定义值,就打破了用户空间的常量关系---用户空间并不知道新的HZ值。

  解决方法:

  内核更改所有导出的jiffies值。内核定义了USER_HZ来代表用户空间看到的HZ值。在x86体系结构上,由于HZ值原来一直是100,所以USER_HZ值就定义为100。内核可以使用宏jiffies_to_clock_t()将一个有HZ表示的节拍计数转换为一个由USER_HZ表示的节拍计数。

  1. /*
  2. * Convert jiffies/jiffies_64 to clock_t and back.
  3. */
  4. clock_t jiffies_to_clock_t(long x)
  5. {
  6. #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
  7. return x / (HZ / USER_HZ);
  8. #else
  9.     u64 tmp = (u64)x * TICK_NSEC;
  10.     do_div(tmp, (NSEC_PER_SEC / USER_HZ));
  11. return (long)tmp;
  12. #endif
  13. }
  14. unsigned long clock_t_to_jiffies(unsigned long x)
  15. {
  16. #if (HZ % USER_HZ)==0
  17. if (x >= ~0UL / (HZ / USER_HZ))
  18. return ~0UL;
  19. return x * (HZ / USER_HZ);
  20. #else
  21.     u64 jif;
  22. /* Don't worry about loss of precision here .. */
  23. if (x >= ~0UL / HZ * USER_HZ)
  24. return ~0UL;
  25. /* .. but do try to contain it here */
  26.     jif = x * (u64) HZ;
  27.     do_div(jif, USER_HZ);
  28. return jif;
  29. #endif
  30. }
  31. u64 jiffies_64_to_clock_t(u64 x)
  32. {
  33. #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
  34.     do_div(x, HZ / USER_HZ);
  35. #else
  36. /*
  37.      * There are better ways that don't overflow early,
  38.      * but even this doesn't overflow in hundreds of years
  39.      * in 64 bits, so..
  40.      */
  41.     x *= TICK_NSEC;
  42.     do_div(x, (NSEC_PER_SEC / USER_HZ));
  43. #endif
  44. return x;
  45. }
  1. /*
  2. * do_div() is NOT a C function. It wants to return
  3. * two values (the quotient and the remainder), but
  4. * since that doesn't work very well in C, what it
  5. * does is:
  6. *
  7. * - modifies the 64-bit dividend _in_place_
  8. * - returns the 32-bit remainder
  9. *
  10. * This ends up being the most efficient "calling
  11. * convention" on x86.
  12. */
  13. #define do_div(n,base) ({ \
  14.     unsigned long __upper, __low, __high, __mod, __base; \
  15.     __base = (base); \
  16.     asm("":"=a" (__low), "=d" (__high):"A" (n)); \
  17.     __upper = __high; \
  18. if (__high) { \
  19.         __upper = __high % (__base); \
  20.         __high = __high / (__base); \
  21.     } \
  22.     asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (__base), "0" (__low), "1" (__upper)); \
  23.     asm("":"=A" (n):"a" (__low),"d" (__high)); \
  24.     __mod; \
  25. })

  用户空间期望HZ=USER_HZ,但是如果它们不相等,则由宏完成转换

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