Thread scheduling methods, electronic devices, storage media, and program products

By performing safety checks and time-slot allocation between the source thread and the target thread, the processing mechanism of the Completely Fair Scheduler is bypassed, achieving efficient scheduling of high-frequency and cooperative thread tasks and solving the problem of thread scheduling latency.

CN121008898BActive Publication Date: 2026-01-30INSPUR SUZHOU INTELLIGENT TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511539629.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-10-27
Publication Date
2026-01-30
Estimated Expiration
2045-10-27

AI Technical Summary

Technical Problem

The Completely Fair Scheduler (CFS) suffers from increased thread scheduling latency when handling high-frequency and cooperative thread tasks due to its indirect handover mechanism and fair scheduling characteristics.

Method used

The thread switching request initiated by the source thread performs a safety check on the target thread and the source thread, calculates the allocatable time period, bypasses the processing mechanism of the Completely Fair Scheduler, updates the virtual runtime of the source thread, allocates the allocatable time period to the target thread, and calls the context switching function to perform the thread switch.

Benefits of technology

It reduces thread scheduling latency and improves thread scheduling efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121008898B_ABST
    Figure CN121008898B_ABST
Patent Text Reader

Abstract

This application discloses a thread scheduling method, electronic device, storage medium, and program product, relating to the field of computer technology. It involves a thread switching request initiated by a source thread, performing security checks on both the source and target threads. Based on the security check results, it calculates the allocatable time period allocated from the source thread to the target thread, bypassing the processing mechanism of the Completely Fair Scheduler (CFS). It updates the virtual runtime of the source thread and allocates the allocatable time period to the target thread. A context switching function is then called to perform a thread switching operation on both the source and target threads. Simultaneously, when the target thread consumes the allocatable time period, its virtual runtime is updated to complete the thread scheduling switch. This solves the problem in related technologies where the CFS, due to its indirect handover mechanism and the characteristics of fair scheduling, increases thread scheduling latency when handling high-frequency and cooperative thread tasks, thus reducing thread scheduling latency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a thread scheduling method, electronic device, storage medium, and program product. Background Technology

[0002] Linux is an open-source operating system kernel that uses the Completely Fair Scheduler (CFS), which ensures that all threads share processor resources fairly. In high-concurrency and high-frequency collaborative thread interaction scenarios, the CFS is used to achieve inter-thread interaction.

[0003] In current related technologies, when a source thread needs to wake up a target thread to take over its work, the system kernel inserts the target thread into the run queue, calculates the target thread's virtual runtime, and the Completely Fair Scheduler (CFS) determines when to wake up the target thread based on the virtual runtime. However, in these technologies, the CFS, due to its indirect handover mechanism and the characteristics of fair scheduling, leads to increased thread scheduling latency when handling high-frequency and cooperative thread tasks. Summary of the Invention

[0004] This application provides a thread scheduling method, electronic device, storage medium, and program product to at least solve the problem of increased thread scheduling latency caused by the completely fair scheduler in the related art when handling high-frequency and cooperative thread tasks due to its indirect handover mechanism and the characteristics of fair scheduling.

[0005] This application provides a thread scheduling method, including:

[0006] In response to a thread switching request initiated by the source thread, the processing function is called to perform security checks on the target thread and the source thread based on the thread switching request, and a security detection result is generated. The thread switching request is generated after the source thread calls the thread switching interface.

[0007] If the security check result is passed, then calculate the allocatable time period from the source thread to the target thread;

[0008] Update the virtual runtime of the source thread based on the allocatable time period, and allocate the allocatable time period to the target thread;

[0009] Calling the context switching function performs a thread switching operation between the source thread and the target thread, switching the working thread from the source thread to the target thread;

[0010] When the target thread consumes the allocatable time period, update the virtual runtime of the target thread to complete the thread scheduling switch.

[0011] This application also provides a thread scheduling device, including:

[0012] The security detection module is used to respond to thread switching requests initiated by the source thread. Based on the thread switching request, it calls the processing function to perform security checks on the target thread and the source thread and generates security detection results. The thread switching request is generated after the source thread calls the thread switching interface.

[0013] The first calculation module is used to calculate the allocatable time period that the source thread is allocated to the target thread if the security detection result is that the detection is passed.

[0014] The allocation module is used to update the virtual runtime of the source thread based on the allocatable time period and allocate the allocatable time period to the target thread.

[0015] The thread switching module is used to call the context switching function to perform thread switching operations between the source thread and the target thread, switching the working thread from the source thread to the target thread.

[0016] The first update module is used to update the virtual runtime of the target thread when the target thread consumes the allocatable time period, so as to complete the thread scheduling switch.

[0017] This application also provides an electronic device, including: a memory for storing a computer program; and a processor for implementing any of the above-described thread scheduling methods when executing the computer program.

[0018] This application also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of any of the above-described thread scheduling methods.

[0019] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of any of the above-described thread scheduling methods.

[0020] This application addresses the issue of increased thread scheduling latency caused by the source thread's indirect handover mechanism and fairness scheduling characteristics when the source thread initiates a thread switching request. Based on the security check results, it calculates the allocatable time period allocated from the source thread to the target thread, bypassing the Completely Fair Scheduler's (CFS) processing mechanism. It updates the source thread's virtual runtime and allocates the allocatable time period to the target thread. A context switching function is then invoked to perform a thread switching operation on both the source and target threads. Furthermore, when the target thread consumes its allocatable time period, its virtual runtime is synchronously updated to complete the thread scheduling switch. This solution resolves the problem in related technologies where the CFS, due to its indirect handover mechanism and fairness scheduling characteristics, increases thread scheduling latency when handling high-frequency and cooperative thread tasks, thereby reducing thread scheduling latency. Attached Figure Description

[0021] To more clearly illustrate the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0022] Figure 1 This is a schematic diagram illustrating an application scenario of the thread scheduling method provided in the embodiments of this application;

[0023] Figure 2 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 1 ;

[0024] Figure 3 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 2 ;

[0025] Figure 4 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 3 ;

[0026] Figure 5 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 4 ;

[0027] Figure 6 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 5 ;

[0028] Figure 7 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 6 ;

[0029] Figure 8 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 7 ;

[0030] Figure 9 This is a schematic diagram of the structure of the thread scheduling device provided in the embodiments of this application;

[0031] Figure 10 A schematic diagram of the structure of the electronic device provided in this application. Detailed Implementation

[0032] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the protection scope of this application.

[0033] It should be noted that, in the description of this application, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. The terms "first," "second," etc., in this application are used to distinguish similar objects and are not used to describe a specific order or sequence.

[0034] First, let me explain the terms used in this application:

[0035] Completely Fair Scheduler (CFS): The default scheduler for thread scheduling in the Linux kernel. Virtual runtime (Vruntime): An attribute of each task in CFS, used to measure the weighted, normalized execution time achieved on an ideal multitasking CPU.

[0036] Task structure: A core data structure in the Linux kernel used to identify a thread.

[0037] Completely Fair Scheduler Run Queue (CFS Run Queue): A data structure implemented using a red-black tree, used to organize all runnable CFS tasks in virtual runtime order.

[0038] Red-black tree: A self-balancing binary search tree, a data structure used in computer science.

[0039] To address the issue of increased thread scheduling latency caused by the indirect handover mechanism and fairness scheduling characteristics of the Completely Fair Scheduler (CFS) when handling high-frequency and cooperative thread tasks, this application proposes the following technical concept: The inventors have designed a thread switching method that bypasses the main scheduling logic, allowing a source thread to actively and directly hand over CPU execution rights to a designated target thread, and allocating unused CPU time within the source thread's current scheduling cycle to the target thread. A security check is performed on both the source and target threads based on the thread switching request initiated by the source thread. The allocatable time period allocated from the source thread to the target thread is calculated based on the security check results. The virtual runtime of the source thread is updated, bypassing the CFS processing mechanism, and the allocatable time period is allocated to the target thread. Considering the invocation of a context switching function to perform thread switching operations on both the source and target threads, and the synchronous update of the target thread's virtual runtime when the target thread consumes its allocatable time period, the thread scheduling switch is completed.

