Data request communication method, data response communication method and inter-process communication system
Through shared memory and memory mapping technology, serialized objects are calculated and constructed, which solves the problem of long data transmission in inter-process communication, achieves zero copy, and improves system response speed.
Patent Information
- Application Number
- CN202410833446.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-25
- Publication Date
- 2024-10-22
Smart Images

Figure CN118819890A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data communication, and in particular to a data request communication method, a data response communication method and an inter-process communication system. Background Art
[0002] In a distributed scale system, remote procedure call (RPC) is a key component of the distributed architecture of a scale system. The inter-process communication methods that are widely used in production environments include Unix domain socket and TCP loopback local port. The performance difference between the two is not much from the benchmark test. From the technical details, both need to copy the communication data in user state and kernel state. In the RPC scenario, a request-response process will have four memory copies in inter-process communication, two in the request path and two in the response path. Although the sequential copying on the CPU is very fast, it still consumes a certain amount of time, resulting in a time delay in data transmission. Summary of the invention
[0003] In this embodiment, a data request communication method, a data response communication method and an inter-process communication system are provided to solve the problem in the related art that the data transmission in the inter-process communication takes a long time and causes response delay.
[0004] In a first aspect, a data request communication method is provided in this embodiment, the method being applied to a sending process communicating with a receiving process through a shared memory, the method comprising:
[0005] Calculate and obtain the data size of the first serialized object to be constructed corresponding to the requested data;
[0006] Based on the data size of the first serialized object, applying for a corresponding first target memory from the shared memory;
[0007] constructing the first serialized object in the first target memory;
[0008] The address information of the first target memory is sent to the receiving process, so that the receiving process establishes a memory mapping for the first target memory, and uses the first serialized object through the memory mapping to obtain the requested data.
[0009] In some embodiments, the step of calculating and obtaining the data size of the first serialized object to be constructed corresponding to the request data includes:
[0010] Obtain all parameters and corresponding parameter types required for scale calculation in the request data;
[0011] Obtaining the instantiated calculation function corresponding to each of the parameters;
[0012] Based on the parameter type and the instantiated calculation function corresponding to the parameter, sequentially obtain the data length corresponding to the parameter;
[0013] The data lengths corresponding to the parameters are accumulated to obtain the data size of the first serialized object.
[0014] In some embodiments, obtaining the instantiated calculation function corresponding to each of the parameters includes:
[0015] Based on the variable parameter function template corresponding to the parameters, the parameters are recursively instantiated to obtain the instantiated calculation functions corresponding to the parameters.
[0016] In some embodiments, the obtaining, based on the parameter type and the instantiated calculation function corresponding to the parameter, the data length corresponding to the parameter in sequence comprises:
[0017] Determining whether the parameter type belongs to a preset type;
[0018] In the case where the parameter type belongs to the preset type, calculating the data length corresponding to the parameter based on the instantiated calculation function corresponding to the parameter;
[0019] In the case that the parameter type does not belong to the preset type, a template specialization function corresponding to the parameter is obtained, and the data length corresponding to the parameter is calculated based on the template specialization function.
[0020] In some embodiments, the calculating the data length corresponding to the parameter based on the template specialization function includes:
[0021] Determine whether the parameter type includes a built-in type;
[0022] In the case where the parameter type includes at least one built-in type, obtaining the data length corresponding to each of the built-in types;
[0023] The data lengths corresponding to the built-in types are accumulated to obtain the data length corresponding to the parameter.
[0024] In some embodiments, applying for a corresponding first target memory from the shared memory based on the data size of the first serialized object includes:
[0025] Comparing the data size of the first serialized object with a preset size threshold;
[0026] In a case where the data size of the first serialized object is less than or equal to the preset size threshold, applying for a preset exclusive memory space in the shared memory as the first target memory, the size of the first target memory being equal to the preset size threshold;
[0027] When the data size of the first serialized object is greater than the preset size threshold, the shared memory is dynamically allocated to obtain the first target memory.
[0028] In some of the embodiments, after sending the address information of the first target memory to the receiving process, the method further includes:
[0029] Receiving address information of a second target memory sent by the receiving process, wherein a second serialized object is stored in the second target memory, and the second serialized object is constructed based on return data corresponding to the request data;
[0030] Based on the address information, establishing a memory mapping for the second target memory;
[0031] The returned data is obtained by using the second serialized object through the memory mapping.
[0032] In a second aspect, a data response communication method is provided in this embodiment, the method is applied to a receiving process that communicates with a sending process through a shared memory, and the method includes:
[0033] Receiving address information of a first target memory sent by the sending process;
[0034] Based on the address information, establishing a memory mapping for the first target memory;
[0035] The first serialized object stored in the first target memory is used through the memory mapping to obtain corresponding request data.
[0036] In some embodiments, after obtaining the corresponding request data, the method further includes:
[0037] Calculate and obtain the data size of the second serialized object to be constructed corresponding to the returned data, wherein the returned data corresponds to the request data;
[0038] Based on the data size of the second serialized object, applying for a corresponding second target memory from the shared memory;
[0039] constructing the second serialized object in the second target memory;
[0040] The address information of the second target memory is sent to the sending process, so that the sending process uses the second serialized object to obtain the return data.
[0041] In a third aspect, an inter-process communication system is provided in this embodiment, comprising a client process and a server process communicating through shared memory, wherein the client process communicates based on the data request communication method described in the first aspect, and the server process communicates based on the data response communication method described in the second aspect.
[0042] Compared with the related art, the data request communication method provided in this embodiment obtains the data size of the first serialized object to be constructed corresponding to the request data by calculation, and applies for the corresponding first target memory from the shared memory according to the data size, and directly constructs the first serialized object in the first target memory, thereby avoiding the sender from constructing the request data as a serialized object and then copying the serialized object to the shared memory, thereby avoiding one data copy and reducing the time consumption of data transmission; by sending the address information of the first target memory to the receiving process, so that the receiving process establishes a memory mapping of the first target memory, and uses the first serialized object through memory mapping to obtain the request data, thereby avoiding the first serialized object from the shared memory to the receiving party and then using it at the receiving party, realizing zero copy of data request communication and solving the problem of long data transmission time and response delay caused by inter-process communication.
[0043] Details of one or more embodiments of the present application are set forth in the following drawings and description to make other features, objects, and advantages of the present application more readily apparent. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0045] Figure 1 is a structural block diagram of an inter-process communication system of a data request communication method in some embodiments of the present application;
[0046] Figure 2 is a flow chart of a data request communication method according to some embodiments of the present application;
[0047] Figure 3 is a flowchart of obtaining the data size of a first serialized object in some embodiments of the present application;
[0048] Figure 4 is a flowchart of obtaining the data length corresponding to the parameters in some embodiments of the present application;
[0049] Figure 5 is a flowchart of calculating data length based on a template-specialized function in some embodiments of the present application;
[0050] Figure 6 is a flowchart of applying for a first target memory in some embodiments of the present application;
[0051] Figure 7 is a flowchart of obtaining returned data in some embodiments of the present application;
[0052] Figure 8 is a flow chart of a data response communication method according to some embodiments of the present application;
[0053] Fig. 9 is a flowchart of sending return data in some embodiments of the present application;
[0054] Fig.10 It is a flowchart of a data request and response communication method according to some preferred embodiments of the present application. DETAILED DESCRIPTION
[0055] In order to more clearly understand the purpose, technical solutions and advantages of the present application, the present application is described and illustrated 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.
[0056] Unless otherwise defined, the technical terms or scientific terms involved in this application shall have the general meaning understood by people with ordinary skills in the technical field to which this application belongs. The words "one", "a", "a", "the", "these" and the like in this application do not represent quantitative restrictions, and they can be singular or plural. The terms "include", "comprise", "have" and any variants thereof involved in this application are intended to cover non-exclusive inclusions; for example, a process, method and system, product or device comprising a series of steps or modules (units) is not limited to the listed steps or modules (units), but may include unlisted steps or modules (units), or may include other steps or modules (units) inherent to these processes, methods, products or devices. The words "connect", "connected", "coupled" and the like involved in this application are not limited to physical or mechanical connections, but may include electrical connections, whether directly or indirectly. The "multiple" involved in this application refers to two or more. "And / or" describes the association relationship of associated objects, indicating that there can be three relationships. For example, "A and / or B" can mean: A exists alone, A and B exist at the same time, and B exists alone. Usually, the character " / " indicates that the objects associated with each other are in an "or" relationship. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific ordering of the objects.
[0057] The data request communication method provided in the embodiment of the present application can be executed in an inter-process communication system. Figure 1 is a structural block diagram of an inter-process communication system in some embodiments of the present application. Figure 1 As shown, the inter-process communication system includes a client process 11 and a server process 12 communicating through a shared memory 13. The client process 11 can be the sender of the request data, and the server process 12 can be the receiver of the request data. The server process 12 can also generate corresponding return data based on the request data. In this case, the server process 12 is the sender of the return data, and the client process 11 is the receiver of the return data. The client process 11 and the server process 12 can run in the user space of a server, a computer or a similar computing device. It can be understood by ordinary technicians in this field that Figure 1 The structure shown is only for illustration and does not limit the structure of the above-mentioned inter-process communication system. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations shown.
[0058] In this embodiment, a data request communication method is provided, which is applied to a sender process of an inter-process communication system. The sender process may be Figure 1The client process 11 is shown. Figure 2 is a flow chart of a data request communication method according to some embodiments of the present application. Figure 2 As shown, the process includes the following steps:
[0059] Step S201, calculating and obtaining the data size of the first serialized object to be constructed corresponding to the request data.
[0060] The inter-process communication system of this embodiment is a communication system based on RPC (remote procedure call), which needs to convert the transmitted request data and return data into binary data for transmission through data serialization. The serialized data requires continuous memory space for storage. Among them, the serialized data corresponding to the request data sent by the sending process is the first serialized object.
[0061] It should be noted that the sending process in this embodiment does not convert the request data into the first serialized object locally and then copy the first serialized object to the shared memory, but directly constructs the first serialized object in the shared memory. Therefore, before constructing the first serialized object, the sending process needs to calculate the data size of the first serialized object, and configure the space of the shared memory according to the data size, and apply for the corresponding memory space to construct the first serialized object. This calculation process does not perform specific serialization actions, but only calculates the size of the serialized data.
[0062] Step S202: based on the data size of the first serialized object, apply for a corresponding first target memory from the shared memory.
[0063] The sending process applies for the corresponding first target memory from the shared memory according to the calculated data size of the first serialized object. The application method can be to use a pre-allocated exclusive memory space, or to dynamically allocate the memory space according to the data size. There are mature technologies for the dynamic allocation and recovery of shared memory, which will not be described in detail in this embodiment.
[0064] Step S203: construct a first serialized object in the first target memory.
[0065] The sending process can construct the first serialized object through the emplace function, which can directly construct an object in the first target memory, avoiding data copying and thus improving performance.
[0066] Step S204: sending the address information of the first target memory to the receiving process, so that the receiving process establishes a memory mapping for the first target memory, and uses the first serialized object through the memory mapping to obtain the requested data.
[0067] After the sending process constructs the first serialized object in the first target memory, the address information of the first target memory is sent to the receiving process. The address information may include the first address and space size of the first target memory. The receiving process may establish a memory mapping of the receiving process to the first target memory based on the address information of the first target memory. Memory mapping refers to the correspondence between the logical address space of the receiving process and the physical space of the first target memory. This correspondence is a logical concept because the logical address space of the receiving process does not exist physically. Through this memory mapping, the receiving process is equivalent to logically placing the first serialized object in the memory of the receiving process, so the first serialized object can be directly used to obtain the requested data based on the first serialized object, avoiding the copy of data from the shared memory to the local of the receiving process.
[0068] Through steps S201 to S204, the data size of the first serialized object to be constructed corresponding to the request data is obtained by calculation, and the corresponding first target memory is applied for from the shared memory according to the data size, and the first serialized object is directly constructed in the first target memory, thereby avoiding the sender from constructing the request data as a serialized object and then copying the serialized object to the shared memory, thereby avoiding one data copy and reducing the time consumption of data transmission; by sending the address information of the first target memory to the receiving process, so that the receiving process establishes a memory mapping of the first target memory, and uses the first serialized object through memory mapping to obtain the request data, thereby avoiding the first serialized object from the shared memory to the receiving party and then using it at the receiving party, realizing zero copy of data request communication and solving the problem of long data transmission time and response delay in inter-process communication.
[0069] In some embodiments, Figure 3 is a flowchart of obtaining the data size of the first serialized object in some embodiments of the present application, such as Figure 3 As shown, the process includes the following steps:
[0070] Step S301, obtaining all parameters and corresponding parameter types required for scale calculation in the request data.
[0071] The data size of the first serialized object is determined according to the data length corresponding to each parameter in the request data. The request data includes one or more parameters, and the data length of the one or more parameters can be calculated by calling the interface. The calculation process can be implemented based on metaprogramming. First, the parameters whose data length needs to be calculated and the corresponding parameter types are passed in, and then the data length is calculated according to the parameter types corresponding to each parameter. The parameter types can include basic types, enumerations, arrays, and built-in types.
[0072] Step S302, obtaining the instantiated calculation function corresponding to each parameter.
[0073] This embodiment uses a preset function template to calculate the data length of each parameter. The function template is a general function. It is necessary to instantiate the function template according to the parameter type corresponding to each parameter to obtain the corresponding instantiated calculation function. The instantiated calculation function refers to the calculation function of the parameter length generated after replacing the parameters in the function template with specific parameter types.
[0074] In a further embodiment, the method for obtaining the instantiated calculation function corresponding to each parameter includes:
[0075] Based on the variable parameter function template corresponding to the parameter, the parameter is recursively instantiated to obtain the instantiated calculation function corresponding to each parameter.
[0076] The number of template parameters contained in the variable parameter function template is not fixed, and any number of parameters of any type can be passed in. The passed in parameters can be uniformly encapsulated using a parameter pack. Each parameter in the parameter pack cannot be obtained directly, but each parameter therein is obtained by expanding the parameter pack. This embodiment expands the parameter pack by recursive calling, and obtains the instantiation calculation function corresponding to each parameter. Recursive calling refers to calling the function template recursively in the variable parameter function template, separating each parameter in the parameter pack in turn, and obtaining the corresponding instantiation calculation function according to the parameter type, until the instantiation calculation function of all parameters of the parameter pack is obtained.
[0077] Step S303, based on the parameter type and the instantiated calculation function corresponding to the parameter, sequentially obtain the data length corresponding to the parameter.
[0078] After obtaining the instantiation calculation functions corresponding to all parameters, the data length corresponding to each parameter is obtained in turn according to the instantiation calculation function corresponding to each parameter.
[0079] Step S304: Accumulate the data lengths corresponding to the parameters to obtain the data size of the first serialized object.
[0080] Through steps S301 to S304, by obtaining all parameters and corresponding parameter types that need to be calculated in the request data, the parameter object and necessary information for data length calculation are determined; by obtaining the instantiated calculation function corresponding to each parameter, a tool for calculating the data length of the parameter is obtained; by obtaining the data length corresponding to the parameter in turn based on the parameter type and the instantiated calculation function corresponding to the parameter, the data length calculation result of each parameter is obtained; by accumulating the data length corresponding to each parameter, the data size of the first serialized object is obtained, which provides necessary data for the subsequent shared memory space allocation and the construction of the first serialized object.
[0081] In some embodiments, Figure 4 is a flowchart of obtaining the data length corresponding to the parameters in some embodiments of the present application, such as Figure 4 As shown, the process includes the following steps:
[0082] Step S401, determining whether the parameter type belongs to a preset type.
[0083] The parameter type of the parameter is read, and it is determined whether the parameter type belongs to a preset type. The preset type may include a basic type, an enumeration, an array, etc. The parameter type may be determined in sequence based on a preset order. In a specific embodiment, it is first determined whether the parameter type belongs to a basic type or an enumeration type; if it does not belong to a basic type or an enumeration type, it is then determined whether it belongs to an array type; if it does not belong to an array type, it is determined that the parameter type does not belong to a preset type.
[0084] Step S402: When the parameter type belongs to a preset type, the data length corresponding to the parameter is calculated based on the instantiation calculation function corresponding to the parameter.
[0085] In the case where the parameter type belongs to a preset type, according to the parameter type determined in step S401, a corresponding instantiation calculation function is used to calculate the data length corresponding to the parameter.
[0086] In a specific embodiment, for parameters of basic types or enumeration types, a standard length calculation function (Sizeof) can be used as an instantiation calculation function. For parameters of array type, the array is first divided into multiple parameters, and then the Sizeof function is used as an instantiation calculation function to calculate the data length of a single parameter, and then the data length of the array is obtained.
[0087] Step S403, when the parameter type does not belong to the preset type, obtain the template specialization function corresponding to the parameter, and calculate the data length corresponding to the parameter based on the template specialization function.
[0088] If the parameter type does not belong to the preset type, determine whether there is a template specialization function corresponding to the parameter type in the container library. The template specialization function refers to a calculation function for the data length of the parameter type. If it exists, the data length corresponding to the parameter is calculated based on the template specialization function; if it does not exist, it is determined to be a program exception, and the data size calculation process of the first serialized object is terminated.
[0089] Through steps S401 to S403, by determining whether the parameter type belongs to the preset type, the specific type of the parameter is obtained. If it belongs to the preset type, the data length corresponding to the parameter is calculated based on the instantiation calculation function corresponding to the parameter. If it does not belong to the preset type, the template specialization function corresponding to the parameter is obtained, and the data length corresponding to the parameter is calculated based on the template specialization function. This realizes the calculation of the data length of the parameter of the preset type or the custom type, and provides necessary data for calculating the data size of the first serialized object.
[0090] In some embodiments, Figure 5 is a flowchart of calculating data length based on a template-specialized function in some embodiments of the present application, such as Figure 5 As shown, the process includes the following steps:
[0091] Step S501, determining whether the parameter type includes a built-in type.
[0092] In the process of calculating the data length based on the template specialization function, it is first determined whether the parameter type contains a built-in type. The built-in type refers to the specific type of the parameter object hidden in the container. The parameter may include one or more built-in types.
[0093] Step S502: when the parameter type includes at least one built-in type, obtain the data length corresponding to each built-in type.
[0094] Based on steps S401 to S403 of the above embodiment, the built-in type of the parameter can be extracted. In the case of multiple built-in types, type extraction is performed on each built-in type. Specifically, the specific parameter type in each built-in type can be obtained through the template specialization function, and then the data length is calculated for the parameter type through the corresponding instantiation calculation function. Furthermore, the built-in type can also include the built-in type of the next layer, and the final data length of the built-in type can be obtained through a cyclic type extraction process.
[0095] Step S503, accumulating the data lengths corresponding to the embedded types to obtain the data lengths corresponding to the parameters.
[0096] Through steps S501 to S503, by determining whether the type of the parameter contains a built-in type, when the type of the parameter contains at least one built-in type, the data length corresponding to each built-in type is obtained, and the data length is calculated through a cyclic built-in type extraction process; by accumulating the data length corresponding to each built-in type, the data length corresponding to the parameter is obtained, thereby realizing the calculation of the data length of the parameter of the built-in type, and providing necessary data for calculating the data size of the first serialized object.
[0097] In some embodiments, Figure 6is a flowchart of obtaining the first target memory in some embodiments of the present application, such as Figure 6 As shown, the process includes the following steps:
[0098] Step S601: compare the data size of the first serialized object with a preset size threshold.
[0099] Step S602, when the data size of the first serialized object is less than or equal to the preset size threshold, apply for a preset exclusive memory space in the shared memory as a first target memory, and the size of the first target memory is equal to the preset size threshold.
[0100] After obtaining the data size of the first serialized object, it is determined whether to apply for the first target memory by exclusive memory space or dynamic allocation according to the comparison result of the data size and the preset size threshold. When the data size of the first serialized object is less than or equal to the preset size threshold, the exclusive memory space method is adopted, and the pre-set exclusive memory space is called an exclusive buffer, and the space size is equal to the preset size threshold.
[0101] Step S603: When the data size of the first serialized object is greater than a preset size threshold, dynamically allocate the shared memory to obtain a first target memory.
[0102] When the data size of the first serialized object is larger than a preset size threshold, a first target memory is applied for in a dynamic allocation manner.
[0103] Through steps S601 to S603, by comparing the data size of the first serialized object with the preset size threshold, it is determined whether to apply for the first target memory by exclusive memory space or dynamic allocation; when the size is less than or equal to the preset size threshold, the exclusive memory space method is adopted, which eliminates the overhead generated by dynamic memory allocation, and also avoids memory fragmentation, thereby improving the performance of inter-process communication; when the size is greater than the preset size threshold, the dynamic allocation method is adopted, which improves the utilization rate of shared memory, and compared with the overall data transmission time, the dynamic memory allocation time accounts for a relatively low proportion, which has little impact on the performance of inter-process communication.
[0104] In some embodiments, Figure 7 is a flowchart of obtaining return data in some embodiments of the present application, such as Figure 7 As shown, the process includes the following steps:
[0105] Step S701: receiving address information of a second target memory sent by a receiving process, wherein a second serialized object is stored in the second target memory, and the second serialized object is constructed based on return data corresponding to request data.
[0106] The receiving process of this embodiment refers to the server process, and the sending process refers to the client process. After the server process uses the first serialized object to obtain the request data, it executes the corresponding service according to the request data to obtain the return data. Similar to the communication method of data request, the server process can also construct a second serialized object corresponding to the return data in the shared memory, and send the address information of the second target memory where the second serialized object is located to the client process.
[0107] Step S702: establishing a memory mapping for the second target memory based on the address information.
[0108] The client process establishes a memory mapping for the second target memory according to the address information, that is, a correspondence between the logical address space of the client process and the physical space of the second target memory.
[0109] Step S703, using the second serialized object through memory mapping to obtain return data.
[0110] The client process logically places the second serialized object in the memory of the client process through the memory mapping, so the second serialized object can be directly used to obtain return data based on the second serialized object, avoiding data copying from the shared memory to the local client process.
[0111] Through steps S701 to S703, by receiving the address information of the second target memory of the second serialized object sent by the receiving process, the server process is prevented from constructing the return data as a serialized object and then copying the serialized object to the shared memory, thereby avoiding one data copy and reducing the time consumption of data transmission; by establishing a memory mapping of the second target memory based on the address information, and using the second serialized object through memory mapping to obtain the return data, it is avoided to copy the second serialized object from the shared memory to the client process, realizing zero copy of data response communication, and solving the problem of response delay caused by long data transmission time in inter-process communication.
[0112] This embodiment also provides a data response communication method, which is applied to a receiving process that communicates with a sending process through a shared memory. Figure 8 is a flow chart of a data response communication method according to some embodiments of the present application, such as Figure 8 As shown, the process includes the following steps:
[0113] Step S801, receiving address information of a first target memory sent by a sending process.
[0114] The receiving process in this embodiment refers to the server process, and the sending process refers to the client process. The client process constructs a first serialized object corresponding to the request data in the shared memory, and sends the address information of the first target memory where the first serialized object is located to the server process.
[0115] Step S802: establishing a memory mapping for the first target memory based on the address information.
[0116] The server process establishes a memory mapping for the first target memory according to the address information.
[0117] Step S803: Obtain corresponding request data by using the first serialized object stored in the first target memory through memory mapping.
[0118] The server process logically places the first serialized object in the memory of the server process through the memory mapping, so the first serialized object can be directly used to obtain the request data according to the first serialized object.
[0119] Through steps S801 to S803, by receiving the address information of the first target memory of the first serialized object sent by the sending process, it is avoided that the client process constructs the request data as a serialized object and then copies the serialized object to the shared memory, thereby avoiding one data copy and reducing the time consumption of data transmission; by establishing a memory mapping of the first target memory based on the address information, and using the first serialized object through memory mapping to obtain the request data, it is avoided to copy the first serialized object from the shared memory to the server process, realizing zero copy of data request communication, and solving the problem of long data transmission time leading to response delay in inter-process communication.
[0120] In some embodiments, Fig. 9 is a flowchart of sending return data in some embodiments of the present application, such as Fig. 9 As shown, the process includes the following steps:
[0121] Step S901, calculating and obtaining the data size of the second serialized object to be constructed corresponding to the returned data, wherein the returned data corresponds to the requested data;
[0122] Step S902, based on the data size of the second serialized object, applying for a corresponding second target memory from the shared memory;
[0123] Step S903, constructing a second serialized object in the second target memory;
[0124] Step S904: Send the address information of the second target memory to the sending process, so that the sending process uses the second serialized object to obtain return data.
[0125] Through steps S901 to S904, the data size of the second serialized object to be constructed corresponding to the returned data is obtained by calculation, and the corresponding second target memory is applied for from the shared memory according to the data size, and the second serialized object is directly constructed in the second target memory, so as to avoid the server process constructing the returned data as a serialized object and then copying the serialized object to the shared memory, thereby avoiding one data copy and reducing the time consumption of data transmission; by sending the address information of the second target memory to the sending process, so that the sending process establishes a memory mapping of the second target memory, and uses the second serialized object through memory mapping to obtain the returned data, thereby avoiding the second serialized object from the shared memory to the client process and then using it on the client, realizing zero copy of data response communication and solving the problem of response delay caused by long data transmission time in inter-process communication.
[0126] The present embodiment is described and illustrated below through a preferred embodiment. The RPC (remote procedure call) based on shared memory further reduces the remote procedure call (RPC) delay time of the distributed architecture of the scale system to the microsecond level in a single request response. In this context, the microsecond-level data transmission time becomes a bottleneck in the response delay. Fig.10 is a flow chart of a data request and response communication method according to some preferred embodiments of the present application, which is applied to a client process and a server process of an inter-process communication system, such as Fig.10 As shown, the process includes the following steps:
[0127] Step S1001, the client process obtains all parameters and corresponding parameter types required for scale calculation in the request data;
[0128] Step S1002: the client process recursively instantiates each parameter based on the variable parameter function template corresponding to the parameter to obtain an instantiated calculation function corresponding to each parameter;
[0129] Step S1003, determining in turn whether the parameter type of each parameter belongs to a preset type;
[0130] Step S1004, when the parameter type belongs to a preset type, calculating the data length corresponding to the parameter based on the instantiated calculation function corresponding to the parameter;
[0131] Step S1005, when the parameter type does not belong to the preset type, obtain the template specialization function corresponding to the parameter;
[0132] Step S1006, determining whether the parameter type includes a built-in type; if the parameter type does not include a built-in type, calculating the data length of the parameter based on the template specialization function;
[0133] Step S1007, when the parameter type includes a built-in type, based on steps S1003 to S1006, type extraction is performed on the built-in type, and the data length corresponding to each built-in type is obtained;
[0134] Step S1008, accumulating the data lengths corresponding to each built-in type to obtain the data length corresponding to the parameter;
[0135] Step S1009, accumulating the data lengths corresponding to the parameters to obtain the data size of the first serialized object;
[0136] Step S1010, comparing the data size of the first serialized object with a preset size threshold;
[0137] Step S1011, when the data size of the first serialized object is less than or equal to the preset size threshold, applying for a pre-set exclusive memory space in the shared memory as a first target memory, and the size of the first target memory is equal to the preset size threshold;
[0138] Step S1012, when the data size of the first serialized object is greater than a preset size threshold, dynamically allocating the shared memory to obtain a first target memory;
[0139] Because the amount of data to be transmitted may range from bytes to gigabytes, and there is concurrency, dynamic shared memory allocation is preferred. The mechanism for memory allocation and recycling on shared memory is more complex than that on the local memory pool. It is a process (thread) safe version of the local memory pool and provides a globally unique identifier for constructed objects (globally unique identifiers are used for cross-process object identification). Because of the need to avoid data contention to provide cross-process security guarantees, time overhead is introduced.
[0140] Taking "boostV1.79 managed memory segment" as an example to benchmark it, in different scenarios, it will introduce memory application time overhead ranging from ten to hundreds of microseconds. But small data (in this embodiment, data less than 20M) takes several microseconds overall. Obviously, dynamic application of shared memory is not applicable in small data request scenarios. Exclusive memory area is provided for small data, that is, memory area is allocated in advance, and zero copy achieves performance improvement in this scenario.
[0141] Step S1013, the client process constructs a first serialized object in the first target memory;
[0142] Step S1014, the client process sends the address information of the first target memory to the server process;
[0143] Step S1015, the server process establishes a memory mapping for the first target memory according to the address information, and obtains the requested data by using the first serialized object through the memory mapping;
[0144] Step S1016, the server process calculates the data size of the second serialized object to be constructed corresponding to the returned data based on the method of steps S1001 to S1009;
[0145] Step S1017, based on the data size of the second serialized object, the server process applies for the corresponding second target memory from the shared memory;
[0146] Step S1018, the server process constructs a second serialized object in the second target memory;
[0147] Step S1019, the server process sends the address information of the second target memory to the client process;
[0148] Step S1020: the client process establishes a memory mapping for the second target memory according to the address information, and uses the second serialized object through the memory mapping to obtain return data.
[0149] Through steps S1001 to S1020, the client process and the server process obtain the data size of the serialized object corresponding to the data to be sent through calculation in turn, apply for the corresponding target memory from the shared memory according to the data size, and construct the serialized object corresponding to the data to be sent in the target memory, thereby avoiding the data sender from constructing the data to be sent as a serialized object and then copying the serialized object to the shared memory, avoiding one data copy and reducing the time consumption of data transmission; the data receiver obtains the data to be sent by establishing a memory mapping for the target memory and using the serialized object through memory mapping, thereby avoiding copying the serialized object from the shared memory to the receiver and using it at the receiver, realizing zero copy of data communication between processes and solving the problem of response delay caused by long data transmission time in inter-process communication.
[0150] The communication method of data request and data response in the above optimal embodiment further optimizes the RPC performance based on shared memory, especially the performance in the scenario of transmission of several microseconds, wherein the total rate of return is about 50%.
[0151] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementation modes, and will not be repeated in this embodiment.
[0152] It should be understood that the specific embodiments described herein are only used to explain the application, rather than to limit it. Based on the embodiments provided in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the protection scope of this application.
[0153] Obviously, the drawings are only some examples or embodiments of the present application. For ordinary technicians in the field, the present application can also be applied to other similar situations based on these drawings without creative work. In addition, it is understandable that although the work done in this development process may be complicated and lengthy, for ordinary technicians in the field, certain changes in design, manufacturing or production based on the technical content disclosed in this application are only conventional technical means and should not be regarded as insufficient content disclosed in this application.
[0154] The term "embodiment" in this application refers to a specific feature, structure or characteristic described in conjunction with the embodiment that can be included in at least one embodiment of the present application. The appearance of this phrase in various locations in the specification does not necessarily mean the same embodiment, nor does it mean that it is mutually exclusive with other embodiments and is independent or optional. It is clearly or implicitly understood by those of ordinary skill in the art that the embodiments described in this application can be combined with other embodiments without conflict.
[0155] The above-mentioned 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 patent protection. 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 scope of protection of the present application. Therefore, the scope of protection of the present application shall be subject to the attached claims.
Claims
1. A data request communication method, characterized in that: The method is applied to a sender process that communicates with a receiver process through a shared memory, and the method comprises: Calculate and obtain the data size of the first serialized object to be constructed corresponding to the requested data; Based on the data size of the first serialized object, applying for a corresponding first target memory from the shared memory; constructing the first serialized object in the first target memory; The address information of the first target memory is sent to the receiving process, so that the receiving process establishes a memory mapping for the first target memory, and uses the first serialized object through the memory mapping to obtain the requested data.
2. The method according to claim 1, characterized in that The step of calculating and obtaining the data size of the first serialized object to be constructed corresponding to the request data includes: Obtain all parameters and corresponding parameter types required for scale calculation in the request data; Obtaining the instantiated calculation function corresponding to each of the parameters; Based on the parameter type and the instantiated calculation function corresponding to the parameter, sequentially obtain the data length corresponding to the parameter; The data lengths corresponding to the parameters are accumulated to obtain the data size of the first serialized object.
3. The method according to claim 2, characterized in that The obtaining of the instantiation calculation function corresponding to each of the parameters comprises: Based on the variable parameter function template corresponding to the parameters, the parameters are recursively instantiated to obtain the instantiated calculation functions corresponding to the parameters.
4. The method according to claim 2, characterized in that: The obtaining, based on the parameter type and the instantiated calculation function corresponding to the parameter, the data length corresponding to the parameter in sequence comprises: Determining whether the parameter type belongs to a preset type; In the case where the parameter type belongs to the preset type, calculating the data length corresponding to the parameter based on the instantiated calculation function corresponding to the parameter; In the case that the parameter type does not belong to the preset type, a template specialization function corresponding to the parameter is obtained, and the data length corresponding to the parameter is calculated based on the template specialization function.
5. The method according to claim 4, characterized in that The calculating the data length corresponding to the parameter based on the template specialization function comprises: Determine whether the parameter type includes a built-in type; In the case where the parameter type includes at least one built-in type, obtaining the data length corresponding to each of the built-in types; The data lengths corresponding to the built-in types are accumulated to obtain the data length corresponding to the parameter.
6. The method according to claim 1, characterized in that The applying for a corresponding first target memory from the shared memory based on the data size of the first serialized object includes: Comparing the data size of the first serialized object with a preset size threshold; In a case where the data size of the first serialized object is less than or equal to the preset size threshold, applying for a preset exclusive memory space in the shared memory as the first target memory, the size of the first target memory being equal to the preset size threshold; When the data size of the first serialized object is greater than the preset size threshold, the shared memory is dynamically allocated to obtain the first target memory.
7. The method according to claim 1, characterized in that After sending the address information of the first target memory to the receiving process, the method further includes: Receiving address information of a second target memory sent by the receiving process, wherein a second serialized object is stored in the second target memory, and the second serialized object is constructed based on return data corresponding to the request data; Based on the address information, establishing a memory mapping for the second target memory; The returned data is obtained by using the second serialized object through the memory mapping.
8. A data response communication method, characterized in that: The method is applied to a receiving process that communicates with a sending process through a shared memory, and the method comprises: Receiving address information of a first target memory sent by the sending process; Based on the address information, establishing a memory mapping for the first target memory; The first serialized object stored in the first target memory is used through the memory mapping to obtain corresponding request data.
9. The method according to claim 8, characterized in that After obtaining the corresponding request data, the method further includes: Calculate and obtain the data size of the second serialized object to be constructed corresponding to the returned data, wherein the returned data corresponds to the request data; Based on the data size of the second serialized object, applying for a corresponding second target memory from the shared memory; constructing the second serialized object in the second target memory; The address information of the second target memory is sent to the sending process, so that the sending process uses the second serialized object to obtain the return data.
10. An inter-process communication system, comprising a client process and a server process communicating via shared memory, characterized in that: The client process communicates based on the data request communication method described in any one of claims 1 to 7, and the server process communicates based on the data response communication method described in any one of claims 8 to 9.
Citation Information
Cited By
Data processing method and device, electronic equipment and readable storage medium
CN119781835A
