Linux clock Processing Mechanism

Source: Internet
Author: User
Tags event timer
Article Title: Linux clock Processing Mechanism. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

In Linux, many activities are related to time, such as process scheduling and network processing. Therefore, understanding the clock processing mechanism in the Linux operating system helps to better understand the operating methods of the Linux operating system. This article analyzes the clock Processing Mechanism of Linux 2.6.25 kernel, first introduces some hardware timers in computer systems, then, it focuses on the hardware clock and Software Clock processing processes in the Linux operating system and the application of software clock. Finally, the full text is summarized.
1. Timer in Computer System

There are many hardware timers in computer systems, such as Real Timer Clock (RTC), Time Stamp Counter (TSC) and Programmable Interval Timer (PIT.

This part of content is not the point of this article. Here is a few simple introductions. For more information, see references:

● Real Timer Clock (RTC ):
Independent from the entire computer system (such as CPU and other chips)
The kernel uses it to obtain the current system time and date.
● Time Stamp Counter (TSC ):
From Pentium, a register TSC is provided to accumulate the clock signals generated by each external oscillator.
Run the rdtsc command to access this register.
TSC provides more accurate time measurement than PIT
● Programmable Interval Timer (PIT ):
Time Measuring Device
The kernel uses a device that generates clock interruptions, and the generated clock interruptions depend on the hardware architecture, which is slow for 10 MS once, fast for 1 MS once
High Precision Event Timer (HPET ):
Compared with the previous timer, HPET provides a higher clock frequency (at least 10 MHz) and a wider counter width (64-bit)
An HPET includes a counter with a fixed frequency value increase and 3 to 32 independent timers, each of which contains a comparator and a register (saving a value, indicates the time when the interruption is triggered ). Each comparator compares the value in the counter with the value in the register. When the two values are equal, an interruption occurs.
2 hardware clock Processing

The hardware clock processing mentioned here refers to the processing process of hardware timer clock interruption.

2.1 Data Structure

There are two main data structures related to the hardware timer (also called the hardware clock in this article, which is different from the Software Clock:

Struct clocksource: Abstraction of hardware devices and description of clock source information
Struct clock_event_device: The event information of the clock, including the operations to be performed when the hardware clock is interrupted (the pointer of the corresponding function is actually saved ). This structure is called as a "clock event device ".
The source code of the two structured kernels contains more detailed annotations, which are located in the clocksource. h and clockchips. h files respectively. Note that event_handler, a member of the clock_event_device structure, specifies the operations that the kernel should perform when the hardware clock is interrupted, that is, the real clock interrupt handler. The "clock initialization" section in Section 2.3 describes which function it actually points.

The Linux kernel maintains two linked lists that store the information of the clock source and the information of the clock event device in the system. The headers of these two linked lists are clocksource_list and clockevent_devices in the kernel. -1 shows the two linked lists.


-1 linked list of clock source and linked list of clock events

 

2.2 notification chain technology (notification chain)

In this part of clock processing, the kernel uses the so-called "notification chain" technology. So before introducing the clock processing process, let's take a look at the "Notification chain" technology.

In the Linux kernel, each subsystem has a strong relationship with each other. events generated or detected by one subsystem may be of interest to another or more subsystems, that is to say, the recipient of this event must be able to notify all subsystems interested in this event, and the notification mechanism must be universal. Based on these, the Linux kernel introduces the "Notification chain" technology.

2.2.1 data structure:

There are four types of notification chains,

1. Atomic notifier chains: The callback function of the notification chain element (the function to be executed when an event occurs) can only be run in the interrupt context and cannot be blocked.
2 Blocking notifier chains: The callback function of the notification Chain Element Runs in the process context, allowing Blocking
3 Raw notifier chains: there are no restrictions on the callback functions of the notification chain element. All locks and protection mechanisms are maintained by the caller.
4 SRCU notification chain (SRCU notifier chains): A variant of blocking notification chains
Therefore, it corresponds to four types of notification chain headers:

● Struct atomic_notifier_head: The chain head of the atomic notification chain.
● Struct blocking_notifier_head: The chain header that can block the notification chain
● Struct raw_notifier_head: The chain header of the original notification chain.
● Struct srcu_notifier_head: The chain Head Of The SRCU notification chain.
Type of the notification link element:

Struct notifier_block: an element in the notification chain. It records the actions that should be performed when a notification is sent (that is, the callback function)
The chain header stores a pointer to the element linked list. The notification chain element structure stores the type and priority of the callback function. For details, see the notifier. h file.

[1] [2] [3] [4] [5] [6] Next page

Related Article

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.