Memory allocation method for novel nonvolatile memory

By dividing nonvolatile memory into independent areas and private memory pools, batch persistence and pre-write logging technology are adopted to solve the concurrent conflict, write wear and fragmentation problems of new nonvolatile memory, and improve memory allocation efficiency and Space utilization.

CN119127481AActive Publication Date: 2024-12-13HUAZHONG UNIV OF SCI & TECH

Patent Information

Application Number
CN202411151881.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-21
Publication Date
2024-12-13
Estimated Expiration
2044-08-21

AI Technical Summary

Technical Problem

When existing computer storage systems use new nonvolatile memory, there are problems with high concurrency conflicts in global memory allocation, write wear and memory fragmentation, resulting in low memory allocation efficiency and reduced space utilization.

Method used

By mapping nonvolatile memory into a persistent memory heap and dividing it into multiple independent areas, a private memory pool and a two-way linked list management super block is established, batch persistence and pre-write logging technology are adopted, and background thread monitoring and defragmentation are combined. Implement efficient memory allocation and management.

Benefits of technology

Reduces cache line refresh and write wear, shortens memory allocation response time, improves memory space utilization and performance, and avoids memory leaks and crash consistency problems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127481A_ABST
    Figure CN119127481A_ABST
Patent Text Reader

Abstract

The invention belongs to the technical field of computer storage, and particularly relates to a memory allocation method for a novel nonvolatile memory, which comprises the following steps of: dividing a plurality of independent memory areas from a persistent memory stack of the nonvolatile memory, and ensuring that each working thread is divided into one memory area; each memory area is logically divided into a plurality of super blocks with the same size, and idle super blocks are managed in the form of a double linked list; in the global memory application, splitting different super blocks according to different size classes, and splitting the same super block into a plurality of memory blocks with the same size; a private memory pool is set for each working thread, a super block in a corresponding size class is searched in the private memory pool according to a memory allocation request size of the working thread, an idle memory block is allocated from the super block, and a batch persistence mechanism is adopted to manage the memory block, so that cache line refreshing can be reduced, and the memory efficiency is improved. Therefore, the problems of time overhead and write wear are avoided.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of computer storage, and more specifically, relates to a memory allocation method for a novel non-volatile memory. Background Art

[0002] The new non-volatile memory (also known as persistent memory) is a new type of computer storage medium, which has the characteristics of low access latency, large storage capacity, byte addressability, and long-term data storage in the power-off state. Non-volatile memory devices include memristors, phase change memories, etc.

[0003] Existing computer storage systems use an architecture that separates internal and external storage. Internal storage uses dynamic random access memory (DRAM) with low access latency and byte addressability, which is used to cache necessary temporary data when the program is running. However, due to the volatility of memory, the data in the memory will be lost after power failure. External storage uses larger capacity and non-volatile disk (or solid-state drive) storage to save data that needs to be used for a long time. However, the access latency of external storage is extremely high (tens of thousands of times higher than that of internal storage) and it cannot be addressed by byte. It can only be read and written through the I / O interrupt of the operating system, which is extremely inefficient. The emergence of new non-volatile memory has bridged the performance gap between external storage and internal storage, and provided a foundation for building a more efficient storage system.

[0004] In order to fully utilize the performance advantages of non-volatile memory, the operating system provides a direct access mechanism that allows applications (i.e. users) to bypass the operating system kernel and directly read and write non-volatile memory in user mode. Unlike traditional disks or solid-state drives (which can only read and write at a 4KB granularity), non-volatile memory can be accessed at a byte granularity, so a finer-grained memory allocation mechanism is required.

[0005] Existing work mainly performs fine-grained memory allocation by dividing into different size classes, implements concurrent memory allocation by establishing a private memory pool for each application, and ensures crash consistency of metadata through write-ahead logging.

