Linux时间管理之clocksource(1)


前面提到了Linux下的时间相关的硬件。TSC PIT,HPET,ACPI_PM,这些硬件以一定的频率产生时钟中断,来帮助我们计时。Linux为了管理这些硬件,抽象出来clocksource。

  1. struct clocksource { 
  2.     /* 
  3.      * Hotpath data, fits in a single cache line when the 
  4.      * clocksource itself is cacheline aligned. 
  5.      */ 
  6.     cycle_t (*read)(struct clocksource *cs); 
  7.     cycle_t cycle_last; 
  8.     cycle_t mask; 
  9.     u32 mult; 
  10.     u32 shift; 
  11.     u64 max_idle_ns; 
  12.     u32 maxadj; 
  13. #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA 
  14.     struct arch_clocksource_data archdata; 
  15. #endif 
  16.   
  17.     const char *name; 
  18.     struct list_head list; 
  19.     int rating; 
  20.     int (*enable)(struct clocksource *cs); 
  21.     void (*disable)(struct clocksource *cs); 
  22.     unsigned long flags; 
  23.     void (*suspend)(struct clocksource *cs); 
  24.     void (*resume)(struct clocksource *cs); 
  25.   
  26.     /* private: */ 
  27. #ifdef CONFIG_CLOCKSOURCE_WATCHDOG 
  28.     /* Watchdog related data, used by the framework */ 
  29.     struct list_head wd_list; 
  30.     cycle_t cs_last; 
  31.     cycle_t wd_last; 
  32. #endif 
  33. } ____cacheline_aligned; 

这些参数当中,比较重要的是rating,shift,mult。其中rating在上一篇博文提到了:

  • 1--99: 不适合于用作实际的时钟源,只用于启动过程或用于测试;
  • 100--199:基本可用,可用作真实的时钟源,但不推荐;
  • 200--299:精度较好,可用作真实的时钟源;
  • 300--399:很好,精确的时钟源;
  • 400--499:理想的时钟源,如有可能就必须选择它作为时钟源;

我们基本在前面看到:    

  1. include/linux/acpi_pmtmr.h 
  2. ------------------------------------------ 
  3. #define PMTMR_TICKS_PER_SEC 3579545 
  4.   
  5. drivers/clocksource/acpi_pm.c 
  6. --------------------------------------------- 
  7. static struct clocksource clocksource_acpi_pm = { 
  8.           .name = "acpi_pm"
  9.           .rating = 200
  10.           .read = acpi_pm_read
  11.           .mask = (cycle_t)ACPI_PM_MASK, 
  12.           .mult = 0, /*to be calculated*/ 
  13.           .shift = 22
  14.           .flags = CLOCK_SOURCE_IS_CONTINUOUS
  15.   
  16.  }; 
  17.   
  18. dmesg output 
  19. ------------------------ 
  20. [ 0.664201] hpet0: 8 comparators, 64-bit 14.318180 MHz counter 
  21.   
  22. arch/86/kernel/hpet.c 
  23. -------------------------------- 
  24. static struct clocksource clocksource_hpet = { 
  25.     .name = "hpet"
  26.     .rating = 250
  27.     .read = read_hpet
  28.     .mask = HPET_MASK
  29.     .flags = CLOCK_SOURCE_IS_CONTINUOUS
  30.     .resume = hpet_resume_counter
  31. #ifdef CONFIG_X86_64 
  32.     .archdata = { .vclock_mode = VCLOCK_HPET }, 
  33. #endif 
  34. }; 
  35.   
  36.   
  37. dmesg output: 
  38. ----------------------------- 
  39. [ 0.004000] Detected 2127.727 MHz processor.  
  40.   
  41.   
  42. arch/x86/kernel/tsc.c 
  43. -------------------------------------- 
  44. static struct clocksource clocksource_tsc = { 
  45.     .name = "tsc"
  46.     .rating = 300
  47.     .read = read_tsc
  48.     .resume = resume_tsc
  49.     .mask = CLOCKSOURCE_MASK(64), 
  50.     .flags = CLOCK_SOURCE_IS_CONTINUOUS | 
  51.                   CLOCK_SOURCE_MUST_VERIFY, 
  52. #ifdef CONFIG_X86_64 
  53.     .archdata = { .vclock_mode = VCLOCK_TSC }, 
  54. #endif 
  55. }; 

从上面可以看到,acpi_pm,hpet tsc的rating分别是200,250,300,他们的rating基本是和他们的frequency符合,TSC以2127.727MHz的频率技压群雄,等级rating=300最高,被选择成current_clocksource:

  1. root@manu:~# cat /sys/devices/system/clocksource/clocksource0/available_clocksource  
  2. tsc hpet acpi_pm  
  3. root@manu:~# cat /sys/devices/system/clocksource/clocksource0/current_clocksource  
  4. tsc 

除此外,还有两个参数shift和mult,这两个参数是干啥的呢?

我们想一下,假如我们需要给你个以一定频率输出中断的硬件,你如何计时?比如我有一个频率是1000Hz的硬件,当前时钟源计数是3500,过了一段时间,我抬头看了下时钟源计数至是5500,过去了2000cycles,我就知道了过去了2000/1000 =2 second。

  1. times_elapse = cycles_interval / frequency  

