linux中的jiffies變數

來源:互聯網
上載者:User

   全域變數jiffies用來記錄自系統啟動以來產生的節拍的總數。啟動時,核心將該變數初始化為0,此後,每次時鐘中斷處理常式都會增加該變數的值。一秒內時鐘中斷的次數等於Hz,所以jiffies一秒內增加的值也就是Hz。

   系統已耗用時間以秒為單位,等於jiffies/Hz。

注意,jiffies類型為無符號長整型(unsigned long),其他任何類型存放它都不正確。

將以秒為單位的時間轉化為jiffies:

seconds * Hz

將jiffies轉化為以秒為單位的時間:

jiffies / Hz

相比之下,核心中將秒轉換為jiffies用的多些。

  • jiffies的內部表示

   jiffies定義於檔案<linux\Jiffies.h>中:

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

  使用者空間期望HZ=USER_HZ,但是如果它們不相等,則由宏完成轉換。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.