[0040] To enable those skilled in the art to better understand the present application, the present application will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0041] The specific application environment architecture or specific hardware architecture on which the execution of the thread scheduling method depends is described here.

[0042] refer to Figure 1 , Figure 1 This diagram illustrates an application scenario of the thread scheduling method provided in this embodiment, where a computer device is configured with a source thread and a target thread. Figure 1 As shown, the computer device includes: a receiving device 101, a processing device 102, and a display device 103.

[0043] It is understood that the structure illustrated in the embodiments of this application does not constitute a specific limitation on the thread scheduling method. In other feasible embodiments of this application, the above architecture may include more or fewer components than illustrated, or combine some components, or split some components, or arrange different components, which can be determined according to the actual application scenario and is not limited here. Figure 1 The components shown can be implemented in hardware, software, or a combination of both.

[0044] In the specific implementation process, the receiving device 101 can be an input / output interface or a communication interface, and can obtain the thread switching request initiated by the source thread.

[0045] The processing device 102 can switch a working thread from the source thread to the target thread.

[0046] The display device 103 can be used to display the above-mentioned thread scheduling results, etc.

[0047] The display device can also be a touch screen, used to receive user commands while displaying the above content, so as to realize the operation interaction with the user.

[0048] It should be understood that the above-mentioned processing device can be implemented by a processor reading instructions from memory and executing those instructions, or it can be implemented by a chip circuit.

[0049] Furthermore, the network architecture and business scenarios described in the embodiments of this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided in the embodiments of this application. As those skilled in the art will know, with the evolution of network architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.

[0050] Figure 2Flowchart of the thread scheduling method provided in the embodiments of this application Figure 1 ,like Figure 2 As shown, embodiments of this application provide a thread scheduling method, which is described in detail below:

[0051] S201: In response to a thread switching request initiated by the source thread, the processing function is called to perform security checks on the target thread and the source thread based on the thread switching request, and a security detection result is generated. The thread switching request is generated after the source thread calls the thread switching interface.

[0052] In this embodiment, a user-space system call is introduced in the Linux kernel as the sole entry point. This system call receives the target thread's ID as a parameter and is the only channel through which the source thread uses the thread scheduling method.

[0053] In this embodiment, the user space system call is denoted as lightweight_thread_switch().

[0054] In this embodiment, the method for designing user space system calls includes, but is not limited to, assigning system call numbers, adding entries, declaring prototypes, and implementing processing functions.

[0055] The specific method for assigning system call numbers is as follows: select the target architecture, determine the CPU architecture for which system calls are to be added, obtain the corresponding architecture's system call table file, obtain the numbers marked as unused in the file, update the entries in the system call table file, and map the selected unique number to the system call function name.

[0056] The specific method for adding entries is as follows: associate the system call number, system call name, and processing function in the Linux kernel (such as lightweight_thread_switch) in the system call table file and add them as new entries.

[0057] The specific method for declaring a prototype is as follows: obtain the public header file, and use a text editor to add the prototype declaration of the system call handler function in the public header file of system calls in the Linux kernel.

[0058] The specific method for implementing the processing function is as follows: create a folder for user-space system calls, use Linux kernel system call macros to perform parameter verification and permission checks in the kernel code according to the number of parameters, and compile the file into the Linux kernel to implement the system call logic.

[0059] In this embodiment, when executing a user-space system call, a kernel function (such as the `capable` function) is invoked to check whether the calling process has the ability to adjust the priority of other processes.

[0060] Specifically, when the Linux kernel executes a user-space system call, it calls a kernel function (such as the `capable` function) to check whether the current thread's effective capability set includes the ability to adjust the priority of other threads. If the check passes, thread switching is allowed; if the check fails, thread switching is prohibited.

[0061] Specifically, it determines whether the target thread and the source thread are located on the same CPU. If they are on the same CPU, it further determines whether the target thread is in a wakeable waiting state. If the target thread is in a wakeable waiting state, it verifies the execution permission of the source thread and generates a security detection result.

[0062] S202: If the security check result is passed, calculate the allocatable time period from the source thread to the target thread.

[0063] Specifically, the load factor is determined based on the instantaneous load information of the system, a load factor function is created based on the load factor, the delay threshold is calculated, the scheduling period is determined based on the number of runnable tasks in the scheduling queue and the delay threshold, the ideal running time of the source thread is calculated, and the allocatable time period of the source thread to the target thread is obtained based on the ideal running time and the execution time of the source thread.

[0064] S203: Update the virtual runtime of the source thread based on the allocatable time period, and allocate the allocatable time period to the target thread.

[0065] Specifically, the virtual runtime of the source thread is pre-incremented, and an unsigned field `donated_time` is added to the scheduling entity of the target thread, specifying the allocatable time period. The value is assigned to the unsigned field.

[0066] In this embodiment, the virtual runtime of the target thread remains unchanged during the period when the allocable time period is obtained, and is still the virtual runtime value before the target thread enters sleep.

[0067] In this embodiment, the formula for calculating the increase in the virtual runtime of the source thread is as follows:

[0068]

[0069] In the formula, This represents the virtual runtime of the source thread at time t; This represents the virtual runtime of the source thread at time t-1; This indicates the allocatable time period, which needs to be converted into virtual runtime. This indicates the weight used to adjust process priority when the value is 0; the default value is 1024. This indicates the weight of the source thread.

[0070] In this embodiment, if the weight of the source thread is equal to If the source thread's virtual runtime and allocable time period are the same, then the source thread's virtual runtime and allocable time period are the same; if the source thread's weight is greater than... If the virtual runtime of the source thread is less than the allocatable time period, then the source thread is scheduled with priority.

[0071] In this embodiment, the Completely Fair Scheduler ensures that all threads share processor resources fairly. The higher the priority of a thread, the slower its virtual runtime increases. The Completely Fair Scheduler always selects the thread with the smallest virtual runtime value in the current run queue for execution.

[0072] In this embodiment, the virtual runtime of the prior art is updated after the thread runs, that is, after the thread has used up the CPU time, the virtual runtime is updated according to the actual execution time. In this application, before the context switch, the Linux kernel updates the virtual runtime of the source thread and the target thread. When the source thread is rescheduled, the virtual runtime of the source thread is ahead of the actual execution time, which is equivalent to paying time for giving up the CPU. This ensures that when the source thread is scheduled next time, it will not get an unfair scheduling opportunity because it gave up the CPU in advance.

[0073] S204: Call the context switching function to perform a thread switching operation between the source thread and the target thread, switching the working thread from the source thread to the target thread.

[0074] In this embodiment, the processing function determines the source thread and target thread to be switched, and directly calls the context switching function in the Linux kernel to perform thread switching.

[0075] Specifically, the context of the source thread is saved, the current CPU state of the source thread is saved to the thread control block of the source thread, the context of the target thread is loaded, the saved CPU state data is restored from the thread control block of the target thread, and the thread switching code is executed to perform the thread switch.

[0076] S205: When the target thread consumes the allocatable time period, update the virtual runtime of the target thread to complete the thread scheduling switch.

[0077] Furthermore, it should be noted that the existing Completely Fair Scheduler (CFS) method for scheduling threads is as follows: When source thread A needs to wake up target thread B to take over its work, thread A initiates a wake-up event to wake up the waiting thread B. The Linux kernel responds to the wake-up event, changing thread B's state from interruptible sleep to running. Thread B is then inserted into the CPU's run queue (red-black tree). At this point, thread B has not started running; it has merely entered the runnable state and become one of many threads waiting to be scheduled, awaiting selection by the scheduler. Thread A continues execution until it relinquishes the CPU (e.g., enters sleep), its time slice expires, or it is preempted by a higher-priority task. At some future point, the CFS main function is called. The main function executes the complete scheduling logic, traversing the red-black tree to find the task with the smallest virtual runtime. If thread B becomes the selected task, the kernel will perform a context switch, allowing thread B to finally run on the CPU.

