A method and device for memory allocation
By determining whether the target page belongs to the target memory area and prohibiting it from being added to the LRU cache, the problems of memory allocation delay and CPU load in the existing technology are solved, and efficient memory allocation is achieved.
Patent Information
- Application Number
- CN202311129007.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-31
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2043-08-31
AI Technical Summary
When allocating contiguous memory, the existing technology causes long-term scheduling delays and CPU load, affecting memory allocation efficiency.
By determining whether the target page belongs to the target memory area, and prohibiting it from being added to the LRU cache when it belongs to this area, memory is allocated directly to avoid core binding and LRU cache operations.
It reduces the time-consuming memory allocation, improves memory allocation efficiency, reduces waiting delay, and improves the stability and reliability of the system.
Smart Images

Figure CN117687932B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computers, and in particular to a method and device for memory allocation. Background Art
[0002] Memory allocation is a core concept in computer systems, which involves the design of operating systems and programming languages. In memory allocation management, the operating system uses a technology called paging to manage memory, which can dynamically divide physical memory into fixed-size blocks, each of which is called a page.
[0003] The LRU list maintains a linked list in memory that organizes pages in the order in which they are used: the most recently used pages are moved to the front of the list, and the pages that have not been used for the longest time are at the end of the list. When a new page needs to be allocated and the physical memory is full, the memory manager will start from the end of the list (that is, the least used page) and move the pages out of the physical memory to free up space for allocating new pages.
[0004] The above-mentioned LRU cache needs to create a core-bound work queue (that is, bound to a certain processor core). The work queue needs to be executed on the corresponding processor core. For example, when allocating physical memory, there may be many concurrent tasks on the system that need to allocate large blocks of continuous memory. The kernel scheduler may take a long time to allocate the required continuous memory for a specific task and bind it to the specified processor core, resulting in a relatively high probability of waiting for a scheduling delay of more than ten milliseconds. Therefore, how to provide an effective continuous memory allocation method to reduce the time consumption of memory allocation and improve the efficiency of memory allocation is an urgent problem to be solved. Summary of the invention
[0005] The technical problem to be solved by the embodiments of the present application is to provide a method and device for memory allocation, thereby improving the efficiency of memory allocation.
[0006] In a first aspect, a memory allocation method according to an embodiment of the present application may include:
[0007] The electronic device includes a memory, and the memory includes a target memory area and a least recently used LRU cache. When a state of a target page in the memory is updated, it is determined whether the target page belongs to the target memory area, and the target memory area includes continuous physical memory; if the target page belongs to the target memory area, it is prohibited to add the target page to the LRU cache.
[0008] In an embodiment of the present application, when the state of the target page is updated (for example, after being accessed or modified), it is first determined whether the target page belongs to the target memory area. If the target page belongs to the target memory area, it is forbidden to add the target page to the LRU cache. That is, when the state of the target page in the target memory area is updated, it is forbidden to add it to the LRU cache, so that the target memory area is unbound from the processor core in the subsequent memory allocation, and there is no need to wait for the extended time caused by the binding core (for example, the delay of waiting for the task to be scheduled), so that the memory allocation time can be reduced. For example, in a specific application scenario, the target memory area can be a specific area in the memory. When the memory is initialized, the system reserves one or more continuous physical memories for the specific area. When there are free memory pages in the specific area (for example, when no memory allocation request specifically for the specific area is received), other memory allocation requests in the system can be satisfied, and the corresponding memory page is forbidden to be added to the LRU cache, thereby simplifying the subsequent memory allocation process. In summary, the above operation determines whether the target page belongs to the target memory area, and prohibits it from being added to the LRU cache if it belongs to the target memory area, so that subsequent memory allocation does not need to perform operations such as refreshing the pages of the LRU cache to the LRU linked list, thereby reducing the delay caused by waiting for tasks to be scheduled, thereby reducing the time consumption of memory allocation, thereby improving the efficiency of memory allocation.
[0009] In a possible implementation, the method further includes: receiving a first memory allocation request, the first memory allocation request is used to request the target memory area to apply for a first number of continuous physical memory pages, the first number of continuous physical memory pages is used for the first object; judging whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number; if greater than or equal to the first number, the first number of free continuous physical memory pages in the target memory area are allocated to the first object for use. In an embodiment of the present application, when the first object in the system needs to apply for a certain number of continuous physical memory pages, the system can issue a first memory allocation request (for example, a request other than a memory allocation request specifically for the specific area), the request is used to apply for a first number of continuous physical memory pages to the target memory area, when the number of free continuous physical memory pages in the target memory area meets the requirements of the first number, the corresponding memory pages are allocated to the first object for use. The embodiment of the present application accurately judges whether the number of free continuous physical memory pages in the target memory area meets the application requirements of the first object, avoids memory fragmentation or discontinuous memory allocation, thereby improving the utilization efficiency of memory resources, ensuring the rationality of resource allocation, and ultimately improving the utilization rate of memory allocation.
[0010] In a possible implementation, the target memory area includes a bitmap; the determination of whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number includes: querying the allocation of pages in the target memory area according to the bitmap, and determining whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number. In an embodiment of the present application, the allocated and free continuous physical memory pages in the target memory area can be quickly determined by querying the allocation of memory pages through a bitmap. Among them, a bitmap is a data structure, which can save memory and speed up the query by representing each page as a bit. In an embodiment of the present application, when the target memory area receives a first memory allocation request, the number of free pages in the target memory area will be detected first, and the free continuous physical memory pages in the target memory area will be queried and counted through a bitmap, and with the help of the bit state in the bitmap, the allocation of memory pages can be monitored in real time to avoid judgment errors, so as to ensure accurate judgment of the number of free pages, and finally determine whether the number of free memory pages meets the memory allocation request.
[0011] In a possible implementation, after the first number of free continuous physical memory pages in the target memory area are allocated to the first object for use, the method further includes: marking the bitmap information of the target page of the bitmap in the target memory area as being in an idle state. In an embodiment of the present application, when there are free memory pages in the target memory area, the memory pages are allocated to other memory allocation requests in the system, and after the allocation request is met, the bitmap information of the allocated pages is marked as being in an idle state. By marking the information of the target page in the bitmap as being in an idle state, the system can maintain the consistency of memory management data, improve the stability and reliability of the system, and prevent memory errors and abnormalities caused by inconsistent data. The embodiment of the present application facilitates device drivers, etc. to quickly find free pages to meet memory allocation requests when subsequent requests are made to use the target memory area.
[0012] In a possible implementation, the target page in the memory undergoes a status update, including: the target page in the memory is added to the first linked list for the first time, or the target page is moved from the second linked list to the third linked list or the target page is moved from the third linked list to the second linked list. In an embodiment of the present application, the target page undergoes a status update, which may specifically include a page being added to a first linked list (e.g., an LRU linked list) for management, or being moved from a second linked list (e.g., an ActiveLRU linked list) to a third linked list (e.g., an InActiveLRU linked list), or being moved from the third linked list to the second linked list. The system can therefore accurately find pages where status updates have occurred, thereby determining whether these pages where status updates have occurred belong to the target memory area, and then performing subsequent operations to prohibit adding to the LRU cache.
[0013] In a possible implementation, the target page contains structural information; including: searching for the type of the target page through the structural information of the target page; and judging whether the target page belongs to the target memory area based on the type of the target page. In an embodiment of the present application, after the target memory area allocates a free memory page to the first object, if the system determines that the target page has been updated, the type of the target page is searched for in the structural information in the target page. If the type of the target page belongs to the target memory area (for example, the MIGRATE-CMA type belongs to the CMA memory area), the page is judged to belong to the target memory area, so as to decide whether to prohibit the memory page from being added to the LRU cache, so as to ensure the accuracy of the memory allocation method in the embodiment of the present application.
[0014] In a possible implementation, the method also includes: receiving a second memory allocation request, the first memory allocation request is used to request an application for a second number of contiguous physical memory pages from the target memory area, and the second number of contiguous physical memory pages is used for use by a second object; determining whether the number of free contiguous physical memory pages in the target memory area is greater than or equal to the second number; if it is greater than or equal to the second number, performing page migration on the target pages in the target memory area; allocating the second number of free contiguous physical memory pages in the target memory area to the second object for use, the second number of free contiguous physical memory pages including one or more target pages that belong to the target memory area and have completed page migration. In an embodiment of the present application, when there are free memory pages in the target memory area and the free memory pages are allocated to the first object according to the first memory allocation request, the system prohibits the memory page from being added to the LRU cache after determining that the target page has been updated in status and that the target page belongs to the target memory area; thereafter, a second memory allocation request is received (for example, a memory allocation request specifically for the target memory area), and the number of free memory pages is determined according to the second number of the second memory allocation request requirements. If the number of free continuous physical memory pages in the target memory area meets the requirements of the second object, the number of continuous free memory pages can be made to meet the requirements of the second object by performing page migration on the target page that has been allocated to the first object. Because the pages in the target memory area have been prohibited from being added to the LRU cache before, there is no need to perform operations such as processor core binding and LRU cache refresh for subsequent page migration, which greatly improves the speed of memory allocation. Finally, after the page migration is completed, the required number of free physical memory pages is allocated to the requester (such as the second object) for use, thereby achieving the function of flexible and efficient allocation of memory resources.
[0015] In a possible implementation, the target memory area also includes a list to be migrated; the page migration of the target page belonging to the target memory area includes: adding one or more target pages belonging to the target memory area to the list to be migrated of the target memory area, and performing a migration operation on the pages in the list to be migrated. In the embodiment of the present application, the pages to be migrated (that is, the pages allocated to the first object in the first memory allocation request) can be uniformly added to the list to be migrated, so as to realize the centralized management of memory page migration. In the prior art, after the page migration of the target memory area, operations such as disabling the LRU cache, waking up the process, and refreshing the LRU cache are required. In the embodiment of the present application, because the target page is previously allocated to be used by the first object, when the target page is updated and it is determined that the target page belongs to the target memory area, the operation of adding the memory page to the LRU cache is prohibited, so that after the target page is added to the list to be migrated, there is no need to perform operations such as disabling the LRU cache, waking up the process, and refreshing the LRU cache, thereby reducing the time consumption caused by waiting for the work queue task scheduling during the allocation process, thereby improving the memory allocation efficiency of the system. Based on the above, the pages in the to-be-migrated list are migrated through a page migration solution to achieve reuse of the migrated pages.
[0016] In a possible implementation, the migration operation of the pages in the to-be-migrated list includes: based on the to-be-migrated list in the target memory area, copying the contents of the pages in the to-be-migrated list to the free pages in the memory except the target memory area; updating the address mapping relationship of the pages in the to-be-migrated list and the address mapping relationship of the free pages. In an embodiment of the present application, when processing the second memory allocation request, after the to-be-migrated list is prepared, the contents of the pages previously allocated to the first object in the to-be-migrated list are moved out to the free pages in the memory except the target memory area, and after copying and migrating the memory contents, the corresponding address mapping relationship is updated. That is, by updating the address mapping relationship of the pages in the to-be-migrated list and the address mapping relationship of the free pages, it is ensured that the system can correctly access and identify the memory contents after migration, and finally, the page migration operation is completed by updating the address mapping relationship. In summary, after the target memory area receives the second memory allocation request, the free pages previously allocated to the first object can be migrated back for use by the second object to improve the memory utilization rate, and also improve the memory allocation efficiency.
[0017] In a second aspect, an embodiment of the present application provides a memory allocation device applied to an electronic device, wherein the electronic device includes a memory, the memory includes a target memory area and a least recently used LRU cache, and may include:
[0018] A first judgment unit, configured to judge whether the target page belongs to the target memory area after the target page state in the electronic device is updated, wherein the target memory area includes a continuous physical memory;
[0019] An execution unit is used to prohibit adding the target page to the LRU linked list if the target page belongs to the target memory area.
[0020] In an embodiment of the present application, when the state of the target page is updated (for example, after being accessed or modified), it is first determined whether the target page belongs to the target memory area. If the target page belongs to the target memory area, it is forbidden to add the target page to the LRU cache. That is, when the state of the target page in the target memory area is updated, it is forbidden to add it to the LRU cache, so that the target memory area is unbound from the processor core in the subsequent memory allocation, and there is no need to wait for the extended time caused by the binding core (for example, the scheduling of a specific core, lock contention, etc.), so the memory allocation time can be reduced. For example, in a specific application scenario, the target memory area can be a specific area in the memory. When the memory is initialized, the system reserves one or more continuous physical memories for the specific area. When there are free memory pages in the specific area (for example, when no memory allocation request specifically for the specific area is received), other memory allocation requests in the system can be satisfied, and the corresponding memory page is forbidden to be added to the LRU cache, thereby simplifying the subsequent memory allocation process. In summary, the above operation determines whether the target page belongs to the target memory area, and prohibits it from being added to the LRU cache if it belongs to the target memory area, so that the subsequent continuous physical memory allocation can be executed without the need to be performed on the corresponding core, reducing the time consumption of continuous physical memory allocation, thereby improving the efficiency of continuous physical memory allocation. In addition, in some scenarios, prohibiting the target page from being added to the LRU cache can also avoid frequent cache replacement operations caused by frequent status updates of the target page, thereby speeding up the speed of continuous physical memory allocation.
[0021] In a possible implementation, the device further includes:
[0022] A first receiving unit receives a first memory allocation request, where the first memory allocation request is used to request a first number of continuous physical memory pages from the target memory area, where the first number of continuous physical memory pages is used by a first object;
[0023] A second judging unit, judging whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number;
[0024] The first allocation unit allocates the first number of free continuous physical memory pages in the target memory area to the first object for use if the number is greater than or equal to the first number.
[0025] In a possible implementation, the target memory area includes a bitmap;
[0026] The second judgment unit is specifically used to:
[0027] The allocation status of pages in the target memory area is queried according to the bitmap, and it is determined whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number.
[0028] In a possible implementation manner, the first allocation unit is specifically configured to:
[0029] The bitmap information of the target page of the bitmap in the target memory area is marked as being in an idle state.
[0030] In a possible implementation manner, the first determining unit is specifically configured to:
[0031] The target page in the memory is added to the first linked list for the first time, or the target page is moved from the second linked list to the third linked list or the target page is moved from the third linked list to the second linked list.
[0032] In a possible implementation, the target page includes structure information;
[0033] The first judgment unit is specifically used to:
[0034] Find the type of the target page through the structure information of the target page;
[0035] Based on the type of the target page, it is determined whether the target page belongs to the target memory area.
[0036] In a possible implementation manner, the device further includes:
[0037] A second receiving unit is used to receive a second memory allocation request, wherein the first memory allocation request is used to request to apply for a second number of continuous physical memory pages from the target memory area, and the second number of continuous physical memory pages is used for a second object;
[0038] A third judging unit, configured to judge whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the second number;
[0039] a migration unit, configured to perform page migration on the target page in the target memory area if the target page is greater than or equal to the second number;
[0040] The second allocation unit is used to allocate the second number of free continuous physical memory pages in the target memory area to the second object for use, and the second number of free continuous physical memory pages includes one or more target pages that belong to the target memory area and have completed page migration.
[0041] In a possible implementation, the target memory area also includes a to-be-migrated list;
[0042] The migration unit is used for:
[0043] One or more target pages belonging to the target memory area are added to a to-be-migrated list of the target memory area, and a migration operation is performed on the pages in the to-be-migrated list.
[0044] In a possible implementation, the migration unit is used to:
[0045] Based on the to-be-migrated list in the target memory area, copy the contents of the pages in the to-be-migrated list to the free pages in the memory except the target memory area;
[0046] The address mapping relationship between the pages in the to-be-migrated list and the idle pages is updated.
[0047] In a third aspect, an embodiment of the present application provides an electronic device, which may include: a processor, a memory; the memory includes a target memory area and a least recently used LRU cache; the processor is used to: when a state update occurs to a target page in the memory, determine whether the target page belongs to the target memory area; if the target page belongs to the target memory area, prohibit the target page from being added to the LRU cache. The embodiment of the present application determines through the processor whether a page in the memory has undergone a state update and belongs to the above-mentioned target memory area, and finally specifically prohibits the target page from being added to the LRU cache, so that it can be executed without being executed on the corresponding core, reducing the time consumption of memory allocation and improving the efficiency of memory allocation.
[0048] In a fourth aspect, an embodiment of the present application provides a computer storage medium for storing computer software instructions used in a processor in a memory allocation identification device provided in the second aspect, which includes a program designed for executing the above aspect.
[0049] In a fifth aspect, an embodiment of the present application provides a computer program, which includes instructions. When the computer program is executed by a computer, the computer can execute the process executed by the processor in the memory allocation device in the second aspect above. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the background technology, the drawings required for use in the embodiments of the present application or the background technology will be described below.
[0051] Figure 1A It is a schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application.
[0052] Figure 1B It is a schematic diagram of the structure of an electronic device taking CMA as an example of a target memory area provided in an embodiment of the present application.
[0053] Figure 1C It is a structural diagram of a memory provided in an embodiment of the present application.
[0054] Figure 1D These are some schematic diagrams of camera application interfaces provided by some embodiments of the present application.
[0055] Figure 2 It is a schematic diagram of a memory allocation process provided in an embodiment of the present application.
[0056] Figure 3 It is a schematic diagram of a memory allocation structure provided in an embodiment of the present application.
[0057] Figure 4 This is a schematic diagram of a first memory allocation request process provided in an embodiment of the present application.
[0058] Figure 5 This is a schematic diagram of a second memory allocation request process provided in an embodiment of the present application.
[0059] Figure 6 It is a schematic diagram of a memory allocation device provided in an embodiment of the present application.
[0060] Figure 7 It is a schematic diagram of the hardware structure of another electronic device provided in an embodiment of the present application.
[0061] Figure 8 It is a schematic diagram of an electronic device software system architecture provided in an embodiment of the present application. DETAILED DESCRIPTION
[0062] The embodiments of the present application are described below in conjunction with the drawings in the embodiments of the present application.
[0063] In the description of the embodiments of the present disclosure, the term "including" and similar terms should be understood as open inclusion, that is, "including but not limited to". The term "based on" should be understood as "based at least in part on". The term "one embodiment" or "the embodiment" should be understood as "at least one embodiment". The terms "first", "second", etc. may refer to different or the same objects. Other explicit and implicit definitions may also be included below.
[0064] The terms "first", "second", "third", and "fourth" in the specification and claims of the present application and the drawings 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. A process, device, system, product, or equipment that exemplarily includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units that are not listed, or may optionally include other steps or units inherent to the process, device, product, or equipment.
[0065] 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.
[0066] First, some terms in this application are explained to facilitate understanding by those skilled in the art.
[0067] (1) Page is the basic unit of memory management. The operating system uses the page table mechanism to map the virtual address space to the physical memory to achieve virtual memory management and access. The following are some important concepts and characteristics of pages in the operating system:
[0068] Free Page: A page that is not allocated to any process and can be used to allocate virtual memory to new page table entries or processes.
[0069] Allocated Page: A page that has been allocated to a process and is used to store the process's program, data, stack and other information.
[0070] It should be understood that in the context of the embodiments of the present application, the terms "page", "page", "memory page" may also be referred to as memory page or physical page, etc.
[0071] (2) Contiguous physical memory means that in a computer system, the address space of the physical memory is continuous, that is, the addresses are arranged in increasing order without gaps or breaks. This means that each physical memory address is adjacent to the previous address and the next address.
[0072] (3) Least Recently Used (LRU) cache is a common cache replacement strategy. Its basic idea is that when the cache space is full, the least recently used data item is eliminated from the cache to make room for new data items.
[0073] (4) Processor, also known as the Central Processing Unit (CPU), is one of the key components in a computer system. The processor is responsible for executing instructions, performing arithmetic and logical operations, and coordinating various operations in a computer system.
[0074] (5) Memory is an important component of a computer, also known as internal memory. It is used to temporarily store the calculation data in the CPU and the data exchanged with external storage such as hard disks. It is a bridge for communication between external storage and the CPU. All programs in the computer run in memory.
[0075] (6) A bitmap is a data structure used to represent a set of binary bits (or Boolean values), where each bit corresponds to information about a certain element or state. Each bit can take only two possible values, usually represented by 0 and 1. Bitmaps are often used in memory management, file systems, image processing, network programming, and other fields, where each bit represents information about a tag, state, or attribute.
[0076] (7) Address Mapping: Address mapping refers to the process of mapping addresses in one address space to another address space. In an operating system, address mapping usually occurs between virtual memory and physical memory, and between virtual addresses and physical addresses. The purpose of address mapping is to convert virtual addresses into corresponding physical addresses to achieve memory access and management.
[0077] (8) Continuous Memory Allocator (CMA): CMA is a function in the operating system kernel that is used to allocate large blocks of continuous memory in continuous physical memory areas for device drivers and other kernel components that require continuous memory. It allows drivers to access the above continuous memory areas through direct memory access (DMA), thereby improving device performance.
[0078] (9) Buddy System is a memory management algorithm used for dynamic memory allocation and release in operating systems. The main goal of the Buddy System is to solve the problems of memory fragmentation and efficiency. For example, the Buddy System divides the memory into a series of fixed-size blocks or page frames and manages them according to a binary tree structure. The size of each block is a power of 2 to better meet the needs of memory segmentation and merging.
[0079] First, the technical problem to be solved by this application is analyzed and proposed. In the prior art, a common memory allocation method may specifically include steps 1 and 2:
[0080] Step 1: Determine whether a status update has occurred based on the target page in memory.
[0081] Step 2: Add the target page whose status has been updated to the LRU cache.
[0082] This solution is usually used for memory allocation. Currently, it is mainly applicable to the scenario where there are enough free pages in the CMA area and they are borrowed by the Buddy system. However, it has the following disadvantages:
[0083] Disadvantages: Waking up the kworker process and refreshing the LRU cache requires creating a work queue such as a core-bound one, which may cause long scheduling delays.
[0084] Since, after adding the target page to the LRU cache, in the subsequent page migration process, after disabling the LRU cache, it is necessary to wake up the kworker process and refresh the LRU cache. This step requires creating a work queue bound to the processor core, which can only be executed on the corresponding core. When allocating a relatively large CMA memory, there is a high probability that a scheduling delay of more than ten milliseconds will be required. At the same time, it will also increase the CPU load: waking up the kworker thread may increase the CPU load because the kworker thread consumes CPU resources when executing, which may affect the execution of other tasks. Among them, the work queue is a subsystem in the operating system kernel, which is designed to asynchronously execute some functions that can be deferred. These functions will be executed in a specific kernel task context. For example, the work queue can be used to perform tasks such as refreshing the least recently used LRU cache. When it involves a work queue bound to a core (for example, bound to a specific processor core), it can ensure that a specific task is fully executed on the local CPU core. This work provides a significant advantage, that is, it can make full use of the locality of the CPU cache to improve execution efficiency. For the LRU cache refresh task, the operating system performs this work to ensure that the cache of the relevant memory page is in a clean state during the memory page migration operation. As for the refresh task of the LRU cache, the operating system will do this to ensure that during the memory page migration operation, the cache of the relevant memory page is in a clean state (for example, the memory page is not currently used, or has not been used recently). This refresh task for the LRU cache can avoid interfering with pages that may be in use during the migration process. However, since the refresh task needs to be executed on a specific CPU core (i.e., the previously bound processor core), there may be a waiting situation, that is, waiting for the corresponding CPU to perform the corresponding refresh operation, especially when trying to allocate a large amount of continuous memory access area (CMA) memory, it may cause a scheduling delay of tens of milliseconds or even longer, thereby affecting the efficiency of memory allocation.
[0085] In order to solve the problem that the current memory allocation method does not meet the actual business needs and achieve the goal of reducing the waiting delay, considering the shortcomings of the existing technology, the technical problems actually to be solved by this application may include some or all of the following aspects:
[0086] Improve the efficiency of memory allocation (the disadvantage of the solution), reduce the memory allocation process in the target memory area, and no longer need to bind the core, reduce the waiting time for delayed scheduling, and improve the efficiency of memory allocation.
[0087] In summary, the existing memory allocation device cannot meet the actual speed requirements of the device, which seriously affects its execution efficiency and waiting time. Therefore, the memory allocation method provided in this application is used to solve the above technical problems.
[0088] Based on the above, the present application embodiment provides an electronic device 01. Figure 1A , Figure 1A A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application, the electronic device can be any computing device that supports continuous physical memory allocation, such as electronic device 01, which can be a camera that supports memory allocation, a smart phone, a computer device, a computer, a video decoder (Video Decoder), a printer device, a scanner device, a projector device, a navigation device, a translator, smart glasses, a drone, a robot, a smart speaker, a smart door lock, a smart camera, a smart TV box, a tablet computer, an e-book reader, a digital photo frame (Digital Photo Frame), an HDMI digital audio and video interface standard (High-Definition Multimedia Interface), an audio processor (Audio Processor), a network adapter (Network Adapter) and other devices.
[0089] like Figure 1A As shown in , the electronic device 01 may include a processor 20 and a memory 10. The memory 10 is used to temporarily store the operation data in the processor 20 and the data exchanged with an external memory such as a hard disk. The memory 10 or the processor 20 may specifically be a chip or a chipset or a circuit board equipped with a chip or a chipset. The chip or chipset or the circuit board equipped with a chip or a chipset may work under the necessary software drive. Specifically,
[0090] Memory 10 is usually a volatile memory that loses power when the power is off. The content stored on it will be lost when the power is off. It can also be called internal memory or main memory. The memory 10 in this application includes a readable and writable operating memory, which is used to temporarily store the operation data in the processor 20, and to exchange data with the DMA controller or other external memory. It can be used as a storage medium for temporary data of the operating system or other running programs. Exemplarily, the operating system running on the processor 20 transfers the data to be calculated from the memory 10 to the processor 20 for calculation, and the processor 20 transmits the result after the calculation is completed.
[0091] The types of memory 10 may include one or more of dynamic random access memory (DRAM), static random access memory (SRAM), synchronous dynamic random access memory (SDRAM), etc. Among them, DRAM includes double data rate synchronous dynamic random access memory (Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM), referred to as DDR, second generation double data rate synchronous dynamic random access memory (DDR2), third generation double data rate synchronous dynamic random access memory (DDR3), fourth generation low power double data rate synchronous dynamic random access memory (Low Power Double Data Rate 4, LPDDR4) and fifth generation low power double data rate synchronous dynamic random access memory (Low Power Double Data Rate 5, LPDDR5), etc.
[0092] Exemplarily, the functional areas in the memory 10 may include a target memory area 100, an LRU cache 101, other storage (memory) areas 102, etc., wherein:
[0093] Target memory area 100: is a memory partition in memory 10, which can store continuous physical memory and process corresponding continuous physical memory allocation requests. For example, see Figure 1B, the target memory area 100 may be a CMA memory area 100-A, which can directly transfer data between peripherals and memories or between memories without being processed by the processor 20, thereby improving the efficiency of the processor 20 and reducing the overhead of data transmission. For example, the CMA memory area 100-A is a fixed-size memory area reserved when the system starts. As the CMA memory area 100-A, this area is usually idle and can be borrowed by the Buddy system. It is only when needed that it is migrated back and reallocated to peripherals or kernel modules that require continuous memory, thereby avoiding waste and fragmentation of reserved exclusive memory. Furthermore, the memory allocation process of the CMA area requires the assistance and management of the processor 20, but the data transmission does not require the participation of the processor 20. For example, when the CMA memory area 100-A is allocating continuous memory and is insufficient, the kernel's page migration mechanism will be called by the processor 20 to migrate the occupied pages in the CMA memory area 100-A to other areas, thereby freeing up continuous pages; for example, when the CMA memory area 100-A releases continuous memory, the memory will be returned to the operating system for management and can be applied for by other applicants; and when the CMA memory area 100-A is performing data transmission, direct communication between peripherals and memory or between memory and memory can be achieved through the DMA controller. This process does not require the participation of the processor 20, which can reduce the burden on the processor 20 and the multiple copies of data between the processor 20 and the memory.
[0094] LRU cache 101: When a piece of data is rarely accessed in the recent past, it may still not be accessed in the future. Therefore, when it is necessary to make room for a new data page, the LRU cache 101 can select the page that has not been accessed for the longest time for replacement. For example, when the processor 20 needs to access data from the memory, sometimes the data is not in the memory, but is stored on a hard disk or other storage device, which will cause the so-called "page miss". When a page miss occurs, the operating system must take out the required data from the storage device and then store it in the memory. However, since the size of the memory is limited, it may be necessary to replace some data that is already in the memory. In order to improve execution efficiency, the processor 20 usually frequently accesses the memory to obtain data. The processor 20 needs to access data quickly to maintain efficient instruction execution, and the memory 10, as a high-speed storage medium, provides this capability for the processor 20. However, due to the limited memory space, a strategy is needed to manage which data should remain in the memory and which data should be replaced. Such a strategy is implemented in the LRU cache 101. By effectively using the LRU cache 101 , the system can ensure that the most recent and frequently used data is always kept in the memory, thereby improving the data access efficiency of the processor 20 .
[0095] Other storage (memory) areas 102: There are a large number of pages in the other storage (memory) area 102, which are usually stored in various memory partitions. For example, some pages can be managed by the Buddy system, and the physical memory is divided into blocks of power of 2, called "orders". Order 0 corresponds to the size of a single page, order 1 corresponds to the size of two consecutive pages, and so on. This system works by dividing the memory into blocks of power of 2, and allocating and merging these blocks in an efficient way. These blocks of different sizes are called "buddies". When the kernel needs to allocate memory, the BuddySystem tries to find an available block in the free list of the appropriate order. If a block of the appropriate size cannot be found, it will try to split a larger block. For example, if an order 2 block is needed, but there is no available block, it can take an order 3 block and split it into two order 2 blocks. When memory is released, the Buddy System checks whether the adjacent blocks are also free, and if so, they are merged into a larger block. If an order 2 block is released, and its "buddy" block is also free, the two blocks are merged into an order 3 block. Merging and splitting are the core operations of the Buddy System. By splitting blocks into required sizes, or by merging adjacent free blocks to create larger blocks, the Buddy System can flexibly manage memory while minimizing fragmentation.
[0096] Processor 20: can run an operating system, file system or application program, etc., to control multiple hardware or software connected to the processor 20, and can process various data and perform operations. The processor 20 can load the memory allocation request into the memory 10 through the DMA controller, and call the instructions or data to be calculated into the processor 20 for calculation. When the calculation is completed, the processor 20 temporarily stores the result in the memory 10. The processor 20 may include one or more processing units (also called processor cores),
[0097] Exemplarily: the processor 20 may include one or more of a central processing unit (CPU), an application processing unit (AP), a modem processing unit, a graphics processing unit (GPU), an image signal processing unit (ISP), a video codec unit, a digital signal processing unit (DSP), a baseband processing unit, and a neural-network processing unit (NPU). Among them, different processing units may be independent devices or integrated in one or more devices. Optionally, a memory may also be provided in the processor 20 for storing instructions and data. In some embodiments, the memory in the processor 20 is a cache memory (Cache). The Cache may store instructions or data that the processor 20 has just used or circulated. If the processor 20 needs to use the instruction or data again, it may be directly called from the Cache. Repeated access is avoided, the waiting time of the processor 20 is reduced, and the efficiency of the system is improved.
[0098] It should be noted that the computer program for implementing any memory allocation method in the present application (referred to as the memory allocation program, which may include relevant instructions and relevant data, which will not be repeated later) involves and includes multiple functions, one of which is the determination of the memory allocation request type by the processor 20 side, and the different processing of different types of memory allocation requests; the other part is the operation of prohibiting the processor 20 from adding the target page in the target memory area 100 to the LRU cache after the status is updated. Finally, through the multiple functions included in the above-mentioned memory allocation, the function of efficient memory allocation between the memory 10 and the processor 20 is realized. This will be further explained in the subsequent embodiments and will not be described in detail here.
[0099] Understandably, Figure 1A or Figure 1BThe structure of the memory 10 in the embodiment of the present application is only some exemplary implementations provided in the embodiments of the present application. The structure of the memory 10 in the embodiments of the present application includes but is not limited to the above implementations.
[0100] It can also be understood that the processor 20 and the memory 10 can communicate with each other through the system bus, or can communicate through other connection methods, and the embodiment of the present application does not specifically limit this. The structure illustrated in the embodiment of the present application does not constitute a specific limitation of the electronic device 01. In other embodiments of the present application, the electronic device 01 may include more or fewer components than shown in the figure, or combine certain components, or split certain components, or arrange the components differently. The illustrated components can be implemented in hardware, software, or a combination of software and hardware.
[0101] Combine the following Figure 1A-Figure 1C The structure of the memory 10 shown in the figure specifically introduces the memory allocation method provided by the embodiment of the present application. In the memory allocation method provided by the embodiment of the present application, after the processor 20 processes the first memory allocation request, the target page in the memory 10 is prohibited from being added to the LRU cache 101, and then the relevant process of processing the second memory allocation request is processed. Further, after the processor 20 sends the first memory allocation request to the memory 10, the processor 20 will first determine whether the number of free pages remaining in the target memory area 100 is greater than or equal to the number of pages required for the memory allocation request. If it is greater than or equal to the number, the free page is directly allocated to the first object for use; if it is less than, the allocation fails. Further, when the state of the target page in the memory 10 is updated, the processor 20 will first determine whether the target page belongs to the target memory area 100. If it does, the target page is prohibited from being added to the LRU cache 101. Furthermore, if a second memory allocation request is received from a second object, the processor 20 will first determine whether the number of free pages remaining in the target memory area 100 is greater than or equal to the number of pages required for the memory allocation request. If it is greater than or equal to the number, the processor 20 will check the status of the free pages in the target memory area 100, query the target pages allocated to the first object in the free pages, and add them to the list to be migrated. After migrating one or more target pages in the list to be migrated, the processor 20 will allocate the memory pages to the second object for use.
[0102] The memory allocation method provided by the present application is specifically described below through how the processor 20 prohibits the target page in the memory 10 from being added to the LRU cache 101 after processing the first memory allocation request, and then processes the second memory allocation request.
[0103] (I) How does the processor 20 process the first memory allocation request?
[0104] In the embodiment of the present application, the following points will be used to exemplify how the processor 20 processes the first memory allocation request: 1. After receiving the first memory allocation request, how the processor 20 determines whether the number of free pages in the target memory area 100 is greater than or equal to the first number of the memory allocation request. 2. If it is greater than or equal to the first number, the first number of free pages in the target memory area 100 is allocated to the first object for use.
[0105] 1. How does the processor 20 determine whether the number of free pages in the target memory area 100 is greater than or equal to the number of memory allocation requests after receiving the memory allocation request?
[0106] To determine whether the number of free continuous physical memory pages in the target memory area 100 is greater than or equal to the first number, the allocation status of pages in the target memory area 100 can be queried based on the bitmap information to determine whether the number of free pages in the target memory area 100 is greater than or equal to the first number.
[0107] Take the target memory area 100 as the CMA memory area as an example. For details, see Figure 1C The viewing of bitmap information in the embodiment of the present application mainly includes the following process:
[0108] There is a corresponding bitmap in the CMA memory area to indicate its allocation status. The number of pages represented by each bit in the bitmap is determined by the order_per_bit field. For example, if order_per_bit is 0, each bit represents one page; if order_per_bit is 1, each bit represents two pages; if order_per_bit is 2, each bit represents four pages, and so on. The processor 20 calls the Buddy system to view the bitmap information of the CMA memory area to find out its allocation status. Specifically, the Buddy system determines its range in physical memory based on the base_pfn and count fields of the CMA memory area, and then determines which pages have been allocated and which pages are still free based on the bitmap and order_per_bit fields of the CMA memory area.
[0109] 2. If it is greater than or equal to the first number, a first number of free pages in the target memory area 100 are allocated to the first object for use.
[0110] The processor 20 may allocate a first number of free continuous physical memory pages in the target memory area 100 to the first object for use, and further mark the bitmap information of the target page of the bitmap in the target memory area 100 as being in an idle state.
[0111] Taking the target memory area 100 as a CMA memory area as an example, marking the bitmap information as an idle state in the embodiment of the present application mainly includes the following process:
[0112] When the system is initialized, a CMA area is divided from the reserved memory and described by a structure struct cma. There is a bitmap member in struct cma, which is used to record the allocation status of each page in the CMA area. 0 means free and 1 means allocated. In the above scheme, when the pages in the CMA area are free, they will be allocated to the buddy system for management. This process is implemented by the cma_init_reserved_areas function. This function will traverse all pages in the CMA area, set them to the MIGRATE_CMA type, and call the __free_pages function to release them to the buddy system. Specifically, the CMA bitmap monitors whether each page can be migrated. As long as a page can be migrated, it is "free" in the bitmap information of the CMA memory area, regardless of whether it has been allocated by the regular kernel. Therefore, when the free page in the CMA memory area is allocated to the Buddy system, the bitmap information of the free page will remain the same, and the bitmap number is still 0, which is the free state.
[0113] (II) How does the processor 20 prohibit the operation of adding the target page in the memory 10 to the LRU cache 101?
[0114] In the embodiment of the present application, the following points will be used to exemplify how the processor 20 operates according to the change in the target page status: 1. When the status of the target page in the memory 10 is updated, determine whether the target page belongs to the target memory area 100. 2. If the target page belongs to the target memory area 100, it is prohibited to add the target page to the LRU cache 101.
[0115] in,
[0116] 1. When the state of the target page in the memory 10 is updated, it is determined whether the target page belongs to the target memory area 100.
[0117] Exemplarily, when the target page in the memory is added to the first linked list for the first time, or when the target page is moved from the second linked list to the third linked list or when the target page is moved from the third linked list to the second linked list, it is determined whether the state of the target page has been updated, and then the type of the target page is found based on the structure information by checking the structure information contained in the target page. Finally, the processor 20 determines whether the target page belongs to the target memory area 100 based on the type of the target page.
[0118] 2. If the target page belongs to the target memory area 100 , it is prohibited to add the target page to the LRU cache 101 .
[0119] Exemplarily, after first determining that the target page belongs to the target memory area 100, the target page is prohibited from being added to the LRU cache.
[0120] In the embodiment of the present application, the method for prohibiting the target page from being added to the LRU cache 101 may include but is not limited to the method flow in the following situations:
[0121] Compile a custom kernel: By changing the implementation details of LRU, you can completely control which pages are added to the LRU list, thereby prohibiting the target page from being added to the LRU cache. Exemplarily, modifications can be made in the following aspects: When adding a new page cache, determine whether the target page meets the conditions (for example, whether it belongs to the CMA memory area). If so, do not call the lru_cache_add function, but directly release the page or add it to another linked list; when accessing an existing page cache, determine whether the target page meets the conditions. If so, do not call the mark_page_accessed function, but directly release the page or add it to another linked list; when reclaiming a page cache in an inactive linked list, determine whether the target page meets the conditions. If so, do not call the shrink_page_list function, but directly release the page or add it to another linked list.
[0122] It is understandable that the above method of prohibiting the target page from being added to the LRU cache 101 may also include other implementations, which are not specifically limited in the embodiment of the present application.
[0123] (III) Processor 20 processes the second memory allocation request
[0124] In the embodiment of the present application, the following points will be used to exemplify how the processor 20 handles the second memory allocation request: 1. How the processor 20 determines whether the number of free pages in the target memory area 100 is greater than or equal to the number of the second memory allocation request after receiving the second memory allocation request. 2. If the number of free pages in the target memory area 100 is greater than or equal to the number of the second memory allocation, a page migration operation is performed on the free pages in the target memory area 100, and after the page migration is completed, the free pages are allocated to the second object for use. Among them,
[0125] 1. How does the processor 20 determine whether the number of free pages in the target memory area 100 is greater than or equal to the number of the second memory allocation request after receiving the second memory allocation request?
[0126] Exemplarily, determining whether the number of free continuous physical memory pages in the target memory area 100 is greater than or equal to the second number of the second memory allocation request may be to query the allocation status of pages according to the bitmap in the target memory area 100, and determine whether the number of free continuous physical memory pages in the target memory area 100 is greater than or equal to the first number. The allocation status is queried according to the bitmap in the target memory area 100, and finally it is determined whether the number of free memory pages meets the first number of the allocation request.
[0127] 2. If the number of free pages in the target memory area 100 is greater than or equal to the number of the second memory allocation request, a page migration operation is performed on the free pages in the target memory area 100, and after the page migration is completed, the free pages are allocated to the second object for use.
[0128] Take the target memory area 100 as the CMA memory area as an example. For details, see Figure 1C In the embodiment of the present application, marking the bitmap information as an idle state is mainly as follows:
[0129] The CMA memory area monitors the usage of the target pages previously allocated to the Buddy system for management. When the number of free pages in the CMA memory area is greater than or equal to the number of the second memory allocation request, one or more blocks of continuous physical memory will be allocated. If these target pages (such as the target pages previously allocated to the first object) are in the one or more blocks of continuous physical memory, then the target pages will be added to the list to be migrated in the CMA memory area, and then the contents of these target pages will be copied to the free pages of other storage (memory) areas 102, and the address mapping relationship between the pages in the list to be migrated and the address mapping relationship between the free pages of the other storage (memory) areas 102 will be updated.
[0130] For example, in the embodiment of the present application, updating the address mapping relationship may be processed in the following manner:
[0131] The processor 20 may include a TLB (Translation Look-aside Buffer), which is a high-speed cache used to store recently used page table entries. When the processor 20 accesses a virtual address, it will first search in the TLB. If it is found, it will directly obtain the physical address. This process is called a TLB hit. If it is not found, it will search in the page table and update the search result to the TLB. This process is called a TLB miss. If the processor 20 accesses a virtual address, it may find that the page corresponding to the address does not exist in the physical memory. This process is called a page fault. This may be because the page has never been accessed or has been swapped out to the disk by the operating system. When a page fault exception occurs, the processor 20 will suspend the current process and execute the page fault exception handler. The program will search for the corresponding data on the disk according to the virtual address, load it into the physical memory, update the page table and the TLB, and then resume the execution of the current process. In this way, address mapping is completed.
[0132] The following takes the electronic device 01 as a smart phone as an example, and further describes the memory allocation method in this application in combination with actual application scenarios.
[0133] For example, see Figure 1D , Figure 1D The following are some schematic diagrams of the camera application interface provided by some users in the embodiments of the present application. Figure 1D The following is an exemplary description of how the memory 10 and the processor 20 interact based on the memory allocation method in the present application after the user clicks on the camera application and starts taking pictures.
[0134] Assume that the user needs to take a photo at this time. At this time, in the user interface 021, when the camera application 0211 is not started, if there is an idle memory page in the target memory area 100, it will be allocated by the processor 20 to the first object (for example, an application that applies for physical memory from the target memory area 100 through the Buddy system, such as a file system, a browser, etc.) for use. However, when the state of the memory page is updated, it will be further determined whether the memory page belongs to the target memory area 100; if it does, the memory page is prohibited from being added to the LRU cache 101. Then the user enters the camera photo interface 022 by clicking the camera application 0211. When the user starts the camera application, the system first loads the application and allocates the required resources. The application interacts with the underlying camera hardware abstraction layer (HAL) to initialize the camera sensor, image signal processor (ISP) and other related hardware. In order to capture image frames, the system needs to allocate space for these frames in memory. Since the ISP and other hardware may need to access continuous physical memory blocks, continuous physical memory pages in the target memory area 100 will be used at this time. The application or the underlying driver requests the kernel to allocate continuous memory for the image frame to be captured through the processor 20. The processor 20 checks the current memory usage of the target memory area 100, and then tries to find a large enough continuous memory block. If found, the memory in the target memory area 100 will be retained and allocated to the requester (the second object is also the camera application in the embodiment of the present application). If not found, the processor 20 will scan the migratable pages in the target memory area 100, add them to the list to be migrated, until the number of continuous memory blocks is greater than or equal to the number of memory allocation requests; then migrate the content of the page in the migration list, update the mapping address, and finally allocate it to the requester (i.e., the camera application). The user takes a photo by clicking the photo button 0221. After clicking the photo button, the photo picture shown in the user interface 023 is obtained. Because there is no need to perform operations such as disabling the LRU cache, waking up Kworker, refreshing the LRU cache, etc., it is not necessary to execute on the corresponding core, so the efficiency of memory allocation is improved, and the speed of camera application startup is ultimately improved.
[0135] Based on the above Figure 1A-Figure 1D The structure of the electronic device provided in the present application is combined with the memory allocation method of the present application to specifically analyze and solve the technical problems raised in the present application.
[0136] See also Figure 2 , Figure 2 is a flowchart of a memory allocation method provided in an embodiment of the present application, which can be applied to the above Figure 1AIn the above system architecture, the memory 10 and the processor 20 can be used to support and execute Figure 2 The method flow steps S100 to S103 shown in FIG. Figure 2 Described from the processor side, the method may include the following steps S101 to S102, and may optionally include steps S100 and S103.
[0137] Step S101: determining whether a state update occurs on a target page in the memory, and determining the memory region to which the target page belongs.
[0138] Specifically, the processor 20 monitors whether the state of the target page has changed, and if so, determines the memory area to which the target page belongs. For example, the memory area to which the target page belongs can be determined by the type of the target page.
[0139] Taking the target memory area 100 as a CMA area as an example, the memory allocation method in the embodiment of the present application is described as follows:
[0140] For example, how to determine whether the state of the target page in the memory 10 is updated can be seen in Figure 3 , Figure 3 The schematic diagram of the structural flow of memory allocation of the present invention. In order to effectively manage the memory pages in the target memory area 100, the operating system can use the least recently used (Least Recently Used, LRU) linked list, such as an active linked list and an inactive linked list. When the page state changes, the memory page needs to be moved between the above two linked lists. The moving process involves locking the linked list. This change of page state is very frequent in the operating system kernel. In order to reduce the locking of the linked list, a structure of LRU cache is added. When the state of the page changes, it is first moved to the corresponding LRU cache linked list. When it meets certain conditions, it is actually moved to the corresponding LRU linked list. Therefore, when the target memory page in the target memory area 100 is allocated to the Buddy system for management, the above-mentioned target page state update may include the target page in the memory 10 being added to the first linked list (such as the LRU linked list) for the first time, or the target page is moved from the second linked list (such as the ActiveLRU linked list) to the third linked list (InActiveLRU linked list), or the target page is moved from the third linked list to the second linked list, etc. Optionally, regarding determining the memory area to which the target page belongs, the type of the target page may be found based on the structure information of the target page, and then based on the type, it may be determined whether the target page belongs to the CMA memory area.
[0141] In the above example process, the target memory area 100 is described as a CMA memory area, and can be applied to the situation where the free pages based on the CMA memory area have been allocated and managed by the Buddy system; in addition, when the above-mentioned status update occurs to the above-mentioned target page, the bitmap information of the target page can still be marked as idle.
[0142] Step S102 : if it is determined that the memory area belongs to the target memory area 100 , it is prohibited to add the target page to the LRU cache 101 .
[0143] Exemplarily, in an embodiment of the present application, after it is determined that the memory area belongs to the target memory area 100, the target page will be prohibited from being added to the LRU cache 101. This operation can prevent pages with status updates outside the above-mentioned CMA memory area in the memory from being prohibited from being added to the LRU cache 101.
[0144] Regarding how to prohibit the target page from being added to the LRU cache 101, please refer to the relevant description of the aforementioned method embodiment, which will not be elaborated in detail here.
[0145] In summary, the above operation determines whether the target page belongs to the target memory area 100, and prohibits it from being added to the LRU cache 101 if it belongs to the target memory area 100, so that subsequent memory allocation does not need to be executed on the corresponding core, reducing the delay of refreshing the LRU cache task waiting to be scheduled, thereby improving the efficiency of memory allocation.
[0146] Optionally, before the above step S101 is performed, the following step S100 may be further included:
[0147] Step S100: the processor 20 allocates a first number of free continuous physical memory pages to a first object in the target memory area 100 according to a first memory allocation request.
[0148] Specifically, the memory 10 includes the target memory area 100, and the target memory area 100 includes a bitmap.
[0149] Exemplarily, after receiving the first memory allocation request, the target memory area 100 determines whether the number of free pages in the target memory area 100 is greater than or equal to the requested number. If it is greater than or equal to the number, these free pages are allocated to the first object.
[0150] As for how to allocate the first number of free continuous physical memory pages to the first object according to the first memory allocation request, for example, see Figure 4 , Figure 4This is a schematic diagram of the first object memory allocation process of the present invention. The allocation process may include the following steps S301 to S305.
[0151] Step S301: Initialize creation and allocation of target memory area 100 memory space.
[0152] The following takes the target memory area 100 as a CMA memory area as an example to exemplarily describe the method of creating and allocating the target memory area 100 in the embodiment of the present application:
[0153] When the memory starts to be initialized, a continuous physical memory is reserved for the CMA memory area to meet the needs of continuous physical memory allocation requests. This reserved continuous physical memory area is divided when the system starts and is used for subsequent memory allocation. By reserving the CMA memory area, when there is a continuous physical memory allocation request, the memory management mechanism can give priority to this area to meet the demand. Since this continuous physical memory area has been reserved during initialization, the memory management mechanism can more easily perform memory allocation and release operations without complex searches and adjustments in the entire memory space to meet continuous physical memory allocation requests.
[0154] Step S302: The first object sends a first memory allocation request to the processor 20.
[0155] Step S303: The processor 20 sends a first memory allocation request to the target memory area 100.
[0156] Taking the target memory area 100 as the CMA memory area as an example, illustratively, the processor 20 will send a first memory allocation request to the Buddy system, and then the Buddy system determines whether the number of free pages in the memory 10 it manages is greater than or equal to the first number of the above-mentioned first memory allocation request. If the number of free pages is less than the above-mentioned first number, the processor 20 sends the first memory allocation request to the CMA memory area through the Buddy system.
[0157] Step S304: If the number of free pages in the target memory area 100 is greater than or equal to the first number of the first memory allocation request, the free pages in the target memory area 100 will be allocated to the first object.
[0158] In an embodiment of the present application, the processor 20 can check the allocated status of the pages based on the bitmap information in the target memory area 100, thereby obtaining the number of free pages in the target memory area 100. If the number is greater than or equal to the first number of the first memory allocation request, these free pages will be allocated to the first object.
[0159] Taking the target memory area 100 as the CMA memory area as an example, the processor 20 can first query the allocation status of pages in the CMA memory area according to the bitmap. Further, the processor 20 queries the number of free pages in the above-mentioned CMA memory area through the Buddy system. If the number of free pages in the above-mentioned CMA memory area is greater than or equal to the first number, the first number of free pages in the CMA memory area can be handed over to the Buddy system for management, and the processor 20 then allocates the above-mentioned first number of free pages to the first object for use through the Buddy system.
[0160] Step S305: After allocating a first number of free pages in the target memory area 100 to the first object, mark the bitmap information of these target pages.
[0161] Exemplarily, after allocating free continuous physical memory pages in the target memory area 100 to the first object for use, the processor 20 marks the bitmap information of the first number of target pages in the above-mentioned target memory area 100 as free, so as to quickly find allocatable free pages in subsequent memory allocation requests.
[0162] Taking the target memory area 100 as the CMA memory area as an example, the steps of this method can be used in the scenario where after the processor 20 receives the first memory allocation request, when the free pages of other storage (memory) areas 102 managed by the Buddy system are less than the first number of the above-mentioned first memory allocation request, the processor 20 sends the first memory allocation request to the CMA memory area through the Buddy system, and when the free pages in the CMA memory area are greater than or equal to the first number, the free pages in the CMA memory area are handed over to the Buddy system for management and then allocated to the first object. The embodiment of the present application avoids memory fragmentation or discontinuous memory allocation by accurately determining the number of free continuous physical memory pages in the CMA memory area and allocating them to the first object for use, thereby improving the utilization efficiency of memory resources, ensuring the rationality of resource allocation, and ultimately improving the utilization rate of memory allocation. Exemplarily, since for the CMA memory area, the free pages allocated to the Buddy system for management are not used in the memory allocation request specifically for the CMA memory area, the CMA memory area will mark the above free pages as free, so that when a memory allocation request specifically for the CMA memory area is actually received, the allocatable free pages can be quickly found and reclaimed, and allocated to the object of the above memory allocation request specifically for the CMA memory area.
[0163] Optionally, after the above step S102 is performed, the following step S103 may be further included:
[0164] Step S103: the processor 20 allocates a second number of free continuous physical memory pages in the target memory area 100 to the second object for use according to the second memory allocation request.
[0165] Specifically, the target memory area 100 also includes a list to be migrated.
[0166] As for how the target memory area 100 allocates the second number of free continuous physical memory pages to the second object according to the second memory allocation request, please refer to Figure 5 , Figure 5 This is a schematic diagram of a second memory allocation request process of the present invention. The allocation process may include the following steps S401 to S406.
[0167] Step S401: The second object sends a second memory allocation request to the processor 20.
[0168] Step S402: After receiving the second memory allocation request, the processor 20 sends the second memory allocation request to the target memory area 100.
[0169] Step S403: Determine whether the number of free pages in the target memory area 100 is greater than or equal to the second number of the second memory allocation request.
[0170] Taking the target memory area 100 as a CMA memory area as an example, there can be a corresponding bitmap in the CMA memory area to indicate its allocation status. Furthermore, the number of pages represented by each bit in its bitmap is determined by the order_per_bit field, and the processor 20 can check the bitmap information of the CMA memory area to learn about its allocation status. Specifically, the processor 20 can determine its range in the physical memory based on the base_pfn and count fields of the CMA memory area, and then determine which pages have been allocated and which pages are still free based on the bitmap and order_per_bit fields of the CMA memory area. The processor 20 then determines whether the number of free pages in the target memory area 100 is greater than or equal to the second number of the second memory allocation request based on the allocation status of the pages.
[0171] Step S404: Determine whether page migration is required.
[0172] Exemplarily, if the number of free pages in the target memory area 100 is greater than or equal to the second number of the second memory allocation request, one or more blocks of continuous physical memory will be allocated to determine whether the target pages previously allocated to the first object are in the one or more blocks of continuous physical memory. If these target pages are in the one or more blocks, it is determined that page migration is required. In the prior art, before performing a page migration task, it is necessary to first refresh the LRU cache. Furthermore, when performing page migration, it is necessary to refresh the LRU cache of all CPUs to the LRU. The LRU cache is a list in the CPU cache and is a Per-CPU variable. Each CPU has a corresponding list. Therefore, when it is necessary to refresh the LRU cache of an unspecified CPU, it is necessary to create a work queue task bound to the CPU in order to execute the task of refreshing the LRU cache. Per-CPU variables When a per-CPU variable is created, each processor in the system will obtain its own copy of this variable, and almost no locking is required when accessing per-CPU variables; however, in the prior art, when performing continuous physical memory allocation, the memory originally allocated in the CMA memory area needs to be migrated to other places, so that this area is freed up for continuous physical memory allocation. Before performing this page migration, it is necessary to refresh the cache list in the CPU to the LRU linked list in the memory. He creates work queues on multiple CPU cores, waits for each core to wake up the tasks in the work queue, and refreshes the LRU cache list separately, which often causes a delay in CMA memory allocation. In the embodiment of the present application, because the pages in the target memory area 100 are prohibited from being added to the LRU cache after the status update occurs, there is no need to refresh the cache list in the CPU to the LRU linked list in the memory, and create work queues on multiple CPU cores, wait for each core to wake up the tasks in the work queue, and refresh the LRU cache list separately, thereby speeding up the memory allocation speed and improving the memory allocation efficiency.
[0173] Step S405: If page migration is required, one or more target pages allocated to the first object are added to a list to be migrated.
[0174] For example, if the target pages allocated to the first object need to be migrated, the target pages will be added to the to-be-migrated list of the target memory area 100. After the addition is completed, the page migration operation will be uniformly performed on the to-be-migrated list.
[0175] Step S406: After performing page migration according to the to-be-migrated list, a second number of free pages in the target memory area 100 are allocated to the second object for use.
[0176] Exemplarily, the content of the page previously allocated to the first object in the list to be migrated is moved out to the free page of other storage (memory) area 102, and after the content of the page is copied and migrated, the corresponding address mapping relationship is updated. By updating the address mapping relationship of the page in the list to be migrated and the address mapping relationship of the free page, it is ensured that the system can correctly access and identify the memory content after migration, and finally, the page migration operation is completed by updating the address mapping relationship.
[0177] Exemplarily, taking the target memory area 100 as a CMA memory area as an example, the specific process of the processor 20 allocating a second number of free continuous physical memory pages in the target memory area 100 to the second object for use according to the second memory allocation request is as follows:
[0178] First, the processor 20 calls the cma_alloc function to allocate a certain number of continuous physical pages from the specified CMA memory area. The function calls the alloc_contig_range function to try to allocate a continuous page frame number (PFN) range. The alloc_contig_range function traverses the specified PFN range and checks whether the physical page corresponding to each PFN can be migrated. If it is a free page or a reserved page, it is directly marked as the MIGRATE_ISOLATE type, indicating that the page has been isolated and will not be allocated or released by other processes.
[0179] If a physical page that has been allocated is encountered, the isolate_migratepages_range function is called to add the page and its adjacent pages of the same type to a migration list, and also marked as MIGRATE_ISOLATE type. After completing the traversal of the PFN range, the alloc_contig_range function will check whether there are physical pages that need to be migrated. If so, the migrate_pages function is called to perform the page migration operation. The migrate_pages function is the core function of page migration. It traverses each physical page in the migration list and calls the get_new_page function to allocate a new target page for each physical page.
[0180] The get_new_page function selects the appropriate memory domain (zone) and migration type (migratetype) to allocate the new page according to the migration reason (such as MR_CONTIG_RANGE). After obtaining the new page, the migrate_pages function calls the unmap_and_move function to perform the specific migration operation. The unmap_and_move function first obtains the locks (PG_locked) of the old page and the new page, and then checks whether the old page can be migrated, such as whether it is being written back (PG_writeback) or locked (PG_locked). If the old page can be migrated, the try_to_unmap function is called to release the mapping relationship between the old page and its corresponding virtual address. The try_to_unmap function calls different unmap functions according to the source of the old page (user space or kernel space), such as try_to_unmap_one or try_to_unmap_ksm. The main function of these functions is to find the location of the old page in the page table through the reverse mapping mechanism (rmap) and invalidate it (pte_clear or zap_pte_range, etc.).
[0181] After unmapping the old page, the unmap_and_move function calls the move_to_new_page function to copy the content and metadata of the old page to the new page, and establishes a mapping relationship between the new page and its corresponding virtual address. The move_to_new_page function first calls the migrate_page_copy function to copy the content and some metadata (such as PG_dirty, etc.) of the old page to the new page, and then calls the rmap_walk function to traverse all reverse mapping items of the old page, and calls the migrate_page_move_mapping callback function to update each reverse mapping item to point to the new page. After completing the migration from the old page to the new page, the unmap_and_move function releases the locks of the old and new pages, and calls the put_page function to reduce the reference count of the old page. If the reference count becomes 0, it means that the old page is no longer used by any process and can be released. After completing the migration of all physical pages in the migration list, the migrate_pages function returns the migration result. If successful, it means that a continuous physical memory has been freed up in the CMA memory area, which can be used by the device driver and allocated to the second object.
[0182] The method of the embodiment of the present application is described in detail above, and the related device of the embodiment of the present application is provided below.
[0183] See also Figure 6 , Figure 6 It is a structural schematic diagram of a memory allocation device provided in an embodiment of the present application. The memory allocation device 50 may include a first receiving unit 501, a second judgment unit 502, a first allocation unit 503, a first judgment unit 504, an execution unit 505, a second receiving unit 506, a third judgment unit 507, a migration unit 508, and a second allocation unit 509, wherein each unit is described in detail as follows.
[0184] A first determination unit 504 is used to determine whether the target page belongs to the target memory area after the target page state in the electronic device is updated, and the target memory area includes continuous physical memory;
[0185] Execution unit 505: configured to prohibit adding the target page to the LRU cache if the target page belongs to the target memory area.
[0186] In a possible implementation, the device further includes:
[0187] A first receiving unit 501 receives a first memory allocation request, where the first memory allocation request is used to request a first number of consecutive physical memory pages from the target memory area, where the first number of consecutive physical memory pages is used by a first object;
[0188] The second determining unit 502 determines whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number;
[0189] First allocation unit 503: If it is greater than or equal to the first number, allocate the first number of free continuous physical memory pages in the target memory area to the first object for use.
[0190] In a possible implementation, the target memory area includes a bitmap;
[0191] The second judgment unit 502 is specifically used for:
[0192] The allocation status of pages in the target memory area is queried according to the bitmap, and it is determined whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number.
[0193] In a possible implementation, the first allocating unit 503 is specifically configured to:
[0194] The bitmap information of the target page of the bitmap in the target memory area is marked as being in an idle state.
[0195] In a possible implementation, the first determining unit 504 is specifically configured to:
[0196] The target page in the memory is added to the first linked list for the first time, or the target page is moved from the second linked list to the third linked list or the target page is moved from the third linked list to the second linked list.
[0197] In a possible implementation, the target page includes structure information;
[0198] The first judging unit 504 is specifically used for:
[0199] Find the type of the target page through the structure information of the target page;
[0200] Based on the type of the target page, it is determined whether the target page belongs to the target memory area.
[0201] In a possible implementation manner, the device further includes:
[0202] A second receiving unit 506 is configured to receive a second memory allocation request, wherein the first memory allocation request is used to request a second number of continuous physical memory pages from the target memory area, and the second number of continuous physical memory pages is used for a second object;
[0203] The third judging unit 507 is used to judge whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the second number;
[0204] Migration unit 508: configured to perform page migration on the target page in the target memory area if the target page is greater than or equal to the second number;
[0205] The second allocation unit 509 is used to allocate the second number of free continuous physical memory pages in the target memory area to the second object for use, wherein the second number of free continuous physical memory pages includes one or more target pages that belong to the target memory area and have completed page migration.
[0206] In a possible implementation, the target memory area also includes a to-be-migrated list;
[0207] Migration unit 508: used for:
[0208] One or more target pages belonging to the target memory area are added to a to-be-migrated list of the target memory area, and a migration operation is performed on the pages in the to-be-migrated list.
[0209] In a possible implementation, the migration unit 508 is configured to:
[0210] Based on the to-be-migrated list in the target memory area, copy the contents of the pages in the to-be-migrated list to the free pages in the memory except the target memory area;
[0211] The address mapping relationship between the pages in the to-be-migrated list and the idle pages is updated.
[0212] It should be noted that the functions of the various functional units in the memory allocation device 50 described in the embodiment of the present application can be found in the above Figure 2 The descriptions of steps S100 to S103 in the method embodiment are not repeated here.
[0213] The electronic device provided by the embodiments of the present application is introduced below.
[0214] Figure 7 The electronic device 01 provided in the embodiment of the present application is a hardware structure diagram of a smart phone. The electronic device 01 can be used to execute the memory allocation method provided in the above method embodiment.
[0215] The electronic device 01 may include a processor 20, a memory 10, a wireless communication module 013, a mobile communication module 014, an antenna 013A, an antenna 014A, a power switch 015, a sensor module 016, a focus motor 017, a camera 018, a display screen 019, etc. Among them, the sensor module 016 may include a gyroscope sensor 016A, an acceleration sensor 016B, an ambient light sensor 016C, an image sensor 016D, a distance sensor 016E, etc. Among them, the wireless communication module 013 may include a WLAN communication module, a Bluetooth communication module, etc. The above multiple parts can transmit data through a bus.
[0216] The processor 20 may include one or more processing units. For example, the processor 20 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU). Different processing units may be independent devices or integrated into one or more processors.
[0217] The memory 10 can be used to store computer executable program codes, and the executable program codes can include instructions. The processor 20 executes various functional applications and data processing of the electronic device 01 by running the instructions stored in the memory 10. The memory 10 can include a program storage area and a data storage area. In a specific implementation, the memory 10 can include a high-speed random access memory, and can also include a non-volatile memory, exemplarily one or more disk storage devices, flash memory devices or other non-volatile solid-state storage devices.
[0218] The wireless communication function of the electronic device 01 can be realized through the antenna 013A, the antenna 014A, the mobile communication module 014, the wireless communication module 013, the modem processor and the baseband processor.
[0219] Antenna 013A and antenna 014A can be used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 01 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of the antennas.
[0220] The mobile communication module 014 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc., applied to the electronic device 01. The mobile communication module 014 can include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), etc. The mobile communication module 014 can receive electromagnetic waves through the antenna 014A, and filter, amplify, etc. the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 014 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 014A.
[0221] The modem processor may include a modulator and a demodulator. The modulator is used to modulate the low-frequency baseband signal to be sent into a medium-high frequency signal. The demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After the low-frequency baseband signal is processed by the baseband processor, it is passed to the application processor. The application processor outputs a sound signal through an audio device, or displays an image or video through a display screen 019.
[0222] The wireless communication module 013 can provide wireless communication solutions including wireless local area networks (WLAN), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to the electronic device 01. The wireless communication module 013 can be one or more devices integrating at least one communication processing module. The wireless communication module 013 receives electromagnetic waves via the antenna 013A, modulates the frequency of the electromagnetic wave signal and performs filtering, and sends the processed signal to the processor 20. The wireless communication module 013 can also receive the signal to be sent from the processor 20, modulate the frequency of the signal, amplify it, and convert it into electromagnetic waves for radiation through the antenna 013A.
[0223] The power switch 015 may be used to control the supply of power to the electronic device 01 .
[0224] The gyroscope sensor 016A can be used to determine the motion posture of the electronic device 01. In some embodiments, the angular velocity of the electronic device 01 around three axes (i.e., x, y, and z axes) can be determined by the gyroscope sensor 016A. The gyroscope sensor 016A can be used for anti-shake shooting. For example, when the shutter is pressed, the gyroscope sensor 016A detects the angle of the electronic device 01 shaking, calculates the distance that the lens module needs to compensate based on the angle, and allows the lens to offset the shaking of the electronic device 01 through reverse movement to achieve anti-shake. The gyroscope sensor 016A can also be used for navigation and somatosensory game scenes.
[0225] The acceleration sensor 016B can detect the magnitude of the acceleration of the electronic device 01 in all directions (generally three axes). When the electronic device 01 is stationary, the magnitude and direction of gravity can be detected. It can also be used to identify the posture of the electronic device. For example, the acceleration sensor 016B can be applied to applications such as horizontal and vertical screen switching and pedometers.
[0226] The ambient light sensor 016C is used to sense the brightness of the ambient light. The electronic device 01 can adaptively adjust the brightness of the display screen 019 according to the sensed brightness of the ambient light. The ambient light sensor 016C can also be used to automatically adjust the white balance when taking pictures.
[0227] Image sensor 016D, also known as a photosensitive element, can utilize the photoelectric conversion function of a photoelectric device to convert the light image on the photosensitive surface into an electrical signal that is proportional to the light image. The image sensor can be a charge coupled device (CCD) sensor or a complementary metal-oxide-semiconductor (CMOS) sensor.
[0228] The distance sensor 016E can be used to measure the distance. The electronic device 01 can measure the distance by infrared or laser. In some shooting scenes, the electronic device 01 can use the distance sensor 016E to measure the distance to achieve fast focusing.
[0229] The focus motor 017 can be used for fast focusing. The electronic device 01 can control the movement of the lens through the focus motor 017 to achieve automatic focusing.
[0230] The electronic device 01 can realize the shooting function through ISP, camera 018, video codec, GPU, display screen 019 and application processor.
[0231] ISP is used to process the data fed back by camera 018. For example, when taking a photo, the shutter is opened, and the light is transmitted to the camera photosensitive element through the lens. The light signal is converted into an electrical signal, and the camera photosensitive element transmits the electrical signal to ISP for processing and converts it into an image visible to the naked eye. ISP can also perform algorithm optimization on the noise and brightness of the image. ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, ISP can be set in camera 018.
[0232] The camera 018 can be used to capture still images or videos. The object generates an optical image through the lens and projects it onto the image sensor. The image sensor can convert the optical signal into an electrical signal, and then transmit the electrical signal to the ISP to convert it into a digital image signal. The ISP can output the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in a standard RGB, YUV or other format. In some embodiments, the electronic device 01 may include 1 or N cameras 018, where N is a positive integer greater than 1.
[0233] The video codec is used to compress or decompress digital images. The electronic device 01 may support one or more image codecs. In this way, the electronic device 01 can open or save pictures or videos in multiple coding formats.
[0234] The electronic device 01 can realize the display function through a GPU, a display screen 019, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 019 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 011 may include one or more GPUs, which execute program instructions to generate or change display information.
[0235] The display screen 019 is used to display images, videos, etc. The display screen 019 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 01 may include 1 or N display screens 019, where N is a positive integer greater than 1.
[0236] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 01. In other embodiments of the present application, the electronic device 01 may include more or fewer components than shown in the figure, or combine certain components, or separate certain components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0237] The operations performed by each device in the electronic device 01 can be specifically referred to above Figure 2-Figure 5 The relevant description of the method embodiment in will not be expanded in detail here.
[0238] The software system of the electronic device 01 may adopt a layered architecture, an event-driven architecture, a micro-kernel architecture, a micro-service architecture, or a cloud architecture. The present application embodiment takes a mobile operating system of a layered architecture as an example to exemplify the software structure of the electronic device 01.
[0239] Figure 8 It is a software structure block diagram of the electronic device 01 according to an embodiment of the present application.
[0240] The layered architecture divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the mobile operating system is divided into four layers, from top to bottom: application layer, application framework layer / core service layer, system library and runtime, and kernel layer.
[0241] The application layer can include a series of application packages.
[0242] like Figure 8 As shown, the application package may include camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message and other applications.
[0243] The application framework layer provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer includes some predefined functions.
[0244] like Figure 8 As shown, the application framework layer may include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and the like.
[0245] The window manager is used to manage window programs. The window manager can obtain the display screen size, determine whether there is a status bar, lock the screen, capture the screen, etc.
[0246] Content providers are used to store and retrieve data and make it accessible to applications. The above data can include videos, images, audio, calls made and received, browsing history and bookmarks, phone books, etc.
[0247] The view system includes visual controls, for example, controls for displaying text, controls for displaying pictures, etc. The view system can be used to build applications. A display interface can be composed of one or more views. For example, a display interface including a text notification icon can include a view for displaying text and a view for displaying pictures.
[0248] The phone manager is used to provide communication functions for electronic devices, for example, management of call status (including answering, hanging up, etc.).
[0249] The resource manager provides various resources for applications, such as localized strings, icons, images, layout files, video files, and so on.
[0250] The notification manager enables applications to display notification information in the status bar. It can be used to convey notification-type messages and can disappear automatically after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc. The notification manager can also be a notification that appears in the system top status bar in the form of a chart or scroll bar text, exemplarily a notification of an application running in the background, or a notification that appears on the screen in the form of a dialog window. Exemplarily, it prompts text information in the status bar, emits a prompt tone, the electronic device vibrates, the indicator light flashes, etc.
[0251] The runtime may refer to all code libraries, frameworks, etc. required for the program to run. For example, for the C language, the runtime includes a series of function libraries required for the C program to run. For the Java language, in addition to the core library, the runtime also includes the virtual machine required for the Java program to run. The above core library may include the function functions that the Java language needs to call.
[0252] The system library may include multiple functional modules, for example: a surface manager, a media library, a 3D graphics processing library (for example: OpenGL ES), a 2D graphics engine (for example: SGL), etc.
[0253] The surface manager is used to manage the display subsystem and provide the fusion of 2D and 3D layers for multiple applications.
[0254] The media library supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc.
[0255] The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, compositing, and layer processing.
[0256] A 2D graphics engine is a drawing engine for 2D drawings.
[0257] The kernel layer is the layer between hardware and software. The kernel layer contains at least display driver, camera driver, audio driver, and sensor driver.
[0258] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0259] It should be noted that, for the above-mentioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the order of the actions described, because according to the present application, some steps may be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0260] In the several embodiments provided in the present application, it should be understood that the disclosed device can be implemented in other ways. For example, the device embodiments described above are only schematic, and the division of the above-mentioned units is exemplarily only a logical function division. There may be other division methods in actual implementation. Exemplarily, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or units can be electrical or other forms.
[0261] The units described above as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0262] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0263] If the above-mentioned integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server or a network device, etc., specifically a processor in a computer device) to perform all or part of the steps of the above-mentioned methods of each embodiment of the present application. Among them, the aforementioned storage medium may include: U disk, mobile hard disk, magnetic disk, optical disk, read-only memory (Read-Only Memory, abbreviated: ROM) or random access memory (Random Access Memory, abbreviated: RAM) and other media that can store program codes.
[0264] As described above, the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A memory allocation method, applied to an electronic device, characterized in that: The electronic device includes a memory, the memory includes a target memory area and a least recently used LRU cache, and the method includes: When the state of the target page in the memory is updated, determining whether the target page belongs to the target memory area, the target memory area including continuous physical memory; wherein the state update of the target page includes the target page being added to the LRU linked list for the first time, or being moved from one LRU linked list to another LRU linked list; If the target page belongs to the target memory area, adding the target page to the LRU cache is prohibited.
2. The method according to claim 1, characterized in that The method further comprises: receiving a first memory allocation request, where the first memory allocation request is used to request a first number of continuous physical memory pages from the target memory area, where the first number of continuous physical memory pages is used by a first object; Determine whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number; If it is greater than or equal to the first number, the first number of free continuous physical memory pages in the target memory area are allocated to the first object for use.
3. The method according to claim 2, characterized in that The target memory area includes a bitmap; and the determining whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number includes: The allocation status of pages in the target memory area is queried according to the bitmap, and it is determined whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number.
4. The method according to claim 3, characterized in that After allocating the first number of free continuous physical memory pages in the target memory area to the first object for use, the method further includes: The bitmap information of the target page of the bitmap in the target memory area is marked as being in an idle state.
5. The method according to claim 1, characterized in that The target page in the memory is updated in state, including: The target page in the memory is added to the first linked list for the first time, or the target page is moved from the second linked list to the third linked list or the target page is moved from the third linked list to the second linked list.
6. The method according to claim 1 or 5, characterized in that: The target page contains structure information, including: Find the type of the target page through the structure information of the target page; Based on the type of the target page, it is determined whether the target page belongs to the target memory area.
7. The method according to any one of claims 1 to 5, characterized in that The method further comprises: receiving a second memory allocation request, where the second memory allocation request is used to request a second number of continuous physical memory pages from the target memory area, where the second number of continuous physical memory pages is used for a second object; Determine whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the second number; If it is greater than or equal to the second number, performing page migration on the target page belonging to the target memory area; The second number of free continuous physical memory pages in the target memory area are allocated to the second object for use, wherein the second number of free continuous physical memory pages includes one or more target pages that belong to the target memory area and have completed page migration.
8. The method according to claim 7, characterized in that The target memory area also includes a to-be-migrated list; and the migrating the target pages in the target memory area includes: One or more target pages belonging to the target memory area are added to a to-be-migrated list of the target memory area, and a migration operation is performed on the pages in the to-be-migrated list.
9. The method according to claim 8, characterized in that The migrating operation on the pages in the to-be-migrated list includes: Based on the to-be-migrated list in the target memory area, copying the contents of the pages in the to-be-migrated list to free pages in the memory except the target memory area; The address mapping relationship of the pages in the to-be-migrated list and the address mapping relationship of the idle pages are updated.
10. A memory allocation device, applied to an electronic device, characterized in that: The electronic device includes a memory, the memory includes a target memory area and a least recently used LRU cache, and the device includes: A first judgment unit is used to judge whether the target page belongs to the target memory area after the target page state in the electronic device is updated, and the target memory area includes continuous physical memory; wherein the state update of the target page includes that the target page is added to the LRU linked list for the first time, or is moved from one LRU linked list to another LRU linked list; An execution unit is used to prohibit adding the target page to the LRU cache if the target page belongs to the target memory area.
11. The device according to claim 10, characterized in that The device also includes: A first receiving unit receives a first memory allocation request, where the first memory allocation request is used to request a first number of continuous physical memory pages from the target memory area, where the first number of continuous physical memory pages is used by a first object; A second judging unit, judging whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number; The first allocation unit allocates the first number of free continuous physical memory pages in the target memory area to the first object for use if the number is greater than or equal to the first number.
12. The device according to claim 11, characterized in that The target memory area includes a bitmap; The second judgment unit is specifically used to: The allocation status of pages in the target memory area is queried according to the bitmap, and it is determined whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the first number.
13. The device according to claim 12, characterized in that The first allocation unit is specifically used to: The bitmap information of the target page of the bitmap in the target memory area is marked as being in an idle state.
14. The device according to claim 10, characterized in that The first judgment unit is specifically used to: The target page in the memory is added to the first linked list for the first time, or the target page is moved from the second linked list to the third linked list or the target page is moved from the third linked list to the second linked list.
15. The device according to claim 10 or 14, characterized in that The target page contains structure information; The first judgment unit is specifically used to: Find the type of the target page through the structure information of the target page; Based on the type of the target page, it is determined whether the target page belongs to the target memory area.
16. The device according to any one of claims 10 to 14, characterized in that The device also includes: A second receiving unit is used to receive a second memory allocation request, where the second memory allocation request is used to request a second number of continuous physical memory pages from the target memory area, where the second number of continuous physical memory pages is used for a second object; A third judging unit, configured to judge whether the number of free continuous physical memory pages in the target memory area is greater than or equal to the second number; a migration unit, configured to perform page migration on the target page in the target memory area if the target page is greater than or equal to the second number; The second allocation unit is used to allocate the second number of free continuous physical memory pages in the target memory area to the second object for use, and the second number of free continuous physical memory pages includes one or more target pages that belong to the target memory area and have completed page migration.
17. The device according to claim 16, characterized in that The target memory area also includes a list to be migrated; The migration unit is used for: One or more target pages belonging to the target memory area are added to a to-be-migrated list of the target memory area, and a migration operation is performed on the pages in the to-be-migrated list.
18. The device according to claim 17, characterized in that The migration unit is used for: Based on the to-be-migrated list in the target memory area, copy the contents of the pages in the to-be-migrated list to the free pages in the memory except the target memory area; The address mapping relationship between the pages in the to-be-migrated list and the idle pages is updated.
19. An electronic device, characterized in that: The electronic device comprises a processor and a memory; the memory comprises a target memory area and a least recently used LRU cache; The processor is configured to: When the state of the target page in the memory is updated, determining whether the target page belongs to the target memory area, the target memory area including continuous physical memory; wherein the state update of the target page includes the target page being added to the LRU linked list for the first time, or being moved from one LRU linked list to another LRU linked list; If the target page belongs to the target memory area, adding the target page to the LRU cache is prohibited.
20. A computer storage medium, characterized in that The computer storage medium stores a computer program, which, when executed by a processor, implements the method described in any one of claims 1 to 9.
21. A computer program product, characterized in that The computer program product comprises instructions, and when the computer program product is executed by a computer, the computer is caused to perform the method according to any one of claims 1 to 9.