从上面的例子中,我抬头看了下当前计数值这个肯定是瞎掰了,实际上要想获取时钟源还是需要和硬件打交道的。在clocksource中有一个成员变量是read,这个就是一个时钟源注册的时候,提供的一个函数,如果你想获得我的当前计数值,请调用这个read 函数。以TSC时钟为例:

  1. static struct clocksource clocksource_tsc = { 
  2.     .name = "tsc"
  3.     .rating = 300
  4.     .read = read_tsc
  5.     .resume = resume_tsc
  6.     .mask = CLOCKSOURCE_MASK(64), 
  7.     .flags = CLOCK_SOURCE_IS_CONTINUOUS | 
  8.                   CLOCK_SOURCE_MUST_VERIFY, 
  9. #ifdef CONFIG_X86_64 
  10.     .archdata = { .vclock_mode = VCLOCK_TSC }, 
  11. #endif 
  12. }; 
  13.   
  14. /*--------- arch/x86/kernel/tsc.c -------------------*/ 
  15. static cycle_t read_tsc(struct clocksource *cs) 
  16.     cycle_t ret = (cycle_t)get_cycles(); 
  17.   
  18.     return ret >= clocksource_tsc.cycle_last ? 
  19.         ret : clocksource_tsc.cycle_last; 
  20.   
  21. /*------- arch/x86/include/asm/tsc.h----------------------*/ 
  22. static inline cycles_t get_cycles(void) 
  23.     unsigned long long ret = 0
  24.   
  25. #ifndef CONFIG_X86_TSC 
  26.     if (!cpu_has_tsc) 
  27.         return 0; 
  28. #endif 
  29.     rdtscll(ret); 
  30.   
  31.     return ret; 
  32.   
  33. /*------arch/x86/include/asm/msr.h-----------------*/ 
  34. #define rdtscll(val)                        \ 
  35.     ((val) = __native_read_tsc()) 
  36.   
  37. static __always_inline unsigned long long __native_read_tsc(void) 
  38.     DECLARE_ARGS(val, low, high); 
  39.   
  40.     asm volatile("rdtsc" : EAX_EDX_RET(val, low, high)); 
  41.   
  42.     return EAX_EDX_VAL(val, low, high); 

根据这个脉络,我们知道,最终就是rdtsc这条指令来获取当前计数值cycles。

扯了半天read这个成员变量,可以回到shift和mult了。其实shift和mult是为了解决下面这个公式的:

  1. times_elapse = cycles_interval / frequency 

就像上面的公式,有频率就足以计时了。为啥弄出来个shift和mult。原因在于kernel搞个除法不太方便,必须转化乘法和移位。Kernel中有很多这种把除法转化成乘法的样例。那么公式变成了:

  1. times_elapse = cycles_interval * mult >> shift 

Kernel用乘法+移位来替换除法:根据cycles来计算过去了多少ns。

  1. /** 
  2.  * clocksource_cyc2ns - converts clocksource cycles to nanoseconds 
  3.  * @cycles:    cycles 
  4.  * @mult:    cycle to nanosecond multiplier 
  5.  * @shift:    cycle to nanosecond pisor (power of two) 
  6.  * 
  7.  * Converts cycles to nanoseconds, using the given mult and shift. 
  8.  * 
  9.  * XXX - This could use some mult_lxl_ll() asm optimization 
  10.  */ 
  11. static inline s64 clocksource_cyc2ns(cycle_t cycles, u32 mult, u32 shift) 
  12.     return ((u64) cycles * mult) >> shift; 

单纯从精度上讲,肯定是mult越大越好,但是计算过程可能溢出,所以mult也不能无限制的大,这个计算中有个magic number 600 :

  1. void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) 
  2.     u64 sec; 
  3.     /* 
  4.      * Calc the maximum number of seconds which we can run before 
  5.      * wrapping around. For clocksources which have a mask > 32bit 
  6.      * we need to limit the max sleep time to have a good 
  7.      * conversion precision. 10 minutes is still a reasonable 
  8.      * amount. That results in a shift value of 24 for a 
  9.      * clocksource with mask >= 40bit and f >= 4GHz. That maps to 
  10.      * ~ 0.06ppm granularity for NTP. We apply the same 12.5% 
  11.      * margin as we do in clocksource_max_deferment() 
  12.      */ 
  13.     sec = (cs->mask - (cs->mask >> 3)); 
  14.     do_p(sec, freq); 
  15.     do_p(sec, scale); 
  16.     if (!sec) 
  17.         sec = 1
  18.     else if (sec > 600 && cs->mask > UINT_MAX) 
  19.         sec = 600
  20.   
  21.     clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, 
  22.              NSEC_PER_SEC / scale, sec * scale); 
  23.   
  24.     /* 
  25.      * for clocksources that have large mults, to avoid overflow. 
  26.      * Since mult may be adjusted by ntp, add an safety extra margin 
  27.      * 
  28.      */ 
  29.     cs->maxadj = clocksource_max_adjustment(cs); 
  30.     while ((cs->mult + cs->maxadj < cs->mult) 
  31.         || (cs->mult - cs->maxadj > cs->mult)) { 
  32.         cs->mult >>= 1; 
  33.         cs->shift--; 
  34.         cs->maxadj = clocksource_max_adjustment(cs); 
  35.     } 
  36.   
  37.     cs->max_idle_ns = clocksource_max_deferment(cs); 

这个600的意思是600秒,表示的Timer两次计算当前计数值的差不会超过10分钟。主要考虑的是系统进入IDLE状态之后,时间信息不会被更新,10分钟内只要退出IDLE,clocksource还是可以成功的转换时间。当然了,最后的这个时间不一定就是10分钟,它由clocksource_max_deferment计算并将结果存储在max_idle_ns中。


相关内容