[0078] As can be seen from the above embodiments, by initiating a thread switching request from the source thread, a security check is performed on the source thread and the target thread. Based on the security check result, the allocatable time period allocated from the source thread to the target thread is calculated, bypassing the processing mechanism of the Completely Fair Scheduler. The virtual runtime of the source thread is updated, and the allocatable time period is allocated to the target thread. The context switching function is called to perform a thread switching operation on the source thread and the target thread. When the target thread consumes the allocatable time period, the virtual runtime of the target thread is updated synchronously to complete the thread scheduling switch. By bypassing the decision path of the Completely Fair Scheduler, the execution right of the source thread is directly handed over to the target thread, reducing the latency of thread scheduling.

[0079] In one embodiment of this application, step S202 includes:

[0080] S202a: If the safety test result is that the test is passed, the system load factor is determined based on the instantaneous load information of the system.

[0081] In this embodiment, the load factor can reflect the instantaneous load of the system.

[0082] In this embodiment, the load factor is determined in ways including but not limited to calculating the average number of runnable threads in the run queue within a set time period, and determining or monitoring the average waiting latency of the run queue based on CPU utilization.

[0083] S202b: Create a load factor function based on the system's load factor.

[0084] In this embodiment, the load factor function determines the scaling of the delay threshold. When the system is under low load, the function value approaches 1, keeping the delay threshold at its original value. When the load factor increases, the function value increases accordingly, dynamically raising the delay threshold.

[0085] In this embodiment, the load factor function is expressed as:

[0086]

[0087] In the formula, Represents the load factor function; Indicates the load factor; This represents the lower limit of the load factor threshold, which is a preset constant. It represents the boundary between low load and load states that require attention, and is an optimization parameter determined based on specific hardware information and workload. This represents the proportional coefficient, a preset constant that determines the strength or sensitivity of the impact of the load excess on the threshold. The default value of k is 1.

[0088] In this embodiment, when A value less than 0 indicates that the load factor is in a safe operating state.

[0089] S202c: Calculate the delay threshold based on the load factor function.

[0090] In this embodiment, the formula for calculating the delay threshold is:

[0091]

[0092] In the formula, Indicates the delay threshold; Represents the load factor function; Indicates the baseline value for the scheduling cycle; This indicates the minimum scheduling granularity.

[0093] In this embodiment, the scheduling period baseline value is the target time interval that ensures all ready threads run at least once. In the Linux kernel, the default value of the scheduling period baseline value is 6ms.

[0094] In this embodiment, the minimum scheduling granularity is the minimum time a single thread must run each time it is scheduled. In the Linux kernel, the default value for the minimum scheduling granularity is 0.75ms.

[0095] S202d: Determine the scheduling period based on the number of runnable tasks in the scheduling queue and the delay threshold.

[0096] Specifically, the number of runnable threads in the current CPU scheduling queue is obtained, and the number of runnable threads is compared with a latency threshold to determine the scheduling cycle.

[0097] Specifically, when the number of runnable threads in the scheduling queue Greater than the delay threshold When the scheduling period P is at that time, the formula for calculating the scheduling period P is:

[0098]

[0099] Specifically, when the number of runnable threads in the scheduling queue Less than or equal to the delay threshold When the scheduling period P is at that time, the formula for calculating the scheduling period P is:

[0100]

[0101] S202e: Calculates the ideal runtime of the source thread based on the scheduling cycle.

[0102] In this embodiment, the formula for calculating the ideal running time of the source thread is:

[0103]

[0104] In the formula, P represents the ideal runtime of the source thread; P represents the scheduling period. The weight of the source thread is obtained by mapping the values ​​of the adjusted process priority. This represents the total weight of all threads in the current scheduling queue.

[0105] In this embodiment, the weight of the source thread and the total weight of all threads in the current scheduling queue are read from the task management structure.

[0106] S202f: Based on the ideal running time and the actual execution time of the source thread, obtain the allocatable time period that the source thread can be allocated to the target thread.

[0107] In this embodiment, the formula for calculating the execution time of the source thread is:

[0108]

[0109] In the formula, Indicates the execution time of the source thread; This represents the current clock reading of the CPU run queue, recording the timestamp from system startup to the current moment, and can be read directly from the run queue structure; This represents the timestamp at which the source thread was scheduled to begin execution.

[0110] In this embodiment, the formula for calculating the allocatable time period is:

[0111]

[0112] In the formula, Indicates the allocatable time period.

[0113] In this embodiment, the value of the allocatable time period is detected. If the allocatable time period is less than or equal to the preset allocatable threshold, it means that the allocatable time period of the source thread has been exhausted and cannot be allocated, and the result of call failure is returned.

[0114] As can be seen from the above embodiments, by obtaining the instantaneous load information of the system, determining the load factor of the system based on the instantaneous load information, calculating the latency threshold based on the load factor function created based on the load factor, determining the scheduling period based on the number of runnable tasks in the scheduling queue and the latency threshold, dynamically calculating the ideal running time of the source thread based on the scheduling period, and obtaining the allocable time period based on the ideal running time and the execution time of the source thread, the introduction of the load factor shortens the scheduling period when the system is under light load and extends the period when under heavy load, reducing the overhead of context switching and achieving a balance between scheduling latency and resource utilization under different load scenarios.

[0115] In one embodiment of this application, step S201 includes:

[0116] S201a: Call the processing function based on the thread switching request to determine whether the target thread and the source thread are located on the same processor.

[0117] Specifically, the source thread initiates a thread switching request, calls a predefined user-space system call, and enters kernel mode, bypassing the Completely Fair Scheduler, through the interface of the system call.

[0118] In this embodiment, the source thread calling a predefined user-space system call is represented as: lightweight_thread_switch(tid), where tid represents the ID of the target thread.

[0119] Specifically, the kernel calls the processing function to find and lock the task management structure of the target thread based on the passed tid, and performs a security check on the task management structure of the target thread.

[0120] In this embodiment, the security check includes, but is not limited to, determining whether the target thread and the source thread are on the same CPU, whether the target thread is in a waiting state that can be woken up, and whether the source thread has the execution permission to switch threads.

[0121] Specifically, kernel interface functions (such as the task_cpu function) are used to determine whether the target thread and the source thread are located on the same processor.

[0122] In this embodiment, if the target thread and the source thread belong to different processor cores, a detection error result is returned.

[0123] S201b: If the target thread and the source thread are located on the same processor, determine whether the target thread is in a wakeable waiting state.

[0124] Specifically, a task wake-up function (such as the task_INTERRUPTIBLE function) is used to determine whether the target thread is in a waiting state that can be woken up. If the target thread is in a deep sleep state that cannot be woken up, an error result is returned.

[0125] S201c: If the target thread is in a wakeable waiting state, verify the execution permission of the source thread to generate a security check result.

[0126] Specifically, a kernel function (such as the `capable` function) is called to verify the execution permission of the source thread and determine whether the source thread has the ability to adjust the priority of other threads. If the check passes, the thread switching operation is allowed; if the check fails, the thread switching operation is prohibited.

[0127] As can be seen from the above embodiments, by determining whether the target thread and the source thread are located on the same processor and whether the target thread is in a wakeable waiting state, if the target thread and the source thread are on the same processor and the target thread is in a wakeable waiting state, the execution permission of the source thread is verified and a security detection result is generated. By transferring the control of the source thread to the target thread on the same processor, the integrity of the cached content is preserved, and cache pollution and performance loss caused by the scheduler migrating the task to threads on other processors are avoided.

[0128] Figure 3 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 2 ,like Figure 3 As shown, in one embodiment of this application, after step S203, the method further includes:

[0129] S301: Create a weight adjustment factor based on the correlation between the data size of the allocable time period and the historical characteristics of the target thread.

