Implementation method for performance optimization of high-precision timer of operating system in hard real-time scene
By splitting and managing the timeout timeout of the hardware timer, the interrupt interference problem of high-precision timers in hard real-time scenarios is solved, and the timing accuracy of 1us is achieved, which is suitable for industrial automation control and aerospace fields.
Patent Information
- Application Number
- CN202510383152.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-28
- Publication Date
- 2025-07-18
AI Technical Summary
The prior art is difficult to implement a 1us-level high-precision timer in hard real-time scenarios, especially in complex systems, which are susceptible to interrupt interference, resulting in a reduction in timing accuracy.
By encapsulating the hardware timer driver, the expected timeout time is split to set the compensation time, actual timeout time, and busy time, and the hardware timer issues interrupts in advance and is busy in the discriminating callback function, etc., to ensure that the timeout callback function is called at the expected timeout time to avoid the impact of interrupts.
Maintain the stability and accuracy of high-precision timers in complex systems, ensure accurate triggering of single-time and periodic timers, and improve the response speed and operation stability of hard real-time systems.
Smart Images

Figure CN120335992A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer application technologies, and more specifically, to a method for optimizing the performance of a high-precision timer in an operating system in a hard real-time scenario. Background Art
[0002] With the rapid development of modern society, the performance requirements for devices and systems in many industries are becoming increasingly stringent. In industrial scenarios such as industrial control, autonomous driving, and communication systems, there are many tasks with extremely strict time requirements, which are usually referred to as real-time tasks. Real-time tasks require the system to respond within a specific time range to ensure the correctness and safety of functions. According to the severity of the consequences of real-time task timeout execution or return, real-time tasks can be further divided into hard real-time tasks and soft real-time tasks. Hard real-time tasks have the strictest time requirements and must be completed within the specified time. If the task times out, it may lead to serious consequences such as system crashes, equipment damage, or even casualties. Although soft real-time tasks also require completion within a specific time, they do not cause catastrophic consequences and usually only affect system performance or user experience.
[0003] In the process of meeting high real-time requirements, high-precision timers are one of the key guarantees. To ensure that real-time tasks are completed on time, the system usually relies on mechanisms such as low-latency interrupt handling, real-time scheduling algorithms, load control, and resource management. Timers, especially high-precision timers, are important means to ensure that real-time tasks are accurately processed within the specified time. In fields such as industrial control, autonomous driving, and communication systems, many tasks have extremely high requirements for time accuracy, and these tasks rely on timer interfaces with various precisions provided in the operating system. Among them, high-precision timers are the core components to ensure the timely execution of time-critical tasks. Timers in a real-time operating system can be divided into hardware timers and software timers. Hardware timers are implemented based on hardware and provide timing functions by the chip itself. Generally, an external crystal oscillator provides the input clock to the chip, and the chip provides a set of configuration registers to the software module, accepts control inputs, and after reaching the set time value, the chip interrupt controller generates a time interrupt, with high precision, up to the nanosecond level; software timers are a type of system interface provided by the operating system, implemented through software calculations, and built on top of hardware timers, enabling the system to provide timer services that are not restricted by hardware timer resources. Currently, the implementation of software timers usually based on systick in the system can achieve precision control at the millisecond level. And high-precision timers usually directly call hardware timers to implement. Summary of the Invention
[0004] The present invention aims to overcome the defects of the above-mentioned prior art and provide a method for optimizing the performance of a high-precision timer in an operating system under a hard real-time scenario. This method can provide a method for implementing a high-precision timer in a scenario with extremely high timing accuracy requirements, achieving a timing accuracy at the 1us level.
[0005] The technical solution adopted by the present invention is a method for optimizing the performance of a high-precision timer in an operating system under a hard real-time scenario, which is usually implemented based on a real-time operating system, but is also applicable in the hard real-time scenario of a general operating system. The object of the present invention is achieved by at least one of the following technical solutions.
[0006] A method for optimizing the performance of a high-precision timer in an operating system under a hard real-time scenario, comprising the following steps: encapsulating this method in the hardware timer driver of the deployment platform; initializing the hardware timer driver of this method; calling the high-precision timer interface of the present invention and passing in information such as the timing duration and callback function; calculating the actual timeout moment of the high-precision timer according to the information initialized by the driver and setting the hardware timer; after the hardware timer times out, calling the discrimination callback function of this method; in the discrimination callback function, busy-wait until the busy-wait duration exceeds the busy-wait protection duration. If the busy-wait duration exceeds the busy-wait protection duration, return an error code; when the busy-wait duration reaches the expected timeout moment, call the passed-in timeout callback function.
[0007] Further, in step S1, the operating system of the deployment platform is usually a real-time operating system, which is commonly found in scenarios with hard real-time requirements. The hardware timer is used to receive a stable clock signal source and count. When it increments to a specified value or decrements to 0, it issues an interrupt to prompt the CPU that this timer has timed out and needs to be immediately responded to.
[0008] The hardware timer driver encapsulates the method of the present invention for different types of hardware timers. After encapsulation, users can call the high-precision timer implemented by this method in the same way as a conventional timer. The method splits the expected timeout duration Le into three parts: the set compensation duration L1, the actual timeout duration La, and the busy-wait duration L2. The expected timeout duration is equal to the sum of the set compensation duration, the actual timeout duration, and the busy-wait duration. The formula is:
[0009] Le = L1 + La + L2
[0010] The hardware timer will call the discrimination callback function earlier than the expected timeout moment, close the core interrupt in advance, and prevent other interrupts from affecting the response of the high-precision timer interrupt. After the expected timeout moment is reached, the discrimination callback function will call the passed-in timeout callback function. The set compensation duration is the duration required for the system to set the hardware timer. Due to the need to perform operations such as writing to the hardware timer control register, disabling and enabling interrupts, the actual effective time of the hardware timer will be later than the calling time, and there is a setting delay. The theoretical value of the set compensation duration is equal to the setting delay of the hardware timer, which is used to compensate for the setting delay of the hardware timer. The actual timeout duration is the timeout duration written into the hardware timer control register. The hardware timer issues a timeout interrupt based on the actual timeout duration, prompting the CPU that the hardware timer has timed out; the value of the actual timeout duration La is equal to the difference between the expected timeout duration Le and the set compensation duration L1 and the busy waiting duration L2, and the formula is:
[0011] La=Le-L1-L2
[0012] The busy waiting time is the time for waking up the timer in advance. There are many interrupt-off operations in the system, and when the system is in the interrupt-off state, it cannot respond to any interrupt. When the hardware timer times out and issues an interrupt, if the system is in the interrupt-off state, it cannot respond to the interrupt request of the hardware timer in time, causing interrupt delay processing and reducing the timer precision. Similarly, in a system that does not support nested interrupts, the system is in the interrupt state and cannot respond to the interrupt request of the hardware timer in time, which then causes the timer to delay calling the callback function and reduce precision. Therefore, the purpose of busy waiting is to make the hardware timer issue a timer interrupt slightly earlier than the expected timeout moment, so as to ensure that when the expected timeout moment arrives, the hardware timer interrupt service routine can hold the CPU and handle the relevant interrupts in time.
[0013] Further, it is also necessary to initialize the driver program of the hardware timer. During the initialization process, it is necessary to measure and then set or set the default value for the set duration and the default busy waiting duration of the hardware timer in the current environment. A reference measurement method is to set the hardware timer with a timeout duration of x n times using the default timer scheme, count the call delay duration of its timeout callback function, and take the average delay duration as the estimated value of the hardware timer set duration. The busy waiting duration can be measured when initializing the hardware timer driver program, or can be measured additionally and then set as the default value. The value of the busy waiting duration should be slightly larger than the maximum delay duration of the hardware timer in the system. When initializing the driver, a reference rough measurement method for the busy waiting duration is that after the set compensation duration measurement is completed, apply it to the driver, and then set the hardware timer with a timeout duration of y n2 times, count the call delay duration of its timeout callback function, and take the maximum value as the busy waiting duration. The busy waiting duration can also be measured through long-term experiments additionally and then set the result as the default value.
[0014] Further, the high-precision timer interface in step S3 is an interface re-encapsulated in the hardware timer driver according to the method of the present invention, and further adds two processes of setting compensation and busy waiting on the basis of the original hardware timer driver interface. Before calling this method, it is necessary to initialize the hardware timer driver and set the set compensation duration and the busy waiting duration using measured values or default values. The passed-in callback function is the function that should be called after reaching the expected timeout moment.
[0015] Further, the driver initialization information is the set compensation duration and the busy waiting duration that have been set. The actual timeout duration is obtained by subtracting the set compensation duration and then subtracting the busy waiting duration from the expected timeout duration. The expected timeout moment is calculated in the high-precision timer interface. After entering the interface function, first access the system counter to obtain the current moment value, and obtain the expected timeout moment by adding the current moment and the expected timeout duration. The system counter is usually globally unique and shared, and is broadcast to all cores. This counter has a high time accuracy, up to the nanosecond level. In the present invention, the moment obtained by accessing the system counter is used as an estimate of the real moment.
[0016] Further, the discrimination callback function is an interrupt service routine that responds to the hardware timer interrupt. When the hardware timer reaches the actual timeout duration, it will send an interrupt request to the CPU through the interrupt controller. After the CPU receives the interrupt request, it searches the interrupt vector table according to the interrupt number and calls the corresponding interrupt service routine, that is, the discrimination callback function registered in the interrupt vector table during the initialization of this method.
[0017] Further, the main process of the discrimination callback function is to obtain the system clock and determine whether the expected timeout moment has been reached. If not, obtain the system clock again and continue the determination. When the busy waiting duration exceeds the busy waiting protection duration, an error code is returned; when the expected timeout moment is reached, the passed-in timeout callback function is called.
[0018] Further, the busy waiting protection duration is a configurable value, which can adopt a default value or a set value passed in by the user, and is used to prevent indefinite busy waiting caused by unexpected situations. When the busy waiting duration exceeds the busy waiting protection duration, the busy waiting state is exited and a busy waiting timeout error code is returned.
[0019] Further, the timeout callback function is a callback function that needs to be called after the expected timeout moment is reached. The timeout callback function is called using a synchronous call method to avoid the influence of other interrupt services and reduce the overhead of synchronous operations, task scheduling, context switching, etc. In a multi-core system, according to the hardware timer interrupt bound to the slave core, the sacrificed computing power and real-time performance of this solution are further reduced.
[0020] The present invention also provides an embodiment, a computer storage medium, storing a computer program, and when the computer program is executed by a processor, it implements the method for optimizing the performance of the high-precision timer of the operating system in any one of the hard real-time scenarios.
[0021] Beneficial effects
[0022] The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario provided by the present invention. In a complex system operating environment, the stability and accuracy of the high-precision timer are crucial. The present invention has outstanding advantages in many aspects and provides a strong guarantee for the efficient operation of the system. During the operation of the system, long-term disabling of interrupt operations often has an adverse impact on the high-precision timer. However, the present invention, with its unique design idea, skillfully avoids this problem. In a system that does not support interrupt nesting, other interrupts are very likely to interfere with the normal operation of the high-precision timer, resulting in timing deviation. But through innovative technical means, the present invention successfully avoids such interference, ensuring that the high-precision timer can operate stably under various complex system conditions and maintain extremely high timing accuracy. Whether it is a single-shot timer scenario or a periodic timer scenario, the present invention can be perfectly adapted. In the application of the single-shot timer, it can accurately trigger a single timing operation to meet the need for precise control of a specific moment. In the application scenario of the periodic timer, it can continuously work at a stable periodic frequency, providing a continuous and accurate time reference for the system. In a hard real-time scenario, the high-precision timer provided by the present invention demonstrates unparalleled precision advantages. In hard real-time systems with strict requirements for time accuracy, such as industrial automation control, aerospace and other fields, the present invention can provide more precise time control, greatly improving the response speed and operation stability of the system, reducing potential risks caused by timing errors, and providing reliable technical support for the efficient and safe operation of various industries. Description of the Drawings
[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on the structures shown in these drawings.
[0024] Figure 1 It is the working step diagram of the embodiment of the present invention;
[0025] Figure 2 It is the execution flow chart of the embodiment of the present invention;
[0026] Figure 3 It is the step schematic diagram of the embodiment of the present invention;
[0027] Figure 4 It is the time splitting schematic diagram of the periodic mode timer of the embodiment of the present invention. Detailed Embodiments
[0028] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without making creative efforts shall fall within the protection scope of the present invention.
[0029] It should be noted that if there are directional indications (such as up, down, left, right, front, back...) involved in the embodiments of the present invention, then the directional indications are only used to explain the relative positional relationship and movement conditions between components in a specific posture. If the specific posture changes, the directional indications will also change accordingly.
[0030] In addition, if there are descriptions involving "first", "second", etc. in the embodiments of the present invention, then the descriptions of "first", "second", etc. are only for descriptive purposes and cannot be understood as indicating or implying their relative importance or implicitly indicating the quantity of the indicated technical features. Thus, the features defined with "first" and "second" may explicitly or implicitly include at least one such feature. In addition, if "and / or" or "and / or" appears throughout the text, its meaning includes three parallel solutions. Taking "A and / or B" as an example, it includes solution A, solution B, or a solution where A and B are satisfied simultaneously. In addition, the technical solutions between various embodiments can be combined with each other, but it must be based on the ability of those of ordinary skill in the art to implement. When the combination of technical solutions results in contradictions or cannot be implemented, it should be considered that such a combination of technical solutions does not exist and is not within the protection scope required by the present invention.
[0031] Embodiment 1
[0032] Reference Figure 1 and Figure 3 , specifically, this embodiment provides a method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario, including the following steps: encapsulating this method in the hardware timer driver of the deployment platform; initializing the hardware timer driver of this method; calling the high-precision timer interface of the present invention and passing in information such as the timing duration and callback function; calculating the actual timeout moment of the high-precision timer and setting the hardware timer according to the information initialized by the driver; after the hardware timer times out, calling the discrimination callback function of this method; in the discrimination callback function, busy wait until the busy wait duration exceeds the busy wait protection duration. If the busy wait duration exceeds the busy wait protection duration, return an error code; when the busy wait duration reaches the expected timeout moment, call the passed-in timeout callback function.
[0033] Furthermore, the operating system of the deployment platform is UniProton and Chuanglong RK3588 development board. The hardware timer is used to receive a stable clock signal source and count. When it increases to a specified value or decreases to 0, it prompts the CPU that the hardware timer has timed out and needs to respond immediately. The hardware timer driver encapsulates the method of the present invention for the hardware timer on the development board. After encapsulation, the user can call the high-precision timer implemented by this method in the same way as a conventional timer.
[0034] The method divides the expected timeout duration Le into three parts: a set compensation duration L1, an actual timeout duration La, and a busy waiting duration L2. The expected timeout duration is equal to the sum of the set compensation duration, the actual timeout duration, and the busy waiting duration. The formula is:
[0035] Le=L1+La+L2
[0036] The set compensation time L1 is the time required for the system to set the hardware timer. Since the hardware timer control register write operation, interrupt disable and enable operations, etc. are required, the actual effective time of the hardware timer will be later than the call time, and there is a setting delay. The theoretical value of the set compensation time is equal to the setting delay of the hardware timer, which is used to compensate for the setting delay of the hardware timer.
[0037] The actual timeout duration is the timeout duration written into the hardware timer control register. The hardware timer issues a timeout interrupt according to the actual timeout duration, prompting the CPU that the hardware timer has timed out. The value of the actual timeout duration La is equal to the difference between the expected timeout duration Le and the set compensation duration L1 and the busy waiting duration L2, and the formula is:
[0038] La=Le-L1-L2
[0039] The busy waiting time is the time for waking up the timer in advance. There are many interrupt-off operations in the system, and when the system is in the interrupt-off state, it cannot respond to any interrupt. When the hardware timer times out and issues an interrupt, if the system is in the interrupt-off state, it cannot respond to the interrupt request of the hardware timer in time, causing interrupt delay processing and reducing the timer precision. Similarly, in a system that does not support nested interrupts, the system is in the interrupt state and cannot respond to the interrupt request of the hardware timer in time, which then causes the timer to delay calling the callback function and reduce precision. Therefore, the purpose of busy waiting is to make the hardware timer issue a timer interrupt slightly earlier than the expected timeout moment, so as to ensure that when the expected timeout moment arrives, the hardware timer interrupt service routine can hold the CPU and handle the relevant interrupts in time.
[0040] Further, in step S2, the driver program of the hardware timer needs to be initialized. During the initialization process, it is necessary to measure and then set or set the default value for the set duration and the default busy waiting duration of the hardware timer in the current environment. In this embodiment, during initialization, the set duration of the hardware timer is measured. The hardware timer with a timeout duration of 1 ms for 1000 times is set using the default timer scheme, and the call delay duration of its timeout callback function is statistically analyzed. The average delay duration is taken as the estimated value of the set duration of the hardware timer. Using the above method, the set compensation duration of the hardware timer in this embodiment is measured to be 6 us.
[0041] The busy waiting duration can be measured when initializing the hardware timer driver program, or can be measured additionally and then set as the default value. The value of the busy waiting duration should be slightly larger than the maximum delay duration of the hardware timer in the system. In this embodiment, the result of an additional long-term test is used as the set value of the busy waiting time. After 7,200,000 single-timer experiments of 1 ms, the maximum call delay duration is measured to be 5 us. Therefore, the busy waiting time is set to 5.1 us.
[0042] Further, the high-precision timer interface is an interface re-encapsulated in the hardware timer driver according to the method of the present invention, and two processes of setting compensation and busy waiting are further added on the basis of the original hardware timer driver interface. Before calling this method, it is necessary to initialize the hardware timer driver and set the set compensation duration and the busy waiting duration using the measured value or the default value. The incoming callback function is the function that should be called after the expected timeout moment is reached.
[0043] Further, the driver initialization information is the set compensation duration and the busy waiting duration that have been set. The actual timeout duration is obtained by subtracting the set compensation duration and then subtracting the busy waiting duration from the expected timeout duration. In this embodiment, when the expected timing duration is 1000 us, the actual timing duration is 1000 us - 6 us - 5.1 us = 988.9 us.
[0044] The expected timeout moment is calculated in the high-precision timer interface. After entering the interface function, first access the system counter to obtain the current moment value, and the expected timeout moment is obtained by adding the current moment and the expected timeout duration.
[0045] The system counter is usually globally unique and shared, and is broadcast to all cores. This counter has a high time accuracy, up to the nanosecond level. In the present invention, the moment obtained by accessing the system counter is used as an estimate of the real moment.
[0046] Further, the discrimination callback function is an interrupt service routine in response to the hardware timer interrupt. When the hardware timer reaches the actual timeout duration, an interrupt request will be sent to the CPU through the interrupt controller. After receiving the interrupt request, the CPU searches the interrupt vector table according to the interrupt number and calls the corresponding interrupt service routine, which is the discrimination callback function registered in the interrupt vector table during the initialization of this method. In this embodiment, the interrupt number is 322.
[0047] Further, the main process of the discrimination callback function is to obtain the system clock and determine whether the expected timeout moment has been reached. If not, the system clock is obtained again and the determination continues. When the busy waiting duration exceeds the busy waiting protection duration, an error code is returned; when the expected timeout moment is reached, the passed-in timeout callback function is called.
[0048] Further, the busy waiting protection duration is a configurable value, which can adopt a default value or a set value passed in by the user, and is used to prevent indefinite busy waiting caused by unexpected situations. When the busy waiting duration exceeds the busy waiting protection duration, the busy waiting state is exited and a busy waiting timeout error code is returned. In this embodiment, the busy waiting protection duration is set to 20 us. When the busy waiting duration exceeds 20 us, the busy waiting state is exited and a busy waiting timeout error code -1 is returned.
[0049] Further, the timeout callback function is a callback function that needs to be called after the expected timeout moment is reached. The timeout callback function is called by using a synchronous call method to avoid the influence of other interrupt services and reduce the overhead of synchronous operations, task scheduling, context switching, etc.
[0050] Embodiment 2
[0051] Reference Figure 2 and Figure 4 This embodiment provides a method for optimizing the performance of the high-precision timer of the operating system in the hard real-time scenario of the periodic mode in the UniProton real-time operating system, which runs on the Chuanglong RK3588 development board and can be used for periodic timing tasks in the hard real-time scenario.
[0052] There are significant differences between the timer in the periodic mode and the timer in the single-shot mode. The main difference is that the periodic mode timer only needs to be set once to trigger periodically, so only the first setting of the timer needs to be compensated. In this embodiment, when the hardware timer is set to the periodic mode, after it reaches the timeout moment, not only an interrupt request will be sent, but also the hardware timer will be automatically reloaded without repeated setting.
[0053] Since the periodic mode timer will automatically load the timing period, if Figure 1Setting the hardware timer in the early wake-up mode will cause the compensation time set by the hardware to be advanced by one cycle for each cycle because there is only a hardware setup delay in the first cycle, and the error will accumulate continuously, resulting in timing errors.
[0054] In this embodiment, the general implementation scheme is similar to that of Embodiment 1. The main difference is that in this embodiment, Figure 1 the early wake-up setting scheme is changed to Figure 2 the scheme of delaying the target timeout moment. Essentially, it still makes the timeout time of the hardware timer earlier than the expected timeout time, and then holds the CPU for busy waiting. The difference is that Figure 1 the scheme in Figure 2 achieves the goal by shortening the timer wake-up duration, while the
[0055] scheme in this embodiment achieves the goal by delaying the target wake-up moment.
[0056] As Figure 4 shown, record the timer setting moment as Ts, the timer effective moment as Tx, and the t-th timeout moment of the timer as the t-th target timeout moment as
[0057] In this embodiment, the determination method of the first target timeout moment is the timer effective moment Tx plus the timer setting delay duration L1 plus the busy waiting duration L2. The formula is:
[0058]
[0059] In this embodiment, the actual timer setting delay duration is equal to the setting compensation duration, which is 6 us, and the busy waiting duration is the same as that in Embodiment 1, which is 5.1 us. The value of each subsequent target timeout moment is the value of the previous timeout moment plus the expected timeout duration; the formula is:
[0060]
[0061] After implementing according to the above implementation scheme, the hardware timer will be woken up about one busy waiting duration before the target timeout moment each time, so as to achieve the effect of closing the interrupt and holding the CPU for busy waiting. This scheme can greatly improve the timer accuracy by sacrificing a small amount of performance and the real-time performance of other interrupts.
[0062] Embodiment 3
[0063] The present invention also provides an embodiment, a computer storage medium storing a computer program, and when the computer program is executed by a processor, it implements the method for optimizing the performance of the high-precision timer of the operating system in any of the above-mentioned hard real-time scenarios.
[0064] Those of ordinary skill in the art can realize that the units and algorithm steps of each example described in combination with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of this application.
[0065] Those skilled in the art can clearly understand that for the convenience and simplicity of description, the specific working processes of the systems, devices, and units described above can refer to the corresponding processes in the foregoing method embodiments and will not be elaborated herein. Those skilled in the art can also clearly understand that each embodiment of the present application has different emphases. For the convenience and simplicity of description, the same or similar parts may not be elaborated in different embodiments. Therefore, the parts not described or not detailedly described in a certain embodiment can be referred to the descriptions of other embodiments.
[0066] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions described in the embodiments of the present application are generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium or transmitted through the computer-readable storage medium. The computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center in a wired manner (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wirelessly (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (for example, a floppy disk, a hard disk, a magnetic tape), an optical medium (for example, a digital versatile disc (DVD)), or a semiconductor medium (for example, a solid state disk (SSD)), etc.
[0067] In the intricate system operating environment, the stability and accuracy of high-precision timers play a decisive role in the reliable operation of the system. The method for optimizing the performance of high-precision timers in the operating system under hard real-time scenarios proposed by the present invention, with many remarkable advantages, has become a powerful boost to promote the efficient operation of the system. During the operation of the system, long-term interrupt disabling operations often have a negative impact on high-precision timers. However, the present invention ingeniously bypasses this problem with its unique design concept. In systems that do not support interrupt nesting, other interrupts are very likely to interfere with the normal operation of high-precision timers, thereby causing timing errors. But the present invention successfully resists such interference through innovative technical measures, ensuring that high-precision timers can operate smoothly under various complex system conditions and always maintain extremely high timing accuracy. Whether it is the application scenario of single-shot timers or the application scenario of periodic timers, the present invention can perfectly fit. In the actual application of single-shot timers, it can accurately trigger single-shot timing operations, fully meeting the need for precise control of specific moments. In the application scenario of periodic timers, it can continuously operate at a stable periodic frequency, providing a persistent and accurate time reference for the system. In hard real-time scenarios with extremely strict requirements for time accuracy, the method for optimizing the performance of high-precision timers in the operating system under hard real-time scenarios provided by the present invention can achieve more precise time control in hard real-time system fields such as industrial automation control and aerospace, which have extremely high requirements for time accuracy, greatly improving the response speed of the system, enhancing operation stability, and effectively reducing potential risks caused by timing errors, providing a solid and reliable technical guarantee for the efficient and safe operation of various industries.
[0068] The above are only the preferred embodiments of the present invention, and do not limit the patent scope of the present invention. Any equivalent structural transformation made by using the description and drawings of the present invention under the inventive concept of the present invention, or direct / indirect application in other related technical fields, is included in the patent protection scope of the present invention.
Claims
1. A method for optimizing the performance of a high-precision timer in an operating system under a hard real-time scenario, characterized in that, Including: Encapsulate this method in the hardware timer driver of the deployment platform; Initialize the hardware timer driver of this method; Call the high-precision timer interface of the present invention and pass in the data information of the timing duration and the callback function; Calculate the actual timeout moment of the high-precision timer and set the hardware timer according to the information initialized by the driver; After the hardware timer times out, call the discrimination callback function of this method; In the discrimination callback function, busy wait until the busy wait duration exceeds the busy wait protection duration. If the busy wait duration exceeds the busy wait protection duration, return an error code; if the busy wait reaches the expected timeout moment, call the passed-in timeout callback function.
2. The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario according to claim 1, characterized in that Also including: Split the expected timeout duration Le into three parts: the set compensation duration L1, the actual timeout duration La, and the busy wait duration L2. The expected timeout duration is equal to the sum of the set compensation duration, the actual timeout duration, and the busy wait duration. The formula is: Le = L1 + La + L2.
3. The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario according to claim 2, wherein, It is necessary to initialize the driver program of the hardware timer; During the initialization process, it is necessary to measure and then set or use the default value to set the set duration of the hardware timer and the default busy wait duration in the current environment. Set the hardware timer with the timeout duration of x for n times using the default timer scheme, and count the call delay duration of its timeout callback function. Take its average delay duration as the estimated value of the set duration of the hardware timer; When initializing the driver, after the set compensation duration is measured, apply it to the driver. Then set the hardware timer with the timeout duration of y for n2 times, and count the call delay duration of its timeout callback function. Take its maximum value as the busy wait duration.
4. The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario according to claim 3, wherein Initialize the hardware timer driver, set the set compensation duration and the busy wait duration using the measured value and / or the default value. The high-precision timer interface includes an interface re-encapsulated in the hardware timer driver, and further adds two processes of set compensation and busy wait on the basis of the original hardware timer driver interface.
5. The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario according to claim 4, characterized in that, The initialized information of the driver is the set compensation duration and the busy wait duration that have been set; the actual timeout duration is obtained by subtracting the set compensation duration and then subtracting the busy wait duration from the expected timeout duration; The expected timeout moment is calculated in the high-precision timer interface; after entering the interface function, first access the system counter to obtain the current moment value, and obtain the expected timeout moment by adding the expected timeout duration to the current moment.
6. The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario according to claim 5, wherein The discrimination callback function is an interrupt service routine that responds to the hardware timer interrupt; when the hardware timer reaches the actual timeout duration, an interrupt request is sent to the CPU through the interrupt controller; after the CPU receives the interrupt request, it searches the interrupt vector table according to the interrupt number and calls the corresponding interrupt service routine.
7. The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario according to claim 6, characterized in that, The main process of the discrimination callback function is to obtain the system clock and judge whether the expected timeout moment has been reached; if not, obtain the system clock again and continue to judge; when the busy wait duration exceeds the busy wait protection duration, return an error code; when the expected timeout moment is reached, call the passed-in timeout callback function.
8. The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario according to claim 7, wherein The busy-wait protection duration is a configurable value, which adopts a default value or a set value passed in by the user, and is used to prevent an indefinite busy-wait duration caused by unexpected situations; when the busy-wait duration exceeds the busy-wait protection duration, the busy-wait state is exited and a busy-wait timeout error code is returned.
9. The method for optimizing the performance of the high-precision timer of the operating system in a hard real-time scenario according to claim 8, wherein The timeout callback function is a callback function that needs to be called after the expected timeout moment; the timeout callback function is called by means of synchronous call.
10. A computer-readable medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the method for optimizing the performance of the high-precision timer of the operating system in the hard real-time scenario as described in claim 1.
Citation Information
Patent Citations
Method for enhancing computer inner core timer control precision
CN101299193A
Method for reducing jittering in POWERLINK SoC frame synchronization based on MCU
CN103812635A
Method for improving efficiency of embedded timer
CN110333938A
Method and device for timer control
CN113767674A
Timing task implementation method and device based on linux system
CN115658156A