Design and Implementation of Linux kernel Reading Notes (9)-timer and time management (1)

Source: Internet
Author: User

Timer and time management

A system timer is a programmable hardware chip that can interrupt at a fixed frequency. The interrupt is called a timer interrupt, and its corresponding Interrupt ProcessingProgramUpdates the system time and executes tasks that require periodic operation. The system timer and clock interrupt handler are the centers in the Linux system kernel management mechanism.

Another focus is on the dynamic timer, a tool used to delay the execution of programs. For example, if the floppy drive is not active for a certain period of time, the floppy drive uses a dynamic timer to close the floppy drive. The kernel can dynamically create or destroy dynamic timers.

 

1. Time Concept in the kernel

The system timer triggers the clock interruption at a certain frequency. The frequency can be scheduled by programming, called the tick rate ). The interval between two consecutive clock interruptions is called a tick, which is equal to one second of the beat rate.

 

2. Beat rate Hz

The system timer frequency (Cycle rate) is defined through static preprocessing, that is, Hz. The hardware is set according to the Hz value when the system starts. The system structure is different, and the Hz value is also different. The arm is 100Hz, and the i386 is 1000Hz.

Increasing the beat rate means that the clock interruption is generated more frequently, so the interrupt processing program will be executed more frequently, which will bring the following benefits to the entire system:

1) The kernel timer can run at a higher frequency and accuracy.

2) system calls that depend on scheduled values, such as poll and select, can run with higher precision.

3) more precise resolutions are available for measurements such as resource consumption and system running time.

4) Improve the process preemption accuracy.

Increasing the beat rate also has a negative effect: the higher the beat rate, the higher the clock interruption frequency, and the heavier the system burden.

 

3. jiffies

The global variable jiffies is used to record the total number of beats generated since the system was started. At startup, the kernel initializes the variable to 0. After that, the value of this variable is added for each clock interruption handler. Because the number of clock interruptions in one second is equal to Hz, the system running time is equivalent to jiffies/Hz in seconds.

The jiffies variable is always unsigned long, so the 32-bit architecture is 32-bit and the 64-bit architecture is 64-bit, when the value of jiffies exceeds its maximum storage range, it will overflow and its value will go back to 0.

The kernel provides four macros to help compare the cycle count, which can correctly handle the cycle count loop. These macros are defined in the file <Linux/jiffies. h>:

# DefineTime_after (unknown, known) (long) (known)-(long) (unknown) <0)# DefineTime_before (unknown, known) (long) (unknown)-(long) (known) <0)# DefineTime_after_eq (unknown, known) (long) (unknown)-(long) (known)> = 0)# DefineTime_before_eq (unknown, known) (long) (known)-(long) (unknown)> = 0)

The unknown parameter is usually jiffies, and the known parameter is the value to be compared.

 

4. Hard clock and Timer

Real-time clock (RTC) is a device used to persistently store system time. Even after the system is turned off, it can also rely on the power supply provided by the micro-battery on the motherboard to maintain the system time. When the system starts, the kernel initializes the wall time by reading RTC, which is stored in the xtime variable.

The system timer is the most important role in the kernel timing mechanism. It provides a periodic interrupt trigger mechanism.

 

5. clock interrupt handling program

The clock interrupt handler can be divided into two parts: the architecture and the architecture. The System-related routine is registered to the kernel as the interrupt handler of the system timer, so that it can run properly when a clock interruption occurs. Although the specific work of the processing program depends on the specific architecture, the vast majority of the processing programs should do the following at a minimum:

1) obtain the xtime_lock to protect the access to jiffies_64 and the wall time xtime.

2) respond or reset the system clock when necessary.

3) periodically update the real-time clock using the wall time.

4) call an architecture-independent clock routine: do_timer ().

The interrupted service program calls the architecture-independent routine do_timer to execute the following tasks:

1) Add 1 to the jiffies_64 variable (this operation is safe even in the 32-bit architecture because the xtime_lock lock has been obtained before ).

2) update the statistical value of resource consumption, such as the system time and user time consumed by the current process.

3) execute an expired dynamic timer.

4) execute the scheduler_tick function.

5) Update the wall time, which is stored in the xtime variable.

6) calculate the average load value.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.