Real-time task scheduling optimization method, device and system for FreeRTOS

Through dynamic optimization and adjustment of task priority and dependency checks, FreeRTOS's task starvation and deadlock problems in high-load scenarios are solved, efficient task scheduling and low energy consumption are achieved, and system performance and response speed are improved.

CN120256063BActive Publication Date: 2025-08-22JIANGSU JITRI TSINGUNITED INTELLIGENT CONTROL TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510732755.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-04
Publication Date
2025-08-22
Estimated Expiration
2045-06-04

AI Technical Summary

Technical Problem

FreeRTOS's static priority preemptive scheduling causes high-priority tasks to occupy CPU for a long time in multi-task high-load scenarios, low-priority tasks are hungry, and task resource allocation cannot be dynamically adjusted during burst loads. Inter-task communication dependence is prone to trigger deadlocks or priority reversals, and the lack of sleep mode optimization leads to low energy consumption.

Method used

Through dynamic optimization and adjustment algorithms, dynamically adjust task load, dynamically adjust task priority order, and conduct dependency checks to determine task priority processing methods, and combine dependency types and wake-up conditions to achieve dynamic scheduling optimization of tasks.

Benefits of technology

Real-time task scheduling in multi-task high-load scenarios is realized, reducing deadlocks and priority reversal between tasks, reducing energy consumption, and improving task response speed and CPU utilization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120256063B_ABST
    Figure CN120256063B_ABST
Patent Text Reader

Abstract

The present invention relates to the technical field of task scheduling for embedded systems, and specifically discloses a real-time task scheduling optimization method, device, and system for FreeRTOS, comprising: obtaining the current task load in FreeRTOS; if the current task load in FreeRTOS is greater than a preset load threshold, dynamically optimizing and adjusting the execution priority order of tasks in the current task load according to a dynamic optimization adjustment algorithm; performing a dependency check on the tasks after the dynamic optimization adjustment and obtaining a task dependency check result; and determining a corresponding task priority processing method based on the task dependency check result. The real-time task scheduling optimization method for FreeRTOS provided by the present invention can implement real-time task scheduling to meet the needs of multi-task and high-load application scenarios.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of task scheduling of embedded systems, and in particular to a real-time task scheduling optimization method for FreeRTOS, a real-time task scheduling optimization device for FreeRTOS, and a FreeRTOS system. Background Art

[0002] FreeRTOS is a scalable embedded operating system designed for small embedded systems. It features a scalable kernel, high real-time performance, a microkernel architecture, and strong scalability. Its portability layer is written in pure C, compliant with MISRA standards, with only a small amount of hardware-related code written in assembly language, resulting in extremely strong portability. The FreeRTOS kernel code is efficient and concise, and its extensive reuse of data structures results in a minimum source code binary image of only approximately 4KB. Although it is a real-time kernel, it possesses all the features and functions expected of a real-time operating system and continuously incorporates new operating system concepts and technologies. Its open source code is completely free, and its high real-time performance makes it suitable for most microprocessor- and microcontroller-based embedded systems. It holds a high market share in the microcontroller market and is widely used in fields such as the Internet of Things and industrial control.

[0003] Static priority preemptive scheduling is the default solution for FreeRTOS. Its core technology is to assign a unique priority to each task when it is created, use the pxReadyTasksLists[configMAX_PRIORITIES] array to maintain the task list of each priority, select tasks from the highest priority non-empty list during scheduling, allow high-priority tasks to interrupt the currently running low-priority tasks, and when the high-priority task is ready, it immediately preempts the low-priority CPU usage without waiting for the end of the time slice. In this method, because a static preemptive method is completely used to schedule tasks of different priorities, high-priority tasks periodically idle (for example, checking sensors every 1ms, but actually only 0.1ms is required), causing low-priority tasks to starve for a long time and resulting in load imbalance. At the same time, when low-priority tasks occupy shared resources (such as mutexes), low-priority tasks may block high-priority tasks, causing high-priority tasks to wait for low-priority tasks to release resources. At this time, low-priority tasks are preempted by medium-priority tasks, resulting in priority inversion. In addition, this method has poor adaptability to sudden loads. For example, in applications of planning and control scenarios, when switching from cruise mode to emergency obstacle avoidance, the priority of the path planning task needs to be temporarily increased, but the fixed priority of this method cannot be used in dynamic scenarios, and the complexity of scheduling needs to be increased.

