Socket-based data transmission method, electronic device, and storage medium
By adding a data cache module and Socket kernel driver in the kernel space, only two data copies are required in Socket inter-process communication, solving the problem of low data transmission efficiency. It also supports user space to modify and delete data, improving the user experience.
Patent Information
- Application Number
- CN202410174195.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-07
- Publication Date
- 2025-09-12
- Estimated Expiration
- 2044-02-07
AI Technical Summary
During Socket inter-process communication, data transmission efficiency is low due to excessive data copying and the lack of support for modifying or deleting forwarded data in user space.
A new data cache module and Socket kernel driver are added to the kernel space. A mapping relationship is established between the data cache module and the data forwarding service module in the user space. Only two data copies are required to achieve inter-process data transmission, and the user space is supported to access and modify the data in the kernel space data cache module.
It reduces the number of data copies, improves data transmission efficiency, and supports modification or deletion of forwarded data in user space, improving user experience.
Smart Images

Figure CN119248530B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to a data transmission method, electronic device, and storage medium based on Socket. Background Art
[0002] Currently, in the process of Socket inter-process communication, such as Figure 1A and Figure 1B As shown, the sender packages the data to be sent into msg structure data, calls the Socket interface, and copies the msg structure data (first copy) to the Socket buffer; then, the data forwarding service module calls the Socket interface to copy (second copy) the msg structure data from the Socket send buffer, and then calls the Socket interface to copy (third copy) the msg structure data to the Socket receive buffer; then, the receiver calls the Socket interface to copy (fourth copy) the msg structure data from the Socket receive buffer.
[0003] When electronic devices use the traditional Socket communication method to forward data, four data copies occur during a single data transmission process, which seriously affects data transmission efficiency. Summary of the Invention
[0004] The present application provides a data transmission method and electronic device based on Socket, which can reduce the number of data copies, improve data transmission efficiency, and enhance user experience.
[0005] In a first aspect, the present application provides a data transmission method based on Socket. The electronic device includes a Socket interface, a Socket kernel driver, a data cache module, and a data forwarding service module. The method includes:
[0006] The Socket kernel driver receives first data from a first application; the Socket kernel driver copies the first data to a first cache area of the data cache module; the Socket kernel driver writes a first cache index value corresponding to the first cache area into a receiving queue of the data forwarding service module; the Socket kernel driver receives second data sent by the data forwarding service module, and the second data is obtained based on the first cache index value; the Socket kernel driver parses the first cache index value from the second data, and writes the first cache index value into a receiving queue of a second application; the Socket kernel driver copies the first data from the first cache area based on the first cache index value, and transmits the first data to the second application.
[0007] Through the Socket-based data transmission method provided by the embodiment of the present application, a data cache module and a Socket kernel driver are added to the kernel space. The Socket kernel driver can control the cache area of the data cache module to cache the data from the first application, and transmit the cache index value corresponding to the cache area to the data forwarding service module of the user space. Then the Socket kernel driver can read the cache index value from the data forwarding service module, and copy the data from the cache area based on the cache index value, and then forward it to the second application. Through the present application, since there is a mapping relationship between the data cache module of the kernel space and the data forwarding service module of the user space, the kernel space and the user space can share data through the data cache module. Therefore, in the process of local inter-process communication, only two data copies are required. Compared with the four data copies required by the related technology, the present application solution can reduce the number of data copies and improve data transmission efficiency.
[0008] The first application can be a third-party application installed on the electronic device by the user, or a system application in the electronic device. Apps such as Photo Album or Camera.
[0009] The second application may be a service application such as managing power consumption, managing policies, monitoring performance status, and / or collecting user habit data, and application B may be used to monitor and manage various event data of application A. For example, the second application may be a power consumption management service program, a policy management service program, a performance status monitoring service program, or a user habit data collection service program.
[0010] When application A generates user input and response data, application B obtains the user input and response data. The data on application A needs to be forwarded by the data forwarding service module before reaching application B.
[0011] In related technologies, data forwarding between local processes is implemented based on Sockets, and four data copies occur in a single message transmission. In this application, data forwarding between local processes is implemented based on Sockets, and two data copies occur in a single message transmission, reducing the number of data copies.
[0012] In some possible implementations, the data forwarding service module's receive queue includes cache index values corresponding to respective cache areas of the data cache module. The data cache module includes multiple cache areas, each corresponding to a cache index value. When the data cache module receives a cache request from the socket kernel driver, the data cache module allocates a cache area and notifies the socket kernel driver of the cache index value corresponding to the cache area.
[0013] In some possible implementations, after the Socket kernel driver receives the first data from the first application, and before the Socket kernel driver copies the first data to the first cache area of the data cache module, the method further includes: the Socket kernel driver determines that the type of the first data is the first business type; the Socket kernel driver sends an application message to the data cache module, and the application message is used to apply for a cache area to store data of the first business type; the Socket kernel driver receives a response message from the data cache module, and the response message is used to indicate that the first cache area is allocated for data of the first business type, and the response message also includes the first cache index value corresponding to the first cache area.
[0014] Through the above scheme, when the data cache module receives a cache request from the Socket kernel driver, the data cache module will allocate a cache area and notify the Socket kernel driver of the cache index value corresponding to the cache area, so that the Socket kernel driver can write the cache index value into the receiving queue of the data forwarding service module, and the data forwarding service module can access the cache area based on the cache index value and modify the data in the cache area; and it can also facilitate the Socket kernel driver to access the cache area based on the cache index value, copy the data in the cache area and send it to the second application. Through this application scheme, not only can the number of data copies be reduced and the data transmission efficiency be improved, but it also supports the user space to access and modify the forwarding data cached in the data cache module of the kernel space.
[0015] In some possible implementations, a mapping relationship exists between the first service type, the first cache area, and the first cache index value. The data of the first service type belongs to service data requested to be collected by the second application.
[0016] In some possible implementations, before the Socket kernel driver receives the first data from the first application, the method further includes: in response to a user operation, the first application executes a first service and generates first data; the first application calls the Socket interface and transmits the first data to the Socket kernel driver.
[0017] In some possible implementations, the first application calls the Socket interface to transmit the first data to the Socket kernel driver, including: the first application calls the Socket interface, encapsulates the first data into a first message structure according to a preset specification, and transmits the first message structure to the Socket kernel driver.
[0018] The first message structure can also be referred to as the first msg structure. The msg structure is used for inter-process Socket communication within an electronic device. In the present application, the electronic device can use the msg structure to transmit messages between different processes, thereby improving the compatibility of inter-process Socket communication.
[0019] In some possible implementations, after the Socket kernel driver writes the first cache index value corresponding to the first cache area into the receiving queue of the data forwarding service module, the method further includes: the Socket kernel driver reads the cache index values from the receiving queue of the data forwarding service module in sequence, encapsulates the read cache index values into a second message structure, and transmits the first message structure to the data forwarding service module.
[0020] Through this application solution, the problem of multiple copies in the data forwarding and filtering link using Socket is solved. This application designs a unique shared cache management mechanism and solves the compatibility problem with the existing data interface of Socket, thereby improving the efficiency of data transmission between local processes based on Socket.
[0021] In some possible implementations, the method also includes: the data forwarding service module receives a data modification instruction; in response to the data modification instruction, the data forwarding service module accesses the first cache area in the data cache module based on the first cache index value, and modifies the data in the first cache area based on the data modification instruction.
[0022] Through the solution of this application, compared with the related art that does not support modification or deletion of forwarding data in user space, the solution of this application supports accessing and modifying the forwarding data cached in the data cache module of the kernel space in user space, which can achieve the purpose of modifying or deleting forwarding data in user space and improve the convenience of data transmission.
[0023] In some possible implementations, before the Socket kernel driver receives the second data sent by the data forwarding service module, the method further includes: the data forwarding service module calls the Socket interface, encapsulates the first cache index value into a third message structure according to a preset specification, and transmits the third message structure as the second data to the Socket kernel driver.
[0024] In some possible implementations, the method further includes: when the Socket kernel driver writes the first cache index value into the receive queue of the second application, the Socket kernel driver sends a new data notification to the second application; and the Socket kernel driver receives a fourth message structure sent by the second application for receiving new data.
[0025] In some possible implementations, the Socket kernel driver copies the first data from the first cache area based on the first cache index value and transmits the first data to the second application, including: the Socket kernel driver accesses the first cache area based on the first cache index value and copies the first data in the first cache area to the fourth message structure; the Socket kernel driver calls the Socket interface and transmits the fourth message structure to the second application.
[0026] In some possible implementations, the data cache module includes a control area and a data area, the control area includes N control blocks, the data area includes N data blocks, the N control blocks correspond one-to-one to the N data blocks, and some of the N control blocks have an associated relationship. A control block may be referred to as a node.
[0027] Among them, the characteristic structure of the control block includes node status, data block allocation size, used size, current node number, next node number, number of occupied services, and real address of the data block.
[0028] The first cache area corresponds to a cache set, the cache set includes a control block and a corresponding data block, and the first cache index value corresponds to the number of the data block.
[0029] In some possible implementations, the state of the data cache module is one of the following states: idle state, kernel space read / write state, waiting state, and user space read / write state.
[0030] In some possible implementations, the method further includes: a Socket kernel driver controls the data cache module to switch between the various states.
[0031] In some possible implementations, the Socket kernel driver controls the data cache module to switch between the various states, including:
[0032] The data cache module is in the idle state after initialization, waiting for allocation;
[0033] After the Socket core driver copies the data to the data cache module, the Socket core driver updates the state of the data cache module to the waiting state;
[0034] When the data forwarding service module reads and writes to the data cache module, the Socket kernel driver updates the state of the data cache module to the user space read and write state;
[0035] After the data forwarding service module completes reading and writing to the data cache module, the Socket kernel driver updates the state of the data cache module to a waiting or idle state again.
[0036] When the Socket kernel driver copies data from the data cache module, the state of the data cache module is updated to the kernel space read-write state again.
[0037] After the Socket kernel driver finishes copying the data from the data cache module, it releases the occupied cache area in the data cache module and updates the state of the data cache module to an idle state.
[0038] The Socket-based data transmission method provided by the embodiment of the present application only requires two data copies, which is less than the four data copies required by the related art. Furthermore, compared to the related art that does not support modification or deletion of forwarded data in user space, the present application adds a data cache module to the kernel space for caching forwarded data, and establishes a mapping relationship between the data cache module and the data forwarding service module in the user space, so that the kernel space and the user space can share data through the data cache module, and support access and modification of the forwarded data cached in the kernel space data cache module in the user space, thereby achieving the purpose of modifying or deleting the forwarded data in the user space.
[0039] In a second aspect, the present application provides a Socket-based data transmission device, comprising a unit for executing the method described in the first aspect. The device can be used to execute the method described in the first aspect. For a description of the units in the device, please refer to the description of the first aspect above and, for the sake of brevity, will not be repeated here.
[0040] The method described in the first aspect above can be implemented by hardware, or by hardware executing corresponding software. The hardware or software includes one or more modules or units corresponding to the above functions. For example, a processing module or unit, a display module or unit, etc.
[0041] In a third aspect, the present application provides an electronic device comprising a processor, a computer program or instructions stored in the processor and a memory, wherein the processor is configured to execute the computer program or instructions so that the method in the first aspect is executed.
[0042] In a fourth aspect, the present application provides a computer-readable storage medium having stored thereon a computer program (also referred to as instructions or code) for implementing the method in the first aspect. For example, when the computer program is executed by a computer, the computer can perform the method in the first aspect.
[0043] In a fifth aspect, the present application provides a chip comprising a processor. The processor is configured to read and execute a computer program stored in a memory to perform the method of the first aspect and any possible implementation thereof. Optionally, the chip further comprises a memory, the memory being connected to the processor via a circuit or wire.
[0044] In a sixth aspect, the present application provides a chip system comprising a processor. The processor is configured to read and execute a computer program stored in a memory to perform the method of the first aspect and any possible implementation thereof. Optionally, the chip system further comprises a memory, the memory being connected to the processor via a circuit or wire.
[0045] In a seventh aspect, the present application provides a computer program product, which includes a computer program (also referred to as instructions or codes). When the computer program is executed by an electronic device, the electronic device implements the method in the first aspect.
[0046] It can be understood that the beneficial effects of the second to seventh aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] Figure 1A Schematic diagram 1 of the process of transmitting data between internal processes of an electronic device in the related art;
[0048] Figure 1B Schematic diagram of the process of transmitting data between internal processes of electronic devices in related technologies Figure 2 ;
[0049] Figure 2 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0050] Figure 3 A schematic diagram of the software architecture of an electronic device provided in an embodiment of the present application;
[0051] Figure 4A A schematic diagram of an improved Socket-based data transmission method provided in an embodiment of the present application relative to related technologies;
[0052] Figure 4B Schematic diagram 1 of the flow chart of the data transmission method based on Socket provided in an embodiment of the present application;
[0053] Figure 5 A timing diagram of a data transmission method based on Socket provided in an embodiment of the present application;
[0054] Figure 6 Schematic diagram of the data transmission method based on Socket provided in this application embodiment Figure 2 ;
[0055] Figure 7 Schematic diagram of the data transmission method based on Socket provided in this application embodiment Figure 3 ;
[0056] Figure 8 A schematic diagram of the structure of a data cache module used in the Socket-based data transmission method provided in an embodiment of the present application;
[0057] Figure 9 A schematic diagram of nodes and data blocks of a data cache module used in a Socket-based data transmission method according to an embodiment of the present application;
[0058] Figure 10 A schematic diagram of the state switching of the data cache module used in the Socket-based data transmission method provided in an embodiment of the present application. DETAILED DESCRIPTION
[0059] In order to make the objectives, technical solutions and advantages of this application more clear, the embodiments of this application are further described in detail below in combination with specific embodiments and with reference to the accompanying drawings.
[0060] To facilitate understanding of the embodiments of the present application, some terms used in the embodiments of the present application are explained below to facilitate understanding by those skilled in the art.
[0061] 1. Socket: This refers to a network socket. Sockets are an inter-process communication mechanism provided by the operating system. This mechanism allows different processes to communicate with each other. This communication is accomplished using socket-related functions.
[0062] 2. Socket interface: This refers to the socket API. A socket interface is an application programming interface (API) provided by the operating system for applications. Applications can exchange data using network sockets through a socket interface. Two communicating processes can use a socket interface to describe the two ends of a communication link, implementing socket communication.
[0063] 3. msg structure: It is a commonly used data structure used to pass messages between applications.
[0064] Specifically, a msg structure typically consists of multiple fields, each with a specific name and data type. These fields can store different types of data, such as integers, strings, and Boolean values. By defining a msg structure, related information can be packaged together and passed between applications.
[0065] In real-world applications, the msg structure is often used for inter-process communication, network communication, and event-driven programming models. By packaging the data to be transmitted into a msg structure, data can be easily exchanged between different applications. This approach improves application flexibility and scalability, allowing independent development and debugging of different modules.
[0066] In network communications, the msg structure can be used to pass messages between clients and servers. For example, in a chat application, user messages need to be passed between the client and server. By defining a msg structure containing fields such as the sender, recipient, and message content, messages can be easily passed between the client and server, enabling real-time communication.
[0067] Specifically, in this application, the msg structure is used for inter-process Socket communication within electronic devices. Electronic devices can use the msg structure to pass messages between different processes. For example, in distributed transactions, different service processes need to exchange data and coordinate work. By defining a unified msg structure, shared data can be easily transferred between these processes, enabling system collaboration.
[0068] Figure 1A Schematic diagram of the system architecture involved in each exemplary embodiment of the present application is shown. Figure 1AAs shown in the figure, the system architecture includes application A, a data forwarding service module / program, and application B. When application A sends data to application B, it calls the socket interface to complete inter-process communication, and the data forwarding module is used to complete the data forwarding. On the sending side, data is stored in the socket send buffer; on the receiving side, data is stored in the socket receive buffer. Application A acts as the data sender, application B as the data receiver, and the data forwarding service module acts as the data forwarder.
[0069] For example, Application A can be a third-party application installed on an electronic device by a user, or it can be a system application in the electronic device. Application B can be a service application that manages power consumption, manages policies, monitors performance status, and / or collects user behavior data. Application B can be used to monitor and manage various event data of Application A. When Application A generates user input and response data, Application B will obtain this user input and response data.
[0070] For example, the electronic device receives an operation of the user on application A, which may be an operation in which the user triggers the opening of application A. Accordingly, the electronic device opens application A in response to the user operation. During this process, application A sends the opening event data to application B, and accordingly, application B obtains the opening event data of application A. It should be noted that the user triggering the opening of application A is used as an example for illustrative explanation here. In actual implementation, the user operation may also be an operation in which the user triggers the installation, closing, or uninstallation of application A, and application B may also obtain various event data such as the installation event, closing event, or uninstallation event of application A, which is not limited in the embodiments of the present application.
[0071] Figure 1B FIG. 1 shows a schematic diagram of inter-process communication in an electronic device in the related art. Figure 1B As shown, the sending side includes multiple senders, for example, these senders are For applications like photo albums and cameras, the receiving side includes multiple receivers, such as the power management service, policy management service, performance status monitoring service, and user behavior data collection service. The sender, data forwarding service module, and receiver are all located in the electronic device's user space (i.e., the application layer), while the socket buffer is located in the electronic device's kernel space (i.e., the kernel layer).
[0072] In inter-process communication, such as Figure 1BIn the direction indicated by the middle arrow, the sender packages the data to be sent into msg structure data, calls the Socket interface, and copies the msg structure data (first copy) to the Socket buffer; then, the data forwarding service module calls the Socket interface to copy (second copy) the msg structure data from the Socket send buffer, and then calls the Socket interface to copy (third copy) the msg structure data to the Socket receive buffer; then, the receiver calls the Socket interface to copy (fourth copy) the msg structure data from the Socket receive buffer.
[0073] It can be seen that in electronic equipment using Figure 1B In the traditional socket communication method shown, data forwarding occurs four times during a single data transfer. This high number of copies significantly reduces data transmission efficiency. Furthermore, data modification and deletion in user space are not supported during data forwarding.
[0074] Based on this, the embodiments of the present application provide a socket-based data transmission method and electronic device. By improving the underlying electronic device system, the number of data copies is reduced, data transmission efficiency is improved, and the user experience is enhanced. In addition, through the solution of this application, during the data forwarding process, the data forwarding service module can modify and delete the forwarded data in the user space.
[0075] In order to better understand the embodiments of the present application, the embodiments of the present application are briefly described below:
[0076] The data transmission method based on Socket provided in the embodiment of the present application is applied to an electronic device, which includes a Socket interface, a Socket kernel driver, a data cache module and a data forwarding service module. The data transmission method based on Socket includes: the Socket kernel driver receives first data from a first application; the Socket kernel driver copies the first data to a first cache area of the data cache module; the Socket kernel driver writes a first cache index value corresponding to the first cache area into a receiving queue of the data forwarding service module; the Socket kernel driver receives second data sent by the data forwarding service module, the second data being obtained based on the first cache index value; the Socket kernel driver parses the first cache index value from the second data and writes the first cache index value into a receiving queue of a second application; the Socket kernel driver copies the first data from the first cache area based on the first cache index value and transmits the first data to the second application.
[0077] Through the Socket-based data transmission method provided by the embodiment of the present application, a data cache module and a Socket kernel driver are added to the kernel space. The Socket kernel driver can control the cache area of the data cache module to cache the data from the first application, and transmit the cache index value corresponding to the cache area to the data forwarding service module of the user space. Then the Socket kernel driver can read the cache index value from the data forwarding service module, and copy the data from the cache area based on the cache index value, and then forward it to the second application. Through the present application, since there is a mapping relationship between the data cache module of the kernel space and the data forwarding service module of the user space, the kernel space and the user space can share data through the data cache module. Therefore, in the process of local inter-process communication, only two data copies are required. Compared with the four data copies required by the related technology, the present application solution can reduce the number of data copies and improve data transmission efficiency.
[0078] Hereinafter, an electronic device to which the data transmission method based on Socket implementation provided in an embodiment of the present application is applied will be described with reference to the accompanying drawings.
[0079] For example, the electronic device in the embodiments of the present application may be a mobile phone, a tablet computer, an ultra-mobile personal computer (UMPC), a netbook, a cellular phone, a personal digital assistant (PDA), a wearable device (such as a smart watch, a smart bracelet), and the like. The embodiments of the present application do not impose any special restrictions on the specific form of the electronic device.
[0080] Figure 2 FIG1 shows a schematic diagram of the structure of an electronic device provided by an embodiment of the present application. For example, taking the electronic device as a mobile phone as an example, Figure 2 As shown, the mobile phone may include: a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver (i.e., an earpiece) 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc.
[0081] The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU). Different processing units may be independent devices or integrated into one or more processors. The controller may be the nerve center and command center of the mobile phone. The controller may generate operation control signals based on instruction opcodes and timing signals to complete the control of instruction fetching and execution.
[0082] In an embodiment of the present application, the processor 110 can be used to: receive first data from a first application through a Socket kernel driver; copy the first data to a first cache area of the data cache module through a Socket kernel driver; write a first cache index value corresponding to the first cache area into a receive queue of the data forwarding service module through a Socket kernel driver; receive second data sent by the data forwarding service module through a Socket kernel driver, the second data being obtained based on the first cache index value; parse the first cache index value from the second data through a Socket kernel driver, and write the first cache index value into a receive queue of a second application; copy the first data from the first cache area based on the first cache index value through a Socket kernel driver, and transmit the first data to the second application.
[0083] This application adds a data cache module and a Socket kernel driver in the kernel space. The Socket kernel driver can control the cache area of the data cache module to cache the data from the first application, and transmit the cache index value corresponding to the cache area to the data forwarding service module in the user space. Then the Socket kernel driver can read the cache index value from the data forwarding service module, and copy the data from the cache area based on the cache index value, and then forward it to the second application. Through this application, since there is a mapping relationship between the data cache module in the kernel space and the data forwarding service module in the user space, the kernel space and the user space can share data through the data cache module. Therefore, in the process of local inter-process communication, only two data copies are required. Compared with the four data copies required by the related technology, the solution of this application can reduce the number of data copies and improve data transmission efficiency.
[0084] Processor 110 may also include a memory for storing instructions and data. In some embodiments, the memory in processor 110 is a cache memory. This memory can store instructions or data that have just been used or are being recycled by processor 110. If processor 110 needs to use the same instruction or data again, it can directly access the memory. This avoids duplicate accesses, reduces processor 110 latency, and thus improves system efficiency.
[0085] In some embodiments, the processor 110 may include one or more interfaces. The interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface.
[0086] It is understood that the interface connection relationship between the modules illustrated in this embodiment is only for illustrative purposes and does not constitute a structural limitation on the mobile phone. In other embodiments, the mobile phone may also adopt different interface connection methods from the above embodiments, or a combination of multiple interface connection methods.
[0087] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the mobile phone. The internal memory 121 can be used to store computer executable program code, which includes instructions. The processor 110 executes various functional applications and data processing of the mobile phone by running the instructions stored in the internal memory 121. For example, in an embodiment of the present application, the processor 110 can execute instructions stored in the internal memory 121, and the internal memory 121 may include a program storage area and a data storage area.
[0088] The wireless communication function of the mobile phone can be realized through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, modem processor and baseband processor.
[0089] The mobile phone can implement audio functions such as music playback and recording through the audio module 170, the speaker 170A, the receiver (ie, earpiece) 170B, the microphone 170C, the headphone jack 170D, and the application processor.
[0090] The mobile phone can implement display functions through a GPU, display screen 194, and an application processor. The GPU is a microprocessor for image processing that connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or modify display information. The display screen 194 is used to display images, videos, etc.
[0091] The mobile phone can implement camera functions through the ISP, camera 193, video codec, GPU, display 194, and application processor. The ISP is used to process data fed back by camera 193. In some embodiments, the ISP can be installed in camera 193. Camera 193 is used to capture still images or videos. In some embodiments, the mobile phone can include one or N cameras 193, where N is a positive integer greater than 1.
[0092] The above is a specific description of the embodiments of the present application using the electronic device 100 as an example. It should be understood that the structure illustrated in the embodiments of the present application does not constitute a specific limitation on the electronic device 100. The electronic device 100 may have more or fewer components than shown in the figure, may combine two or more components, or may have a different component configuration. The various components shown in the figure may be implemented in hardware, including one or more signal processing and / or application-specific integrated circuits, software, or a combination of hardware and software.
[0093] Furthermore, an operating system runs on the aforementioned hardware. The operating system at the operating system layer can be any one or more computer operating systems that implement business processing through processes, such as the Linux operating system, Unix operating system, Android operating system, iOS operating system, or Windows operating system. Applications can be installed and run on the operating system.
[0094] The operating system of the electronic device 100 can adopt a layered architecture, an event-driven architecture, a micro-kernel architecture, a micro-service architecture, or a cloud architecture. In the embodiment of the present application, the Android system with a layered architecture is used as an example to illustrate the software structure of the electronic device 100.
[0095] Figure 3This is a block diagram of the software structure of the electronic device 100 according to an embodiment of the present application. A layered architecture divides software into several layers, each with distinct roles and responsibilities. Layers communicate with each other via software interfaces. In some embodiments, the Android system is divided into four layers: the application layer (applications), the application framework layer (application framework), the native system layer (native), and the kernel layer (kernel).
[0096] Specifically for this application, each layer, including the kernel layer, system library layer, application framework layer, and application layer, can call the Socket interface. This application implements the data forwarding function between local application processes based on the Socket mechanism. The specific process will be described in detail below.
[0097] The application layer may include a series of application packages. For example, the application layer may include a camera application (application, APP), a gallery APP, a calendar APP, a map APP, a navigation APP, a music APP, a video APP, and other applications, as well as a data forwarding service program / module, a power consumption management program, a policy management program, etc., which are not limited in this embodiment of the present application.
[0098] The application framework layer provides an application programming interface (API) and programming framework for applications in the application layer. The application framework layer includes some predefined functions. In the embodiment of the present application, the application framework layer may include a subscription unit and a collection unit for application data. As operating systems become increasingly complex, a unified data bus is needed to meet the needs of each unit for collecting and subscribing to system messages.
[0099] The application layer and application framework layer run in a virtual machine. The virtual machine executes Java files in the application layer and application framework layer as binary files. The virtual machine manages object lifecycles, stack management, thread management, security and exception management, and garbage collection.
[0100] The system library can include multiple functional modules. For example: surface manager, media library, 3D graphics processing library (for example: OpenGL ES), 2D graphics engine (for example: SGL), etc. The system library layer can also be called the native layer (Native layer), which is a native code platform library that can provide native services for hardware operations for the upper layer. In an embodiment of the present application, the system library includes a data bus and an adapter library. The data bus includes a collector, a distributor, a cache management, a pipeline control, a socket interface, etc.
[0101] The kernel layer is the layer between hardware and software. The kernel layer includes at least a display driver, a camera driver, an audio driver, and a sensor driver. In an embodiment of the present application, the kernel layer includes a data cache module (DC), a Socket kernel driver, a CPU data acquisition module, an I / O data acquisition module, a memory data acquisition module, etc. Among them, the data cache module and the cache driver are newly added modules in the existing software architecture of the present application. The present application is based on the Socket mechanism and adopts the data cache module and the cache driver to realize the data forwarding service between local processes, which reduces the number of data copies and supports the modification of the content of the forwarded data. The specific inter-process communication process will be described in detail below.
[0102] It should be noted that although the embodiments of the present application are described using the Android system as an example, its basic principles are also applicable to electronic devices based on operating systems such as iOS or Windows.
[0103] The execution subject of the Socket-based data transmission method provided in the embodiment of the present application can be the above-mentioned electronic device, or it can be a functional module and / or functional entity in the electronic device that can implement the Socket-based data transmission method. The present application scheme can be implemented through hardware and / or software, and the specific implementation can be determined according to actual use requirements and is not limited by the embodiment of the present application. The following uses an electronic device as an example and combines the accompanying drawings to illustrate the Socket-based data transmission method provided in the embodiment of the present application.
[0104] The following will be combined with the accompanying drawings to illustrate the embodiments of the present application through the following exemplary embodiments. The methods in the following embodiments can be implemented in an electronic device with the above hardware structure and software architecture. The hardware structure diagram of the electronic device can be as follows: Figure 2 As shown, the software structure diagram of the electronic device can be as follows Figure 3 For the sake of convenience, the embodiments of the present application are all based on electronic devices. Figure 2 Take the mobile phone shown as an example.
[0105] Figure 4A A schematic diagram showing the improvements of the present application relative to the related art is shown.
[0106] In the Socket communication architecture of related technologies, the user space includes application A (or the first application), data forwarding service, and application B (or the second application); the kernel space includes the Socket send buffer and the Socket receive buffer; the user space interacts with the kernel space by calling the Socket interface. When electronic devices use this traditional Socket communication architecture to forward data, four data copies will occur during a single data transmission process. For details, please refer to the above combined Figure 1B The high number of data copies severely impacts data transmission efficiency. Furthermore, during data forwarding, data modification and deletion in user space are not supported.
[0107] In the Socket communication architecture of this application, the user space includes application A, data forwarding service, and application B; the kernel space includes the Socket kernel driver and data cache module; the user space interacts with the kernel space by calling the Socket interface. When an electronic device adopts the Socket communication architecture provided by this application to forward data, a single data transmission process only requires two data copies. For details, please refer to the above combined Figure 1B Therefore, the present application solution can reduce the number of data copies, improve data transmission efficiency, and enhance user experience. In addition, the present application solution allows the data forwarding service module to modify and delete data to be forwarded in the user space during data forwarding. This will be described in detail below.
[0108] Figure 4B This is a schematic diagram of inter-application process communication within an electronic device provided by an embodiment of the present application. Figure 4B As shown, the present application solution adds a data cache module to the kernel space and establishes a custom protocol. Based on this custom protocol, the data cache module is shared to the user space via memory mapping (Mmap), and the data forwarding service module in the user space is allowed to modify the content of the forwarded data. In addition, the present application solution also adds a socket kernel driver that meets the custom protocol in the kernel space, and triggers the data cache module to cache data through this socket kernel driver.
[0109] Reference below Figure 4B The flow shown by the arrows is explained below. The sender (application A), data forwarding service module, and receiver (application B) are all located in user space, while the socket kernel driver and data cache module are located in kernel space.
[0110] 1. The sender (application A) calls the Socket interface to encapsulate the data to be sent and then passes it into kernel space. The data is encapsulated into the first msg structure.
[0111] The Socket kernel driver in the kernel space receives the encapsulated data, that is, the first msg structure.
[0112] 2-1. The socket kernel driver parses the first msg structure to obtain the service data, then applies for a buffer area from the data buffer module and copies the parsed service data to the buffer area.
[0113] 2-2. The socket kernel driver encapsulates the cache index value corresponding to the cache area and then writes it into the receiving queue of the data forwarding service module. The cache index value is encapsulated into the second msg structure.
[0114] 3. The data forwarding service module reads the cache index value from the receiving queue of the data forwarding service module, and then can access the cache area of the data cache module according to the cache index value. When the data forwarding service module receives a modification instruction, the data forwarding service module can respond to the modification instruction and modify the data in the cache area.
[0115] 4-1. The data forwarding service module calls the Socket interface to encapsulate the cache index value and then transmits it to the kernel space. The cache index value is encapsulated as the third msg structure.
[0116] The Socket kernel driver in the kernel space receives the encapsulated data, that is, the third msg structure.
[0117] 4-2. The socket kernel driver parses the third msg structure and writes the parsed cache index value into the receiver's (application B) receive queue.
[0118] 5. The receiver (application B) sends a fourth msg structure to the socket kernel driver to receive data. The socket kernel driver reads the cache index value from the receiver's (application B's) receive queue and, based on the cache index value, accesses the corresponding buffer area in the data cache module. It copies the data stored in this buffer area to the fourth msg structure. The socket kernel driver then calls the socket interface to send the fourth msg structure to the receiver (application B).
[0119] Thus, inter-process communication between application programs within electronic devices is achieved based on Socket.
[0120] It should be noted that the msg structure is a nested structure pointer. Data is encapsulated into the msg structure according to the BSD specification, and its purpose is to implement communication between local application processes according to the BSD Socket specification.
[0121] For example, calling the msgget function will create a new message queue or get an existing message queue.
[0122] For example, when the msgsnd function is called to send a message of a specified size on a specified message queue, a msg_msg structure is created.
[0123] For example, call the alloc_msg function to allocate space and copy the user data to the kernel msg_msg structure.
[0124] The message queue is a communication mechanism in Linux that transmits structured data, rather than a simple byte stream. A message queue is essentially a linked list provided by the kernel, which implements a data structure based on this linked list. Message queues enable inter-process communication.
[0125] This application solution implements data forwarding service between local processes based on Socket mechanism. Figure 4B When forwarding data using the Socket communication method shown, a single data transmission process only requires two data copies. Therefore, the present application solution reduces the number of data copies and supports modification and deletion of the content of the forwarded data during the forwarding process.
[0126] The following describes the data transmission method based on Socket provided by the embodiments of the present application in conjunction with specific embodiments.
[0127] Figure 5 This is a schematic diagram of the interactive process of the data transmission method based on Socket provided in the embodiment of the present application. Figure 5 As shown, the method includes the following steps S201-S223. The modules involved in the interactive process include application A, socket sending interface, socket kernel driver, data cache module, data forwarding service module, socket receiving interface, and application B. For ease of explanation, the embodiment of this application uses application A as a video app and application B as a power management program as an example for illustrative purposes.
[0128] S201 : Application A (first application) detects a first operation of a user. In response to the operation, application A executes a first service.
[0129] In some embodiments, the first operation is a user's installation operation of the video APP, and the electronic device installs the video APP in response to the installation operation. In this case, the first service is installation of the video APP.
[0130] In some embodiments, the first operation is a user clicking on an application icon of the video APP, and the electronic device opens the video APP in response to the clicking operation. In this case, the first service is opening the video APP.
[0131] In some embodiments, the first operation is a user closing operation on the video APP, and the electronic device closes the video APP in response to the closing operation. In this case, the first service is closing the video APP.
[0132] In some embodiments, the first operation is an uninstallation operation of the video APP by the user, and the electronic device uninstalls the video APP in response to the uninstallation operation. In this case, the first service is uninstalling the video APP.
[0133] S202: Application A sends the first data to the Socket sending interface.
[0134] The first data refers to data related to the first business.
[0135] S203. The Socket sending interface encapsulates the first data into a first msg structure according to the BSD specification.
[0136] The BSD specification refers to the Berkeley Sockets specification, also known as the BSD Socket specification. The BSD Socket specification's application programming interface (API) is a C-language library for inter-process communication, commonly used for communication across computer networks. The BSD Socket specification's API has become an abstract standard for network sockets.
[0137] The first msg structure includes first data and complies with the BSD Socket specification.
[0138] S204. The Socket sending interface sends the first msg structure to the Socket kernel driver.
[0139] The Socket kernel driver parses the first msg structure to obtain the first data.
[0140] S205 . The socket kernel driver sends a message 1 to the data cache module. The message 1 is used to apply for a cache area.
[0141] S206 . The data cache module creates a first cache area in response to the message 1 , and returns a message 2 indicating a successful application to the Socket kernel driver. The message 2 carries index information of the first cache area.
[0142] The index information can be represented by a preset value. For ease of explanation, the index information of the cache area is referred to as a cache index value below.
[0143] In some embodiments, the socket kernel driver determines that the type of the first data is a first service type. The socket kernel driver then sends a request message to a data cache module, requesting a cache area to store the data of the first service type. The socket kernel driver then receives a response message from the data cache module, indicating allocation of the first cache area for the data of the first service type. The response message also includes a cache index value corresponding to the first cache area.
[0144] There is a mapping relationship among the first service type, the first cache area, and the cache index value.
[0145] The data of the first business type belongs to the business data requested to be collected by application B (second application).
[0146] Thus, the socket kernel driver can determine the corresponding first buffer area according to the buffer index value, and can also determine that the type of data stored in the first buffer area is the first service type. Moreover, the socket kernel driver can also determine that the data stored in the first buffer area needs to be sent to application B.
[0147] S207 : The socket kernel driver receives the message 2 indicating successful application, and copies the data in the first msg structure to the first buffer area corresponding to the buffer index value.
[0148] Correspondingly, the data cache module feeds back a message indicating that the data copy was successful to the Socket kernel driver.
[0149] S208 . The socket kernel driver writes the cache index value into the receiving queue of the data forwarding service module and sends a wake-up message to the data forwarding service module.
[0150] S209 , the data forwarding service module responds to the wake-up message, reads the cache index value from the receiving queue in sequence, and writes it into the second msg structure.
[0151] The second msg structure is identical to the first msg structure in structure but different in content.
[0152] Optionally, after the data forwarding service module writes the cache index value in the receiving queue into the second msg structure, the data forwarding service module may modify the first data in the first cache area. For specific processes, see S210 and S211 below.
[0153] S210. The data forwarding service module sends an instruction to modify the data in the first buffer area to the data buffer module. The instruction carries a second msg structure.
[0154] S211 : The data cache module modifies the data in the first cache area in response to the instruction, and then returns a modification completion message to the data forwarding service module.
[0155] S212: The data forwarding service module encapsulates the cache index value in the second msg structure into a third msg structure according to the BSD specification.
[0156] The third msg structure is identical to the second msg structure in structure, and may be identical or different in content.
[0157] S213: The data forwarding service module sends the third msg structure to the Socket kernel driver.
[0158] S214. The socket kernel driver parses the cache index value from the third msg structure according to the BSD specification.
[0159] The socket kernel driver may determine the corresponding first cache area based on the cache index value, and may also determine that the type of data stored in the first cache area is the first service type. If the data of the first service type is service data requested to be collected by application B (the second application), the socket kernel driver may also determine that the data stored in the first cache area needs to be sent to application B.
[0160] S215 , the Socket kernel driver calls the Socket receiving interface, writes the cache index value into the receiving queue of application B, and sends a wake-up message to application B.
[0161] S216 . Application B transmits a fourth msg structure to the Socket kernel driver in the kernel space in response to the wake-up message. The fourth msg structure is used to receive data.
[0162] S217 . The socket kernel driver requests data from the data cache module according to the cache index value read from the receiving queue of application B.
[0163] S218: The data cache module returns the first data in the first cache area corresponding to the cache index value to the Socket kernel driver.
[0164] In an embodiment of the present application, the socket kernel driver sends a data retrieval instruction to the data cache module, the instruction including a cache index value. In response to the instruction, the data cache module retrieves first data from a first cache area corresponding to the cache index value based on the cache index value, and sends the first data to the socket kernel driver.
[0165] S219. After receiving the first data, the Socket kernel driver copies the first data to the fourth msg structure.
[0166] The fourth msg structure may be the same as or different from the third msg structure in structure, and may be different in content.
[0167] S220. The Socket kernel driver calls the Socket receiving interface to send the fourth msg structure to application B.
[0168] S221. After receiving the fourth msg structure, application B parses the fourth msg structure to obtain the first data.
[0169] S222. The socket kernel driver sends a message 3 to the data cache module. The message 3 is used to request the release of the cache area corresponding to the cache index value.
[0170] S223. After receiving message 3, the data cache module releases the cache area corresponding to the cache index value, and then returns message 4 to the Socket kernel driver. Message 4 is used to indicate that the cache area corresponding to the cache index value has been released.
[0171] In related technologies, data forwarding between local processes is implemented using Sockets, resulting in four data copies per message transmission. However, in this application, data forwarding between local processes is implemented using Sockets, resulting in two data copies per message transmission. This application solves the problem of multiple copies in data forwarding and filtering using Sockets. This application designs a unique shared cache management mechanism and resolves compatibility issues with existing Socket data interfaces, improving the efficiency of data transmission between local processes using Sockets.
[0172] This application mainly implements a data forwarding service based on Socket, reduces the number of data copies, and supports modification / deletion of forwarded data. This application adds a data cache module in the kernel space, which is used to cache forwarded data, and the data cache module is shared to the user space through memory mapping (Mmap), and allows the service program (data forwarding service module) in the user space to modify the forwarded data. This application also adds a Socket kernel driver in the kernel space, and realizes reading and writing of the data cache module through the Socket kernel driver.
[0173] Below through Figure 6 and Figure 7 The sending and receiving are explained separately.
[0174] Figure 6 A schematic diagram of the process of sending data based on the Socket mechanism provided in an embodiment of the present application is shown.
[0175] S301-S303: The sender calls the Socket sending interface, and encapsulates the first data into a msg structure according to the BSD specification, and then transfers it to the kernel space.
[0176] The sender may be application A or a data forwarding service module.
[0177] In some embodiments, the sender is application A and the receiver is a data forwarding service module.
[0178] In other embodiments, the sender is a data forwarding service module and the receiver is application B.
[0179] That is to say, the process of transmitting data based on the Socket mechanism includes two stages: the first stage is the transmission from application A to the data forwarding service module, and the second stage is the transmission from the data forwarding service module to application B.
[0180] It should be noted that data needs to be specifically encapsulated at each stage to achieve Socket communication between different applications (ie, between different processes).
[0181] It should also be noted that the encapsulated data passes through the Socket send interface and reaches the Socket kernel driver in the kernel space, and then the Socket kernel driver completes data forwarding based on the encapsulated data.
[0182] S304: The socket kernel driver determines whether the receiver is a data forwarding service module.
[0183] On the one hand, the socket kernel driver determines that the recipient is the data forwarding service module. In this case, the process continues with steps S305 to S308, and data transmission is performed according to the following socket transmission path: the socket kernel driver uses the data cache module to cache the new data from application A, adds the cache index corresponding to the new data to the receive queue of the data forwarding service module in user space, and notifies the data forwarding service module of the arrival of new data. This sending operation requires a data copy.
[0184] On the other hand, the socket kernel driver determines that the recipient is not the data forwarding service module. In this case, the socket kernel driver continues to execute S309, where it determines whether the sender is the data forwarding service module. If the socket kernel driver determines that the sender is the data forwarding service module and the recipient is application B, data transmission proceeds according to the following socket transmission path: the data forwarding service module calls the socket kernel driver, writes the cache index value corresponding to the buffer area for storing new data into application B's receive queue, and notifies application B to receive the new data.
[0185] S309: The socket kernel driver determines whether the sender is the data forwarding service module. If the socket kernel driver determines that the sender is the data forwarding service module, no data copy is required for this send operation. The socket kernel driver then continues with S310 and S307-S308, transmitting data according to the following socket transmission path: The data forwarding service module calls the socket kernel driver and notifies application B to receive the data. If the socket kernel driver determines that the sender is not the data forwarding service module, the socket kernel driver continues with S311, in which the socket kernel driver returns a message to user space indicating that the data transmission failed.
[0186] The following describes the first sending process of data transmission based on the Socket mechanism provided by the embodiment of the present application:
[0187] In the first sending process, application A is the sender and the data forwarding service module is the receiver. Application A sends first data, which includes data related to the first service. The following is an exemplary description with reference to S301-S308.
[0188] First, user-space application A encapsulates the data to be sent into a first msg structure according to the BSD specification, invokes the kernel-space socket kernel driver, and sends the first msg structure to the kernel space. The kernel-space socket kernel driver receives the first msg structure and parses it to obtain the first data.
[0189] The socket kernel driver determines that the first data needs to be forwarded by the data forwarding service module. The data forwarding service module enters the receiving phase, in which case data caching is required. This data caching process involves a data copy operation, and the socket kernel driver needs to copy the transmitted data to the data cache module for caching. The socket kernel driver requests a cache area from the data cache module and uses this cache area to cache the first data sent by application A. Furthermore, the socket kernel driver records the cache index value corresponding to the cache area in the data forwarding service module's receiving queue.
[0190] For example, the Socket kernel driver applies for the first cache area from the data cache module, and then the Socket kernel driver copies the first data in the first msg structure sent by application A to the first cache area of the data cache module, and the Socket kernel driver records the cache index value corresponding to the first cache area to the receiving queue of the data forwarding service module, and then the Socket kernel driver notifies the data forwarding service module that new data has arrived.
[0191] It should be noted that in the present application, in kernel space, the first data is stored in the first cache area of the data cache module; in user space, the data forwarding service module stores the corresponding cache index value of the first cache area. In other words, a mapping relationship exists between the data cache module and the data forwarding service module. In actual use, the data forwarding service module can access the first data stored in the first cache area corresponding to the cache index value in the data cache module based on the cache index value.
[0192] After the data forwarding service module receives the new data notification, the data forwarding service module can read the cache index value from the data forwarding service module's receive queue and, based on the cache index value, access the first cache area corresponding to the cache index value in the data cache module in the kernel space. Furthermore, if the data forwarding service module receives a modification / deletion instruction, the data forwarding service module can modify / delete the first data stored in the first cache area in response to the modification / deletion instruction.
[0193] Steps S301-S308 above illustrate the first transmission process of the socket transmission path from application A to the data forwarding service module. Application A encapsulates the data to be sent into a first msg structure according to the BSD specification, calls the socket interface, and transfers the first msg structure into kernel space, where it reaches the socket kernel driver. The socket kernel driver then requests a first buffer area from the data cache module, uses the first buffer area to cache the first data from application A, and records the cache index value corresponding to the first buffer area in the data forwarding service module's receive queue.
[0194] The following describes the second sending process for transmitting data based on the Socket mechanism provided in an embodiment of the present application:
[0195] In the second sending process, the data forwarding service module is the sender and application B is the receiver. The data forwarding service module sends the second data, which includes the cache index value. The following is an exemplary description of S301-S304, S309-S310, and S307-S308.
[0196] First, the data forwarding service module reads a cache index value from a receive queue of the data forwarding service module. Based on the cache index value, the data forwarding service module can access a first cache area in the data cache module corresponding to the cache index value. Furthermore, if the data forwarding service module receives a modification / deletion instruction, the data forwarding service module can modify / delete the first data stored in the first cache area in response to the modification / deletion instruction.
[0197] Then, the data forwarding service module encapsulates the cache index value into the third msg structure according to the BSD specification, and then the data forwarding service module calls the Socket interface to pass the third msg structure into the kernel space to reach the Socket kernel driver.
[0198] Then, the Socket kernel driver parses the third msg structure to obtain a cache index value, and the Socket kernel driver writes the cache index value into the receiving queue of application B, and then notifies application B to receive new data.
[0199] Specifically, the socket kernel driver determines that the data forwarding service module will forward the data. Accordingly, the socket kernel driver parses all cache index values from the msg structure sent by the data forwarding service module according to a preset specification, records the cache index values in application B's receive queue, and notifies application B of the arrival of new data. In other words, the socket kernel driver notifies application B of the new data. This notification process does not involve any data copying.
[0200] The above steps S301-S304, S309-S3010, and S307-S3008 illustrate the sending process of the Socket transmission path from the data forwarding service module to application B. Through the solution of this application, the data forwarding service module encapsulates the cache index value in the receive queue into a third msg structure in accordance with the BSD specification, and calls the Socket interface to pass the third msg structure into the kernel space. Among them, the third msg structure passes through the Socket interface and reaches the Socket kernel driver in the kernel space. The Socket kernel driver parses the third msg structure to obtain the cache index value, and based on the cache index value, accesses the cache area corresponding to the cache index value in the data cache module, writes the cache index value into the receive queue of application B, and notifies application B to receive data.
[0201] In addition, there is also the case where data transmission fails in S311. Specifically, the socket kernel driver determines that the data forwarding service module is neither the receiver nor the sender, and thus deems this data transmission an abnormal transmission and does not respond. In some embodiments, the socket kernel driver returns the EINVAL error code to user space. The EINVAL error code indicates that data cannot be forwarded or that data transmission failed.
[0202] Figure 7 A schematic diagram of the process of receiving data based on the Socket mechanism provided in an embodiment of the present application is shown.
[0203] S401-S402: The receiver calls the Socket receiving interface to transfer the msg structure used to receive data to the Socket kernel driver in the kernel space.
[0204] The receiver may be a data forwarding service module or application B.
[0205] In some embodiments, the receiver is a data forwarding service module, and the corresponding sender is application A.
[0206] In other embodiments, the receiver is application B, and the corresponding sender is a data forwarding service module.
[0207] S403: The socket kernel driver writes the new data (cache index value) into the receiver's receiving queue (ie, the new data is queued) according to the blocking waiting mechanism.
[0208] S404: The socket kernel driver reads the cache index values from the receiver's receive queue in sequence.
[0209] S405: The socket kernel driver determines whether the receiver is a data forwarding service module.
[0210] On the one hand, if the socket kernel driver determines that the recipient is the data forwarding service module, no data copy is required for this reception operation, and the process continues with step S406. Data transmission is performed according to the following socket transmission path: the socket kernel driver writes the cache index value read from the data forwarding service module's receive queue into the second msg structure according to a preset specification (e.g., the BSD specification), and returns it to the data forwarding service module in the user space.
[0211] On the other hand, if the socket kernel driver determines that the recipient is not the data forwarding service module, this receive operation requires data copying, and the process continues with step S407, where the socket kernel driver further determines whether the sender is the data forwarding service module. Specifically, if the socket kernel driver determines that the sender is the data forwarding service module (and the recipient is application B), data transmission proceeds according to the following socket transmission path (see steps S408 and S409): the socket kernel driver sequentially copies the data in the data cache module into the fourth msg structure based on the read cache index value, then releases the read cache area of the data cache module and returns the fourth msg structure to application B in user space.
[0212] The following describes the first receiving process of data transmission based on the Socket mechanism provided by the embodiment of the present application:
[0213] In the first receiving process, the data forwarding service module is the receiver, and the data forwarding service module receives the cache index value.
[0214] After the Socket kernel driver in the kernel space receives the first data sent from the user space, the Socket kernel driver caches the first data in the first data into the first cache area in the data cache module; at the same time, the Socket kernel driver writes the cache index value corresponding to the first cache area into the receiving queue of the data forwarding service module, and the Socket kernel driver sends a new data notification to the data forwarding service module.
[0215] The data forwarding service module receives the new data notification, and then the data forwarding service module calls the Socket receiving interface to transmit the second msg structure for receiving data to the Socket kernel driver in the kernel space.
[0216] The socket kernel driver receives the second msg structure sent by the data forwarding service module. The socket kernel driver then sequentially parses the cache index value from the data forwarding service module's receive queue and determines whether the recipient is the data forwarding service module.
[0217] In the case where the receiver is a data forwarding service module, no data copy is required for this receiving operation.
[0218] The socket kernel driver directly writes the read cache index value into the second msg structure according to a preset specification (for example, according to the BSD specification), and then returns the second msg structure to the data forwarding service module in the user space.
[0219] It should be noted that the data forwarding service module can obtain the cache index value list through the second msg structure.
[0220] The following describes the second receiving process for transmitting data based on the Socket mechanism provided in the embodiment of the present application:
[0221] In the second receiving process, the sender is the data forwarding service module and the receiver is application B. Application B receives the first data.
[0222] First, the data forwarding service module in the user space receives a new data notification, and then reads the data from the receiving queue of the data forwarding service module in sequence and parses it to obtain a cache index value.
[0223] In some embodiments, if the data forwarding service module receives a modification instruction and the modification instruction is bound to the cache index value 1, the data forwarding service module can access the cache area 1 corresponding to the cache index value 1 in the data cache module based on the cache index value 1, and modify the business data stored in the cache area 1 according to the modification instruction.
[0224] In other embodiments, if the data forwarding service module receives a deletion instruction and the deletion instruction is bound to the cache index value 2, the data forwarding service module can access the cache area 2 corresponding to the cache index value 2 in the data cache module based on the cache index value 2, and delete the business data stored in the cache area 2 according to the deletion instruction.
[0225] Then, the data forwarding service module encapsulates the cache index value into the third msg structure, and sends the third msg structure to the Socket kernel driver in the kernel space.
[0226] After the Socket kernel driver in the kernel space receives the data sent by the user space, the Socket kernel driver parses the third msg structure to obtain a cache index value.
[0227] Then, the Socket kernel driver writes the cache index value into the receiving queue of application B according to the blocking wait mechanism, and the Socket kernel driver sends a new data notification to application B.
[0228] Application B receives the new data notification, and then application B calls the Socket receiving interface to transmit the fourth msg structure used to receive data to the Socket kernel driver in the kernel space.
[0229] The socket kernel driver receives the fourth msg structure sent by application B. The socket kernel driver then sequentially parses the cache index values from the receive queue of application B. The socket kernel driver determines whether the sender is a data forwarding service module.
[0230] In the case where the sender is a data forwarding service module, this receiving operation requires data copying.
[0231] The socket kernel driver accesses the data cache module according to the read cache index value, copies the data stored in the cache area corresponding to the cache index value to the fourth msg structure, then releases the accessed cache area of the data cache module, and returns the fourth msg structure to the application B in the user space.
[0232] Thus, application B can obtain real business data through the fourth msg structure.
[0233] In addition, there is also the case of data transmission failure in S410. Specifically, after the Socket kernel driver in the kernel space receives the data sent by the user space, the Socket kernel driver determines that the identity of the data forwarding service module is neither the receiver nor the sender, and determines that this data transmission is an abnormal transmission and does not respond. In some embodiments, the Socket kernel driver returns the EINVAL error code to the user space. The EINVAL error code is used to indicate that the data sent by the user space does not support forwarding and the data transmission failed.
[0234] The Socket-based data transmission method provided by the embodiment of the present application only requires two data copies, which is less than the four data copies required by the related art. Furthermore, compared to the related art that does not support modification or deletion of forwarded data in user space, the present application adds a data cache module to the kernel space for caching forwarded data, and establishes a mapping relationship between the data cache module and the data forwarding service module in the user space, so that the kernel space and the user space can share data through the data cache module, and support access and modification of the forwarded data cached in the kernel space data cache module in the user space, thereby achieving the purpose of modifying or deleting the forwarded data in the user space.
[0235] The above embodiments describe in detail the data transmission method based on Socket provided by the present application. The data cache module mentioned in the present application is described below.
[0236] This application adds a data cache module in the kernel space, and the kernel space and user space can share data through the data cache module. In the embodiment of this application, the entire shared memory is divided into two parts, namely the node area and the data area.
[0237] For example, Figure 8 The node area and the data area are shown. The node area can be called the control area.
[0238] The node area contains all control information, such as node status, data block allocation size, data block used size, node number, associated node and data block real address. The node area includes N nodes. N is a positive integer. For example, Figure 8 8 nodes in the node area are shown, namely node 1, node 2, ..., node 8. Each node serves as a control block.
[0239] There is a directional relationship between the associated nodes in the node area. For example, the next node of node 1 is node 2. The next node of node 3 is node 5, and the next node of node 5 is node 6. The next node of node 4 is node 8, and the next node of node 8 is node 7.
[0240] The data area is the area where data is actually stored. In this application, it is divided into N blocks of the same size according to the set size. For example, Figure 8 Eight data blocks in the data area are shown, namely data block 1, data block 2, ..., data block 8.
[0241] It should be noted that the N data blocks in the data area correspond to the N nodes in the node area in a one-to-one manner according to the sequence numbers, so as to separate the data blocks from the control blocks.
[0242] The first cache area described in the above embodiment corresponds to a cache set, which includes a control block and a corresponding data block. The first cache index value described in the above embodiment corresponds to the number or sequence number of the data block.
[0243] Figure 9 The characteristic structure of any node in the node area and the schematic diagram of the corresponding data block are shown. For the convenience of explanation and understanding, the following exemplary description is given by taking node 1 and data block 1 as an example.
[0244] like Figure 9 As shown, the characteristic structure of node 1 includes node state Atomic state, data block allocation size u32real_size, used size u32 used_size, current node number u32 curr_index, next node number u32next_index, occupied service number u32 ref_count, and data block real address void*buff_ptr.
[0245] According to the Atomic state, it can be determined whether node 1 is occupied. For example, if Atomic state = 1, it means that node 1 is occupied. If Atomic state = 0, it means that node 1 is not occupied.
[0246] The data block allocation size u32 real_size refers to the size of the initially allocated available storage space for data block 1 corresponding to node 1. For example, u32 real_size = 512 MB, which means that the actual available storage area size of data block 1 is 512 MB.
[0247] The used size u32 used_size refers to the size of the storage space occupied by data block 1 corresponding to node 1. For example, u32 used_size = 100MB, which means that data block 1 has occupied 100MB of storage space.
[0248] The current node number u32 curr_index refers to the number of the current node. For example, u32 curr_index = 1 means that the current node number is 1.
[0249] The next node number u32 next_index refers to the number of the next node of the current node. For example, u32 next_index = 2 means that the next node of node 1 is node 2, where nodes 1 and 2 are associated nodes.
[0250] The number of occupied services, u32 ref_count, refers to the number of services occupied by node 1 when it is occupied by one or more services. For example, u32 ref_count = 2 indicates that node 1 is occupied by two services. The specific value of u32 ref_count increases as forwarding services are created and decreases as forwarding services are completed. For example, if one of the two services completes data forwarding, the storage space occupied by that service is released, and the corresponding value is updated from u32 ref_count = 2 to u32ref_count = 1.
[0251] The data block real address void*buff_ptr refers to the real address in data block 1 corresponding to node 1.
[0252] The embodiment of the present application proposes a shared cache management mechanism. In the embodiment of the present application, each node of the data cache module can include the following four states: idle, kernel space read and write, waiting, and user space read and write. Figure 10 During data forwarding, the state of each node switches between these four states. The node state is maintained by the Socket kernel driver and the data forwarding service module.
[0253] For example, in some embodiments, during the data forwarding process, state switching is performed as follows:
[0254] First, the data cache module is in the "idle" state after initialization, waiting for allocation.
[0255] Then, when the Socket kernel driver receives data, it updates the node state of the data cache module to the "kernel space read and write" state.
[0256] Then, after the Socket kernel driver completes the data copy, the node state of the data cache module is updated to the "waiting" state. At this time, the data cache module is in a state where there is data waiting to be processed.
[0257] Then, when the data forwarding service module performs reading and writing, the node state of the data cache module is updated to the "user space reading and writing" state.
[0258] Then, after the data forwarding service module completes reading and writing, the node state of the data cache module is updated to a "waiting" or "idle" state again.
[0259] Then, when the Socket kernel driver performs reading and writing, the node state of the data cache module is updated to the "kernel space reading and writing" state again.
[0260] When all processing is completed, all occupied cache space of the data cache module is released, and the node status of the data cache module is updated to the "idle" state.
[0261] In an embodiment of the present application, based on the Socket mechanism, the original data cache sk_buff is replaced with a newly designed shared cache, namely, a data cache module (data cache), and the present application proposes a shared cache management mechanism that can efficiently handle concurrent read and write accesses between kernel space and user space, reducing the number of data copies and greatly improving the efficiency of data forwarding based on the Socket mechanism.
[0262] It should be noted that, in the embodiments of the present application, "greater than" can be replaced by "greater than or equal to", "less than or equal to" can be replaced by "less than", or "greater than or equal to" can be replaced by "greater than", and "less than" can be replaced by "less than or equal to".
[0263] The various embodiments described herein may be independent solutions or may be combined according to internal logic, and all of these solutions fall within the scope of protection of this application.
[0264] The above mainly describes the solution provided by the embodiment of the present application from the perspective of method steps. It is understandable that, in order to achieve the above functions, the electronic device implementing the method includes hardware structures and / or software modules corresponding to the execution of each function. Those skilled in the art should be aware that, in combination with the units and algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in a hardware or computer software driven hardware manner depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of protection of this application.
[0265] In the embodiment of the present application, the electronic device can be divided into functional modules according to the above method examples. For example, each functional module can be divided according to each function, or two or more functions can be integrated into one processing module. The above-mentioned integrated modules can be implemented in the form of hardware or in the form of software functional modules. It should be noted that the division of modules in the embodiment of the present application is schematic and is only a logical function division. In actual implementation, there may be other feasible division methods.
[0266] The present application also provides a chip, which is coupled to a memory and is used to read and execute computer programs or instructions stored in the memory to perform the methods in the above embodiments.
[0267] The present application also provides an electronic device, which includes a chip, and the chip is used to read and execute computer programs or instructions stored in a memory, so that the methods in each embodiment are executed.
[0268] This embodiment also provides a computer-readable storage medium, which stores computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the above-mentioned related method steps to implement the Socket-based data transmission method in the above embodiment.
[0269] This embodiment further provides a computer program product, wherein the computer-readable storage medium stores program code. When the computer program product is run on a computer, the computer is caused to execute the above-mentioned related steps to implement the Socket-based data transmission method in the above-mentioned embodiment.
[0270] In addition, an embodiment of the present application also provides a device, which can specifically be a chip, component or module, and the device may include a connected processor and memory; wherein the memory is used to store computer-executable instructions, and when the device is running, the processor can execute the computer-executable instructions stored in the memory to enable the chip to execute the Socket-based data transmission method in the above-mentioned method embodiments.
[0271] Among them, the electronic device, computer-readable storage medium, computer program product or chip provided in this embodiment are all used to execute the corresponding methods provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding methods provided above, and will not be repeated here.
[0272] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of modules or units is only a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0273] The term "and / or" as used herein describes an association between related objects, indicating that three possible relationships exist. For example, "A and / or B" can represent: A exists alone, A and B exist simultaneously, or B exists alone. The symbol " / " as used herein indicates that the related objects are in an "or" relationship, for example, A / B means either A or B.
[0274] In the specification and claims herein, the terms "first" and "second" are used to distinguish different objects, rather than to describe a specific order of objects. In the description of the embodiments of this application, unless otherwise specified, the meaning of "plurality" refers to two or more. For example, "multiple processing units" refers to two or more processing units, etc.; "multiple components" refers to two or more components, etc.
[0275] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
Claims
1. A data transmission method based on Socket, applied to electronic equipment, characterized in that: The electronic device includes a Socket interface, a Socket kernel driver and a data cache module located in a kernel space, and a data forwarding service module located in a user space. The method includes: The Socket kernel driver receives first data from a first application of the electronic device; The Socket kernel driver copies the first data to the first buffer area of the data buffer module; The Socket kernel driver writes the first cache index value corresponding to the first cache area into the receiving queue of the data forwarding service module; The Socket kernel driver receives second data sent by the data forwarding service module, where the second data is obtained based on the first cache index value; The Socket kernel driver parses the second data to obtain the first cache index value, and writes the first cache index value into a receiving queue of a second application of the electronic device; The Socket kernel driver copies the first data from the first cache area based on the first cache index value, and transmits the first data to the second application.
2. The method according to claim 1, characterized in that The receiving queue of the data forwarding service module includes cache index values corresponding to respective cache areas of the data cache module.
3. The method according to claim 1, characterized in that After the Socket kernel driver receives the first data from the first application and before the Socket kernel driver copies the first data to the first buffer area of the data buffer module, the method further includes: The Socket kernel driver determines that the type of the first data is a first service type; The Socket kernel driver sends a request message to the data cache module, wherein the request message is used to request a cache area to store data of the first service type; The Socket kernel driver receives a response message from the data cache module, where the response message is used to indicate allocation of the first cache area for data of the first service type, and the response message also includes the first cache index value corresponding to the first cache area.
4. The method according to claim 3, characterized in that There is a mapping relationship between the first service type, the first cache area, and the first cache index value.
5. The method according to claim 3, characterized in that The data of the first business type belongs to the business data requested to be collected by the second application.
6. The method according to any one of claims 1 to 5, characterized in that Before the Socket kernel driver receives the first data from the first application, the method further includes: In response to a user operation, the first application executes a first service and generates first data; The first application calls the Socket interface to transmit the first data to the Socket kernel driver.
7. The method according to claim 6, characterized in that The first application calls the Socket interface to transmit the first data to the Socket kernel driver, including: The first application calls the Socket interface, encapsulates the first data into a first message structure according to a preset specification, and transmits the first message structure to the Socket kernel driver.
8. The method according to any one of claims 1 to 5, characterized in that After the Socket kernel driver writes the first cache index value corresponding to the first cache area into the receiving queue of the data forwarding service module, the method further includes: The Socket kernel driver sequentially reads cache index values from the receiving queue of the data forwarding service module, encapsulates the read cache index values into a second message structure, and transmits the second message structure to the data forwarding service module.
9. The method according to claim 8, characterized in that The method further comprises: The data forwarding service module receives a data modification instruction; In response to the data modification instruction, the data forwarding service module accesses the first cache area in the data cache module based on the first cache index value, and modifies the data in the first cache area based on the data modification instruction.
10. The method according to any one of claims 1 to 5, characterized in that Before the Socket kernel driver receives the second data sent by the data forwarding service module, the method further includes: The data forwarding service module calls the Socket interface, encapsulates the first cache index value into a third message structure according to a preset specification, and transmits the third message structure as the second data to the Socket kernel driver.
11. The method according to any one of claims 1 to 5, characterized in that The method further comprises: In a case where the Socket kernel driver writes the first cache index value into the receiving queue of the second application, the Socket kernel driver sends a new data notification to the second application; The Socket kernel driver receives a fourth message structure sent by the second application for receiving new data.
12. The method according to claim 11, characterized in that The Socket kernel driver copies the first data from the first cache area based on the first cache index value and transmits the first data to the second application, including: The Socket kernel driver accesses the first buffer area based on the first buffer index value, and copies the first data in the first buffer area to the fourth message structure; The Socket kernel driver calls the Socket interface to transmit the fourth message structure to the second application.
13. The method according to any one of claims 1 to 5, characterized in that The data cache module includes a control area and a data area, the control area includes N control blocks, the data area includes N data blocks, the N control blocks correspond to the N data blocks on a one-to-one basis, and some of the N control blocks have an associated relationship; The first cache area corresponds to a cache set, the cache set includes a control block and a corresponding data block, and the first cache index value corresponds to the number of the data block.
14. The method according to any one of claims 1 to 5, characterized in that The state of the data cache module is one of the following states: idle state, kernel space read / write state, waiting state, user space read / write state; The method further includes: the Socket kernel driver controls the data cache module to switch states between the various states.
15. The method according to claim 14, characterized in that The Socket kernel driver controls the data cache module to switch between the various states, including: The data cache module is in the idle state after initialization, waiting for allocation; After the Socket core driver copies the data to the data cache module, the Socket core driver updates the state of the data cache module to the waiting state; When the data forwarding service module reads and writes to the data cache module, the Socket kernel driver updates the state of the data cache module to the user space read and write state; After the data forwarding service module completes reading and writing to the data cache module, the Socket kernel driver updates the state of the data cache module to a waiting or idle state again; When the Socket kernel driver copies data from the data cache module, the state of the data cache module is updated to the kernel space read-write state again; After the Socket kernel driver finishes copying the data from the data cache module, it releases the occupied cache area in the data cache module and updates the state of the data cache module to an idle state.
16. An electronic device, characterized in that: The electronic device includes: one or more processors, and a memory; The memory is coupled to the one or more processors, and the memory is used to store computer program code, where the computer program code includes computer instructions. The one or more processors call the computer instructions to enable the electronic device to execute the method according to any one of claims 1 to 15.
17. A chip system, characterized in that: The chip system is applied to an electronic device, and the chip system includes one or more processors, and the one or more processors are used to call computer instructions so that the electronic device executes the method as described in any one of claims 1 to 15.
18. A computer-readable storage medium, characterized in that The computer-readable storage medium comprises instructions, which, when executed on an electronic device, cause the electronic device to perform the method according to any one of claims 1 to 15.
Citation Information
Patent Citations
Distributed system supported transparent interprocess communication system and method
CN105426260A
NFS server, data writing method and device thereof and storage medium
CN111881104A
Data storage method and device, equipment and medium
CN112346647A
Data transmission method and device, electronic equipment and medium
CN114968535A
Data caching method and chip
CN115086706A