Home System Tutorial LINUX Relevant introduction and operation framework analysis of high-precision timers

Relevant introduction and operation framework analysis of high-precision timers

Jul 20, 2024 am 08:36 AM

Relevant introduction and operation framework analysis of high-precision timers

The sudden mention of high-precision timers makes me feel confused. At least beginners will be frustrated. And if you understand it literally, it is very simple. If the timer is less precise, then there will be no more. Although that's pretty much it, it just involves some other details.

If a worker wants to do his job well, he must first sharpen his tools. Before we start talking, let’s sharpen his tools first:

2 Several related source code files and their paths are as follows:

Hrtimers.txt(linux-3.2.12documentationtimers)

Hrtimer.c(linux-3.2.12kernel)

Hrtimer.h(linux-3.2.12includelinux)

2 Simply operate the high-precision timer in high-precision timer mode. The entire operation framework is as follows:

Initialize hrtimer_init, set relevant data through the hetimer structure, such as timing duration, etc.-> Turn on the timer hrtimer_start-> Run the wide-precision timer hrtimer_run_queues-> Trigger the interrupt, call the interrupt rebound function, hrtimer_interrupt-> Remove High-precision timer remove_hrtimer.

Readers now have a framework in their heads, and the specific driver details will be discussed one by one below.

Let’s give an overview first. There may be some awkward things in it that are difficult to understand. It doesn’t matter. There will be relevant codes and examples to explain it.

? High-precision timer sorts on a black red tree according to time.

? They are independent of the period clock linux application timer, using pulse width timestamps instead of jiffies time dimensions.

First take out the documentation related to high-precision timers in the Linux code and take a look at its introduction. I will explain it later, the document path: Hrtimers.txt (linux-3.2.12documentationtimers)

The content of the document... To be honest, the document is a bit long, but the maintenance of Linux documents is not very high. I found a few sentences from the above and translated them, and then explained them:

?Thispatchintroducesanewsubsystemforhigh-resolutionkerneltimers. The phrase patch in this sentence is a bit interesting. It means that high-precision timers are installed into the system as a patch package. There was no such concept before 2.6.16.

? The second point, the English is too long so I won’t post it. It’s why we need to use a high-precision timer. Since every system has a timer, the accuracy is actually not high. In comparison, it is called a low-precision timer. . To put it bluntly, high precision is required.

?Third point, another feature of high-precision timer is that its framework is in the kernel when compiling, and if the high-precision timer is not configured, such a high-precision timer is based on an ordinary timer. run.

?Last point, the high-precision timer is implemented using the black mangrove algorithm, while the ordinary timer is implemented using the time round robin algorithm.

?In addition, the document also explains many issues such as clock source, data structure, red-black tree, etc. These issues are discussed separately below.

应用定时器程序-1PLC_linux 应用定时器_应用定时器2

1. Related data structures

The data structure involved in the high frame rate timer, we consider from the following aspects:

About the source: How does this clock come from? A structure is defined in hrtimer.h, the code is as follows:

structhrtimer_clock_base{

structhrtimer_cpu_base*cpu_base;

intindex; //Attributes used to distinguish clocks (there are two types in total, which will be mentioned below)

clockid_tclockid;//The ID of the clock supported by each CPU

structtimerqueue_headactive;//The black and red branch node of the timer being enabled

ktime_tresolution;//Frame rate of clock, milliseconds

ktime_t(*get_time)(void);//Used to restore the current clock

ktime_tsoftirq_time;//The time to run the wide precision timer queue in soft interrupt

ktime_toffset;//Change the amount of offset of the timer clock

};

Regarding the previous elements, please explain some things.

The high frame rate timer can be based on two clocks (clockbase): one is a monotonic clock (CLOCK_MONOTONIC), which starts from 0 when the system starts; the other clock (CLOCK_REALTIME) represents the system real time. In the structure structhrtimer_clock_base above, the index element is used to distinguish whether it is a CLOCK_MONOTONIC or CLOCK_REALTIME clock. For each CPU of the system, a data structure containing these two clock bases is provided. Each total clock base has a black red tree to sort all pending high-precision timerslinux application timer , and each CPU provides two clock bases (monotonic clock and real time), and all timers are sorted on the black red tree by expiration time. If the timer has expired but its handler rebound function has not yet been executed, then Migrate from black mangrove to an array. When adjusting the real-time clock, there will be an error between the expiration time value of the timer stored in the CLOCK_REALTIME clock and the current actual time. The offset array helps correct these situations. It represents the amount of offset that the timer needs to calibrate. Because this is only a temporary effect and rarely occurs.

Before understanding the clock source, we may also need to know a structure structhrtimer, the code is as follows:

structhrtimer{

structtimerqueue_nodenode;//The timer queue node also manages node.expires. The absolute expiration time of the high-precision timer is in its internal algorithm. This time is related to the clock the timer is based on (the two time bases mentioned above) ).

应用定时器2_linux 应用定时器_应用定时器程序-1PLC

ktime_t_softexpires;//The absolute earliest expiration time

enumhrtimer_restart(*)(structhrtimer*);//Timer expiration rebound function

structhrtimer_clock_base*base;//Hand pointing to the time base (per CPU, per clock)

unsignedlongstate;//Status information, used to see the bit value

#ifdefCONFIG_TIMER_STATS

intstart_pid; //The pid of the task that starts timing stored in the timer statistics area

void*start_site;//Timer stores the start value of the current timing

charstart_comm[16];//The storage process of starting the timing of the timer statistics area name

#endif

};