[0004] Although applying the EDF (Earliest Deadline First) task scheduling algorithm in the FreeRTOS multi-tasking system can provide theoretically optimal real-time guarantees, it also has the following disadvantages: (1) The EDF algorithm conflicts with the native FreeRTOS architecture. FreeRTOS uses static priority bitmap scheduling by default, while EDF requires maintaining a global ordered task queue (such as a red-black tree or heap), which requires modifying core files, destroying the modular design of FreeRTOS and making upgrades and maintenance difficult; (2) The EDF algorithm requires expanding the TCB to store deadline parameters, which may increase RAM usage; (3) It does not consider data / timing dependencies between tasks, which may lead to deadlocks.

[0005] In summary, the default scheduler of the FreeRTOS multi-tasking operating system adopts priority preemptive scheduling, which has the following problems in multi-tasking high-load scenarios: (1) High-priority tasks occupy the CPU for a long time, causing low-priority tasks to "starve"; (2) Task resource allocation cannot be dynamically adjusted during burst loads (such as network packet bursts); (3) Communication dependencies between tasks require manual management by developers (such as using semaphores), which can easily cause deadlock or priority inversion; (4) Idle tasks only execute portYIELD() and lack corresponding sleep mode optimization, resulting in low energy consumption.

[0006] Therefore, how to implement real-time task scheduling to meet the needs of multi-task and high-load application scenarios has become a technical problem that needs to be solved urgently by those skilled in the art. Summary of the Invention

[0007] The present invention provides a real-time task scheduling optimization method for FreeRTOS, a real-time task scheduling optimization device for FreeRTOS and a FreeRTOS system, which solve the problem that real-time task scheduling cannot be realized in related technologies.

[0008] As a first aspect of the present invention, a real-time task scheduling optimization method for FreeRTOS is provided, which includes: obtaining the current task load in FreeRTOS; if the current task load in FreeRTOS is greater than a preset load threshold, dynamically optimizing and adjusting the execution priority order of tasks in the current task load according to a dynamic optimization adjustment algorithm; performing dependency checks on the tasks after dynamic optimization adjustment and obtaining task dependency check results; determining a corresponding task priority processing method according to the task dependency check results; wherein, dynamically optimizing and adjusting the execution priority order of tasks in the current task load according to the dynamic optimization adjustment algorithm includes: traversing all tasks in the current task load and reading task parameters of each task; calculating a new priority corresponding to each task; determining whether the new priority of each task is within a preset priority boundary limit range; if the new priority of the current task is within the preset priority boundary limit range, updating the priority order in the task control module of FreeRTOS; if the new priority of the current task is not within the preset priority boundary limit range, adjusting the new priority of the current task so that the adjusted new priority is within the preset priority boundary limit range.

[0009] Furthermore, a new priority corresponding to each task is calculated, including: obtaining the running time of FreeRTOS, wherein the calculation formula of the running time of FreeRTOS is: ,in, Indicates the running time of FreeRTOS, Indicates the start time of statistics. Indicates the end time of statistics. Represents the time counter frequency; the task period is calculated based on the clock beat data after FreeRTOS is started. The calculation formula for the task period is: ,in, Indicates the task cycle, Indicates the current total number of clock beats, Indicates the number of beats when the task was last woken up; the new priority of each task is determined according to the running time of the FreeRTOS, the task cycle and the user-preset benchmark priority, where the calculation formula for the new priority of each task is: ,in, Indicates the new priority of each task, Indicates the user preset baseline priority. Indicates the smoothing coefficient used to control the priority adjustment amplitude.

[0010] Furthermore, a dependency check is performed on the tasks after dynamic optimization adjustment, and a task dependency check result is obtained, including: traversing the task optimization adjustment list of all priorities in order from high to low priority, wherein each priority is configured with a task optimization adjustment list corresponding to the priority; judging whether the task optimization adjustment list of the current priority is empty; if the task optimization adjustment list of the current priority is not empty, traversing all tasks in the task optimization adjustment list of the current priority, and judging whether each task in the task optimization adjustment list satisfies the dependency, and obtaining the task dependency check result.

[0011] Furthermore, it is determined whether each task in the task optimization and adjustment list satisfies the dependency relationship, and the task dependency check result is obtained, including: determining whether the current task in the task optimization and adjustment list satisfies the dependency relationship according to the preset dependency list; if the dependency flag of the current task in the task optimization and adjustment list is non-zero, then determining that the task dependency check result is that the current task satisfies the dependency relationship; if the dependency flag of the current task in the task optimization and adjustment list is zero, then determining that the task dependency check result is that the current task does not satisfy the dependency relationship.