[0006] However, existing works generally have the problem of high concurrency conflicts in global memory allocation. When the thread private memory pool is exhausted or the required memory size exceeds the size of the maximum memory block that the thread private memory pool can provide, the thread private memory pool will become invalid. When the private memory pool fails, the thread needs to allocate memory to the global persistent memory heap. In some cases, multiple worker threads will perform global memory allocation at the same time, resulting in conflicts and competition. Furthermore, existing worker threads need to write logs and modify metadata after each memory allocation, resulting in a large number of repeated cache line refreshes, bringing additional time overhead and amplifying the write wear problem of non-volatile memory. In addition, the mutual isolation between different super blocks in the same size class will cause memory fragmentation problems, thereby reducing the utilization of memory space. Summary of the invention

[0007] In view of the defects and improvement needs of the prior art, the present invention provides a memory allocation method for a new type of non-volatile memory, which aims to reduce cache line refreshes during memory block management and avoid time overhead and write wear problems.

[0008] To achieve the above object, according to one aspect of the present invention, a memory allocation method for a novel non-volatile memory is provided, comprising:

[0009] Initialization phase: Based on the main thread, multiple independent memory areas are divided from the persistent memory heap of non-volatile memory; each memory area is logically divided into multiple super blocks of equal size and linked into a doubly linked list; multiple private memory pools are set and each private memory pool is initialized to empty; multiple empty super block doubly linked lists are generated, each doubly linked list corresponds to a memory block size class;

[0010] Allocation phase: Global memory application and memory block allocation are performed based on each worker thread, where:

[0011] The global memory application is as follows: after receiving a memory allocation request, determine whether the private memory pool of the worker thread is empty; if so, apply for a free super block from the memory area of ​​the worker thread based on a bidirectional linked list and put it into the private memory pool; determine the size class closest to the requested size from a plurality of preset optional split size classes by aligning upwards, split the free super block according to the closest size class to obtain a plurality of memory blocks, add the free super block to the bidirectional linked list of the corresponding memory block size class, and execute memory block allocation; if not, execute memory block allocation;

[0012] The memory block allocation is as follows: according to the requested size, a super block of the corresponding size class is found from the private memory pool, a free memory block in the super block is allocated to the user, and a batch persistence mechanism is used to manage the memory block of the super block, specifically: the super block header is cached in the DRAM memory to obtain a metadata copy, and when memory blocks are allocated from the super block for multiple consecutive times, only the metadata copy of the super block is updated, and when the number of updates reaches the batch value, the metadata copy is updated and synchronized to the super block header in the private memory pool.

[0013] Furthermore, it also includes:

[0014] Before the initialization phase, non-volatile memory is mapped to the user's virtual address space in direct access mode, and the virtual address space serves as the persistent memory heap.

[0015] Furthermore, the multiple independent memory areas are divided from the persistent memory heap of the non-volatile memory, specifically:

[0016] Part of the head space of the persistent memory heap is used as the heap header area to store metadata information of the persistent memory heap, and the remaining part is divided into several independent memory areas;

[0017] The metadata information includes: a formatting flag of the persistent memory heap, a starting address and a total size of the persistent memory heap, a starting address of the persistent memory heap when it was last run, metadata of the private memory pool of each worker thread, and log data.

[0018] Furthermore, each memory area is logically divided into multiple super blocks of equal size, specifically:

[0019] The header of each memory area is used to store the status information of the memory area, specifically including: the number of super blocks contained in the memory area; the number of currently free super blocks in the memory area; a pointer to the first free super block in the memory area; and a pointer to the last free super block in the memory area; the size of the header is the same as the size of the super block to be divided;

[0020] The remaining part is logically divided into multiple super blocks of equal size. The header of each super block is used to store the status information of the super block, including: the size class number of the super block; the size of each memory block in the super block; the total number of memory blocks in the super block; the number of currently free memory blocks in the super block; a bitmap marking the status of all memory blocks in the super block; the number of the first free memory block in the super block; a pointer to the next super block in the same size class; and a pointer to the previous super block in the same size class.

[0021] Further, each worker thread selects and determines a memory area from the persistent memory heap based on a hash function as the memory area of ​​the worker thread;

[0022] The hash function is: H=(TID+rand())%R; wherein TID is the thread number of the working thread, rand() is a random number generation function, R is the number of memory areas, and H is the calculated number of the memory area.

