Method, device and electronic device for lock wait queue ordering
Patent Information
- Application Number
- CN202211104646.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-09
- Publication Date
- 2026-10-30
- Estimated Expiration
- 2042-09-09
AI Technical Summary
但是,相关技术中,存在由于锁等待导致电子设备上应用卡顿的问题
[0008]本申请实施例提供的锁等待队列排序的方法、装置、电子设备以及存储介质,通过在检测到目标锁放锁的情况下,确定等待目标锁的初始等待队列包括的多个任务中各任务的预测持锁时间;若各任务的预测持锁时间满足预设条件,则调整初始等待队列包括的多个任务的排列顺序,获得目标等待队列,其中,目标等待队列包括的多个任务按目标顺序排列;按目标顺序,唤醒多个任务持目标锁。本申请通过预测目标锁等待队列中任务持锁的运行时间,根据任务的持锁运行时间调整等待队列中任务的运行顺序,提高了持锁吞吐量,也提高了电子设备执行任务的流畅性。
Smart Images

Figure CN115495211B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a method, apparatus, and electronic device for sorting a lock waiting queue. Background Technology
[0002] With the rapid development of science and technology, consumers' demands for electronic devices are no longer limited to simple functions, but also include the ability to smoothly use multiple functions. However, some technologies suffer from lag issues on electronic devices due to lock-and-wait conditions. Summary of the Invention
[0003] In view of the above problems, this application proposes a method, apparatus and electronic device for sorting lock waiting queues. By predicting the lock holding time of tasks in the lock waiting queue, the execution order of tasks in the waiting queue is adjusted according to the predicted lock holding time, thereby improving the lock holding throughput and the smoothness of task execution by the electronic device.
[0004] In a first aspect, embodiments of this application provide a method for sorting a lock waiting queue. The method includes: when a target lock is detected to be released, determining the predicted lock holding time of each task among multiple tasks in an initial waiting queue waiting for the target lock; if the predicted lock holding time of each task meets a preset condition, adjusting the arrangement order of the multiple tasks in the initial waiting queue to obtain a target waiting queue, wherein the multiple tasks in the target waiting queue are arranged in a target order; and waking up the multiple tasks to hold the target lock according to the target order.
[0005] Secondly, embodiments of this application provide an apparatus for sorting a lock waiting queue, the apparatus comprising: a predicted lock holding time determination module, a sequence adjustment module, and a wake-up module. The predicted lock holding time determination module is used to determine the predicted lock holding time of each task in an initial waiting queue awaiting the target lock when a target lock is detected to be released. The sequence adjustment module is used to adjust the order of the tasks in the initial waiting queue to obtain a target waiting queue if the predicted lock holding times of each task meet preset conditions, wherein the tasks in the target waiting queue are arranged in a target order. The wake-up module is used to wake up the tasks to hold the target lock according to the target order.
[0006] Thirdly, embodiments of this application provide an electronic device, including a memory and a processor, wherein the memory is coupled to the processor, the memory stores instructions, and when the instructions are executed by the processor, the processor performs the above-described method.
[0007] Fourthly, embodiments of this application provide a computer-readable storage medium storing program code, which can be invoked by a processor to execute the above-described method.
[0008] The lock waiting queue sorting method, apparatus, electronic device, and storage medium provided in this application, when detecting that a target lock has been released, determine the predicted lock-holding time of each task in the initial waiting queue. If the predicted lock-holding time of each task meets a preset condition, the order of the tasks in the initial waiting queue is adjusted to obtain a target waiting queue, wherein the tasks in the target waiting queue are arranged in a target order. The tasks are then woken up to hold the target lock according to the target order. This application improves lock-holding throughput and the smoothness of task execution by predicting the lock-holding time of tasks in the target lock waiting queue and adjusting the execution order of tasks in the waiting queue based on the lock-holding time. Attached Figure Description
[0009] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying 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.
[0010] Figure 1 A schematic diagram of the sleep lock wake-up task provided in an embodiment of this application is shown;
[0011] Figure 2 A flowchart illustrating a lock waiting queue sorting method according to an embodiment of this application is shown;
[0012] Figure 3 A flowchart illustrating a lock waiting queue sorting method according to an embodiment of this application is shown;
[0013] Figure 4 A flowchart illustrating a lock waiting queue sorting method according to an embodiment of this application is shown;
[0014] Figure 5 A block diagram of a lock waiting queue sorting apparatus according to an embodiment of this application is shown;
[0015] Figure 6 A block diagram of an electronic device for performing a lock waiting queue sorting method according to an embodiment of the present application is shown;
[0016] Figure 7A storage unit for storing or carrying program code implementing a lock waiting queue sorting method according to an embodiment of the present application is shown. Detailed Implementation
[0017] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings.
[0018] With the rapid development of science and technology, consumers' demands for electronic devices are no longer limited to simple functions, but include the smooth use of multiple functions. As electronic devices become increasingly powerful, various ultra-large applications have emerged. These applications place higher demands on the system resources of electronic devices, leading to a greater need for the protection and synchronization of shared resources. Currently, application lag on electronic devices is becoming increasingly severe due to lockout issues, and in more serious cases, it can even cause system crashes.
[0019] In related technologies, locks in operating systems of electronic devices are generally divided into two categories: spin locks and sleep locks. When a task in an electronic device needs to hold a spin lock, if it finds that the lock is already held by another task, the task will continue spinning, meaning the central processing unit (CPU) of the electronic device continuously runs and waits, instructing the lock holder to release the lock. Spin locks are generally used in scenarios where shared resource access time is short, or when the task waiting for the lock is in a situation where it cannot enter sleep mode, such as during an interrupt, thus limiting their applicability. When a task in an electronic device needs to hold a sleep lock, if the lock is already held by another task, the task will relinquish the CPU and enter sleep mode until the lock holder releases the lock, at which point the CPU will attempt to wake it up. Because sleep locks avoid excessive processor load caused by excessive spinning, sleep locks are widely used in operating systems of electronic devices in related technologies.
[0020] Please see Figure 1 , Figure 1 The diagram illustrates the process of waking up a task from a sleep lock in related technologies. In these technologies, for sleep locks, when a task holds the lock, the operating system in an electronic device can control multiple threads requesting the lock to sleep, i.e., the task requests the lock and waits; and these multiple threads are added to the waiting queue corresponding to the lock, i.e., added to the lock waiting queue. When the lock holder releases the lock, the threads in the waiting queue are woken up according to the timing of their addition to the queue, i.e., the lock-holding task releases the lock, waking up tasks in the waiting queue in a first-in-first-out order. In other words, if multiple threads request the same lock, these multiple threads are controlled to sleep and added to the waiting queue corresponding to the lock; when the lock holder releases the lock, the threads in the waiting queue are woken up according to the timing of their addition to the queue.
[0021] For example, after a task in the waiting queue acquires a lock, multiple threads may request to acquire the lock and access the shared resource at different times. If there are 10 threads in the waiting queue, with the first thread accessing the resource for 1 second and the remaining 9 threads accessing it for a total of 0.1 seconds, in related technologies, the operating system of the electronic device will first wake up the first thread, making the remaining 9 threads wait for 1 second before running. That is, in related technologies, the Linux system of the electronic device only determines which thread to wake up based on the time it has been added to the waiting queue.
[0022] It is understandable that throughput refers to the number of requests a system can process per unit of time. Therefore, in related technologies, the low throughput of electronic devices with locks can cause application lag on electronic devices due to lock waiting.
[0023] To address the aforementioned problems, the inventors, through long-term research, discovered and proposed a method, apparatus, electronic device, and storage medium for sorting lock waiting queues, as provided in the embodiments of this application. By predicting the lock holding time of tasks in the target lock waiting queue and adjusting the running order of tasks in the waiting queue according to the lock holding time, the lock holding throughput is improved, and the smoothness of task execution by the electronic device is also improved.
[0024] Please see Figure 2 , Figure 2 A flowchart illustrating a lock waiting queue sorting method according to an embodiment of this application is shown. This lock waiting queue sorting method improves lock throughput and enhances the smoothness of task execution by predicting the lock holding execution time of tasks in the target lock waiting queue and adjusting the execution order of tasks in the waiting queue based on the lock holding execution time. In specific embodiments, this lock waiting queue sorting method can be applied to, for example... Figure 5 The lock waiting queue sorting device 200 shown and the electronic device 100 configured with the lock waiting queue sorting device 200 are shown. Figure 6 The following will use an electronic device as an example to illustrate the specific process of this embodiment. Of course, it is understood that the electronic device used in this embodiment may include smartphones, tablets, wearable electronic devices, vehicles, etc., and is not limited thereto. The following will focus on... Figure 2 The process shown will be described in detail. The method for sorting the lock waiting queue may specifically include the following steps:
[0025] Step S110: If the target lock is detected to be released, determine the predicted lock holding time of each task in the initial waiting queue for the target lock.
[0026] In some embodiments, the lock waiting queue sorting method provided in this application can be implemented in software, integrated into the system software package of an electronic device, and executed by the operating system of the electronic device. The target lock is the lock corresponding to the electronic device executing the lock waiting queue sorting method of this application.
[0027] In some implementations, the operating system within an electronic device can detect the execution status of requests made by applications within the electronic device. Furthermore, the electronic device can use the operating system to detect the locking and unlocking behavior of tasks within the electronic device on a target lock.
[0028] In one implementation, the operating system in the electronic device may detect the release of the target lock by receiving a release notification from the task holding the target lock.
[0029] As another implementation, the operating system in the electronic device can detect the release of the target lock by detecting whether the thread of the task holding the target lock holds the lock. If it detects that the thread of the task does not hold the lock, it determines that the target lock has been released. In this embodiment, the method by which the electronic device detects the release of the target lock is not limited.
[0030] Understandably, when multiple threads request the same lock (target lock) for a sleep lock, the operating system of the electronic device can control these threads to sleep while the target lock is held, and add them to the waiting queue (i.e., the initial waiting queue) corresponding to the target lock. This reduces the energy consumption of the electronic device, saves resources, and ensures that the operating system can smoothly execute the tasks corresponding to each thread.
[0031] Furthermore, when the electronic device detects that the target lock has been released, it can determine the predicted holding time of each task among the multiple tasks included in the initial waiting queue for the target lock. Specifically, the electronic device can determine the predicted holding time of each task in the initial waiting queue by obtaining a historical holding time of each task from a related cloud or electronic device via wireless communication technology (e.g., WiFi, Bluetooth, Zigbee, etc.), obtaining a historical holding time of each task from a related electronic device via a serial communication interface (e.g., a serial peripheral interface, etc.), or determining a historical holding time from multiple historical holding times of each task stored in the electronic device. In this embodiment, the method by which the electronic device determines the predicted holding time of each task in the initial waiting queue for the target lock is not limited.
[0032] Step S120: If the predicted lock holding time of each task meets the preset conditions, the order of the multiple tasks included in the initial waiting queue is adjusted to obtain the target waiting queue, wherein the multiple tasks included in the target waiting queue are arranged in the target order.
[0033] In some implementations, the electronic device may have preset conditions, which can serve as the basis for determining whether to adjust the order of tasks in the initial waiting queue. If the predicted holding time of each task meets the preset conditions, the order of multiple tasks in the initial waiting queue is adjusted to obtain the target waiting queue; if the predicted holding time of each task does not meet the preset conditions, the order of multiple tasks in the initial waiting queue is not adjusted, and the initial waiting queue is used as the target waiting queue.
[0034] Optionally, the preset condition can be that the predicted lock holding time of each task in the initial waiting queue is greater than the lock holding time threshold, or it can be that the predicted lock holding time of a preset number of tasks in the initial waiting queue is greater than the lock holding time threshold. The electronic device can pre-set the lock holding time threshold and the preset number. In this embodiment, the content of the preset condition is not limited.
[0035] The target waiting queue includes multiple tasks arranged in target order. Optionally, the target arrangement order can be from shortest to longest time; alternatively, tasks with predicted lock holding times less than or equal to a lock holding time threshold can be arranged in the order of their lock request times, while tasks with predicted lock holding times greater than the lock holding time threshold can be arranged from shortest to longest time, following the tasks with predicted lock holding times less than or equal to the lock holding time threshold. In this embodiment, the target arrangement order is not limited.
[0036] In one optional implementation provided in this application, in order to improve lock holding throughput and enable more tasks to complete lock access to resources within the same time period, the target arrangement order can be arranged in ascending order of time to improve lock holding throughput.
[0037] Step S130: Wake up the multiple tasks holding the target lock according to the target sequence.
[0038] In some implementations, the operating system of an electronic device can, after obtaining the target waiting queue, wake up each task holding the target lock in the target waiting queue in the target order.
[0039] Understandably, with sleep locks, a task will go to sleep when the lock it requests is held by another task. After that other task releases the lock, the electronic device can wake up multiple tasks in the target waiting queue to hold the lock in the target order. This improves the lock-holding throughput of the electronic device while ensuring the smooth execution of applications. Each task holding the target lock can perform resource access, memory operations, I / O manipulation, hard floating-point operations, etc., on the electronic device, which are not limited here.
[0040] An embodiment of this application provides a method for sorting a lock waiting queue. Upon detecting that a target lock has been released, the method determines the predicted lock-holding time of each task in the initial waiting queue. If the predicted lock-holding time of each task meets a preset condition, the method adjusts the order of the tasks in the initial waiting queue to obtain a target waiting queue, wherein the tasks in the target waiting queue are arranged in a target order. Then, multiple tasks are woken up to hold the target lock according to the target order. This application improves lock-holding throughput and enhances the smoothness of task execution by predicting the lock-holding time of tasks in the target lock waiting queue and adjusting the execution order of tasks in the waiting queue based on the lock-holding time.
[0041] Please see Figure 3 , Figure 3 A flowchart illustrating a lock waiting queue sorting method according to an embodiment of this application is shown. This method is applied to the aforementioned electronic device, and will be discussed below. Figure 3 The process shown will be described in detail. The method for sorting the lock waiting queue may specifically include the following steps:
[0042] Step S210: If the target lock is detected to be released, determine the predicted lock holding time of each task in the initial waiting queue for the target lock.
[0043] In some implementations, if the operating system in the electronic device receives a release notification from the task holding the target lock, it confirms that the electronic device has detected the target lock being released. Furthermore, the electronic device can perform an operation to determine the predicted lock-holding time for each of the multiple tasks included in the initial waiting queue for the target lock.
[0044] In some implementations, for each of the multiple tasks included in the initial waiting queue, the electronic device may determine the predicted lock holding time for each of the multiple tasks included in the initial waiting queue for the target lock, which may include steps S211-S213.
[0045] Step S211: Determine the historical average holding time of the target lock for the task.
[0046] Optionally, the electronic device can record the holding time of each task on the target lock in real time and save it to the device's memory, improving the device's operating speed; alternatively, it can record the holding time of each task on the target lock in real time and save it to a related cloud or electronic device, saving storage space. Specifically, the holding time of each task can be calculated by recording the start and end times of each task's entry into the target lock.
[0047] In one implementation, the electronic device can determine the historical average holding time of each task on the target lock among the multiple tasks included in the initial waiting queue by recording and pre-storing multiple historical holding times of each task in the multiple tasks included in the initial waiting queue in real time. The electronic device can directly determine the historical average holding time of the target lock of the task based on the pre-stored multiple holding times of each task.
[0048] As another implementation, the associated cloud or electronic device stores multiple historical lock holding times for each task in the initial waiting queue. The electronic device can also obtain multiple historical lock holding times for each task from the associated cloud or electronic device through wireless communication technology, and determine the historical average lock holding time of the target lock for the task based on the multiple lock holding times of each task.
[0049] In another implementation, the associated cloud or electronic device stores multiple historical lock-holding times for each task in the initial waiting queue, and the associated cloud or electronic device can calculate the historical average lock-holding time for each task based on the real-time recorded lock-holding times of each task. The electronic device can directly obtain the historical average lock-holding time of the task on the target lock from the associated cloud or electronic device via wireless communication technology, reducing the computational load and power consumption of the electronic device.
[0050] Step S212: Determine the first holding time and the second holding time of the task on the target lock, wherein the first holding time is the most recent holding time of the task on the target lock, and the second holding time is the penultimate holding time of the task on the target lock.
[0051] In some implementations, the electronic device may record the historical lock holding time of each of the multiple tasks included in the initial waiting queue. The electronic device may determine the most recent lock holding time of each task on the target lock as the first lock holding time from the multiple historical lock holding times of each task, and determine the second to last lock holding time of each task on the target lock as the second lock holding time.
[0052] In some implementations, the electronic device may also determine the most recent holding time of each task on the target lock as the first holding time, and determine the penultimate holding time of each task on the target lock as the second holding time, from multiple historical holding times of each task recorded by the associated cloud or electronic device.
[0053] Step S213: Determine a lock holding time from the historical average lock holding time, the first lock holding time, and the second lock holding time as the predicted lock holding time for the task.
[0054] In some implementations, after obtaining the historical average lock-holding time, the first lock-holding time, and the second lock-holding time for each task, the electronic device can determine the historical average lock-holding time of each task as the predicted lock-holding time for that task; it can also determine the first lock-holding time of each task as the predicted lock-holding time for that task; it can also determine the second lock-holding time of each task as the predicted lock-holding time for that task; or it can determine the average, median, or arithmetic mean of the historical average lock-holding time, the first lock-holding time, and the second lock-holding time as the predicted lock-holding time for that task. In the embodiments of this application, the method of determining a lock-holding time from the historical average lock-holding time, the first lock-holding time, and the second lock-holding time as the predicted lock-holding time for the task is not limited here.
[0055] In some implementations, step S212 may include step S212A.
[0056] Step S212A: If the first lock holding time is greater than the historical average lock holding time, and the second lock holding time is greater than the historical average lock holding time, then the historical average lock holding time is determined as the predicted lock holding time of the task.
[0057] In some implementations, when the electronic device determines that the first lock holding time of a task is greater than the historical average lock holding time and the second lock holding time is greater than the historical average lock holding time, it can determine that the next lock holding time of the task on the target lock is equal to or greater than the historical average lock holding time of the task, and then the historical average lock holding time of the task can be determined as the predicted lock holding time of the task.
[0058] In some other embodiments, step S212 may include steps S212B1-S212B3.
[0059] Step S212B1: If the first lock holding time is less than the historical average lock holding time, and the second lock holding time is less than the historical average lock holding time, then calculate the first difference between the first lock holding time and the historical average lock holding time, and calculate the second difference between the second lock holding time and the historical average lock holding time.
[0060] In some implementations, the electronic device compares the historical average lock-holding time of a task with the first lock-holding time and the second lock-holding time of the task. If the first lock-holding time is less than the historical average lock-holding time and the second lock-holding time is less than the historical average lock-holding time, then a first difference between the first lock-holding time and the historical average lock-holding time is further calculated, and a second difference between the second lock-holding time and the historical average lock-holding time is calculated.
[0061] Step S212B2: If the first difference is greater than a preset difference and the second difference is greater than the preset difference, then determine the larger difference between the first difference and the second difference as the target difference.
[0062] In some implementations, after obtaining a first difference between a first holding time and a historical average holding time, and a second difference between a second holding time and a historical average holding time, the electronic device can compare the first and second differences with a preset difference. The preset difference can be used as a criterion for determining the reliability of the historical average holding time of a task. If both the first and second differences are greater than the preset difference, it can be determined that the next holding time for the task is very short, and therefore the historical average holding time of the task is unreliable. If either the first or second difference is less than or equal to the preset difference, then the historical average holding time of the task is determined to be reliable, and the electronic device can use the historical average holding time of the task as the predicted holding time for that task.
[0063] The electronic device can have a preset difference value set in advance; if the first difference value of the task is greater than the preset difference value, and the second difference value is greater than the preset difference value, the electronic device can further determine the larger difference value between the first difference value and the second difference value of the task as the target difference value.
[0064] Step S212B3: Determine the lock holding time corresponding to the target difference as the predicted lock holding time corresponding to the task.
[0065] In some implementations, after the electronic device determines the larger difference between the first and second differences of the task as the target difference, it can determine the lock-holding time corresponding to the target difference as the predicted lock-holding time for the task. The lock-holding time corresponding to the target difference can be either the first lock-holding time or the second lock-holding time.
[0066] Understandably, to improve lock holding throughput, this application embodiment predicts the lock holding time of tasks and wakes up tasks with longer predicted lock holding times later. The lock holding time of a task is related to the resource status of the electronic device's operating system; when system resources are strained, the lock holding time will also increase. Therefore, considering that resource strain in the electronic device may raise the average lock holding time, this application determines the most recent lock holding time (first lock holding time) and the penultimate lock holding time (second lock holding time). When the last two lock holding times of a task are less than the historical average lock holding time of that task, it can be determined that the previous lock holding times of that task were too long, raising the historical average lock holding time. Therefore, it is assumed that the next lock holding time for that task will be very short. Using the historical average lock holding time as the predicted lock holding time for that task would result in low accuracy. To avoid affecting the wake-up of the lock-waiting process for that task, this application determines the minimum of the first and second lock holding times as the predicted lock holding time. That is, the lock holding time corresponding to the target difference is determined as the predicted lock holding time for the task, so as to improve the accuracy of predicting the lock holding time of the task.
[0067] Step S220: Determine a preset proportion of tasks from the multiple tasks included in the initial waiting queue.
[0068] In some implementations, the electronic device determines a preset proportion of tasks from the multiple tasks included in the initial waiting queue. This can be done by determining the preset proportion of tasks from the multiple tasks included in the initial waiting queue in descending order of predicted lock holding time; or by the electronic device randomly determining the preset proportion of tasks from the multiple tasks included in the initial waiting queue. The preset proportion may be pre-set in the electronic device.
[0069] For example, the electronic device has a preset ratio of 30%, and the initial waiting queue of the target lock includes 10 tasks. The electronic device determines the preset ratio of tasks from the multiple tasks included in the initial waiting queue, that is, the electronic device determines 3 (10 × 30%) tasks from the 10 tasks.
[0070] Understandably, to improve lock holding throughput, embodiments of this application predict the lock holding time of tasks and wake up tasks with longer predicted lock holding times later. In an optional implementation of this application, to reduce the computational load and power consumption of the electronic device, the electronic device can determine a preset proportion of tasks from the multiple tasks included in the initial waiting queue, in descending order of predicted lock holding times.
[0071] Step S230: Based on the predicted lock holding time of each task in the preset proportion of tasks, determine the sum of the predicted lock holding times of the preset proportion of tasks, and use it as the first predicted lock holding time.
[0072] The electronic device can determine the sum of the predicted lock-holding times of the preset proportion of tasks based on the predicted lock-holding times of each task in the preset proportion of tasks, and use this sum as the first predicted lock-holding time.
[0073] Step S240: Based on the predicted lock holding time of each of the multiple tasks, determine the sum of the predicted lock holding times of the multiple tasks, and use it as the second predicted lock holding time.
[0074] The electronic device can determine the sum of the predicted holding times of multiple tasks based on the predicted holding times of each task, and use this sum as the second predicted holding time.
[0075] Step S250: If the ratio of the first predicted lock holding time to the second predicted lock holding time is greater than the ratio threshold, then adjust the order of the multiple tasks included in the initial waiting queue to obtain the target waiting queue.
[0076] In some implementations, a ratio threshold is preset in the electronic device. After obtaining the first and second predicted lock holding times of a task, the electronic device can calculate the ratio of the first and second predicted lock holding times and compare this ratio with the ratio threshold. The ratio threshold can be used as a basis for determining whether to adjust the order of multiple tasks included in the initial waiting queue. If the ratio of the first and second predicted lock holding times is greater than the ratio threshold, the order of multiple tasks included in the initial waiting queue is adjusted to obtain the target waiting queue. If the ratio of the first and second predicted lock holding times is less than or equal to the ratio threshold, the order of multiple tasks included in the initial waiting queue is not adjusted, and the initial waiting queue is determined as the target waiting queue.
[0077] In one implementable method provided in the embodiments of this application, when the predicted lock holding times of multiple tasks included in the initial waiting queue are all short, in order to reduce the computational load and power consumption of the electronic device, the embodiments of this application can determine the initial waiting queue as the target waiting queue when the ratio of the first predicted lock holding time and the second predicted lock holding time is less than or equal to a proportional threshold, so that the electronic device wakes up multiple tasks to hold the target lock based on the target order of the target waiting queue.
[0078] In some implementations, the process of adjusting the order of multiple tasks included in the initial waiting queue to obtain the target waiting queue may include step S250A.
[0079] Step S250A: Adjust the order of multiple tasks included in the initial waiting queue according to the predicted lock holding time from shortest to longest to obtain the target waiting queue.
[0080] In some implementations, to improve the lock-holding throughput of the electronic device and enhance the smoothness of task execution, the electronic device can adjust the order of multiple tasks in the initial waiting queue according to the predicted lock-holding time, from shortest to longest, to obtain a target waiting queue. In other words, tasks with longer predicted lock-holding times are woken up later.
[0081] For example, the initial waiting queue of the target lock includes 10 task threads. The predicted lock holding time of the thread at the head of the initial waiting queue is 1 second, and the total predicted lock holding time of the remaining 9 task threads is 0.1 seconds. The electronic device adjusts the order of the multiple tasks in the initial waiting queue according to the predicted lock holding time from shortest to longest to obtain the target waiting queue. That is, the remaining 9 threads in the initial waiting queue run first, and then the first thread with a predicted lock holding time of 1 second is woken up, so that the lock holding process of the 9 tasks can be completed in 0.1 seconds, improving lock holding throughput and making the execution of electronic device applications smoother.
[0082] In other embodiments, the electronic device adjusts the order of multiple tasks included in the initial waiting queue to obtain the target waiting queue, and the process may include steps S250B1-S250B3.
[0083] Step S250B1: Strip the core task from the multiple tasks included in the initial waiting queue to obtain the first waiting queue.
[0084] In some implementations, the initial waiting queue of the target lock includes core services of the electronic device's operating system, such as fixed scheduling tasks in a server. To avoid operating system anomalies, the electronic device can extract core tasks from the initial waiting queue for priority execution and obtain a first waiting queue for adjusting the subsequent task sequence.
[0085] Step S250B2: Adjust the order of tasks in the first waiting queue according to the predicted lock holding time from shortest to longest to obtain the second waiting queue.
[0086] In some implementations, after obtaining the first waiting queue, the electronic device can adjust the order of tasks in the first waiting queue according to the predicted lock holding time from shortest to longest to obtain a second waiting queue, so as to wake up tasks with longer predicted lock holding times later, thereby improving lock holding throughput while avoiding system anomalies.
[0087] Step S250B3: Arrange the core task before the second waiting queue to obtain the target waiting queue.
[0088] In some implementations, after the electronic device obtains the second waiting queue, it can arrange the core tasks stripped from the initial waiting queue of the target lock before the second waiting queue to obtain the target waiting queue.
[0089] In some implementations, the core tasks stripped from the initial waiting queue may include one or more tasks. The electronic device may adjust the order of multiple core tasks stripped from the initial waiting queue according to the predicted lock holding time from shortest to longest to obtain a core task queue; after obtaining the second waiting queue and the core task queue, the electronic device may arrange the core task queue before the second waiting queue to obtain the target waiting queue.
[0090] In other implementations, the electronic device can adjust the order of the core tasks according to the order in which the core tasks request the lock, and obtain a core task queue; after obtaining the second waiting queue and the core task queue, the electronic device can arrange the core task queue before the second waiting queue to obtain the target waiting queue.
[0091] Step S260: Wake up the multiple tasks holding the target lock according to the target sequence.
[0092] In the application embodiment, after the electronic device obtains the target waiting queue, it can wake up multiple task-holding target locks in the target order.
[0093] For example, please refer to Figure 4 , Figure 4 This illustration shows a flowchart of a lock waiting queue sorting method according to an embodiment of this application. When a target lock is held by another task, the electronic device adds the task requesting the target lock to the initial waiting queue of the target lock. After receiving a notification from the lock-holding task to release the target lock, the electronic device calculates the predicted holding times of the multiple tasks currently included in the initial waiting queue of the target lock.
[0094] Furthermore, the electronic device can determine a preset proportion of tasks from the multiple tasks included in the initial waiting queue; based on the predicted lock holding time of each task in the preset proportion of tasks, determine the sum of the predicted lock holding times of the preset proportion of tasks as the first predicted lock holding time; based on the predicted lock holding time of each task in the multiple tasks, determine the sum of the predicted lock holding times of the multiple tasks as the second predicted lock holding time.
[0095] Specifically, if the ratio of the first predicted lock holding time to the second predicted lock holding time is greater than a threshold, then the combined predicted lock holding time of a small number of tasks exceeds the "threshold proportion" of the predicted lock holding time of all tasks in the initial waiting queue. These small number of tasks are then set to be woken up later, meaning they occupy a significant portion of the predicted lock holding time. Furthermore, before waking up these small number of tasks later, it can be determined whether they are core tasks to prevent important core tasks from being woken up later, which could cause system malfunctions. After determining that the initial waiting queue includes core tasks, the electronic device can extract the core tasks from the multiple tasks included in the initial waiting queue to obtain the first waiting queue; adjust the order of tasks in the first waiting queue according to the predicted lock holding time from shortest to longest to obtain the second waiting queue; and place the core tasks before the second waiting queue to obtain the target waiting queue (extracting system core tasks, with remaining tasks woken up later). Finally, the electronic device wakes up multiple tasks in the target order to hold the target lock.
[0096] In some implementations, if the ratio of the first predicted lock holding time to the second predicted lock holding time is less than or equal to a proportional threshold, that is, a small number of tasks occupy a large number of predicted lock holding times, then it is confirmed that the time of each task in the initial waiting queue is very uniform. The electronic device can directly determine the initial waiting queue as the target waiting queue and wake up multiple tasks to hold the target lock according to the target order of the target waiting queue.
[0097] The technical solutions provided in this application are compared to... Figure 2 The lock waiting queue sorting method shown in this embodiment can further determine a preset proportion of tasks from the multiple tasks included in the initial waiting queue. Based on the predicted lock holding time of each task in the preset proportion, the sum of the predicted lock holding times of the preset proportion of tasks is determined as the first predicted lock holding time. Based on the predicted lock holding times of each task in the multiple tasks, the sum of the predicted lock holding times of the multiple tasks is determined as the second predicted lock holding time. If the ratio of the first predicted lock holding time and the second predicted lock holding time is greater than a proportion threshold, the arrangement order of the multiple tasks included in the initial waiting queue is adjusted to obtain the target waiting queue. While improving the accuracy of predicting task lock holding times, by predicting the lock holding execution time of tasks in the target lock waiting queue, the execution order of tasks in the waiting queue is adjusted according to the lock holding execution time of tasks, thereby improving lock holding throughput and the smoothness of electronic devices executing applications.
[0098] Please see Figure 5 , Figure 5 A block diagram of a lock waiting queue sorting apparatus according to an embodiment of this application is shown. This lock waiting queue sorting apparatus 200 is applied to the aforementioned electronic device, and will be discussed below. Figure 5The process is described in detail below. The lock waiting queue sorting device 200 includes: a lock holding time prediction and determination module 210, a sequence adjustment module 220, and a wake-up module 230, wherein:
[0099] The predicted lock holding time determination module 210 is used to determine the predicted lock holding time of each task among the multiple tasks included in the initial waiting queue waiting for the target lock when the target lock is detected to be released.
[0100] The sequence adjustment module 220 is used to adjust the order of multiple tasks included in the initial waiting queue to obtain a target waiting queue if the predicted holding time of each task meets the preset conditions, wherein the multiple tasks included in the target waiting queue are arranged in a target order.
[0101] The wake-up module 230 is used to wake up the multiple tasks holding the target lock according to the target sequence.
[0102] Further, the sequence adjustment module 220 includes: a preset task determination module, a first predicted lock holding time determination module, a second predicted lock holding time determination module, and a sequence adjustment submodule, wherein:
[0103] The preset task determination module is used to determine a preset proportion of tasks from the multiple tasks included in the initial waiting queue.
[0104] The first predicted lock holding time determination module is used to determine the sum of the predicted lock holding times of the preset proportion of tasks based on the predicted lock holding times of each task in the preset proportion of tasks, and use this sum as the first predicted lock holding time.
[0105] The second predicted lock holding time determination module is used to determine the sum of the predicted lock holding times of the multiple tasks based on the predicted lock holding times of each task, and use this sum as the second predicted lock holding time.
[0106] The sequence adjustment submodule is used to adjust the order of multiple tasks included in the initial waiting queue to obtain the target waiting queue if the ratio of the first predicted lock holding time to the second predicted lock holding time is greater than a proportional threshold.
[0107] Furthermore, the preset task determination module includes: a preset task determination submodule, wherein:
[0108] The preset task determination submodule is used to determine the preset proportion of tasks from the multiple tasks included in the initial waiting queue, in descending order of predicted lock holding time.
[0109] Further, the sequence adjustment submodule includes: a sequence adjustment first module, wherein:
[0110] The sequence adjustment first module is used to adjust the order of multiple tasks included in the initial waiting queue according to the predicted lock holding time from shortest to longest, so as to obtain the target waiting queue.
[0111] Furthermore, the sequence adjustment module 220 further includes: a second sequence adjustment module, wherein:
[0112] The second sequence adjustment module is used to determine the initial waiting queue as the target waiting queue if the ratio of the first predicted lock-holding time to the second predicted lock-holding time is less than or equal to the ratio threshold.
[0113] Furthermore, the sequence adjustment module 220 further includes: a first queue acquisition module, a second queue acquisition module, and a third sequence adjustment module, wherein:
[0114] The first queue acquisition module is used to extract the core task from the multiple tasks included in the initial waiting queue to obtain the first waiting queue.
[0115] The second queue acquisition module is used to adjust the order of tasks in the first waiting queue according to the predicted lock holding time from shortest to longest, so as to obtain the second waiting queue.
[0116] The third module for sequence adjustment is used to arrange the core task before the second waiting queue to obtain the target waiting queue.
[0117] Furthermore, for each of the plurality of tasks, the predicted lock holding time determination module 210 includes: a historical average lock holding time determination module, a first lock holding time and a second lock holding time determination module, and a predicted lock holding time determination submodule, wherein:
[0118] The historical average lock holding time determination module is used to determine the historical average lock holding time of the task on the target lock.
[0119] The first holding time and second holding time determination module is used to determine the first holding time and the second holding time of the task on the target lock, wherein the first holding time is the most recent holding time of the task on the target lock, and the second holding time is the penultimate holding time of the task on the target lock.
[0120] The predicted lock holding time determination submodule is used to determine a lock holding time as the predicted lock holding time of the task from the historical average lock holding time, the first lock holding time, and the second lock holding time.
[0121] Furthermore, the predicted holding time determination submodule further includes: a predicted holding time determination first module, wherein:
[0122] The first module for predicting lock holding time is used to determine the historical average lock holding time as the predicted lock holding time of the task if the first lock holding time is greater than the historical average lock holding time and the second lock holding time is greater than the historical average lock holding time.
[0123] Furthermore, the predicted holding time determination submodule also includes: a difference determination module, a target difference determination module, and a second predicted holding time determination module, wherein:
[0124] The difference determination module is used to calculate a first difference between the first lock holding time and the historical average lock holding time, and to calculate a second difference between the second lock holding time and the historical average lock holding time, if the first lock holding time is less than the historical average lock holding time and the second lock holding time is less than the historical average lock holding time.
[0125] The target difference determination module is used to determine the larger difference between the first difference and the second difference as the target difference if the first difference is greater than a preset difference and the second difference is greater than the preset difference.
[0126] The second module for predicting lock holding time is used to determine the lock holding time corresponding to the target difference as the predicted lock holding time corresponding to the task.
[0127] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the above-described device and module can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0128] In the several embodiments provided in this application, the coupling between modules can be electrical, mechanical, or other forms of coupling.
[0129] Furthermore, the functional modules in the various embodiments of this application can be integrated into one processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The integrated modules described above can be implemented in hardware or as software functional modules.
[0130] Please see Figure 6This document illustrates a structural block diagram of an electronic device 100 provided in an embodiment of this application. The electronic device 100 can be a smartphone, tablet computer, e-reader, or other electronic device capable of running applications. The electronic device 100 in this application may include one or more of the following components: a processor 110, a memory 120, and one or more applications, wherein the one or more applications can be stored in the memory 120 and configured to be executed by one or more processors 110, and the one or more applications are configured to perform the methods described in the foregoing method embodiments.
[0131] The processor 110 may include one or more processing cores. The processor 110 connects to various parts within the electronic device 100 using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 120, and by calling data stored in the memory 120. Optionally, the processor 110 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 110 may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the content to be displayed; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 110 and may be implemented separately using a communication chip.
[0132] The memory 120 may include random access memory (RAM) or read-only memory (ROM). The memory 120 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 120 may include a program storage area and a data storage area. The program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as touch functionality, sound playback functionality, image playback functionality, etc.), and instructions for implementing the various method embodiments described below. The data storage area may also store data created by the electronic device 100 during use (such as phonebook data, audio and video data, chat log data, etc.).
[0133] Please see Figure 7 This diagram illustrates a structural block diagram of a computer-readable storage medium provided in an embodiment of this application. The computer-readable medium 300 stores program code that can be called by a processor to execute the methods described in the above method embodiments.
[0134] The computer-readable storage medium 300 may be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM, hard disk, or ROM. Optionally, the computer-readable storage medium 300 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 300 has storage space for program code 310 that performs any of the method steps described above. This program code can be read from or written to one or more computer program products. The program code 310 may be compressed, for example, in a suitable form.
[0135] In summary, the lock waiting queue sorting method, apparatus, electronic device, and storage medium provided in this application, upon detecting a target lock release, determine the predicted lock-holding time of each task in the initial waiting queue for the target lock; if the predicted lock-holding time of each task meets a preset condition, the order of the tasks in the initial waiting queue is adjusted to obtain a target waiting queue, wherein the tasks in the target waiting queue are arranged in a target order; and multiple tasks are awakened to hold the target lock in the target order. This application improves lock-holding throughput and the smoothness of task execution by predicting the lock-holding time of tasks in the target lock waiting queue and adjusting the execution order of tasks in the waiting queue based on the lock-holding time.
[0136] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A method for sorting a lock waiting queue, characterized in that, The method includes: If the release of the target lock is detected, the predicted lock holding time of each task in the initial waiting queue for the target lock is determined. If the predicted lock holding time of each task meets the preset conditions, the arrangement order of the multiple tasks included in the initial waiting queue is adjusted to obtain the target waiting queue. The multiple tasks included in the target waiting queue are arranged in a target order. The target order includes an arrangement order from shortest to longest predicted lock holding time, or an arrangement order in which tasks with predicted lock holding time less than or equal to the lock holding time threshold are arranged in the order of the time they requested lock holding, and tasks with predicted lock holding time greater than the lock holding time threshold are arranged in the order from shortest to longest predicted lock holding time, and are arranged after tasks with predicted lock holding time less than or equal to the lock holding time threshold. In the order of the targets, wake up the multiple tasks holding the target locks.
2. The method according to claim 1, characterized in that, If the predicted holding time of each task meets a preset condition, the order of the multiple tasks in the initial waiting queue is adjusted to obtain a target waiting queue, including: From the multiple tasks included in the initial waiting queue, a preset proportion of tasks are determined; Based on the predicted lock-holding time of each task in the preset proportion of tasks, the sum of the predicted lock-holding times of the preset proportion of tasks is determined as the first predicted lock-holding time. Based on the predicted lock holding time of each of the multiple tasks, the sum of the predicted lock holding times of the multiple tasks is determined as the second predicted lock holding time. If the ratio of the first predicted lock holding time to the second predicted lock holding time is greater than a proportional threshold, the order of the multiple tasks included in the initial waiting queue is adjusted to obtain the target waiting queue.
3. The method according to claim 2, characterized in that, The step of determining a preset proportion of tasks from the multiple tasks included in the initial waiting queue includes: From the multiple tasks included in the initial waiting queue, the preset proportion of tasks is determined in descending order of predicted lock holding time.
4. The method according to claim 2, characterized in that, The step of adjusting the order of multiple tasks in the initial waiting queue to obtain the target waiting queue includes: The order of the multiple tasks in the initial waiting queue is adjusted according to the predicted lock holding time from shortest to longest to obtain the target waiting queue.
5. The method according to claim 2, characterized in that, The method further includes: If the ratio of the first predicted lock-holding time to the second predicted lock-holding time is less than or equal to the ratio threshold, then the initial waiting queue is determined as the target waiting queue.
6. The method according to any one of claims 1-5, characterized in that, The step of adjusting the order of multiple tasks in the initial waiting queue to obtain the target waiting queue includes: By stripping the core task from the multiple tasks included in the initial waiting queue, a first waiting queue is obtained; According to the predicted lock holding time from shortest to longest, the order of tasks in the first waiting queue is adjusted to obtain the second waiting queue; The core task is arranged before the second waiting queue to obtain the target waiting queue.
7. The method according to any one of claims 1-5, characterized in that, For each of the plurality of tasks, determining the initial waiting queue for the target lock includes the predicted lock holding time for each of the plurality of tasks, comprising: Determine the historical average lock holding time of the task on the target lock; Determine the first holding time and the second holding time of the task on the target lock, wherein the first holding time is the most recent holding time of the task on the target lock, and the second holding time is the penultimate holding time of the task on the target lock; From the historical average lock holding time, the first lock holding time, and the second lock holding time, a lock holding time is determined as the predicted lock holding time for the task.
8. The method according to claim 7, characterized in that, Determining a lock-holding time as the predicted lock-holding time for the task from the historical average lock-holding time, the first lock-holding time, and the second lock-holding time includes: If the first lock holding time is greater than the historical average lock holding time, and the second lock holding time is greater than the historical average lock holding time, then the historical average lock holding time is determined as the predicted lock holding time of the task.
9. The method according to claim 7, characterized in that, Determining a lock-holding time as the predicted lock-holding time for the task from the historical average lock-holding time, the first lock-holding time, and the second lock-holding time includes: If the first lock holding time is less than the historical average lock holding time, and the second lock holding time is less than the historical average lock holding time, then calculate the first difference between the first lock holding time and the historical average lock holding time, and calculate the second difference between the second lock holding time and the historical average lock holding time. If the first difference is greater than a preset difference and the second difference is greater than the preset difference, then the larger difference between the first difference and the second difference is determined as the target difference. The lock holding time corresponding to the target difference is determined as the predicted lock holding time for the task.
10. An apparatus for sorting a lock waiting queue, characterized in that, The device includes: The predicted lock holding time determination module is used to determine the predicted lock holding time of each task in the initial waiting queue of multiple tasks waiting for the target lock when the target lock is detected to be released. The sequence adjustment module is used to adjust the order of multiple tasks in the initial waiting queue if the predicted lock holding time of each task meets the preset conditions, so as to obtain a target waiting queue. The multiple tasks in the target waiting queue are arranged in a target order, which includes an order in which the predicted lock holding time is from shortest to longest, or an order in which tasks with predicted lock holding time less than or equal to a lock holding time threshold are arranged in the order of the time they requested lock holding, and tasks with predicted lock holding time greater than a lock holding time threshold are arranged in the order of the predicted lock holding time from shortest to longest, and are arranged after tasks with predicted lock holding time less than or equal to a lock holding time threshold. The wake-up module is used to wake up the multiple tasks holding the target lock in the order of the target.
11. An electronic device, characterized in that, include: One or more processors; Memory; One or more applications, wherein the one or more applications are stored in the memory and configured to be executed by the one or more processors, the one or more applications being configured to perform the method as described in any one of claims 1-9.
12. A computer-readable storage medium, characterized in that, The computer-readable storage medium contains program code that can be invoked by a processor to execute the method as described in any one of claims 1-9.
Citation Information
Patent Citations
Method and device for controlling process
CN108958911A
Concurrent thread processing method and device, electronic equipment and storage medium
CN113760369A