[0012] Furthermore, a corresponding task priority processing method is determined based on the task dependency check result, including: if the task dependency check result is that the current task satisfies the dependency, the current task is added to the task ready queue, and the tasks in the task ready queue are executed in order of priority from high to low; if the task dependency check result is that the current task does not satisfy the dependency, the current task is suspended and waits for the corresponding dependency.

[0013] Furthermore, the method further includes the following steps before the step of adding the current task to the task ready queue: determining the dependency type of the current task that satisfies the dependency relationship, wherein the dependency type includes at least data dependency and timing dependency.

[0014] Furthermore, the current task is suspended and waits for the corresponding dependency, including: performing a dependency judgment on the suspended current task according to a preset wake-up interval to determine whether the wake-up condition is met; if the suspended current task has a dependency, determining that the suspended current task meets the wake-up condition, and marking the current task that meets the wake-up condition as a schedulable state.

[0015] As another aspect of the present invention, a real-time task scheduling optimization device for FreeRTOS is provided, which is used to implement the real-time task scheduling optimization method for FreeRTOS described above, which includes: an acquisition module for acquiring the current task load in FreeRTOS; a dynamic optimization adjustment module for dynamically optimizing and adjusting the execution priority order of tasks in the current task load according to a dynamic optimization adjustment algorithm if the current task load in FreeRTOS is greater than a preset load threshold; a dependency check module for performing dependency check on the tasks after dynamic optimization adjustment and obtaining task dependency check results; and a determination module for determining a corresponding task priority processing method according to the task dependency check results.

[0016] As another aspect of the present invention, a FreeRTOS system is provided, which includes a scheduler, and the scheduler includes the real-time task scheduling optimization device for FreeRTOS described above.

[0017] The real-time task scheduling optimization method for FreeRTOS provided by the present invention monitors the current task load in FreeRTOS, and when the current task load exceeds a preset load threshold, dynamically optimizes and adjusts the execution priority order of tasks in the current task load according to a dynamic optimization adjustment algorithm, and then determines the final task priority processing method by dependency checking. This real-time task scheduling optimization method for FreeRTOS can meet the application scenario of multi-task high load by optimizing and adjusting the tasks under the condition of large task load through a dynamic optimization adjustment algorithm, and when the task load is lower than the preset load threshold, the dynamic optimization adjustment algorithm can be disabled, thereby maintaining low power consumption when the current task load is met. Therefore, the real-time task scheduling optimization method for FreeRTOS provided by the present invention can make corresponding dynamic optimization adjustments according to the actual load situation, realize real-time task scheduling to meet the application scenario of multi-task high load, and can also maintain low power consumption mode when the task load is low. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. Together with the following specific embodiments, they are used to explain the present invention, but do not constitute a limitation of the present invention.

[0019] Figure 1 The present invention provides a flow chart of the real-time task scheduling optimization method for FreeRTOS.

[0020] Figure 2This is a flowchart of the specific implementation process of the real-time task scheduling optimization method for FreeRTOS provided by the present invention.

[0021] Figure 3 This is a specific flow chart of the dynamic optimization adjustment provided by the present invention.

[0022] Figure 4 This is a specific flow chart of calculating the new priority provided by the present invention.

[0023] Figure 5 This is a specific flow chart of the dynamic priority calculation provided by the present invention.

[0024] Figure 6 This is a specific flow chart of the dependency check provided by the present invention.

[0025] Figure 7 The present invention provides a flowchart for determining the dependency relationship of each task.

[0026] Figure 8 This is a flowchart of the present invention for processing task priorities based on dependency relationships.

[0027] Figure 9 This is a specific flow chart provided by the present invention for performing corresponding processing based on whether the task meets the dependency.

[0028] Figure 10 This is a structural block diagram of the real-time task scheduling optimization device for FreeRTOS provided by the present invention. DETAILED DESCRIPTION

[0029] It should be noted that, in the absence of conflict, the embodiments and features of the embodiments of the present invention may be combined with each other. The present invention will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0030] In order to enable those skilled in the art to better understand the solutions of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of the present invention.

[0031] It should be noted that the terms "first," "second," and the like in the specification and claims of the present invention and the accompanying drawings are used to distinguish similar objects and are not necessarily used to describe a particular order or precedence. It should be understood that the terms used in this manner are interchangeable where appropriate for the embodiments of the present invention described herein. In addition, the terms "including," "having," and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product, or apparatus comprising a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units that are not explicitly listed or that are inherent to these processes, methods, products, or apparatuses.

[0032] In this embodiment, a real-time task scheduling optimization method for FreeRTOS is provided. Figure 1 : is a flowchart of a real-time task scheduling optimization method for FreeRTOS provided according to an embodiment of the present invention, such as Figure 1 As shown, it includes: S100, obtaining the current task load in FreeRTOS.