[0130] Specifically, historical feature data of the target thread is collected, the correlation between the data size of the allocable time period and the historical features of the target thread is analyzed, and based on the correlation analysis results, a calculation formula for the weight adjustment factor is designed. The weight adjustment factor is normalized to ensure that its value falls within the preset range.

[0131] In this embodiment, historical feature data includes, but is not limited to, the actual running time, the amount of data processed, and the waiting time within the previously allocatable time period.

[0132] For example, methods for resolving the correlation between the size of data in an allocatable time period and the historical characteristics of a target thread include: calculating the correlation coefficient between data size and thread performance using statistical methods, or using a regression model to determine the extent to which data size affects thread load.

[0133] S302: Generate the compensatory virtual runtime of the target thread based on the weight adjustment factor.

[0134] Specifically, the current virtual runtime baseline value of the target thread is obtained, the weight adjustment factor is applied to the current virtual runtime baseline value, a compensation value is generated through multiplication, the compensation virtual runtime is smoothed, and the compensation virtual runtime of the target thread is generated.

[0135] In this embodiment, the current virtual runtime baseline value of the target thread is obtained based on the thread's priority, historical virtual runtime, or a system-preset initial value.

[0136] In this embodiment, the smoothing method includes, but is not limited to, using the moving average method or the method of limiting the magnitude of change.

[0137] S303: Update the virtual runtime of the target thread based on the compensatory virtual runtime of the target thread, and determine the reclaimable allocable time period based on the updated virtual runtime, wherein the reclaimable allocable time period is used to compensate the unused allocable time period to the source thread.

[0138] Specifically, the compensatory virtual runtime of the target thread is added to the virtual runtime of the target thread to obtain the updated time value. The updated time value is compared with the standard virtual runtime value. If the updated virtual runtime is greater than the standard virtual runtime, the difference is obtained to obtain the reclaimable allocable time period. The reclaimable allocable time period is then allocated to the source thread.

[0139] As can be seen from the above embodiments, by creating a weight adjustment factor based on the correlation between the data size of the allocatable time period and the historical characteristics of the target thread, a compensatory virtual running time for the target thread is generated based on the weight adjustment factor, the virtual running time of the target thread is updated based on the compensatory virtual running time, and a reclaimable allocatable time period is determined based on the updated virtual running time. The reclaimable allocatable time period is then added to the source thread. By establishing a weight adjustment factor to compensate for the virtual running time of the target thread, and determining a reclaimable allocatable time period based on the compensated time, a reclamation mechanism is established, which improves the accuracy of the virtual running time of the target thread and the utilization rate of the allocatable time period of the source thread.

[0140] In one embodiment of this application, step S205 includes:

[0141] S205a: Get the allocatable time and the running time of the target thread recorded in the allocatable time period.

[0142] In this embodiment, the runtime of the target thread is recorded as follows: .

[0143] S205b: Determine whether the allocatable time is greater than the preset value.

[0144] Specifically, the newly added unsigned field `donated_time` in the scheduling entity of the target thread is detected, and it is determined whether the allocatable time in the unsigned field is greater than 0.

[0145] S205c: If the allocatable time is greater than the preset value, the allocatable time consumed by the target thread is calculated based on the target thread's running time and the allocatable time.

[0146] Specifically, if the allocatable time is greater than 0, then the allocatable time consumed by the target thread is the minimum of the target thread's running time and the allocatable time period in the scheduling entity.

[0147] In this embodiment, the formula for calculating the allocatable time consumed by the target thread is:

[0148]

[0149] In the formula, Indicates the allocatable time consumed by the target thread; Indicates the execution time of the target thread; Indicates the allocatable time period.

[0150] Furthermore, the running time of the target thread and the size of the allocatable time period are determined. If the running time of the target thread is greater than the allocatable time period, the allocatable time period is consumed during the running of the target thread, and the consumed allocatable time period is equal to the allocatable time period. If the running time of the target thread is less than or equal to the allocatable time period, the remaining allocatable time period is calculated, and the newly added unsigned field donated_time in the scheduling structure is updated.

[0151] The formula for calculating the remaining allocatable time period is:

[0152]

[0153] In the formula, This represents the remaining allocatable time interval at time t; This represents the remaining allocatable time interval at time t-1; This indicates the allocatable time consumed by the target thread.

[0154] S205d: Calculate the actual running time based on the running time of the target thread and the allocable time consumed by the target thread.

[0155] Specifically, the allocatable time consumed by the target thread is deducted from the target thread's runtime to calculate the actual runtime.

[0156] In this embodiment, the formula for calculating the actual running time is:

[0157]

[0158] In the formula, Indicates the actual running time; Indicates the execution time of the target thread; This indicates the allocatable time consumed by the target thread.

[0159] S205e: Determine whether the actual running time is greater than the preset running time.

[0160] Specifically, if the actual running time is less than or equal to 0, the update of the target thread's virtual running time is terminated.

[0161] S205f: If the actual running time is greater than the preset running time, the virtual running time of the target thread is updated according to the actual running time to complete the thread scheduling switch.

[0162] Specifically, if the actual runtime is greater than 0, the virtual runtime of the target thread is updated.

[0163] For example, if the target thread runs for 3ms, of which 2ms comes from the allocatable time period, then the virtual running time of the target thread is increased by 1ms.

[0164] As can be seen from the above embodiments, by obtaining the allocatable time and the running time of the target thread, calculating the allocatable time and actual running time consumed by the target thread, and updating the virtual running time of the target thread according to the actual running time, the correctness of the evolution of the total virtual running time of the system and the global fairness of the Completely Fair Scheduler are ensured.

[0165] Figure 4 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 3 ,like Figure 4 As shown, in one embodiment of this application, after step S204, the method further includes:

[0166] S401: Determine the memory access prediction strategy corresponding to the target thread based on the length of the allocatable time period.

[0167] In this embodiment, multiple memory access prediction strategies are pre-configured in the system.

[0168] Specifically, the system monitors and allocates an executable time period for the target thread, obtains the length of the time period, and compares the length of the allocable time period with a preset threshold to select the corresponding prediction strategy. S402: Generate a prefetch instruction sequence for the target thread based on the memory access prediction strategy, wherein the prefetch instruction sequence for the target thread is used to instruct the data to be accessed by the target thread to be loaded from main memory into the cache.

[0169] For example, if a sequential prefetch strategy is used, the address sequence of the next N consecutive memory blocks is generated starting from the last address of the target thread's current instruction pointer or data access.

[0170] For example, if a step-size prefetching strategy is used, the memory access address sequence of the target thread is analyzed, a fixed address step size is calculated, and future memory addresses are generated based on the current address and the step size.

[0171] Specifically, based on the generated memory address sequence, the memory address sequence is converted into specific prefetch instructions that the processor can recognize. The prefetch instructions are then sorted and optimized to generate an ordered prefetch instruction sequence.

[0172] S403: Execute the prefetch instruction sequence and obtain the cache hit rate of the target thread based on the allocable time period consumed by the prefetch instruction sequence.

[0173] Specifically, before the target thread begins execution of its allotted time slot, the system's prefetch execution unit executes the generated prefetch instruction sequence to preload the predicted data into the cache. Execution of the target thread is then initiated, allowing it to run within its allotted time slot. During thread execution, events related to cache access are monitored and recorded in real time by the processor's performance monitoring unit. The cache hit rate is calculated after the target thread has consumed its allotted time slot, or at the end of a monitoring cycle.

[0174] S404: Adjust the parameters of the memory access prediction strategy used to generate the prefetch instruction sequence based on the cache hit rate.

[0175] Specifically, the cache hit rate is compared with a preset target hit rate threshold, and the key parameters of the memory access prediction strategy are dynamically adjusted based on the comparison results. The adjusted parameters are then updated in the memory access prediction strategy corresponding to the target thread.

[0176] As can be seen from the above embodiments, the memory access prediction strategy of the target thread is determined by the length of the allocatable time period, the prefetch instruction sequence of the target thread is generated according to the memory access prediction strategy, the prefetch instruction sequence is executed, the cache hit rate of the allocatable time period is obtained, and the parameters of the memory access prediction strategy of the next round of prefetch instruction sequence are dynamically optimized according to the cache hit rate. Before the target thread is actually executed, the data is loaded into the cache according to the prediction strategy. When the thread needs to access memory, it can directly obtain the data from the cache, avoiding time-consuming main memory access.

[0177] Figure 5 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 4 ,like Figure 5 As shown, in one embodiment of this application, before step S201c, the following steps are further included:

[0178] S501: Obtain the cooperating thread group identifiers of the source thread and the target thread based on the thread control structure of the source thread and the target thread.

[0179] Specifically, the thread control structure of the source thread and the target thread in the operating system kernel is located and accessed. The cooperating thread group identifier information is read from the fields of the thread control structure. If the identifier is not directly stored in the thread control structure, the parent process pointer in the structure is used to trace to the process control block to which it belongs, and the shared cooperating group identifier is obtained from the process control block. The cooperating thread group identifiers of the source thread and the target thread are extracted.

[0180] S502: Determine whether the source thread and the target thread belong to the same cooperative thread group based on the cooperative thread group identifiers of the source thread and the target thread.

[0181] Specifically, the cooperating thread group identifiers of the source thread and the target thread are converted into boolean values ​​for comparison.

[0182] S503: If the source thread and the target thread belong to the same cooperating thread group, a flag is generated to prevent cross-group switching between the source thread and the target thread.

[0183] Specifically, in the system-wide thread relationship management table and the control structure of the source thread or target thread, a status flag is created, the status flag value is set to a valid state, and the status flag is associated and bound with the "source thread and target thread" pair to complete the generation and setting of the flag.

[0184] S504: Update the thread isolation policy based on the prohibition of cross-group switching flags between the source thread and the target thread and the interface security protocol.

[0185] Specifically, the effective interface security protocol rules are obtained from the system's security configuration module, a new thread isolation policy is generated based on the marked state value, and this newly generated policy rule is updated to the system kernel's thread scheduler and security module.

[0186] Among them, the effective interface security protocol rules define the security level of thread interaction between different collaboration groups, the permissions of data sharing, and the mandatory isolation requirements.

[0187] As can be seen from the above embodiments, by obtaining the cooperating thread group identifiers of the source thread and the target thread, it is determined whether the source thread and the target thread belong to the same cooperating thread group. If the source thread and the target thread belong to the same cooperating thread group, a flag prohibiting cross-group switching between the source thread and the target thread is generated, and the thread isolation policy is updated according to the generated flag and the interface security protocol to avoid resource contention caused by the abuse of the bypass mechanism and improve the stability of the system.

[0188] Figure 6 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 5 ,like Figure 6 As shown, in one embodiment of this application, before step S202, the following steps are further included:

[0189] S601: Obtain the cache hit rate and branch prediction error rate of the source thread during runtime according to the preset sampling period.

[0190] Specifically, a timer is set in the operating system kernel, and its triggering period is the preset sampling period. When the sampling period arrives, the kernel's performance monitoring subsystem is activated to collect data on the running source thread. By reading specific registers of the processor's hardware performance monitoring unit, the cache access event count of the source thread in the previous sampling period is obtained, including the total number of L1 cache accesses and the number of hits.

[0191] Specifically, the performance monitoring unit obtains the total number of branch instructions executed and the number of branch prediction failures in the previous sampling period of the source thread, and calculates the branch prediction error rate based on the collected event count.

[0192] S602: Calculate the adjustment amount of the cache locality constraint based on the cache hit rate and branch prediction error rate.

[0193] Specifically, the instantaneous values ​​of multiple cache hit rates and branch prediction error rates recorded within the sliding window are obtained based on the sliding window. The average values ​​of the cache hit rate and branch prediction error rate are calculated based on the instantaneous values ​​of the multiple cache hit rates and branch prediction error rates. The average values ​​of the cache hit rate and branch prediction error rate are input into a preset linear regression model, and the initial adjustment amount is output. The initial adjustment amount and the target parameter values ​​of the cache locality constraint in the Completely Fair Scheduler are weighted and fused to generate the adjustment amount of the cache locality constraint.

[0194] S603: Adjust the parameter values ​​of the cache locality constraint in the Completely Fair Scheduler based on the adjustment amount of the cache locality constraint.

[0195] Specifically, the parameters for managing cache affinity within the Fully Fair Scheduler are located, the current value of the parameters is obtained, the adjustment amount of the calculated cache locality constraint is applied to the current value to obtain a new parameter value, a boundary check is performed on the new parameter value, and the cache locality constraint parameters of the corresponding source thread or its scheduling domain in the Fully Fair Scheduler are updated to the new parameter value.

[0196] S604: Calculate the allocatable time period from the source thread to the target thread based on the adjusted cache locality constraint parameter values ​​and the virtual runtime of the source thread.

[0197] Specifically, the current virtual runtime of the source thread is obtained from the run queue data structure of the Completely Fair Scheduler. The basic allocatable time period is calculated based on the updated cache locality constraint parameter value. The cache locality constraint parameter is used as a multiplier to correct the basic allocatable time period, and the final allocatable time period allocated to the target thread is calculated.

[0198] As can be seen from the above embodiments, by obtaining the cache hit rate and branch prediction error rate of the source thread during runtime through a preset sampling period, the adjustment amount of the cache locality constraint is calculated, the parameter value of the cache locality constraint in the Completely Fair Scheduler is adjusted according to the adjustment amount, and the allocatable time period is calculated based on the parameter value after license suspension and the virtual runtime of the source thread, thereby improving the accuracy of calculating the allocatable time period.

[0199] In one embodiment of this application, step S602 includes:

[0200] S602a: Based on the preset sliding window length, obtain the instantaneous values ​​of multiple cache hit rates and branch prediction error rates recorded within the sliding window.

[0201] Specifically, the system kernel maintains a fixed-size circular data buffer for each thread. The size of this buffer is the preset sliding window length. Whenever a new sampling period is completed, the newly calculated instantaneous value of cache hit rate and instantaneous value of branch prediction error rate are added as a new data pair to the circular buffer corresponding to that thread.

[0202] S602b: Calculate the average of cache hit rate and branch prediction error rate based on the instantaneous values ​​of multiple cache hit rates and branch prediction error rates.

[0203] In this embodiment, the average of the cache hit rate and the branch prediction error rate is used as the stable performance feature within the most recent time window.

[0204] S602c: Input the average of the cache hit rate and branch prediction error rate into the preset linear regression model and output the initial adjustment amount.

[0205] In this embodiment, the preset linear regression model takes the average cache hit rate and the average branch prediction error rate as input features and outputs an adjustment amount.

[0206] S602d: The initial adjustment amount and the target parameter values ​​of the cache locality constraints in the Completely Fair Scheduler are weighted and fused to generate the adjustment amount of the cache locality constraints.

[0207] Specifically, based on the initial adjustment amount, weight coefficients, and weight coefficients of the target parameter value, a weighted fusion calculation is performed to generate the adjustment amount for the cache locality constraint.

[0208] As can be seen from the above embodiments, the instantaneous values ​​of multiple cache hit rates and branch prediction error rates within a preset sliding window are obtained by using the sliding window method. The average value is calculated and input into a preset linear regression model to obtain the initial adjustment amount. The initial adjustment amount and the target parameter values ​​of the cache locality constraint in the completely fair scheduler are weighted and fused to generate the adjustment amount of the cache locality constraint, which smooths data fluctuations and improves the accuracy of the calculated adjustment amount.

[0209] Figure 7 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 6 ,like Figure 7 As shown, in one embodiment of this application, after step S204, the method further includes:

