CN114153785B - Memory management method and device based on remote direct memory access - Google Patents

Memory management method and device based on remote direct memory access Download PDF

Info

Publication number
CN114153785B
CN114153785B CN202111430440.7A CN202111430440A CN114153785B CN 114153785 B CN114153785 B CN 114153785B CN 202111430440 A CN202111430440 A CN 202111430440A CN 114153785 B CN114153785 B CN 114153785B
Authority
CN
China
Prior art keywords
memory
linked list
page
idle
thread
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202111430440.7A
Other languages
Chinese (zh)
Other versions
CN114153785A (en
Inventor
孙文全
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
SmartX Inc
Original Assignee
SmartX Inc
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by SmartX Inc filed Critical SmartX Inc
Priority to CN202111430440.7A priority Critical patent/CN114153785B/en
Publication of CN114153785A publication Critical patent/CN114153785A/en
Application granted granted Critical
Publication of CN114153785B publication Critical patent/CN114153785B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F15/00Digital computers in general; Data processing equipment in general
    • G06F15/16Combinations of two or more digital computers each having at least an arithmetic unit, a program unit and a register, e.g. for a simultaneous processing of several programs
    • G06F15/163Interprocessor communication
    • G06F15/173Interprocessor communication using an interconnection network, e.g. matrix, shuffle, pyramid, star, snowflake
    • G06F15/17306Intercommunication techniques
    • G06F15/17331Distributed shared memory [DSM], e.g. remote direct memory access [RDMA]
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5022Mechanisms to release resources
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Computer Hardware Design (AREA)
  • Mathematical Physics (AREA)
  • Memory System Of A Hierarchy Structure (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention provides a memory management method and device based on remote direct memory access. The method comprises the following steps: applying for a memory area from a computer system to obtain an address of the memory area, and registering the address of the memory area into RDMA equipment of the computer system; dividing the memory area into memory segments according to 8MiB based on the address of the memory area, and connecting the memory segments into a global heap linked list; responding to the thread starting of remote direct memory access, allocating memory segments for the threads from the global heap linked list, connecting the memory segments to a thread heap linked list, and returning a head pointer of the thread heap linked list to the threads; responding to a memory block allocation request received by a thread, dividing a memory segment in a thread heap linked list into four subsections of 2MiB, taking one subsection as a large page of 2MiB or dividing the subsection into eight small pages of 256KiB, dividing one large page or one small page into memory blocks with the same size, and selecting one memory block and returning the address of the memory block to the thread.

Description

Memory management method and device based on remote direct memory access
Technical Field
The invention relates to the technical field of computers, in particular to a memory management method and device based on remote direct memory access.
Background
Remote Direct Memory Access (RDMA) is generated to solve a delay of server-side data processing in network transmission. RDMA transfers material directly into the memory area of a computer over a network to move data quickly from one system to a remote system memory without any impact on the operating system, thus eliminating the need for as many computer processing functions as are needed. It eliminates the overhead of external memory copy and context switch, thus freeing up memory bandwidth and CPU cycles for improved performance of the application system.
In the existing memory management method for remote direct memory access, after a section of memory area is obtained from a computer system, the address of the memory area is registered to an RDMA device through a registration function ibv _ reg _ MR of IBVerbs MR; then, the registered memory area is divided into memory blocks with specific sizes, and the memory blocks obtained by division are placed into a queue; and when the network transmission needs, allocating the memory blocks from the queue. The memory area may be divided into a plurality of memory blocks with different sizes, or the memory area may be divided into all the memory blocks with the same size.
However, the existing memory management method for remote direct memory access has the following problems in the actual use process:
1. when the memory blocks in the queue are insufficient, system memory allocation and MR registration are triggered once, because MR registration operation belongs to system calling, the speed is low, the cost is high, and MR registration is frequently triggered when memory allocation is frequent, so that the performance of remote direct memory access is influenced;
2. performance requirements under multithreading are not considered, and even if each queue is correctly locked, when multiple threads simultaneously allocate and release memory, the performance pressure of remote direct memory access is still large.
Disclosure of Invention
The invention provides a memory management method and device based on remote direct memory access, which are used for solving the problem that MR registration and multithreading concurrency are frequently triggered by remote direct memory access in the prior art, and the influence is brought to the performance of remote direct memory access, the triggering of MR registration can be reduced, all operations of memory allocation and release are basically realized through a single thread, and the performance of remote direct memory access is ensured.
In a first aspect, the present invention provides a memory management method based on remote direct memory access, including:
applying for a memory area from a computer system, obtaining an address of the memory area allocated by the computer system, and registering the obtained address of the memory area in an RDMA (remote direct memory Access) device of the computer system;
dividing the memory area into memory segments according to 8MiB based on the obtained address of the memory area, dividing each memory segment into a head part and an available memory part, setting a linked list pointer at the head part, and connecting the memory segments obtained by division into a global heap linked list;
responding to the thread starting of remote direct memory access, allocating memory segments for the thread from the global heap chain table, setting chain table pointers at the heads of the allocated memory segments, connecting the allocated memory segments into a thread heap chain table, and returning the head pointers of the thread heap chain table to the thread;
responding to a memory block allocation request received by the thread, dividing a memory segment in the thread heap linked list into four 2MiB subsections, and based on the size of the memory block required to be allocated, taking the divided subsections as a 2MiB large page or eight 256KiB small pages;
dividing the large page or one of the small pages into memory blocks with the same size based on a preset memory allocation type matched with the size of the memory block requested to be allocated, selecting one memory block as a target memory block, and returning the address of the target memory block to the thread.
According to the memory management method based on remote direct memory access provided by the invention, the method further comprises the following steps:
for each thread stack linked list, pages with the same size of the divided memory blocks are respectively connected into a page idle linked list and a page use linked list based on the use condition of the memory blocks to form a page linked list group; wherein the page is the large page or the small page;
responding to the thread to receive a memory block allocation request, and determining a memory allocation type matched with the size of the memory block requested to be allocated based on a preset memory allocation type;
inquiring a corresponding page idle linked list in the page linked list group based on the determined memory allocation type;
judging whether an idle memory block exists in a corresponding page idle linked list or not;
if an idle memory block exists in the corresponding page idle linked list, taking one idle memory block from one page in the corresponding page idle linked list as a target memory block, and returning the address of the target memory block to the thread;
and if the corresponding page idle linked list does not have an idle memory block, connecting the page in the corresponding page idle linked list to the corresponding page use linked list.
According to the memory management method based on remote direct memory access provided by the invention, the method further comprises the following steps:
for each thread pile linked list, connecting large pages which are not divided into memory blocks in the thread pile linked list into an idle large page linked list, and connecting small pages which are not divided into memory blocks in the thread pile linked list into an idle small page linked list;
judging whether the corresponding page idle linked list is empty or not;
if the corresponding page idle linked list is not empty, judging whether an idle memory block exists in the corresponding page idle linked list or not;
if the corresponding page idle linked list is empty, inquiring the idle large page linked list or the idle small page linked list based on the determined memory allocation type;
taking out a page from the corresponding idle large page linked list or idle small page linked list, dividing the taken out page into memory blocks with the same size based on the determined memory allocation type, and connecting the pages divided into the memory blocks into the corresponding page idle linked list;
and then, an operation of taking out an idle memory block from one page in the corresponding page idle linked list as a target memory block is executed.
According to the memory management method based on remote direct memory access provided by the invention, the method further comprises the following steps:
judging whether the corresponding idle large page linked list or idle small page linked list is empty or not;
if the corresponding idle large page chain table or idle small page chain table is not empty, taking out a page from the corresponding idle large page chain table or idle small page chain table;
if the corresponding idle large page linked list or the idle small page linked list is empty, querying the thread stack linked list;
judging whether the thread heap linked list has idle subsections which are not used as large pages or divided into small pages;
if the thread stack linked list has idle subsections, based on the determined memory allocation type, taking an idle subsection as a large page or dividing the idle subsection into eight small pages, and connecting the obtained large page or small page to a corresponding idle large page linked list or idle small page linked list;
then, the operation of taking out a page from the corresponding idle large page linked list or idle small page linked list is executed;
if the thread stack linked list does not have an idle subsection, judging whether the thread stack linked list has an idle memory section which is not divided into subsections;
if the thread heap linked list has an idle memory segment, dividing an idle memory segment into four subsections;
then, based on the determined memory allocation type, an idle subsection is taken as a large page or divided into eight small pages, and the obtained large page or small page is connected to the corresponding operation in the idle large page chain table or idle small page chain table
And if the thread heap linked list does not have an idle memory segment, applying for the memory segment from the global heap linked list.
According to the memory management method based on remote direct memory access provided by the invention, the method further comprises the following steps:
aiming at each thread stack linked list, connecting idle blocks in each page in the page idle linked list into an idle block linked list of the page;
responding to the thread receiving a memory block release request, and determining a target memory segment where the memory block requested to be released is located based on the address of the memory block requested to be released;
judging whether a head pointer of a thread stack linked list where the target memory segment is stored at the head of the target memory segment is consistent with a head pointer of the thread stack linked list of the thread;
if the head pointer of the thread stack linked list stored at the head of the target memory segment is consistent with the head pointer of the thread stack linked list of the thread, releasing the same-thread memory, inquiring the page linked list group, and judging whether the page of the memory block requested to be released is in the corresponding page idle linked list or not;
if the page where the memory block requested to be released is located is in the corresponding page idle linked list, connecting the memory block requested to be released to the idle linked list of the page where the memory block is located;
if the page where the memory block requested to be released is located in the corresponding page use linked list, connecting the page where the memory block requested to be released is located to the corresponding page idle linked list;
and if the head pointer of the thread stack linked list stored at the head of the target memory segment is inconsistent with the head pointer of the thread stack linked list of the thread, connecting the memory block requested to be released to the cross-thread release linked list pointed by the cross-thread release linked list pointer stored at the head of the target memory segment for cross-thread memory release.
The memory management method based on remote direct memory access provided by the invention further comprises the following steps:
judging whether all memory blocks in the page where the memory blocks requested to be released are in idle states or not;
if all the memory blocks in the page where the memory block requested to be released is in an idle state, connecting the page where the memory block requested to be released is in a corresponding idle large page linked list or idle small page linked list;
judging whether all the small pages in the subsections where the small pages connected to the idle small page linked list are in idle states or not; or, judging whether the length of the idle large page linked list exceeds a preset threshold value:
if all the small pages in the subsections where the small pages connected to the idle small page linked list are in idle states, deleting all the small pages in the subsections from the idle small page linked list; if the length of the idle large page linked list exceeds a preset threshold value, deleting large pages from the idle large page linked list to enable the length of the idle large page linked list not to exceed the preset threshold value;
the querying the idle large page chain table or the idle small page chain table based on the determined memory allocation type includes:
traversing all memory segments in the thread stack linked list, and sequentially releasing memory blocks in the cross-thread release linked list pointed by pointers of the cross-thread release linked list stored at the head of the memory segments in the thread stack linked list;
if the size of the memory block in the cross-thread release linked list released by the current memory segment meets the determined memory allocation type, or the memory block in the cross-thread release linked list released by the current memory segment forms an idle page meeting the determined memory allocation type, stopping traversing the memory segment in the thread stack linked list, and allocating the memory block;
and if the sizes of the memory blocks in the cross-thread release linked list released by traversing all the memory segments in the thread stack linked list do not accord with the determined memory allocation type, or the memory blocks in the cross-thread release linked list released by traversing all the memory segments in the thread stack linked list do not form an idle page which accords with the determined memory allocation type, inquiring the idle large page linked list or the idle small page linked list based on the determined memory allocation type.
According to the memory management method based on remote direct memory access provided by the present invention, before dividing the memory area into memory segments according to 8MiB based on the obtained address of the memory area, the method includes:
and constructing a memory area address tree for each RDMA device based on the obtained addresses of the memory areas, taking the memory areas obtained each time as leaf nodes of the tree, and taking the starting address and the ending address of the memory areas as parent nodes of the leaf nodes and parent nodes when the parent nodes are used as child nodes respectively.
In a second aspect, the present invention further provides an electronic device, which includes a storage, a processor, and a computer program stored in the storage and executable on the processor, where the processor executes the computer program to implement the steps of the memory management method based on remote direct memory access according to the first aspect.
In a third aspect, the present invention further provides a non-transitory computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, implements the steps of the memory management method based on remote direct memory access according to the first aspect.
In a fourth aspect, the present invention further provides a computer program product having a computer program stored thereon, which when executed by a processor, implements the steps of the remote direct memory access-based memory management method according to the first aspect.
According to the memory management method based on remote direct memory access, the memory area which is large enough is obtained to register on the RDMA device, triggering of MR registration is reduced, and large time consumption, which is difficult to predict due to single memory allocation, caused by frequent triggering of MR registration when the memory is insufficient is avoided. Meanwhile, the memory is allocated to each thread, all operations of memory allocation and memory release are basically realized through a single thread, performance consumption and safety risks of multi-thread concurrent operation are avoided, optimization and verification are easy, the speed and stability of remote direct memory access can be guaranteed, and the performance of remote direct memory access is guaranteed.
Drawings
In order to more clearly illustrate the present invention or the technical solutions in the prior art, the drawings used in the embodiments or the description of the prior art will be briefly described below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and other drawings can be obtained by those skilled in the art without creative efforts.
FIG. 1 is a flow chart illustrating a method for memory management based on remote direct memory access according to the present invention;
FIG. 2 is a schematic diagram of a memory segment in a thread heap linked list according to the present invention;
FIG. 3 is a schematic flow chart of another memory allocation provided by the present invention;
FIG. 4 is a schematic diagram of a thread heap linked list according to the present invention;
FIG. 5 is a schematic flow chart of another memory allocation provided by the present invention;
FIG. 6 is a schematic flow chart of another memory allocation provided by the present invention;
FIG. 7 is a flowchart illustrating another method for memory management based on remote direct memory access according to the present invention;
FIG. 8 is a flow chart illustrating another memory release provided by the present invention;
fig. 9 is a schematic flow chart illustrating the memory release performed in the memory allocation process according to the present invention;
FIG. 10 is a schematic structural diagram of a cross-thread release chain table provided by the present invention;
FIG. 11 is a flow chart illustrating a further method for memory management based on remote direct memory access according to the present invention;
FIG. 12 is a diagram of a memory region address tree according to the present invention;
fig. 13 is a schematic structural diagram of a special-shaped memory segment provided in the present invention;
fig. 14 is a schematic structural diagram of an electronic device provided by the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings, and it is obvious that the described embodiments are some, but not all embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The following describes a memory management method based on remote direct memory access with reference to fig. 1 to 13.
Referring to fig. 1, fig. 1 is a schematic flow chart of a memory management method based on remote direct memory access according to the present invention, where the memory management method based on remote direct memory access shown in fig. 1 may be executed by a memory allocator, for example, the memory allocator may implement a dmalloc function, which is not limited in this embodiment of the present invention, and as shown in fig. 1, the memory management method based on remote direct memory access at least includes:
101, applying for a memory region from a computer system, obtaining an address of the memory region allocated by the computer system, and registering the obtained address of the memory region in an RDMA device of the computer system.
In the embodiment of the present invention, when a computer transmits data through remote direct memory access, a memory area needs to be applied to a computer system through a memory allocator, after the memory allocator obtains an address of the memory area allocated to the computer system, the memory allocator registers the obtained address of the memory area into an RDMA device of the computer system, for example, the RDMA device may be a network card, that is, the obtained memory area is registered as mr (memory region), so as to obtain credentials lkey and rkey required for local and remote access, and corresponding keys need to be specified during each input or output operation on the registered memory area, and these keys are stored and distributed by the memory allocator. The implementation method for obtaining the address of the memory region allocated by the computer system by the memory allocator in the embodiment of the present invention is not limited, for example, in Linux, the memory allocator may obtain the address of the memory region allocated by the computer system by using mmap. The memory allocation unit typically obtains at least a few GiB memory regions at a time for performance reasons. Considering that the memory segment needs to be aligned to 8MiB, when the address of the memory region is obtained from the computer system, the memory allocator may obtain the expected size plus the address of the memory region of 8MiB to ensure that the size of the aligned memory region still meets the expectation. To ensure performance, the memory allocator may provide the option of specifying large pages of memory at mmap.
And 102, dividing the memory area into memory segments according to 8MiB based on the obtained address of the memory area, dividing each memory segment into a head part and an available memory part, setting a linked list pointer at the head part, and connecting the memory segments obtained by division into a global heap linked list.
In the embodiment of the present invention, after the memory allocator registers the obtained memory area as the MR, the memory allocator may divide the registered memory area into memory segment segments according to the size of 8MiB and the alignment of 8MiB, may divide each of the memory segments obtained by the division into a head portion and an available memory portion, may set a linked list pointer at the head portion of each of the memory segments obtained by the division, and connect the memory segments obtained by the division into a global heap linked list through the linked list pointer, and the memory allocator holds the head pointer of the global heap linked list. The global heap linked list may be provided with a lock, and when a memory segment is acquired and returned from the global heap linked list, the lock needs to be acquired first, and then the corresponding operation of the linked list is performed through the operation of the head pointer of the global heap linked list.
And 103, responding to the thread starting of the remote direct memory access, allocating memory segments for the thread from the global heap chain table, setting a chain table pointer at the head of the allocated memory segments, connecting the allocated memory segments into a thread heap chain table, and returning the head pointer of the thread heap chain table to the thread.
In the embodiment of the invention, when a computer has a plurality of threads through remote direct memory access transmission data, a memory distributor distributes a memory segment for each thread from a global heap linked list after responding to the start of each thread, sets a linked list pointer at the head of the memory segment distributed by each thread, connects the memory segments distributed by each thread into a thread heap linked list of the thread through the linked list pointer, and returns the head pointer of the thread heap linked list to the corresponding thread. Each thread has a thread heap linked list and manages memory allocation requests for the thread.
And 104, responding to the fact that the thread receives a memory block allocation request, dividing a memory segment in the thread heap linked list into four 2MiB subsections, and based on the size of the memory block which is requested to be allocated, taking the divided subsections as a 2MiB large page or eight 256KiB small pages.
In the embodiment of the present invention, when a thread for remote direct memory access data transmission receives a memory block allocation request, the memory allocator may, in response to the thread receiving the memory block allocation request, divide a memory segment in a thread heap list of the thread into four subsegments of 2MiB size, and according to the size of the memory block that is requested to be allocated, use one obtained by the division as a large page of 2MiB in size or as eight small pages of 256 MiB in size. The first sub-segment in each memory segment is divided into seven small pages because the data of the memory segment header is to be stored.
And 105, dividing a large page or a small page into memory blocks with the same size based on a preset memory allocation type matched with the size of the memory block requested to be allocated, selecting one memory block as a target memory block, and returning the address of the target memory block to the thread.
In the embodiment of the present invention, after dividing a memory segment in the thread heap linked list into subsections, and taking the subsections as large pages or dividing the subsections into small pages, the memory allocator may match the size of the memory block requested to be allocated with a preset memory allocation type, determine a memory allocation type matched with the size of the memory block requested to be allocated, divide the large page or one of the small pages into memory blocks of the same size according to the determined memory allocation type, select one memory block from the memory blocks obtained by division as a target memory block, and return the address of the target memory block to the corresponding thread.
In the embodiment of the present invention, the preset memory allocation type may be a preset memory allocation size type, and the memory allocation size may be divided into 39 types, which are respectively smaller than 32B,
Figure BDA0003379983680000081
Figure BDA0003379983680000082
Figure BDA0003379983680000083
only the memory allocation size smaller than 2MiB is supported, each type is allocated to the memory block with the same size, the size is the maximum value of the type size, for example, the types of the memory allocation sizes corresponding to 127B and 128B are 65B to 128B, and the sizes of the allocated memory blocks are 128B.
In the embodiment of the present invention, two sizes of pages are set, i.e., a large page of 2MiB and a small page of 256 KiB. Wherein, a 2-MiB-sized page is necessary, otherwise, a 2-MiB-sized memory block cannot be provided; the reason for setting the 256KiB size page is mainly as follows: one is that for a too small allocation size, e.g., 32B, 65536 blocks are needed to fill a 2MiB page, and the whole system may not have so many blocks needed, which is wasteful; the other is that the header of each memory segment needs to store the management information of some memory segments, so the sub-segment of the first 2MiB of the memory segment cannot be used as a complete large page of the 2MiB after storing the management information of the header, and can only be divided into seven small pages.
According to the memory management method based on remote direct memory access, which is provided by the embodiment of the invention, the memory area which is large enough is obtained to register on the RDMA device, so that the triggering of MR registration is reduced, and the problem that MR registration is frequently triggered when the memory is insufficient, so that the larger time consumption that the memory allocation is difficult to predict in a single time is caused is avoided. Meanwhile, the memory is allocated to each thread, all operations of memory allocation and release are basically realized through a single thread, the performance consumption and the safety risk of multi-thread concurrent operation are avoided, optimization and verification are easy, the speed and the stability of remote direct memory access can be ensured, and the performance of the remote direct memory access is ensured.
Moreover, the number of memory fragments can be reduced, and for the memory size specification commonly used for network transmission, for the memory between 4k and 512k, the internal fragment rate is always less than 25% because the step size of the memory allocation type is 1/4; because the page size is relatively large, the outer fragmentation rate is less than 12.5%. For other allocation sizes, the memory fragmentation rate is less than 50%, and the external fragmentation rate is 0; the internal fragmentation rate problem of 50% of other allocation sizes is not significant because for allocation sizes smaller than 4k, which are relatively small and the life cycle is usually relatively short, memory waste is acceptable; for larger memory allocation requirements, the number is usually not large.
In addition, a large enough memory area is registered in the RDMA device, so that the number of MRs is small, and the query speed of the MRs can be improved.
For example, for a memory allocation requirement of 33k, if a 40k memory block is allocated, and the 40k memory block is on a 256k page, the page is placed with at most 6 blocks, so that the remaining 256-40 × 6 × 16k memory on the page cannot be utilized and is called an external fragment, and the 40k-33k memory in the memory block is allocated to be also not utilized and is called an internal fragment.
Referring to fig. 2, fig. 2 is a schematic structural diagram of a memory segment in the thread heap list according to the present invention, as shown in fig. 2, each segment has a size of 8MiB, and includes four segments with a size of 2MiB, each segment includes 1 large page with a size of 2MiB or 8 small pages with a size of 256KiB, and one small page includes 8 memory blocks with a size of 32KiB, where the first segment stores data of a segment header, so that only 7 small pages with 256KiB can be included.
Referring to fig. 3, fig. 3 is a schematic view of another memory allocation process provided by the present invention, as shown in fig. 3, the memory allocation process at least includes:
301, in response to the thread receiving the memory block allocation request, determining a memory allocation type matching the size of the memory block requested to be allocated based on a preset memory allocation type.
302, based on the determined memory allocation type, querying a corresponding page idle linked list in the page linked list group.
303, judging whether the corresponding page idle linked list has idle memory blocks.
If the corresponding page idle linked list has idle memory blocks, executing 304; if no idle memory block exists in the corresponding page idle linked list, 305 is executed.
And 304, taking out one idle memory block from one page in the corresponding page idle linked list as a target memory block, and returning the address of the target memory block to the thread.
And 305, connecting the pages in the corresponding page idle linked list to the corresponding page use linked list.
In the embodiment of the present invention, the memory allocator may, for the thread heap linked list of each thread, connect pages of the same size of the partitioned memory blocks into the page free _ list and the page use linked list full _ list respectively based on the use condition of the memory blocks to form the page linked list group, where the pages connected into the page free linked list and the page use linked list may be large pages or small pages. Referring to fig. 4, fig. 4 is a schematic structural diagram of a thread stack linked list provided by the present invention, as shown in fig. 4, two linked lists exist in a page linked list group for each size of memory block, one is free _ list, and the other is page full _ list, in fig. 4, only free _ list and full _ list of memory blocks with a size of 32B in the page linked list group are shown, where free _ list and full _ list are connected by pages of memory blocks divided into a size of 32B, free _ list is generally connected by pages of memory blocks in a free state, that is, unused memory blocks in a page, and full _ list is connected by pages of memory blocks in a page in a use state.
When a thread for remote direct memory access transmission data receives a memory block allocation request, a memory allocator responds to the thread to receive the memory block allocation request, matches the size of the memory block requested to be allocated with a preset memory allocation type, determines the memory allocation type matched with the size of the memory block requested to be allocated, then queries a corresponding page idle linked list in a page linked list group according to the determined memory allocation type, judges whether an idle memory block exists in the corresponding page idle linked list, if the idle memory block exists in the corresponding page idle linked list, takes out the idle memory block from one page in the corresponding page idle linked list as a target memory block, returns the address of the target memory block to the thread, and if the idle memory block does not exist in the corresponding page idle linked list, and connecting the pages in the corresponding page idle linked list to the corresponding page use linked list.
For example, a memory block may be allocated from a thread heap linked list through an interface function, the interface function first determines a class size _ class of a corresponding memory allocation type according to the size of the memory block that is requested to be allocated, then queries a page linked list group to find a page free linked list, free _ list, corresponding to the size _ class, and if the page free linked list, free _ list, is not empty, then takes out a free memory block from a page of the page free linked list, as a target memory block, returns a first address of the target memory block to a thread, and if no free memory block exists in the page of the page free linked list, connects the page to a corresponding page use linked list, full _ list.
Referring to fig. 5, fig. 5 is a schematic view of another memory allocation process provided by the present invention, as shown in fig. 5, the memory allocation process at least includes:
501, judging whether the corresponding page idle linked list is empty.
If the corresponding page idle linked list is empty, 502 is executed; if the corresponding page free linked list is not empty, 504 is performed.
502, based on the determined memory allocation type, querying an idle large page linked list or an idle small page linked list.
503, taking out a page from the corresponding idle large page linked list or idle small page linked list, dividing the taken out page into memory blocks with the same size based on the determined memory allocation type, and connecting the pages divided into memory blocks into the corresponding page idle linked list.
And 505, taking out an idle memory block from one page in the corresponding page idle linked list as a target memory block.
And 504, judging whether the corresponding page idle linked list has idle memory blocks.
In the embodiment of the present invention, the memory distributor may connect, for the thread stack linked list of each thread, large pages that are not divided into memory blocks in the thread stack linked list into an idle large page linked list, and connect small pages that are not divided into memory blocks in the thread stack linked list into an idle small page linked list. Referring to fig. 4, the idle large page chain table in the thread stack chain table is formed by connecting all 2MiB large pages that are not divided into memory blocks in the thread stack chain table, and the idle small page chain table in the thread stack chain table is formed by connecting all 256KiB small pages that are not divided into memory blocks in the thread stack chain table, as shown in fig. 4, a page chain table group, an idle large page chain table, an idle small page chain table, and an undivided memory segment exist in the thread stack chain table at the same time.
When a thread for accessing transmission data by a remote direct memory receives a memory block allocation request, the memory allocator responds to the thread to receive the memory block allocation request, after querying a corresponding page idle linked list in a page linked list group, can judge whether the corresponding page idle linked list is empty, if the corresponding page idle linked list is not empty, further judge whether an idle memory block exists in the corresponding page idle linked list, if an idle memory block exists in the corresponding page idle linked list, take out one idle memory block from one page in the corresponding page idle linked list as a target memory block, return the address of the target memory block to the thread, if the corresponding page idle linked list is empty, further query an idle large page linked list or an idle small page linked list according to the determined memory allocation type, and taking out a page from the corresponding idle large page linked list or idle small page linked list, dividing the taken out page into memory blocks with the same size based on the determined memory allocation type, connecting the pages divided into the memory blocks into the corresponding page idle linked list, taking out an idle memory block from one page in the corresponding page idle linked list at the moment as a target memory block, and returning the address of the target memory block to the thread.
For example, when a memory block is allocated from the thread stack linked list by the interface function "heal" - > addr ", after the interface function finds the page free linked list free _ list corresponding to size _ class in the query page linked list group, the interface function may determine whether the found page free linked list free _ list is empty, if the page free linked list free _ list is not empty, take out an idle memory block from one page of the page free linked list free _ list as the target memory block, return the first address of the target memory block to the thread, if the page free linked list free _ list is empty, query the idle large page linked list or the idle small page linked list according to size _ class, wherein if size _ class is smaller than 256KiB, query the idle small linked list page, otherwise query the idle large page linked list, if the queried idle large page linked list or the idle small page linked list is not empty, take out a page from the head of the idle large page linked list, dividing the taken out page into memory blocks with the size corresponding to the size _ class according to the size _ class, connecting the pages divided into the memory blocks to the tail part of the corresponding page free linked list free _ list, taking out one free memory block from one page of the corresponding page free linked list free _ list as a target memory block, and returning the first address of the target memory block to the thread.
Referring to fig. 6, fig. 6 is a schematic diagram illustrating a further memory allocation process provided by the present invention, as shown in fig. 6, the memory allocation process at least includes:
601, judging whether the corresponding idle large page linked list or idle small page linked list is empty.
If the corresponding idle large page linked list or idle small page linked list is not empty, executing 608; if the corresponding idle large page linked list or idle small page linked list is empty, 602 is executed.
And 608, taking out one page from the corresponding idle large page chain table or idle small page chain table.
And 602, inquiring a thread heap linked list.
603, judging whether the thread stack linked list has idle subsections which are not used as large pages or divided into small pages.
If the thread heap linked list has an idle sub-section, the execution is performed 607; if there are no free sub-segments in the thread heap linked list, 604 is performed.
607, based on the determined memory allocation type, taking an idle sub-segment as a large page or dividing the idle sub-segment into eight small pages, and connecting the obtained large page or small page to a corresponding idle large page linked list or idle small page linked list.
604, it is determined whether there are any free memory segments in the thread heap linked list that are not divided into subsections.
If the thread heap linked list has an idle memory segment, executing 606; if the thread heap linked list does not have a free memory segment, 605 is performed.
605, apply for a memory segment from the global heap link table.
A free memory segment is divided into four subsegments 606.
In the embodiment of the invention, when a thread for remote direct memory access transmission data receives a memory block allocation request, the memory allocator responds to the thread to receive the memory block allocation request, after inquiring the idle large page chain table or the idle small page chain, the memory allocator can judge whether the corresponding idle large page chain table or the idle small page chain table is empty, if the corresponding idle large page chain table or the idle small page chain table is not empty, a page is taken out from the corresponding idle large page chain table or the idle small page chain table, if the corresponding idle large page chain table or the idle small page chain table is empty, the memory allocator can further inquire the thread stack chain table and judge whether the thread stack chain table has idle subsections which are not used as large pages or divided into small pages, if the thread stack chain table has idle subsections, an idle subsection is used as one large page or divided into eight small pages according to the determined memory allocation type, and connecting the obtained large page or small page to the corresponding idle large page linked list or idle small page linked list, and taking out one page from the corresponding idle large page linked list or idle small page linked list.
If the thread stack linked list does not have an idle subsection, whether the thread stack linked list has an idle memory section which is not divided into subsections or not can be further judged, if the thread stack linked list has the idle memory section, one idle memory section in the thread stack linked list is divided into four subsections, one subsection obtained by division is taken as a large page or divided into eight small pages according to the determined memory allocation type, the obtained large page or small page is connected to the corresponding idle large page linked list or idle small page linked list, one page is taken out from the corresponding idle large page linked list or idle small page linked list, and if the thread stack linked list does not have the idle memory section, the memory section is applied to the global stack linked list.
For example, when allocating a memory block from a thread stack list through an interface function, the interface function may determine whether the idle large page list or idle small page list is empty after querying the idle large page list or idle small page list according to size _ class, if the queried idle large page list or idle small page list is not empty, take out a page from the head of the corresponding idle large page list or idle small page list, if the queried idle large page list or idle small page list is empty, query the thread stack list, traverse memory segment in the thread stack list, try to find an idle sub-segment, use the found idle sub-segment as a large page or divide the found idle sub-segment into eight small pages, and connect the obtained large page or small page to the tail of the corresponding idle large page list or small page idle small page list, if the idle subsegment is not found, trying to find an idle memory segment, dividing the found idle memory segment into four subsegments, taking one subsegment as a large page or dividing the subsegment into eight small pages, connecting the subsegment to the tail part of the corresponding idle large page linked list or idle small page linked list, and if the idle memory segment is not found, applying for a memory segment from the global heap linked list.
When the thread receives the memory block allocation request and the memory allocator searches the idle memory blocks from the thread heap linked list and returns the idle memory blocks to the thread, if the idle memory blocks in the thread heap linked list are insufficient, the memory distributor applies for a new memory segment back to the global heap linked list, when applying for a new memory segment from the global heap linked list, if the global heap linked list is empty, the memory allocator will apply for a new memory region from the computer system, when the memory allocator requests a new memory region from the computer system again, the computer system allocates the memory region to the memory allocator according to a policy, which may be set when establishing the global heap link table, for example, the size of the memory region allocated to the memory allocator again may be specified in the policy, or the computer system may be specified in the policy to no longer allocate the memory region to the memory allocator, and the like, which is not limited in the embodiment of the present invention.
Referring to fig. 7, fig. 7 is a flowchart illustrating another remote dma-based memory management method according to the present invention, as shown in fig. 7, the remote dma-based memory management method at least includes:
701, in response to the thread receiving a memory block release request, determining a target memory segment where the memory block requested to be released is located based on an address of the memory block requested to be released.
And 702, judging whether the head pointer of the thread stack linked list where the target memory segment is stored at the head of the target memory segment is consistent with the head pointer of the thread stack linked list of the thread.
If the head pointer of the thread pile linked list stored at the head of the target memory segment is consistent with the head pointer of the thread pile linked list of the thread, executing 703; if the head pointer of the thread stack linked list stored at the head of the target memory segment is not consistent with the head pointer of the thread stack linked list of the thread, execute 706.
703, for releasing the same-thread memory, inquiring the page chain table group, and judging whether the page where the memory block requested to be released is located is in the corresponding page idle chain table.
If the page where the memory block requested to be released is located is in the corresponding page idle linked list, executing 704; if the page where the memory block requested to be released is located is in the corresponding page usage linked list, 705 is executed.
And 704, connecting the memory block requested to be released to the free block linked list of the page where the memory block is located.
705, the page where the memory block requested to be released is located is connected to the corresponding page idle linked list.
And 706, for releasing the cross-thread memory, connecting the memory block requested to be released to the cross-thread release linked list pointed by the cross-thread release linked list pointer stored at the head of the target memory segment.
In the embodiment of the present invention, the memory allocator may connect, for the thread heap linked list of each thread, the idle blocks in each page in the page idle linked list into an idle block linked list of the page, that is, each page in the page idle linked list has a corresponding idle block linked list. When a thread for accessing and transmitting data by a remote direct memory receives a memory block release request, a memory distributor can respond to the thread to receive the memory block release request, determine a target memory segment where a memory block requested to be released is located according to the address of the memory block requested to be released, judge whether a head pointer of a thread stack linked list where the target memory segment is located and stored at the head of the target memory segment is consistent with a head pointer of the thread stack linked list of the thread, if the head pointer of the thread stack linked list stored at the head of the target memory segment is consistent with the head pointer of the thread stack linked list of the thread, determine that the release of the memory block is the same-thread memory release, further judge whether a page where the memory block requested to be released is located in a corresponding page free linked list by inquiring a page linked list group, if the page where the memory block requested to be released is located in the corresponding page free linked list, connecting the memory block requested to be released to an idle block linked list of a page where the memory block is located, if the page where the memory block requested to be released is located in a corresponding page use linked list, connecting the page where the memory block requested to be released is located to the corresponding page idle linked list, and if a head pointer of a thread stack linked list stored at the head of the target memory segment is not consistent with a head pointer of the thread stack linked list of the thread, determining that the release of the memory block is cross-thread release, and connecting the memory block requested to be released to the cross-thread release linked list pointed by the cross-thread release linked list pointer stored at the head of the target memory segment.
For example, the interface function heap _ free (heap, ptr) - > void can release the memory blocks in the thread heap linked list, the interface function first finds the target memory segment where the memory block requested to be released is located through the pointer ptr, then obtains the head pointer heap of the thread heap linked list recorded in the target memory segment, if the head pointer heap of the obtained thread heap linked list points to the thread heap linked list of the current thread, that is, is consistent with the parameter heap in the interface function, the release of the memory block is considered as the same-thread memory release or the local release, finds the page free _ list and the page linked list using full _ list corresponding to the memory block requested to be released in the thread heap linked list of the current thread, if the page where the memory block requested to be released is located in the page free _ list, connects the memory block requested to the free linked list of the page where the memory block is located, if the page where the memory block requested to be released is located is in the page use linked list full _ list, connecting the page where the memory block requested to be released is located to the tail of the corresponding page idle linked list free _ list, if the head pointer heap of the obtained thread heap linked list does not point to the thread heap linked list of the current thread, namely is inconsistent with the parameter heap in the interface function, finding the cross-thread release linked list pointed to by the head pointer heap of the obtained thread heap linked list, and connecting the memory block requested to be released to the cross-thread release linked list through atomic operation.
Referring to fig. 8, fig. 8 is a schematic view of another memory release process provided by the present invention, and as shown in fig. 8, the memory release process at least includes:
801, judging whether all the memory blocks in the page where the memory block requested to be released is in an idle state.
If all the memory blocks in the page where the memory block requested to be released is in an idle state, executing 802; otherwise, the flow ends.
And 802, connecting the page where the memory block requested to be released is located to the corresponding idle large page linked list or idle small page linked list.
803, judging whether all the small pages in the subsections where the small pages connected to the idle small page linked list are in idle state or not, or judging whether the length of the idle large page linked list exceeds a preset threshold value or not.
If yes, execute 804; otherwise, the flow ends.
And 804, deleting all the small pages in the subsegment from the idle small page linked list or deleting the large pages from the idle large page linked list, so that the length of the idle large page linked list does not exceed a preset threshold value.
In the embodiment of the present invention, when a thread for remote direct memory access transmission data receives a memory block release request, the memory allocator may further determine whether all memory blocks in a page of a memory block requested to be released are in an idle state after responding to the thread receiving the memory block release request and clearing content in the memory block requested to be released or connecting the page of the memory block requested to be released to a page idle linked list corresponding to the thread, if all memory blocks in the page of the memory block requested to be released are in an idle state, connect the page of the memory block requested to be released to a corresponding idle large page linked list or idle small page linked list, and if a small page is connected to an idle small page linked list, further determine whether all small pages in a sub-segment where the small page connected to the idle small page linked list is in an idle small page linked list are in an idle state, if all the small pages in the sub-segment where the small pages connected to the idle small page linked list are located are in an idle state, deleting all the small pages in the sub-segment from the idle small page linked list, if the large pages are connected to the idle large page linked list, further judging whether the length of the idle large page linked list exceeds a preset threshold value, and if the length of the idle large page linked list exceeds the preset threshold value, deleting the large pages from the idle large page linked list to enable the length of the idle large page linked list not to exceed the preset threshold value.
Referring to fig. 9, fig. 9 is a schematic diagram illustrating a process of performing memory release during a memory allocation process according to the present invention, as shown in fig. 9, the process of performing memory release at least includes:
and 901, traversing all the memory segments in the thread stack linked list, and sequentially releasing the memory blocks in the cross-thread release linked list pointed by the pointers of the cross-thread release linked list stored at the head of the memory segments in the thread stack linked list.
And 902, if the size of the memory block in the cross-thread release linked list released by the current memory segment meets the determined memory allocation type, or the memory block in the cross-thread release linked list released by the current memory segment forms an idle page meeting the determined memory allocation type, stopping traversing the memory segments in the thread stack linked list, and allocating the memory blocks.
903, if the sizes of the memory blocks in the cross-thread release linked list released by traversing all the memory segments in the thread heap linked list do not conform to the determined memory allocation type, or the memory blocks in the cross-thread release linked list released by traversing all the memory segments in the thread heap linked list do not form an idle page conforming to the determined memory allocation type, querying the idle large page linked list or the idle small page linked list based on the determined memory allocation type.
In the embodiment of the present invention, when the corresponding page idle linked list is determined to be empty and the idle large page linked list or the idle small page linked list is queried based on the determined memory allocation type, the memory block in the cross-thread release linked list, which is recorded by the memory segment in the release thread stack linked list, is considered first. When a thread for remote direct memory access transmission data receives a memory block allocation request, the memory allocator queries an idle large page linked list or an idle small page linked list in response to the thread receiving the memory block allocation request based on the determined memory allocation type, the memory allocator may first traverse all memory segments in the thread stack linked list of the thread, and sequentially release memory blocks in a cross-thread release linked list pointed by pointers of the cross-thread release linked list stored at heads of the memory segments in the thread stack linked list, and after releasing each memory block, determine whether the size of the released memory block matches the memory allocation type corresponding to the size of the memory block requested to be allocated, or determine whether the released memory block forms an idle page matching the memory allocation type corresponding to the size of the memory block requested to be allocated, if the size of the released memory block matches the memory allocation type corresponding to the size of the memory block requested to be allocated, or the released memory blocks form idle pages which accord with the memory allocation types corresponding to the sizes of the memory blocks requested to be allocated, the traversal of the memory segments in the thread stack linked list is stopped, the memory blocks are allocated, and if the sizes of the memory blocks in the cross-thread release linked list released by traversing all the memory segments in the thread stack linked list do not accord with the memory allocation types corresponding to the sizes of the memory blocks requested to be allocated, or the memory blocks in the cross-thread release linked list released by traversing all the memory segments in the thread stack linked list do not form idle pages which accord with the memory allocation types corresponding to the sizes of the memory blocks requested to be allocated, the memory segments are applied to the global stack linked list.
The release of the memory blocks in the cross-thread release linked list can be realized by judging whether the page of the memory block in the cross-thread release linked list is in the page free linked list free _ list, if the page of the memory block in the cross-thread release linked list is in the page free linked list free _ list, connecting the memory block in the cross-thread release linked list to the free block linked list of the page in which the memory block is, and if the page of the memory block in the cross-thread release linked list is in the page use linked list full _ list, connecting the page of the memory block in the cross-thread release linked list to the page free linked list free _ list corresponding to the thread.
For example, when allocating a memory block from the thread heap linked list by the interface function "heap" - > addr, when the interface function queries the free large page linked list or the free small page linked list based on the determined memory allocation type, the interface function may first traverse all the memory segment segments in the thread heap linked list, and perform the following operations on each memory segment: 1. acquiring the content of the cross-thread release linked list through an atomic instruction, and setting the cross-thread release linked list of the memory segment to be empty; 2. traversing the cross-thread release linked list, and releasing the memory blocks in the linked list by adopting the same process as the local release; 3. and if the size of the memory block in the released memory blocks is the same as the size _ class corresponding to the size of the memory block requested to be allocated, or the released memory blocks form a free page which accords with the size _ class corresponding to the size of the memory block requested to be allocated, directly returning to the memory allocation flow, otherwise, continuously traversing.
Referring to fig. 10, fig. 10 is a schematic structural diagram of an idle block linked list provided by the present invention, as shown in fig. 10, in a memory block in the idle block linked list according to an embodiment of the present invention, a linked list pointer may be set at a head of each memory block, the memory blocks are connected in series by the linked list pointer to form a linked list, the idle block linked list is a part of a thread stack linked list, and each thread tries to take out a memory block of a specified size from the idle block linked list during memory allocation.
In the embodiments of the present invention, the cross-thread memory release only involves a single atomic operation, and the speed is very fast; because each memory segment is large enough, the frequency of acquiring or releasing the memory segment from the global memory segment by the thread stack linked list is low, and the adverse effect on the performance is small when the memory segment is spread; when the memory area registered to the RDMA device is insufficient, the memory area can be supplemented at a determined time; meanwhile, except for cross-thread memory release and the acquisition and release of memory segments from the global heap linked list, all operations of memory allocation and release are local single-thread, performance consumption and safety risks of multi-thread concurrent operation are avoided, and optimization and verification are easy.
In the embodiments of the present invention, when performing memory release, when a page is connected to the corresponding page free linked list free _ list, the page is also removed from the corresponding page use linked list full _ list, and the page is connected to the tail of the page free linked list free _ list, so as to avoid that only one free memory block exists on the page of the page free linked list free _ list, and when the page is connected to the head of the linked list, the page is easy to move back and forth between the two linked lists, i.e., the free _ list and the free _ list. When the memory is released, if all the memory blocks in a page are released, a threshold value may be preset, for example, the preset threshold value is 3, and when the length of the free _ list of the page exceeds the preset threshold value, the flag in the page is cleared and connected to the corresponding free page list; and if the length of the page free linked list free _ list does not exceed a preset threshold, connecting the page to the tail part of the page free linked list free _ list.
Referring to fig. 11, fig. 11 is a schematic flowchart illustrating a further memory management method based on remote direct memory access according to the present invention, as shown in fig. 11, the memory management method based on remote direct memory access at least includes:
1101, applying for a memory region from the computer system, obtaining an address of the memory region allocated by the computer system, and registering the obtained address of the memory region in an RDMA device of the computer system.
1102, constructing a memory area address tree for each RDMA device based on the obtained addresses of the memory areas, taking the memory areas obtained each time as leaf nodes of the tree, and respectively taking the starting address and the ending address of the memory areas as parent nodes of the leaf nodes and parent nodes when the parent nodes are child nodes.
1103, dividing the memory area into memory segments according to 8MiB based on the obtained addresses of the memory area, dividing each memory segment into a head part and an available memory part, setting a linked list pointer at the head part, and connecting the memory segments obtained by division into a global heap linked list.
And 1104, responding to the thread starting of the remote direct memory access, allocating memory segments for the thread from the global heap linked list, setting linked list pointers at the heads of the allocated memory segments, connecting the allocated memory segments into a thread heap linked list, and returning the head pointers of the thread heap linked list to the thread.
1105, in response to the thread receiving the memory block allocation request, dividing one memory segment in the thread heap linked list into four 2-MiB subsections, and based on the size of the memory block that is requested to be allocated, taking the divided subsections as a 2-MiB large page or eight 256-MiB small pages.
1106, dividing a large page or a small page into memory blocks with the same size based on a preset memory allocation type matched with the size of the memory block requested to be allocated, selecting one memory block as a target memory block, and returning the address of the target memory block to the thread.
In the embodiment of the present invention, since each MR corresponds to one segment of memory region and one RDMA device, when a process needs to perform remote direct memory access through multiple RDMA devices, or when a new memory region is additionally allocated after a first memory registration of an RDMA device, an input and output operation of the remote direct memory access is performed, a correct MR needs to be specified, or an error occurs. Allocating a segment of memory region by the memory allocator requires acquiring a corresponding MR, which is a very high frequency operation, and in order to increase the speed of this operation, a memory region address tree may be constructed for each RDMA device, and a memory region forest may be constructed from these trees.
Before dividing the memory area into memory segments according to the obtained addresses of the memory area and 8MiB, the memory allocator may further construct a memory area address tree for each RDMA device based on the obtained addresses of the memory area, where the memory area address tree uses the memory area obtained each time as a leaf node of the tree, and uses the start address and the end address of the memory area as a parent node of the leaf node and a parent node when the parent node is a child node, respectively. Referring to fig. 12, fig. 12 is a schematic diagram of a memory region address tree according to the present invention, as shown in fig. 12, the memory region address tree includes two memory regions, a [ a, B) and B [ c, d), respectively. When an MR is registered in an RDMA device, the start address and the end address of the memory region should be inserted into the memory region address tree of the device, and after the memory region address tree needs to be balanced, the method for balancing the memory region address tree may use an existing balancing method of a binary tree, which is not limited in this embodiment of the present invention.
The process of searching the memory area in the memory area forest can be realized by firstly finding a memory area address tree corresponding to the equipment through RDMA equipment, and the process can be realized by inquiring a hash table, then inquiring the found memory area address tree by taking a memory area address to be inquired as an inquiry basis, if an MR is found by inquiring the memory area address tree, the MR can be used for RDMA communication for the current RDMA equipment and the memory address, and if an MR is not found by inquiring the memory area address tree, the current memory area address is not registered as the MR for the RDMA equipment, and error information needs to be fed back.
According to the embodiment of the invention, the memory area address tree is constructed, and the memory area forest is formed, so that the rapid and accurate query of the MR of the RDMA equipment can be realized under the condition of multiple MRs and multiple RDMA equipment, and the correct MR is obtained.
In actual use, thread hot-spot transfer may occur, that is, a thread is allocated a large amount of memory at a certain time, so that a thread heap list holds a large amount of memory segment segments, and at a certain next time, the thread allocation operation is greatly reduced and another thread is greatly allocated. Thread hot-spot transfers can in some cases cause the remaining memory of the system to be clearly available for current memory usage, but because the hot-spot thread cannot retrieve enough memory segment segments, it results in insufficient or excessive memory usage.
The memory management method based on remote direct memory access provided by the embodiment of the invention can solve the problem through the memory segment return mechanism, namely, the thread heap linked list can return the memory segment with smaller current usage amount or free memory segment to the global heap linked list. Firstly, traversing all memory segment segments in a thread heap linked list through memory sorting, counting the total memory utilization rate of the thread heap linked list, and arranging the memory segment segments in an ascending order according to the block-level utilization rate; then if the idle memory segment segments are directly returned to the global heap link list, selecting a reasonable amount of low-load memory segment segments, marking the low-load memory segment segments as a state to be recovered, taking out the pages added with the page idle link list free _ list from the page link list group, and skipping the pages in the subsequent memory allocation; and finally, when the next memory sorting is triggered, returning the memory segment marked as the state to be recovered to the global heap linked list.
At this time, it is safe to return a memory segment that has not returned to the idle state to the global heap linked list, because the heap field on the memory segment is cleared at this time, and any release operation will be put into the cross-thread release linked list as cross-thread release at this time.
In the embodiment of the invention, the thread pile arrangement can be triggered by a user, because the user side can relatively accurately sense the pressure transfer, and the thread pile arrangement can be periodically and automatically triggered based on the characteristic of the thread pile arrangement.
In practical use, the storage system sometimes encounters large and irregular memory allocation requirements, for example, iSCSI may in some scenarios negotiate the size of 1280KiB memory block, and the allocation requirement of 1280KiB may result in 2048KiB memory block, which may result in 37.5% of space waste, and if there are many such allocations, huge memory pressure may be caused.
In order to deal with the common memory allocation request which does not belong to the standard allocation size, the memory management method based on remote direct memory access provided by the embodiment of the invention can solve the problem through a special-shaped allocation mechanism, namely, the special-shaped allocation mechanism uses the special-shaped memory segment.
The size of the special-shaped memory segment is still 8MiB, the structure is that memory blocks aligned according to 256KiB are continuously placed behind the head of one memory segment, please refer to fig. 13, fig. 13 is a schematic structural diagram of the special-shaped memory segment provided by the present invention, as shown in fig. 13, 6 memory blocks of 1280KiB are placed in one special-shaped memory segment of 1280KiB, compared with 3 memory blocks of 1280KiB that can be placed in a normal memory segment, the space utilization rate of the special-shaped memory segment is doubled.
The special-shaped memory segment still has a page structure, each memory block corresponds to a page, when the special-shaped memory block allocation is hit in the allocation stage, the thread heap link table tries to find a free special-shaped page with a corresponding size, if not, the thread heap link table tries to find a memory segment which is not used yet and marks the memory segment as the special-shaped memory segment, and the special-shaped memory block is allocated from the memory segment.
When the special-shaped memory blocks are released, if all the memory blocks on one special-shaped memory segment are released, the special-shaped memory segment is restored to the common memory segment.
Fig. 14 illustrates a physical structure diagram of an electronic device, and as shown in fig. 14, the electronic device may include: a processor (processor)1410, a communication interface (communication interface)1420, a memory (memory)1430 and a communication bus 1440, wherein the processor 1410, the communication interface 1420 and the memory 1430 communicate with each other via the communication bus 1440. The processor 1410 may call logic instructions in the storage 1430 to execute a remote direct memory access-based memory management method, which includes:
applying for a memory area from a computer system, obtaining an address of the memory area allocated by the computer system, and registering the obtained address of the memory area in an RDMA (remote direct memory Access) device of the computer system;
dividing the memory area into memory segments according to 8MiB based on the obtained address of the memory area, dividing each memory segment into a head part and an available memory part, setting a linked list pointer at the head part, and connecting the memory segments obtained by division into a global heap linked list;
responding to the thread starting of remote direct memory access, allocating memory segments for the thread from the global heap chain table, setting chain table pointers at the heads of the allocated memory segments, connecting the allocated memory segments into a thread heap chain table, and returning the head pointers of the thread heap chain table to the thread;
responding to a memory block allocation request received by the thread, dividing a memory segment in the thread heap linked list into four 2MiB subsections, and based on the size of the memory block required to be allocated, taking the divided subsections as a 2MiB large page or eight 256KiB small pages;
dividing the large page or one of the small pages into memory blocks with the same size based on a preset memory allocation type matched with the size of the memory block requested to be allocated, selecting one memory block as a target memory block, and returning the address of the target memory block to the thread.
In addition, the logic instructions in the memory 1430 may be implemented in software functional units and stored in a computer readable storage medium when sold or used as a stand-alone product. Based on such understanding, the technical solution of the present invention or a part thereof which substantially contributes to the prior art may be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-only memory (ROM), a Random Access Memory (RAM), a magnetic disk, an optical disk, or other various media capable of storing program codes.
In another aspect, the present invention further provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, the computer program including program instructions, when the program instructions are executed by a computer, the computer being capable of executing the memory management method based on remote direct memory access provided in the foregoing embodiments, the method including:
applying for a memory area from a computer system, obtaining an address of the memory area allocated by the computer system, and registering the obtained address of the memory area in an RDMA (remote direct memory Access) device of the computer system;
dividing the memory area into memory segments according to 8MiB based on the obtained address of the memory area, dividing each memory segment into a head part and an available memory part, setting a linked list pointer at the head part, and connecting the memory segments obtained by division into a global heap linked list;
responding to the thread starting of remote direct memory access, allocating memory segments for the thread from the global heap chain table, setting chain table pointers at the heads of the allocated memory segments, connecting the allocated memory segments into a thread heap chain table, and returning the head pointers of the thread heap chain table to the thread;
responding to a memory block allocation request received by the thread, dividing a memory segment in the thread heap linked list into four 2MiB subsections, and based on the size of the memory block required to be allocated, taking the divided subsections as a 2MiB large page or eight 256KiB small pages;
dividing the large page or one of the small pages into memory blocks with the same size based on a preset memory allocation type matched with the size of the memory block requested to be allocated, selecting one memory block as a target memory block, and returning the address of the target memory block to the thread.
In another aspect, the present invention further provides a non-transitory computer-readable storage medium, on which a computer program is stored, where the computer program, when executed by a processor, implements the method for memory management based on remote direct memory access provided in the foregoing embodiments, where the method includes:
applying for a memory area from a computer system, obtaining an address of the memory area allocated by the computer system, and registering the obtained address of the memory area in an RDMA (remote direct memory Access) device of the computer system;
dividing the memory area into memory segments according to 8MiB based on the obtained address of the memory area, dividing each memory segment into a head part and an available memory part, setting a linked list pointer at the head part, and connecting the memory segments obtained by division into a global heap linked list;
responding to the thread starting of remote direct memory access, allocating memory segments for the thread from the global heap chain table, setting chain table pointers at the heads of the allocated memory segments, connecting the allocated memory segments into a thread heap chain table, and returning the head pointers of the thread heap chain table to the thread;
responding to a memory block allocation request received by the thread, dividing a memory segment in the thread heap linked list into four 2MiB subsections, and based on the size of the memory block required to be allocated, taking the divided subsections as a 2MiB large page or eight 256KiB small pages;
dividing the large page or one of the small pages into memory blocks with the same size based on a preset memory allocation type matched with the size of the memory block requested to be allocated, selecting one memory block as a target memory block, and returning the address of the target memory block to the thread.
The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
Finally, it should be noted that: the above examples are only intended to illustrate the technical solution of the present invention, but not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.

Claims (6)

1. A memory management method based on remote direct memory access is characterized by comprising the following steps:
applying for a memory area from a computer system, obtaining an address of the memory area allocated by the computer system, and registering the obtained address of the memory area in an RDMA (remote direct memory Access) device of the computer system;
dividing the memory area into memory segments according to 8MiB based on the obtained address of the memory area, dividing each memory segment into a head part and an available memory part, setting a linked list pointer at the head part, and connecting the memory segments obtained by division into a global heap linked list;
responding to the thread starting of remote direct memory access, allocating memory segments for the thread from the global heap linked list, setting linked list pointers at the heads of the allocated memory segments, connecting the allocated memory segments into a thread heap linked list, and returning the head pointers of the thread heap linked list to the thread;
responding to a memory block allocation request received by the thread, dividing a memory segment in the thread heap linked list into four 2MiB subsections, and based on the size of the memory block requiring allocation, taking the divided subsections as a 2MiB large page or eight 256KiB small pages;
dividing the large page or one of the small pages into memory blocks with the same size based on a preset memory allocation type matched with the size of the memory block requested to be allocated, selecting one memory block as a target memory block, returning the address of the target memory block to the thread,
the method further comprises the following steps:
for each thread stack linked list, pages with the same size of the divided memory blocks are respectively connected into a page idle linked list and a page use linked list based on the use condition of the memory blocks to form a page linked list group; wherein the page is the large page or the small page;
responding to the thread to receive a memory block allocation request, and determining a memory allocation type matched with the size of the memory block requested to be allocated based on a preset memory allocation type;
inquiring a corresponding page idle linked list in the page linked list group based on the determined memory allocation type;
judging whether an idle memory block exists in a corresponding page idle linked list or not;
if an idle memory block exists in the corresponding page idle linked list, taking one idle memory block from one page in the corresponding page idle linked list as a target memory block, and returning the address of the target memory block to the thread;
if the corresponding page idle linked list does not have idle memory blocks, connecting the pages in the corresponding page idle linked list to the corresponding page use linked list,
the method further comprises the following steps:
for each thread stack linked list, connecting large pages which are not divided into memory blocks in the thread stack linked list into an idle large page linked list, and connecting small pages which are not divided into memory blocks in the thread stack linked list into an idle small page linked list;
judging whether the corresponding page idle linked list is empty or not;
if the corresponding page idle linked list is not empty, judging whether an idle memory block exists in the corresponding page idle linked list or not;
if the corresponding page idle linked list is empty, inquiring the idle large page linked list or the idle small page linked list based on the determined memory allocation type;
taking out a page from the corresponding idle large page linked list or idle small page linked list, dividing the taken out page into memory blocks with the same size based on the determined memory allocation type, and connecting the pages divided into the memory blocks into the corresponding page idle linked list;
then, an operation of taking out an idle memory block from a page in the corresponding page idle linked list as a target memory block is executed,
the method further comprises the following steps:
judging whether the corresponding idle large page linked list or idle small page linked list is empty or not;
if the corresponding idle large page chain table or idle small page chain table is not empty, taking out a page from the corresponding idle large page chain table or idle small page chain table;
if the corresponding idle large page linked list or idle small page linked list is empty, inquiring the thread stack linked list;
judging whether the thread heap linked list has idle subsections which are not used as large pages or divided into small pages;
if the thread stack linked list has an idle sub-segment, based on the determined memory allocation type, taking an idle sub-segment as a large page or dividing the idle sub-segment into eight small pages, and connecting the obtained large page or small page to a corresponding idle large page linked list or idle small page linked list;
then, the operation of taking out a page from the corresponding idle large page linked list or idle small page linked list is executed;
if the thread stack linked list does not have an idle subsection, judging whether the thread stack linked list has an idle memory section which is not divided into subsections;
if the thread heap linked list has an idle memory segment, dividing an idle memory segment into four subsections;
then, based on the determined memory allocation type, an idle subsection is used as a large page or divided into eight small pages, and the obtained large page or small page is connected to the corresponding idle large page linked list or idle small page linked list;
and if the thread heap linked list does not have an idle memory segment, applying for the memory segment from the global heap linked list.
2. The method according to claim 1, further comprising:
aiming at each thread stack linked list, connecting idle blocks in each page in the page idle linked list into an idle block linked list of the page;
responding to the thread receiving a memory block release request, and determining a target memory segment where the memory block requested to be released is located based on the address of the memory block requested to be released;
judging whether a head pointer of a thread stack linked list where the target memory segment is stored at the head of the target memory segment is consistent with a head pointer of the thread stack linked list of the thread;
if the head pointer of the thread stack linked list stored at the head of the target memory segment is consistent with the head pointer of the thread stack linked list of the thread, releasing the same-thread memory, inquiring the page linked list group, and judging whether the page of the memory block requested to be released is in the corresponding page idle linked list or not;
if the page where the memory block requested to be released is located is in the corresponding page idle linked list, connecting the memory block requested to be released to the idle block linked list of the page where the memory block is located;
if the page where the memory block requested to be released is located in the corresponding page use linked list, connecting the page where the memory block requested to be released is located to the corresponding page idle linked list;
and if the head pointer of the thread stack linked list stored at the head of the target memory segment is not consistent with the head pointer of the thread stack linked list of the thread, connecting the memory block requested to be released to the cross-thread release linked list pointed by the cross-thread release linked list pointer stored at the head of the target memory segment for cross-thread memory release.
3. The method according to claim 2, further comprising:
judging whether all memory blocks in the page where the memory block requested to be released is in an idle state or not;
if all the memory blocks in the page where the memory block requested to be released is in an idle state, connecting the page where the memory block requested to be released is in a corresponding idle large page linked list or idle small page linked list;
judging whether all the small pages in the subsections where the small pages connected to the idle small page linked list are in idle states or not; or, judging whether the length of the idle large page linked list exceeds a preset threshold value:
if all the small pages in the subsections where the small pages connected to the idle small page linked list are in idle states, deleting all the small pages in the subsections from the idle small page linked list; if the length of the idle large page linked list exceeds a preset threshold value, deleting large pages from the idle large page linked list to enable the length of the idle large page linked list not to exceed the preset threshold value;
the querying the idle large page chain table or the idle small page chain table based on the determined memory allocation type includes:
traversing all memory segments in the thread stack linked list, and sequentially releasing memory blocks in the cross-thread release linked list pointed by pointers of the cross-thread release linked list stored at the head of the memory segments in the thread stack linked list;
if the size of the memory block in the cross-thread release linked list released by the current memory segment meets the determined memory allocation type, or the memory block in the cross-thread release linked list released by the current memory segment forms an idle page meeting the determined memory allocation type, stopping traversing the memory segment in the thread stack linked list, and allocating the memory block;
and if the sizes of the memory blocks in the cross-thread release linked list released by traversing all the memory segments in the thread stack linked list do not accord with the determined memory allocation type, or the memory blocks in the cross-thread release linked list released by traversing all the memory segments in the thread stack linked list do not form an idle page which accords with the determined memory allocation type, inquiring the idle large page linked list or the idle small page linked list based on the determined memory allocation type.
4. The method according to any one of claims 1 to 3, wherein before dividing the memory region into memory segments according to 8MiB based on the obtained address of the memory region, the method comprises:
and constructing a memory area address tree for each RDMA device based on the obtained addresses of the memory areas, taking the memory areas obtained each time as leaf nodes of the tree, and taking the starting address and the ending address of the memory areas as parent nodes of the leaf nodes and parent nodes when the parent nodes are used as child nodes respectively.
5. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor executes the program to implement the steps of the method for memory management based on remote direct memory access according to any one of claims 1 to 4.
6. A non-transitory computer readable storage medium, on which a computer program is stored, wherein the computer program, when being executed by a processor, implements the steps of the memory management method based on remote direct memory access according to any one of claims 1 to 4.
CN202111430440.7A 2021-11-29 2021-11-29 Memory management method and device based on remote direct memory access Active CN114153785B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111430440.7A CN114153785B (en) 2021-11-29 2021-11-29 Memory management method and device based on remote direct memory access

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111430440.7A CN114153785B (en) 2021-11-29 2021-11-29 Memory management method and device based on remote direct memory access

Publications (2)

Publication Number Publication Date
CN114153785A CN114153785A (en) 2022-03-08
CN114153785B true CN114153785B (en) 2022-08-30

Family

ID=80784286

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111430440.7A Active CN114153785B (en) 2021-11-29 2021-11-29 Memory management method and device based on remote direct memory access

Country Status (1)

Country Link
CN (1) CN114153785B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117033270B (en) * 2023-10-08 2024-01-26 腾讯科技(深圳)有限公司 Chip, device and data processing method
CN117311997B (en) * 2023-11-29 2024-02-23 苏州元脑智能科技有限公司 Memory management method and device of computing chip and computing chip

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106873915A (en) * 2017-02-22 2017-06-20 郑州云海信息技术有限公司 A kind of data transmission method and device based on RDMA registers memory blocks
CN110674052A (en) * 2019-09-30 2020-01-10 广州虎牙科技有限公司 Memory management method, server and readable storage medium

Family Cites Families (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7290038B2 (en) * 2002-07-31 2007-10-30 Sun Microsystems, Inc. Key reuse for RDMA virtual address space
US7849272B2 (en) * 2007-12-17 2010-12-07 International Business Machines Corporation Dynamic memory management in an RDMA context
US9594707B2 (en) * 2014-09-02 2017-03-14 Unisys Corporation Data input/output (I/O) handling for computer network communications links
US9519666B2 (en) * 2014-11-27 2016-12-13 E8 Storage Systems Ltd. Snapshots and thin-provisioning in distributed storage over shared storage devices
CN110535811B (en) * 2018-05-25 2022-03-04 中兴通讯股份有限公司 Remote memory management method and system, server, client and storage medium

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106873915A (en) * 2017-02-22 2017-06-20 郑州云海信息技术有限公司 A kind of data transmission method and device based on RDMA registers memory blocks
CN110674052A (en) * 2019-09-30 2020-01-10 广州虎牙科技有限公司 Memory management method, server and readable storage medium

Also Published As

Publication number Publication date
CN114153785A (en) 2022-03-08

Similar Documents

Publication Publication Date Title
CN114153785B (en) Memory management method and device based on remote direct memory access
US7827375B2 (en) Defensive heap memory management
CN106294190B (en) Storage space management method and device
US20040221120A1 (en) Defensive heap memory management
US11372681B2 (en) Slab memory allocator with dynamic buffer resizing
JP6501916B2 (en) Method and system for performing indexing in file storage
CN106445835B (en) Memory allocation method and device
CN110188110B (en) Method and device for constructing distributed lock
US10515078B2 (en) Database management apparatus, database management method, and storage medium
US20080133467A1 (en) Data structure supporting random delete and timer function
CN105988876A (en) Memory allocation method and apparatus
CN110334069B (en) Data sharing method among multiple processes and related device
CN110727517A (en) Memory allocation method and device based on partition design
US7849272B2 (en) Dynamic memory management in an RDMA context
JP5969122B2 (en) Host bus adapter and system
CN112231101A (en) Memory allocation method and device and readable storage medium
CN112306383A (en) Method for executing operation, computing node, management node and computing equipment
CN110162395B (en) Memory allocation method and device
WO2019174206A1 (en) Data reading method and apparatus of storage device, terminal device, and storage medium
CN110445580B (en) Data transmission method and device, storage medium, and electronic device
US9697048B2 (en) Non-uniform memory access (NUMA) database management system
CN111694806A (en) Transaction log caching method, device, equipment and storage medium
WO2017095367A1 (en) Managing objects stored in memory
CN115599542A (en) Method and system for realizing shared memory pool
US10824640B1 (en) Framework for scheduling concurrent replication cycles

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant