Memory management method, related equipment and program
By judging the memory block required capacity in the memory management method, and allocating preset memory blocks or partitioned memory blocks based on the judgment, the problem of insufficient memory management performance in the existing technology is solved, and more efficient memory allocation and management are achieved.
Patent Information
- Application Number
- CN202411359073.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2025-01-21
AI Technical Summary
The existing memory management methods still need to improve performance when dealing with large-scale memory intensive applications, especially when memory allocation requests fail, which affect efficiency.
By determining the demand capacity of memory blocks, if less than the first capacity, the preset memory block is divided into the preset memory block according to the first capacity; if it is greater than or equal to the first capacity, it will allocate the preset memory block for the application and lack the preset memory block in the preset memory block. Apply for a new memory block from the kernel.
It reduces the number of page table entries, reduces the pressure on the TLB cache, improves the flexibility and performance of memory management, and avoids the waste of time due to page faults.
Smart Images

Figure CN119336493A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of computer technology, and in particular to a memory management method and related devices and programs. Background Art
[0002] With the rapid development of modern servers, large-scale applications such as databases and scientific computing have increasingly higher performance requirements. These large-scale applications are usually memory-intensive applications. As computing needs grow, the demand for memory resources also increases. Therefore, efficient memory management can not only save physical memory resources, but also significantly improve program performance.
[0003] Memory-intensive applications usually use user-mode memory management libraries to manage memory. However, the performance of existing memory management methods still needs to be improved. Summary of the invention
[0004] The embodiment of the present invention provides a memory management method and related equipment and program.
[0005] To achieve the above objectives, the embodiments of the present invention provide the following technical solutions.
[0006] In a first aspect, an embodiment of the present invention provides a memory management method, the memory management method comprising:
[0007] Obtaining a memory application request, wherein the memory application request includes a required capacity of a memory block;
[0008] Determine whether the required capacity of the memory block is greater than or equal to a first capacity, wherein the first capacity is a capacity used to divide a preset memory block, and the preset memory block is divided into a plurality of partition memory blocks based on the first capacity;
[0009] If yes, check whether there is a free preset memory block, if yes, allocate the preset memory block to the application, if no, apply to the kernel for the preset memory block, and then allocate the preset memory block to the application, wherein the capacity of the preset memory block is greater than or equal to the required capacity of the memory block;
[0010] If not, the partition memory block is allocated to the application.
[0011] In a second aspect, an embodiment of the present invention provides a memory manager, the memory manager comprising:
[0012] A request acquisition module, used to acquire a memory application request, wherein the memory application request includes a required capacity of a memory block;
[0013] A memory block allocation module, wherein the memory block allocation module determines whether the required capacity of the memory block is greater than or equal to a first capacity, wherein the first capacity is a capacity for dividing a preset memory block, and the preset memory block is divided into a plurality of partition memory blocks based on the first capacity;
[0014] If yes, check whether there is a free preset memory block, if yes, allocate the preset memory block to the application, if no, apply to the kernel for the preset memory block, and then allocate the preset memory block to the application, wherein the capacity of the preset memory block is greater than or equal to the required capacity of the memory block;
[0015] If not, the partition memory block is allocated to the application.
[0016] In a third aspect, an embodiment of the present invention provides an electronic device, including a processor and a memory manager, wherein the electronic device is used to implement the memory management method as described in the first aspect.
[0017] In a fourth aspect, an embodiment of the present invention provides a computer program product, comprising computer instructions, which, when executed, implement the memory management method as described in the first aspect.
[0018] The memory management method provided by the embodiment of the present invention judges the required capacity of the memory block. When the required capacity of the memory block is less than the first capacity, the partitioned memory block obtained by dividing the preset memory block according to the first capacity is allocated to the application. When the required capacity of the memory block is greater than or equal to the first capacity, in order to meet the large-capacity memory block demand of the application, the preset memory block is allocated to the application, the number of page table entries is reduced, the pressure on the TLB cache is reduced, and when the preset memory block is missing, a new preset memory block is applied to the kernel, thereby improving the flexibility of the memory management method, saving time for processing page faults caused by insufficient preset memory blocks, and thus improving the performance of the memory management method. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0020] Figure 1 It is a flow chart of the memory management method provided by an embodiment of the present invention.
[0021] Figure 2 This is another flow chart of the memory management method provided by an embodiment of the present invention.
[0022] Figure 3It is a schematic diagram of a memory management method provided by an embodiment of the present invention.
[0023] Figure 4 is another schematic diagram of the memory management method provided by an embodiment of the present invention.
[0024] Figure 5 is a schematic diagram of a memory manager provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0025] As described in the background technology section, as computing demands grow, memory-intensive applications also have an increasing demand for memory resources. However, for processors, when accessing memory, problems such as TLB miss (Translation Lookaside Buffer) that affect memory access efficiency may occur.
[0026] In an existing memory management method, a hugetlbfs file system can be used, and a memory allocation process is performed on the basis of the system to calculate the mapping from virtual address to physical address, and then sort by physical address to meet the memory application requirements of the application. It can be understood that this memory library management method takes advantage of the high efficiency of static memory allocation, and uses 2MB huge pages (HugePage) instead of 4kB pages, reducing the number of page table entries under the same capacity, thereby reducing the pressure on the TLB cache and reducing the probability of TLB miss.
[0027] However, huge page memory is usually pre-allocated when the system starts, and the allocated amount and capacity are fixed. When the memory capacity requested by an application exceeds the allocated huge page memory, the system may be unable to allocate more memory blocks for the application, causing the memory allocation request to fail, affecting the efficiency of memory allocation. In other words, the performance of existing memory management methods still needs to be improved.
[0028] To solve the above problems, the present invention provides a memory management method, comprising:
[0029] Obtaining a memory application request, wherein the memory application request includes a required capacity of a memory block;
[0030] Determine whether the required capacity of the memory block is greater than or equal to a first capacity, wherein the first capacity is a capacity used to divide a preset memory block, and the preset memory block is divided into a plurality of partition memory blocks based on the first capacity;
[0031] If yes, check whether there is a free preset memory block, if yes, allocate the preset memory block to the application, if no, apply to the kernel for the preset memory block, and then allocate the preset memory block to the application, wherein the capacity of the preset memory block is greater than or equal to the required capacity of the memory block;
[0032] If not, the partition memory block is allocated to the application.
[0033] It can be seen that the memory management method provided by the present invention first obtains a memory application request, wherein the memory application request includes a memory block required capacity. After determining the memory block required capacity, it is determined whether the memory block required capacity is greater than a first capacity, wherein the first capacity is a capacity for dividing a preset memory block, and the preset memory block is divided into a plurality of partition memory blocks based on the first capacity. If so, that is, the memory block required capacity is greater than the first capacity, it indicates that the partition memory block cannot meet the memory application request, and therefore, it is checked whether there is an idle preset memory block. If so, the preset memory block is allocated to the application. If not, it indicates that it is necessary to apply for memory space from the kernel, that is, the preset memory block, to avoid memory allocation request failure and improve memory allocation efficiency. Thereafter, the preset memory block is allocated to the application, wherein the capacity of the preset memory block is greater than or equal to the memory block required capacity. If not, that is, the memory block required capacity is less than or equal to the first capacity, it indicates that the partition memory block can meet the memory application request, and therefore, the partition memory block is allocated to the application.
[0034] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0035] Figure 1 This is a flow chart of a memory management method provided by an embodiment of the present invention. Figure 1 The memory management method provided by the present invention comprises the following steps:
[0036] Step S100: Obtain a memory application request.
[0037] The memory application request includes the required capacity of the memory block.
[0038] A memory application request is a request sent by an application to an operating system to allocate a memory area. The memory application request includes a required capacity of the memory block, that is, the capacity of the memory block required by the application.
[0039] In some specific implementations, when the application is started, the memory bank configuration information is initialized, and a memory bank is requested from the kernel according to the memory bank configuration information, and the memory bank includes at least: at least one preset memory block.
[0040] After the memory application request is obtained, step S110 is continued.
[0041] Step S110: Determine whether the required capacity of the memory block is greater than or equal to the first capacity.
[0042] The first capacity is a capacity used to divide a preset memory block, and the preset memory block is divided into a plurality of partition memory blocks based on the first capacity.
[0043] Considering that memory-intensive applications may require a large number of large-capacity memory blocks, the preset memory blocks can meet the large-capacity memory block requirements of the application program, and the preset memory blocks are divided into multiple partition memory blocks according to the first capacity, which can meet the application program's application for small-capacity memory blocks. In some specific implementations, the preset memory blocks are hugepage memory blocks, and the partition memory blocks are region memory blocks. The hugepage memory blocks can be divided into multiple region memory blocks based on the first capacity. The first capacity can be adjusted according to the load of the application program. For example, the first capacity can be 16KB, 32KB, etc.
[0044] For the memory block required capacity included in the memory application request, it is necessary to determine whether it is greater than or equal to the first capacity. When the memory block required capacity is less than the first capacity, step S111 is executed.
[0045] Step S111: Allocate the partition memory block to the application.
[0046] Since the partitioned memory block is obtained by dividing the preset memory block according to the first capacity, when the required capacity of the memory block is less than the first capacity, the partitioned memory block can be directly allocated to the application that issues the memory application request to meet the memory application request of the application. For example, when the first capacity is 32KB and the required capacity of the memory block is 16KB, the partitioned memory block can be directly allocated to the application. Specifically, the operation of allocating the partitioned memory block to the application is: returning the first address of the partitioned memory block to the application.
[0047] When the required capacity of the memory block is greater than or equal to the first capacity, step S120 is executed.
[0048] Step S120: Check whether there is an idle preset memory block.
[0049] Since the partitioned memory block is obtained by dividing the preset memory block according to the first capacity, when the required capacity of the memory block is greater than or equal to the first capacity, it means that the partitioned memory block cannot meet the memory application request of the application program, and considering that memory-intensive applications may require a large number of large-capacity memory blocks, allocating a larger capacity preset memory block to the application program is beneficial to reducing the number of page table entries, thereby reducing the pressure on the TLB cache, reducing the probability of TLB miss, and improving the memory access performance of the processor. In some embodiments, the capacity of the preset memory block includes multiple types, and different types of preset memory blocks correspond to different capacities, wherein any preset memory block is a positive integer multiple of the second capacity, and the second capacity is the minimum capacity of the preset memory block, and the second capacity is the minimum capacity of the preset memory block.
[0050] However, the preset memory blocks are obtained by applying to the kernel. If all the preset memory blocks that have been applied for are allocated to the application, the free preset memory blocks cannot be allocated to the application. Therefore, it is necessary to first find out whether there are free preset memory blocks, and the free preset memory blocks are preset memory blocks that have not been allocated to the application. When the free preset memory blocks are found, step S130 is executed.
[0051] Step S130: Allocate the preset memory block to the application.
[0052] When a free preset memory block is found, it indicates that there is a free preset memory block that can be allocated to the application, and the preset memory block can be allocated to the application. In some specific implementations, when the capacity of the preset memory block includes multiple types, different types of preset memory blocks correspond to different capacities, and when any preset memory block is a positive integer multiple of the second capacity, allocating the preset memory block to the application is specifically: according to the required capacity of the memory block, select a preset memory block of the minimum capacity type that can meet the required capacity of the memory block, and allocate it to the application. Specifically, the operation of allocating the preset memory block to the application is: returning the first address of the preset memory block to the application.
[0053] When no free preset memory block is found, step S121 is executed.
[0054] Step S121: Apply to the kernel for a preset memory block.
[0055] When no free preset memory blocks are found, it means that the preset memory blocks are all allocated to the application program, so it is necessary to apply for the preset memory blocks from the kernel, saving the time of processing the page fault caused by insufficient preset memory blocks, avoiding page fault interruption, and improving the processing efficiency of page fault. After applying for the preset memory blocks from the kernel, continue to execute step S130 to allocate the preset memory blocks to the application program. In some specific implementations, for example, in the Linux system, hugepage memory blocks can be applied to the kernel by calling the mmap system call.
[0056] It can be seen that the memory management method provided in the embodiment of the present invention determines the required capacity of the memory block. When the required capacity of the memory block is less than the first capacity, the partitioned memory block obtained by dividing the preset memory block according to the first capacity is allocated to the application. When the required capacity of the memory block is greater than or equal to the first capacity, in order to meet the large-capacity memory block demand of the application, the preset memory block is allocated to the application, the number of page table entries is reduced, the pressure on the TLB cache is reduced, and when the preset memory block is missing, a new preset memory block is applied to the kernel, thereby improving the processing efficiency of page faults, thereby improving the performance of the memory management method.
[0057] It should also be noted that when the application no longer needs the memory block that has been allocated to the application, the released memory block can be returned in the direction of the memory block application. In some embodiments, the above-mentioned memory management method also includes: obtaining the memory block released by the application; wherein, when the memory block released by the application is a preset memory block, releasing it to the kernel; when the memory block released by the application is a partition memory block, if the sum of the capacity of the released partition memory block and all free partition memory blocks is greater than or equal to the second capacity, the released partition memory block and the free partition memory block are merged into a preset memory block according to the second capacity; otherwise, the released partition memory block is used as a free partition memory block; when the memory block released by the application is a thread memory block, it is used as a free partition memory block.
[0058] Figure 2 is another flow chart of the memory management method provided by the embodiment of the present invention. Since the application may have the need to apply for memory blocks in batches, Figure 2 In some implementations, when the memory application requests a preset number of memory blocks, the memory management method provided by the embodiment of the present invention includes the following steps:
[0059] Step S200: Obtain a memory application request, wherein the memory application request applies for a preset number of memory blocks.
[0060] The preset number is the number of memory blocks required by the application, that is, the number of memory blocks requested by the memory application request. The memory application request is a one-time request. Since memory-intensive applications require a large amount of memory resources, applying for a preset number of memory blocks through a one-time memory application request can improve memory management efficiency.
[0061] After the memory application request is obtained, step S210 is continued.
[0062] Step S210: Determine whether the required capacity of the memory block is greater than or equal to the first capacity.
[0063] For the memory block required capacity included in the memory application request, it is necessary to determine whether it is greater than or equal to the first capacity. When the memory block required capacity is less than the first capacity, step S211 is executed.
[0064] Step S211: Allocate a preset number of the partition memory blocks to the application.
[0065] Since the partitioned memory block is obtained by dividing the preset memory block according to the first capacity, when the required capacity of the memory block is less than the first capacity, the preset number of partitioned memory blocks can be directly allocated to the application that issues the memory application request to meet the memory application request of the application. Specifically, the operation of allocating the preset number of partitioned memory blocks to the application is: returning the first address of the first number of partitioned memory blocks to the application.
[0066] When the required capacity of the memory block is greater than or equal to the first capacity, step S220 is executed.
[0067] Step S220: Check whether there are a preset number of free memory blocks.
[0068] Since the partition memory block is obtained by dividing the preset memory block according to the first capacity, when the required capacity of the memory block is greater than or equal to the first capacity, it means that the partition memory block cannot meet the memory application request of the application. However, the preset memory block is obtained by applying to the kernel. If all the preset memory blocks that have been applied for are allocated to the application, or the number of free preset memory blocks is less than the preset number, the preset number of free preset memory blocks cannot be allocated to the application. Therefore, it is necessary to first find out whether there is a preset number of free preset memory blocks. The free preset memory blocks are preset memory blocks that have not been allocated to the application. When the preset number of free preset memory blocks is found, step S230 is executed.
[0069] Step S230: Allocating a preset number of the preset memory blocks to the application.
[0070] When a preset number of free preset memory blocks is found, it indicates that there are a preset number of free preset memory blocks that can be allocated to the application, and the preset number of preset memory blocks can be allocated to the application. Specifically, the operation of allocating the preset number of preset memory blocks to the application is: returning the first addresses of the preset number of preset memory blocks to the application.
[0071] When no free preset memory block is found, step S221 is executed.
[0072] Step S221: Apply to the kernel for a preset number of preset memory blocks.
[0073] When no free preset memory blocks are found, it means that the preset memory blocks are all allocated to the application program, so it is necessary to apply to the kernel for a preset number of preset memory blocks, saving time for processing page faults caused by insufficient preset memory blocks, avoiding page fault interrupts, and improving page fault processing efficiency. After applying to the kernel for a preset number of preset memory blocks, continue to execute step S230 to allocate the preset number of preset memory blocks to the application program.
[0074] It can be seen that when the memory application request applies for a preset number of memory blocks, the memory management method provided in the embodiment of the present invention obtains a memory block application request for a preset number of memory blocks once, and for a case where the memory block requirement capacity is less than the first capacity, allocates the preset number of partitioned memory blocks to the application, and for a case where the memory block requirement capacity is greater than or equal to the first capacity, in order to meet the large-capacity memory block requirement of the application, allocates a preset number of preset memory blocks to the application at one time, thereby improving memory allocation efficiency, and when the preset memory blocks are missing, applies to the kernel for new preset number of preset memory blocks, thereby improving the flexibility of the memory management method, saving time for processing page faults caused by insufficient preset memory blocks, and thus improving the performance of the memory management method.
[0075] In some specific implementations, in step S111, allocating the partition memory block to the application includes:
[0076] Check whether there is a free partition memory block; if so, allocate the partition memory block to the application; if not, apply for a free preset memory block; if there is no free preset memory block, apply for a preset memory block from the kernel, divide the preset memory block into multiple partition memory blocks based on the first capacity, and then allocate the partition memory block to the application.
[0077] Since the partition memory blocks are obtained by dividing the preset memory blocks based on the first capacity, if all the partition memory blocks are allocated to the application, the free partition memory blocks cannot be allocated to the application. Therefore, it is necessary to first find out whether there are free partition memory blocks. The free partition memory blocks are partition memory blocks that have not yet been allocated to the application.
[0078] When a free partition memory block is found, it means that there is a free partition memory block that can be allocated to the application, and the partition memory block can be allocated to the application at this time; when no free partition memory block is found, it means that the partition memory blocks are all allocated to the application, so it is necessary to apply for a free preset memory block, and after dividing it into multiple partition memory blocks based on the first capacity, allocate the partition memory block to the application.
[0079] When applying for a free preset memory block, if there is no free preset memory block, apply for a preset memory block from the kernel and use it as the free preset memory block.
[0080] It is understandable that if all the preset memory blocks that have been applied for are allocated to the application, not only can the free preset memory blocks not be allocated to the application, but the free preset memory blocks cannot be split based on the first capacity. Therefore, in the case where there are no free preset memory blocks, it is also necessary to apply for preset memory blocks from the kernel, and after obtaining the free preset memory blocks, the partition memory blocks are split based on the first capacity. These memory blocks obtained by splitting have not been allocated to the application, so the partition memory blocks can be allocated to the application.
[0081] Taking into account that an application may have multiple threads, in order to reduce lock contention in concurrent multi-threaded scenarios and avoid additional performance overhead, in some embodiments, the memory application request also includes a thread number, where the thread number is the number of the thread that issues the memory application request, and each thread number corresponds to a thread memory block, which is a partitioned memory block applied for according to the thread number; under this premise, allocating the preset memory block to the application includes: searching according to the thread number to see whether there is a free thread memory block corresponding to it, and if so, allocating the thread memory block to the application; if not, applying for a free partition memory block as the thread memory block, and then allocating the thread memory block to the application.
[0082] The application has multiple threads, and the thread number is the number of the thread in the application that issues the memory application request. Since the partition memory block cannot be allocated to multiple threads when multiple threads send memory application requests at the same time, specifically, since the partition memory block can be used by all threads, the partition memory block needs to be locked to ensure that the partition memory block will not be allocated to multiple threads at the same time to ensure thread safety, but this will cause lock competition between different threads, reducing memory allocation efficiency.
[0083] Therefore, a thread memory block corresponding to the thread number is set, and the thread memory block is a partition memory block applied for according to the thread number. Thus, when the memory application request includes a thread number, a search is performed based on the thread number to find out whether there is a free thread memory block corresponding to it. If so, the thread memory block is allocated to the application. If not, a free partition memory block is applied for as the thread memory block, and then the thread memory block is allocated to the application. This can reduce lock contention in concurrent multi-threaded scenarios and improve the performance of the memory management method. In some specific implementations, the application for free partition memory blocks as the thread memory blocks is specifically: applying for a first number of free partition memory blocks as the thread memory blocks at one time, avoiding multiple applications for the free partition memory blocks, and improving memory management performance.
[0084] In order to further improve the performance of the memory management method, in some optional embodiments, allocating the preset memory block to the application includes: after offsetting the first address of the preset memory block, allocating the preset memory block to the application, wherein the first address offset of the preset memory block is different from the first address offset of the preset memory block allocated to the application.
[0085] Since a cache line is the basic unit of data storage in a computer system, when a processor needs to read or write data, it needs to read the entire cache line instead of a single data in the cache; if the corresponding data is not in the cache, it needs to read the data from the memory and load it into the cache. However, the preset memory blocks allocated to the application may be continuous. When the processor accesses the preset memory blocks, objects in multiple continuous preset memory blocks may be mapped to the same cache line. This situation will cause the processor to frequently exchange objects in the cache line between the processor's physical cache and the memory when accessing different objects in the cache line. This is also called a cache line conflict problem.
[0086] Therefore, before allocating the preset memory block to the application, the first address of the preset memory block is offset by a certain amount, and it is ensured that the first address offset of the preset memory block is different from the first address offset of the preset memory block allocated to the application. This can avoid differences among multiple preset memory blocks allocated to the application, thereby reducing the probability of cache line conflict problems, improving the efficiency of the processor accessing the preset memory block when executing the application, and improving the performance of the memory management method.
[0087] In order to further improve the performance of the memory management method, in some optional implementations, the partition memory block and the thread memory block are both cache line aligned; after being divided into multiple partition memory blocks based on the first capacity, allocating the partition memory block to the application includes: after being divided into multiple partition memory blocks based on the first capacity, performing a cache line alignment operation on the multiple partition memory blocks, and allocating the cache line aligned partition memory block to the application.
[0088] In some implementations, the application has multiple threads. When different threads call to access and modify different data in the same cache line, even if these data do not affect each other, due to the structure of the cache line, when one thread (e.g., thread 1) modifies the data, the entire cache line where the data is located will be marked. At this time, if other threads (e.g., thread 2) want to modify the data, they need to wait for the data in the marked cache line to be modified by thread 1, the mark is cancelled, and thread 2 re-reads the data in the marked cache line. This problem is usually called false sharing, which leads to unnecessary cache consistency traffic and system performance degradation.
[0089] Therefore, the partition memory block and the thread memory block are aligned with the cache line so that the data of different partition memory blocks or thread memory blocks are not stored in the same cache line, thereby avoiding the occurrence of false sharing, improving the efficiency of the processor accessing the memory, and thus improving the performance of the memory management method. Specifically, the cache line alignment operation for the multiple partition memory blocks is: aligning the first address of the partition memory block to an integer multiple of the size of the cache line. For example, when the cache line size of the processor is 64 bytes, and the first address of a partition memory block after segmentation is 0x010013, it is obvious that the value of the address cannot be divided by 64 (ie 0x40), so it is necessary to align the first address 0x010013 of the partition memory block to the next address 0x010040 that is divisible by 0x40, that is, the first address of the aligned partition memory block is 0x010040.
[0090] Furthermore, since the thread memory block is obtained by applying for an idle partition memory block, the thread memory block is also cache line aligned.
[0091] It can be seen that the above implementation method avoids the occurrence of false sharing by performing cache line alignment operation on multiple partitioned memory blocks obtained by dividing the preset memory block, improves the efficiency of the processor accessing the memory, and thus improves the performance of the memory management method.
[0092] In some implementations, the capacity of the preset memory block is: a positive integer power of 2 times the second capacity. Further, in some specific implementations, the second capacity is 2 MB.
[0093] Since allocating a preset memory block with a larger capacity to an application is beneficial to reducing the number of page table entries, the capacity of the preset memory block applied to the kernel can be a positive integer power of 2 times the second capacity. For example, when the second capacity is 2MB, the capacity of the preset memory block can be 2MB, 4MB, 8MB, 32MB, etc. The capacity of the preset memory block allocated to the application needs to be greater than or equal to the required capacity of the memory block. For example, when the required capacity of the memory block is 7MB, the capacity of the preset memory block allocated to the application should be 8MB, thereby avoiding waste of memory block resources, reducing the pressure of the TLB cache and the probability of TLB miss, improving the memory access performance of the processor, and thus improving the performance of the memory management method.
[0094] It can be seen that the memory management method provided by the embodiment of the present invention determines the required capacity of the memory block. When the required capacity of the memory block is less than the first capacity, the partitioned memory block obtained by dividing the preset memory block according to the first capacity is allocated to the application. When the required capacity of the memory block is greater than or equal to the first capacity, in order to meet the large-capacity memory block demand of the application, the preset memory block is allocated to the application, the number of page table entries is reduced, the pressure of the TLB cache is reduced, and when the preset memory block is missing, a new preset memory block is applied to the kernel to improve the processing efficiency of page faults, thereby improving the performance of the memory management method. Furthermore, the embodiment of the present invention also corresponds each thread number to a thread memory block. When the memory application request includes a thread number, it searches for a corresponding idle thread memory block based on the thread number, thereby reducing lock contention in concurrent multi-threaded scenarios and improving the performance of the memory management method. Furthermore, the embodiment of the present invention also offsets the first address of the preset memory block before allocating the preset memory block to the application, thereby reducing the probability of cache line conflict problems; and, after dividing the first capacity into multiple partitioned memory blocks, performs cache line alignment operations on the multiple partitioned memory blocks to avoid the occurrence of false sharing, improve the efficiency of the processor accessing the memory, and further improve the performance of the memory management method.
[0095] A specific implementation of the memory management method provided by an embodiment of the present invention is described in detail below. Figure 3FIG. 1 is a schematic diagram of a memory management method provided by an embodiment of the present invention. Figure 3 To implement the memory management method provided by the embodiment of the present invention, in a computer system based on the Linux kernel, when the application is started, the memory management module of the application needs to be initialized. The specific method is as follows:
[0096] 1) Initializing configuration information required by the memory management method, wherein the configuration information at least includes a first capacity size and a second capacity page_size;
[0097] 2) Based on the configuration information, apply to the kernel Kernel through the mmap system call for a memory block page_1 with a preset capacity of page_size;
[0098] 3) Divide the preset memory block page_1 into n partition memory blocks with a capacity of size (i.e., region_0 to region_n-1), and perform cache line alignment operation on region_0 to region_n-1, that is, align the first addresses of these partition memory blocks to an integer multiple of the cache line size cacheline_size.
[0099] In one implementation, the first capacity size is 16KB, the second capacity page_size is 2MB, and specifically, the preset memory block is a transparent huge page (THP). Figure 3 When the memory block capacity (i.e., the required memory block capacity) requested by the application through the malloc function is 12KB, the capacity is smaller than the first capacity size, so first check whether there is a free partition memory block; since there is a free partition memory block region_0, the first address of the partition memory block region_0 is returned to the application, that is, the partition memory block region_0 is allocated to the application.
[0100] If the application continues to apply for a memory block with a capacity of 8KB through the malloc function, since there is a free partition memory block region_1, the first address of the partition memory block region_1 can continue to be returned to the application, that is, the partition memory block region_1 can be allocated to the application.
[0101] If the application continues to apply for a memory block with a capacity of 1MB through the malloc function, since the capacity of the memory block is greater than the first capacity size, and page_1 has been divided into multiple partitioned memory blocks, page_1 is not in an idle state, it is necessary to apply to the kernel through the mmap system call for the second capacity, that is, the preset memory block page_2 with page_size = 2MB, and return the first address of the idle preset memory block page_2 to the application, that is, allocate the preset memory block page_2 to the application.
[0102] If the application no longer needs the preset memory block page_2, the preset memory block page_2 is released through the free function. At this time, the preset memory block page_2 can be released to the kernel through the munmap system call, and the preset memory block page_2 previously requested through the mmap system call is released to the kernel Kernel to avoid wasting memory resources.
[0103] In the case where the application has multiple threads, for example, the application has thread 0, thread 1, ..., thread 7, if thread m (m can be any integer value from 0 to 7) applies for a memory block with a capacity of 8KB through the malloc function, the memory management module first searches for a preset number of free partition memory blocks and uses them as thread memory blocks corresponding to thread m. For example, when the preset number cached_num is 10, region_2, region_3, ..., region_11 are found to be free partition memory blocks from the partition memory blocks, and these 10 memory blocks are used as thread memory blocks m_cached, and the first address of one of the thread memory blocks m_cached is returned to thread m, that is, the thread memory block is allocated to the application. It can be understood that each thread has a corresponding thread memory block, and the thread memory block corresponds to the thread number.
[0104] In another embodiment, the first capacity size is 16KB, and the second capacity page_size is 2MB. Figure 4 When the application requests a memory block with a capacity of 5MB through the malloc function, the system does not have a preset memory block with a capacity greater than or equal to 5MB. Therefore, it is necessary to request a preset memory block with a capacity that is a positive integer power of the second capacity page_size from the kernel through the mmap system call, that is, 2 3MB=8MB of preset memory block page_3, and allocate the preset memory block page_3 to the application; if the application continues to apply for a memory block with a capacity of 5MB through the malloc function, it continues to apply for 8MB of preset memory block page_4 from the kernel through the mmap system call, and after offsetting the first address of the preset memory block page_4 by 0x01, allocate the preset memory block page_4 to the application.
[0105] The embodiment of the present invention also provides a memory manager, such as Figure 5 As shown, the memory manager 1 includes:
[0106] A request acquisition module 11 is used to acquire a memory application request, wherein the memory application request includes a required capacity of a memory block;
[0107] A memory block allocation module 12, wherein the memory block allocation module 12 determines whether the required capacity of the memory block is greater than or equal to a first capacity, wherein the first capacity is a capacity for dividing a preset memory block, and the preset memory block is divided into a plurality of partition memory blocks based on the first capacity;
[0108] If yes, check whether there is a free preset memory block, if yes, allocate the preset memory block to the application, if no, apply to the kernel for the preset memory block, and then allocate the preset memory block to the application, wherein the capacity of the preset memory block is greater than or equal to the required capacity of the memory block;
[0109] If not, the partition memory block is allocated to the application.
[0110] In some embodiments, the memory block allocation module 12 allocates the preset memory block to the application, including: after offsetting the first address of the preset memory block, allocating the preset memory block to the application, wherein the first address offset of the preset memory block is different from the first address offset of the preset memory block allocated to the application.
[0111] In some implementations, the memory block allocation module 12 allocates the partition memory block to the application program, including:
[0112] Check whether there is a free partition memory block. If so, allocate the partition memory block to the application. If not, apply for a free preset memory block. If there is no free preset memory block, apply for a preset memory block from the kernel. After dividing the preset memory block into multiple partition memory blocks based on the first capacity, allocate the partition memory block to the application.
[0113] In some embodiments, after the memory block allocation module 12 is divided into multiple partition memory blocks based on the first capacity, allocating the partition memory block to the application includes: after being divided into multiple partition memory blocks based on the first capacity, performing cache line alignment operation on the multiple partition memory blocks, and allocating the cache line aligned partition memory blocks to the application.
[0114] In some implementations, when the memory application request obtained by the request obtaining module 11 applies for a preset number of memory blocks, the searching whether there are free preset memory blocks includes: searching whether there are a preset number of free preset memory blocks;
[0115] The allocating the preset memory blocks to the application comprises: allocating a preset number of the preset memory blocks to the application;
[0116] After applying for the preset memory blocks from the kernel, allocating the preset memory blocks to the application comprises: after applying for a preset number of preset memory blocks from the kernel, allocating the preset memory blocks to the application;
[0117] The allocating the partition memory blocks to the application includes: allocating a preset number of the partition memory blocks to the application.
[0118] In some embodiments, the preset memory blocks include multiple types, and the different types of preset memory blocks correspond to different capacities, wherein the capacity of any preset memory block is a positive integer multiple of the second capacity, and the second capacity is the minimum capacity of the preset memory blocks.
[0119] In some implementations, the memory block allocation module 12 allocates the preset memory block to the application, including: selecting a preset memory block of a minimum capacity type that can meet the required capacity of the memory block according to the required capacity of the memory block, and allocating it to the application.
[0120] In some implementations, the memory application request acquired by the request acquisition module 11 further includes: a thread number, the thread number being the number of the thread that issues the memory application request, each of the thread numbers corresponding to a thread memory block, the thread memory block being a partition memory block applied for according to the thread number;
[0121] The memory block allocation module 12 allocates the preset memory block to the application, including: searching whether there is a free thread memory block corresponding to the thread number according to the thread number, if so, allocating the thread memory block to the application; if not, applying for a free partition memory block as the thread memory block, and then allocating the thread memory block to the application.
[0122] In some implementations, the applying for the free partition memory block as the thread memory block includes:
[0123] A first number of free partition memory blocks are applied for at one time as the thread memory blocks.
[0124] In some implementations, the memory block allocation module 12 allocates the preset memory block to the application program, including: returning the first address of the preset memory block to the application program;
[0125] The allocating the partition memory block to the application comprises: returning the first address of the partition memory block to the application;
[0126] The allocating the thread memory block to the application includes: returning the first address of the thread memory block to the application.
[0127] In some implementations, the capacity of any preset memory block is: a positive integer power of 2 times the second capacity.
[0128] In some implementations, the memory manager further includes:
[0129] A memory block releasing module 13 is used to obtain a memory block released by an application program;
[0130] Wherein, when the memory block released by the application is a preset memory block, releasing it to the kernel;
[0131] When the memory block released by the application is a partition memory block, if the sum of the capacity of the released partition memory block and all the free partition memory blocks is greater than or equal to the second capacity, the released partition memory block and the free partition memory blocks are merged into a preset memory block according to the second capacity; otherwise, the released partition memory block is used as the free partition memory block;
[0132] When the memory block released by the application is a thread memory block, it is used as a free partition memory block.
[0133] An embodiment of the present invention further provides an electronic device, including a processor and a memory manager, wherein the electronic device is used to implement the memory management method as described in the above embodiment.
[0134] An embodiment of the present invention further provides a computer program product, including computer instructions, which implement the memory management method described in the above embodiment when executed.
[0135] The above describes multiple implementation schemes provided by the embodiments of the present invention. The various optional methods introduced in the implementation schemes can be combined and cross-referenced with each other without conflict, thereby extending a variety of possible implementation schemes, which can all be considered as implementation schemes disclosed and open in the embodiments of the present invention.
[0136] Although the present invention is disclosed as above, the present invention is not limited thereto. Any person skilled in the art can make various changes and modifications without departing from the spirit and scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the scope defined by the claims.
Claims
1. A memory management method, characterized in that: include: Obtaining a memory application request, wherein the memory application request includes a required capacity of a memory block; Determine whether the required capacity of the memory block is greater than or equal to a first capacity, wherein the first capacity is a capacity used to divide a preset memory block, and the preset memory block is divided into a plurality of partition memory blocks based on the first capacity; If yes, check whether there is a free preset memory block, if yes, allocate the preset memory block to the application, if no, apply to the kernel for the preset memory block, and then allocate the preset memory block to the application, wherein the capacity of the preset memory block is greater than or equal to the required capacity of the memory block; If not, the partition memory block is allocated to the application.
2. The memory management method according to claim 1, characterized in that: The allocating the preset memory block to the application includes: allocating the preset memory block to the application after offsetting the first address of the preset memory block, wherein the first address offset of the preset memory block is different from the first address offset of the preset memory block allocated to the application.
3. The memory management method according to claim 2, characterized in that: The allocating the partition memory block to the application comprises: Check whether there is a free partition memory block. If so, allocate the partition memory block to the application. If not, apply for a free preset memory block. If there is no free preset memory block, apply for a preset memory block from the kernel. After dividing the preset memory block into multiple partition memory blocks based on the first capacity, allocate the partition memory block to the application.
4. The memory management method according to claim 3, characterized in that: After the partition memory blocks are divided into multiple partition memory blocks based on the first capacity, allocating the partition memory blocks to the application includes: after the partition memory blocks are divided into multiple partition memory blocks based on the first capacity, performing a cache line alignment operation on the multiple partition memory blocks, and allocating the cache line aligned partition memory blocks to the application.
5. The memory management method according to claim 4, characterized in that: When the memory application requests a preset number of memory blocks, the searching whether there are idle preset memory blocks includes: searching whether there are idle preset memory blocks of a preset number; The allocating the preset memory blocks to the application comprises: allocating a preset number of the preset memory blocks to the application; After applying for the preset memory blocks from the kernel, allocating the preset memory blocks to the application comprises: after applying for a preset number of preset memory blocks from the kernel, allocating the preset memory blocks to the application; The allocating the partition memory blocks to the application includes: allocating a preset number of the partition memory blocks to the application.
6. The memory management method according to claim 2, characterized in that: The preset memory blocks include multiple types, and the preset memory blocks of different types have different capacities, wherein the capacity of any preset memory block is a positive integer multiple of the second capacity, and the second capacity is the minimum capacity of the preset memory blocks.
7. The memory management method according to claim 6, characterized in that: The allocating the preset memory block to the application includes: selecting, according to the required capacity of the memory block, a preset memory block of a minimum capacity type that can meet the required capacity of the memory block, and allocating it to the application.
8. The memory management method according to claim 3, characterized in that: The memory application request further includes a thread number, where the thread number is the number of the thread that issues the memory application request, and each thread number corresponds to a thread memory block, where the thread memory block is a partition memory block applied for according to the thread number; The allocating the preset memory block to the application includes: searching whether there is a free thread memory block corresponding to the thread number, if so, allocating the thread memory block to the application; if not, applying for a free partition memory block as the thread memory block, and then allocating the thread memory block to the application.
9. The memory management method according to claim 8, characterized in that: The application for the idle partition memory block as the thread memory block includes: A first number of free partition memory blocks are applied for at one time as the thread memory blocks.
10. The memory management method according to claim 8, characterized in that: The allocating the preset memory block to the application comprises: returning the first address of the preset memory block to the application; The allocating the partition memory block to the application comprises: returning the first address of the partition memory block to the application; The allocating the thread memory block to the application includes: returning the first address of the thread memory block to the application.
11. The memory management method according to claim 6, characterized in that: The capacity of the arbitrary preset memory block is: a positive integer power of 2 times the second capacity.
12. The memory management method according to claim 10, characterized in that: The method further comprises: Get the memory block released by the application; Wherein, when the memory block released by the application is a preset memory block, releasing it to the kernel; When the memory block released by the application is a partition memory block, if the sum of the capacity of the released partition memory block and all the free partition memory blocks is greater than or equal to the second capacity, the released partition memory block and the free partition memory blocks are merged into a preset memory block according to the second capacity; otherwise, the released partition memory block is used as the free partition memory block; When the memory block released by the application is a thread memory block, it is used as a free partition memory block.
13. A memory manager, characterized in that: include: A request acquisition module, used to acquire a memory application request, wherein the memory application request includes a required capacity of a memory block; A memory block allocation module, wherein the memory block allocation module determines whether the required capacity of the memory block is greater than or equal to a first capacity, wherein the first capacity is a capacity for dividing a preset memory block, and the preset memory block is divided into a plurality of partition memory blocks based on the first capacity; If yes, check whether there is a free preset memory block, if yes, allocate the preset memory block to the application, if no, apply to the kernel for the preset memory block, and then allocate the preset memory block to the application, wherein the capacity of the preset memory block is greater than or equal to the required capacity of the memory block; If not, the partition memory block is allocated to the application.
14. The memory manager of claim 13, wherein: The memory block allocation module allocates the preset memory block to the application, including: after offsetting the first address of the preset memory block, allocating the preset memory block to the application, wherein the first address offset of the preset memory block is different from the first address offset of the preset memory block allocated to the application.
15. The memory manager of claim 14, wherein: The memory block allocation module allocates the partition memory block to the application program, comprising: Check whether there is a free partition memory block. If so, allocate the partition memory block to the application. If not, apply for a free preset memory block. If there is no free preset memory block, apply for a preset memory block from the kernel. After dividing the preset memory block into multiple partition memory blocks based on the first capacity, allocate the partition memory block to the application.
16. The memory manager of claim 15, wherein: After the memory block allocation module divides the partition memory blocks into multiple partition memory blocks based on the first capacity, allocating the partition memory blocks to the application includes: after the memory block allocation module divides the partition memory blocks into multiple partition memory blocks based on the first capacity, performing a cache line alignment operation on the multiple partition memory blocks, and allocating the cache line aligned partition memory blocks to the application.
17. The memory manager of claim 16, wherein: When the memory application request obtained by the request obtaining module applies for a preset number of memory blocks, the searching whether there are free preset memory blocks includes: searching whether there are free preset memory blocks of a preset number; The allocating the preset memory blocks to the application comprises: allocating a preset number of the preset memory blocks to the application; After applying for the preset memory blocks from the kernel, allocating the preset memory blocks to the application comprises: after applying for a preset number of preset memory blocks from the kernel, allocating the preset memory blocks to the application; The allocating the partition memory blocks to the application includes: allocating a preset number of the partition memory blocks to the application.
18. The memory manager of claim 14, wherein: The preset memory blocks include multiple types, and the preset memory blocks of different types have different capacities, wherein the capacity of any preset memory block is a positive integer multiple of the second capacity, and the second capacity is the minimum capacity of the preset memory blocks.
19. The memory manager of claim 18, wherein: The memory block allocation module allocates the preset memory block to the application program, including: selecting a preset memory block of a minimum capacity type that can meet the required capacity of the memory block according to the required capacity of the memory block, and allocating it to the application program.
20. The memory manager of claim 15, wherein: The memory application request obtained by the request obtaining module further includes: a thread number, the thread number being the number of the thread that issues the memory application request, each of the thread numbers corresponding to a thread memory block, the thread memory block being a partition memory block applied for according to the thread number; The memory block allocation module allocates the preset memory block to the application, including: searching whether there is a free thread memory block corresponding to the thread number according to the thread number, if so, allocating the thread memory block to the application; if not, applying for a free partition memory block as the thread memory block, and then allocating the thread memory block to the application.
21. The memory manager of claim 20, wherein: The application for the idle partition memory block as the thread memory block includes: A first number of free partition memory blocks are applied for at one time as the thread memory blocks.
22. The memory manager of claim 20, wherein: The memory block allocation module allocates the preset memory block to the application program, including: returning the first address of the preset memory block to the application program; The allocating the partition memory block to the application comprises: returning the first address of the partition memory block to the application; The allocating the thread memory block to the application includes: returning the first address of the thread memory block to the application.
23. The memory manager of claim 18, wherein: The capacity of the arbitrary preset memory block is: a positive integer power of 2 times the second capacity.
24. The memory manager of claim 20, wherein: The memory manager also includes: The memory block release module is used to obtain the memory block released by the application; Wherein, when the memory block released by the application is a preset memory block, releasing it to the kernel; When the memory block released by the application is a partition memory block, if the sum of the capacity of the released partition memory block and all the free partition memory blocks is greater than or equal to the second capacity, the released partition memory block and the free partition memory blocks are merged into a preset memory block according to the second capacity; otherwise, the released partition memory block is used as the free partition memory block; When the memory block released by the application is a thread memory block, it is used as a free partition memory block.
25. An electronic device, characterized in that: The electronic device comprises a processor and a memory manager, and is used to implement the memory management method as described in any one of claims 1 to 12.
26. A computer program product, characterized in that It comprises computer instructions, which, when executed, implement the memory management method as described in any one of claims 1 to 12.