[0210] S701: Obtain the historical power consumption data of the target thread and the power consumption threshold of the system, wherein the historical power consumption data is read by the thread running monitoring module.

[0211] Specifically, the thread running monitoring module in the system kernel is invoked to extract the instantaneous power consumption value sequence of the target thread in the most recent scheduling cycles, as well as statistical information such as average power consumption, peak power consumption, and the system's power consumption threshold from the circular buffer or data log of the thread running monitoring module.

[0212] S702: Based on historical power consumption data and the system's power consumption threshold, calculate the energy efficiency factor of the target thread using a preset energy efficiency model, where the energy efficiency model takes historical power consumption data as input and outputs the energy efficiency factor.

[0213] Specifically, the energy efficiency model calculates the basic energy efficiency ratio based on historical power consumption data and the system's power consumption threshold, uses the system power consumption threshold as a normalization reference, calculates the power consumption constraint factor, and combines the basic energy efficiency ratio and the power consumption constraint factor to calculate the final energy efficiency factor.

[0214] S703: Based on the energy efficiency factor and the system load factor, an energy efficiency adjustment coefficient is generated through a weighted fusion algorithm, wherein the weighted fusion algorithm is calculated according to the weight ratio of the energy efficiency factor and the load factor.

[0215] Specifically, the calculated energy efficiency adjustment coefficient is subjected to boundary processing to ensure that its value falls within the preset effective range.

[0216] S704: Generate an energy efficiency optimization time period based on the energy efficiency adjustment coefficient and the allocatable time period of the target thread.

[0217] Specifically, the basic allocatable time period and energy efficiency adjustment coefficient allocated to the target thread in the current scheduling cycle are obtained, and the energy efficiency optimization time period is generated through multiplication.

[0218] S705: Optimize the virtual runtime of the target thread based on the energy efficiency optimization time period and generate an energy efficiency optimization log.

[0219] Specifically, when the target thread begins to execute its energy efficiency optimization time period, the scheduler records the start time of its execution. When the target thread finishes execution, it records its actual running time. The scheduler uses the energy efficiency optimization time period as the "equivalent running time" of the thread in this scheduling cycle to calculate the increment of its virtual running time and writes the information of this scheduling into a specific log file or system buffer.

[0220] As can be seen from the above embodiments, by acquiring historical power consumption data and the system's power consumption threshold, the energy efficiency factor of the target thread is calculated using an energy efficiency model. An adjustment coefficient is calculated based on the energy efficiency factor and load factor using a weighted fusion algorithm. An energy efficiency optimization time period is generated based on the energy efficiency adjustment coefficient and the allocatable time period of the target thread, thereby optimizing the virtual running time of the target thread and improving the accuracy of the virtual running time of the target thread.

[0221] Figure 8 Flowchart of the thread scheduling method provided in the embodiments of this application Figure 7 ,like Figure 8 As shown, in one embodiment of this application, step S201c includes:

[0222] S801: Extract the security domain identifier and privilege level from the source thread's thread control structure.

[0223] In this embodiment, the security domain identifier is an integer security domain ID stored in a structure or a pointer to a security domain descriptor.

[0224] S802: Query the system security policy database based on the security domain identifier to obtain the list of target domains that the source thread is allowed to access.

[0225] Specifically, the security domain identifier is used as the query key to perform a lookup operation in the security policy database, locate the access control policy entry corresponding to the security domain identifier, and read the "list of target domains allowed to access" from the policy entry.

[0226] S803: Determine whether the security domain identifier is in the list of target domains that the source thread is allowed to access.

[0227] Specifically, if the target domain list is a bitmap, check whether the bit of the corresponding security domain identifier in the bitmap is set.

[0228] Specifically, if the target domain list is an array or linked list of IDs, the list is traversed to find an item that matches the security domain identifier.

[0229] S804: If the security domain identifier is in the list of target domains that the source thread is allowed to access, then verify that the privilege level of the source thread is higher than the privilege level required to perform the thread switch.

[0230] Specifically, if the privilege level of the source thread is lower than the privilege level required to perform the thread switch, a verification failure message will be generated.

[0231] S805: If the privilege level of the source thread is higher than the privilege level required to perform the thread switch, then record the context switch event and generate a security check result.

[0232] Specifically, a new log entry is created in the kernel's security event log buffer, and key information is recorded in the log entry.

[0233] The key information includes, but is not limited to, event type, source thread ID, source security domain identifier, target thread ID, target security domain identifier, source thread privilege level, and timestamp.

[0234] As can be seen from the above embodiments, by verifying the security domain affiliation between the source thread and the target thread and checking the privilege level of the source thread, abnormal thread switching across security domains is effectively prevented, and thread switching operations are restricted to the range allowed by the security policy, thereby improving the system's ability to resist internal attacks.

[0235] In one embodiment of this application, the method further includes the following step before step S402:

[0236] S901: Obtain the memory access pattern sequence of the target thread within the historical scheduling cycle.

[0237] Specifically, pattern features are extracted from the captured raw memory address sequence. The main memory access patterns identified in each scheduling cycle are recorded as a data item in chronological order into the circular buffer corresponding to the thread. When analysis is required, the memory access patterns recorded in the most recent N historical scheduling cycles are read from the buffer to form an ordered pattern sequence.

[0238] S902: Predict the memory access probability distribution of the target thread within the allocable time slice based on the memory access pattern sequence.

[0239] Specifically, a hidden Markov model or a lightweight recurrent neural network is used to predict the memory access probability distribution of the target thread within the allocatable time slice.

[0240] S903: The predicted memory addresses are probabilistically sorted according to the memory access probability distribution, and the prefetch instruction sequence is determined based on the sorted memory addresses.

[0241] In this embodiment, the probabilities are sorted in descending order.

[0242] As can be seen from the above embodiments, by obtaining the memory access pattern sequence of the target thread in the historical scheduling cycle, calculating the memory access probability distribution of the target thread in the allocatable time slice, sorting them according to the probability from high to low, and including the sorted addresses in the prefetch instruction sequence, the correlation of addresses in the prefetch instruction sequence is improved.

[0243] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method.

[0244] Figure 9 This is a schematic diagram of the structure of the thread scheduling device provided in an embodiment of this application. Figure 9As shown, embodiments of this application also provide a thread scheduling device 90, including: a security detection module 901, a first calculation module 902, an allocation module 903, a thread switching module 904, and a first update module 905.

[0245] The security detection module 901 is used to respond to the thread switching request initiated by the source thread, call the processing function according to the thread switching request to perform security checks on the target thread and the source thread, and generate security detection results. The thread switching request is generated after the source thread calls the thread switching interface.

[0246] The first calculation module 902 is used to calculate the allocatable time period from the source thread to the target thread if the security detection result is a pass.

[0247] The allocation module 903 is used to update the virtual runtime of the source thread according to the allocatable time period and allocate the allocatable time period to the target thread.

[0248] The thread switching module 904 is used to call the context switching function to perform thread switching operations between the source thread and the target thread, switching the working thread from the source thread to the target thread.

[0249] The first update module 905 is used to update the virtual runtime of the target thread when the target thread consumes the allocable time period, so as to complete the thread scheduling switch.

[0250] In one embodiment of this application, the first computing module 902 includes:

[0251] The first determining unit is used to determine the load factor of the system based on the instantaneous load information of the system if the security detection result is that the detection is passed.

[0252] Create a unit to generate a load factor function based on the system's load factor.

[0253] The first calculation unit is used to calculate the delay threshold based on the load factor function.

[0254] The second determining unit is used to determine the scheduling period based on the number of runnable tasks in the scheduling queue and the delay threshold.

[0255] The second calculation unit is used to calculate the ideal running time of the source thread based on the scheduling cycle.

[0256] The first generation unit is used to obtain the allocatable time period of the source thread to the target thread based on the ideal running time and the execution time of the source thread.

[0257] In one embodiment of this application, the security detection module 901 includes:

