Task scheduling method and device
By splitting the task scheduling red-black tree into independent trees for online and offline tasks in the Linux kernel, forced eviction of online tasks is achieved, solving the problem of offline tasks interfering with online tasks in the existing technology and improving the efficiency and responsiveness of task scheduling.
Patent Information
- Application Number
- CN202510797747.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-13
- Publication Date
- 2025-09-19
AI Technical Summary
Existing task isolation solutions are difficult to effectively isolate the interference of offline tasks on online tasks when online tasks and offline tasks are mixedly deployed. In particular, it is difficult to respond to the processing needs of online tasks in a timely manner when resources are competing.
By splitting the task scheduling red-black tree in the Linux kernel into two independent red-black trees, online tasks and offline tasks are managed separately, and low-priority tasks are forcibly evicted when the CPU utilization exceeds the threshold, task switching can be achieved in milliseconds.
It improves the isolation effect between online tasks and offline tasks, ensuring that online tasks can respond to processing needs in a timely manner under resource-constrained conditions, and improves the efficiency and response performance of task scheduling.
Smart Images

Figure CN120670118A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a task scheduling method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Art
[0002] CPU tasks can be divided into online tasks and offline tasks. Deploying both online and offline tasks on the same CPU is called hybrid deployment. To minimize the interference of offline tasks on online tasks, offline and online tasks need to be isolated.
[0003] However, the existing isolation scheme has poor isolation effect. When offline tasks and online tasks suddenly compete for resources, it is difficult to respond to the processing needs of online tasks in a timely manner.
[0004] It should be noted that the above content is not necessarily prior art, nor is it intended to limit the scope of patent protection of this application. Summary of the Invention
[0005] Embodiments of the present application provide a task scheduling method, apparatus, computer device, computer-readable storage medium, and computer program product to solve or alleviate one or more of the technical problems raised above.
[0006] One aspect of an embodiment of the present application provides a task scheduling method, the method comprising: Obtaining a status identifier of an isolation mode; in the isolation mode, different types of tasks are associated with different red-black trees, tasks of a first type are associated with a first red-black tree, and tasks of a second type are associated with a second red-black tree; When the status flag is on, periodically obtain the current CPU utilization and the first task currently running; When the current CPU utilization is greater than a first preset threshold and the currently running task is of the second type, determining to set a first rescheduling; In response to the first rescheduling, selecting a second task from the first red-black tree; The currently running task is switched from the first task to the second task.
[0007] Optionally, each of the tasks corresponds to a virtual runtime, and the method further includes: According to the task type and virtual running time of each task, each task is associated with a corresponding node in the corresponding red-black tree.
[0008] Optionally, the method further includes: When the current CPU utilization is not greater than the first preset threshold, the task with the smallest virtual running time among the multiple tasks associated with the first red-black tree and the second red-black tree is determined as the second task.
[0009] Optionally, in response to the first rescheduling, selecting a second task from the first red-black tree includes: The task with the smallest virtual running time among the multiple tasks associated with the first red-black tree is determined as the second task.
[0010] Optionally, when applied to a Linux operating system, the method further includes: Determine the target interface, which is located in the proc / sys directory; Based on the target interface, the first preset threshold at the kernel level is configured.
[0011] Optionally, when the status indicator is closed, the method further includes: The first type of tasks and / or the second type of tasks are both associated with a first red-black tree, and the second red-black tree is set to an empty value; Update and obtain the running time of the currently running first task every preset time period; When the running time of the currently running first task is greater than a second preset threshold and a preset condition is met, determining to set a second rescheduling; In response to the second rescheduling, selecting the third task from the first red-black tree; The currently running task is switched from the first task to the third task.
[0012] Another aspect of an embodiment of the present application provides a task scheduling device, the device comprising: The acquisition module is used to obtain the status identifier of the isolation mode; An associating module, configured to associate different types of tasks with different red-black trees in the isolation mode, with tasks of a first type being associated with a first red-black tree and tasks of a second type being associated with a second red-black tree; The acquisition module is further configured to, when the status indicator is on, acquire the current CPU utilization and the first task currently running; a determination module, configured to determine to set a first rescheduling when the current CPU utilization is greater than a first preset threshold and the currently running task is of the second type; A selection module, configured to select a second task from the first red-black tree in response to the rescheduling task; A switching module is used to switch the currently running task from the first task to the second task.
[0013] Another aspect of an embodiment of the present application provides a computer device, including: at least one processor; and a memory communicatively coupled to the at least one processor; Wherein: the memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method described above.
[0014] Another aspect of an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, and when the computer instructions are executed by a processor, the method described above is implemented.
[0015] Another aspect of an embodiment of the present application provides a computer program product, including a computer program, which implements the above-mentioned method when executed by a processor.
[0016] The embodiment of the present application adopts the above-mentioned technical solution, which may include the following advantages: by associating different types of tasks into different red-black trees in isolation mode, different types of tasks can be clearly distinguished. When the CPU utilization exceeds the threshold and the currently running task is of the second type, the first rescheduling is performed. When the next task (the second task) is selected, the tasks in the second red-black tree only select tasks on the first red-black tree, thereby achieving the forced eviction of the current task of the second type. As a result, the isolation effect between the first type of tasks and the second type of tasks is improved, and the processing requirements of the first type of tasks can be responded to in a timely manner even when resources are tight. Specifically, the threshold isolation eviction implemented in the kernel state controls the corresponding time of eviction at the millisecond level. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] The accompanying drawings illustrate exemplary embodiments and constitute a part of the specification. Together with the description of the specification, they serve to explain exemplary implementation of the embodiments. The illustrated embodiments are for illustrative purposes only and do not limit the scope of the claims. Throughout the drawings, the same reference numerals designate similar, but not necessarily identical, elements.
[0018] Figure 1 The following schematically shows a flowchart of a task scheduling method according to the first embodiment of the present application; Figure 2 The following schematically shows a new flow chart of the task scheduling method according to the first embodiment of the present application; Figure 3 The following schematically shows a new flow chart of the task scheduling method according to the first embodiment of the present application; Figure 4Schematically shows another newly added flow chart of the task scheduling method according to the first embodiment of the present application; Figure 5 Schematically shows an application example diagram according to the first embodiment of the present application; Figure 6 Schematically shows another application example diagram according to the first embodiment of the present application; Figure 7 Schematically shows another application example diagram according to the first embodiment of the present application; Figure 8 Schematically shows another application example diagram according to the first embodiment of the present application; Figure 9 Schematically shows another application example diagram according to the first embodiment of the present application; Figure 10 A block diagram schematically shows a task scheduling device according to the second embodiment of the present application; and Figure 11 The following schematically shows a hardware architecture diagram of a computer device according to the third embodiment of the present application. DETAILED DESCRIPTION
[0019] In order to make the purpose, technical solutions and advantages of this application more clear, the present application is further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0020] It should be noted that the descriptions of "first", "second", etc. in the embodiments of the present application are for descriptive purposes only and should not be understood as indicating or implying their relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" or "second" may explicitly or implicitly include at least one of such features. In addition, the technical solutions between the various embodiments can be combined with each other, but this must be based on the fact that they can be implemented by ordinary technicians in this field. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such a combination of technical solutions does not exist and is not within the scope of protection required by this application.
[0021] It should be noted that in all aspects of this application involving data collection, storage, use, transmission, and processing, all data must strictly adhere to the laws, regulations, industry standards, and regulatory requirements of the data's source and use locations, as well as the relevant countries and regions, to ensure the legality and compliance of data activities. During the collection phase, the purpose, method, and scope of collection will be clearly stated to the data subject in a conspicuous manner. Collection will be carried out only after obtaining the data subject's legal authorization, ensuring that the collection process adheres to the "minimum necessary" principle and does not exceed the scope of data collection. During storage, the storage period will be limited, and after the storage purpose has been achieved, data will be promptly deleted, anonymized, or encrypted. During the use phase, strict data security protection mechanisms will be implemented, using field-level desensitization technology to process raw data according to pre-set desensitization rules. A variety of desensitization strategies, such as data generalization, data anonymization, and data encryption, will be employed for different data types to effectively mitigate the risk of sensitive information leakage and ensure that the data ultimately used is securely desensitized data, fully protecting the rights and interests of data subjects and data security. During the transmission and processing phases, the confidentiality and security of data will be ensured during transmission and processing.
[0022] In the description of this application, it should be understood that the numerical labels before the steps do not indicate the order in which the steps are executed. They are only used to facilitate the description of this application and to distinguish each step. Therefore, they cannot be understood as limitations on this application.
[0023] First, an explanation of the terms used in this application is provided: Task scheduling: It is the process by which the operating system determines the CPU to execute a specific task at a specific time based on a preset strategy to achieve reasonable allocation of resources.
[0024] Hybrid deployment: Online tasks (usually delay-sensitive high-priority tasks) and offline tasks (usually delay-insensitive low-priority tasks) of different priorities are deployed on the same CPU to share CPU resources.
[0025] Cgroup: The full name is Control group, which is a function in the Linux kernel used to uniformly monitor and limit resources for process groups.
[0026] CFS: It is a scheduling algorithm in the Linux kernel. It builds a red-black tree to maintain the task ready queue based on the virtual running time of the task, and gives priority to running the task with the smallest virtual running time.
[0027] Red-black tree: It is a self-balancing binary search tree that can achieve efficient insertion, deletion and search operations while ensuring order.
[0028] Virtual run time: It is an indicator used by the operating system scheduler to measure the "fair run time" of a task. It represents the value of the actual run time of the task on the CPU after adjusting the weight (such as priority). It is used to achieve fairness in multi-task scheduling.
[0029] Secondly, to facilitate those skilled in the art in understanding the technical solutions provided by the embodiments of this application, the following describes the relevant technologies: CPU tasks can be divided into online tasks and offline tasks. In a mixed deployment scenario, to prevent offline tasks from interfering with the operation of online tasks, important online tasks can be given exclusive CPU resources. Although this approach can ensure the performance of online tasks, it results in low CPU resource utilization, leading to increased costs.
[0030] Therefore, offline task interference with online tasks can be limited through isolation solutions, which include both user-mode and kernel-mode solutions. User-mode solutions include core binding, tidal scheduling, and direct eviction. Specifically, the core binding solution binds offline tasks to fixed CPU logical cores and sets a low cpu.share value to minimize the impact on online tasks. The tidal scheduling solution dynamically adjusts CPU resource allocation based on the task's operating characteristics over time. Direct eviction migrates offline tasks in user mode when online tasks are detected to be interfering or when the CPU utilization of the entire machine reaches a certain threshold. Kernel-mode solutions include mechanisms such as Group Identity, the BT scheduling algorithm, and throttle for limiting offline tasks. Specifically, Group Identity assigns an identity to each CPU cgroup to differentiate the priorities of tasks within different CPU cgroups. The BT scheduling algorithm creates a dedicated offline scheduling class for offline tasks by adding a new scheduling class, thus isolating high- and low-priority tasks. The throttle mechanism restricts offline tasks and adds the cfs_rq corresponding to the offline tasks to a global percpu linked list throttle_list, thereby giving priority to the use of CPU resources by online tasks.
[0031] However, the above isolation solutions have the following shortcomings in actual applications: The core binding solution limits the impact of offline tasks by binding them to designated CPU logical cores, but it cannot completely avoid resource competition between offline tasks and online tasks. Although the tidal solution can dynamically adjust resource allocation according to time, it does not achieve mixed deployment of online and offline tasks in the same time period. The direct eviction solution triggers task migration only after the CPU usage exceeds the threshold, and the operation occurs in the user state. The response delay can reach seconds, making it difficult to respond to sudden loads in a timely manner. In the kernel state solution, when faced with a sudden surge in business traffic and the CPU is suddenly full in a short period of time, there is still a lack of forced eviction of offline tasks. The isolation effect is poor, and it is difficult to effectively guarantee the response performance of online tasks.
[0032] To this end, an embodiment of the present application also provides a task scheduling technical solution. In this technical solution, when the CPU utilization of the entire machine reaches a set threshold, low-priority tasks (i.e., offline tasks) are forcibly evicted at the kernel layer. Specifically, in the Linux kernel, the red-black tree for managing scheduling tasks is split into two independent red-black trees, one of which is used as a high-priority tree to manage online tasks, and the other is used as a low-priority tree to manage offline tasks. When the CPU water level of the entire machine exceeds the threshold, if the currently running task comes from a low-priority tree, rescheduling is triggered and the CPU is forced to give up. In the task selection stage, if the CPU water level exceeds the threshold, the low-priority tree is skipped and the task is directly selected from the high-priority tree. Through this technical solution, the millisecond-level eviction effect of offline tasks is achieved at the kernel layer, thereby significantly improving the response efficiency of online tasks. See below for details.
[0033] The technical solutions of the present application are described below through a number of embodiments. It should be noted that these embodiments can be implemented in a variety of different forms and should not be construed as being limited to the embodiments described herein.
[0034] Example 1 Figure 1 The flowchart of the task scheduling method according to the first embodiment of the present application is schematically shown.
[0035] like Figure 1 As shown, the task scheduling method may include steps S100 to S108, wherein: Step S100, obtaining a status identifier of an isolation mode; in the isolation mode, different types of tasks are associated with different red-black trees, a first type of task is associated with a first red-black tree, and a second type of task is associated with a second red-black tree.
[0036] Step S102: When the status flag is on, obtain the current CPU utilization and the first task currently running.
[0037] Step S104 : When the current CPU utilization is greater than a first preset threshold and the currently running task is of the second type, determine to set a first rescheduling.
[0038] Step S106 : In response to the first rescheduling, select a second task from the first red-black tree.
[0039] Step S108: Switch the currently running task from the first task to the second task.
[0040] By associating different types of tasks with different red-black trees in isolation mode, different types of tasks are clearly distinguished. When CPU utilization exceeds a threshold and the currently running task is of the second type, a first rescheduling is performed. When selecting the next task (the second task), tasks in the second red-black tree are only selected from the first red-black tree, forcing the second type of task to be evicted. This improves the isolation between first and second type tasks, allowing for timely response to first type task processing needs even in resource-constrained situations.
[0041] The following combination Figure 1 , each step in steps S100~S108 and other optional steps are described in detail.
[0042] Step S100 , obtain the status identifier of the isolation mode; in the isolation mode, different types of tasks are associated with different red-black trees, the first type of tasks are associated with the first red-black tree, and the second type of tasks are associated with the second red-black tree.
[0043] Different types of tasks can include online tasks and offline tasks. For example, the first type of task can be online tasks. Online tasks can include business tasks that require high response timeliness and require priority, such as real-time communication, video calls, or interactive applications. The second type of task can be offline tasks. Offline tasks can include background computing, batch processing, or data analysis tasks that have a high tolerance for latency and can be deferred. Different types of tasks can be categorized and managed by setting their priority, the scheduling group (cgroup) to which the task belongs, or by task identifiers (such as process attributes and scheduling classes), thereby achieving isolation for different task types. It should be noted that in addition to online and offline, tasks can also be categorized by other criteria based on needs.
[0044] By obtaining the status flag of the isolation mode, different types of tasks are associated with two different red-black trees when the isolation mode is turned on, thereby achieving logical isolation between the first type of tasks (online tasks) and the second type of tasks (offline tasks) when tasks are rescheduled. In one example, the status flag of the isolation mode can be obtained by reading the kernel interface parameters. Specifically, in a Linux system, the current value of / proc / sys / kernel / sched_cpu_util_limit_enabled can be read through the cat command to determine whether the isolation mode is enabled. If the read result is 1, it can indicate that the OOC (Out of CPU) isolation mode is currently enabled. If the read result is 0, it means that the OOC isolation mode is not enabled, and a red-black tree is used to uniformly manage all types of tasks.
[0045] Step S102 , when the status indicator is on, obtain the current CPU utilization and the first task currently running.
[0046] By periodically obtaining the current CPU utilization and currently running tasks, it is possible to determine whether CPU resources are scarce, and thus decide whether to directly reschedule the task. The CPU utilization can reflect the current processor load. For example, the higher the CPU utilization, the busier the task execution in the current CPU, and the more attention the scheduler needs to pay to matching task types with priorities. The currently running first task can refer to a task instance assigned and executed by the scheduler on a CPU core. If the first task belongs to the second type of task (such as an offline task), the rescheduling task will be directly executed in high-load scenarios.
[0047] In some embodiments, CPU utilization and information about currently running tasks can be obtained through a Linux kernel interface or status file. Specifically, the current CPU utilization can be estimated by periodically reading the time statistics field in the / proc / stat file and calculating the difference between the active time and the total time between two sampling intervals. Information about the currently running task can be obtained by using the current macro provided by the kernel scheduler to obtain the task_struct structure of the process currently running on the CPU. The task type of the currently running task can then be further determined based on relevant fields in the task_struct (such as scheduling policy, priority, etc.).
[0048] Step S104 , when the current CPU utilization is greater than the first preset threshold and the currently running task is of the second type, determine to set the first rescheduling.
[0049] Rescheduling is a mechanism in the operating system kernel that triggers the rescheduling of tasks on the current CPU, notifying the scheduler to switch tasks at the next scheduling point. The purpose of setting up rescheduling here is to allow the scheduler to reselect in a timely manner.
[0050] Setting the (first) rescheduling is to set a flag bit for the current task, so that the scheduler will replace the task by detecting the flag bit when the next scheduling occurs, and select the next suitable task from the queue to execute.
[0051] The first preset threshold value may be related to factors such as the number of CPU cores, the distribution of task types, and resource usage. The first preset threshold value may reflect the upper limit of the acceptable maximum CPU utilization rate to determine whether the current state is under high load. In practical applications, the first preset threshold value may be statically configured, for example, set to 80%, 85%, or 90%. It may also be dynamically adjusted according to the operating status. For example, based on the CPU usage over a period of time, the threshold value may be adaptively set through a sliding window or moving average algorithm to adapt to different load fluctuation patterns. In scenarios with high real-time requirements, the first preset threshold value may be set relatively low to improve the timeliness of task scheduling. In scenarios where resource utilization is prioritized, the threshold value may be relatively increased to take into account both the task throughput and the task scheduling cost.
[0052] When the system is under high load and the currently running task is of the second type, it is determined to set the first rescheduling. After the first rescheduling is determined to set, the second type of task (such as offline task) running on the CPU can be actively interrupted to ensure the scheduling priority and response time of the first type of task (such as online task). For example, in an online live broadcast platform, the audio and video encoding and decoding tasks belong to the first type of tasks. If a batch image compression task (belonging to the second type of task) is running on a CPU core at this time, and the current CPU utilization is higher than the first preset threshold (for example, 90%), the scheduler will immediately trigger rescheduling. The image compression task will be directly returned to the ready queue and wait for the next scheduling to ensure that the audio and video processing tasks can obtain CPU resources in a timely manner, thereby ensuring a smooth user experience.
[0053] Step S106 , in response to the first rescheduling, selecting a second task from the first red-black tree.
[0054] Based on this first rescheduling trigger mechanism, if the current CPU utilization exceeds the threshold, when selecting the next task (the second task), tasks in the second red-black tree are skipped, and only tasks in the first red-black tree are selected. This replaces the second type of task currently running on the CPU at millisecond speeds, thereby ensuring the operation of tasks that are sensitive to response latency, such as real-time communication and video processing. When the load is low or during the resource recovery phase, second type tasks, such as offline batch processing or background synchronization tasks, can be selected from the second red-black tree to further improve resource utilization.
[0055] Step S108 , switching the currently running task from the first task to the second task.
[0056] In practical applications, switching the first task currently running on the CPU to the second task can be accomplished through the kernel scheduling mechanism. Specifically, by saving the first task's context state (such as register information, program counter, stack pointer, etc.) and marking the first task's status as schedulable, the first task is placed back into the corresponding red-black tree to await subsequent scheduling. Furthermore, by loading the second task's context information, the second task becomes the currently running task, thereby achieving seamless switching between the first and second tasks. For example, in a scenario where a high-priority online task (such as a voice processing task) requires immediate scheduling, if the current CPU core is currently running a background log compression task (a second-type task), the background log compression task is suspended and placed back into the second red-black tree. Simultaneously, the voice processing task in the first red-black tree is scheduled to run on this CPU core, thereby ensuring the timely response of the voice processing task and that the background log compression task can subsequently continue to execute under low load.
[0057] The task scheduling process will be further introduced below in conjunction with multiple embodiments.
[0058] In an optional embodiment, each of the tasks corresponds to a virtual runtime, and the method further includes associating each of the tasks with a corresponding node in the corresponding red-black tree according to the task type and the virtual runtime of each of the tasks.
[0059] A task's virtual runtime represents the relative amount of time the task has consumed on the CPU. It can be used to measure task execution fairness and priority. A smaller virtual runtime indicates a task has consumed less runtime and is therefore more likely to be scheduled first.
[0060] Associating each task with the corresponding node in the corresponding red-black tree is equivalent to organizing and managing the tasks in the ready queue using a red-black tree data structure. Specifically, each task in the ready queue corresponds to a node in the red-black tree, and each node can store key scheduling information for the corresponding task (such as virtual runtime, task type, etc.). In practical applications, tasks can be classified according to their task types and inserted into the corresponding red-black trees. During the insertion process, the virtual runtime of the task can be used as a keyword to map the task to a node in the red-black tree. The specific configuration of the parent-child relationship and color (red or black) between nodes can be determined by the self-balancing insertion algorithm of the red-black tree to maintain the order and balance of the red-black tree, which is not further restricted here.
[0061] In this embodiment, tasks are classified by type and inserted into corresponding red-black trees based on their virtual runtimes. Different types of tasks can be managed in isolation. By utilizing the red-black tree's ability to organize nodes in an orderly manner based on virtual runtimes, the most suitable task can be quickly selected when setting up task scheduling, thereby further improving task scheduling efficiency and the timeliness of task processing.
[0062] In an optional embodiment, if Figure 2 As shown, the method further includes: Step S200 : When the current CPU utilization is not greater than the first preset threshold, determine the task with the smallest virtual running time among the multiple tasks associated with the first red-black tree and the second red-black tree as the second task.
[0063] If the current CPU utilization does not exceed the threshold, an optimal task is selected from each red-black tree for comparison, and the second task is determined based on the comparison results. For example, after a peak period has passed and user activity suddenly and rapidly decreases, the current CPU utilization will drop significantly. The current CPU utilization can reflect the return of CPU resources to an idle state. At this time, based on the current low load state, the tasks with the smallest virtual run time in the first and second red-black trees can be compared, and the task with the lowest scheduling cost and the best fit can be selected for execution, thereby improving overall resource utilization.
[0064] In practical applications, in order to determine the task with the minimum virtual runtime from the first red-black tree and the second red-black tree, the minimum node search operation can be performed on the first red-black tree and the second red-black tree respectively. Specifically, since the red-black tree can be constructed using the virtual runtime as the key value, the task with the minimum virtual runtime in each red-black tree can correspond to the leftmost child node of the red-black tree. Thus, the task with the minimum virtual runtime in the current red-black tree can be obtained by traversing from the root node to the left until the leftmost node. On this basis, the second task is determined by comparing the tasks corresponding to the minimum virtual runtime in the first red-black tree and the second red-black tree, thereby dynamically adapting to the current resource status and improving the rationality of the task scheduling strategy.
[0065] In this embodiment, when the load is low (the current CPU utilization is not greater than the first preset threshold), the task with the smallest virtual running time is selected from the two red-black trees to improve resource utilization efficiency and fairness of task scheduling.
[0066] In an optional embodiment, S104 may include: The task with the smallest virtual running time among the multiple tasks associated with the first red-black tree is determined as the second task.
[0067] The fact that the current CPU utilization is greater than the first preset threshold indicates that it is currently in a high load state and CPU resources are tight. At this time, high-priority tasks can be scheduled first to ensure that high-priority tasks can be executed in a timely manner. For example, during business peak periods, the number of user requests surges, resulting in a continued high CPU utilization. After determining that the execution task is rescheduled, the current load state can be determined by obtaining the latest current CPU utilization again. Under high load conditions, the scheduling strategy is adjusted to ignore the second red-black tree and directly select the task with the smallest virtual running time from the first red-black tree for execution. In actual applications, the task with the smallest virtual running time can be obtained by traversing the leftmost child node of the first red-black tree and determined as the second task, thereby giving priority to ensuring the execution needs of high-priority tasks and improving the responsiveness and scheduling efficiency of high-priority tasks under high loads.
[0068] In this embodiment, the CPU's current utilization is used to determine whether it is currently in a high-load state. Tasks with the smallest virtual runtimes are then prioritized in the first red-black tree for execution, prioritizing the scheduling of first-type tasks (online tasks) and forcibly evicting second-type tasks (offline tasks). This further enhances isolation, enabling more timely and rapid response to processing needs of first-type tasks (online tasks).
[0069] In an optional embodiment, if Figure 3 As shown, in a Linux operating system, the method further includes: Step S300: Determine the target interface, where the target interface is located in the proc / sys directory.
[0070] Step S302: Based on the target interface, configure the first preset threshold at the kernel level.
[0071] In actual applications, the first preset threshold can be adjusted appropriately based on specific business scenarios and server resources. A low first threshold can lead to excessive restrictions on offline tasks, impacting system throughput. A high first threshold can weaken the real-time response capabilities of high-priority tasks. Therefore, the optimal first threshold can be determined through stress testing or historical load analysis. For example, if you want to avoid second-type tasks (offline, low-priority tasks) when the server's overall CPU utilization exceeds 90%, ensuring that first-type tasks (online, high-priority tasks) can respond quickly and promptly, you can execute the following command in your terminal: echo 90 > / proc / sys / kernel / sched_cpu_util_limit to set the first preset threshold to 90%. Specifically, this command directly affects kernel parameter configuration by writing to the sched_cpu_util_limit interface in the proc / sys / kernel path, resulting in faster scheduling response. When the kernel detects that CPU utilization exceeds 90%, it will proactively avoid low-priority offline tasks.
[0072] In this embodiment, by configuring a first kernel-level preset threshold through the Linux operating system's target interface, dynamic kernel-mode CPU utilization-based determination of whether offline tasks need to be completely avoided can be implemented, thereby achieving millisecond-level isolation control of second-type tasks. This ensures the scheduling priority of first-type tasks when load increases, further improving their responsiveness in high-load scenarios.
[0073] In an optional embodiment, if Figure 4 As shown, when the state is marked as closed, the method further includes: Step S400: Associating the first type of tasks and the second type of tasks into a first red-black tree, and setting the second red-black tree to an empty value.
[0074] Step S402: Update and obtain the running time of the currently running first task every preset time period.
[0075] Step S404 : When the running time of the currently running first task is greater than a second preset threshold and a preset condition is met, determining to set a second rescheduling.
[0076] Step S406 : In response to the second re-scheduling, select the third task from the first red-black tree.
[0077] Step S408: Switch the currently running task from the first task to the third task.
[0078] In actual applications, when the status is marked as closed, it means that the current mode is non-isolated. At this time, the first type of task and the second type of task are both associated with the first red-black tree, the second red-black tree is set to an empty value, and task scheduling is triggered periodically every preset time period. Specifically, the running time of the currently running first task is obtained. If it is detected that the running time of the first task is greater than the minimum running time threshold (i.e., the second preset threshold), and the preset conditions (such as high CPU load, priority inversion) are met at the same time, the second rescheduling is set. After setting the second rescheduling, the task with the smallest virtual running time is selected as the third task in the first red-black tree containing the first type of task and / or the second type of task, and the currently running task is switched from the first task to the third task. Exemplarily, when the status of the isolation mode is marked as closed, task rescheduling can be performed by the CFS scheduling algorithm. Specifically, as Figures 5 and 6 As shown, the runtime of the currently running task A is checked every preset interval (one tick). If the runtime of the currently running task A is less than the preset minimum runtime (sched_min_granularity), the current task A continues to run. If the runtime of the currently running task A is greater than the minimum runtime, the system checks whether other rescheduling conditions are met. If so, the system reschedules the task; otherwise, the current task A continues to run.
[0079] In this embodiment, in non-isolated mode, various types of tasks are uniformly associated with the first red-black tree. The current task's runtime is periodically obtained and combined with pre-set conditions to determine whether to reschedule. This ensures the minimum runtime of tasks and avoids frequent task switching.
[0080] In order to make this application easier to understand, the following Figures 7-9 An exemplary application is provided.
[0081] S11: Check every tick whether the global switch of the OOC function is turned on (whether OOC is enabled). If it is turned on, enter S13; otherwise, enter S12.
[0082] In step S12, the global OOC function switch is not turned on (OOC is not enabled). The runtime of the currently running task (task A) is obtained. If the runtime of the currently running task is less than the minimum runtime, the current task continues to run. If the runtime of the currently running task is greater than the minimum runtime, the system checks whether other rescheduling conditions are met. If so, rescheduling is performed; otherwise, the current task continues to run.
[0083] In step S13, after confirming that the OOC function is globally enabled (OOC is enabled), obtain the CPU threshold. Specifically, add the threshold setting interface sched_cpu_util_limit_enabled or sched_cpu_util_limit in the proc / sys directory. For example, run the command echo 90 > / proc / sys / kernel / sched_cpu_util_limit. This sets the CPU threshold to 90%.
[0084] In step S14, after the OOC function is enabled, the task ready queue is divided into the first red-black tree (high-priority tree) and the second red-black tree (low-priority tree) for maintenance based on task type. For example, if tasks E and F are low-priority tasks, and tasks B, C, and D are high-priority tasks, tasks E and F will be managed in the low-priority tree, while tasks B, C, and D will be managed in the high-priority tree.
[0085] S15, checks whether the current CPU utilization reaches the threshold and whether the currently running task (task A) is an offline task.
[0086] If the CPU utilization is greater than the threshold and the currently running task is an offline task, proceed to step S16; otherwise, proceed to step S12.
[0087] S16: Set the CPU to be rescheduled.
[0088] S17, after the scheduler detects the flag that the CPU needs to be rescheduled, it directly selects a task (such as task B) from the high-priority tree (the first red-black tree).
[0089] In this example application, different types of tasks are clearly distinguished by associating them with different red-black trees when OOC (isolation mode) is enabled. When CPU utilization exceeds a threshold and the currently running task is offline, the task is rescheduled, forcing the offline task to be evicted. This improves the isolation between high-priority and low-priority tasks, allowing for timely response to high-priority task processing needs even when resources are limited.
[0090] Example 2 Figure 10The block diagram of the task scheduling device according to the second embodiment of the present application is schematically shown. The device can be divided into one or more program modules, one or more program modules are stored in a storage medium and executed by one or more processors to complete the embodiment of the present application. The program module referred to in the embodiment of the present application refers to a series of computer program instruction segments that can complete specific functions. The following description will specifically introduce the functions of each program module in this embodiment. Figure 10 As shown, the apparatus 1600 may include: an acquisition module 1610, an association module 1620, a determination module 1630, a selection module 1640, and a switching module 1650, wherein: An acquisition module 1610 is configured to acquire a status identifier of an isolation mode; An associating module 1620 is configured to associate different types of tasks with different red-black trees in the isolation mode, with tasks of a first type being associated with a first red-black tree and tasks of a second type being associated with a second red-black tree; The acquisition module 1610 is further configured to, when the status indicator is on, acquire the current CPU utilization and the first task currently running; a determining module 1630, configured to determine to set a first rescheduling if the current CPU utilization is greater than a first preset threshold and the currently running task is of the second type; A selection module 1640, configured to select a second task from the first red-black tree in response to the first rescheduling; The switching module 1650 is configured to switch the currently running task from the first task to the second task.
[0091] In an optional embodiment, each of the tasks corresponds to a virtual runtime, and the apparatus 1600 is further configured to: According to the task type and virtual running time of each task, each task is associated with a corresponding node in the corresponding red-black tree.
[0092] In an optional embodiment, the selection module 1640 is further configured to: When the current CPU utilization is not greater than the first preset threshold, the task with the smallest virtual running time among the multiple tasks associated with the first red-black tree and the second red-black tree is determined as the second task.
[0093] In an optional embodiment, the selection module 1640 is further configured to: The task with the smallest virtual running time among the multiple tasks associated with the first red-black tree is determined as the second task.
[0094] In an optional embodiment, applied to a Linux operating system, the apparatus 1600 is further configured to: Determine the target interface, which is located in the proc / sys directory; Based on the target interface, the first preset threshold at the kernel level is configured.
[0095] In an optional embodiment, when the state indicator is closed, the device 1600 is further configured to: The first type of tasks and / or the second type of tasks are both associated with a first red-black tree, and the second red-black tree is set to an empty value; Update and obtain the running time of the currently running first task every preset time period; When the running time of the currently running first task is greater than a second preset threshold and a preset condition is met, determining to set a second rescheduling; In response to the second rescheduling, selecting the third task from the first red-black tree; The currently running task is switched from the first task to the third task.
[0096] Example 3 Figure 11 The following schematically shows a hardware architecture diagram of a computer device 10000 suitable for implementing a task scheduling method according to the third embodiment of the present application. In some embodiments, the computer device 10000 may be a terminal device such as a smart phone, a wearable device, a tablet computer, a personal computer, a vehicle-mounted terminal, a game console, a virtual device, a workbench, a digital assistant, a set-top box, a robot, etc. In other embodiments, the computer device 10000 may be a rack server, a blade server, a tower server, or a cabinet server (including an independent server or a server cluster composed of multiple servers), etc. Figure 11 As shown, the computer device 10000 includes but is not limited to: a memory 10010, a processor 10020, and a network interface 10030 that can communicate with each other via a system bus. Memory 10010 includes at least one type of computer-readable storage medium, including flash memory, a hard disk, a multimedia card, card-type memory (e.g., SD or DX memory), random access memory (RAM), static random access memory (SRAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), programmable read-only memory (PROM), magnetic storage, a magnetic disk, an optical disk, and the like. In some embodiments, memory 10010 may be an internal storage module of computer device 10000, such as a hard disk or memory of computer device 10000. In other embodiments, memory 10010 may also be an external storage device of computer device 10000, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) card, a flash memory card, and the like equipped on computer device 10000. Of course, memory 10010 may also include both internal storage modules and external storage devices of computer device 10000. In this embodiment, the memory 10010 is generally used to store the operating system and various application software installed on the computer device 10000, such as program code of the task scheduling method, etc. In addition, the memory 10010 can also be used to temporarily store various data that has been output or is to be output.
[0097] In some embodiments, processor 10020 may be a central processing unit (CPU), a controller, a microcontroller, a microprocessor, or other chips. Processor 10020 is typically used to control the overall operation of computer device 10000, such as performing control and processing related to data exchange or communication with computer device 10000. In this embodiment, processor 10020 is used to execute program code stored in memory 10010 or process data.
[0098] Network interface 10030 may include a wireless network interface or a wired network interface. Network interface 10030 is typically used to establish a communication link between computer device 10000 and other computer devices. For example, network interface 10030 is used to connect computer device 10000 to an external terminal via a network, establishing a data transmission channel and a communication link between computer device 10000 and the external terminal. The network may be a wireless or wired network such as an intranet, the Internet, the Global System of Mobile Communications (GSM), Wideband Code Division Multiple Access (WCDMA), a 4G network, a 5G network, Bluetooth, or Wi-Fi.
[0099] It should be pointed out that Figure 11 Only a computer device having components 10010 - 10030 is shown, but it should be understood that implementing all of the shown components is not a requirement, and more or fewer components may alternatively be implemented.
[0100] In this embodiment, the task scheduling method stored in the memory 10010 can also be divided into one or more program modules and executed by one or more processors (such as processor 10020) to complete the embodiment of the present application.
[0101] Example 4 An embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program implements the steps of the task scheduling method in the embodiment when executed by a processor.
[0102] In this embodiment, computer-readable storage media include flash memory, hard disks, multimedia cards, card-type memories (e.g., SD or DX memories), random access memory (RAM), static random access memory (SRAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), programmable read-only memory (PROM), magnetic storage, magnetic disks, optical disks, and the like. In some embodiments, the computer-readable storage medium may be an internal storage unit of a computer device, such as the computer device's hard disk or memory. In other embodiments, the computer-readable storage medium may also be an external storage device of the computer device, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) card, a flash memory card, and the like. Of course, the computer-readable storage medium may also include both the internal storage unit and external storage devices of the computer device. In this embodiment, the computer-readable storage medium is typically used to store the operating system and various application software installed on the computer device, such as the program code of the task scheduling method described in the embodiment. In addition, the computer-readable storage medium may also be used to temporarily store various types of data that has been output or is about to be output.
[0103] Example 5 An embodiment of the present application further provides a computer program product, including a computer program, which implements the method in the above embodiment when executed by a processor.
[0104] Obviously, those skilled in the art should understand that the modules or steps of the above-mentioned embodiments of the present application can be implemented using general-purpose computer devices. They can be concentrated on a single computer device or distributed on a network composed of multiple computer devices. Alternatively, they can be implemented using program codes executable by the computer device, so that they can be stored in a storage device and executed by the computer device. In some cases, the steps shown or described can be performed in a different order than herein, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the embodiments of the present application are not limited to any specific combination of hardware and software.
[0105] It should be noted that the above are only preferred embodiments of the present application and do not limit the scope of patent protection of the present application. Any equivalent structure or equivalent process transformation made using the contents of the description and drawings of this application, or directly or indirectly applied in other related technical fields, are also included in the scope of patent protection of the present application.
Claims
1. A task scheduling method, characterized in that: The method comprises: Obtaining a status identifier of an isolation mode; in the isolation mode, different types of tasks are associated with different red-black trees, tasks of a first type are associated with a first red-black tree, and tasks of a second type are associated with a second red-black tree; When the status indicator is on, obtaining the current CPU utilization and the first task currently running; When the current CPU utilization is greater than a first preset threshold and the currently running task is of the second type, determining to set a first rescheduling; In response to the first rescheduling, selecting a second task from the first red-black tree; The currently running task is switched from the first task to the second task.
2. The method according to claim 1, characterized in that Each of the tasks corresponds to a virtual runtime, and the method further includes: According to the task type and virtual running time of each task, each task is associated with a corresponding node in the corresponding red-black tree.
3. The method according to claim 2, characterized in that The method further comprises: When the current CPU utilization is not greater than the first preset threshold, the task with the smallest virtual running time among the multiple tasks associated with the first red-black tree and the second red-black tree is determined as the second task.
4. The method according to claim 2, characterized in that In response to the first rescheduling, selecting a second task from the first red-black tree includes: The task with the smallest virtual running time among the multiple tasks associated with the first red-black tree is determined as the second task.
5. The method according to any one of claims 1 to 4, characterized in that Applied to the Linux operating system, the method further includes: Determine the target interface, which is located in the proc / sys directory; Based on the target interface, the first preset threshold at the kernel level is configured.
6. The method according to any one of claims 1 to 4, characterized in that When the status indicator is closed, the method further includes: Associating the first type of tasks and the second type of tasks into a first red-black tree, and setting the second red-black tree to an empty value; Update and obtain the running time of the currently running first task every preset time period; When the running time of the currently running first task is greater than a second preset threshold and a preset condition is met, determining to set a second rescheduling; In response to the second rescheduling, selecting a third task from the first red-black tree; The currently running task is switched from the first task to the third task.
7. A task scheduling device, characterized in that: The device comprises: The acquisition module is used to obtain the status identifier of the isolation mode; An associating module, configured to associate different types of tasks with different red-black trees in the isolation mode, with tasks of a first type being associated with a first red-black tree and tasks of a second type being associated with a second red-black tree; The acquisition module is further configured to, when the status indicator is on, acquire the current CPU utilization and the first task currently running; a determination module, configured to determine to set a first rescheduling when the current CPU utilization is greater than a first preset threshold and the currently running task is of the second type; A selection module, configured to select a second task from the first red-black tree in response to the first rescheduling; A switching module is used to switch the currently running task from the first task to the second task.
8. A computer device, characterized in that: include: at least one processor; and a memory communicatively coupled to the at least one processor; wherein: The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer instructions, which, when executed by a processor, implement the method according to any one of claims 1 to 6.
10. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to claims 1 to 6 are implemented.