[0033] In an embodiment of the present invention, a task load monitoring module is provided in the FreeRTOS system, which can count the task execution time and cycle in real time and obtain the current task load.

[0034] S200: If the current task load in FreeRTOS is greater than a preset load threshold, the execution priority order of the tasks in the current task load is dynamically optimized and adjusted according to a dynamic optimization adjustment algorithm.

[0035] In the embodiment of the present invention, Figure 2 As shown, it is determined whether the current task load in FreeRTOS is greater than the preset load threshold. If it is greater, dynamic adjustment is allowed, that is, the scheduler is started, and the execution priority order of the tasks in the current task load is dynamically optimized and adjusted according to the dynamic optimization adjustment algorithm to obtain a task optimization adjustment list.

[0036] S300: performing dependency check on the tasks adjusted by dynamic optimization, and obtaining task dependency check results.

[0037] In an embodiment of the present invention, since there may be dependencies between tasks, that is, the output of task A may be the input of task B, a dependency check is performed on the tasks after dynamic optimization adjustment to confirm whether the execution priority of the tasks in the above task optimization adjustment list needs to be adjusted.

[0038] S400: Determine a corresponding task priority processing method according to the task dependency check result.

[0039] In an embodiment of the present invention, a task priority processing method in the task optimization and adjustment list is determined according to the above-mentioned task dependency check result.

[0040] Therefore, the real-time task scheduling optimization method for FreeRTOS provided by the present invention monitors the current task load in FreeRTOS, and when the current task load exceeds the preset load threshold, the execution priority order of the tasks in the current task load is dynamically optimized and adjusted according to the dynamic optimization adjustment algorithm, and then the final task priority processing method is determined by dependency inspection. This real-time task scheduling optimization method for FreeRTOS can meet the application scenario of multi-task high load by optimizing and adjusting the tasks under the condition of large task load through the dynamic optimization adjustment algorithm, and when the task load is lower than the preset load threshold, the dynamic optimization adjustment algorithm can be disabled, thereby maintaining low power consumption when the current task load is met. Therefore, the real-time task scheduling optimization method for FreeRTOS provided by the present invention can make corresponding dynamic optimization adjustments according to the actual load situation, realize real-time task scheduling to meet the application scenario of multi-task high load, and can also maintain low power consumption mode when the task load is low.

[0041] As a specific implementation method, Figure 3 As shown, the execution priority order of tasks in the current task load is dynamically optimized and adjusted according to the dynamic optimization adjustment algorithm, including: S210, traversing all tasks in the current task load and reading the task parameters of each task.

[0042] Specifically, for all tasks in the current task load, the task parameters of each task are read so as to facilitate subsequent priority calculation based on the task parameters.

[0043] S220: Calculate a new priority corresponding to each task.

[0044] It's important to note that a dynamic priority algorithm dynamically adjusts task priorities based on task requirements and the system's real-time status. Unlike static priority scheduling, dynamic priority scheduling can flexibly adjust task priorities during execution based on actual conditions, thereby optimizing overall system performance and responsiveness.

[0045] Specifically, a new priority corresponding to each task is calculated, such as Figure 4 As shown, it includes: S221, obtaining the running time of FreeRTOS, wherein the calculation formula of the running time of FreeRTOS is: ,in, Indicates the running time of FreeRTOS, Indicates the start time of statistics. Indicates the end time of statistics. Indicates the time counter frequency.

[0046] S222. Calculate the task period based on the clock tick data after FreeRTOS is started, where the calculation formula for the task period is: ,in, Indicates the task cycle, Indicates the current total number of clock beats, Indicates the number of ticks when the task was last woken up.

[0047] S223. Determine a new priority for each task based on the FreeRTOS runtime, the task cycle, and a user-preset baseline priority, wherein the calculation formula for the new priority for each task is: ,in, Indicates the new priority of each task, Indicates the user preset baseline priority, Indicates the smoothing coefficient used to control the priority adjustment amplitude.

[0048] S230: Determine whether the new priority of each task is within a preset priority boundary limit.

[0049] S240: If the new priority of the current task is within the preset priority boundary limit, update the priority order in the task control module of FreeRTOS.

[0050] S250: If the new priority of the current task is not within the preset priority boundary limit range, adjust the new priority of the current task so that the adjusted new priority is within the preset priority boundary limit range.

[0051] The following combination Figure 5 The figure shows a detailed description of the specific process of dynamic priority calculation in the embodiment of the present invention.

