[Linux Basics] - Linux Time Management

Time management occupies a very important position in the kernel. Compared with time-driven, a large number of functions in the kernel are based on time-driven. The kernel must manage the running time of the system and the current date and time.

First find out the role of RTC in the Kernel:

Linux system has two clocks: real-time clock and system timer

One, real-time clock

One is the "Real Time Clock" powered by a button battery, also called RTC (Real Time Clock) or CMOS clock, hardware clock. When the operating system is shut down, this is used to record the time, but this time is not used for the running system. When the system starts, the kernel initializes the wall time by reading the RTC, and the changed time is stored in the xtime variable. The so-called wall time is the current actual time .

Second, the system timer

The other time is "System Clock", which is also called kernel clock or software clock or system timer. It is counted by software based on time interrupts. The system timer is the most important part of the kernel time mechanism. It provides a A periodic trigger interrupt mechanism, that is, the system timer uses HZ (clock tick rate) as the frequency to trigger the clock interrupt by itself. When a clock interrupt occurs, the kernel handles it through the clock interrupt handler timer_interrupt().

The system timer is completely managed by the operating system, so it is also called the system clock or the software clock. When the system is started, the kernel initializes the system timer through RTC, and then the system timer is fixed-frequency timing by the operating system chief.

It can be seen that the system time is not a timekeeping clock in the traditional sense, but a special way of timing to express time. The kernel clock does not exist when the system is shut down. Therefore, when the operating system is started, the kernel clock is Yaodu District RTC time for time synchronization. And when the system is shut down, the system time is written back to the RTC for synchronization.

2.1、jiffies

The global variable jiffies is used to record the total number of beats generated by the system startup dependency. It is used to record how many ticks have passed since the system was turned on. Every time a timer interrupt occurs, the jiffies variable will be incremented by one. At startup, the kernel initializes this variable to 0, and thereafter, every time the clock interrupt handler increases the value of this variable. Because the number of clock interruptions in one second is equal to HZ, the value added by jiffies in one second is also HZ, and the system running time is calculated in seconds, which is equal to jiffies/HZ.

  1. Conversion of jiffies to seconds can be calculated using the formula: (jiffies/HZ);
  2. Converting seconds to jiffies can use the formula: (jiffies/HZ) calculation;
  3. Tick ​​is the reciprocal of HZ, which means the time of each interruption of the timer interrupt;
  4. Jiffies is only the relative time relative to the system startup. If you want to obtain absolute time or wall time, you need to use RTC. The kernel uses the variable xtime to record. When the system starts, read RTC and record it in xtime. When the system is halt , The wall time is written back to RTC, and the function do_gettimeofday() is used to read the wall time.

The system timer and its interrupt handler are the core of the kernel management mechanism . The following are some of the work performed by the system timer cycle (the work done by the interrupt handler):

  1. The kernel reads the start-up time and date from the RTC at startup (Linux system time initialization); reads the RTC time by calling rtc_read_time(rtc, &tm); calls do_settimeofday(&tv) to initialize the system time xtime.
  2. The kernel writes the time and date back to the RTC when needed. When the system starts, the kernel initializes the kernel clock by reading the RTC, which is also called wall time, and the changed time is placed in the xtime variable.

When the system sleeps, the CPU must be powered off and the system clock does not work, so the sleep function rtc_suspend of RTC reads the time of RTC and system clock, calculates the difference between them, and then saves it to a static variable. After the system is woken up, read the current RTC time and system time. The system time should be close to 0, because the CPU has just been powered on. The time difference between the originally saved RTC and the system plus the time of the RTC just read is the latest The calculated time is called timekeeping_inject_sleeptime(&sleep_time) through the rtc_resume function to reinitialize the system clock xtime. If the RTC time and the system time are the same, then the difference between them is 0.

But some system hardware RTC time cannot be written, but can only be read, then the difference between them is not zero.

 

 

 

Guess you like

Origin blog.csdn.net/u014674293/article/details/114079951