[0023] Furthermore, the status information stored in the header of each memory region also includes a 64-bit flag variable;

[0024] After determining the memory area of ​​each working thread, the CAS atomic instruction is used to modify the mark variable of the memory area to the thread number of the working thread to prevent other working threads from performing memory allocation in the memory area at the same time. After the memory allocation is completed, the working thread clears the mark variable of the memory area.

[0025] Furthermore, it also includes:

[0026] In the initialization phase, several background threads are started to monitor the usage of the private memory pool of each worker thread and the usage of the global persistent memory heap in real time, and perform defragmentation operations when specific conditions are met;

[0027] The specific conditions include: for any size class in the private memory pool of any working thread, if the proportion of free memory in the size class is higher than a preset value and the currently available memory space in the persistent memory heap is lower than the preset value, a super block merge operation is performed on the size class of the private memory pool.

[0028] Furthermore, the preset value is 80% or 20%.

[0029] The present invention also provides an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and is characterized in that when the processor executes the computer program, it implements the memory allocation method for the new type of non-volatile memory as described above.

[0030] The present invention also provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the memory allocation method for the novel non-volatile memory as described above is implemented.

[0031] In general, the above technical solutions conceived by the present invention can achieve the following beneficial effects:

[0032] (1) The present invention provides a memory allocation method for a new type of non-volatile memory, which uses a direct access mechanism to map the non-volatile memory to a persistent memory heap, and divides the persistent memory heap into several independent areas to support concurrent memory allocation for multi-threaded applications. A private memory pool is set up for each active thread to alleviate the concurrent conflict problem of multi-threaded memory allocation, while shortening the critical path length of memory allocation, greatly shortening the response time of memory allocation. Write-ahead logging technology is used to ensure the crash consistency of metadata, avoid memory leakage problems caused by partial persistence of metadata, and use batch persistence technology to accelerate the update operation of metadata, reduce the time overhead brought by write-ahead logging, and redirect a large number of write operations to non-volatile memory to write operations to DRAM memory, further improving performance and alleviating the write wear problem of non-volatile memory.

[0033] (2) Due to the mutual isolation between different size classes, a large amount of memory fragmentation may occur in some cases, reducing memory utilization. In order to reduce memory fragmentation and improve memory utilization, the memory management method provided by the present invention creates a background thread to monitor the real-time memory usage. When the available memory space is insufficient or the memory fragmentation problem is serious, a super block merge operation is performed to integrate the memory fragments in the super blocks of the same size class, and the free super blocks after integration are released, thereby generating available memory space. This method effectively reduces memory fragmentation and improves the utilization of memory space. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] Figure 1 A flowchart of a memory allocation method for a novel non-volatile memory provided by an embodiment of the present invention;

[0035] Figure 2 A schematic diagram of a batch persistence solution provided by an embodiment of the present invention;

[0036] Figure 3 An overall schematic diagram of a memory allocation method for a novel non-volatile memory provided by an embodiment of the present invention;

[0037] Figure 4 A flow chart of memory allocation request processing provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0038] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.

[0039] Embodiment 1

[0040] A memory allocation method for new non-volatile memory, such as Figure 1 As shown, including:

[0041] Initialization phase: Based on the main thread, multiple independent memory areas are divided from the persistent memory heap of non-volatile memory; each memory area is logically divided into multiple super blocks of equal size and linked into a doubly linked list; multiple private memory pools are set and each private memory pool is initialized to empty; multiple empty super block doubly linked lists are generated, each doubly linked list corresponds to a memory block size class.

[0042] It should be noted that reinitialization is required only after the program is restarted or the computer is restarted. One user is an application, corresponding to one main thread and multiple worker threads. The number of private memory pools is not less than the number of worker threads. Each worker thread private memory pool is unique to a specific thread and cannot be accessed by other threads. It contains a thread identifier. If the identifier is 0, it means that the private memory pool has not been occupied by a thread, otherwise it indicates the thread ID that occupies the private memory pool. When any thread occupies the private memory pool for the first time, the identifier will be set to the thread ID of the thread, and other threads will no longer be able to access the private memory pool.

