Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Time Management Note arrangement of Linux Kernel device driver

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about the time management notes of the Linux kernel device driver kernel. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

/ * * time management of linux kernel * /

(1) the concept of time in kernel

Time management plays a very important role in the linux kernel.

As opposed to event-driven, a large number of functions in the kernel are based on time-driven.

Some functions are executed periodically, such as refreshing the screen every 10 milliseconds

Some functions are delayed for a certain amount of time, such as the kernel performing a task after 500 milliseconds.

To distinguish:

* absolute time and relative time

* periodically generated events and delayed events

Periodic events are driven by system timers

(2) HZ value

The kernel can only calculate and manage time with the help of hardware timers.

The frequency at which the timer produces interruptions is called tick rate.

A variable HZ is specified in the kernel, which determines the beat rate of the timer when the kernel initializes.

HZ is defined as the current HZ value of 1000 on the i386 platform.

That is, clock interrupts occur 1000 times per second, with a period of 1 millisecond. That is:

# define HZ 1000

Be careful! HZ is not a fixed value, it can be changed and can be entered when the kernel source code is configured.

Different architectures have different HZ values. For example, arm uses 100.

If you want to use the interrupt frequency of the system in the driver, use HZ directly instead of 100 or 1000

a. Ideal HZ value

The HZ value of i386 was kept at 100 until version 2.5, which was changed to 1000.

Increasing the beat rate means that clock interrupts are generated more frequently and interrupt handlers are executed more frequently.

The benefits are:

* Internal approval timer can be run with higher frequency and higher accuracy

* system calls that rely on timers, such as poll () and select (), run with higher precision

* improve the accuracy of process preemption

(the scheduling delay is shortened. If the process still has 2ms time slices, the process will run more 8ms under the scheduling cycle of 10ms.

Due to the delay of preemption, it will have an impact on some time-critical tasks)

The disadvantages are:

* the higher the beat rate, the heavier the burden on the system.

Interrupt handlers will take up more processor time.

(3) jiffies

The global variable jiffies is used to record the total number of beats generated since the system was started.

At startup, jiffies is initialized to 0, and each time the clock interrupt handler increases the value of this variable.

In this way, the running time after the system starts is jiffies/HZ seconds.

Jiffies is defined in:

Extern unsigned long volatile jiffies

The jiffies variable is always unsigned long.

Therefore, it is 32-bit on 32-bit architecture and 64-bit on 64-bit architecture. For 32-bit jiffies, if HZ is 1000, it will overflow after 49.7 days. Although overflows are not common, the program can still cause errors due to rewinding when it detects timeouts. Linux provides four macros to compare beat counts, which can handle beat count wrapping correctly.

# include # define time_after (unknown, known) / / unknow > known#define time_before (unknown, known) / / unknow

< known#define time_after_eq(unknown, known) // unknow >

= known#define time_before_eq (unknown, known) / / unknow

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report