EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 pulbieup 于 2020-12-24 15:55 编辑
% F! @& k0 u; N% }4 B/ y1 O# B2 M' ^6 J
系统中有很多与时间相关的程序(比如定期执行的任务,某一时间执行的任务,推迟一段时间执行的任务),因此,时间的管理对于linux来说非常重要。
. c- X" j% d4 b/ I4 ?1 t3 w主要内容: - 系统时间
- 定时器
- 定时器相关概念
- 定时器执行流程
- 实现程序延迟的方法
- 定时器和延迟的例子
% n) ]0 ]" I7 o* K" S . t% s$ D6 F& D. O0 k
1. 系统时间系统中管理的时间有2种:实际时间和定时器。 1.1 实际时间实际时间就是现实中钟表上显示的时间,其实内核中并不常用这个时间,主要是用户空间的程序有时需要获取当前时间, 所以内核中也管理着这个时间。
2 ~6 ^1 X/ e$ | y O实际时间的获取是在开机后,内核初始化时从RTC读取的。 内核读取这个时间后就将其放入内核中的 xtime 变量中,并且在系统的运行中不断更新这个值。 注:RTC就是实时时钟的缩写,它是用来存放系统时间的设备。一般和BIOS一样,由主板上的电池供电的,所以即使关机也可将时间保存。
8 B }, H+ b; P9 A) D: B% L1 {( [+ W实际时间存放的变量 xtime 在文件 kernel/time/timekeeping.c中。 1 E7 c( E& g# Y0 v: X
- /* 按照16位对齐,其实就是2个long型的数据 */
- struct timespec xtime __attribute__ ((aligned (16)));
- /* timespec结构体的定义如下, 参考 <linux/time.h> */
- struct timespec {
- __kernel_time_t tv_sec; /* seconds */
- long tv_nsec; /* nanoseconds */
- };
- /* _kernel_time_t 定义如下 */
- typedef long __kernel_time_t;
% d9 y$ `: a% I2 Q
% y# u9 t# G: l
3 }3 F q' K% D9 f: t: T: k" V! q- }3 E: G
系统读写 xtime 时用的就是顺序锁。 8 F+ U' a6 C6 ^( V- e1 n; N
- /* 写入 xtime 参考 do_sometimeofday 方法 */
- int do_settimeofday(struct timespec *tv)
- {
- /* 省略 。。。。 */
- write_seqlock_irqsave(&xtime_lock, flags); /* 获取写锁 */
- /* 更新 xtime */
- write_sequnlock_irqrestore(&xtime_lock, flags); /* 释放写锁 */
- /* 省略 。。。。 */
- return 0;
- }
- void getnstimeofday(struct timespec *ts)
- /* 读取 xtime 参考 do_gettimeofday 方法 */
- void do_gettimeofday(struct timeval *tv)
- {
- struct timespec now;
- getnstimeofday(&now); /* 就是在这个方法中获取读锁,并读取 xtime */
- tv->tv_sec = now.tv_sec;
- tv->tv_usec = now.tv_nsec/1000;
- }
- {
- /* 省略 。。。。 */
- /* 顺序锁中读锁来循环获取 xtime,直至读取过程中 xtime 没有被改变过 */
- do {
- seq = read_seqbegin(&xtime_lock);
- *ts = xtime;
- nsecs = timekeeping_get_ns();
- /* If arch requires, add in gettimeoffset() */
- nsecs += arch_gettimeoffset();
- } while (read_seqretry(&xtime_lock, seq));
- /* 省略 。。。。 */
- }
! q5 @1 n5 D, X9 ?7 F9 a' m3 @! q$ Q- E 2 F; A# l( p! J
; v/ j+ I0 b1 H$ J
上述场景中,写锁必须要优先于读锁(因为 xtime 必须及时更新),而且写锁的使用者很少(一般只有系统定期更新xtime的线程需要持有这个锁)。 这正是 顺序锁的应用场景。
- n1 e: k1 k: G# o: v. ]1.2 定时器定时器是内核中主要使用的时间管理方法,通过定时器,可以有效的调度程序的执行。 动态定时器是内核中使用比较多的定时器,下面重点讨论的也是动态定时器。 T* L& c; h) M
2. 定时器内核中的定时器有2种,静态定时器和动态定时器。 静态定时器一般执行了一些周期性的固定工作: - 更新系统运行时间
- 更新实际时间
- 在SMP系统上,平衡各个处理器上的运行队列
- 检查当前进程是否用尽了自己的时间片,如果用尽,需要重新调度。
- 更新资源消耗和处理器时间统计值5 K2 G q5 u% V5 i
8 Z: c. i. ^9 D/ [: t3 d: o
动态定时器顾名思义,是在需要时(一般是推迟程序执行)动态创建的定时器,使用后销毁(一般都是只用一次)。 一般我们在内核代码中使用的定时器基本都是动态定时器,下面重点讨论动态定时器相关的概念和使用方法。
. _ m5 s0 }: B3 o; M- }3. 定时器相关概念定时器的使用中,下面3个概念非常重要: - HZ
- jiffies
- 时间中断处理程序
& P4 q# x6 S j- x3 D1 f- H
3 [4 H' ]* J0 {3.1 HZ节拍率(HZ)是时钟中断的频率,表示的一秒内时钟中断的次数。 比如 HZ=100 表示一秒内触发100次时钟中断程序。
4 j: F) H* G9 K, ^9 D* [HZ的值一般与体系结构有关,x86 体系结构一般定义为 100,参考文件 include/asm-generic/param.h HZ值的大小的设置过程其实就是平衡 精度和性能 的过程,并不是HZ值越高越好。 HZ值 | 优势 | 劣势 | | 高HZ | 时钟中断程序运行的更加频繁,依赖时间执行的程序更加精确,
# j3 o P8 u/ ?9 G3 O对资源消耗和系统运行时间的统计更加精确。 | 时钟中断执行的频繁,增加系统负担 / ~: {7 w1 r# y: T5 [( T+ E) I: K
时钟中断占用的CPU时间过多 |
+ l8 K7 p- g, b4 {4 Z* x此外,有一点需要注意,内核中使用的HZ可能和用户空间中定义的HZ值不一致,为了避免用户空间取得错误的时间, 内核中也定义了 USER_HZ,即用户空间使用的HZ值。 一般来说,USER_HZ 和 HZ 都是相差整数倍,内核中通过函数 jiffies_to_clock_t 来将内核来将内核中的 jiffies转为 用户空间 jiffies
6 M1 s# u9 d9 b' q! X" f$ [- /* 参见文件: kernel/time.c *
- //*
- * Convert jiffies/jiffies_64 to clock_t and back.
- */
- clock_t jiffies_to_clock_t(unsigned long x)
- {
- #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
- # if HZ < USER_HZ
- return x * (USER_HZ / HZ);
- # else
- return x / (HZ / USER_HZ);
- # endif
- #else
- return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
- #endif
- }
- EXPORT_SYMBOL(jiffies_to_clock_t);
/ G. L: J. c- E5 A+ R
5 M% l5 j) z4 S) i" B& F
- r; ~: S* y: N* |, s! J9 h3.2 jiffiesjiffies用来记录自系统启动以来产生的总节拍数。比如系统启动了 N 秒,那么 jiffies就为 N×HZ jiffies的相关定义参考头文件 <linux/jiffies.h> include/linux/jiffies.h 0 E: p# P2 r' s
- /* 64bit和32bit的jiffies定义如下 */
- extern u64 __jiffy_data jiffies_64;
- extern unsigned long volatile __jiffy_data jiffies;4 L5 L% {4 X/ r4 S. [
' m" X5 L+ Q3 r3 x1 `8 g; X: O5 N; q+ R2 A
: ]# A* x: O: ?$ `) }. Z( o使用定时器时一般都是以jiffies为单位来延迟程序执行的,比如延迟5个节拍后执行的话,执行时间就是 jiffies+5 32位的jiffies的最大值为 2^32-1,在使用时有可能会出现回绕的问题。 比如下面的代码: 5 n3 Y; H2 a4 k2 a v D
- unsigned long timeout = jiffies + HZ/2; /* 设置超时时间为 0.5秒 */
- while (timeout < jiffies)
- {
- /* 还没有超时,继续执行任务 */
- }
- /* 执行超时后的任务 */4 c, h# ?% M( v
' C: _( O( ~* l; ^7 n4 n- M" F9 x; c' E& N- S) g d7 h8 t3 K
; n9 [, w2 ~; U4 n* z正常情况下,上面的代码没有问题。当jiffies接近最大值的时候,就会出现回绕问题。 由于是unsinged long类型,所以jiffies达到最大值后会变成0然后再逐渐变大,如下图所示:
) o2 U. S s1 a) L# E: i2 w% F所以在上述的循环代码中,会出现如下情况: - 循环中第一次比较时,jiffies = J1,没有超时
- 循环中第二次比较时,jiffies = J2,实际已经超时了,但是由于jiffies超过的最大值后又从0开始,所以J2远远小于timeout
- while循环会执行很长时间(> 2^32-1 个节拍)不会结束,几乎相当于死循环了9 A- U9 ^0 f4 J$ s( X9 F9 m
8 I0 V. _9 f$ ~. C
为了回避回扰的问题,可以使用<linux/jiffies.h>头文件中提供的 time_after,time_before等宏 ! B- m! V% b/ Z
- #define time_after(a,b) \
- (typecheck(unsigned long, a) && \
- typecheck(unsigned long, b) && \
- ((long)(b) - (long)(a) < 0))
- #define time_before(a,b) time_after(b,a)
- #define time_after_eq(a,b) \
- (typecheck(unsigned long, a) && \
- typecheck(unsigned long, b) && \
- ((long)(a) - (long)(b) >= 0))
- #define time_before_eq(a,b) time_after_eq(b,a)4 b& h, r* o: n! _$ u4 t/ B, J8 Y
% f; R a L- F: ~
6 u$ e& T# Q+ M# @, S
) t6 g4 a d2 d' K上述代码的原理其实就是将 unsigned long 类型转换为 long 类型来避免回扰带来的错误, long 类型超过最大值时变化趋势如下: 8 |, ?4 c$ W) y3 n
long 型的数据的回绕会出现在 2^31-1 变为 -2^32 的时候,如下图所示: - 第一次比较时,jiffies = J1,没有超时
- 第二次比较时,jiffies = J2,一般 J2 是负数 6 L B) T) B& o& ~, f
理论上 (long)timeout - (long)J2 = 正数 - 负数 = 正数(result) * a! m8 P0 P5 k- L
但是,这个正数(result)一般会大于 2^31 - 1,所以long型的result又发生了一次回绕,变成了负数。
7 v/ x5 ?2 o: h- d" }除非timeout和J2之间的间隔 > 2^32 个节拍,result的值才会为正数(注1)。
/ D) b; E+ t" X% J
注1:result的值为正数时,必须是在result的值 小于 2^31-1 的情况下,大于 2^31-1 会发生回绕。 上图中 X + Y 表示timeout 和 J2之间经过的节拍数。 result 小于 2^31-1 ,也就是 timeout - J2 < 2^31 – 1 timeout 和 -J2 表示的节拍数如上图所示。(因为J2是负数,所有-J2表示上图所示范围的值) 因为 timeout + X + Y - J2 = 2^31-1 + 2^32 所以 timeout - J2 < 2^31 - 1 时, X + Y > 2^32 也就是说,当timeout和J2之间经过至少 2^32 个节拍后,result才可能变为正数。 timeout和J2之间相差这么多节拍是不可能的(不信可以用HZ将这些节拍换算成秒就知道了。。。)
/ f* I' z# X \3 X利用time_after宏就可以巧妙的避免回绕带来的超时判断问题,将之前的代码改成如下代码即可: $ E* E/ S/ W* s: f, L! r
- unsigned long timeout = jiffies + HZ/2; /* 设置超时时间为 0.5秒 */
- while (time_after(jiffies, timeout))
- {
- /* 还没有超时,继续执行任务 */
- }
- /* 执行超时后的任务 */
$ ^' }: r9 A8 _0 u
0 y7 ]) h7 W- g3 O, ~$ z7 \; Z. z/ W9 `% I8 J- b2 p& A
3.3 时钟中断处理程序时钟中断处理程序作为系统定时器而注册到内核中,体系结构的不同,可能时钟中断处理程序中处理的内容不同。 但是以下这些基本的工作都会执行: - 获得 xtime_lock 锁,以便对访问 jiffies_64 和墙上时间 xtime 进行保护
- 需要时应答或重新设置系统时钟
- 周期性的使用墙上时间更新实时时钟
- 调用 tick_periodic()
9 _) Y- q3 S( V+ Q
4 w( p8 R) s1 Z% s' U4 ctick_periodic函数位于: kernel/time/tick-common.c 中 + L, F' P- h' g/ Y) W5 G6 ~* X
- static void tick_periodic(int cpu)
- {
- if (tick_do_timer_cpu == cpu) {
- write_seqlock(&xtime_lock);
- /* Keep track of the next tick event */
- tick_next_period = ktime_add(tick_next_period, tick_period);
- do_timer(1);
- write_sequnlock(&xtime_lock);
- }
- update_process_times(user_mode(get_irq_regs()));
- profile_tick(CPU_PROFILING);
- }+ ? T+ i' g6 w% l7 _6 F
b: v2 O# s* V$ m4 R5 T. I5 L" J7 w
; Y4 ~ j+ \ N- V' \- ]其中最重要的是 do_timer 和 update_process_times 函数。 我了解的步骤进行了简单的注释。
4 d9 A$ h5 n$ o9 p C% y3 T0 W" l- void do_timer(unsigned long ticks)
- {
- /* jiffies_64 增加指定ticks */
- jiffies_64 += ticks;
- /* 更新实际时间 */
- update_wall_time();
- /* 更新系统的平均负载值 */
- calc_global_load();
- }
- void update_process_times(int user_tick)
- {
- struct task_struct *p = current;
- int cpu = smp_processor_id();
- /* 更新当前进程占用CPU的时间 */
- account_process_tick(p, user_tick);
- /* 同时触发软中断,处理所有到期的定时器 */
- run_local_timers();
- rcu_check_callbacks(cpu, user_tick);
- printk_tick();
- /* 减少当前进程的时间片数 */
- scheduler_tick();
- run_posix_cpu_timers(p);
- }
8 t4 Z7 v+ k, e6 F
9 Z" s9 a+ L, `
9 A! P. S% I/ m) f( X' S4. 定时器执行流程这里讨论的定时器执行流程是动态定时器的执行流程。
, K! A8 n) ~/ b+ r( m) P( D4.1 定时器的定义定时器在内核中用一个链表来保存的,链表的每个节点都是一个定时器。 参见头文件 <linux/timer.h>
- m( H0 L* G3 N+ d- struct timer_list {
- struct list_head entry;
- unsigned long expires;
- void (*function)(unsigned long);
- unsigned long data;
- struct tvec_base *base;
- #ifdef CONFIG_TIMER_STATS
- void *start_site;
- char start_comm[16];
- int start_pid;
- #endif
- #ifdef CONFIG_LOCKDEP
- struct lockdep_map lockdep_map;
- #endif
- };
6 W+ q$ m: g: g( H# Y/ Z7 g% \ - l2 B# x: @; p7 |$ K6 G) y, i
通过加入条件编译的参数,可以追加一些调试信息。 , p6 P* D3 B/ f* _) ?- e8 r! V! ]
4.2 定时器的生命周期一个动态定时器的生命周期中,一般会经过下面的几个步骤: 1. 初始化定时器:
" U8 X3 m: I/ P t/ G2 d' `' D- struct timer_list my_timer; /* 定义定时器 */
- init_timer(&my_timer); /* 初始化定时器 */. l* R1 l' [% ?0 G
* k# U* M8 K0 ?3 v# J4 G5 O6 a
2. 填充定时器: ' \: _& q5 N$ S% y' ^* ]9 \
- my_timer.expires = jiffies + delay; /* 定义超时的节拍数 */
- my_timer.data = 0; /* 给定时器函数传入的参数 */
- my_timer.function = my_function; /* 定时器超时时,执行的自定义函数 */
- /* 从定时器结构体中,我们可以看出这个函数的原型应该如下所示: */
- void my_function(unsigned long data);
1 s# q2 Y, ?2 t' u6 W
/ Q- r; F5 ^0 I5 a. n" m3. 激活定时器和修改定时器: 激活定时器之后才会被触发,否则定时器不会执行。 修改定时器主要是修改定时器的延迟时间,修改定时器后,不管原先定时器有没有被激活,都会处于激活状态。 . l' C, a5 D: y9 q0 D
填充定时器结构之后,可以只激活定时器,也可以只修改定时器,也可以激活定时器后再修改定时器。 所以填充定时器结构和触发定时器之间的步骤,也就是虚线框中的步骤是不确定的。
@' g7 Z' B+ A' s) \& N3 [- add_timer(&my_timer); /* 激活定时器 */
- mod_timer(&my_timer, jiffies + new_delay); /* 修改定时器,设置新的延迟时间 *// _% x, a! b! E; n
# U5 m; U4 l3 v2 o* ?1 N
0 [5 c/ y. c! q2 W- k% S+ a* n
4. 触发定时器: 每次时钟中断处理程序会检查已经激活的定时器是否超时,如果超时就执行定时器结构中的自定义函数。
6 M+ h7 D2 I/ x, a3 }- L5. 删除定时器: 激活和未被激活的定时器都可以被删除,已经超时的定时器会自动删除,不用特意去删除。 ( J% i5 a0 p/ }8 O$ f" m
- /*
- * 删除激活的定时器时,此函数返回1
- * 删除未激活的定时器时,此函数返回0
- */
- del_timer(&my_timer);
- w9 ]% r! J; D
4 Z' Y+ O6 U7 r! X4 ]$ i6 F在多核处理器上用 del_timer 函数删除定时器时,可能在删除时正好另一个CPU核上的时钟中断处理程序正在执行这个定时器,于是就形成了竞争条件。 为了避免竞争条件,建议使用 del_timer_sync 函数来删除定时器。 del_timer_sync 函数会等待其他处理器上的定时器处理程序全部结束后,才删除指定的定时器。
! k) |9 [' k. R4 @* r9 \' E5 Y- /*
- * 和del_timer 不同,del_timer_sync 不能在中断上下文中执行
- */
- del_timer_sync(&my_timer);4 w% f q b" z+ ~9 o+ ~% W0 r7 j
; k9 S# e& _/ n6 Z) s: z
3 E. ~% @& n5 C( [
5 h1 x; b3 M" ?9 K3 ?; ]8 ^) Z
5. 实现程序延迟的方法内核中有个利用定时器实现延迟的函数 schedule_timeout 这个函数会将当前的任务睡眠到指定时间后唤醒,所以等待时不会占用CPU时间。 6 G9 g/ }# r! p- P
- /* 将任务设置为可中断睡眠状态 */
- set_current_state(TASK_INTERRUPTIBLE);
- /* 小睡一会儿,“s“秒后唤醒 */
- schedule_timeout(s*HZ);
0 _ e# J7 e% f8 U6 p6 @$ E
3 R' R# \7 B0 v* Z% c6 J, e# u1 Q7 C4 w8 ^
查看 schedule_timeout 函数的实现方法,可以看出是如何使用定时器的。
0 {6 m4 [- T; B2 e" B. A8 Z6 @% E6 `6 G, S' v' A3 ~7 w
- signed long __sched schedule_timeout(signed long timeout)
- {
- /* 定义一个定时器 */
- struct timer_list timer;
- unsigned long expire;
- switch (timeout)
- {
- case MAX_SCHEDULE_TIMEOUT:
- /*
- * These two special cases are useful to be comfortable
- * in the caller. Nothing more. We could take
- * MAX_SCHEDULE_TIMEOUT from one of the negative value
- * but I' d like to return a valid offset (>=0) to allow
- * the caller to do everything it want with the retval.
- */
- schedule();
- goto out;
- default:
- /*
- * Another bit of PARANOID. Note that the retval will be
- * 0 since no piece of kernel is supposed to do a check
- * for a negative retval of schedule_timeout() (since it
- * should never happens anyway). You just have the printk()
- * that will tell you if something is gone wrong and where.
- */
- if (timeout < 0) {
- printk(KERN_ERR "schedule_timeout: wrong timeout "
- "value %lx\n", timeout);
- dump_stack();
- current->state = TASK_RUNNING;
- goto out;
- }
- }
- /* 设置超时时间 */
- expire = timeout + jiffies;
- /* 初始化定时器,超时处理函数是 process_timeout,后面再补充说明一下这个函数 */
- setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
- /* 修改定时器,同时会激活定时器 */
- __mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
- /* 将本任务睡眠,调度其他任务 */
- schedule();
- /* 删除定时器,其实就是 del_timer_sync 的宏
- del_singleshot_timer_sync(&timer);
- /* Remove the timer from the object tracker */
- destroy_timer_on_stack(&timer);
- timeout = expire - jiffies;
- out:
- return timeout < 0 ? 0 : timeout;
- }
- EXPORT_SYMBOL(schedule_timeout);
- /*
- * 超时处理函数 process_timeout 里面只有一步操作,唤醒当前任务。
- * process_timeout 的参数其实就是 当前任务的地址
- */
- static void process_timeout(unsigned long __data)
- {
- wake_up_process((struct task_struct *)__data);
- }
- @% U5 n/ C( `" y8 F* T
: A1 V' }# ]+ u4 }8 t
schedule_timeout 一般用于延迟时间较长的程序。 这里的延迟时间较长是对于计算机而言的,其实也就是延迟大于 1 个节拍(jiffies)。 : v% I9 `! B4 {
对于某些极其短暂的延迟,比如只有1ms,甚至1us,1ns的延迟,必须使用特殊的延迟方法。 1s = 1000ms = 1000000us = 1000000000ns (1秒=1000毫秒=1000000微秒=1000000000纳秒) 假设 HZ=100,那么 1个节拍的时间间隔是 1/100秒,大概10ms左右。 所以对于那些极其短暂的延迟,schedule_timeout 函数是无法使用的。 好在内核对于这些短暂,精确的延迟要求也提供了相应的宏。
6 [' F) }' }$ ~ Y- C" m2 H' v- /* 具体实现参见 include/linux/delay.h
- * 以及 arch/x86/include/asm/delay.h
- */
- #define mdelay(n) ...
- #define udelay(n) ...
- #define ndelay(n) ...
; ^7 [9 k! n4 Z# @" n, F* a, L
* P9 p/ F2 m) O, g& H% {6 o: B% e1 m4 @
通过这些宏,可以简单的实现延迟,比如延迟 5ns,只需 ndelay(5); 即可。 1 o' M5 y8 A/ C
这些短延迟的实现原理并不复杂, 首先,内核在启动时就计算出了当前处理器1秒能执行多少次循环,即 loops_per_jiffy (loops_per_jiffy 的计算方法参见 init/main.c 文件中的 calibrate_delay 方法)。 然后算出延迟 5ns 需要循环多少次,执行那么多次空循环即可达到延迟的效果。 5 ^2 v1 q6 H) j. a1 Q9 m
loops_per_jiffy 的值可以在启动信息中看到:
" E1 T; Q- G4 e$ |- [root@vbox ~]# dmesg | grep delay
- Calibrating delay loop (skipped), value calculated using timer frequency.. 6387.58 BogoMIPS (lpj=3193792)0 [! k$ O) Z2 A I) u# j
5 J5 M' w' i2 P# J: o3 C
我的虚拟机中看到 (lpj=3193792)
1 o' \3 d0 A0 C K- w6. 定时器和延迟的例子下面的例子测试了短延迟,自定义定时器以及 schedule_timeout 的使用: 4 g! W$ o. G% i: E( i
- #include <linux/sched.h>
- #include <linux/timer.h>
- #include <linux/jiffies.h>
- #include <asm/param.h>
- #include <linux/delay.h>
- #include "kn_common.h"
- MODULE_LICENSE("Dual BSD/GPL");
- static void test_short_delay(void);
- static void test_delay(void);
- static void test_schedule_timeout(void);
- static void my_delay_function(unsigned long);
- static int testdelay_init(void)
- {
- printk(KERN_ALERT "HZ in current system: %dHz\n", HZ);
- /* test short delay */
- test_short_delay();
- /* test delay */
- test_delay();
- /* test schedule timeout */
- test_schedule_timeout();
- return 0;
- }
- static void testdelay_exit(void)
- {
- printk(KERN_ALERT "*************************\n");
- print_current_time(0);
- printk(KERN_ALERT "testdelay is exited!\n");
- printk(KERN_ALERT "*************************\n");
- }
- static void test_short_delay()
- {
- printk(KERN_ALERT "jiffies [b e f o r e] short delay: %lu", jiffies);
- ndelay(5);
- printk(KERN_ALERT "jiffies [a f t e r] short delay: %lu", jiffies);
- }
- static void test_delay()
- {
- /* 初始化定时器 */
- struct timer_list my_timer;
- init_timer(&my_timer);
- /* 填充定时器 */
- my_timer.expires = jiffies + 1*HZ; /* 2秒后超时函数执行 */
- my_timer.data = jiffies;
- my_timer.function = my_delay_function;
- /* 激活定时器 */
- add_timer(&my_timer);
- }
- static void my_delay_function(unsigned long data)
- {
- printk(KERN_ALERT "This is my delay function start......\n");
- printk(KERN_ALERT "The jiffies when init timer: %lu\n", data);
- printk(KERN_ALERT "The jiffies when timer is running: %lu\n", jiffies);
- printk(KERN_ALERT "This is my delay function end........\n");
- }
- static void test_schedule_timeout()
- {
- printk(KERN_ALERT "This sample start at : %lu", jiffies);
- /* 睡眠2秒 */
- set_current_state(TASK_INTERRUPTIBLE);
- printk(KERN_ALERT "sleep 2s ....\n");
- schedule_timeout(2*HZ);
- printk(KERN_ALERT "This sample end at : %lu", jiffies);
- }
- module_init(testdelay_init);
- module_exit(testdelay_exit);
6 `+ H2 ~7 m; [
6 l9 @$ |0 h" H) l
& }6 }4 E) ^' L# e( e- r$ w, I3 I其中用到的 kn_common.h 和 kn_common.c 参见之前的博客 《Linux内核设计与实现》读书笔记(六)- 内核数据结构 Makefile如下:
6 X- g% D$ b- D; m- # must complile on customize kernel
- obj-m += mydelay.o
- mydelay-objs := testdelay.o kn_common.o
- #generate the path
- CURRENT_PATH:=$(shell pwd)
- #the current kernel version number
- LINUX_KERNEL:=$(shell uname -r)
- #the absolute path
- LINUX_KERNEL_PATH:=/usr/src/kernels/$(LINUX_KERNEL)
- #complie object
- all:
- make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
- rm -RF modules.order Module.symvers .*.cmd *.o *.mod.c .tmp_versions *.unsigned
- #clean
- clean:
- rm -rf modules.order Module.symvers .*.cmd *.o *.mod.c *.ko .tmp_versions *.unsigned
0 ^, e( \) F# T3 z Z) N, W - i8 _7 U" g W, L
1 n R7 m* f$ @" j" v
执行测试命令及查看结果的方法如下:(我的测试系统是 CentOS 6.3 x64) : B8 Z4 x$ K- Q1 O
- [root@vbox chap11]# make
- [root@vbox chap11]# insmod mydelay.ko
- [root@vbox chap11]# rmmod mydelay.ko
- [root@vbox chap11]# dmesg | tail -14
- HZ in current system: 1000Hz
- jiffies [b e f o r e] short delay: 4296079617
- jiffies [a f t e r] short delay: 4296079617
- This sample start at : 4296079619
- sleep 2s ....
- This is my delay function start......
- The jiffies when init timer: 4296079619
- The jiffies when timer is running: 4296080621
- This is my delay function end........
- This sample end at : 4296081622
- *************************
- 2013-5-9 23:7:20
- testdelay is exited!
- *************************& m5 ^5 h2 m+ X/ K7 W' w" w
% Y9 P0 K! Z* ^8 {
- F5 E5 s6 @ G5 Z2 k5 P) k$ f结果说明: 1. 短延迟只延迟了 5ns,所以执行前后的jiffies是一样的。 3 ]# x4 m# O) f% j& h
- jiffies [b e f o r e] short delay: 4296079617
- jiffies [a f t e r] short delay: 4296079617* S- f* p# ~7 s! S. t6 |
& ?* b. A& x8 F7 h1 O, h& r
, g' T7 U1 ^) T' z& n1 f# ]
2. 自定义定时器延迟了1秒后执行自定义函数,由于我的系统 HZ=1000,所以jiffies应该相差1000 " x& M0 T1 J; l# Q
- The jiffies when init timer: 4296079619
- The jiffies when timer is running: 4296080621* i1 ?! }( ^% g0 v8 Y
4 H/ W* G) I4 F) \) l6 ]: A& Z实际上jiffies相差了 1002,多了2个节拍 * s- G% R$ K* m" Z
3. schedule_timeout 延迟了2秒,jiffies应该相差 2000 6 H8 i! u% {8 v$ V+ z) _
- This sample start at : 4296079619
- This sample end at : 4296081622
; y6 Y" D- F; q. N. K4 ?$ W4 P3 s a, ^% L4 A
# H5 Q3 q( M# Z% C3 H$ u
实际上jiffies相差了 2003,多了3个节拍 1 \9 Q' B* I, f8 u
以上结果也说明了定时器的延迟并不是那么精确,差了2,3个节拍其实就是误差2,3毫秒(因为HZ=1000) 如果HZ=100的话,一个节拍是10毫秒,那么定时器的误差可能就发现不了了(误差只有2,3毫秒,没有超多1个节拍)。
. _( z* ]& e2 p; A. F
`/ ?0 d9 o5 c9 g/ P$ D |