EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 pulbieup 于 2020-12-24 15:55 编辑 ; _8 v1 X2 Q0 \# y4 r+ [: N6 Q
: f% r* g0 A# [系统中有很多与时间相关的程序(比如定期执行的任务,某一时间执行的任务,推迟一段时间执行的任务),因此,时间的管理对于linux来说非常重要。 % \" Q9 W) E. |3 {% @8 h
主要内容: - 系统时间
- 定时器
- 定时器相关概念
- 定时器执行流程
- 实现程序延迟的方法
- 定时器和延迟的例子" ?# m" i: \8 T- ?7 C6 ^' S, ~$ r
/ ^- P$ Q5 |8 d( L$ a+ T
1. 系统时间系统中管理的时间有2种:实际时间和定时器。 1.1 实际时间实际时间就是现实中钟表上显示的时间,其实内核中并不常用这个时间,主要是用户空间的程序有时需要获取当前时间, 所以内核中也管理着这个时间。 g0 n5 f; l# C' j( P" y1 Y
实际时间的获取是在开机后,内核初始化时从RTC读取的。 内核读取这个时间后就将其放入内核中的 xtime 变量中,并且在系统的运行中不断更新这个值。 注:RTC就是实时时钟的缩写,它是用来存放系统时间的设备。一般和BIOS一样,由主板上的电池供电的,所以即使关机也可将时间保存。
8 `% ~5 i7 W2 R" K7 R实际时间存放的变量 xtime 在文件 kernel/time/timekeeping.c中。 ; o. ]- g$ J8 ~" F9 Y( U
- /* 按照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;( P2 y$ T( D0 W& l0 V& N! h
: }, K; E2 b& n/ g( X% c6 G% }# @6 _0 g/ ?1 o- }. m- _
2 E9 d0 C7 s1 x系统读写 xtime 时用的就是顺序锁。
4 L0 d0 t. g" V L( B: i, q- /* 写入 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));
- /* 省略 。。。。 */
- }8 z& `6 D+ R2 O! z( T
* ` Y& y. D2 K) X$ R% Q K% e5 g# I5 n- m% F3 Q: U" `
上述场景中,写锁必须要优先于读锁(因为 xtime 必须及时更新),而且写锁的使用者很少(一般只有系统定期更新xtime的线程需要持有这个锁)。 这正是 顺序锁的应用场景。 & T- e# ?# k$ [7 j7 \; }4 ?3 N
1.2 定时器定时器是内核中主要使用的时间管理方法,通过定时器,可以有效的调度程序的执行。 动态定时器是内核中使用比较多的定时器,下面重点讨论的也是动态定时器。 5 I2 y1 }% i* C
2. 定时器内核中的定时器有2种,静态定时器和动态定时器。 静态定时器一般执行了一些周期性的固定工作: - 更新系统运行时间
- 更新实际时间
- 在SMP系统上,平衡各个处理器上的运行队列
- 检查当前进程是否用尽了自己的时间片,如果用尽,需要重新调度。
- 更新资源消耗和处理器时间统计值7 |+ r8 {0 h8 U7 y8 E# m* a) x z- e
. B: ]+ N1 _3 f
动态定时器顾名思义,是在需要时(一般是推迟程序执行)动态创建的定时器,使用后销毁(一般都是只用一次)。 一般我们在内核代码中使用的定时器基本都是动态定时器,下面重点讨论动态定时器相关的概念和使用方法。
; e( X+ X* U5 E3 W1 `3. 定时器相关概念定时器的使用中,下面3个概念非常重要: - HZ
- jiffies
- 时间中断处理程序0 e( U& d. M& w3 ?2 q3 t: K
1 ~4 J% n% s; T9 z# B3.1 HZ节拍率(HZ)是时钟中断的频率,表示的一秒内时钟中断的次数。 比如 HZ=100 表示一秒内触发100次时钟中断程序。 ! m' h: E; Y( L7 \# T. T
HZ的值一般与体系结构有关,x86 体系结构一般定义为 100,参考文件 include/asm-generic/param.h HZ值的大小的设置过程其实就是平衡 精度和性能 的过程,并不是HZ值越高越好。 HZ值 | 优势 | 劣势 | | 高HZ | 时钟中断程序运行的更加频繁,依赖时间执行的程序更加精确, , {4 n- o* C; c! g: u/ i
对资源消耗和系统运行时间的统计更加精确。 | 时钟中断执行的频繁,增加系统负担 1 `4 F/ ?% V8 i3 k+ H: e, a
时钟中断占用的CPU时间过多 |
2 U* x* u; c2 f2 e此外,有一点需要注意,内核中使用的HZ可能和用户空间中定义的HZ值不一致,为了避免用户空间取得错误的时间, 内核中也定义了 USER_HZ,即用户空间使用的HZ值。 一般来说,USER_HZ 和 HZ 都是相差整数倍,内核中通过函数 jiffies_to_clock_t 来将内核来将内核中的 jiffies转为 用户空间 jiffies : |/ e* W9 @& @0 j9 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);& U- G+ }7 e: T/ l2 b
' t# K) {4 B7 |: h; S n1 N: i2 \! I- X2 Y. N9 z
3.2 jiffiesjiffies用来记录自系统启动以来产生的总节拍数。比如系统启动了 N 秒,那么 jiffies就为 N×HZ jiffies的相关定义参考头文件 <linux/jiffies.h> include/linux/jiffies.h 4 D' o) Y/ G5 W2 }4 p1 m
- /* 64bit和32bit的jiffies定义如下 */
- extern u64 __jiffy_data jiffies_64;
- extern unsigned long volatile __jiffy_data jiffies;
. d) ?6 x8 `2 V! l 9 e+ ~1 ~ L$ |$ Y7 P+ E
# n* v6 k! O: Q- c9 g6 F& c. ~5 g- Q$ R' y' f, N. H6 T5 [
使用定时器时一般都是以jiffies为单位来延迟程序执行的,比如延迟5个节拍后执行的话,执行时间就是 jiffies+5 32位的jiffies的最大值为 2^32-1,在使用时有可能会出现回绕的问题。 比如下面的代码:
3 u5 @; L2 c; C) i6 B, m- unsigned long timeout = jiffies + HZ/2; /* 设置超时时间为 0.5秒 */
- while (timeout < jiffies)
- {
- /* 还没有超时,继续执行任务 */
- }
- /* 执行超时后的任务 */
$ A( K8 j) X( X7 U
/ |: A% U1 _& t& @
5 z8 P* u$ \2 Z7 t: c& [
. ^( v0 J; C5 C; T b正常情况下,上面的代码没有问题。当jiffies接近最大值的时候,就会出现回绕问题。 由于是unsinged long类型,所以jiffies达到最大值后会变成0然后再逐渐变大,如下图所示:
' K9 e5 `6 y: g2 k, @& K2 c& L所以在上述的循环代码中,会出现如下情况: - 循环中第一次比较时,jiffies = J1,没有超时
- 循环中第二次比较时,jiffies = J2,实际已经超时了,但是由于jiffies超过的最大值后又从0开始,所以J2远远小于timeout
- while循环会执行很长时间(> 2^32-1 个节拍)不会结束,几乎相当于死循环了/ T+ y4 y' z, J' u) g- h {* `
3 M8 R- y* l$ i为了回避回扰的问题,可以使用<linux/jiffies.h>头文件中提供的 time_after,time_before等宏 4 a% Y5 E( Y! D ]8 _* ~8 L
- #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)
, t1 | G4 x; D' c5 p $ v: w( e5 j6 k) W2 m' v
3 ?; z: |$ f: w7 x" c8 c0 _
$ e3 K. ?+ r: f+ L6 G0 @$ x2 x
上述代码的原理其实就是将 unsigned long 类型转换为 long 类型来避免回扰带来的错误, long 类型超过最大值时变化趋势如下:
- J1 B; m) l% Xlong 型的数据的回绕会出现在 2^31-1 变为 -2^32 的时候,如下图所示: - 第一次比较时,jiffies = J1,没有超时
- 第二次比较时,jiffies = J2,一般 J2 是负数 , L% g( O: F6 z) A7 U9 U+ j' }
理论上 (long)timeout - (long)J2 = 正数 - 负数 = 正数(result) & K1 y) d: n; f. V! z
但是,这个正数(result)一般会大于 2^31 - 1,所以long型的result又发生了一次回绕,变成了负数。
2 {5 D; f' _' U5 ?1 O除非timeout和J2之间的间隔 > 2^32 个节拍,result的值才会为正数(注1)。1 _* E. V x4 | o' Z
注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将这些节拍换算成秒就知道了。。。) 5 o" r4 X7 y8 F6 ]1 r& Y
利用time_after宏就可以巧妙的避免回绕带来的超时判断问题,将之前的代码改成如下代码即可:
+ F0 S; g; s- P; l- unsigned long timeout = jiffies + HZ/2; /* 设置超时时间为 0.5秒 */
- while (time_after(jiffies, timeout))
- {
- /* 还没有超时,继续执行任务 */
- }
- /* 执行超时后的任务 */+ o. Y. T3 Y" P7 [) M+ g8 j( ~
, f$ ~- ^' ]8 N" k4 v# ^
' K7 K) M$ Z0 X$ t M1 i3.3 时钟中断处理程序时钟中断处理程序作为系统定时器而注册到内核中,体系结构的不同,可能时钟中断处理程序中处理的内容不同。 但是以下这些基本的工作都会执行: - 获得 xtime_lock 锁,以便对访问 jiffies_64 和墙上时间 xtime 进行保护
- 需要时应答或重新设置系统时钟
- 周期性的使用墙上时间更新实时时钟
- 调用 tick_periodic()
T' p" p3 U6 r; _9 t ( i( y0 y) D, J: I5 Y8 ^! D% P1 n- s
tick_periodic函数位于: kernel/time/tick-common.c 中 . }' R$ R0 R* _! |& w+ u: z
- 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);
- }9 P$ q1 x' }3 q; _
8 i5 Z# ^5 X) s6 z" R( k- |" S C e) [ G4 m
4 a( s; t1 o. V' m
其中最重要的是 do_timer 和 update_process_times 函数。 我了解的步骤进行了简单的注释。 2 [1 Y0 o I- ~% S, u
- 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);
- }
# L+ q! o+ d& T1 @
+ {5 D, [7 h: _! `
7 v& Q8 g! C! D. S- y4. 定时器执行流程这里讨论的定时器执行流程是动态定时器的执行流程。 5 a# ]$ \3 r3 t0 ~" G$ y
4.1 定时器的定义定时器在内核中用一个链表来保存的,链表的每个节点都是一个定时器。 参见头文件 <linux/timer.h>
/ T; Y% V( ?9 G9 O- 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
- };! P( _2 G2 k& D
6 n& s* U) v0 ]* l$ `3 x( C. Y
通过加入条件编译的参数,可以追加一些调试信息。
0 ~/ O6 d( R% l0 ~/ V4.2 定时器的生命周期一个动态定时器的生命周期中,一般会经过下面的几个步骤: 1. 初始化定时器: 7 c9 X' j4 G: Q8 _8 r" L
- struct timer_list my_timer; /* 定义定时器 */
- init_timer(&my_timer); /* 初始化定时器 */* q, I8 J4 K; r+ A( [) R
4 t: v a# G D/ t, l6 {
% T+ t# V6 N2 ?; N: U! l5 L! o) C2. 填充定时器: $ T% b0 d8 `) y+ G
- my_timer.expires = jiffies + delay; /* 定义超时的节拍数 */
- my_timer.data = 0; /* 给定时器函数传入的参数 */
- my_timer.function = my_function; /* 定时器超时时,执行的自定义函数 */
- /* 从定时器结构体中,我们可以看出这个函数的原型应该如下所示: */
- void my_function(unsigned long data);
3 t. _ ^. `+ c* }1 e9 ]
" J8 D; }# x }, k3. 激活定时器和修改定时器: 激活定时器之后才会被触发,否则定时器不会执行。 修改定时器主要是修改定时器的延迟时间,修改定时器后,不管原先定时器有没有被激活,都会处于激活状态。
7 l9 { E/ Z$ u4 u% P填充定时器结构之后,可以只激活定时器,也可以只修改定时器,也可以激活定时器后再修改定时器。 所以填充定时器结构和触发定时器之间的步骤,也就是虚线框中的步骤是不确定的。 7 p- G' T: _6 `; v7 a
- add_timer(&my_timer); /* 激活定时器 */
- mod_timer(&my_timer, jiffies + new_delay); /* 修改定时器,设置新的延迟时间 */
7 \" c* J; G. Q4 z2 S( ]( m 6 r4 s2 o+ _ M& {
, e; m4 g6 M" \5 O
4. 触发定时器: 每次时钟中断处理程序会检查已经激活的定时器是否超时,如果超时就执行定时器结构中的自定义函数。 / K J! \. b1 y
5. 删除定时器: 激活和未被激活的定时器都可以被删除,已经超时的定时器会自动删除,不用特意去删除。
! ~, ~; f K3 B3 a0 I2 L o* ~- /*
- * 删除激活的定时器时,此函数返回1
- * 删除未激活的定时器时,此函数返回0
- */
- del_timer(&my_timer);
1 F$ o" q* I9 F" q3 t5 ~, z7 A
& [- \, Q$ f o2 ?* x: @* c k" [在多核处理器上用 del_timer 函数删除定时器时,可能在删除时正好另一个CPU核上的时钟中断处理程序正在执行这个定时器,于是就形成了竞争条件。 为了避免竞争条件,建议使用 del_timer_sync 函数来删除定时器。 del_timer_sync 函数会等待其他处理器上的定时器处理程序全部结束后,才删除指定的定时器。 ; O# L1 I4 I1 h! T+ v; D) T
- /*
- * 和del_timer 不同,del_timer_sync 不能在中断上下文中执行
- */
- del_timer_sync(&my_timer);* R7 i" z5 U9 g
; R/ Q8 t. D6 u' k- D2 t( l2 W% s0 T- o; s
$ I* m& e! T( o- D( W( `5. 实现程序延迟的方法内核中有个利用定时器实现延迟的函数 schedule_timeout 这个函数会将当前的任务睡眠到指定时间后唤醒,所以等待时不会占用CPU时间。
" c* g8 w# P; X- r+ K1 W5 M& W: t- /* 将任务设置为可中断睡眠状态 */
- set_current_state(TASK_INTERRUPTIBLE);
- /* 小睡一会儿,“s“秒后唤醒 */
- schedule_timeout(s*HZ);+ @3 x+ r9 R8 E
+ E, X9 T# O% S4 i- y7 M* R1 T
/ e2 v0 |- ^/ [. `" B4 U5 y+ L
查看 schedule_timeout 函数的实现方法,可以看出是如何使用定时器的。
7 g5 H+ I9 D$ G- Y) B$ R, y; _$ V. j4 a* c f/ J0 U0 x
- 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);
- }" s3 h( f, }* `3 j
3 ^# w) x5 ?" x$ ]' W" L
schedule_timeout 一般用于延迟时间较长的程序。 这里的延迟时间较长是对于计算机而言的,其实也就是延迟大于 1 个节拍(jiffies)。
% w6 Y9 j$ C$ O& G) {! U对于某些极其短暂的延迟,比如只有1ms,甚至1us,1ns的延迟,必须使用特殊的延迟方法。 1s = 1000ms = 1000000us = 1000000000ns (1秒=1000毫秒=1000000微秒=1000000000纳秒) 假设 HZ=100,那么 1个节拍的时间间隔是 1/100秒,大概10ms左右。 所以对于那些极其短暂的延迟,schedule_timeout 函数是无法使用的。 好在内核对于这些短暂,精确的延迟要求也提供了相应的宏。 2 f. S6 w: z$ I, h2 S3 s4 c
- /* 具体实现参见 include/linux/delay.h
- * 以及 arch/x86/include/asm/delay.h
- */
- #define mdelay(n) ...
- #define udelay(n) ...
- #define ndelay(n) ...
" T$ l! N$ d" E% t4 j- |
" e1 W: e% m; ]) C通过这些宏,可以简单的实现延迟,比如延迟 5ns,只需 ndelay(5); 即可。 " w5 d8 U0 R5 ?$ @
这些短延迟的实现原理并不复杂, 首先,内核在启动时就计算出了当前处理器1秒能执行多少次循环,即 loops_per_jiffy (loops_per_jiffy 的计算方法参见 init/main.c 文件中的 calibrate_delay 方法)。 然后算出延迟 5ns 需要循环多少次,执行那么多次空循环即可达到延迟的效果。 . X4 Y; O' v: @' e) S C7 X/ {
loops_per_jiffy 的值可以在启动信息中看到:
2 s( x3 Q! A+ j/ a9 _- c- [root@vbox ~]# dmesg | grep delay
- Calibrating delay loop (skipped), value calculated using timer frequency.. 6387.58 BogoMIPS (lpj=3193792)5 D* `0 E. W; x1 _! M7 P( z" ~
5 l1 Z5 w0 d/ C3 c0 i9 |% ]
我的虚拟机中看到 (lpj=3193792) 7 G+ ~2 R8 E- z8 m, S4 f
6. 定时器和延迟的例子下面的例子测试了短延迟,自定义定时器以及 schedule_timeout 的使用:
5 g/ B* M9 X! `- #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); u4 h1 \, k$ W# ]
" v! b) z( C5 }! f5 S) P/ h( Q3 y. h# ^; _( y
其中用到的 kn_common.h 和 kn_common.c 参见之前的博客 《Linux内核设计与实现》读书笔记(六)- 内核数据结构 Makefile如下: 3 F, }, l8 X! Q3 J# d+ q" ~+ H
- # 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' K* @; W% j( u7 M
& Q! m- C& u; y9 d
' D, t2 q: j+ d: W& P8 ]执行测试命令及查看结果的方法如下:(我的测试系统是 CentOS 6.3 x64)
5 W) z, B4 K5 r/ R8 K- [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!
- *************************$ g& m3 e2 g) E
3 C! @9 ^. ~1 ?; |" i; q/ Y( e2 O/ w: N3 s' L& L9 A
结果说明: 1. 短延迟只延迟了 5ns,所以执行前后的jiffies是一样的。 # ^7 y+ A% M: I% k" d+ b" X
- jiffies [b e f o r e] short delay: 4296079617
- jiffies [a f t e r] short delay: 4296079617* X7 O/ Y3 s1 Y) m& H2 B" z
q. Q+ ~5 s! G3 `9 U- r
$ m% m% V/ a) h* K2. 自定义定时器延迟了1秒后执行自定义函数,由于我的系统 HZ=1000,所以jiffies应该相差1000 ' \ s8 T" k, Z
- The jiffies when init timer: 4296079619
- The jiffies when timer is running: 4296080621
S7 j$ u) f* W ^
) ^8 m3 a/ s1 I) M% {' \实际上jiffies相差了 1002,多了2个节拍
3 E6 c1 p' L1 Y9 d! W5 R3. schedule_timeout 延迟了2秒,jiffies应该相差 2000
; b: D) d @0 W0 s4 f0 L7 d7 ]- This sample start at : 4296079619
- This sample end at : 4296081622' k4 I# i) `1 ?# R) ?, i* h
f0 u' B- L1 J8 C: U1 {, B+ E
实际上jiffies相差了 2003,多了3个节拍
: o4 c6 X$ u: G( `" ], |* l+ ~( S- y以上结果也说明了定时器的延迟并不是那么精确,差了2,3个节拍其实就是误差2,3毫秒(因为HZ=1000) 如果HZ=100的话,一个节拍是10毫秒,那么定时器的误差可能就发现不了了(误差只有2,3毫秒,没有超多1个节拍)。 . m9 v& ^9 |" a
+ j1 ]0 _) S! j. i J |