2024年10月Linux全局变量jiffies的用法

发布时间:

  ⑴jiffies是Linux系统中的全局变量,与时间有关,那么jiffies变量具体有哪些作用呢?下面小编就给大家介绍下Linux全局变量jiffies的用法,感兴趣的朋友不妨来了解下吧。

  ⑵系统运行时间以秒为单位,等于jiffies/Hz。

  ⑶注意,jiffies类型为无符号长整型(unsigned long,其他任何类型存放它都不正确。

  ⑷将以秒为单位的时间转化为jiffies:

  ⑸seconds * Hz

  ⑹将jiffies转化为以秒为单位的时间:

  ⑺jiffies / Hz

  ⑻相比之下,内核中将秒转换为jiffies用的多些。

  ⑼jiffies的内部表示

  ⑽jiffies定义于文件中:

  ⑾* The -bit value is not atomic - you MUST NOT read it

  ⑿* without sampling the sequence number in xtime_lock.

  ⒀* get_jiffies_( will do this for you as appropriate.

  ⒁extern u __jiffy_data jiffies_;

  ⒂extern unsigned long volatile __jiffy_data jiffies;

  ⒃ld(脚本用于连接主内核映像(在x上位于arch/i/kernel/vmlinux.lds.S中,然后用jiffies_变量的初值覆盖jiffies变量。因此jiffies取整个jiffies_变量的低位。

  ⒄访问jiffies的代码只会读取jiffies_的低位,通过get_jiffies_(函数就可以读取整个位的值。在位体系结构上,jiffies_和jiffies指的是同一个变量。

  ⒅#if (BITS_PER_LONG 《

  ⒆u get_jiffies_(void;

  ⒇static inline u get_jiffies_(void

  ⒈return (ujiffies;

  ⒉#if (BITS_PER_LONG 《

  ⒊u get_jiffies_(void

  ⒋unsigned long seq;

  ⒌seq = read_seqbegin(&xtime_lock;

  ⒍ret = jiffies_;

  ⒎} while (read_seqretry(&xtime_lock, seq;

  ⒏return ret;

  ⒐jiffies的回绕wrap around

  ⒑当jiffies的值超过它的最大存放范围后就会发生溢出。对于位无符号长整型,最大取值为(^-,即。如果节拍计数达到了最大值后还要继续增加,它的值就会回绕到。

  ⒒内核提供了四个宏来帮助比较节拍计数,它们能正确的处理节拍计数回绕的问题:

  ⒓* These inlines deal with timer wrapping correctly. You are

  ⒔* strongly encouraged to use them

  ⒕* . Because people otherwise fet

  ⒖* . Because if the timer wrap changes in future you won‘t have to

  ⒗* alter your driver code.

  ⒘* time_after(a,b returns true if the time a is after time b.

  ⒙* Do this with “《” and “》=” to only test the sign of the result. A

  ⒚* good piler would generate better code (and a really good piler

  ⒛* wouldn’t care。 G is currently neither.

  ①#define time_after(a,b /

  ②(typecheck(unsigned long, a && /

  ③typecheck(unsigned long, b && /

  ④((long(b - (long(a 《

  ⑤#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 》=

  ⑩#define time_before_eq(a,b time_after_eq(b,a

  Ⅰ/* Same as above, but does so with platform independent bit types.

  Ⅱ* These must be used when utilizing jiffies_ (i.e. return value of

  Ⅲ* get_jiffies_( */

  Ⅳ#define time_after(a,b /

  Ⅴ(typecheck(__u, a && /

  Ⅵtypecheck(__u, b && /

  Ⅶ((__s(b - (__s(a 《

  Ⅷ#define time_before(a,b time_after(b,a

  Ⅸ#define time_after_eq(a,b /

  Ⅹ(typecheck(__u, a && /

  ㈠typecheck(__u, b && /

  ㈡((__s(a - (__s(b 》=

  ㈢#define time_before_eq(a,b time_after_eq(b,a