[0258] The first judgment unit is used to call the processing function based on the thread switching request to determine whether the target thread and the source thread are located on the same processor.

[0259] The second judgment unit is used to determine whether the target thread is in a wakeable waiting state if the target thread and the source thread are located on the same processor.

[0260] The verification unit is used to verify the execution permissions of the source thread if the target thread is in a wakeable waiting state, in order to generate a security detection result.

[0261] In one embodiment of this application, the thread scheduling device 90 further includes:

[0262] Create a module to generate a weight adjustment factor based on the correlation between the data size of the allocable time period and the historical characteristics of the target thread.

[0263] The first generation module is used to generate the compensatory virtual runtime of the target thread based on the weight adjustment factor.

[0264] The second update module is used to update the virtual runtime of the target thread based on the compensatory virtual runtime of the target thread, and to determine the reclaimable allocable time period based on the updated virtual runtime. The reclaimable allocable time period is used to compensate the source thread for the unused allocable time period.

[0265] In one embodiment of this application, the first update module 905 includes:

[0266] The first acquisition unit is used to acquire the allocatable time and the running time of the target thread recorded in the allocatable time period.

[0267] The third judgment unit is used to determine whether the allocatable time is greater than the preset value.

[0268] The third calculation unit is used to calculate the allocatable time consumed by the target thread based on the target thread's running time and the allocatable time if the allocatable time is greater than a preset value.

[0269] The fourth calculation unit is used to calculate the actual running time based on the running time of the target thread and the allocable time consumed by the target thread.

[0270] The fourth judgment unit is used to determine whether the actual running time is greater than the preset running time.

[0271] The first update unit is used to update the virtual runtime of the target thread according to the actual runtime if the actual runtime is greater than the preset runtime, so as to complete the thread scheduling switch.

[0272] In one embodiment of this application, the thread scheduling device 90 further includes:

[0273] The determination module is used to determine the memory access prediction strategy corresponding to the target thread based on the length of the allocatable time period.

[0274] The second generation module is used to generate a prefetch instruction sequence for the target thread according to the memory access prediction strategy, wherein the prefetch instruction sequence of the target thread is used to instruct the data to be accessed by the target thread to be loaded from main memory into the cache.

[0275] The execution module is used to execute the prefetch instruction sequence and obtain the cache hit rate of the target thread based on the allocable time period consumed by the prefetch instruction sequence.

[0276] The first adjustment module is used to adjust the parameters of the memory access prediction strategy used to generate the prefetch instruction sequence based on the cache hit rate.

[0277] In one embodiment of this application, the security detection module 901 further includes:

[0278] The second acquisition unit is used to acquire the cooperating thread group identifiers of the source thread and the target thread based on the thread control structure of the source thread and the target thread.

[0279] The fifth judgment unit is used to determine whether the source thread and the target thread belong to the same cooperative thread group based on the cooperative thread group identifier of the source thread and the target thread.

[0280] The second generation unit is used to generate a flag prohibiting cross-group switching between the source thread and the target thread if the source thread and the target thread belong to the same cooperating thread group.

[0281] The second update unit is used to update the thread isolation policy based on the prohibition of cross-group switching flags and interface security protocols between the source thread and the target thread.

[0282] In one embodiment of this application, the thread scheduling device 90 further includes:

[0283] The first acquisition module is used to acquire the cache hit rate and branch prediction error rate of the source thread during runtime according to a preset sampling period.

[0284] The second calculation module is used to calculate the adjustment amount of the cache locality constraint based on the cache hit rate and branch prediction error rate.

[0285] The second adjustment module is used to adjust the parameter values ​​of the cache locality constraint in the Completely Fair Scheduler according to the adjustment amount of the cache locality constraint.

[0286] The third calculation module is used to calculate the allocatable time period allocated from the source thread to the target thread based on the adjusted cache locality constraint parameter values ​​and the virtual runtime of the source thread.

[0287] In one embodiment of this application, the second computing module includes:

[0288] The third acquisition unit is used to acquire the instantaneous values ​​of multiple cache hit rates and branch prediction error rates recorded within the sliding window, based on a preset sliding window length.

[0289] The fifth calculation unit is used to calculate the average value of cache hit rate and branch prediction error rate based on the instantaneous values ​​of multiple cache hit rates and branch prediction error rates.

[0290] The output unit is used to input the average of the cache hit rate and the branch prediction error rate into a preset linear regression model and output the initial adjustment.

[0291] The weighted fusion unit is used to weight and fuse the initial adjustment amount and the target parameter values ​​of the cache locality constraints in the Completely Fair Scheduler to generate the adjustment amount of the cache locality constraints.

[0292] In one embodiment of this application, the thread scheduling device 90 further includes:

[0293] The second acquisition module is used to acquire the historical power consumption data of the target thread and the power consumption threshold of the system, wherein the historical power consumption data is read by the thread running monitoring module.

[0294] The fourth calculation module is used to calculate the energy efficiency factor of the target thread based on historical power consumption data and the system's power consumption threshold using a preset energy efficiency model. The energy efficiency model takes historical power consumption data as input and outputs the energy efficiency factor.

[0295] The third generation module is used to generate energy efficiency adjustment coefficients based on energy efficiency factors and system load factors through a weighted fusion algorithm, wherein the weighted fusion algorithm is calculated according to the weight ratio of energy efficiency factors and load factors.

[0296] The fourth generation module is used to generate energy efficiency optimization time periods based on the energy efficiency adjustment coefficient and the allocatable time periods of the target thread.

[0297] The optimization module is used to optimize the virtual runtime of the target thread according to the energy efficiency optimization period and generate energy efficiency optimization logs.

[0298] In one embodiment of this application, the verification unit includes:

[0299] Extract sub-units to extract security domain identifiers and privilege levels from the source thread's thread control structure.

[0300] The query subunit is used to query the system security policy database based on the security domain identifier to obtain a list of target domains that the source thread is allowed to access.

[0301] The judgment subunit is used to determine whether the security domain identifier is in the list of target domains that the source thread is allowed to access.

[0302] The verification subunit is used to verify whether the privilege level of the source thread is higher than the privilege level required to perform the thread switch, if the security domain identifier is in the list of target domains that the source thread is allowed to access.

[0303] The recording subunit is used to record the context switch event and generate a security detection result if the privilege level of the source thread is higher than the privilege level required to perform the thread switch.

[0304] For a description of the features in the embodiment corresponding to the thread scheduling device, please refer to the relevant description of the embodiment corresponding to the thread scheduling method, which will not be repeated here.

[0305] Figure 10 A schematic diagram of the structure of the electronic device provided in this application. Figure 10 As shown, the electronic device 100 provided in this embodiment includes at least one processor 1001 and a memory 1002. Optionally, the electronic device 100 further includes a communication component 1003. The processor 1001, memory 1002, and communication component 1003 are connected via a bus.

[0306] In a specific implementation, at least one processor 1001 executes computer execution instructions stored in memory 1002, causing at least one processor 1001 to execute the above-described thread scheduling method embodiment.

[0307] The specific implementation process of processor 1001 can be found in the above method embodiments, and its implementation principle and technical effect are similar. It will not be repeated here.

[0308] In the above embodiments, it should be understood that the processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in the application can be directly manifested as being executed by a hardware processor, or executed by a combination of hardware and software modules within the processor.

[0309] The memory may include random access memory (RAM) and may also include non-volatile memory (NVM), such as at least one disk storage device.

[0310] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses shown in the accompanying drawings are not limited to a single bus or a single type of bus.

[0311] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above-described thread scheduling method embodiments at runtime.

[0312] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.

[0313] Embodiments of this application also provide a computer program product, which includes a computer program that, when executed by a processor, implements the steps in any of the above-described thread scheduling method embodiments.

[0314] Embodiments of this application also provide another computer program product, including a non-volatile computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps in any of the above-described thread scheduling method embodiments.