[0043] Allocation phase: Global memory application and memory block allocation are performed based on each worker thread, where:

[0044] The global memory application is as follows: after receiving a memory allocation request, determine whether the private memory pool of the worker thread is empty. If so, apply for a free super block from the memory area of ​​the worker thread based on a doubly linked list according to the size of the memory allocation request and put it into the private memory pool; determine the size class closest to the requested size from multiple preset optional split size classes by aligning upwards (so that the size of the memory block is sufficient and not wasted), split the free super block according to the nearest size class to obtain multiple memory blocks, add the free super block to the doubly linked list of the corresponding memory block size class, and execute memory block allocation; if not, execute memory block allocation;

[0045] The memory block allocation is as follows: according to the requested size, a super block of the corresponding size class is searched from the private memory pool, a free memory block in the super block is allocated to the user, and a batch persistence mechanism is used to manage the memory block of the super block, specifically: the super block header is cached in the DRAM memory to obtain a metadata copy, and when memory blocks are allocated from the super block for multiple consecutive times, only the metadata copy of the super block is updated, and the update counter is increased by one. When the value of the update counter reaches the batch size, the metadata copy is updated and synchronized to the super block header in the private memory pool, and the update counter is cleared.

[0046] The private memory pool of each worker thread caches a small number of super blocks allocated from the global persistent memory heap. These super blocks are organized into multiple doubly linked lists according to different size classes. Super blocks in the same size class are in the same linked list and are split into several small memory blocks of the same size. When the super blocks in the thread private memory pool are insufficient, several free super blocks will be allocated from the global persistent memory heap to supplement them.

[0047] The method of this embodiment involves the fine-grained memory allocation problem of a new type of non-volatile memory in direct access mode. In the user state of the application, the non-volatile memory mapped to the virtual address space (this part of the memory space is called the persistent memory heap) is finely allocated and recycled. Specifically, the free super blocks are managed and allocated in the form of a doubly linked list, and the global memory application will be allocated and recycled at the granularity of the super block; at the same time, it is necessary to support multi-threaded concurrent memory allocation to ensure the crash consistency of metadata.

[0048] A memory allocation method proposed in this embodiment first divides multiple independent memory areas from the persistent memory heap of non-volatile memory to ensure that each working thread will be assigned to a memory area for memory application; in addition, each memory area in the persistent memory heap is logically divided into several super blocks of equal size, and the free super blocks are managed and allocated in the form of a bidirectional linked list, and global memory application is performed in units of super blocks; in global memory application, different super blocks are split according to different size classes, and the same super block is split into several memory blocks of the same size, and the sizes of memory blocks in different super blocks may be different. At the same time, a private memory pool is set for each working thread for thread-local memory allocation. According to the size of the memory allocation request of the working thread, the super block in the corresponding size class is searched in its private memory pool, and a free memory block is allocated from the super block. When allocating memory blocks, a batch persistence mechanism is proposed, which adopts the method of writing logs first and then modifying metadata to ensure the crash consistency of metadata.

[0049] This embodiment uses a write-ahead log technology to ensure the crash consistency of metadata. Due to the persistence characteristics of the new non-volatile memory, when the system crashes unexpectedly and loses power, the metadata in the CPU cache may not have enough time to be written back to the non-volatile memory, resulting in partial persistence of metadata. Partial persistence of metadata can cause inconsistency in the system state, thereby causing serious problems such as memory leaks and runtime errors. In order to avoid partial persistence of metadata, this embodiment uses a write-ahead log technology. Specifically, when an application process allocates a memory block from a super block, the header metadata of the super block will first be written to the log area and persisted, and then the metadata of the super block can be modified. In this way, after the system crashes, the header metadata of the super block can be restored based on the metadata items saved in the log.

