Usage of Linux global variable jiffies

  
                

jiffies are global variables in Linux systems, which are related to time. What is the specific role of jiffies variables? The following small series will introduce you to the usage of the Linux global variable jiffies, interested friends may wish to understand.

system uptime in seconds, equal jiffies /Hz.

Note that the jiffies type is unsigned long, and it is not correct for any other type.

Convert time in seconds to jiffies:

seconds * Hz

Convert jiffies to time in seconds:

jiffies /Hz

In contrast, the kernel converts seconds to jiffies for more.

Internal representation of jiffies

jiffies are defined in the file:

/*

* The 64-bit value is not atomic - you MUST NOT read It

* without sampling the sequence number in xtime_lock.

* get_jiffies_64() will do this for you as appropriate.

*/

extern u64 __jiffy_data jiffies_64;

extern unsigned long volatile __jiffy_data jiffies;

The ld(1) script is used to connect to the main kernel image (in arch/i386/kernel/vmlinux.lds.S on x86) ), then overwrite the jiffies variable with the initial value of the jiffies_64 variable. So jiffies take the lower 32 bits of the entire jiffies_64 variable.

The code to access jiffies will only read the lower 32 bits of jiffies_64, and the entire 64-bit value can be read via the get_jiffies_64() function. On a 64-bit architecture, jiffies_64 and jiffies refer to the same variable.

#if (BITS_PER_LONG "64)

u64 get_jiffies_64(void);

#else

static inline u64 get_jiffies_64(void)

{

return (u64)jiffies;

}

#endif

in

#if (BITS_PER_LONG "64 )

u64 get_jiffies_64(void)

{

unsigned long seq;

u64 ret;

do {

seq = read_seqbegin(&xtime_lock);

ret = jiffies_64;

} while (read_seqretry(&xtime_lock, seq));

return ret;< Br>

}

jiffies wrap around

An overflow occurs when the value of jiffies exceeds its maximum storage range. For 32-bit unsigned long integers, the maximum value is (2^32)-1, which is 429496795. If the beat count continues to increase after reaching the maximum value, its value will wrap around to zero.

The kernel provides four macros to help compare beat counts, which correctly handle the problem of beat count rewind:

/*

* These inlines deal with timer wrapping Correctly. You are

* strongly encouraged to use them

* 1. Because people otherwise forget

* 2. 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 “"0” and “"=0” to only test the sign of the result. A

* good compiler would generate better code (and a Really good compiler

* wouldn’t care). Gcc is currently neither.

*/

#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)

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

* These must be used when utilizing jiffies_64 (ie return value of

* get_jiffies_64() */

#define time_after64(a,b) /

(typecheck(__u64, a) && /

typecheck(__u64 , b) && /

((__s64)(b) - (__s64)(a) 0))

#define time_before64(a,b) time_after64(b,a)< Br>

#define time_after_eq64(a,b) /

(typecheck(__u64, a) && /

typecheck(__u64, b) && /< Br>

((__s64)(a) - (__s64)(b) 》= 0))

#define time_before_eq64(a,b) time_after_eq64(b,a)

User space and HZ
Previous12Next page Total 2 pages

Copyright © Windows knowledge All Rights Reserved