[0052] Add a task load monitoring module to the FreeRTOS scheduler to count the task execution time and cycle in real time; dynamically adjust the priority according to the real-time load of the task. The formula is as follows: ,in, Baseline priorities preset for users; Task utilization (needs to be limited to below 0.9 to prevent priority inversion); The execution time of the most recent cycle of the task; is the task period (the time between two executions); The smoothing coefficient is 2.0 by default, which controls the amplitude of priority adjustment to prevent frequent jitter.

[0053] Specifically, use the FreeRTOS system's runtime statistics (Runtime Stats) to obtain : , here is the system running time; The system end time for statistics; The starting time for statistics; is the time counter frequency in seconds.

[0054] Get the number of clock ticks that have passed since the FreeRTOS kernel was started and calculate the task period : , here is the total number of clock beats of the current system, The number of ticks since the last task was woken up.

[0055] calculate And the adjusted priorities: , add hysteresis (Hystersis), when When the task priority is adjusted to operations, reducing frequent priority adjustments.

[0056] Specifically, set the upper and lower limits of the priority to limit the boundaries of the new priority. When it is less than the FreeRTOS minimum priority threshold, The value of is set to the FreeRTOS minimum priority threshold; when In FreeRTOS the minimum priority threshold and Between, remain unchanged; when The value is greater than When Set the value of , so as to avoid exceeding the priority range allowed by FreeRTOS; finally, when adjusting the priority, adjust it within the upper and lower limits of the priority.

[0057] In the embodiment of the present invention, the task after dynamic optimization adjustment is checked for dependency, and the task dependency check result is obtained, such as Figure 6 As shown, it includes: S310, traversing the task optimization adjustment lists of all priorities in order from high to low priority, wherein each priority is configured with a task optimization adjustment list corresponding to the priority.

[0058] It should be understood that when performing dependency checking, the task optimization adjustment lists after dynamic optimization adjustment are matched with their corresponding priorities. That is, if the priorities are divided into A, B, and C from high to low, then priority A matches a task optimization adjustment list under priority A, priority B matches a task optimization adjustment list under priority B, and priority C matches a task optimization adjustment list under priority C. The task optimization adjustment lists of each priority level are traversed in order from priority A to priority C.

[0059] S320: Determine whether the task optimization adjustment list of the current priority is empty.

[0060] Specifically, for example, when traversing to priority B, it is determined whether the task optimization and adjustment list of priority B is empty. If it is empty, the task optimization and adjustment list corresponding to priority C is continued to be traversed.

[0061] S330: If the task optimization and adjustment list of the current priority is not empty, traverse all tasks in the task optimization and adjustment list of the current priority, and determine whether each task in the task optimization and adjustment list satisfies the dependency relationship, and obtain the task dependency check result.

[0062] Specifically, for example, when traversing to the current priority B, the task optimization and adjustment list of priority B is not empty, then all tasks in the task optimization and adjustment list corresponding to priority B are traversed, and the dependency relationship of each task in the task optimization and adjustment list is checked to obtain the task dependency check result.

[0063] Specifically, determine whether each task in the task optimization adjustment list satisfies the dependency relationship, and obtain the task dependency check result, such as Figure 7 As shown, it includes: S331, judging whether the current task in the task optimization adjustment list meets the dependency relationship according to the preset dependency relationship list.

[0064] S332: If the dependency flag of the current task in the task optimization and adjustment list is non-zero, determine that the task dependency check result is that the current task satisfies the dependency.

[0065] S333: If the dependency flag of the current task in the task optimization and adjustment list is zero, determine that the task dependency check result is that the current task does not satisfy the dependency.

[0066] In an embodiment of the present invention, a corresponding task priority processing method is determined according to the task dependency check result, such as Figure 8As shown, it includes: S410, if the task dependency check result is that the current task meets the dependency, the current task is added to the task ready queue, and the tasks in the task ready queue are executed in order of priority from high to low.

[0067] S420: If the task dependency check result is that the current task does not satisfy the dependency, suspend the current task and wait for the corresponding dependency.

[0068] It should be understood that when traversing the task optimization adjustment list according to priority, if the current task in the task optimization adjustment list does not satisfy the dependency relationship, then continue to judge the next task in the optimization task adjustment list; if the current task in the task optimization adjustment list satisfies the dependency relationship, then determine and return the check result that the current task satisfies the dependency relationship.