[0050] This embodiment provides a batch persistence mechanism to reduce the time overhead of frequent log writing. Due to the loop structure of the program, there may be a situation where the same size of memory blocks are allocated multiple times in a row, and these same size memory blocks are often obtained from the same super block. If the metadata of the super block (that is, the header of the super block) is logged and persisted after each memory block is allocated, the same memory address will be written repeatedly, resulting in unnecessary time overhead. To address this problem, Figure 2 As shown, the present invention adopts batch persistence technology to cache metadata items in non-volatile memory into DRAM memory, obtain metadata item copies (called volatile headers), and only update the metadata backup in DRAM memory when allocating memory blocks from the same super block for multiple consecutive times, and increase the update counter by one. When the value of the update counter reaches the batch size, the update of metadata in DRAM is synchronized to the non-volatile memory, and the update counter is cleared. The specific synchronization frequency is determined by the batch size. Due to the volatility of DRAM, there is no need to write logs when updating the metadata copy in DRAM, and logs need to be written to non-volatile memory only during synchronization operations. The batch persistence mechanism can effectively reduce repeated log writing and non-volatile memory writing operations, and redirect a large number of write operations to non-volatile memory to write operations to DRAM memory, further improving performance, while alleviating the write wear problem of non-volatile memory.

[0051] As a preferred embodiment, the method further comprises:

[0052] Before the above initialization phase, the non-volatile memory device is mounted to the file system, and the non-volatile memory is mapped to the virtual address space of the application through memory mapping, and the virtual address space serves as a persistent memory heap.

[0053] For example, the non-volatile memory in this embodiment is mounted using the following command:

[0054] sudondctl create-namespace --mode=devdax --map=mem.

[0055] This preferred method is a memory allocation method for a new type of non-volatile memory. It uses a direct access mechanism to map non-volatile memory to a persistent memory heap, and divides the persistent memory heap into several independent areas to support concurrent memory allocation for multi-threaded applications. A private memory pool is set up for each active thread to alleviate the concurrent conflict problem of multi-threaded memory allocation, while shortening the critical path length of memory allocation, greatly shortening the response time of memory allocation. Write-ahead log technology is used to ensure the crash consistency of metadata, avoid memory leaks caused by partial persistence of metadata, and use batch persistence technology to accelerate metadata update operations, reduce the time overhead brought by write-ahead logs, and redirect a large number of write operations to non-volatile memory to write operations to DRAM memory, further improving performance and alleviating the write wear problem of non-volatile memory.

[0056] As a preferred implementation, the above-mentioned method of dividing multiple independent memory areas from the persistent memory heap of the non-volatile memory is as follows:

[0057] Part of the head space of the persistent memory heap is used as a heap header area. The size of the heap header area is, for example, 1 MB, for storing important metadata information of the persistent memory heap. The remaining part is divided into several independent memory areas for memory allocation by different threads.

[0058] Among them, important metadata information includes: for example, the formatting flag of the persistent memory heap with a size of 8 bytes; the starting address and total size of the persistent memory heap; the starting address of the persistent memory heap when it was last run; the metadata of the private memory pool of each worker thread; and log data.

[0059] like Figure 3 As shown, the remaining memory space except the heap header is divided into several independent memory areas of the same size. In this embodiment, the size of each memory area is set to 256MB. Further, the head 16KB of each memory area is used to store the status information of the memory area.

[0060] As a preferred implementation, the above-mentioned logical division of each memory area into multiple super blocks of equal size is specifically as follows:

[0061] The header of each memory area is used to store the status information of the memory area, specifically including: the number of super blocks contained in the memory area; the number of currently free super blocks in the memory area; a pointer to the first free super block in the memory area; and a pointer to the last free super block in the memory area; the size of the header is the same as the size of the super block to be divided, for example, 16KB;

[0062] The remaining part is logically divided into multiple super blocks of equal size. The header of each super block (for example, the first 128 bytes) is used to store the status information of the super block, including: the size class number of the super block; the size of each memory block in the super block; the total number of memory blocks in the super block; the number of currently free memory blocks in the super block; a bitmap marking the status of all memory blocks in the super block; the number of the first free memory block in the super block; a pointer to the next super block in the same size class; and a pointer to the previous super block in the same size class.

