search
HomeSystem TutorialLINUXRelevant introduction and operation framework analysis of high-precision timers

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
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
How does performance differ between Linux and Windows for various tasks?How does performance differ between Linux and Windows for various tasks?May 14, 2025 am 12:03 AM

Linux performs well in servers and development environments, while Windows performs better in desktop and gaming. 1) Linux's file system performs well when dealing with large numbers of small files. 2) Linux performs excellently in high concurrency and high throughput network scenarios. 3) Linux memory management has more advantages in server environments. 4) Linux is efficient when executing command line and script tasks, while Windows performs better on graphical interfaces and multimedia applications.

How to Create GUI Applications In Linux Using PyGObjectHow to Create GUI Applications In Linux Using PyGObjectMay 13, 2025 am 11:09 AM

Creating graphical user interface (GUI) applications is a fantastic way to bring your ideas to life and make your programs more user-friendly. PyGObject is a Python library that allows developers to create GUI applications on Linux desktops using the

How to Install LAMP Stack with PhpMyAdmin in Arch LinuxHow to Install LAMP Stack with PhpMyAdmin in Arch LinuxMay 13, 2025 am 11:01 AM

Arch Linux provides a flexible cutting-edge system environment and is a powerfully suited solution for developing web applications on small non-critical systems because is a completely open source and provides the latest up-to-date releases on kernel

How to Install LEMP (Nginx, PHP, MariaDB) on Arch LinuxHow to Install LEMP (Nginx, PHP, MariaDB) on Arch LinuxMay 13, 2025 am 10:43 AM

Due to its Rolling Release model which embraces cutting-edge software Arch Linux was not designed and developed to run as a server to provide reliable network services because it requires extra time for maintenance, constant upgrades, and sensible fi

12 Must-Have Linux Console [Terminal] File Managers12 Must-Have Linux Console [Terminal] File ManagersMay 13, 2025 am 10:14 AM

Linux console file managers can be very helpful in day-to-day tasks, when managing files on a local machine, or when connected to a remote one. The visual console representation of the directory helps us quickly perform file/folder operations and sav

qBittorrent: A Powerful Open-Source BitTorrent ClientqBittorrent: A Powerful Open-Source BitTorrent ClientMay 13, 2025 am 10:12 AM

qBittorrent is a popular open-source BitTorrent client that allows users to download and share files over the internet. The latest version, qBittorrent 5.0, was released recently and comes packed with new features and improvements. This article will

Setup Nginx Virtual Hosts, phpMyAdmin, and SSL on Arch LinuxSetup Nginx Virtual Hosts, phpMyAdmin, and SSL on Arch LinuxMay 13, 2025 am 10:03 AM

The previous Arch Linux LEMP article just covered basic stuff, from installing network services (Nginx, PHP, MySQL, and PhpMyAdmin) and configuring minimal security required for MySQL server and PhpMyadmin. This topic is strictly related to the forme

Zenity: Building GTK  Dialogs in Shell ScriptsZenity: Building GTK Dialogs in Shell ScriptsMay 13, 2025 am 09:38 AM

Zenity is a tool that allows you to create graphical dialog boxes in Linux using the command line. It uses GTK , a toolkit for creating graphical user interfaces (GUIs), making it easy to add visual elements to your scripts. Zenity can be extremely u

See all articles

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!