For the above structure, users only need to care about three points. The first is the array, which is the rebound function after the timer expires. The second is expires, which represents the expiration time. The third is the last sentence. The use of the high-precision timer structure must be initialized by the hrtimer_init() function. The hrtimer_init() function belongs to the application socket, so it is mentioned above. There is another problem here, which is also the core problem of high-precision timers, which is the application of black mangrove in high-precision timers. In fact, it is a bit early to talk about this now, and let readers have a certain understanding first. Traditional timing of Linux The timer is implemented through the time wheel algorithm (timer.c), but the hrtimer is implemented through the black mangrove algorithm. There is a node field on structhrtimer, the type is structtimerqueue_node. This field represents the position of hrtimer in the black red tree. Note that the source code I refer to is version 3.2.12. In version 2.6.X, the format of this field is structrb_node. Let me say hello to the readers first. There is such a thing. When we use it in detail, we will talk about how it is implemented.

The two important structures are finished. Because they need to be compatible with multi-core processors, they will involve the time base of each CPU. The structure structhrtimer_cpu_base is used to define the clock of each CPU. Currently, each CPU only corresponds to monotonic Clock and real-time clock, the structure is as follows:

structhrtimer_cpu_base{//Single CPU time base structure

raw_spinlock_tlock;//Lock related time base and timer, carrier lock

unsignedlongactive_bases;//Mark the base bit array with active timer

#ifdefCONFIG_HIGH_RES_TIMERS

ktime_texpires_next;//The absolute time of the next time that is about to expire

inthres_active; //High frame rate mode status, Boolean variable

应用定时器2_linux 应用定时器_应用定时器程序-1PLC

inthang_detected;//The latest detected pending high-precision timer interrupt

unsignedlongnr_events;//Total amount of high-precision timer interrupts

unsignedlongnr_retries;//Total amount of high-precision timer interrupt retries

unsignedlongnr_hangs;//Total amount of high-precision timer interrupt hangs

ktime_tmax_hang_time;//The maximum time for the high-precision timer interrupt to trigger

#endif

structhrtimer_clock_baseclock_base[HRTIMER_MAX_CLOCK_BASES]; //This CPU time base needle

};

The three structures inside should be the most basic, defining functions and elements related to high-precision timers, and each CPU has a complete set of defined structures, and then initializes hrtimers.

Now that we have finished talking about the basic structure, let’s start talking about the API socket.

The first is to configure and initialize the API of hrtimers. When we talked about structhrtimer at the beginning, we mentioned that to use structhrtimer, we need to initialize it first. The function declaration code is as follows:

voidhrtimer_init(structhrtimer*timer,clockid_tclock_id,

enumhrtimer_modemode)//Given clock initialization timer

debug_init(timer,clock_id,mode);

__hrtimer_init(timer,clock_id,mode);

The above function implements the initialization of a high-precision timer. The following is an explanation of the relevant elements:

/**

*hrtimer_init – Initialize timer with given clock

应用定时器2_应用定时器程序-1PLC_linux 应用定时器

*@timer: The timer that is about to be initialized

*@clock_id: The clock that will be used

*@mode: timer mode abs/rel

*/

mode can use five constants, as follows:

enumhrtimer_mode{

HRTIMER_MODE_ABS=0x0,/*Time is absolute*/

HRTIMER_MODE_REL=0x1,/*Time is relative*/

HRTIMER_MODE_PINNED=0x02,/*The timer is bound to the CPU*/

HRTIMER_MODE_ABS_PINNED=0x02,

HRTIMER_MODE_REL_PINNED=0x03,

};

The hrtimer_init() function calls the __hrtimer_init() function. The following is the prototype of this function:

staticvoid__hrtimer_init(structhrtimer*timer,clockid_tclock_id,enumhrtimer_modemode)

structhrtimer_cpu_base*cpu_base;

intbase;

memsettimer,0,sizeof(structhrtimer));

cpu_base=&__raw_get_cpu_var(hrtimer_bases);

应用定时器程序-1PLC_应用定时器2_linux 应用定时器

if(clock_id==CLOCK_REALTIME&&mode!=HRTIMER_MODE_ABS)

clock_id=CLOCK_MONOTONIC;

base=hrtimer_clockid_to_base(clock_id);

timer->base=&cpu_base->clock_base[base];

timerqueue_init(&timer->node);

#ifdefCONFIG_TIMER_STATS

タイマー->start_site=NULL;

タイマー->start_pid=-1;

memset(timer->start_comm,0,TASK_COMM_LEN);

#endif

__hrtimer_init() 関数は、structhrtimer_cpu_base 構造体を呼び出して CPU を初期化し、me​​mset() 関数を使用します。プロトタイプは次のとおりです。

void*memset(void*s,intc,size_tn)

インティ;

char*ss=s;

for(i=0;i ss[i]=c;

返品;

この関数はメモリの内容をクリアし、初期化を完了します、memset(timer,0,sizeof(structhrtimer))。

ここで注意してください、それは依然として上記の問題です。私が使用したソースコードは 3.2.12 であり、2.6.X で提供されるソースコードには 2 つの定数しかありません。

The above is the detailed content of Relevant introduction and operation framework analysis of high-precision timers. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Does the internet run on Linux? Does the internet run on Linux? Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations? What are Linux operations? Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

What is the salary of Linux administrator? What is the salary of Linux administrator? Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

What are the main tasks of a Linux system administrator? What are the main tasks of a Linux system administrator? Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

What are the differences in virtualization support between Linux and Windows? What are the differences in virtualization support between Linux and Windows? Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

Is it hard to learn Linux? Is it hard to learn Linux? Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps? The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps? Apr 25, 2025 am 09:10 AM

For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

See all articles