[0063] In this embodiment, global memory application is performed in units of super blocks. That is, each global application will obtain several continuous free super blocks. The free super blocks in each memory area are organized in the form of a two-way linked list and arranged in order from low to high by address. When each memory area allocates a super block, it will traverse the super block linked list from front to back and allocate it according to the principle of the first adaptation algorithm; when reclaiming a super block, it will traverse the super block linked list from front to back and select a suitable position for insertion according to the principle of address order. After insertion, the front and back continuous free super blocks will be merged.

[0064] The above preset multiple optional split size classes may include:

[0065] {8,16,24,32,40,48,56,64,80,96,112,128,160,192,224,256,320,384,448,512,640,768,896,1024,1280,1536,1792,2048,2560,3072,3584,4096,5120,6144,7168,8192}

[0066] In this embodiment, any request size will be aligned upward to the nearest size class. For example, a memory allocation request of 500 bytes will be aligned upward to 512 bytes. Figure 4As shown in the figure, memory allocation requests larger than 8192 bytes (8KB) ​​will directly allocate several consecutive free super blocks. For example, a memory allocation request of 50KB will obtain 4 consecutive free super blocks, totaling 64KB of continuous memory space. Memory allocation requests larger than 128MB will directly map a new memory area.

[0067] A small amount of super blocks allocated from the global persistent memory heap are cached in the private memory pool of each working thread. These super blocks are organized into multiple doubly linked lists according to different size classes. Super blocks in the same size class are in the same linked list and are split into several small memory blocks of the same size. In this embodiment, there are 36 different size classes, so there are up to 36 doubly linked lists in each thread private memory pool, and the number of linked lists is determined according to user needs. The super blocks in the linked list are respectively divided into small memory blocks of {8, 16, 24, 32, ..., 8192} bytes in size.

[0068] When the super blocks in the thread's private memory pool are insufficient, the thread will apply for several free super blocks from the global persistent memory heap to supplement them.

[0069] When a thread applies for global memory, a hash function will be used to specify a memory area for the thread. That is, as a preferred implementation, each worker thread selects and determines a memory area from the persistent memory heap based on the hash function as the memory area of ​​the worker thread.

[0070] The hash function is: H=(TID+rand())%R; wherein TID is the thread number of the working thread, rand() is a random number generation function, R is the number of memory areas, and H is the calculated number of the memory area.

[0071] As a preferred implementation, the status information stored in the header of each memory area also includes a 64-bit flag variable;

[0072] After determining the target memory area of ​​each working thread, the CAS atomic instruction is used to modify the mark variable of the target memory area to the thread number of the working thread to prevent other working threads from performing memory allocation in the memory area at the same time. After the memory allocation is completed, the working thread clears the mark variable of the memory area.

[0073] That is, to prevent multiple threads from allocating memory in the same memory area at the same time, the thread that arrives at the area first will use the CAS atomic instruction to modify the mark variable of the area to its own thread number. Threads that arrive at the area subsequently will no longer be able to enter the area for memory allocation until the previous thread completes the allocation and exits the area.

[0074] like Figure 4 As shown, when the user's memory allocation request does not exceed 8KB, it will be allocated from its private memory pool. If the thread has not occupied a private memory pool, an idle private memory pool will be occupied for it. Preferably, in this embodiment, a total of 1024 idle private memory pools are set, which can support up to 1024 worker threads to perform memory allocation simultaneously. Further preferably, when the private memory pool of a thread is exhausted, a super block will be applied from the global persistent memory heap for replenishment.

[0075] As a preferred embodiment, the method further comprises:

[0076] During initialization, several background threads are started to monitor the usage of the private memory pool of each worker thread and the usage of the global persistent memory heap in real time, and perform defragmentation operations when specific conditions are met;

[0077] The specific conditions include: for any size class in the private memory pool of any worker thread, if the proportion of free memory in the size class is higher than a preset value and the currently available memory space in the persistent memory heap is lower than the preset value, then a super block merge operation is performed on the size class in the private memory pool. Preferably, the preset value is 80% or 20%.