[0069] In an embodiment of the present invention, specifically, first, the FreeRTOS task priority management traverses from the highest priority to the lowest priority, and it is necessary to traverse the ready task lists of all priorities, and then check whether the current priority list is empty. If there is a task in the priority queue, it continues to execute, traverses all tasks of the priority, and checks whether the task meets the dependency. If there is a dependency, it immediately returns to the task for scheduling. If the current task dependency is not met, it continues to check the next task under the priority. If all tasks have unsatisfied dependencies, it returns the first task with the highest priority by default.

[0070] In an embodiment of the present invention, the following step is further performed before the step of adding the current task to the task ready queue: determining the dependency type of the current task that satisfies the dependency, wherein the dependency type includes at least data dependency and timing dependency.

[0071] Specifically, when selecting the next executable task, the scheduler first determines whether the task's dependencies are met. It checks the value of the dependency flag in the TCP (task control block) to determine the task's dependencies. If the dependency flag is non-zero, it indicates that the tasks have dependencies. The scheduler then further determines the dependency type. Dependency types primarily check for data dependencies and timing dependencies. Data dependencies primarily check shared resource flags, such as semaphores and message queue status. Timing dependencies primarily compare timestamps using FreeRTOS's clock tick count function. Table 1 below provides a detailed description of dependency types.

[0072] Table 1 Dependency type table

[0073]

[0074] To store task dependency information, this embodiment of the present invention extends the Task Control Block (TCB) structure to add dependent tasks, dependency types, and task completion timestamps to the tskTCB structure. The structure's members are: task stack pointer, task status, task priority, dependent tasks, dependency types, and task completion timestamp. The task completion timestamp records the last task completion time, facilitating timing dependency determination.

[0075] It should be understood that by extending the FreeRTOS TCB (task control block) and adding dependency identifiers between tasks, the scheduler can prioritize tasks that have no dependencies or whose dependencies have been satisfied, thereby reducing blocking.

[0076] It should be noted that an embodiment of the present invention also provides an API for declaring dependencies, so that tasks can register a function that describes the dependencies during initialization. The function includes obtaining the TCB of the task, recording the dependent tasks, setting the dependency type, and recording the timing dependency (delay time), where the timing dependency time is the sum of the current system time and the delay time.

[0077] As a specific implementation method, the current task is suspended and waits for the corresponding dependency, such as Figure 9 As shown, it includes: judging the dependency of the suspended current task according to the preset wake-up interval time to determine whether the wake-up condition is met; if the suspended current task has a dependency, determining that the suspended current task meets the wake-up condition, and marking the current task that meets the wake-up condition as a schedulable state.

[0078] It should be understood that in the embodiment of the present invention, in order to balance power consumption and task blocking, two triggering methods can be used. One is periodic triggering, that is, calling the priority adjustment function in the idle task (the triggering frequency can be reduced in low power mode); the other is event triggering, that is, immediately adjusting when the task execution time exceeds ±10% of the preset threshold.

[0079] In summary, the advantages of the real-time task scheduling optimization method for FreeRTOS provided by the present invention in terms of low power consumption optimization can be specifically reflected in the integration of dynamic frequency adjustment in the idle task and the adjustment of the clock frequency according to the CPU load. The specific implementation operations are as follows: (1) Calculate the CPU load. Calculate the CPU occupancy of the task during the system operation under the same time unit. , here Indicates the task running time, Indicates the number of clock ticks since the system was started.

[0080] (2) Dynamically adjust the CPU frequency. The CPU load is calculated through the task monitoring mechanism. In FreeRTOS, a function can be called to obtain task running time statistics. When the load is less than 30%, it means that the system is idle for a long time. At this time, it switches to low power mode to reduce energy consumption. When the CPU load is higher than 70%, it means that the system is busy. At this time, it is necessary to increase the high-performance mode to provide more computing resources. If the CPU load is between 30% and 70%, no frequency adjustment is performed and the current state is maintained.

[0081] (3) Combined with FreeRTOS Tickless mode. Based on (2), the CPU load is calculated through the task monitoring mechanism. When the load is less than 20%, the system enters light sleep mode to reduce power consumption. When the CPU load is higher than 20%, it enters light sleep mode to save energy while maintaining response speed.

[0082] The real-time task scheduling optimization method for FreeRTOS according to an embodiment of the present invention also features deep sleep and light sleep characteristics. In the deep sleep mode, the CPU clock may be turned off, leaving only low-power peripherals enabled; external interrupts or RTC events may be required for wakeup. In the light sleep mode, the CPU frequency may be reduced while remaining operational. Furthermore, the real-time task scheduling optimization method for FreeRTOS according to the present invention enables rapid wakeup, making it suitable for tasks with high real-time requirements.

