RTOS Timer Interrupt Handling System, Device, Method, Electronic Device and Readable Storage Medium
By calculating the average switching time of tasks in the RTOS timer queue, optimizing task management and hardware timer configuration, the problem of reduced throughput caused by frequent task switching in RTOS is solved, and the stability and response speed of the system is improved. It is suitable for industrial control, military equipment, aerospace and other fields.
Patent Information
- Application Number
- CN202410500577.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-24
- Publication Date
- 2025-07-11
- Estimated Expiration
- 2044-04-24
AI Technical Summary
In existing RTOS timer queues, frequent task switching leads to reduced system throughput, especially in time-required application areas such as industrial control, military equipment, and aerospace, which can lead to serious consequences.
By calculating the average switching time of tasks, optimizing timer queue management, reducing invalid task switching, using intelligent management strategies to free and reorder one-shot and periodic tasks, and accurately setting the preinstalled value of hardware timer overflow to improve system response speed.
It reduces frequent context switching caused by high-priority tasks preempt CPU, improves system throughput and performance, ensures effective utilization of resources and timely execution of tasks, and is suitable for real-time applications that require precise time control and fast response.
Smart Images

Figure CN118445038B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of real-time operating systems, and particularly to an RTOS timer interrupt processing system, device, method, electronic device, and readable storage medium. Background Art
[0002] A real-time operating system (RTOS) is an operating system specifically designed to handle real-time tasks, characterized by its ability to respond quickly to external events or data and schedule all resources to complete real-time tasks. The RTOS includes a real-time task scheduler that allocates CPU time slices according to the priorities of tasks. High-precision timers play a crucial role in the RTOS, and they usually require hardware timers independent of the system TICK to achieve precise time control. These hardware timers are typically organized into a queue and sorted according to their timeout lengths to facilitate the effective management and triggering by the RTOS.
[0003] In existing RTOSs, the process of creating and inserting a new timer into the timer queue includes: creating a new timer and inserting it into the ordered timer queue (usually a priority queue or a heap) according to its timeout length. If the newly inserted timer has the shortest timeout length, the entire queue is traversed and the remaining time of each timer is recalculated. Subsequently, the overflow preload value of the hardware timer is reset to the timeout length of the newly inserted timer, the interrupt of the hardware timer is enabled, and the hardware timer is started. When the hardware timer reaches its overflow preload value, it triggers an interrupt to notify the RTOS that the timer has expired. The RTOS processes the expired timer in the interrupt service and recalculates the remaining timeout times of all timers in the queue as needed.
[0004] A major problem with the existing technology is that when the timing durations of the current several timers in the timer queue are less than the task switching time, the system may experience frequent task switching, resulting in a reduced throughput. Specifically, when the RTOS attempts to schedule a task (such as task X) at a specific time point a, if another task (such as task Y) expires at a later time point b but has a higher priority, then the system spends time at time point a to switch the context to task X, but then task Y preempts the CPU, causing the context switch at time point a to become invalid. Such frequent context switches not only waste CPU time but also reduce the overall performance and throughput of the system. Especially in time-critical application fields such as industrial control, military equipment, and aerospace, such performance degradation may lead to serious consequences. Therefore, a more optimized method is needed to handle this situation to reduce unnecessary task switching and improve the throughput of the system. Summary of the Invention
[0005] To overcome the deficiencies of the prior art, the present invention provides an RTOS timer interrupt processing system, device, method, electronic device, and readable storage medium to solve the technical problem in the prior art that when the timing duration of an RTOS timer task is less than the task switching time, frequent task switching will occur, reducing the system throughput rate.
[0006] To solve the above technical problem, a technical solution adopted by the present invention is: to provide an RTOS timer interrupt processing device, including: an average task switching time calculation unit, configured to calculate the average task switching time by pre-statistically averaging the time-consuming of multiple task context switches; an interrupt service management unit, configured to start interrupt service when the timer interrupt is triggered; a timer task management and calculation unit, configured to recalculate the remaining timeout of all timers according to the timeout of the head task in the timer task list; traverse the timer task list to determine whether a task expires within the average task switching time; and cancel the suspended state of the timer task that expires and is within the average task switching time and enter the ready state; a hardware timer configuration unit, configured to configure and restart the hardware timer, and set the timer overflow preload value according to the timer task timeout; the interrupt service management unit is further configured to clean up resources and exit the interrupt service process when the interrupt service processing is completed.
[0007] Among them, the timer task management and calculation unit is further configured to determine whether the expired task is a one-shot or periodic task: for an expired task that is a one-shot task, delete the task node and release resources; for an expired task that is a periodic task, reset the remaining timeout and move it to an appropriate position in the task queue.
[0008] Among them, the hardware timer configuration unit is configured to: set the timer at the head of the timer task list as the first task that expires after the average task switching time, and reset the hardware timer overflow preload value to the timeout of the timer; and restart the hardware timer to continue monitoring the timer timeout situation.
[0009] Among them, the average task switching time calculation unit is configured to: calculate the time of each task switch \(t_{switch}=t2 - t1\); where \(t1\) is the starting point with the time point when the current task is switched out as the starting point, and \(t2\) is the ending point with the time point when switching to another task context and starting to execute as the ending point; save the most recent previous \(n\) task switching times, and eliminate the oldest task switching time; and calculate the average task switching time according to the saved task switching times.
[0010] To solve the above technical problems, another technical solution adopted by the present invention is: to provide an RTOS timer interrupt processing system, including a processor and a timer provided in the processor; and further including the interrupt processing device as described above.
[0011] To solve the above technical problems, another technical solution adopted by the present invention is: to provide an RTOS timer interrupt processing method, the method including: starting an interrupt service when a timer interrupt is triggered; recalculating the remaining timeout of all timers according to the timeout of the head task in the timer task list; traversing the timer task list from the head to determine whether the task expires within the average task switching time; wherein, the average task switching time is obtained by pre-statistically averaging the time-consuming of multiple task context switches; for the timer task that expires within the average task switching time, cancel the suspended state of the corresponding task to make it enter the ready state; set the timer at the head of the timer task list to the first task that expires after the average task switching time, and reset the hardware timer overflow preload value to the timeout of this timer; restart the hardware timer to continue monitoring the timeout of the timer; and after the interrupt service is completed, exit the processing flow.
[0012] Wherein, before canceling the suspended state of the timer task that expires within the average task switching time to make it enter the ready state, the method further includes: for the timer task that expires within the average task switching time, determining whether the expired task is a one-shot or periodic task; for the expired task being a one-shot task, directly deleting the task node and releasing relevant resources; for the expired task being a periodic task, resetting the remaining timeout of the task node to the period time and moving it to a suitable position in the task queue according to the timeout length.
[0013] Wherein, the average task switching time is obtained by pre-statistically averaging the time-consuming of multiple task context switches, specifically including: calculating the time of each task switch t_switch = t2 - t1; wherein, t1 is the starting point with the time point when the current task is switched out as the starting point, and t2 is the ending point with the time point when switching to another task context and starting to execute as the ending point; saving the recent first n task switching times and excluding the oldest task switching time; and calculating the average task switching time according to the saved task switching times.
[0014] To solve the above technical problems, another technical solution adopted by the present invention is: to provide an electronic device, including: a processor and a memory, the memory is used to store computer program code, the computer program code includes computer instructions, when the processor executes the computer instructions, the electronic device executes the steps of the RTOS timer interrupt processing method as described above.
[0015] To solve the above technical problems, another technical solution adopted by the present invention is: to provide a readable storage medium storing a computer program, the computer program includes program instructions, and when the program instructions are executed by a processor of an electronic device, the processor is caused to execute the steps of the RTOS timer interrupt processing method as described above.
[0016] The beneficial effects of the embodiments of the present invention are: by pre-computing the average switching time of tasks, it is possible to more accurately determine which tasks expire within the average task switching time. This helps to reduce frequent context switches caused by high-priority tasks preempting the CPU, thereby improving the throughput and performance of the system; the timer queue is intelligently managed according to the timeout of tasks and types (one-shot or periodic tasks), the expired one-shot tasks will be deleted and resources released, while periodic tasks will reset the timeout and reorder, this management method not only ensures the effective utilization of resources but also guarantees the timely execution of tasks; by setting the overflow preload value of the hardware timer according to the first task that expires after the average task switching time, it ensures that the hardware timer can trigger interrupts more accurately, thereby improving the utilization efficiency of hardware resources; by reducing task switches and improving system performance, it can ensure the stability and reliability of these applications, improve the throughput and performance of the system, and is applicable to various real-time applications that require precise time control and fast response. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] 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-described drawings are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on the structures shown in these drawings.
[0018] Figure 1 is a schematic structural diagram of an RTOS timer interrupt processing system in an embodiment of the present invention;
[0019] Figure 2 is Figure 1 a schematic diagram of the functional modules of the interrupt processing device shown;
[0020] Figure 3 is a schematic scheduling diagram of an RTOS timer interrupt processing system in an embodiment of the present invention;
[0021] Figure 4 is a schematic flowchart of an RTOS timer interrupt processing method in an embodiment of the present invention;
[0022] Figure 5It is a schematic flowchart of a method for processing RTOS timer interrupts in another embodiment of the present invention;
[0023] Figure 6 It is a schematic diagram of the hardware structure of an electronic device in an embodiment of the present invention. Specific embodiments
[0024] 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 the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0025] 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, 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.
[0026] In addition, if there are descriptions involving "first", "second", etc. in the embodiments of the present invention, 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 that satisfies both A and B at the same time. In addition, the technical solutions between various embodiments can be combined with each other, but it must be based on the fact that those of ordinary skill in the art can implement them. When the combination of technical solutions is contradictory 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.
[0027] Please refer to Figure 1 , which is a RTOS timer interrupt processing system in an embodiment of the present invention; the system 10 includes a processor 20, as well as a timer 30 and an interrupt processing device 40 provided in the processor 20.
[0028] The processor 20 is a core component of the RTOS timer interrupt processing system 10, and is used to execute various tasks and operations of the system. Specifically, it receives and executes the interrupt signal from the timer 30, and at the same time coordinates the work of the interrupt processing device 40; runs the RTOS (Real-Time Operating System), and through task scheduling and management, ensures the execution and coordination of real-time tasks; utilizes the multi-task management framework provided by the RTOS to ensure that time is processed in real time to meet the real-time requirements of the system.
[0029] The timer 30 is a key component in the RTOS timer interrupt processing system 10, and is used to generate interrupt signals; specifically, the timer 30 counts time according to a preset time interval or condition, and when the set value is reached, it sends an interrupt request to the processor 20, so that the processor 20 can execute corresponding interrupt processing tasks according to the interrupt signal of the timer.
[0030] The interrupt processing device 40 is used to receive and process the interrupt signal from the timer 30. The interrupt processing device 40 includes multiple computer software function modules, which work together to realize functions such as starting, managing interrupt services, managing and calculating timer tasks, calculating the average task switching time, configuring hardware timers, and exiting interrupt services. When the interrupt signal arrives, the interrupt processing device 40 will respond quickly, and according to the task scheduling strategy of the RTOS, execute the corresponding interrupt service program to ensure that real-time tasks are processed in a timely manner. The interrupt processing device 40 is also used to update and maintain the timer 30 task list, and calculate and adjust the timeout time and overflow preload value of the timer 30 to meet the real-time requirements of the system.
[0031] Please also refer to Figure 2 for a Figure 1 schematic diagram of the function modules of the interrupt processing device 40 shown in an embodiment. The interrupt processing device 40 includes: an interrupt service management unit 41, a timer task management and calculation unit 42, an average task switching time calculation unit 43, and a hardware timer configuration unit 44.
[0032] The interrupt service management unit 41 is used to start an interrupt service when the timer 30 interrupts.
[0033] Specifically, the interrupt service management unit 41 monitors and responds to the interrupts, startup, and end of the interrupt service process of the timer 30. The interrupt service management unit 41 is initially in a monitoring state, continuously detecting whether an interrupt signal is generated in the system. When the timer 30 reaches a preset time point, it sends an interrupt signal to notify the interrupt service management unit 41 that a task needs to be processed immediately. When the interrupt service management unit 41 receives the interrupt signal from the timer 30, it immediately responds to this request; among them, the response process includes identifying the interrupt source (i.e., the timer 30) and determining the corresponding interrupt handler; the handler is predefined and used to process specific types of interrupt events. Once the interrupt handler is determined, the interrupt service management unit 41 starts the interrupt service process; the interrupt service process usually includes saving the context of the current processor (such as register status, program counter, etc.) so that the previous execution state can be restored after the interrupt processing is completed. Then, the control right is transferred to the interrupt handler to process the interrupt event. In the interrupt handler, some urgent or high-priority tasks are usually executed, such as updating the system status, processing external inputs, etc. These tasks need to be completed in the shortest possible time to resume the normal system execution process as soon as possible.
[0034] The timer task management and calculation unit 42 is used to recalculate the remaining timeout times of all timers according to the timeout time of the task at the head of the timer task list; traverse the timer task list to determine whether the task expires within the average task switching time; and cancel the suspended state of the timer task that has expired and is within the average task switching time, and enter the ready state.
[0035] Specifically, the timer task management and calculation unit 42 maintains the timer task list, calculates the remaining timeout time of the timer, and processes the task expiration logic. The timer task management and calculation unit 42 obtains the timeout time of the task at the head of the timer task list (i.e., the earliest task to be executed), traverses the entire task list, and recalculates the remaining timeout time for each task to ensure that each task has a correct timeout countdown according to its scheduled execution time. The timer task management and calculation unit 42 traverses the timer task list, checks the remaining timeout time of each task, and determines which tasks are about to expire, that is, whether the remaining timeout time of the task is less than or equal to the current average task switching time. In this way, the timer task management and calculation unit 42 can identify in advance those tasks that are about to be executed, so as to prepare. During the traversal process, the timer task management and calculation unit 42 compares the remaining timeout time of each task with the current average task switching time. If the remaining timeout time of a task is less than or equal to the average task switching time, then the task is considered to be about to expire. This is because the average task switching time represents the average time required for the system to switch from one task to another. For timer tasks that are due and within the average task switching time, the timer task management and calculation unit 42 will cancel them from the suspended state and set them to the ready state. This means that these tasks are ready to be executed and are only waiting for the scheduling of the system scheduler. In this way, the system can ensure that the tasks are executed at the predetermined time point while minimizing task delays and switching overhead.
[0036] The timer task management and calculation unit 42 ensures the timely execution and efficient scheduling of the timer task by recalculating the remaining timeout time, traversing the task list, determining whether the task is due, and canceling the suspended state.
[0037] The average task switching time calculation unit 43 is used to calculate the average task switching time by pre-stating the average time consumption of multiple task context switches; the average task switching time is used to evaluate the overhead of task switching. Specifically, the average task switching time calculation unit 43 is used to:
[0038] Calculate the time of each task switching t_switch = t2-t1; where t1 is the time point when the current task is switched out as the starting point, and t2 is the time point when the task context is switched to another task and execution starts as the ending point;
[0039] Save the most recent n task switching times and remove the oldest task switching time; in this embodiment, n=1000; and
[0040] Calculate the average task switching time based on the saved task switching times.
[0041] When the system switches from the current task to another task, a series of operations are involved, such as saving the context of the current task, loading the context of the new task, etc. The time required for these operations is the task switching time. The module average task switching time calculation unit 43 accurately measures the time consumed in this process by recording the time point t1 when the task is switched out and the time point t2 when the new task is switched to and starts to execute, and then calculating the time difference t_switch = t2 - t1. To more comprehensively evaluate the task switching performance, the module average task switching time calculation unit 43 not only calculates the time of a single switch, but also saves the most recent 1000 task switching times. When a new switching time is calculated, the oldest switching time is automatically removed to keep the record list updated and of a fixed length. After saving n task switching times, the module average task switching time calculation unit 43 calculates the average value of these times, that is, the average task switching time, which reflects the overall efficiency of the system task switching. If the average task switching time is low, it indicates that the system task switching speed is fast and the performance is good; on the contrary, if the average task switching time is high, it may mean that there is a large overhead or bottleneck in the system during task switching.
[0042] The hardware timer configuration unit 44 is used to configure and restart the hardware timer, and set the timer overflow preload value according to the timer task timeout time. Specifically, the hardware timer configuration unit 44 is used for:
[0043] Set the timer at the head of the timer task list as the first task that expires after the average task switching time, and reset the hardware timer overflow preload value to the timeout time of this timer; and
[0044] Restart the hardware timer to continue monitoring the timer timeout situation.
[0045] The hardware timer configuration unit 44 checks the task at the head of the timer task list, that is, the next task that is about to expire. According to the timeout of this task, the hardware timer configuration unit 44 sets it as the first task to expire after the average task switching time, so as to ensure that the system can arrange the execution order of the timer according to the actual requirements of the task. The hardware timer configuration unit 44 resets the overflow preload value of the hardware timer; the overflow preload value is the value at which the timer 30 triggers an overflow event when reaching a certain specific time point. In this scenario, the overflow preload value of the hardware timer is set to the timeout of the head timer. In this way, when the hardware timer runs to this time point, an interrupt signal will be triggered to notify the system that the timer task has expired. After the above settings are completed, the hardware timer configuration unit 44 restarts the hardware timer, so that the timer 30 starts to count down according to the new overflow preload value, continuously monitoring the timeout of the timer. In this way, the system can track the status of the timer in real time and make corresponding responses when necessary. Thus, the hardware timer configuration unit 44 realizes the effective management and scheduling of timer tasks by accurately setting the overflow preload value of the timer and restarting the timer, ensuring that the system can accurately and efficiently process timer tasks, and improving the overall performance and stability of the system.
[0046] The interrupt service management unit 41 is further configured to be responsible for cleaning up resources and exiting the interrupt service process when the interrupt service processing is completed. Specifically, the interrupt service management unit 41 cleans up resources and exits the processing process when the timer ends the interrupt service.
[0047] After the interrupt handler finishes execution, the interrupt service management unit 41 is used to end the interrupt service process, including restoring the previously saved processor context so that the system can continue to execute the task before the interruption. Then, the interrupt service management unit 41 will re-enter the listening state, waiting for the arrival of the next interrupt signal.
[0048] Furthermore, the timer task management and calculation unit 42 is further configured to determine whether the expired task is a one-shot task or a periodic task: for an expired task that is a one-shot task, delete the task node and release resources; for an expired task that is a periodic task, reset the remaining timeout and move it to the appropriate position in the task queue.
[0049] For an expired task, the timer task management and calculation unit 42 further determines its type: whether it is a one-shot task or a periodic task.
[0050] One-shot tasks: These tasks are executed only once and are deleted once they expire. Therefore, the timer task management and computing unit 42 will delete the corresponding task nodes and release the relevant resources to ensure the effective utilization of system resources.
[0051] Periodic tasks: These tasks are repeatedly executed within a set period. When a periodic task expires, the timer task management and computing unit 42 will reset its remaining timeout to the start time of the next period and move it to the appropriate position in the task queue according to the new timeout length so that it can be correctly executed when the next period arrives.
[0052] Whether it is a one-shot task or a periodic task, as long as they expire and are within the average task switching time, the timer task management and computing unit 42 will cancel their pending status and make them enter the ready state. This means that these tasks are ready to be executed and are only waiting for the system scheduler to schedule them. Such processing can ensure that tasks can respond in a timely manner and reduce the waiting time of the system.
[0053] Through the above principle, the timer task management and computing unit 42 can effectively manage and schedule timer tasks to ensure that they can be executed according to the predetermined time and requirements. This is crucial for systems that require precise control of task execution times.
[0054] Please also refer to Figure 3 , which is a scheduling schematic diagram of an RTOS timer interrupt processing system in an embodiment of the present invention. As shown in the figure, in the scheduling algorithm in the prior art, when the time point a is reached, the system will directly switch to task X for execution. However, if it is subsequently found that there is a higher-priority task Y that will expire soon, then the system will switch from task X to task Y again. Such a context switch is ineffective because task X has not actually been executed.
[0055] The present invention adopts a more intelligent scheduling strategy: when the timer interrupt is triggered, the system not only considers the currently expired tasks but also traverses the task queue to find the list of tasks (list_in_average_task_switching_time) that will expire within the average task switching time interval. These tasks will then be made into the ready state, and the system will select the task with the highest priority from them for scheduling.
[0056] This strategy brings two significant benefits:
[0057] First, it reduces ineffective task context switches. Since the system considers the tasks that will expire in advance and preferentially schedules high-priority tasks, it can avoid the situation of switching to a low-priority task and then immediately switching back to a high-priority task.
[0058] Secondly, the turnaround time of tasks is shortened and the system throughput is improved. By optimizing the scheduling order, high-priority tasks can start and complete execution earlier, thus shortening the overall turnaround time of tasks. At the same time, due to the reduction of ineffective switching and the improvement of task execution efficiency, the system throughput is also enhanced.
[0059] Specifically, taking tasks X and Y as examples, in the prior art, the turnaround time of X is h - a = 14 us, the turnaround time of Y is e - b = 6 us, and the system throughput is 2 / 14 = 1 / 7. In the present invention, the turnaround time of X is shortened to g - a = 12 us, the turnaround time of Y is shortened to d - b = 4 us, and the system throughput is increased to 2 / 12 = 1 / 6.
[0060] The advantages of the present invention are as follows:
[0061] An RTOS timer interrupt processing system and device provided by the present invention can more accurately determine which tasks expire within the average task switching time by pre-calculating the average switching time of tasks. This helps to reduce frequent context switching caused by high-priority tasks preempting the CPU, thereby improving the system throughput rate and performance; intelligently manages the timer queue according to the timeout of tasks and their types (one-shot or periodic tasks). Expired one-shot tasks will be deleted and resources will be released, while periodic tasks will reset the timeout and be re-sorted. This management method not only ensures the effective utilization of resources but also guarantees the timely execution of tasks; sets the overflow preload value of the hardware timer according to the first task that expires after the average task switching time, ensuring that the hardware timer can trigger interrupts more accurately, thereby improving the utilization efficiency of hardware resources; by reducing task switching and improving system performance, it can ensure the stability and reliability of these applications, improve the system throughput rate and performance, and is applicable to various real-time applications that require precise time control and fast response.
[0062] Please refer to Figure 4 , which is a schematic flowchart of an RTOS timer interrupt processing method in an embodiment of the present invention. The method includes the following steps:
[0063] Step S50, start the interrupt service when the timer 30 interrupt is triggered.
[0064] Step S51, recalculate the remaining timeout of all timers according to the timeout of the task at the head of the timer task list.
[0065] Specifically, the timer task management and calculation unit 42 obtains the timeout of the task at the head of the timer task list (i.e., the task that needs to be executed earliest), traverses the entire task list, and recalculates the remaining timeout for each task to ensure that each task has a correct timeout countdown according to its scheduled execution time.
[0066] Step S52: Traverse the timer task list starting from the head, and determine whether the task expires within the average task switching time; if so, go to step S53; otherwise, go to step S54.
[0067] Among them, the average task switching time is obtained by pre-statistically calculating the average time-consuming of multiple task context switches.
[0068] "The average task switching time is obtained by pre-statistically calculating the average time-consuming of multiple task context switches" specifically includes: calculating the time of each task switch t_switch = t2 - t1; where t1 is the starting point with the time point of the current task cut out, and t2 is the ending point with the time point of switching to another task context and starting to execute; saving the most recent previous n task switching times and removing the oldest task switching time; in this embodiment, n = 1000; and calculating the average task switching time according to the saved task switching times.
[0069] The module average task switching time calculation unit 43 accurately measures the time consumed in this process by recording the time point t1 when the task is cut out and the time point t2 when switching to a new task and starting to execute, and then calculating the time difference t_switch = t2 - t1. To more comprehensively evaluate the task switching performance, the module average task switching time calculation unit 43 not only calculates the time of a single switch, but also saves the most recent previous 1000 task switching times. When a new switching time is calculated, the oldest switching time is automatically removed to keep the record list updated and of a fixed length. After saving n task switching times, the module average task switching time calculation unit 43 calculates the average of these times, that is, the average task switching time, which reflects the overall efficiency of the system task switching. If the average task switching time is low, it indicates that the system task switching speed is fast and the performance is good; on the contrary, if the average task switching time is high, it may mean that there is a large overhead or bottleneck in the system during task switching.
[0070] Step S53: For the timer task that expires within the average task switching time, cancel the suspended state of the corresponding task to make it enter the ready state. Then, return to step S52.
[0071] Step S54: Set the timer at the head of the timer task list as the first task to expire after the average task switching time, and reset the hardware timer overflow preload value to the timeout of this timer.
[0072] The hardware timer configuration unit 44 checks the task at the head of the timer task list, that is, the next task to expire. According to the timeout of this task, the hardware timer configuration unit 44 sets it as the first task to expire after the average task switching time, so as to ensure that the system can arrange the execution order of the timer according to the actual requirements of the task. The hardware timer configuration unit 44 resets the overflow preload value of the hardware timer; the overflow preload value is the value at which the timer 30 triggers an overflow event when reaching a certain specific time point. In this scenario, the overflow preload value of the hardware timer is set to the timeout of the head timer. In this way, when the hardware timer runs to this time point, an interrupt signal will be triggered to notify the system that this timer task has expired. After the above settings are completed, the hardware timer configuration unit 44 restarts the hardware timer, so that the timer 30 starts to count down according to the new overflow preload value, continuously monitoring the timeout of the timer. In this way, the system can track the status of the timer in real time and make corresponding responses when necessary. In this way, the hardware timer configuration unit 44 realizes the effective management and scheduling of the timer task by accurately setting the overflow preload value of the timer and restarting the timer, ensuring that the system can process the timer task accurately and efficiently, and improving the overall performance and stability of the system.
[0073] Step S55: Restart the hardware timer to continue monitoring the timeout of the timer.
[0074] Step S56: The interrupt service is completed, and the processing flow exits.
[0075] Please also refer to Figure 5 , which is a schematic flowchart of a method for handling RTOS timer interrupts in another embodiment of the present invention. The method includes the following steps:
[0076] Step S60: Start the interrupt service when the timer 30 interrupt is triggered.
[0077] Step S61: Recalculate the remaining timeout of all timers according to the timeout of the task at the head of the timer task list.
[0078] Step S62: Traverse the timer task list starting from the head, and determine whether the task expires within the average task switching time; if so, go to step S63; otherwise, go to step S64.
[0079] Among them, the average task switching time is obtained by pre-statistically averaging the time-consuming of multiple task context switches.
[0080] Step S63: For the timer tasks that expire within the average task switching time, determine whether the expired task is a one-shot task or a periodic task. If it is a one-shot task, go to Step S64; if it is a periodic task, go to Step S65.
[0081] Step S64: For the expired task being a one-shot task, directly delete the task node and release the relevant resources; then, go to Step S66.
[0082] Step S65: For the expired task being a periodic task, reset the remaining timeout of the task node to the period time and move it to the appropriate position in the task queue according to the timeout length.
[0083] Step S66: For the timer tasks that expire within the average task switching time, cancel the suspended state of the corresponding tasks to make them enter the ready state. Then, return to Step S62.
[0084] Step S67: Set the timer at the head of the timer task list to the first task that expires after the average task switching time, and reset the hardware timer overflow preload value to the timeout of this timer.
[0085] Step S68: Restart the hardware timer to continue monitoring the timeout of the timer.
[0086] Step S69: The interrupt service is completed, and the processing flow exits.
[0087] Through the optimization process of the above steps, unnecessary task switching can be effectively reduced, and the performance and stability of the real-time operating system can be improved. Especially in application scenarios with strict time requirements, the response speed and throughput of the system can be significantly enhanced.
[0088] It should be noted that since the RTOS timer interrupt processing method is applied to the RTOS timer interrupt processing device, the RTOS timer interrupt processing method also has the same advantages and beneficial effects. The advantages and beneficial effects of the RTOS timer interrupt processing device have been elaborated above and will not be repeated here.
[0089] One embodiment of the present invention also provides a readable storage medium, in which a computer program is stored. The computer program includes program instructions. When the program instructions are executed by the processor of an electronic device, the processor is caused to execute the steps of any one of the above-mentioned RTOS timer interrupt processing methods.
[0090] One embodiment of the present invention further provides an electronic device, including: a processor and a memory, where the memory is used to store computer program code, and the computer program code includes computer instructions. When the processor executes the computer instructions, the electronic device executes the steps of any one of the above RTOS timer interrupt processing methods.
[0091] Please refer to Figure 6 , which is a schematic diagram of the hardware structure of an electronic device provided by an embodiment of the present invention.
[0092] The electronic device 7 includes a processor 71, a memory 72, an input device 73, and an output device 74. The processor 71, the memory 72, the input device 73, and the output device 74 are coupled through a connector, and the connector includes various interfaces, transmission lines, or buses, etc., which are not limited in the embodiments of the present invention. It should be understood that in various embodiments of the present invention, coupling means being interconnected in a specific manner, including being directly connected or indirectly connected through other devices. For example, they can be connected through various interfaces, transmission lines, buses, etc.
[0093] The processor 71 can be one or more graphics processing units (GPUs). When the processor 71 is a single GPU, the GPU can be a single-core GPU or a multi-core GPU. Optionally, the processor 71 can be a processor group composed of multiple GPUs, and the multiple processors are coupled to each other through one or more buses. Optionally, the processor can also be other types of processors, etc., which are not limited in the embodiments of the present invention.
[0094] The memory 72 can be used to store computer program instructions and various computer program codes including the program code for executing the solution of the present invention. Optionally, the memory at least includes a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), or a compact disc read-only memory (CD-ROM), and the memory is used for relevant instructions and data.
[0095] The input device 73 is used to input data and / or signals, and the output device 74 is used to output data and / or signals. The output device 73 and the input device 74 can be independent devices or an integrated device.
[0096] It can be understood that in the embodiments of the present invention, the memory 72 can be used not only to store relevant instructions, and the embodiments of the present invention do not limit the specific data stored in the memory.
[0097] It can be understood that Figure 5 only a simplified design of an electronic device is shown. In practical applications, the electronic device may further include other necessary elements, including but not limited to any number of input / output devices, processors, memories, etc., and all RTOS timer interrupt processing devices that can implement the embodiments of the present invention are within the protection scope of the present invention.
[0098] 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 the present invention.
[0099] Those skilled in the art can clearly understand that for the convenience and conciseness 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 repeated here. Those skilled in the art can also clearly understand that each embodiment of the present invention has its own emphasis. For the convenience and conciseness of description, the same or similar parts may not be repeated in different embodiments. Therefore, the parts not described or not described in detail in a certain embodiment can refer to the descriptions in other embodiments.
[0100] In several embodiments provided by the present invention, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. For example, the device embodiments described above are only illustrative. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces. The indirect couplings or communication connections of the devices or units can be in electrical, mechanical, or other forms.
[0101] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they can be located in one place, or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0102] In addition, in each embodiment of the present invention, each functional unit can be integrated into one processing unit, or each unit can physically exist alone, or two or more units can be integrated into one unit.
[0103] 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 to-be-sealed component. The computer program to-be-sealed component 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 invention 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 a data center that includes one or more integrated available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., digital versatile disc (DVD)), or a semiconductor medium (e.g., solid state disk (SSD)), etc.
[0104] Those of ordinary skill in the art can understand all or part of the processes in the methods of the above embodiments. These processes can be completed by relevant hardware instructed by a computer program. The program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the above method embodiments. The aforementioned storage medium includes: read-only memory (ROM), random access memory (RAM), magnetic disk, or optical disc, etc., which can store program codes of various types.
[0105] 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 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. An RTOS timer interrupt handling device, characterized in that, Including: An average task switching time calculation unit, which is used to calculate the average task switching time by statistically averaging the time-consuming of multiple task context switches in advance, including: calculating the time t_switch of each task switch = t2 - t1; where t1 is the starting point with the time point when the current task is switched out, and t2 is the ending point with the time point when switching to another task context and starting to execute; saving the most recent previous n task switching times, and excluding the oldest task switching time; and calculating the average task switching time according to the saved task switching times; An interrupt service management unit, which is used to start interrupt service when the timer interrupt is triggered, and when the interrupt service processing is completed, clean up resources and exit the interrupt service process; A timer task management and calculation unit, which is used to recalculate the remaining timeout times of all timers according to the timeout time of the head task in the timer task list; traverse the timer task list to determine whether the task expires within the average task switching time; and cancel the suspended state of the timer task that expires and is within the average task switching time and enter the ready state. It is also used to determine whether the expired task is a one-shot or periodic task: for an expired task that is a one-shot task, delete the task node and release resources; for an expired task that is a periodic task, reset the remaining timeout time and move it to the appropriate position in the task queue; A hardware timer configuration unit, which is used to configure and restart the hardware timer, set the timer overflow preload value according to the timer task timeout time; set the timer at the head of the timer task list as the first task that expires after the average task switching time, and reset the hardware timer overflow preload value to the timeout time of the timer; and restart the hardware timer to continue monitoring the timer timeout situation.
2. An RTOS timer interrupt handling system, comprising a processor and a timer provided in the processor; characterized in that, It further includes the interrupt processing device as described in claim 1.
3. A method for handling RTOS timer interrupts, characterized in that, The method includes: Starting interrupt service when the timer interrupt is triggered, and when the interrupt service processing is completed, cleaning up resources and exiting the interrupt service process; Recalculating the remaining timeout times of all timers according to the timeout time of the head task in the timer task list; Traversing the timer task list starting from the head to determine whether the task expires within the average task switching time; where the average task switching time is obtained by statistically averaging the time-consuming of multiple task context switches in advance, including: calculating the time t_switch of each task switch = t2 - t1; where t1 is the starting point with the time point when the current task is switched out, and t2 is the ending point with the time point when switching to another task context and starting to execute; saving the most recent previous n task switching times, and excluding the oldest task switching time; and calculating the average task switching time according to the saved task switching times; For the timer task that expires within the average task switching time, determining whether the expired task is a one-shot or periodic task; For an expired task that is a one-shot task, directly deleting the task node and releasing the relevant resources; For a periodic task whose deadline has expired, reset the remaining timeout of the task node to the period time and move it to the appropriate position in the task queue according to the timeout length; For a timer task that expires within the average task switching time, cancel the suspension status of the corresponding task to make it enter the ready state; Set the timer at the head of the timer task list to the first task that expires after the average task switching time, and reset the hardware timer overflow preload value to the timeout of this timer; Set the timer at the head of the timer task list to the first task that expires after the average task switching time, and reset the hardware timer overflow preload value to the timeout of the said timer; and restart the hardware timer to continue monitoring the timer timeout situation; Restart the hardware timer to continue monitoring the timeout of the timer.
4. An electronic device, comprising: A processor and a memory, characterized in that the memory is used to store computer program code, and the computer program code includes computer instructions. When the processor executes the computer instructions, the electronic device executes the steps of the RTOS timer interrupt processing method as described in claim 3.
5. A readable storage medium storing a computer program, characterized in that, The computer program includes program instructions that, when executed by the processor of the electronic device, cause the processor to execute the steps of the RTOS timer interrupt processing method as described in claim 3.
Citation Information
Patent Citations
Method and device for evaluating the real-time performance of real time operating system
CN106021083A
Task scheduling method and system
CN114911591A