[0078] Due to the mutual isolation between different size classes, a large amount of memory fragmentation may occur in some cases, reducing memory utilization. In order to reduce memory fragmentation and improve memory utilization, the memory management method provided by the present invention creates a background thread to monitor the real-time memory usage, and performs a super block merge operation when the available memory space is insufficient or the memory fragmentation problem is serious, and integrates the memory fragments in the super blocks of the same size class, and releases the free super blocks after integration, thereby generating available memory space. The method effectively reduces memory fragmentation and improves the utilization of memory space.

[0079] When the super block merge operation is performed in this embodiment, for example, the background thread selects the super block B1 with the lowest usage rate in the current super block linked list and the super block B2 with the highest usage rate among the super blocks that meet the usage rate not higher than 1-r(B1), where r(B1) represents the usage rate of super block B1. B1 and B2 are removed from the current linked list, and all allocated memory blocks in B1 are copied to the free memory blocks in B2. After the copying is completed, B1 is returned to the global persistent memory heap, and B2 is added back to the super block linked list. Since r(B2)≤1-r(B1), it can be proved that the free memory blocks in B2 are not less than the allocated memory blocks in B1, that is, the free memory blocks in B2 are sufficient to accommodate the copy operation of all allocated memory blocks in B1.

[0080] In general, if Figure 3As shown, in order to make full use of the performance advantages of the new non-volatile memory, the present invention maps the non-volatile memory to a persistent memory heap in direct access mode, and divides the persistent memory heap into several independent memory areas, each of which is managed and allocated in units of super blocks. In addition, a private memory pool is set for each worker thread to reduce the conflict problem of multi-threaded concurrent memory allocation. In order to ensure the crash consistency of metadata and avoid memory leaks and runtime errors caused by system crashes, the present invention adopts the method of writing logs first and then modifying metadata. Furthermore, the present invention adopts a batch persistence mechanism to reduce the time overhead of writing logs. In addition, the present invention creates a background thread to monitor the usage and fragmentation of the persistent memory heap in real time, and performs super block merge operations when necessary, thereby reducing memory fragmentation and improving memory space utilization.

[0081] Embodiment 2

[0082] An electronic device comprises a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.

[0083] The electronic device may be a computing device such as a desktop computer, a notebook, a PDA, a cloud server, etc. The processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components, etc. The memory may be used to store computer programs and / or modules, and the processor may perform various functions of the electronic device by running or executing computer programs and / or modules stored in the memory, and calling data stored in the memory.

[0084] The relevant technical solution is the same as that in Embodiment 1 and will not be described in detail here.

[0085] Embodiment 3

[0086] A computer-readable storage medium stores a computer program, which implements the steps of the method described above when executed by a processor.

[0087] Specifically, the memory may include a high-speed random access memory, and may also include a non-volatile memory, such as a hard disk, an internal memory, a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.

[0088] The relevant technical solution is the same as that in Embodiment 1 and will not be described in detail here.

[0089] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.

Claims

1. A memory allocation method for a novel non-volatile memory, characterized in that: include: Initialization phase: Based on the main thread, multiple independent memory areas are divided from the persistent memory heap of non-volatile memory; Each memory area is logically divided into multiple super blocks of equal size and linked into a doubly linked list; multiple private memory pools are set and each private memory pool is initialized to be empty; multiple empty super block doubly linked lists are generated, each doubly linked list corresponds to a memory block size class; Allocation phase: Global memory application and memory block allocation are performed based on each worker thread, where: The global memory application is as follows: after receiving a memory allocation request, determine whether the private memory pool of the worker thread is empty; if so, apply for a free super block from the memory area of ​​the worker thread based on a bidirectional linked list and put it into the private memory pool; determine the size class closest to the requested size from a plurality of preset optional split size classes by aligning upwards, split the free super block according to the closest size class to obtain a plurality of memory blocks, add the free super block to the bidirectional linked list of the corresponding memory block size class, and execute memory block allocation; if not, execute memory block allocation; The memory block allocation is as follows: according to the requested size, a super block of the corresponding size class is found from the private memory pool, a free memory block in the super block is allocated to the user, and a batch persistence mechanism is used to manage the memory block of the super block, specifically: the super block header is cached in the DRAM memory to obtain a metadata copy, and when memory blocks are allocated from the super block for multiple consecutive times, only the metadata copy of the super block is updated, and when the number of updates reaches the batch value, the metadata copy is updated and synchronized to the super block header in the private memory pool.

