Resource allocation method, device, storage medium and electronic device
By determining and marking the target thread from the background thread when memory is tight, and prioritizing resources according to thread categories, the problem of low recycling efficiency in memory management is solved, and the stability and user experience of the system are improved.
Patent Information
- Application Number
- CN202010628797.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-07-01
- Publication Date
- 2024-12-03
- Estimated Expiration
- 2040-07-01
AI Technical Summary
The existing memory management methods are inefficient in recycling when memory is tight, resulting in system lag, operation errors and crashes.
When detecting that the system free memory is less than the preset threshold, the target thread is determined from the background thread, marked it as a first-class thread, sent a close instruction, and prioritized allocation system for the first-class thread according to the thread category and preset allocation mechanism Resources to improve memory recycling efficiency.
By prioritizing the shutdown and release of thread resources that need to be closed, improve memory recycling efficiency, avoid system lag and crash due to insufficient memory, and improve system stability.
Smart Images

Figure CN111831438B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of electronic equipment, and in particular to a resource allocation method, device, storage medium and electronic equipment. Background Art
[0002] Memory management is one of the core technologies of the system, and plays a key role in improving the user experience of the system and the stability of the system operation. When the memory is tight, the system will experience a series of problems such as freezing, operation errors, and crashes. In related technologies, memory shortages can be alleviated by memory recycling, but conventional memory management methods have the problem of low recycling efficiency. Summary of the invention
[0003] Embodiments of the present application provide a resource allocation method, device, storage medium, and electronic device, which can improve memory recycling efficiency.
[0004] In a first aspect, an embodiment of the present application provides a resource allocation method, including:
[0005] When it is detected that the amount of free memory in the system is less than a preset threshold, a target thread is determined from the background threads;
[0006] Marking the target thread as a first-category thread;
[0007] Sending a preset instruction to the target thread, wherein the preset instruction is used to instruct the target thread to perform a shutdown process;
[0008] When it is necessary to allocate system resources to the threads to be allocated, determining the categories of the threads to be allocated, wherein other threads except the first category of threads are regarded as second category threads;
[0009] System resources are allocated to the threads to be allocated according to the categories and a preset allocation mechanism, wherein a speed or quantity of allocating system resources to the first category of threads according to the preset allocation mechanism is greater than a speed or quantity of allocating system resources to the second category of threads.
[0010] In a second aspect, an embodiment of the present application further provides a resource allocation device, including:
[0011] The thread selection module is used to determine a target thread from background threads when it is detected that the amount of free memory in the system is less than a preset threshold;
[0012] A thread marking module, used for marking the target thread as a first type of thread;
[0013] An instruction sending module, used for sending a preset instruction to the target thread, wherein the preset instruction is used for instructing the target thread to perform a shutdown process;
[0014] A thread identification module, used for determining the category of the to-be-allocated thread when it is necessary to allocate system resources to the to-be-allocated thread, wherein the threads other than the first category of threads are regarded as second category of threads;
[0015] A resource allocation module is used to allocate system resources to the threads to be allocated according to the categories and a preset allocation mechanism, wherein the speed or quantity of allocating system resources to the first category of threads according to the preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second category of threads.
[0016] In a third aspect, an embodiment of the present application further provides a storage medium on which a computer program is stored. When the computer program is run on a computer, the computer executes a resource allocation method as provided in any embodiment of the present application.
[0017] In a fourth aspect, an embodiment of the present application further provides an electronic device, comprising a processor and a memory, wherein the memory has a computer program, and the processor executes a resource allocation method as provided in any embodiment of the present application by calling the computer program.
[0018] The technical solution provided by the embodiment of the present application is that when it is detected that the amount of free memory of the system is less than a preset threshold, a target thread is determined from the background thread, the target thread is marked as a first-class thread, and a preset instruction is sent to the target thread, the preset instruction is used to instruct the target thread to perform a shutdown process, and when it is necessary to allocate system resources to the thread to be allocated, the category of the thread to be allocated is first determined, and when allocating system resources, system resources are allocated to the thread based on the category of the thread and the preset allocation mechanism, and the speed or quantity of allocating system resources to the first-class thread according to the preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second-class thread. In this way, when the system needs to close the thread due to insufficient memory, the thread to be closed is marked as a first-class thread, and the kernel will give priority to allocating resources to the first-class thread when allocating resources, so that the thread to be closed can obtain resources quickly to respond to the shutdown instruction, and then release the memory occupied by it as soon as possible, thereby improving the efficiency of memory recovery. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0020] Figure 1 A first flow chart of a resource allocation method provided in an embodiment of the present application.
[0021] Figure 2 A schematic diagram of a processing unit allocation method for a resource allocation method provided in an embodiment of the present application.
[0022] Figure 3 A schematic diagram of the structure of a resource allocation device provided in an embodiment of the present application.
[0023] Figure 4 A first structural schematic diagram of an electronic device provided in an embodiment of the present application.
[0024] Figure 5 A second structural schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0025] The technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.
[0026] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0027] The embodiment of the present application provides a resource allocation method, and the execution subject of the resource allocation method can be the resource allocation device provided in the embodiment of the present application, or an electronic device integrating the resource allocation device, wherein the resource allocation device can be implemented in hardware or software. The electronic device can be a smart phone, a tablet computer, a PDA, a laptop computer, or a desktop computer.
[0028] See also Figure 1 , Figure 1 This is a schematic diagram of a first process flow of the resource allocation method provided in the embodiment of the present application. The specific process flow of the resource allocation method provided in the embodiment of the present application may be as follows:
[0029] In 101, when it is detected that the amount of free memory in the system is less than a preset threshold, a target thread is determined from the background threads.
[0030] In the embodiment of the present application, the operating system of the electronic device may be a system based on the Linux kernel, for example, an Android operating system, etc. System processes and application processes are running on the electronic device, and a thread is an execution path of a process and is the smallest unit of program execution.
[0031] A process can have multiple threads, but at least one thread. For the kernel, when scheduling resources, such as CPU scheduling, it is specific to a certain thread. There is a main thread in the process, and it will also create many sub-threads to assist in the work. For example, a WeChat application process will start a main thread to execute code, and during the execution, it will also start other sub-threads to assist in running the task code of each part.
[0032] The running of a thread requires the kernel to allocate memory space for it. And as the system runs, the kernel continuously recycles and allocates memory. When the kernel detects that the remaining memory is insufficient, for example, when it detects that the amount of free memory in the system is less than a preset threshold, some mechanisms will be used to recycle memory. One memory recycling mechanism is that when insufficient remaining memory is detected, a low memory killer (LMK) is triggered to clean up the running threads and release the memory resources corresponding to the cleaned threads. For example, some threads running in the background of the electronic device are closed by LMK, and the memory resources corresponding to the threads running in the background are released. The method of closing the thread is to send a kill signal to the killed thread, and the closed thread responds after receiving the signal. If the system load is too high, the closed thread may take a long time to get the required resources (such as CPU) to run, so the process from sending the kill signal to the thread actually responding to the signal to exit and release the memory may be long. If the process is slow, it may affect the speed of system memory release, resulting in low memory recycling efficiency.
[0033] In an embodiment of the present application, when it is detected that the amount of free memory of the system is less than a preset threshold, a target thread is determined from the background threads, wherein the target thread may be one or more threads depending on the size of the memory recovery requirement.
[0034] For example, in some embodiments, when it is detected that the amount of free memory in the system is less than a preset threshold, a target thread is determined from background threads, including: when it is detected that the amount of free memory in the system is less than a preset threshold, obtaining the recycling priority of each background thread; determining one or more threads with the highest recycling priority from the background threads as the target thread.
[0035] The recycling priority can be determined according to the Adj value of the thread. The larger the Adj value of the thread, the higher its recycling priority. Generally, the Adj value of a thread with high importance is less than 0, such as the native thread, system thread, core thread, etc. started by the system; the Adj value of some background threads is generally greater than 0, while the Adj value of the foreground thread is equal to 0. The Adj value is a value set by the system according to the priority of the thread. The higher the Adj value, the lower the priority or importance of the thread.
[0036] When there are multiple background threads, the Adj value of each background thread is obtained, and one or more threads with the highest Adj value are determined as target threads to be closed by the low memory killer. Since the target thread runs in the background, it may be in the waiting queue. If the kernel sends a preset instruction to the thread for executing the thread to close, the thread cannot respond to the instruction in time if the processor resources are not allocated in time.
[0037] Therefore, in the embodiment of the present application, after the target thread is determined, the thread is marked as a first-class thread, so that when the kernel identifies the thread as a first-class thread, it preferentially allocates resources to the first-class thread.
[0038] In 102, the target thread is marked as a first-category thread.
[0039] In 103, a preset instruction is sent to the target thread, where the preset instruction is used to instruct the target thread to perform a shutdown process.
[0040] Linux uses the task_struct structure to describe and record threads, and each thread has a corresponding task_struct structure. The task_struct records the name, identifier, state, priority, memory pointer, context data and other attribute information of the thread. Therefore, the application framework layer can add the corresponding ux flag member in the task_struct structure to mark the UI thread, Render thread, GL thread and other threads of the foreground process that perform related tasks in user interaction events with the ux flag bit, that is, add the ux tag, so that the kernel layer can identify the category of the thread. After marking, the kernel sends a preset instruction to the target thread, instructing the target thread to perform shutdown processing.
[0041] In 104 , when it is necessary to allocate system resources to threads to be allocated, the categories of threads to be allocated are determined, wherein threads other than threads of the first category are regarded as threads of the second category.
[0042] In 105, system resources are allocated to the threads to be allocated according to the categories and the preset allocation mechanism, wherein the speed or amount of allocating system resources to the first category of threads according to the preset allocation mechanism is greater than the speed or amount of allocating system resources to the second category of threads.
[0043] Since thread closing mainly uses processor resources, some threads may also involve memory resources or IO resources. The following uses the allocation of processor resources as an example to illustrate the scheme of the embodiment of the present application, but their principles are similar, that is, to tilt resources toward the first type of threads and increase the speed at which the first type of threads obtain resources.
[0044] For a multi-core CPU, it will be scheduled according to certain scheduling rules, and all threads running on the processor cores will receive the overall scheduling of the system. For example, in general electronic devices with ARM (Advanced RISC Machine) architecture, the CPU cores are scheduled in groups, and all threads running on the CPU cores in the CPU group will receive the overall scheduling of the system. In the ARM standard architecture, there are differences in the computing power of the CPU cores. Generally, the cores with stronger computing power are called large cores, and the cores with weaker computing power are called small cores. For example, for an eight-core processor, there are generally four large cores and four small cores. It should be noted that the above-mentioned number of cores is only for example purposes. The embodiments of the present application do not specifically limit the number of cores of the processor. When there are at least two cores, the solution of the embodiments of the present application can be used.
[0045] When a multi-core CPU is scheduled, a CPU core needs to be allocated to a thread to add the thread to the run queue of the allocated CPU core so that the thread can run on the allocated CPU core. In the embodiment of the present application, a processor core can be regarded as an independent processing unit. For example, if the electronic device is an eight-core processor, each core is an independent processing unit.
[0046] When the kernel needs to allocate processor resources to a thread, it takes the thread as a thread to be allocated and determines whether the thread is a first-class thread.
[0047] It should be noted that in the embodiments of the present application, the threads of objects whose low memory killer is closed are marked as first-class threads. In some embodiments, other threads related to user experience may also be marked as first-class threads, for example, threads that execute related tasks in user interaction events.
[0048] For threads that do not have ux labels, the kernel treats them as second-class threads. It should be noted that the "first class" and "second class" here are only to distinguish whether the thread has a ux label, rather than simply dividing the threads in the system into these two categories. The resource allocation optimization scheme of this application is based on whether the thread has a ux label. In addition, the system will use its original allocation mechanism to allocate resources to different threads. Based on such a mechanism, when allocating resources to threads, the category of the thread (first-class thread or second-class thread) is given priority, and then the preset allocation mechanism is considered, and system resources are allocated to the threads to be allocated according to the category and the preset allocation mechanism.
[0049] For example, in one embodiment, system resources are allocated to threads to be allocated according to categories and a preset allocation mechanism, including: determining a target scheduling class corresponding to the thread, and adding the thread to a sub-run queue corresponding to the target scheduling class; when scheduling threads in a sub-run queue, detecting whether there are first-class threads in the sub-run queue; when there are first-class threads in the sub-run queue, running the first-class threads; when there are no first-class threads in the sub-run queue, running second-class threads.
[0050] Generally speaking, threads in the kernel are divided into different scheduling classes. The scheduling classes are stored in the task_struct structure mentioned above. The most common threads in the kernel are CFS (complete fair schedule) scheduling class threads (such as UI threads, Render threads, and many common threads are CFS) and RT (Real Time Scheduler) scheduling class threads (such as Surface flinger threads and some other core threads). Different scheduling classes have different priorities (the priority here refers to the overall priority of a scheduling class thread relative to another scheduling class thread). For example, the priority of the scheduling class is reflected in: when a multi-core CPU is scheduled, the RT thread will be selected to run through the task scheduling method of the RT scheduling class first. If there is no RT thread, the CFS thread will be selected from the scheduling method of the CFS scheduling class. In other words, the priority of the RT scheduling class is higher than that of the CFS scheduling class. Of course, in the scheduling methods of these two scheduling classes, the priorities of the threads they process will be distinguished (the priority here refers to the priority of each thread within a scheduling class), such as RT priority and CFS priority.
[0051] Without distinguishing between ux threads and non-ux threads, the kernel allocates resources according to the priorities of the scheduling classes mentioned above and the priorities of the threads within each scheduling class.
[0052] In the embodiment of the present application, after the ux thread is marked, the ux attribute of the thread in the same scheduling class is given priority, and then the thread priority in the scheduling class is considered. That is to say, the ux flag bit is a newly added flag bit. After a thread adds this flag bit, it does not affect its other original flag bits. For example, after a CFS thread has a ux flag, it still belongs to the CFS thread, and resources are still allocated according to the preset allocation mechanism corresponding to the CFS scheduling class. However, before allocating resources according to the preset allocation mechanism corresponding to the CFS scheduling class, it is necessary to consider that the thread has a ux tag. In the CFS thread queue, the CFS thread with the ux tag can obtain more processor resources faster than other CFS threads without the ux tag. However, the priority of the RT thread without the ux tag is still higher than that of the CFS thread with the ux tag, because the priority of the RT scheduling class is higher than that of the CFS scheduling class.
[0053] When allocating processor units to threads, for the sake of system load balancing, the thread is allocated to the processor unit with the smallest load. After the processor unit is allocated, the thread needs to be added to the run queue of the processor unit. In this run queue, the RT thread is generally at the head of the queue and the CFS thread is at the end of the queue. In this way, the run queue of the processor unit can be divided into multiple sub-run queues according to different thread scheduling classes, and each scheduling class corresponds to a sub-run queue. When adding the thread to the run queue of the processor unit, first determine which scheduling class the thread belongs to, and then add it to the sub-run queue corresponding to the scheduling class. When scheduling threads in the sub-run queue, ux threads are scheduled first. When all ux threads have run, non-ux threads are scheduled.
[0054] That is to say, in this application, the so-called "the speed or quantity of allocating system resources to the first type of threads according to the preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second type of threads" is a resource optimization allocation mechanism performed when other attribute conditions of the threads are the same. From the perspective of dividing threads according to scheduling classes, it can be understood here that when the scheduling classes of the first type of threads and the second type of threads are the same, the speed or quantity of allocating system resources to the first type of threads according to the preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second type of threads.
[0055] For example, if there are three RT threads A, B and C, regardless of whether their priorities are the same, if A is a ux thread (first-class thread), and B and C are non-ux threads (second-class threads), then A will be scheduled and executed first. For B and C, they will still be scheduled according to the RT priority of B and C, such as scheduling the thread with the highest RT priority among B and C first.
[0056] For example, in some embodiments, a large CPU core can be reserved specifically for ux threads, and all ux threads can be assigned to run on the reserved core to ensure that the ux threads are allocated resources in a timely manner. For non-ux threads, they still compete for other CPU resources except the reserved CPU core according to the default allocation mechanism.
[0057] For example, in one embodiment, system resources are allocated to threads to be allocated according to categories and a preset allocation mechanism, including: if the thread is a first-category thread, the thread to be allocated is added to a running queue of a first processing unit, so as to close the thread to be allocated based on the first processing unit, wherein a plurality of processing units of the processor are divided into a first processing unit and a second processing unit; if the thread is a second-category thread, the thread is added to a running queue of the second processing unit.
[0058] In this embodiment, a processor core is separated from multiple processor cores, and the processor core only runs the ux thread, while those non-ux threads run on other processor cores except the independent processor core, and will not occupy the resources of the ux thread, so that the ux thread can run quickly and timely.
[0059] Moreover, in low-load scenarios, shutting down a processor core will not only not affect the operation of electronic devices, but also save power consumption. In high-load scenarios, ux threads generally account for a high proportion, and using the reserved processor core exclusively for the operation of ux threads can improve the operating efficiency of ux threads.
[0060] For example, when system startup is detected, processor information is obtained; a first processing unit is determined from multiple processing units of the processor based on the processor information, and the first processing unit is turned off; other processing units except the first processing unit are used as second processing units, wherein the frequency of the first processing unit is greater than or equal to the frequency of the second processing unit.
[0061] When the electronic device is started, the system first reads the processor information, such as the topological structure information of the CPU, to determine the number of cores contained in the CPU and the computing power of each processor core, so as to determine a processor core from the multi-core processor core and separate it. For example, a large core is separated from the scheduling strategy of the CPU group to which it belongs, does not participate in daily CPU operations, and shuts it down. Among them, since a processing core is a processing unit, the isolated processor core is recorded as the first processing unit, and the other processing units except the first processing unit are recorded as the second processing unit. Among them, the second processing unit is still scheduled according to the scheduling strategy of the CPU group to which it originally belongs.
[0062] When the kernel allocates processor resources to a thread, it first determines whether the thread is a ux thread. If the kernel determines that the thread is a ux thread, the ux thread is added to the run queue of the first processing unit to execute the task corresponding to the thread based on the first processing unit. In this way, the thread does not need to participate in the overall CPU scheduling and does not need to compete with other threads, ensuring that the Ux thread can run smoothly on the first processing unit.
[0063] In one embodiment, the first processing unit is independent of the scheduling strategy of the CPU group to which it belongs, and does not participate in daily CPU operations, but can also be always in an open state. As long as there is a ux thread, the ux thread can be run on the first processing unit.
[0064] Alternatively, in another embodiment, when the thread is a first-class thread, adding the thread to a run queue of the first processing unit includes:
[0065] When the thread is a first type of thread, determining whether the first processing unit is turned on, wherein when the system load is greater than a first preset load, turning on the first processing unit;
[0066] When the first processing unit is turned on, the thread is added to a run queue of the first processing unit.
[0067] In this embodiment, the first processing unit is turned on only when the system load is greater than the first preset load. The first preset load may be an empirical value. When the system load is greater than the first preset load, the system is in a high-load state. When the load is low, even if the ux thread is run on the second processing unit and scheduled according to the scheduling policy of the CPU group to which the second processing unit belongs, the ux thread can still obtain processor resources at a faster speed. In 102, when the system detects that the thread is a first-class thread, it first determines whether the first processing unit is turned on. If the first processing unit is turned on, the thread is added to the running queue of the first processing unit; if the first processing unit is not turned on, it means that it is currently in a low-load state, and the thread can be added to the running queue of the second processing unit. When the thread is not a thread of a preset type, the thread can be directly added to the running queue of the second processing unit.
[0068] See also Figure 2 , Figure 2Schematic diagram of the processing unit allocation method of the resource allocation method provided in the embodiment of the present application. Assume that the processor of the electronic device has four cores, among which CPU0 and CPU1 are small cores, and CPU2 and CPU3 are large cores, that is, the computing power of CPU2 and CPU3 is greater than the computing power of CPU0 and CPU1, or, that is, the frequency of CPU2 and CPU3 is greater than the frequency of CPU0 and CPU1, and one of the large cores CPU3 is used as an independent core reserved for ux threads. Among them, ux1, ux2, and ux3 are all ux threads, which are assigned to CPU3 for running, and task1 to task9 are not ux threads, so they are assigned to CPU0, CPU1 or CPU2 for running according to the CPU scheduling policy. Based on this, the ux thread is put on the large core of the specified CPU to run, without participating in the overall CPU scheduling, and without competing with other threads, to ensure that the ux thread can run smoothly on the large core, so that it can run smoothly and smoothly, avoid system jams, and improve user experience.
[0069] In one embodiment, after the first processing unit is turned on, the system load continues to be detected. When the system load is detected to be less than a second preset load, the first processing unit is turned off, wherein the second preset load is less than or equal to the first preset load; the second preset load may be an empirical value, and when the system load is less than the second preset load, the system is in a low load state. Alternatively, in another embodiment, when it is detected that the execution of the run queue of the first processing unit is completed, the first processing unit is turned off.
[0070] In specific implementation, the present application is not limited by the execution order of the various steps described. If no conflict occurs, some steps can be performed in other orders or simultaneously.
[0071] As can be seen from the above, the resource allocation method provided by the embodiment of the present application, when it is detected that the amount of free memory of the system is less than a preset threshold, determines the target thread from the background thread, marks the target thread as a first-class thread, sends a preset instruction to the target thread, and the preset instruction is used to instruct the target thread to perform a shutdown process. When it is necessary to allocate system resources to the thread to be allocated, first determine the category of the thread to be allocated, and when allocating system resources, allocate system resources to the thread based on the category of the thread and the preset allocation mechanism, and the speed or quantity of allocating system resources to the first-class thread according to the preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second-class thread. In this way, when the system needs to close the thread due to insufficient memory, the thread to be closed is marked as a first-class thread, and the kernel will give priority to allocating resources to the first-class thread when allocating resources, so that the thread to be closed can quickly obtain resources to respond to the shutdown instruction, and then release the memory occupied by it as soon as possible, thereby improving the efficiency of memory recovery.
[0072] In one embodiment, a resource allocation device is also provided. Figure 3 , Figure 3 The schematic diagram of the structure of the resource allocation device 300 provided in the embodiment of the present application. The resource allocation device 300 is applied to an electronic device, and the resource allocation device 300 includes a thread selection module 301, a thread marking module 302, an instruction sending module 303 and a thread identification module 304, as follows:
[0073] The thread selection module 301 is used to determine a target thread from background threads when it is detected that the amount of free memory in the system is less than a preset threshold;
[0074] A thread marking module 302, used to mark the target thread as a first type of thread;
[0075] An instruction sending module 303 is used to send a preset instruction to the target thread, wherein the preset instruction is used to instruct the target thread to perform a shutdown process;
[0076] A thread identification module 304, configured to determine the category of the to-be-allocated thread when it is necessary to allocate system resources to the to-be-allocated thread, wherein the other threads except the first category of threads are regarded as second category of threads;
[0077] The resource allocation module 305 is used to allocate system resources to the threads to be allocated according to the categories and the preset allocation mechanism, wherein the speed or quantity of allocating system resources to the first category of threads according to the preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second category of threads.
[0078] In some embodiments, the thread selection module 301 is further used to: when it is detected that the amount of free memory in the system is less than a preset threshold, obtain the recycling priority of each background thread;
[0079] One or more threads with the highest recycling priority are determined from the background threads as the target threads.
[0080] In some embodiments, the system resource is a processor resource; the resource allocation module 305 is further used to: if the thread is a first-class thread, add the thread to be allocated to a run queue of a first processing unit to close the thread to be allocated based on the first processing unit, wherein the plurality of processing units of the processor are divided into a first processing unit and a second processing unit;
[0081] If the thread is a second-category thread, the thread is added to a run queue of the second processing unit.
[0082] In some embodiments, determining whether the first processing unit is turned on, wherein when the system load is greater than a first preset load, turning on the first processing unit;
[0083] When the first processing unit is turned on, adding the thread to a run queue of the first processing unit;
[0084] When the first processing unit is not turned on, the thread is added to a running queue of the second processing unit.
[0085] In some embodiments, the resource allocation module 305 is further used to: obtain processor information when system startup is detected;
[0086] Determine a first processing unit from a plurality of processing units of the processor according to the processor information, and shut down the first processing unit;
[0087] The other processing units except the first processing unit are used as second processing units, wherein the frequency of the first processing unit is greater than or equal to the frequency of the second processing unit.
[0088] In some embodiments, the system resource is a processor resource; the resource allocation module 305 is further used to: determine a target scheduling class corresponding to the thread, and add the thread to a sub-run queue corresponding to the target scheduling class;
[0089] When scheduling threads in the sub-run queue, detecting whether there is a first-category thread in the sub-run queue;
[0090] When there is a first type of thread in the sub-run queue, run the first type of thread;
[0091] When there is no first-category thread in the sub-run queue, the second-category thread is run.
[0092] It should be noted that the resource allocation device provided in the embodiment of the present application belongs to the same concept as the resource allocation method in the above embodiment. Any method provided in the resource allocation method embodiment can be implemented through the resource allocation device. The specific implementation process is detailed in the resource allocation method embodiment and will not be repeated here.
[0093] As can be seen from the above, the resource allocation device proposed in the embodiment of the present application, when detecting that the amount of free memory of the system is less than a preset threshold, determines the target thread from the background thread, marks the target thread as a first-class thread, sends a preset instruction to the target thread, and the preset instruction is used to instruct the target thread to perform a shutdown process. When it is necessary to allocate system resources to the thread to be allocated, first determine the category of the thread to be allocated, and when allocating system resources, allocate system resources to the thread based on the category of the thread and the preset allocation mechanism, and the speed or quantity of allocating system resources to the first-class thread according to the preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second-class thread. In this way, when the system needs to close the thread due to insufficient memory, the thread to be closed is marked as a first-class thread, and the kernel will give priority to allocating resources to the first-class thread when allocating resources, so that the thread to be closed can quickly obtain resources to respond to the shutdown instruction, and then release the memory occupied by it as soon as possible, thereby improving the efficiency of memory recovery.
[0094] The present application also provides an electronic device. The electronic device may be a smart phone, a tablet computer, or other device. Figure 4 , Figure 4 The first structural diagram of the electronic device provided in the embodiment of the present application is as follows: The electronic device 400 includes a processor 401 and a memory 402. The processor 401 is electrically connected to the memory 402.
[0095] The processor 401 is the control center of the electronic device 400. It uses various interfaces and lines to connect various parts of the entire electronic device. It executes various functions of the electronic device and processes data by running or calling computer programs stored in the memory 402, and calling data stored in the memory 402, thereby monitoring the electronic device as a whole.
[0096] The memory 402 may be used to store computer programs and data. The computer programs stored in the memory 402 include instructions that can be executed in the processor. The computer programs may constitute various functional modules. The processor 401 executes various functional applications and data processing by calling the computer programs stored in the memory 402.
[0097] In this embodiment, the processor 401 in the electronic device 400 loads instructions corresponding to the processes of one or more computer programs into the memory 402 according to the following steps, and the processor 401 runs the computer programs stored in the memory 402 to implement various functions:
[0098] When it is detected that the amount of free memory in the system is less than a preset threshold, a target thread is determined from the background threads;
[0099] Marking the target thread as a first-category thread;
[0100] Sending a preset instruction to the target thread, wherein the preset instruction is used to instruct the target thread to perform a shutdown process;
[0101] When it is necessary to allocate system resources to the threads to be allocated, determining the categories of the threads to be allocated, wherein other threads except the first category of threads are regarded as second category threads;
[0102] System resources are allocated to the threads to be allocated according to the categories and a preset allocation mechanism, wherein a speed or quantity of allocating system resources to the first category of threads according to the preset allocation mechanism is greater than a speed or quantity of allocating system resources to the second category of threads.
[0103] In some embodiments, see Figure 5 , Figure 5 A second structural diagram of an electronic device provided in an embodiment of the present application. The electronic device 400 further includes: a radio frequency circuit 403, a display screen 404, a control circuit 405, an input unit 406, an audio circuit 407, a sensor 408, and a power supply 409. The processor 401 is electrically connected to the radio frequency circuit 403, the display screen 404, the control circuit 405, the input unit 406, the audio circuit 407, the sensor 408, and the power supply 409, respectively.
[0104] The radio frequency circuit 403 is used to send and receive radio frequency signals to communicate with network devices or other electronic devices through wireless communication.
[0105] The display screen 404 may be used to display information input by a user or information provided to a user and various graphical user interfaces of the electronic device. These graphical user interfaces may be composed of images, texts, icons, videos and any combination thereof.
[0106] The control circuit 405 is electrically connected to the display screen 404 and is used to control the display screen 404 to display information.
[0107] The input unit 406 can be used to receive input numbers, character information or user feature information (such as fingerprints), and generate keyboard, mouse, joystick, optical or trackball signal input related to user settings and function control. The input unit 406 can include a fingerprint recognition module.
[0108] The audio circuit 407 can provide an audio interface between the user and the electronic device through a speaker and a microphone. The audio circuit 407 includes a microphone. The microphone is electrically connected to the processor 401. The microphone is used to receive voice information input by the user.
[0109] The sensor 408 is used to collect external environment information. The sensor 408 may include one or more sensors such as an environment brightness sensor, an acceleration sensor, and a gyroscope.
[0110] The power supply 409 is used to supply power to various components of the electronic device 400. In some embodiments, the power supply 409 may be logically connected to the processor 401 through a power management system, so that the power management system can manage charging, discharging, power consumption, and other functions.
[0111] Although not shown in the figure, the electronic device 400 may also include a camera, a Bluetooth module, etc., which will not be described in detail here.
[0112] In this embodiment, the processor 401 in the electronic device 400 loads instructions corresponding to the processes of one or more computer programs into the memory 402 according to the following steps, and the processor 401 runs the computer programs stored in the memory 402 to implement various functions:
[0113] When it is detected that the amount of free memory in the system is less than a preset threshold, a target thread is determined from the background threads;
[0114] Marking the target thread as a first-category thread;
[0115] Sending a preset instruction to the target thread, wherein the preset instruction is used to instruct the target thread to perform a shutdown process;
[0116] When it is necessary to allocate system resources to the threads to be allocated, determining the categories of the threads to be allocated, wherein other threads except the first category of threads are regarded as second category threads;
[0117] System resources are allocated to the threads to be allocated according to the categories and a preset allocation mechanism, wherein a speed or quantity of allocating system resources to the first category of threads according to the preset allocation mechanism is greater than a speed or quantity of allocating system resources to the second category of threads.
[0118] As can be seen from the above, an embodiment of the present application provides an electronic device, which, when detecting that the amount of free memory in the system is less than a preset threshold, determines a target thread from a background thread, marks the target thread as a first-class thread, and sends a preset instruction to the target thread, the preset instruction is used to instruct the target thread to perform a shutdown process, and when it is necessary to allocate system resources to the thread to be allocated, first determine the category of the thread to be allocated, and when allocating system resources, allocate system resources to the thread based on the category of the thread and the preset allocation mechanism, and the speed or quantity of allocating system resources to the first-class thread according to the preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second-class thread. In this way, when the system needs to close the thread due to insufficient memory, the thread to be closed is marked as a first-class thread, and the kernel will give priority to allocating resources to the first-class thread when allocating resources, so that the thread to be closed can quickly obtain resources to respond to the shutdown instruction, and then release the memory occupied by it as soon as possible, thereby improving the efficiency of memory recovery.
[0119] An embodiment of the present application further provides a storage medium, wherein a computer program is stored in the storage medium. When the computer program runs on a computer, the computer executes the resource allocation method described in any of the above embodiments.
[0120] It should be noted that, a person skilled in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing related hardware through a computer program, and the computer program can be stored in a computer-readable storage medium, and the storage medium can include but is not limited to: read-only memory (ROM), random access memory (RAM), disk or optical disk, etc.
[0121] In addition, the terms "first", "second", and "third" in this application are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product, or device that includes a series of steps or modules is not limited to the listed steps or modules, but some embodiments also include steps or modules that are not listed, or some embodiments also include other steps or modules inherent to these processes, methods, products, or devices.
[0122] The resource allocation method, device, storage medium and electronic device provided in the embodiments of the present application are described in detail above. Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core idea of the present application. At the same time, for those skilled in the art, according to the idea of the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as limiting the present application.
Claims
1. A resource allocation method, It is characterized in that include: When it is detected that the amount of free memory in the system is less than a preset threshold, the recycling priority of each background thread is obtained; Determine one or more threads with the highest recycling priority from the background threads as the target threads; Marking the target thread as a first-class thread, so that when the kernel identifies the thread as the first-class thread, it preferentially allocates resources to the first-class thread; Sending a preset instruction to the target thread, wherein the preset instruction is used to instruct the target thread to perform a shutdown process; When it is necessary to allocate system resources to the threads to be allocated, determining the categories of the threads to be allocated, wherein other threads among the threads to be allocated except the first category of threads are regarded as second category threads, and the system resources are processor resources; Allocating system resources to the to-be-allocated thread according to the category and the preset allocation mechanism, including: if the thread is a first-category thread, adding the to-be-allocated thread to a run queue of a first processing unit to close the to-be-allocated thread based on the first processing unit, wherein a plurality of processing units of a processor are divided into a first processing unit and a second processing unit; If the thread is a second-type thread, the thread is added to the running queue of the second processing unit, wherein the speed or quantity of allocating system resources to the first-type threads according to a preset allocation mechanism is greater than the speed or quantity of allocating system resources to the second-type threads, and the frequency of the first processing unit is greater than or equal to the frequency of the second processing unit.
2. The resource allocation method according to claim 1, It is characterized in that The step of adding the to-be-allocated thread to the run queue of the first processing unit comprises: Determining whether the first processing unit is turned on, wherein when the system load is greater than a first preset load, the first processing unit is turned on; When the first processing unit is turned on, adding the thread to a run queue of the first processing unit; When the first processing unit is not turned on, the thread is added to a running queue of the second processing unit.
3. The resource allocation method according to claim 2, It is characterized in that The method further comprises: When the system boot is detected, the processor information is obtained; Determine a first processing unit from a plurality of processing units of the processor according to the processor information, and shut down the first processing unit; The other processing units except the first processing unit are used as second processing units.
4. The resource allocation method according to claim 1, It is characterized in that The system resources are processor resources; The allocating system resources to the threads to be allocated according to the categories and the preset allocation mechanism includes: Determine a target scheduling class corresponding to the thread, and add the thread to a sub-run queue corresponding to the target scheduling class; When scheduling threads in the sub-run queue, detecting whether there is a first-category thread in the sub-run queue; When there is a first type of thread in the sub-run queue, run the first type of thread; When there is no first-category thread in the sub-run queue, the second-category thread is run.
5. A resource allocation device, It is characterized in that include: A thread selection module is used to obtain the recycling priority of each background thread when it is detected that the amount of free memory in the system is less than a preset threshold, and determine one or more threads with the highest recycling priority from the background threads as the target thread; A thread marking module, used for marking the target thread as a first-class thread, so that when the kernel identifies the thread as the first-class thread, it preferentially allocates resources to the first-class thread; An instruction sending module, used for sending a preset instruction to the target thread, wherein the preset instruction is used for instructing the target thread to perform a shutdown process; a thread identification module, used for determining the category of the to-be-allocated thread when it is necessary to allocate system resources to the to-be-allocated thread, wherein the other threads among the to-be-allocated threads except the first category of threads are regarded as second category threads, and the system resources are processor resources; A resource allocation module, used to allocate system resources to the to-be-allocated thread according to the category and a preset allocation mechanism, including: if the thread is a first-category thread, adding the to-be-allocated thread to a running queue of a first processing unit to close the to-be-allocated thread based on the first processing unit, wherein a plurality of processing units of a processor are divided into a first processing unit and a second processing unit; if the thread is a second-category thread, adding the thread to a running queue of a second processing unit, wherein a speed or quantity of allocating system resources to the first-category thread according to the preset allocation mechanism is greater than a speed or quantity of allocating system resources to the second-category thread, and a frequency of the first processing unit is greater than or equal to a frequency of the second processing unit.
6. The resource allocation device according to claim 5, It is characterized in that The resource allocation module is also used for: Determine a target scheduling class corresponding to the thread, and add the thread to a sub-run queue corresponding to the target scheduling class; When scheduling threads in the sub-run queue, detecting whether there is a first-category thread in the sub-run queue; When there is a first type of thread in the sub-run queue, run the first type of thread; When there is no first-category thread in the sub-run queue, the second-category thread is run.
7. A storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed on a computer, the computer is enabled to execute the resource allocation method according to any one of claims 1 to 4.
8. An electronic device comprising a processor and a memory, wherein the memory stores a computer program, It is characterized in that The processor is configured to execute the resource allocation method according to any one of claims 1 to 4 by calling the computer program.