[0083] In summary, the real-time task scheduling optimization method for FreeRTOS provided by the present invention can make corresponding dynamic optimization adjustments according to the actual load situation, realize real-time task scheduling to meet the application scenarios of multi-task and high load, and can also maintain low power consumption mode when the task volume is low, shorten the average task response time by 20%~30%, and improve the CPU utilization by more than 15%. It is compatible with the existing FreeRTOS API and does not require modification of user code.

[0084] As another embodiment of the present invention, a real-time task scheduling optimization device 100 for FreeRTOS is provided, which is used to implement the real-time task scheduling optimization method for FreeRTOS described above, wherein, Figure 10As shown, it includes: an acquisition module 110, which is used to obtain the current task load in FreeRTOS; a dynamic optimization adjustment module 120, which is used to dynamically optimize and adjust the execution priority order of tasks in the current task load according to the dynamic optimization adjustment algorithm if the current task load in FreeRTOS is greater than the preset load threshold; a dependency check module 130, which is used to perform dependency check on the tasks after dynamic optimization adjustment and obtain the task dependency check result; a determination module 140, which is used to determine the corresponding task priority processing method according to the task dependency check result.

[0085] The real-time task scheduling optimization device for FreeRTOS provided by the present invention monitors the current task load in FreeRTOS, and when the current task load exceeds a preset load threshold, dynamically optimizes and adjusts the execution priority order of tasks in the current task load according to a dynamic optimization adjustment algorithm, and then determines the final task priority processing method by checking the dependency relationship. This real-time task scheduling optimization device for FreeRTOS can meet the application scenario of multi-task high load by optimizing and adjusting tasks under the condition of large task load through a dynamic optimization adjustment algorithm, and when the task load is lower than the preset load threshold, the dynamic optimization adjustment algorithm can be disabled, thereby meeting the current task load while maintaining low power consumption. Therefore, the real-time task scheduling optimization device for FreeRTOS provided by the present invention can make corresponding dynamic optimization adjustments according to the actual load situation, realize real-time task scheduling to meet the application scenario of multi-task high load, and can also maintain a low power consumption mode when the task load is low.

[0086] The specific working principle of the real-time task scheduling optimization device for FreeRTOS can be referred to the description of the real-time task scheduling optimization method for FreeRTOS in the previous article, which will not be repeated here.

[0087] As another embodiment of the present invention, a FreeRTOS system is provided, which includes a scheduler, and the scheduler includes the real-time task scheduling optimization device for FreeRTOS mentioned above.

[0088] In an embodiment of the present invention, the FreeRTOS system further includes a task load monitoring module and a task control module, and the scheduler is communicatively connected with the task load monitoring module and the task control module.

[0089] The real-time task scheduling optimization system for FreeRTOS provided by the present invention monitors the current task load in FreeRTOS, and when the current task load exceeds a preset load threshold, dynamically optimizes and adjusts the execution priority order of tasks in the current task load according to a dynamic optimization adjustment algorithm, and then determines the final task priority processing method by dependency checking. This real-time task scheduling optimization system for FreeRTOS can meet the application scenario of multi-task high load by optimizing and adjusting tasks under the condition of large task load through a dynamic optimization adjustment algorithm, and when the task load is lower than the preset load threshold, the dynamic optimization adjustment algorithm can be disabled, thereby meeting the current task load while maintaining low power consumption. Therefore, the real-time task scheduling optimization system for FreeRTOS provided by the present invention can make corresponding dynamic optimization adjustments according to the actual load situation, realize real-time task scheduling to meet the application scenario of multi-task high load, and can also maintain low power consumption mode when the task load is low.

[0090] The specific working principle of the real-time task scheduling optimization system for FreeRTOS can be referred to the description of the real-time task scheduling optimization method for FreeRTOS in the previous article, which will not be repeated here.

[0091] It will be understood that the above embodiments are merely exemplary embodiments for illustrating the principles of the present invention, and the present invention is not limited thereto. Those skilled in the art will appreciate that various modifications and improvements can be made without departing from the spirit and substance of the present invention, and such modifications and improvements are also considered to be within the scope of protection of the present invention.

Claims