2. The memory allocation method according to claim 1, characterized in that: Also includes: Before the initialization phase, non-volatile memory is mapped to the user's virtual address space in direct access mode, and the virtual address space serves as the persistent memory heap.

3. The memory allocation method according to claim 1, characterized in that: The method of dividing a plurality of independent memory areas from the persistent memory heap of the non-volatile memory is as follows: Part of the head space of the persistent memory heap is used as the heap header area to store metadata information of the persistent memory heap, and the remaining part is divided into several independent memory areas; The metadata information includes: a formatting flag of the persistent memory heap, a starting address and a total size of the persistent memory heap, a starting address of the persistent memory heap when it was last run, metadata of the private memory pool of each worker thread, and log data.

4. The memory allocation method according to claim 1, characterized in that: The above method of logically dividing each memory area into multiple super blocks of equal size is as follows: The header of each memory area is used to store the status information of the memory area, specifically including: the number of super blocks contained in the memory area; the number of currently free super blocks in the memory area; a pointer to the first free super block in the memory area; and a pointer to the last free super block in the memory area; the size of the header is the same as the size of the super block to be divided; The remaining part is logically divided into multiple super blocks of equal size. The header of each super block is used to store the status information of the super block, including: the size class number of the super block; the size of each memory block in the super block; the total number of memory blocks in the super block; the number of currently free memory blocks in the super block; a bitmap marking the status of all memory blocks in the super block; the number of the first free memory block in the super block; a pointer to the next super block in the same size class; and a pointer to the previous super block in the same size class.

5. The memory allocation method according to claim 1, characterized in that: Each worker thread selects and determines a memory area from the persistent memory heap based on a hash function as the memory area of ​​the worker thread; The hash function is: H=(TID+rand())%R; wherein TID is the thread number of the working thread, rand() is a random number generation function, R is the number of memory areas, and H is the calculated number of the memory area.

6. The memory allocation method according to claim 5, characterized in that: The status information stored in the header of each memory area also includes a 64-bit flag variable; After determining the memory area of ​​each working thread, the CAS atomic instruction is used to modify the mark variable of the memory area to the thread number of the working thread to prevent other working threads from performing memory allocation in the memory area at the same time. After the memory allocation is completed, the working thread clears the mark variable of the memory area.

7. The memory allocation method according to claim 1, characterized in that: Also includes: In the initialization phase, several background threads are started to monitor the usage of the private memory pool of each worker thread and the usage of the global persistent memory heap in real time, and perform defragmentation operations when specific conditions are met; The specific conditions include: for any size class in the private memory pool of any working thread, if the proportion of free memory in the size class is higher than a preset value and the currently available memory space in the persistent memory heap is lower than the preset value, a super block merge operation is performed on the size class of the private memory pool.

8. The memory allocation method according to claim 7, characterized in that: The preset value is 80% or 20%.

9. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, it implements a memory allocation method for a new type of non-volatile memory as described in any one of claims 1 to 8.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, a memory allocation method for a new type of non-volatile memory as described in any one of claims 1 to 8 is implemented.

Citation Information

Patent Citations

  • Method and device for allocating internal memory

    CN102866954A

  • Cluster fine-grained memory management method

    CN103914265A

  • Method, device and system for processing data in nonvolatile memory

    CN111444114A

  • Embedded memory database-oriented nonvolatile memory storage method and system

    CN113050886A

  • Method and system for constructing persistent memory index in non-uniform memory access architecture

    US20220413952A1

Cited By

  • Distributor, memory distribution method, memory release method and electronic equipment

    CN122173300A