Shared memory application method, device, equipment and computer program product
By allocating independent shared memory and pre-configured storage space in shared memory management, the problem of reduced efficiency caused by thread competition in the prior art is solved, and higher processing efficiency is achieved.
Patent Information
- Application Number
- CN202411154775.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-22
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2044-08-22
AI Technical Summary
In the prior art, when using the locking mechanism to manage shared memory, threads will fall into kernel-state waiting due to multiple threads accessing the underlying resources of the system, resulting in reduced efficiency.
A shared memory application method is provided. By obtaining the communication data size of the thread and the allocated shared memory size, if it is insufficient, the pre-configured storage space sizes are obtained, and the corresponding storage space is allocated according to the communication data size and storage space size to avoid performance losses caused by competition conflicts.
By allocating independent shared memory and pre-configured storage space, the performance loss caused by thread competition is solved and processing efficiency is improved.
Smart Images

Figure CN118672808B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of memory management technology, and in particular to a shared memory application method, apparatus, device and computer program product. Background Art
[0002] Currently, using shared memory is the fastest way to transfer data between processes. Generally, a locking mechanism is used to satisfy the shared memory resources used by multiple threads at the same time.
[0003] However, the locking mechanism will cause threads to fall into kernel state waiting due to multiple threads accessing the underlying system resources, thereby reducing efficiency. Summary of the invention
[0004] Based on this, it is necessary to provide a shared memory application method, device, equipment and computer program product that can improve efficiency in response to the above technical problems.
[0005] In a first aspect, the present application provides a shared memory application method, the method comprising:
[0006] Get the size of the thread's communication data;
[0007] In the case that the thread has been allocated an independent shared memory, obtaining the size of the independent shared memory;
[0008] When the size of the independent shared memory is smaller than the size of the communication data, obtaining the size of each type of pre-configured storage space;
[0009] Based on the size of the communication data and the size of each type of storage space, a corresponding type of storage space is allocated to the communication data.
[0010] In one embodiment, after obtaining the size of the communication data of the thread, the method further includes:
[0011] In the case that the thread is not allocated with independent shared memory, applying for independent shared memory for the thread;
[0012] In the case where the independent shared memory application is successful, a cross-process synchronization object is allocated to the thread, and the step of obtaining the size of the independent shared memory is continued.
[0013] In one embodiment, the method further comprises:
[0014] When the size of the independent shared memory is greater than or equal to the size of the communication data, the communication data is written into the independent shared memory and the cross-process synchronization object is released, wherein the cross-process synchronization object is used to notify the recipient of the communication data to access the independent shared memory to obtain the communication data.
[0015] In one embodiment, the method further comprises:
[0016] When the thread ends, the independent shared memory that has been applied for is released.
[0017] In one of the embodiments, the type of storage space includes at least one of a block-managed shared memory area, a locked-transfer shared memory area, a ring-buffered shared memory area, and a file transfer space that exceeds the shared memory limit.
[0018] In one embodiment, each type of storage space has a priority, and the priority of each type of storage space is from high to low: a block-managed shared memory area, a locked transmission shared memory area, a ring buffer shared memory area, and a file transfer space that exceeds the shared memory limit; the corresponding type of storage space is allocated to the communication data based on the size of the communication data and the size of each type of storage space, including:
[0019] According to the priority level, space is applied to the corresponding type storage space in sequence to allocate the corresponding type storage space to the communication data.
[0020] In one embodiment, allocating corresponding type storage space to the communication data based on the size of the communication data and the size of each type storage space includes:
[0021] When the size of the shared memory area managed by the block is greater than or equal to the size based on the communication data, querying a writable block list corresponding to the shared memory area managed by the block;
[0022] When it is determined based on the writable block list that a writable block exists in the shared memory area managed by the block, the writable block is used as the shared memory corresponding to the communication data.
[0023] In one embodiment, before querying the writable block list corresponding to the shared memory area of the block management, the method further includes:
[0024] Determine the thread currently applying for a writable block from the shared memory area managed by the block, and assign a corresponding first queue identifier to each of the threads by atomic counting;
[0025] When it is determined to start allocating the block-managed shared memory area to the thread based on the first queue identifier of the thread, the step of querying a writable block list corresponding to the block-managed shared memory area is continued.
[0026] In one embodiment, the method further comprises:
[0027] In a case where it is determined based on the writable block list that the shared memory area managed by the block does not have a writable block, incrementing the query count corresponding to the thread;
[0028] Before querying the writable block list corresponding to the shared memory area managed by the block, the method further includes:
[0029] When the query number is less than or equal to the query number threshold, continue to execute the step of querying the writable block list corresponding to the shared memory area of the block management;
[0030] When the query times are greater than the query times threshold, applying for the type storage space corresponding to the communication data from the next type storage space.
[0031] In one embodiment, each storage block in the shared memory area managed by the block includes a status flag; the method further includes:
[0032] When it is determined based on the writable block list that there are no writable blocks in the shared memory area managed by the block, the writable block list is refreshed based on the status flag of each of the storage blocks.
[0033] In one embodiment, allocating corresponding storage space to the communication data based on the size of the communication data and the size of each type of storage space includes:
[0034] When the size of the ring buffer shared memory area is greater than or equal to the size of the communication data, the communication data is sent to a background sending thread in the form of a task, and the background sending thread writes the communication data into the ring buffer shared memory area.
[0035] In one embodiment, before sending the communication data to the background sending thread in the form of a task, the method further includes:
[0036] Allocating a corresponding second queue identifier to each of the threads by means of atomic counting;
[0037] In the case where it is determined to start allocating the ring buffer shared memory area for the thread based on the second queue identifier of the thread, the step of sending the communication data to the background sending thread in the form of a task is continued.
[0038] In one embodiment, the method further comprises:
[0039] Acquire storage information of the communication data, and store the storage information in the independent shared memory, wherein the storage information is used to instruct a receiver of the communication data to determine a storage location of the communication data.
[0040] In a second aspect, the present application further provides a shared memory application device, the device comprising:
[0041] A communication data size acquisition module is used to obtain the size of the communication data of the thread;
[0042] A first allocation module, configured to obtain a size of the independent shared memory when the independent shared memory has been allocated to the thread;
[0043] A storage space size acquisition module, used to acquire the size of each type of pre-configured storage space when the size of the independent shared memory is smaller than the size of the communication data;
[0044] The second allocation module is used to allocate a corresponding type of storage space to the communication data based on the size of the communication data and the size of each type of storage space.
[0045] In a third aspect, the present application also provides a shared memory application device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the method in any one of the above-mentioned embodiments when executing the computer program.
[0046] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the steps of the method in any one of the above-mentioned embodiments.
[0047] In a fifth aspect, the present application also provides a computer program product, including a computer program, which implements the steps of the method in any one of the above-mentioned embodiments when executed by a processor.
[0048] The above-mentioned shared memory application method, device, equipment and computer program product obtain the size of the communication data of the thread; when the thread has been allocated an independent shared memory, the size of the independent shared memory is obtained, so that each thread initiating communication has an independent memory area, and therefore no thread competition will be caused during operation, and when the size of the independent shared memory is smaller than the size of the communication data, the size of each type of pre-configured storage space is obtained; based on the size of the communication data and the size of each type of storage space, the corresponding type of storage space is allocated to the communication data, which can solve the performance loss caused by competition conflicts and improve processing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the drawings required for use in the embodiments of the present application or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0050] Figure 1 for the intention of the competitive process in traditional technologies;
[0051] Figure 2 A schematic diagram of a cache miss when using memory in conventional technology;
[0052] Figure 3 A schematic diagram showing a significant deterioration in memory application contention in conventional technology;
[0053] Figure 4 An application environment diagram of a shared memory application method in one embodiment;
[0054] Figure 5 A schematic diagram of a shared memory application method in one embodiment;
[0055] Figure 6 is a schematic diagram of a multi-level storage space in one embodiment;
[0056] Figure 7 is a flow chart of a shared memory application method in yet another embodiment;
[0057] Figure 8 A flowchart of an independent shared memory in one embodiment;
[0058] Fig. 9 is a schematic diagram of an independent shared memory in one embodiment;
[0059] Fig.10 A schematic diagram of a block-managed shared memory area in one embodiment;
[0060] Fig.11 A flowchart of a block-managed shared memory zone application in one embodiment;
[0061] Fig.12 A flowchart of a shared memory area application for locked transmission in one embodiment;
[0062] Fig.13 A flowchart of a ring buffer shared memory area application in one embodiment;
[0063] Fig.14 A flowchart for requesting file transfer space that exceeds the shared memory limit in one embodiment;
[0064] Fig.15 A structural block diagram of a shared memory application device in one embodiment;
[0065] Fig.16 A diagram of the internal structure of a shared memory application device in one embodiment. DETAILED DESCRIPTION
[0066] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0067] Please combine Figure 1 As shown, Figure 1 For the purpose of the competition process in the traditional technology, in this embodiment, thread 1 of process A (i.e., the sender process) communicates with thread 1 corresponding to process B (i.e., the receiver process) through a kernel object (the cross-process synchronization object mentioned above), and thread 2 of process A communicates with thread 2 corresponding to process B through a kernel object. Since the traditional technology adopts a locking mechanism, when thread 1 applies for shared memory, thread 2 is in a kernel waiting state, resulting in reduced thread processing efficiency.
[0068] Please combine Figure 2 As shown, Figure 2 Schematic diagram of cache misses when using memory in traditional technology. In this embodiment, different threads read and write the same memory area, resulting in a large number of kernel-specific cache (L1 / L2 cache) misses, and there is a cache consistency problem.
[0069] Please combine Figure 3 As shown, Figure 3 This is a diagram showing the significant deterioration of memory application contention in traditional technologies; bookkeeping refers to the process of recording information such as the user (process ID and thread ID) and size of shared memory applications. There is bookkeeping contention overhead in the multi-threaded application process. As the number of bookkeeping objects increases, the search efficiency will decrease. Memory allocation algorithms, memory fragmentation, etc. will also lead to efficiency loss. This loss is usually tens to hundreds of microseconds, and is significantly worse under conditions of competition or frequent fragmented memory applications.
[0070] Therefore, improving the concurrent performance of shared memory and optimizing the hit rate of a single CPU dedicated cache (L1 / L2 cache) are key technical issues in the design of shared memory efficiency.
[0071] The shared memory application method provided in the embodiment of the present application can be applied to Figure 4In the application environment shown. Each thread of process A communicates with the thread corresponding to process B, that is, each thread that initiates a communication request corresponds to a thread that receives the communication request. The basis for the communication between each thread of process A and the thread corresponding to process B is cross-process synchronization objects and shared memory. The shared memory includes various layers of storage space, that is, the communication between the threads of the two processes depends on cross-process synchronization objects and shared memory for read and write synchronization. Storage space 1, storage space 2, and storage space N all contain multiple layers of storage space, that is, the 5 layers of storage space involved in this application.
[0072] Optionally, the communication between each thread of process A and the corresponding thread of process B may be applied in a remote procedure call framework (RPC), but the shared memory application method in the present application is not limited to the remote procedure call framework.
[0073] Memory pool is a commonly used memory management technology, and shared memory itself is a memory pool. A memory pool of reasonable size can make full use of the spatial locality of the cache, and repeated use can make use of the temporal locality of the cache. Therefore, based on the memory pool and layering technology, the present application refines the design details of each layer and improves the synchronization mechanism for access to shared memory between processes. Specifically, each layer of storage space includes at least an independent shared memory and various types of pre-configured storage spaces. The various types of pre-configured storage spaces include at least one of a block-managed shared memory area, a locked transmission shared memory area, a ring buffer shared memory area, and a file transfer space that exceeds the shared memory limit.
[0074] Optionally, the five layers of storage space involved will be introduced in detail in this application, and taking into account the size of the storage space and the IO read and write pressure, the five layers of storage space have priorities, among which the priority of the independent shared memory is greater than the priority of the shared memory area including block management, the priority of the shared memory area including block management is greater than the priority of the shared memory area for locked transmission, the priority of the shared memory area for locked transmission is greater than the priority of the ring buffer shared memory area, and the priority of the ring buffer shared memory area is greater than the priority of the file transfer space that exceeds the shared memory limit.
[0075] Thus, before communication, threads initiate space application requests to the corresponding storage space in descending order of priority until they obtain storage space that meets the requirements. For example, first initiate an application request to the first level. If the application request is not met, then the application request is raised to the next level until it is passed to a level that can be processed and resolved.
[0076] The above five levels of shared memory application methods provide a systematic solution for improving the performance of modern computer multi-threaded programs when using shared memory, and can effectively solve the performance loss problem caused by competition conflicts.
[0077] In an exemplary embodiment, Figure 5 As shown, a shared memory application method is provided, which is applied to Figure 1 The process A (i.e., the sending process) in the example is used for explanation, and the steps include the following steps 502 to 506. Among them:
[0078] S502: Obtain the size of the thread's communication data.
[0079] When initiating communication, the threads in a process can determine the size of the communication data. For example, if thread 1 in process A needs to send data M to thread 2 in process B, thread 1 can first obtain the size of data M for subsequent allocation of shared memory space.
[0080] S504: When an independent shared memory has been allocated to the thread, the size of the independent shared memory is obtained.
[0081] An independent shared memory is allocated by default for each thread that initiates communication. Each thread that initiates communication has an independent memory area, so it will not cause thread competition during runtime.
[0082] Optionally, the size of the independent shared memory can be dynamically adjusted by the user, but it is recommended to be designed not to exceed 4KB. Therefore, when initiating a communication request, if the thread has allocated an independent shared memory, the size of the independent shared memory is obtained, and the size can be pre-stored so that the program can directly obtain it.
[0083] When a thread initiates a communication request, the independent shared memory is allocated to the thread, and a cross-process synchronization object is provided for the thread to solve the synchronization problem between processes and perform a shared memory bookkeeping. If the thread does not initiate communication, the user will not apply for thread-independent shared memory when using the thread to complete other tasks.
[0084] S506: When the size of the independent shared memory is smaller than the size of the communication data, the sizes of pre-configured storage spaces of various types are obtained.
[0085] S508: Allocate corresponding type storage space to the communication data based on the size of the communication data and the size of each type of storage space.
[0086] When the size of the independent shared memory is smaller than the size of the communication data, that is, when the space of the independent shared memory cannot store the communication data, the sizes of various types of pre-configured storage spaces may be obtained.
[0087] Each type of storage space may include at least one of a block-managed shared memory area, a locked transmission shared memory area, a ring buffer shared memory area, and a file transmission space that exceeds the shared memory limit, and each type of storage space has a designed size.
[0088] Optionally, combined Figure 6 As shown, the size of the shared memory area for block management is between 4KB and 1M. The size of the shared memory area for locked transmission is between 1M and 128M, the size of the ring buffer shared memory area is between 128M and 1G, and the size of the file transmission space beyond the shared memory limit is greater than 1G. In other embodiments, the size of each type of storage space mentioned above can also be set to other values, which are not specifically limited here.
[0089] The thread can obtain the size of each type of storage space, and then allocate storage space for the thread. Optionally, each type of storage space has a priority, and the priority of each type of storage space is from high to low: block-managed shared memory area, locked transmission shared memory area, ring buffer shared memory area, and file transfer space that exceeds the shared memory limit; based on the size of the communication data and the size of each type of storage space, the corresponding type of storage space is allocated to the communication data, including: applying for space from the corresponding type of storage space in turn according to the priority, so as to allocate the corresponding type of storage space for the communication data.
[0090] Specifically, combined Figure 7 As shown, Figure 7The present invention is a flowchart of a shared memory application method in another embodiment. In this embodiment, after a thread is started and a communication request is initiated, it is checked whether there is an allocated independent shared memory for the thread. If not, an independent shared memory is allocated for the thread. If it is allocated, it is checked whether the size of the allocated independent shared memory is greater than or equal to the size of the communication data. If so, the independent shared memory is used as the storage space for the communication data. Otherwise, the size of the block-managed shared memory is continued to be obtained, and it is checked whether the size of the block-managed shared memory is greater than or equal to the size of the communication data. If so, the block-managed shared memory is used as the storage space for the communication data. Otherwise, the size of the locked transmission shared memory area is continued to be obtained, and it is checked whether the size of the locked transmission shared memory area is greater than or equal to the size of the communication data. If so, the locked transmission shared memory area is used as the storage space for the communication data. Otherwise, the size of the ring buffer shared memory area is continued to be obtained, and it is checked whether the size of the ring buffer shared memory area is greater than or equal to the size of the communication data. If so, the ring buffer shared memory area is used as the storage space for the communication data. Otherwise, the file transfer space exceeding the shared memory limit is used as the storage space for the communication data.
[0091] in, Figure 7 Only one optional embodiment is given in the above. In other embodiments, each type of storage space may include the above 4, 3, 2 or 1 types, and the priority of each type of storage space may also be configured based on needs, and is not limited to Figure 6 The priority is shown in .
[0092] In the above embodiment, an independent shared memory is allocated to each thread in advance, so that thread competition will not occur during runtime. When the size of the independent shared memory is smaller than the size of the communication data, the size of each type of pre-configured storage space is obtained, and based on the size of the communication data and the size of each type of storage space, the corresponding type of storage space is allocated to the communication data. This can effectively solve the performance loss problem caused by competition conflicts.
[0093] In one of the optional embodiments, after obtaining the size of the thread's communication data, it also includes: if the thread has not allocated independent shared memory, applying for independent shared memory for the thread; if the independent shared memory application is successful, allocating a cross-process synchronization object for the thread, and continuing to execute the step of obtaining the size of the independent shared memory.
[0094] Wherein, in the case where the thread is not allocated with independent shared memory, an independent shared memory is applied for the thread. Specifically, when the process is started, the process is initialized to obtain a target number of independent shared memories to be allocated to the thread corresponding to the process, wherein the independent shared memory can be managed by a queue, and the index of the independent shared memory is stored in the queue. When the thread in the process initiates an inter-process communication request, it is determined whether the thread is allocated with independent shared memory. If not, an independent shared memory is allocated to the thread, for example, an unallocated index is obtained from the queue, and the independent shared memory corresponding to the index is allocated to the thread. In some optional embodiments, if there is no independent shared memory to be allocated, an exception handling process is performed.
[0095] In the case where the thread applies for an independent shared memory, it may be further determined whether the size of the independent shared memory is greater than or equal to the size of the communication data, so as to allocate storage space for the communication data of the thread.
[0096] In some of the optional embodiments, the method also includes: when the size of the independent shared memory is greater than or equal to the size of the communication data, writing the communication data into the independent shared memory and releasing the cross-process synchronization object, wherein the cross-process synchronization object is used to notify the recipient of the communication data to access the independent shared memory to obtain the communication data.
[0097] When the size of the independent shared memory is greater than or equal to the size of the communication data, the communication data is written into the independent shared memory to complete the allocation of storage space for the communication data. In addition, in order to achieve cross-process communication, after the thread completes writing the communication data, it releases the cross-process synchronization object for data synchronization. After receiving the synchronization data signal, the communication receiver (the thread in another process) accesses the independent shared memory to obtain the communication data.
[0098] In one of the optional embodiments, the method further includes: releasing the applied independent shared memory when the thread ends.
[0099] Since the thread-independent shared memory does not follow the communication process, the thread-independent shared memory does not need to be released after the actual communication use ends. In fact, the release function for the thread-independent shared memory is registered in the thread cleanup callback function list. When the thread completes all the calling tasks and ends, that is, when the thread ends, the thread-independent cache resources will be cleaned up and the memory account book will be cleaned up. At this time, the resources are released for other threads to use. The thread-independent shared memory allocation is accompanied by the binding of a cross-process synchronization object, which is used for data consistency synchronization between processes. That is, each independent shared memory corresponds to a cross-process synchronization object to facilitate communication between processes.
[0100] Optionally, after a thread communication ends, there is no need to release independent shared memory resources. When communication is initiated again, since independent shared memory has been allocated, independent shared memory and cross-process synchronization objects can be used for communication again without the need to allocate independent shared memory. When all communications are completed and thread resources are cleaned up, the shared memory bookkeeping will be called again to return the independent shared memory, such as returning the index of the independent shared memory, thereby achieving the purpose of releasing ownership to the process.
[0101] For ease of understanding, combined Figure 8 As shown, Figure 8 The present invention is a workflow diagram of an independent shared memory in an embodiment. In this embodiment, after a thread is started and a communication request is initiated, it is determined whether the thread has been allocated an independent shared memory. If an independent shared memory has been allocated, it is checked whether the size of the independent shared memory is larger than the size of the communication data. If so, the address of the independent shared memory is returned, and a cross-process synchronization object is allocated to the thread. After the communication data is stored in the independent shared memory, other to-do items are processed to complete the communication, and it is continued to detect whether there is still a communication request. If so, the above steps are continued. Otherwise, after waiting for the thread to end, the bookkeeping is locked, and after the allocated shared memory and the cross-process synchronization object are returned, the bookkeeping is unlocked to complete the thread processing.
[0102] If there is no allocated independent shared memory, the bookkeeping is locked and whether there is unallocated independent shared memory is detected. If so, independent shared memory and cross-process synchronization objects are allocated to the thread. After the bookkeeping is unlocked, the step of checking whether the size of the independent shared memory is greater than the size of the communication data is continued. If there is no unallocated independent shared memory, the exception handling process is performed.
[0103] In the above embodiment, combined with Fig. 9 As shown, since the size of the independent shared memory is much smaller than the dedicated high-speed cache of a single CPU core of a modern computer, the temporal and spatial locality of the cache is fully utilized during frequent communications, resulting in better data reading and writing performance.
[0104] In one of the optional embodiments, based on the size of the communication data and the size of each type of storage space, a corresponding type of storage space is allocated to the communication data, including: when the size of the block-managed shared memory area is greater than or equal to the size based on the communication data, querying a writable block list corresponding to the block-managed shared memory area; when it is determined based on the writable block list that there are writable blocks in the block-managed shared memory area, using the writable blocks as the shared memory corresponding to the communication data.
[0105] Among them, after the size of the independent shared memory is smaller than the size of the communication data, other storage space needs to be allocated to the thread. This embodiment mainly introduces the allocation of block-managed shared memory areas. Among them, if the size of the block-managed shared memory area is greater than or equal to the size based on the communication data, the block-managed shared memory area can be allocated to the thread.
[0106] The block-managed shared memory is mainly managed through a writable block list. When it is determined based on the writable block list that a writable block exists in the block-managed shared memory area, the writable block is used as the shared memory corresponding to the communication data.
[0107] Specifically, when the amount of data transmitted exceeds 4K, the cost of allocating independent shared memory to each thread may be very expensive. On the one hand, the shared memory used for communication is very limited, and it is impossible to independently allocate a larger shared memory area for each thread. On the other hand, when a computer transmits more data, the efficiency bottleneck is on IO rather than CPU, and it is necessary to make full use of the CPU to reduce IO pressure. Therefore, when transmitting data between 4K and 1M, this application equips each thread with a block-managed shared memory area, which is shared by threads and dynamically allocated. The number of blocks is equal to the number of logical cores of the currently running computer. Contention may occur during the bookkeeping process, because dynamic allocation is only initiated when the amount of data exceeds the limit of 4KB, and the time cost of the allocated thread using the memory is much greater than the shared memory bookkeeping time, so contention is usually weak or does not occur.
[0108] In order to manage each block, the block-managed memory area introduces a writable block list, which is an unused block. Therefore, in this application, when confirming that the block-managed memory area is allocated to a thread, the writable block list is read, and then when it is determined based on the writable block list that there is a writable block in the shared memory area of the block management, the writable block is used as the shared memory corresponding to the communication data. Since there may be multiple writable blocks in the writable block list, one writable block can be obtained in sequence as the shared memory corresponding to the communication data. Other writable blocks can be used by other threads to improve efficiency.
[0109] In one of the optional embodiments, each storage block in the block-managed shared memory area includes a status flag; the method also includes: when it is determined based on the writable block list that there is no writable block in the block-managed shared memory area, refreshing the writable block list based on the status flag of each storage block.
[0110] Among them, combined Fig.10As shown, the block-managed shared memory area is occupied by process A that initiates the communication and released by process B, the receiving party of the communication. Therefore, the first 8 bytes in each block are designed as status flags, marked as used (readable) and unused (writable). In order to avoid performance problems caused by the status flags being queried and modified by multiple threads, the buffer queue is introduced in this application to solve this problem. Specifically, when there are multiple threads applying for the block-managed shared memory area, the application requests of each thread can be cached in the buffer queue, so that the requests of each thread are processed in sequence according to the order of the application requests of each thread in the buffer queue, avoiding performance problems caused by the status flags being queried and modified by multiple threads.
[0111] When the application request of one of the threads is being processed, if the number of available blocks (writable blocks, unused blocks) is insufficient, the current status of all blocks can be refreshed at one time and the list of writable blocks can be updated. This can reduce the competition caused by concurrent application processes for atomic status queries and avoid introducing performance problems.
[0112] In one of the optional embodiments, before querying the writable block list corresponding to the block-managed shared memory area, it also includes: determining the thread that currently applies for the writable block to the block-managed shared memory area, and assigning a corresponding first queue identifier to each thread through atomic counting; when it is determined to start allocating the block-managed shared memory area to the thread based on the first queue identifier of the thread, continue to execute the step of querying the writable block list corresponding to the block-managed shared memory area.
[0113] Among them, when there are multiple threads applying for block-managed shared memory areas, the corresponding first queue identifier can be assigned to each thread through atomic counting, and the application requests of each thread can be cached in the buffer queue. The application requests of the threads in the buffer queue are processed in sequence based on the first queue identifier to allocate block-managed shared memory areas to the threads. This can reduce the competition state of the application process and avoid introducing performance problems. Since the buffer queue is introduced, the memory management bookkeeping process at this level adopts a lock-free design, and the queue number is obtained through atomic counting when there is a concurrency conflict.
[0114] In one of the optional embodiments, the method also includes: when it is determined based on the writable block list that there are no writable blocks in the shared memory area managed by the block, increasing the query count corresponding to the thread; before querying the writable block list corresponding to the shared memory area managed by the block, it also includes: when the query count is less than or equal to the query count threshold, continuing to execute the step of querying the writable block list corresponding to the shared memory area managed by the block; when the query count is greater than the query count threshold, applying for the type storage space corresponding to the communication data to the next type storage space.
[0115] The query times threshold is preset. If after multiple attempts, it is still not confirmed that there is no writable block in the block-managed shared memory area, then the type storage space corresponding to the communication data can be applied to the next type storage space, otherwise the ownership of the corresponding writable block can be obtained to transmit the communication data through the writable block with the ownership obtained. In this way, the ownership of the bookkeeping process is obtained through a limited number of spins, and the communication data with a size smaller than the size of the block-managed shared memory area can be placed in the block-managed shared memory area for communication as much as possible, avoiding communication through a larger type of storage space to save storage space.
[0116] For ease of understanding, combined Fig.11 As shown, Fig.11 The present invention is a flowchart of a block-managed shared memory area application in an embodiment. In this embodiment, after a thread is started and a communication request is initiated, it is determined whether the thread has been allocated an independent shared memory. If an independent shared memory has been allocated, it is checked whether the size of the independent shared memory is larger than the size of the communication data. If not, the size of the block-managed shared memory area is obtained, and it is checked whether the size of the block-managed shared memory area is larger than the size of the communication data. If so, the application request of the thread is cached in a buffer queue and a first queue identifier is assigned to it. When it is determined based on the first queue identifier that a block-managed shared memory area is to be allocated to the thread, the thread obtains bookkeeping ownership, and a query number is assigned to the thread. It is determined whether the query number exceeds a query number threshold. If not, a writable block list is queried to determine whether a writable block exists. If no writable block exists, the writable block list is refreshed, the query number is incremented, and the step of determining whether the query number exceeds a query number threshold is continued. If there is a writable block, the storage address or writable block index of the writable block is returned to use the writable block as shared memory for communication data, and then other to-do items are processed to complete the communication, and continue to detect whether there is still a communication request. If so, continue the above steps, otherwise wait for the thread to end, return the allocated shared memory and cross-process synchronization objects.
[0117] If the size of the shared memory area managed by the block is smaller than the size of the communication data, you can continue to apply to storage spaces at other levels.
[0118] Among them, combined Fig.12 As shown, Fig.12This is a flowchart of a shared memory area application for locked transmission in an embodiment. In this embodiment, when the shared memory area managed by the block fails to meet the application conditions, the next level of memory application is continued. There are usually two conditions that cannot meet the application requirements. The first is the bookkeeping times that exceed the query times threshold of the current running computer. The second is that the data transmitted at a time is larger than the single limit size of 1MB of the shared memory area managed by the block. At this time, too many blocks usually lead to reduced utilization and increased costs. The above two levels solve the problem of fragmentation caused by small data and avoid defragmentation. Since this area needs to meet the concurrency of threads and processes, the bookkeeping process of memory usage is generally implemented in the form of locking during design. And use unique descriptors (or named objects) to book different applications. This will inevitably bring about switching between user mode and kernel mode and waiting overhead, but this overhead is not an efficiency bottleneck problem for data operation time greater than 1M. This is in line with the general design idea and is supported by open source design. In this application, the third-party library Boost can be used to implement the shared memory area for locked transmission of such large data transmission, and it is set to 128M. Those skilled in the art may know that in other embodiments, other methods may be used to implement the shared memory area for locked transmission, and the size is not limited to 128M, or even if the third-party library Boost is used to implement it, the size is not limited to 128M. This embodiment only provides an optional implementation method.
[0119] Combination Fig.12 In the process, after the thread is started and a communication request is initiated, it is determined whether the thread has been allocated an independent shared memory. If an independent shared memory has been allocated, it is checked whether the size of the independent shared memory is greater than the size of the communication data. If not, the size of the shared memory area managed by the block is obtained, and the size of the shared memory area managed by the block is greater than the size of the communication data. If not, it is checked whether the size of the shared memory transmitted by the lock is greater than the size of the communication data. If yes, it is locked, and it is confirmed whether there is a storage fragment of a size that meets the requirements. If there is, it is booked and the lock is released, and the storage address of the storage fragment is returned to store the communication data in the storage fragment. If there is no storage fragment of a size that meets the requirements, an available memory area is obtained. If the size of the available memory area is greater than or equal to the size of the communication data, the storage address of the available memory area is returned by bookkeeping to store the communication data in the available memory area. If the size of the available memory area is less than the size of the communication data, it enters the waiting queue, releases the lock, and waits for the thread to wake up again, and then continues to execute the locking step. After the shared memory area with a new lock transmission is released, the waiting thread can be awakened.
[0120] Subsequently, after a shared memory area with locked transmission is allocated to the thread and the communication data is written into the shared memory area with locked transmission, other to-do items are processed to complete the communication, and the process continues to detect whether there is still a communication request. If so, continue the above steps. Otherwise, wait for the thread to end, return the allocated shared memory and cross-process synchronization objects.
[0121] In one of the optional embodiments, corresponding storage space is allocated to the communication data based on the size of the communication data and the size of each type of storage space, including: when the size of the ring buffer shared memory area is greater than or equal to the size of the communication data, the communication data is sent to the background sending thread in the form of a task, and the background sending thread writes the communication data to the ring buffer shared memory area.
[0122] When the independent shared memory, block-managed shared memory, or locked transmission shared memory allocated by the communication thread cannot meet the data transmission needs of this communication, and the amount of communication data is less than the size of the ring buffer shared memory area, for example, less than 1GB, the thread needs to use the ring buffer shared memory area for data transmission. The thread passes the communication data to the background sending thread in the form of a task. The background sending thread writes the data into the ring buffer shared memory area in sequence by unpacking and sending out of order. The background receiving thread at the receiving end splices the data through the packet header protocol.
[0123] Specifically, the available shared memory is still very limited, and the process memory available within the process is still an order of magnitude more than the shared memory. When transmitting very large data at a time, a piece of shared memory is selected as a ring buffer shared memory area, and the reading process reads from one end in a loop, and the writing process writes from one end in a loop. The size of the ring buffer shared memory area can be 128M (it can be other sizes in other embodiments), and it is divided into 16 (it can be divided into other numbers in other embodiments, which are not specifically limited here) first-connected ring buffer queues to facilitate reading and writing at the same time. The total amount of data transmission is limited to 1GB. The ring buffer supports out-of-order sending and a packetization mechanism to achieve priority return of smaller communication data during concurrency.
[0124] In one of the optional embodiments, before sending the communication data in the form of a task to the background sending thread, it also includes: allocating a corresponding second queue identifier to each thread by means of atomic counting; when it is determined to start allocating a ring buffer shared memory area to the thread based on the second queue identifier of the thread, continuing to execute the step of sending the communication data in the form of a task to the background sending thread.
[0125] Among them, in order to avoid conflicts between threads, a corresponding second queue identifier is allocated to each thread by means of atomic counting, and the second queue identifier is cached in a cache queue. When it is determined to start allocating a ring buffer shared memory area to the thread based on the second queue identifier of the thread, the application in the cache queue is executed, that is, the communication data is sent to the background sending thread in the form of a task.
[0126] For ease of understanding, combined Fig.13 As shown, Fig.13 The present invention is a flowchart of a circular buffer shared memory area application in an embodiment. After a thread is started and a communication request is initiated, it is determined whether the thread has been allocated an independent shared memory. If an independent shared memory has been allocated, it is checked whether the size of the independent shared memory is larger than the size of the communication data. If not, the size of the block-managed shared memory area is obtained, and it is checked whether the size of the block-managed shared memory area is larger than the size of the communication data. If not, it is checked whether the size of the locked transmission shared memory is larger than the size of the communication data. If not, it is checked whether the size of the circular buffer shared memory area is smaller than the size of the communication data. If not, the request of the thread is cached in a cache queue, and a corresponding second queue identifier is allocated to each thread by atomic counting. When it is determined to start allocating a circular buffer shared memory area for the thread based on the second queue identifier of the thread, the communication data is sent to the sending queue, the background sending thread obtains the sending task in the sending queue, unpacks and writes it into the circular shared storage area, and the background receiving thread reads the assembled package in the circular shared storage area after confirming that there is a receiving task to complete the sending of the communication data.
[0127] Subsequently, other to-do items are processed to complete the communication, and the process continues to detect whether there is still a communication request. If so, the above steps are continued. Otherwise, after waiting for the thread to end, the allocated shared memory and cross-process synchronization objects are returned.
[0128] In one of the optional embodiments, in combination Fig.13 As shown, Fig.13 The present invention is a flowchart of a circular buffer shared memory area application in an embodiment. After a thread is started and a communication request is initiated, it is determined whether the thread has been allocated an independent shared memory. If an independent shared memory has been allocated, it is checked whether the size of the independent shared memory is larger than the size of the communication data. If not, the size of the block-managed shared memory area is obtained, and it is checked whether the size of the block-managed shared memory area is larger than the size of the communication data. If not, it is checked whether the size of the locked transmission shared memory is larger than the size of the communication data. If not, it is checked whether the size of the circular buffer shared memory area is larger than the size of the communication data. If not, the communication data is transmitted through a file transfer space that exceeds the shared memory limit.
[0129] When the data to be transmitted exceeds 1GB, it is necessary to balance the efficiency of concurrent hard disk transmission and the efficiency of the ring buffer, and IO becomes a bottleneck. Therefore, the fifth-layer cross-process data communication design chooses to use files for cross-process data transmission to meet the pressure requirements in extreme scenarios. The implementation of this solution relies on the atomic counting strategy to achieve concurrent isolation.
[0130] Specifically, combined Fig.14 As shown, an atomic count is obtained, and the file is created and mapped using atomic technology, and then the address of the file is returned for communication data transmission. Subsequently, other to-do items are processed to complete the communication, and continue to detect whether there is still a communication request. If so, continue the above steps, otherwise wait for the thread to end, return the allocated shared memory and cross-process synchronization objects.
[0131] In one of the optional embodiments, the method further includes: acquiring storage information of the communication data, and storing the storage information in an independent shared memory, wherein the storage information is used to instruct a receiver of the communication data to determine a storage location of the communication data.
[0132] Among them, the processing of other to-do items mentioned above refers to the processing of the storage information of the communication data here, that is, when using shared memory to transmit data, the receiving end process needs to obtain which memory (or unique identifier) at which level is designated as the area for storing communication data. In this embodiment, the storage information of the communication data is stored in the independent shared memory of the thread. For example, the storage information of the communication data is stored in the data header area of the independent shared memory of the thread in the form of a data packet header. Optionally, the design size of the data header area is 128 bytes, which contains all the memory management information required by the other end. In other embodiments, the size of the data header area can be other values, which are not specifically limited here.
[0133] The five levels of shared memory described above provide a systematic solution for improving the performance of modern computer multi-threaded programs when using shared memory.
[0134] It should be understood that, although the steps in the flowcharts involved in the above embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above embodiments may include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0135] Based on the same inventive concept, the embodiment of the present application also provides a shared memory application device for implementing the shared memory application method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more shared memory application device embodiments provided below can refer to the limitations on the shared memory application method above, and will not be repeated here.
[0136] In an exemplary embodiment, Fig.15 As shown, a shared memory application device is provided, including: a communication data size acquisition module 1501, a first allocation module 1502, a storage space size acquisition module 1503 and a second allocation module 1504, wherein:
[0137] The communication data size acquisition module 1501 is used to acquire the size of the communication data of the thread;
[0138] A first allocation module 1502 is used to obtain the size of the independent shared memory when the thread has been allocated with the independent shared memory;
[0139] The storage space size acquisition module 1503 is used to acquire the size of each type of pre-configured storage space when the size of the independent shared memory is smaller than the size of the communication data;
[0140] The second allocation module 1504 is used to allocate corresponding types of storage space to the communication data based on the size of the communication data and the size of each type of storage space.
[0141] In one of the optional embodiments, the above device further includes:
[0142] The allocation module is used to apply for independent shared memory for the thread when the thread is not allocated with independent shared memory; when the independent shared memory application is successful, a cross-process synchronization object is allocated for the thread, and the step of obtaining the size of the independent shared memory is continued.
[0143] In one of the optional embodiments, the above device further includes:
[0144] The communication module is used to write the communication data into the independent shared memory and release the cross-process synchronization object when the size of the independent shared memory is greater than or equal to the size of the communication data, wherein the cross-process synchronization object is used to notify the receiver of the communication data to access the independent shared memory to obtain the communication data.
[0145] In one of the optional embodiments, the above device further includes:
[0146] The release module is used to release the applied independent shared memory when the thread ends.
[0147] In one of the optional embodiments, the type storage space includes at least one of a block-managed shared memory area, a locked-transfer shared memory area, a ring-buffered shared memory area, and a file transfer space that exceeds a shared memory limit.
[0148] In one of the optional embodiments, each type of storage space has a priority, and the priority of each type of storage space is from high to low: block-managed shared memory area, locked transmission shared memory area, ring buffer shared memory area, and file transfer space that exceeds the shared memory limit; the above-mentioned second allocation module 1504 is also used to apply for space from the corresponding type of storage space in sequence according to the priority, so as to allocate the corresponding type of storage space for communication data.
[0149] In one optional embodiment, the second allocation module 1504 includes:
[0150] The block management unit is used to query the writable block list corresponding to the block-managed shared memory area when the size of the block-managed shared memory area is greater than or equal to the size based on the communication data; when it is determined based on the writable block list that there are writable blocks in the block-managed shared memory area, the writable blocks are used as the shared memory corresponding to the communication data.
[0151] In one of the optional embodiments, the block management unit is also used to determine the thread that is currently applying for a writable block to the block-managed shared memory area, and assign a corresponding first queue identifier to each thread through an atomic counting method; when it is determined to start allocating the block-managed shared memory area to the thread based on the first queue identifier of the thread, the step of querying the writable block list corresponding to the block-managed shared memory area is continued.
[0152] In one of the optional embodiments, the block management unit is also used to, when it is determined based on the writable block list that there are no writable blocks in the shared memory area managed by the block, increment the query count corresponding to the thread; when the query count is less than or equal to the query count threshold, continue to execute the step of querying the writable block list corresponding to the shared memory area managed by the block; when the query count is greater than the query count threshold, apply for the type storage space corresponding to the communication data to the next type storage space.
[0153] In one of the optional embodiments, each storage block in the block-managed shared memory area includes a status flag; the above-mentioned block management unit is also used to refresh the writable block list based on the status flag of each storage block when it is determined based on the writable block list that there is no writable block in the block-managed shared memory area.
[0154] In one optional embodiment, the second allocation module 1504 includes:
[0155] The ring buffer shared memory area management unit is used to send the communication data to the background sending thread in the form of a task when the size of the ring buffer shared memory area is greater than or equal to the size of the communication data, and the background sending thread writes the communication data into the ring buffer shared memory area.
[0156] In one of the optional embodiments, the above-mentioned ring buffer shared memory area management unit is also used to allocate a corresponding second queue identifier for each thread by means of atomic counting; when it is determined to start allocating a ring buffer shared memory area for the thread based on the second queue identifier of the thread, the step of sending the communication data to the background sending thread in the form of a task is continued.
[0157] In one of the optional embodiments, the above device further includes:
[0158] The storage information transmission module is used to obtain the storage information of the communication data and store the storage information in an independent shared memory, wherein the storage information is used to instruct the receiver of the communication data to determine the storage location of the communication data.
[0159] Each module in the above-mentioned shared memory application device can be implemented in whole or in part by software, hardware and their combination. Each module can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory in the computer device in the form of software, so that the processor can call and execute the operations corresponding to each module above.
[0160] In an exemplary embodiment, a shared memory application device is provided. The shared memory application device may be a terminal, and its internal structure diagram may be as follows: Fig.16As shown. The shared memory application device includes a processor, a memory, an input / output interface, a communication interface, a display unit and an input device. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface, the display unit and the input device are connected to the system bus through the input / output interface. The processor of the shared memory application device is used to provide computing and control capabilities. The memory of the shared memory application device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The input / output interface of the shared memory application device is used to exchange information between the processor and an external device. The communication interface of the shared memory application device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a mobile cellular network, near field communication (NFC) or other technologies. When the computer program is executed by the processor, a shared memory application method is implemented. The display unit of the shared memory application device is used to form a visually visible picture, which can be a display screen, a projection device or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen, and the input device of the shared memory application device can be a touch layer covering the display screen, or a button, trackball or touchpad set on the shell of the shared memory application device, or an external keyboard, touchpad or mouse.
[0161] Those skilled in the art will understand that Fig.16 The structure shown in the figure is only a block diagram of a part of the structure related to the present application scheme, and does not constitute a limitation on the shared memory application device to which the present application scheme is applied. The specific shared memory application device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.
[0162] In one embodiment, a computer device is further provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above method embodiments when executing the computer program.
[0163] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments are implemented.
[0164] In one embodiment, a computer program product is provided, including a computer program, which implements the steps in the above method embodiments when executed by a processor.
[0165] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, an artificial intelligence (AI) processor, etc., but are not limited to this.
[0166] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0167] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A shared memory application method, characterized in that: The method comprises: Get the size of the thread's communication data; In the case that the thread has been allocated an independent shared memory, obtaining the size of the independent shared memory; When the size of the independent shared memory is smaller than the size of the communication data, obtaining the size of each type of pre-configured storage space; Allocating corresponding type storage space to the communication data based on the size of the communication data and the size of each type storage space; The type storage space includes at least one of a block-managed shared memory area, a locked transmission shared memory area, a ring buffer shared memory area, and a file transmission space exceeding the shared memory limit; Each type of storage space has a priority, and the priority of each type of storage space is from high to low: block-managed shared memory area, locked transmission shared memory area, ring buffer shared memory area, and file transfer space exceeding the shared memory limit; the corresponding type of storage space is allocated to the communication data based on the size of the communication data and the size of each type of storage space, including: The type storage space of the corresponding level is applied for in descending order of the priority. If the type storage space of the corresponding level is not sufficient, the type storage space of the next level is applied for in descending order of the priority until the corresponding type storage space is allocated for the communication data.
2. The method according to claim 1, characterized in that After obtaining the size of the communication data of the thread, the method further includes: In the case that the thread is not allocated with independent shared memory, applying for independent shared memory for the thread; In the case where the independent shared memory application is successful, a cross-process synchronization object is allocated to the thread, and the step of obtaining the size of the independent shared memory is continued.
3. The method according to claim 2, characterized in that The method further comprises: When the size of the independent shared memory is greater than or equal to the size of the communication data, the communication data is written into the independent shared memory and the cross-process synchronization object is released, wherein the cross-process synchronization object is used to notify the recipient of the communication data to access the independent shared memory to obtain the communication data.
4. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: When the thread ends, the independent shared memory that has been applied for is released.
5. The method according to claim 1, characterized in that The allocating corresponding type storage space to the communication data based on the size of the communication data and the size of each type storage space includes: When the size of the shared memory area managed by the block is greater than or equal to the size based on the communication data, querying a writable block list corresponding to the shared memory area managed by the block; When it is determined based on the writable block list that a writable block exists in the shared memory area managed by the block, the writable block is used as the shared memory corresponding to the communication data.
6. The method according to claim 5, characterized in that Before querying the writable block list corresponding to the shared memory area managed by the block, the method further includes: Determine the thread currently applying for a writable block from the shared memory area managed by the block, and assign a corresponding first queue identifier to each of the threads by atomic counting; When it is determined to start allocating the block-managed shared memory area to the thread based on the first queue identifier of the thread, the step of querying a writable block list corresponding to the block-managed shared memory area is continued.
7. The method according to claim 5, characterized in that The method further comprises: In a case where it is determined based on the writable block list that there is no writable block in the shared memory area managed by the block, incrementing the query count corresponding to the thread; Before querying the writable block list corresponding to the shared memory area managed by the block, the method further includes: When the query number is less than or equal to the query number threshold, continue to execute the step of querying the writable block list corresponding to the shared memory area of the block management; When the query times are greater than the query times threshold, applying for the type storage space corresponding to the communication data from the next type storage space.
8. The method according to claim 5, characterized in that Each storage block in the shared memory area managed by the block includes a status flag; the method further includes: When it is determined based on the writable block list that there are no writable blocks in the shared memory area managed by the block, the writable block list is refreshed based on the status flag of each of the storage blocks.
9. The method according to claim 1, characterized in that: The allocating corresponding storage space to the communication data based on the size of the communication data and the size of each type of storage space includes: When the size of the ring buffer shared memory area is greater than or equal to the size of the communication data, the communication data is sent to a background sending thread in the form of a task, and the background sending thread writes the communication data into the ring buffer shared memory area.
10. The method according to claim 9, characterized in that Before sending the communication data to the background sending thread in the form of a task, the method further includes: Allocating a corresponding second queue identifier to each of the threads by means of atomic counting; In the case where it is determined to start allocating the ring buffer shared memory area for the thread based on the second queue identifier of the thread, the step of sending the communication data to the background sending thread in the form of a task is continued.
11. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: Acquire storage information of the communication data, and store the storage information in the independent shared memory, wherein the storage information is used to instruct a receiver of the communication data to determine a storage location of the communication data.
12. A shared memory application device, characterized in that: The device comprises: A communication data size acquisition module is used to obtain the size of the communication data of the thread; A first allocation module, configured to obtain a size of the independent shared memory when the independent shared memory has been allocated to the thread; A storage space size acquisition module, used to acquire the size of each type of pre-configured storage space when the size of the independent shared memory is smaller than the size of the communication data; A second allocation module, configured to allocate a corresponding type of storage space to the communication data based on the size of the communication data and the size of each type of storage space; The type storage space includes at least one of a block-managed shared memory area, a locked transmission shared memory area, a ring buffer shared memory area, and a file transmission space exceeding the shared memory limit; Each type of storage space has a priority, and the priority of each type of storage space is from high to low: block management shared memory area, locked transmission shared memory area, ring buffer shared memory area, and file transmission space exceeding the shared memory limit; The second allocation module is specifically used to apply for space from the type storage space of the corresponding level in descending order of the priority. If the type storage space of the corresponding level is not sufficient, continue to apply for space from the type storage space of the next level in descending order of the priority until the corresponding type storage space is allocated for the communication data.
13. A shared memory application device, comprising a memory and a processor, wherein the memory stores a computer program, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 11 are implemented.
14. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 11 are implemented.
Citation Information
Patent Citations
Memory allocation method and device, computer equipment and storage medium
CN111324461A
Gene analysis method and device based on shared memory and computer equipment
CN112270959A