[0315] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0316] The thread scheduling method, electronic device, storage medium, and program product provided in this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are only intended to help understand the method and core ideas of this application. It should be noted that those skilled in the art can make several improvements and modifications to this application without departing from the principles of this application, and these improvements and modifications also fall within the protection scope of the claims of this application.

Claims

1. A thread scheduling method, characterized by, The method comprises the following steps: In response to a thread switching request initiated by a source thread, performing security checks on a target thread and the source thread according to a processing function of the thread switching request, and generating a security detection result, wherein the thread switching request is generated after the source thread calls a thread switching interface; If the security detection result is passed, calculating an allocatable time period of the source thread allocated to the target thread; Updating a virtual running time of the source thread according to the allocatable time period, and allocating the allocatable time period to the target thread; Calling a context switching function to perform a thread switching operation on the source thread and the target thread, and switching the thread in a working state from the source thread to the target thread; When the target thread consumes the allocatable time period, updating the virtual running time of the target thread to complete the switching of thread scheduling; The security checks on the target thread and the source thread according to the processing function of the thread switching request and the generation of the security detection result comprise the following steps: Calling the processing function according to the thread switching request to determine whether the target thread and the source thread are located in the same processor; If the target thread and the source thread are located in the same processor, determining whether the target thread is in a wakeable waiting state; If the target thread is in the wakeable waiting state, verifying the execution authority of the source thread to generate the security detection result.

2. The thread scheduling method of claim 1, wherein, If the security detection result is passed, the calculation of the allocatable time period of the source thread allocated to the target thread comprises the following steps: If the security detection result is passed, determining a load factor of a system according to instantaneous load information of the system; Creating a load factor function according to the load factor of the system; Calculating a delay threshold according to the load factor function; Determining a scheduling period according to the number of runnable tasks in a scheduling queue and the delay threshold; Calculating an ideal running time of the source thread according to the scheduling period; Obtaining the allocatable time period of the source thread allocated to the target thread according to the ideal running time of the source thread and the executed time of the source thread.

3. The thread scheduling method of claim 2, wherein, The formula for calculating the delay threshold is: In the formula, represents a delay threshold value; represents a load factor function; represents a scheduling period reference value; represents a minimum scheduling granularity.

4. The thread scheduling method of claim 1, wherein, After the allocatable time period is allocated to the target thread, the method further comprises the following steps: Creating a weight adjustment factor according to the relevance between the data size of the allocatable time period and the historical characteristics of the target thread; Generating a compensatory virtual running time of the target thread according to the weight adjustment factor; Updating the virtual running time of the target thread according to the compensatory virtual running time of the target thread, and determining a recyclable allocatable time period according to the updated virtual running time, wherein the recyclable allocatable time period is used to compensate the unused allocatable time period to the source thread.

5. The thread scheduling method of claim 1, wherein, When the target thread consumes the allocatable time period, the updating of the virtual running time of the target thread to complete the switching of thread scheduling comprises the following steps: Obtaining the allocatable time and the running time of the target thread recorded in the allocatable time period; Determining whether the allocatable time is greater than a preset value; If the allocatable time is greater than a preset value, the allocatable time consumed by the target thread is calculated according to the running time of the target thread and the allocatable time; The actual running time is calculated according to the running time of the target thread and the allocatable time consumed by the target thread; It is judged whether the actual running time is greater than a preset running time; If the actual running time is greater than the preset running time, the virtual running time of the target thread is updated according to the actual running time to complete the switching of thread scheduling.

6. The thread scheduling method of claim 1, wherein, After the thread switching operation is performed on the source thread and the target thread by calling the context switching function, the method further comprises: determining a memory access prediction strategy corresponding to the target thread according to the length of the allocatable time period; generating a prefetch instruction sequence of the target thread according to the memory access prediction strategy, wherein the prefetch instruction sequence of the target thread is used to instruct loading data to be accessed by the target thread from the main memory to the cache; executing the prefetch instruction sequence and obtaining a cache hit rate of the target thread consuming the allocatable time period according to the prefetch instruction sequence; adjusting the parameters of the memory access prediction strategy used to generate the prefetch instruction sequence according to the cache hit rate.

7. The thread scheduling method of claim 1, wherein, Before verifying the execution authority of the source thread to generate a security detection result if the target thread is in a wakeable waiting state, the method further comprises: obtaining the cooperative thread group identifier of the source thread and the target thread according to the thread control structure of the source thread and the target thread; determining whether the source thread and the target thread belong to the same cooperative thread group according to the cooperative thread group identifier of the source thread and the target thread; if the source thread and the target thread belong to the same cooperative thread group, generating a cross-group switching prohibition marker between the source thread and the target thread; updating the thread isolation strategy according to the cross-group switching prohibition marker between the source thread and the target thread and the interface security protocol.

8. The thread scheduling method of claim 1, wherein, Before calculating the allocatable time period of the source thread allocated to the target thread if the security detection result is a detection pass, the method further comprises: obtaining the cache hit rate and the branch prediction error rate of the source thread running at runtime according to a preset sampling period; calculating an adjustment amount of the cache locality constraint condition according to the cache hit rate and the branch prediction error rate; adjusting the parameter value of the cache locality constraint condition in the completely fair scheduler according to the adjustment amount of the cache locality constraint condition; calculating the allocatable time period of the source thread allocated to the target thread according to the adjusted cache locality constraint condition parameter value and the virtual running time of the source thread.

9. The thread scheduling method of claim 8, wherein, The method further comprises: obtaining the instantaneous values of the plurality of cache hit rates and branch prediction error rates recorded in the sliding window according to a preset sliding window length; calculating the average values of the cache hit rate and the branch prediction error rate respectively according to the instantaneous values of the plurality of cache hit rates and branch prediction error rates; inputting the average values of the cache hit rate and the branch prediction error rate into a preset linear regression model to output an initial adjustment amount; The initial adjustment amount and the target parameter value of the cache locality constraint condition in the full fair scheduler are weighted and fused to generate an adjustment amount of the cache locality constraint condition.

10. The thread scheduling method of claim 1, wherein, After the calling context switching function performs a thread switching operation on the source thread and the target thread, the method further includes: obtaining historical power consumption data of the target thread and a power consumption threshold of the system, wherein the historical power consumption data is read by a thread running monitoring module; calculating an energy efficiency factor of the target thread using a preset energy efficiency model according to the historical power consumption data and the power consumption threshold of the system, wherein the energy efficiency model takes the historical power consumption data as input and outputs the energy efficiency factor; generating an energy efficiency adjustment coefficient by a weighted fusion algorithm according to the energy efficiency factor and a load factor of the system, wherein the weighted fusion algorithm calculates according to the weight proportion of the energy efficiency factor and the load factor; generating an energy efficiency optimization time period according to the energy efficiency adjustment coefficient and an allocable time period of the target thread; optimizing a virtual running time of the target thread according to the energy efficiency optimization time period, and generating an energy efficiency optimization log.

11. The thread scheduling method of claim 1, wherein, If the target thread is in a wakeable waiting state, verifying an execution authority of the source thread to generate a security detection result, including: extracting a security domain identifier and a privilege level from a thread control structure of the source thread; querying a system security policy database according to the security domain identifier to obtain a target domain list that the source thread is allowed to access; determining whether the security domain identifier is located in the target domain list that the source thread is allowed to access; if the security domain identifier is located in the target domain list that the source thread is allowed to access, verifying whether the privilege level of the source thread is higher than a privilege level required for performing thread switching; if the privilege level of the source thread is higher than the privilege level required for performing thread switching, recording a context switching event, and generating a security detection result.

12. An electronic device, comprising: The method comprises: a memory for storing a computer program; a processor for executing the computer program to implement the steps of the thread scheduling method according to any one of claims 1 to 11.

Citation Information

Patent Citations

  • Thread scheduling method and device, storage medium and electronic equipment

    CN111831409A

  • Task scheduling method and device, electronic equipment and storage medium

    CN115658277A