原文地址:http://blog.chinaunix.net/uid-20727076-id-3086954.html
clocksource定义了一个clock device的基本属性及行为, 这些clock device一般都有计数, 定时, 产生中断能力。
最重要的成员是read(), cycle_last和cycle_interval.
read()定义了读取clock device count 寄存器当前计数值接口
cycle_last保存上一次周期(时钟中断)计数值,它会在update_wall_time()得到更新,且以cycle_interval为基数增加:clock->cycle_last += clock->cycle_interval;
cycle_interval定义了“每个jiffies对应的计数值”
以1128为例,它使用ixp4xx的cpu,HZ=100,clocksource 为66.6666Mhz,那么这个值为666667(0xa2c2b)
通过在内核里打印这个变量的值,也验证了这个结果
这个结构内的值, 无论是cycle_t, 还是u64类型(实际cycle_t就是u64)都是时钟的计数值(cycle)
内核会默认注册一个jiffies的clocksource, 如下:
kernel/time/jiffies.c
注册jiffies之后,内核会提供精度为1/HZ的时间精度,但是你的设备体系还会注册它自己的clocksource,这个clocksource是硬件相关的,会更精准,例如:
arch/arm/mach-ixp4xx/common.c
rating高的clocksource会被优先使用 ,所以66.66Mhz的时钟源被优先使用,一个计数大概相当于15ns,所以,理论上提供15ns级的精度。
linux内核里的墙上时钟 xtime
xtime的更新:
你的时钟中断处理函数->timer_tick->update_wall_time()
在update_wall_time里会更新墙上时间xtime
大家发现了,update_wall_time是在时钟中断里被调用的,时钟中断每秒运行HZ次,那在粒度为HZ的时钟中断里如何更新纳秒级别的墙上时钟xtime的值呢?
从上述代码里可以看到,在update_wall_time里会从之前注册的clocksource中读取计数值,然后将其换算为纳秒并赋值给xtime!以此为基础,xtime为用户空间提供了精准时钟,比如:
用户空间的gettimeofday函数,进入内核为:
测试:
实例一:
在我的1121的内核代码里,仅仅提供jiffies这个clocksource且HZ为200,测试结果如下:
可见内核只能提供精度为1/HZ=1/200=5ms=5000us的精度
实例二:在我的1128下,由于注册了它自己的clocksource,且精度比jiffies高(见上面),所以会使用它自己的clocksource,测试结果如下:
前后两次调用gettimeofday时最小的差值是22us,远远大于jiffies精度,虽然没有测试到15ns的精度,但可以确认内核使用的是它自己的clocksource!
注:
update_wall_time打印出的信息
offset is cb6f9, interval is a2c2b
offset is a3347, interval is a2c2b
offset is a2e7f, interval is a2c2b
offset is a2e48, interval is a2c2b
offset is a2dcc, interval is a2c2b
offset is a2de4, interval is a2c2b
offset is a2ff9, interval is a2c2b
offset is a2e12, interval is a2c2b
offset is a2de6, interval is a2c2b
offset is a2e63, interval is a2c2b
offset is a2e2b, interval is a2c2b
offset is a2ed5, interval is a2c2b
offset is a2e88, interval is a2c2b
offset is a2e03, interval is a2c2b
offset is a2e66, interval is a2c2b
offset is a2dde, interval is a2c2b
offset is a2e96, interval is a2c2b
offset is a2e06, interval is a2c2b
offset is a2e0d, interval is a2c2b
offset is a2df7, interval is a2c2b
offset is a2e19, interval is a2c2b
offset is a2dd3, interval is a2c2b
clocksource定义了一个clock device的基本属性及行为, 这些clock device一般都有计数, 定时, 产生中断能力。
- struct clocksource {
- /*
- * First part of structure is read mostly
- */
- char *name;
- struct list_head list;
- int rating;
- cycle_t (*read)(struct clocksource *cs);
- int (*enable)(struct clocksource *cs);
- void (*disable)(struct clocksource *cs);
- cycle_t mask;
- u32 mult;
- u32 mult_orig;
- u32 shift;
- unsigned long flags;
- cycle_t (*vread)(void);
- void (*resume)(void);
- cycle_t cycle_interval;
- u64 xtime_interval;
- u32 raw_interval;
- cycle_t cycle_last ____cacheline_aligned_in_smp;
- u64 xtime_nsec;
- s64 error;
- struct timespec raw_time;
- ...
- };
最重要的成员是read(), cycle_last和cycle_interval.
read()定义了读取clock device count 寄存器当前计数值接口
cycle_last保存上一次周期(时钟中断)计数值,它会在update_wall_time()得到更新,且以cycle_interval为基数增加:clock->cycle_last += clock->cycle_interval;
cycle_interval定义了“每个jiffies对应的计数值”
以1128为例,它使用ixp4xx的cpu,HZ=100,clocksource 为66.6666Mhz,那么这个值为666667(0xa2c2b)
通过在内核里打印这个变量的值,也验证了这个结果
这个结构内的值, 无论是cycle_t, 还是u64类型(实际cycle_t就是u64)都是时钟的计数值(cycle)
内核会默认注册一个jiffies的clocksource, 如下:
kernel/time/jiffies.c
- static cycle_t jiffies_read(struct clocksource *cs)
- {
- return (cycle_t) jiffies;
- }
-
- struct clocksource clocksource_jiffies = {
- .name = "jiffies",
- .rating = 1, /* lowest valid rating*/
- .read = jiffies_read,
- .mask = 0xffffffff, /*32bits*/
- .mult = NSEC_PER_JIFFY << JIFFIES_SHIFT, /* details above */
- .mult_orig = NSEC_PER_JIFFY << JIFFIES_SHIFT,
- .shift = JIFFIES_SHIFT,
- };
- static int __init init_jiffies_clocksource(void)
- {
- return clocksource_register(&clocksource_jiffies);
- }
注册jiffies之后,内核会提供精度为1/HZ的时间精度,但是你的设备体系还会注册它自己的clocksource,这个clocksource是硬件相关的,会更精准,例如:
arch/arm/mach-ixp4xx/common.c
- cycle_t ixp4xx_get_cycles(struct clocksource *cs)
- {
- return *IXP4XX_OSTS;
- }
-
- static struct clocksource clocksource_ixp4xx = {
- .name = "OSTS",
- .rating = 200,
- .read = ixp4xx_get_cycles,
- .mask = CLOCKSOURCE_MASK(32),
- .shift = 20,
- .flags = CLOCK_SOURCE_IS_CONTINUOUS,
- };
- unsigned long ixp4xx_timer_freq = FREQ;
- static int __init ixp4xx_clocksource_init(void)
- {
- clocksource_ixp4xx.mult =
- clocksource_hz2mult(ixp4xx_timer_freq,
- clocksource_ixp4xx.shift);
- clocksource_register(&clocksource_ixp4xx);
-
- return 0;
- }
- #define FREQ 66666666
rating高的clocksource会被优先使用 ,所以66.66Mhz的时钟源被优先使用,一个计数大概相当于15ns,所以,理论上提供15ns级的精度。
linux内核里的墙上时钟 xtime
- struct timespec xtime __attribute__ ((aligned (16)));
- struct timespec {
- __kernel_time_t tv_sec; /* seconds */
- long tv_nsec; /* nanoseconds */
- };
xtime的更新:
你的时钟中断处理函数->timer_tick->update_wall_time()
在update_wall_time里会更新墙上时间xtime
- void update_wall_time(void)
- {
- cycle_t offset;
-
- /* Make sure we're fully resumed: */
- if (unlikely(timekeeping_suspended))
- return;
-
- #ifdef CONFIG_GENERIC_TIME
- offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask;
- #else
- offset = clock->cycle_interval;
- #endif
- clock->xtime_nsec = (s64)xtime.tv_nsec << clock->shift;
- printk(KERN_INFO "offset is %llx, interval is %llx\n", offset, clock->cycle_interval);//测试用
- /* normally this loop will run just once, however in the
- * case of lost or late ticks, it will accumulate correctly.
- */
- while (offset >= clock->cycle_interval) {
- /* accumulate one interval */
- offset -= clock->cycle_interval;
- clock->cycle_last += clock->cycle_interval;
-
- clock->xtime_nsec += clock->xtime_interval;
- if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) {
- clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift;
- xtime.tv_sec++;
- second_overflow();
- }
-
- clock->raw_time.tv_nsec += clock->raw_interval;
- if (clock->raw_time.tv_nsec >= NSEC_PER_SEC) {
- clock->raw_time.tv_nsec -= NSEC_PER_SEC;
- clock->raw_time.tv_sec++;
- }
-
- /* accumulate error between NTP and clock interval */
- clock->error += tick_length;
- clock->error -= clock->xtime_interval << (NTP_SCALE_SHIFT - clock->shift);
- }
-
- /* correct the clock when NTP error is too big */
- clocksource_adjust(offset);
-
- if (unlikely((s64)clock->xtime_nsec < 0)) {
- s64 neg = -(s64)clock->xtime_nsec;
- clock->xtime_nsec = 0;
- clock->error += neg << (NTP_SCALE_SHIFT - clock->shift);
- }
-
- /* store full nanoseconds into xtime after rounding it up and
- * add the remainder to the error difference.
- */
- xtime.tv_nsec = ((s64)clock->xtime_nsec >> clock->shift) + 1;
- clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
- clock->error += clock->xtime_nsec << (NTP_SCALE_SHIFT - clock->shift);
-
- update_xtime_cache(cyc2ns(clock, offset));
-
- /* check to see if there is a new clocksource to use */
- change_clocksource();
- update_vsyscall(&xtime, clock);
- }
大家发现了,update_wall_time是在时钟中断里被调用的,时钟中断每秒运行HZ次,那在粒度为HZ的时钟中断里如何更新纳秒级别的墙上时钟xtime的值呢?
从上述代码里可以看到,在update_wall_time里会从之前注册的clocksource中读取计数值,然后将其换算为纳秒并赋值给xtime!以此为基础,xtime为用户空间提供了精准时钟,比如:
用户空间的gettimeofday函数,进入内核为:
- void do_gettimeofday(struct timeval *tv)
- {
- struct timespec now;
-
- getnstimeofday(&now);
- tv->tv_sec = now.tv_sec;
- tv->tv_usec = now.tv_nsec/1000;
- }
- void getnstimeofday(struct timespec *ts)
- {
- cycle_t cycle_now, cycle_delta;
- unsigned long seq;
- s64 nsecs;
-
- WARN_ON(timekeeping_suspended);
-
- do {
- seq = read_seqbegin(&xtime_lock);
-
- *ts = xtime;
-
- /* read clocksource: */
- cycle_now = clocksource_read(clock);
-
- //计算自上次中断依赖clock的增加值
- cycle_delta = (cycle_now - clock->cycle_last) & clock->mask;
- //将clock的增加值转化为纳秒值
- nsecs = cyc2ns(clock, cycle_delta);
- nsecs += arch_gettimeoffset();
- //我的设备体系里arch_gettimeoffset=0
- } while (read_seqretry(&xtime_lock, seq));
- timespec_add_ns(ts, nsecs);
- }
测试:
- #include <sys/time.h>
- #include <stdio.h>
- int main()
- {
- int i=0;
- struct timeval tv;
- for(i=0; i<100; i++){
- gettimeofday(&tv, NULL);
- printf("gettimeofday sec is %d, usec is %d\n", tv.tv_sec, tv.tv_usec);
- }
- return 0;
- }
在我的1121的内核代码里,仅仅提供jiffies这个clocksource且HZ为200,测试结果如下:
- ...
- gettimeofday sec is 1196141330, usec is 315000
- gettimeofday sec is 1196141330, usec is 315000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 320000
- gettimeofday sec is 1196141330, usec is 325000
- gettimeofday sec is 1196141330, usec is 325000
- ...
实例二:在我的1128下,由于注册了它自己的clocksource,且精度比jiffies高(见上面),所以会使用它自己的clocksource,测试结果如下:
- gettimeofday sec is 1393836919, usec is 786698
- gettimeofday sec is 1393836919, usec is 787353
- gettimeofday sec is 1393836919, usec is 787388
- gettimeofday sec is 1393836919, usec is 787413
- gettimeofday sec is 1393836919, usec is 787435
- gettimeofday sec is 1393836919, usec is 787458
- gettimeofday sec is 1393836919, usec is 787481
- gettimeofday sec is 1393836919, usec is 787504
- gettimeofday sec is 1393836919, usec is 787527
- gettimeofday sec is 1393836919, usec is 787549
- gettimeofday sec is 1393836919, usec is 787572
注:
update_wall_time打印出的信息
offset is cb6f9, interval is a2c2b
offset is a3347, interval is a2c2b
offset is a2e7f, interval is a2c2b
offset is a2e48, interval is a2c2b
offset is a2dcc, interval is a2c2b
offset is a2de4, interval is a2c2b
offset is a2ff9, interval is a2c2b
offset is a2e12, interval is a2c2b
offset is a2de6, interval is a2c2b
offset is a2e63, interval is a2c2b
offset is a2e2b, interval is a2c2b
offset is a2ed5, interval is a2c2b
offset is a2e88, interval is a2c2b
offset is a2e03, interval is a2c2b
offset is a2e66, interval is a2c2b
offset is a2dde, interval is a2c2b
offset is a2e96, interval is a2c2b
offset is a2e06, interval is a2c2b
offset is a2e0d, interval is a2c2b
offset is a2df7, interval is a2c2b
offset is a2e19, interval is a2c2b
offset is a2dd3, interval is a2c2b