1. A real-time task scheduling optimization method for FreeRTOS, characterized in that: include: Get the current task load in FreeRTOS; If the current task load in FreeRTOS is greater than the preset load threshold, the execution priority order of the tasks in the current task load is dynamically optimized and adjusted according to the dynamic optimization adjustment algorithm; Perform dependency checks on tasks adjusted by dynamic optimization and obtain task dependency check results; Determine the corresponding task priority processing method according to the task dependency check result; The execution priority order of tasks in the current task load is dynamically optimized and adjusted according to the dynamic optimization adjustment algorithm, including: Traverse all tasks in the current task load and read the task parameters of each task; Calculate the corresponding new priority for each task; Determine whether the new priority of each task is within the preset priority boundary limit; If the new priority of the current task is within the preset priority boundary limit, the priority order in the FreeRTOS task control module is updated; If the new priority of the current task is not within the preset priority boundary limit range, the new priority of the current task is adjusted so that the adjusted new priority is within the preset priority boundary limit range; The dependency check is performed on the tasks after dynamic optimization adjustment, and the task dependency check results are obtained, including: Traverse the task optimization adjustment lists of all priorities in descending order, where each priority is configured with a task optimization adjustment list corresponding to the priority; Determine whether the task optimization adjustment list of the current priority is empty; If the task optimization and adjustment list of the current priority is not empty, all tasks in the task optimization and adjustment list of the current priority are traversed, and each task in the task optimization and adjustment list is judged to determine whether the dependency relationship is satisfied, and a task dependency check result is obtained; The new priority corresponding to each task is calculated, including: Get the running time of the task in FreeRTOS, where the calculation formula for the running time of the task in FreeRTOS is: , in, Indicates the running time of the task in FreeRTOS, Indicates the start time of the statistical task. Indicates the end time of the statistical task. Indicates the time counter frequency; The task cycle is calculated based on the clock tick data after FreeRTOS is started. The calculation formula for the task cycle is: , in, Indicates the task cycle, Indicates the current total number of clock beats, Indicates the number of beats when the task was last woken up; The new priority of each task is determined according to the running time of the task in the FreeRTOS, the task cycle, and the user-preset baseline priority, wherein the calculation formula for the new priority of each task is: , in, Indicates the new priority of each task, Indicates the user preset baseline priority, Indicates the smoothing coefficient used to control the priority adjustment amplitude.

2. The real-time task scheduling optimization method for FreeRTOS according to claim 1, characterized in that Determine whether each task in the task optimization adjustment list satisfies the dependency relationship and obtain the task dependency check result, including: Determine whether the current task in the task optimization adjustment list meets the dependency relationship based on the preset dependency relationship list; If the dependency flag of the current task in the task optimization adjustment list is non-zero, determining that the task dependency check result is that the current task satisfies the dependency; If the dependency flag of the current task in the task optimization adjustment list is zero, it is determined that the task dependency check result is that the current task does not satisfy the dependency.

3. The real-time task scheduling optimization method for FreeRTOS according to claim 2, characterized in that: Determine the corresponding task priority processing method based on the task dependency check result, including: If the task dependency check result is that the current task satisfies the dependency, the current task is added to the task ready queue, and the tasks in the task ready queue are executed in descending order of priority; If the task dependency check result is that the current task does not satisfy the dependency, the current task is suspended and waits for the corresponding dependency.

4. The real-time task scheduling optimization method for FreeRTOS according to claim 3, characterized in that: It also includes the following steps before adding the current task to the task ready queue: Determine a dependency type of a current task that satisfies the dependency, where the dependency type includes at least data dependency and timing dependency.

5. The real-time task scheduling optimization method for FreeRTOS according to claim 3, characterized in that: Suspend the current task and wait for the corresponding dependencies, including: Dependency judgment is performed on the suspended current task according to the preset wake-up interval to determine whether the wake-up condition is met; If the suspended current task has a dependency relationship, it is determined that the suspended current task meets the wake-up condition, and the current task meeting the wake-up condition is marked as a schedulable state.

6. A real-time task scheduling optimization device for FreeRTOS, used to implement the real-time task scheduling optimization method for FreeRTOS according to any one of claims 1 to 5, characterized in that: include: The acquisition module is used to obtain the current task load in FreeRTOS; A dynamic optimization adjustment module is used to dynamically optimize and adjust the execution priority order of tasks in the current task load according to a dynamic optimization adjustment algorithm if the current task load in FreeRTOS is greater than a preset load threshold; A dependency checking module is used to check the dependencies of tasks after dynamic optimization adjustment and obtain the task dependency checking results; The determination module is used to determine the corresponding task priority processing method according to the task dependency inspection result.

7. A FreeRTOS system, characterized in that: It includes a scheduler, and the scheduler includes the real-time task scheduling optimization device for FreeRTOS according to claim 6.

Citation Information

Patent Citations

  • A task packaging and scheduling method and system for multi-type context dependence

    CN109669767A

  • Intelligent task optimization method based on adaptive multi-dimensional dependency graph and dynamic scheduling

    CN119512707A