Several important terms or variables related to time in Linux are described below: Hz, tick, and jiffies.
(1) Hz
The Linux kernel sends timer interrupt (IRQ 0) Every fixed period. Hz is used to define the number of times timer interrupts is generated every second. For example, if Hz is 1000, there are 1000 timer interrupts requests per second. Hz can be set when the core is compiled. For details about the query setting method, see the previous article "How long has it been running in Linux". Hz can be set to 100, 250, 300, or 1000. The default value of core version is 250. Experiment:
Observe the number of timer interruptions of/proc/interrupt, and observe its value again after one second. Theoretically, the difference between the two is about 250.
Adrian @ Adrian-desktop :~ $ CAT/proc/interrupts | grep timer & sleep 1 & CAT/proc/interrupts | grep Timer
0: 9309306 io-APIC-edge Timer
0: 9309562 io-APIC-edge Timer
Supplementary results: the above results are run on a single CPU. I did not get the expected results on my own dual-core computer. I doubt whether the dual-core computer experiment is suitable.
(2) tick
Tick is the reciprocal of Hz, that is, the time of every interruption of timer interrupt. For example, if Hz is 250, tick is 4 ms (millisecond ).
(3) global variable xtime
Xtime is a timeval Data Structure Variable. First, we need to look at the timeval structure.
Struct timeval
{
Time_t TV _sec;/*** second ***/
Susecond_t TV _usec;/*** microsecond ***/
}
1 second = 1000 milliseconds (3 zero), 1 second = 1000 000 microseconds (6 zero), 1 second = 1000 000 000 nanoseconds (9 zero ), 1 second = 1000 000 000 000 second (12 zeros ). The second is represented by S, the millisecond is represented by MS, And the microsecond is represented by us. The second is represented by ns, and the second is represented by PS. Their classification unit is thousands, that is, three zeros each time. Therefore, microsecond indicates us.
Xtime is the time obtained from the CMOS circuit, generally from a historical time point to the present time, that is, to obtain the date displayed on the operating system. This is the so-called "Real-Time Clock", and its accuracy is microseconds.
(4) jiffies
In <Linux/jiffies. h>, defines jiffies as the Linux core variable (32-bit meta variable, unsigned long). It is used to record how many tick the system has since it started up, in the Linux kernel, jiffies is far more important than xtime. Every time timer interrupt occurs, the jiffies variable is added. The conversion between jiffies and absolute time is achieved by using two macros: jiffies_to_ns () and ns_to_jiffies ().
The hardware provides a system timer for the kernel to calculate and manage the time. It is worth noting that jiffies is not initialized to zero when the system is started, it is set to-300 * Hz (kernel/time. c), that is, after the system is started for five minutes, jiffies will overflow. What should I do with the overflow? In fact, the Linux core defines several macro (timer_after (unknown, known), time_after_eq (unknown, known), time_before (unknown, known), and time_before_eq (unknown, known )), even if it is an overflow, jiffies content can be correctly obtained through these macro.
Jiffies can be used to set timeout, for example:
Unsigned long timeout = jiffies + tick_rate * 2; // timeout after 2 seconds, for example, tick_rate = 250. After 500 tick interruptions, 2 seconds will pass.
If (time_before (jiffies, timeout)
{
// No timeout
}
Else
{
// Timeout
}
In addition, the 80x86 Architecture defines a jiffies-related variable jiffies_64, which is a 64-bit variable. It may take several millions of years for this variable to overflow. Therefore, it should be difficult to wait until the overflow occurs. Then how can we obtain jiffies information through jiffies_64? In fact, jiffies is mapped to the lowest 32-bit jiffies_64. Therefore, jiffies_64 can be used to obtain jiffies without the problem of overflow.
(5) In addition to the system timer jiffies, there is also a time-related clock: Real-time clock (RTC), which is a hardware clock used to persistently store the system time, the system shuts down and depends on the micro-battery on the motherboard to maintain timing. When the system starts, the kernel initializes the wall time by reading RTC and stores it in the xtime variable. This is the main function of RTC.
Http://blog.csdn.net/bdc995/archive/2009/05/03/4144031.aspx ()
Http://blog.csdn.net/linweig/archive/2010/03/04/5341231.aspx ()