Data transmission method and apparatus, electronic device, and storage medium
By maintaining record tables and resource pools in both user processes and kernel mode, and using data identifiers and reference counts to determine data transfer requirements, the system overhead problem when user processes pass data to kernel processes on the Linux platform is solved, achieving more efficient data transfer.
Patent Information
- Application Number
- CN202410678068.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-29
- Publication Date
- 2026-01-27
- Estimated Expiration
- 2044-05-29
AI Technical Summary
On the Linux platform, when user processes pass data to kernel processes, existing technologies suffer from significant system overhead, especially in scenarios involving large amounts of data and frequent data interactions, which can negatively impact program performance.
By maintaining record tables and resource pools in user processes and kernel mode, data identifiers and reference counts are used to determine whether data needs to be transmitted, reducing data comparisons. The MD5 algorithm is used to calculate data identifiers, and red-black trees are used to store kernel mode data, thus optimizing the data transmission process.
It reduces system overhead, minimizes redundant data transmission, and improves data transmission efficiency and system performance.
Smart Images

Figure CN118567879B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a data transmission method, apparatus, electronic device, and storage medium. Background Technology
[0002] For Linux platform program development, there are two main methods for user processes to pass data to kernel processes: one is through system calls, and the other is through shared memory.
[0003] The system call method involves using system call interfaces to switch between user mode and kernel mode, and then using specific system call functions to transfer data from user space to kernel mode. In essence, this is a copying process from user space memory to kernel space memory. For scenarios with large amounts of data and frequent data interactions, this can result in significant system overhead, severely impacting program performance. Summary of the Invention
[0004] The purpose of this application is to provide a data transmission method, apparatus, electronic device, and storage medium to reduce system overhead when transmitting data between user mode and kernel mode.
[0005] In a first aspect, embodiments of this application provide a data transmission method, including:
[0006] The first data identifier is obtained by calculating the data through the user process;
[0007] If the first data identifier does not exist in the record table corresponding to the user process, then the first data identifier and the pointer corresponding to the data are sent to the kernel mode; wherein, the pointer is used to represent the location information of the data storage;
[0008] The kernel-mode resource pool is queried to see if it contains the first data identifier. If the kernel-mode resource pool contains the first data identifier, then it is not necessary to transfer the data in the user process to the kernel mode. If the kernel-mode resource pool does not contain the first data identifier, then the data is obtained from the user process according to the pointer and transferred to the kernel mode.
[0009] This application maintains a record table in the user process to record which data has been sent from user mode to kernel mode, thereby determining whether data needs to be sent to kernel mode again. Additionally, data is also maintained in kernel mode. For a specific piece of data, if user mode has not previously sent the data to kernel mode, but a first data identifier indicates that the data already exists in kernel mode, then user mode does not need to send the data again. Therefore, when determining whether data exists in kernel mode, this application uses a first data identifier comparison method instead of a data comparison method, reducing system overhead. Furthermore, reducing the transmission of duplicate data further reduces system overhead.
[0010] In any embodiment, if the resource pool contains the first data identifier, the method further includes:
[0011] Obtain the second data identifier and the first reference count value corresponding to the first data identifier in the kernel state; wherein, the second data identifier is used to uniquely represent the data;
[0012] Update the first reference count value and return the second data identifier to the user process;
[0013] The user process stores the second data identifier and the first data identifier in the record table.
[0014] In this embodiment of the application, if the first data identifier is not present in the record table but is present in the resource pool, it means that the data already exists in the kernel mode and there is no need to transmit the data again. In order to facilitate data management, the reference count value of the data is maintained in both the resource pool and the record table. On the one hand, this facilitates monitoring the processing status of each data and on the other hand, it avoids repeated processing of the data.
[0015] In any embodiment, the method further includes:
[0016] If the first data identifier exists in the record table corresponding to the user process, then the second data identifier corresponding to the first data identifier is obtained from the record table;
[0017] Send the second data identifier to the kernel mode;
[0018] The corresponding first reference count value is obtained from the resource pool in the kernel state according to the second data identifier;
[0019] Update the first reference count value.
[0020] In this embodiment of the application, when the first data identifier exists in the record table, it is not necessary to send the data to the kernel state. Instead, the second data identifier is sent to the kernel state, and the kernel state updates the first reference count value to indicate that the data is to be processed.
[0021] In any embodiment, the kernel-mode resource pool includes a red-black tree; the step of transferring the data to the kernel-mode includes:
[0022] Generate a second data identifier corresponding to the data;
[0023] The data, along with the corresponding first data identifier, second data identifier, and first reference count value, are stored in the red-black tree.
[0024] In this embodiment of the application, since the kernel mode includes multiple processes, the same process may use the same data twice. Since the data is stored in the red-black tree with a first data identifier, a second data identifier, and a first reference count value, the second data identifier of the data to be used twice can be compared instead of the content of the data, thus reducing the system overhead.
[0025] In any embodiment, the method further includes:
[0026] If a kernel process in the kernel mode uses the data, the first reference count value of the data is updated.
[0027] This application embodiment can monitor the usage of data in kernel mode by updating the first reference count value.
[0028] In any embodiment, the user process calculates the first data identifier of the obtained data, including:
[0029] The user process uses the MD5 algorithm to calculate the first data identifier of the data.
[0030] This application embodiment calculates the first data identifier of the data using the MD5 algorithm. This first data identifier can be used to determine whether the user process has sent the data to the kernel space from the record table, and can also be used to determine whether the data exists in the kernel space from the resource pool. This determines whether the data needs to be sent to the kernel space, reducing redundant data transmission.
[0031] In any embodiment, the record table includes a second reference count value; the second reference count value is used to characterize the number of times the corresponding data needs to be processed; the method further includes:
[0032] If the amount of data recorded in the record table is greater than a preset value, then data to be removed is obtained from the record table based on the least recently used strategy; the data to be removed includes a first data identifier to be removed and a second data identifier to be removed;
[0033] The data to be removed is deleted from the record table, and the data to be removed is sent to the kernel state;
[0034] Query whether the second data identifier to be removed exists in the resource pool in the kernel mode. If it exists and the reference count value corresponding to the second data identifier to be removed is 0, then delete the data corresponding to the second data identifier to be removed from the resource pool.
[0035] In this embodiment of the application, data in the record table and resource pool are deleted according to preset rules, thereby reducing the data pressure in the record table and resource pool.
[0036] Secondly, embodiments of this application provide a data transmission apparatus, including:
[0037] The identifier calculation module is used to calculate and obtain the first data identifier of the data through the user process.
[0038] The first data sending module is configured to send the first data identifier and a pointer corresponding to the data to the kernel if the first data identifier does not exist in the record table corresponding to the user process; the pointer is used to represent the location information of the data storage.
[0039] The second data sending module is used to query whether the kernel-mode resource pool contains the first data identifier. If it does, then it is not necessary to transfer the data in the user process to the kernel mode. If it does not, then the data is obtained from the user process according to the pointer and transferred to the kernel mode.
[0040] Thirdly, embodiments of this application provide an electronic device, including: a processor, a memory, and a bus, wherein,
[0041] The processor and the memory communicate with each other via the bus;
[0042] The memory stores program instructions that can be executed by the processor, and the processor can execute the method of the first aspect by calling the program instructions.
[0043] Fourthly, embodiments of this application provide a non-transitory computer-readable storage medium, comprising:
[0044] The non-transitory computer-readable storage medium stores computer instructions that cause the computer to perform the method of the first aspect.
[0045] Fifthly, embodiments of this application provide a computer program product, including computer program instructions, which, when read and executed by a processor, perform the method of the first aspect.
[0046] Other features and advantages of this application will be set forth in the following description and will be apparent in part from the description or may be learned by practicing embodiments of this application. The objectives and other advantages of this application may be realized and obtained by means of the structures particularly pointed out in the written description, claims, and drawings. Attached Figure Description
[0047] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0048] Figure 1 The data transmission principle diagrams for user space and kernel space provided for comparison;
[0049] Figure 2 This is a schematic flowchart of a data transmission method provided in an embodiment of this application;
[0050] Figure 3 This is a schematic diagram of another data transmission method provided in an embodiment of this application;
[0051] Figure 4 A data transmission principle diagram provided for an embodiment of this application;
[0052] Figure 5 This is a schematic diagram of a data transmission device structure provided in an embodiment of this application;
[0053] Figure 6 This is a schematic diagram of the physical structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0054] The embodiments of the technical solution of this application will now be described in detail with reference to the accompanying drawings. These embodiments are only used to more clearly illustrate the technical solution of this application and are therefore merely examples, and should not be used to limit the scope of protection of this application.
[0055] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains; the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the application; the terms “comprising” and “having”, and any variations thereof, in the specification, claims, and foregoing description of the drawings are intended to cover non-exclusive inclusion.
[0056] In the description of the embodiments of this application, technical terms such as "first" and "second" are used only to distinguish different objects and should not be construed as indicating or implying relative importance or implicitly specifying the number, specific order, or primary and secondary relationship of the indicated technical features. In the description of the embodiments of this application, "multiple" means two or more, unless otherwise explicitly defined.
[0057] In this document, the term "embodiment" means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0058] In the description of the embodiments in this application, the term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this document generally indicates that the preceding and following related objects have an "or" relationship.
[0059] In the description of the embodiments of this application, the term "multiple" refers to two or more (including two), similarly, "multiple sets" refers to two or more (including two sets), and "multiple pieces" refers to two or more (including two pieces).
[0060] In the description of the embodiments of this application, unless otherwise expressly specified and limited, technical terms such as "installation," "connection," "joining," and "fixing" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral part; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two components or the interaction between two components. For those skilled in the art, the specific meaning of the above terms in the embodiments of this application can be understood according to the specific circumstances.
[0061] To facilitate understanding of the embodiments of this application, the relevant terms used in the embodiments of this application are now explained:
[0062] Linux System: Linux is an open-source operating system. It is widely popular for its stability, security, efficiency, flexibility and portability, especially in the fields of servers and high-performance computing.
[0063] The main features of the Linux system include:
[0064] Multitasking: Linux systems support multitasking, allowing multiple programs to run simultaneously, with each program running independently.
[0065] A user-friendly interface: The Linux system provides users with two types of interfaces: the user interface and system calls. Linux also provides a graphical user interface (GUI), which uses a mouse, menus, windows, scroll bars, and other features to present users with an intuitive, easy-to-use, and highly interactive graphical interface.
[0066] Device independence: The Linux system treats all external devices as files. Once the driver is installed, any user can manipulate and use these devices just like files. This device independence gives the Linux system a high degree of flexibility and scalability.
[0067] Rich networking capabilities: The Linux system has built-in comprehensive networking capabilities, which can easily enable network communication and data sharing.
[0068] A reliable security system: The Linux system employs numerous security technologies, including read / write control, protected subsystems, audit trails, and kernel authorization, providing essential security for users in multi-user network environments.
[0069] Excellent portability: The Linux system has excellent portability, and the operating system can be easily transferred from one platform to another while maintaining its original functionality and performance.
[0070] User mode: User mode is a lower-privilege execution state set by the operating system to restrict user access to system resources. When a user program runs, it is usually in user mode. In user mode, the program can only access limited system resources and cannot perform certain privileged operations, such as accessing hardware or modifying the memory of other processes.
[0071] User processes: User-mode processes are processes that run within an operating system. They execute in user mode and are subject to the permissions and restrictions provided by the operating system. In most modern operating systems, including Unix, Linux, and Windows, processes are designed to switch between user mode and kernel mode.
[0072] Kernel mode: Kernel mode is the highest privilege level execution state in an operating system. When the operating system kernel program runs, it is typically in kernel mode. In kernel mode, programs can access all system resources and perform any privileged operation. These privileged operations include accessing hardware, allocating memory, managing processes and threads, and managing system files.
[0073] Kernel process: A process that runs in kernel mode.
[0074] MD5 Algorithm: MD5 (Message-Digest Algorithm 5) is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value to ensure the integrity and consistency of transmitted information. Designed by Ronald Linn Rivest and released in 1992, MD5 has been widely applied in the field of cryptography.
[0075] Key features of the MD5 algorithm
[0076] Fixed-length output: Regardless of the length of the input data, the output of the MD5 algorithm is a fixed-length hash value (128 bits).
[0077] High sensitivity: Even small changes in the input data will result in significant differences in the hash value output by the MD5 algorithm.
[0078] Irreversibility: The original data cannot be recovered from the MD5 hash value.
[0079] Collision avoidance: Ideally, two different input data should produce different hash values.
[0080] Hash Algorithm: A hash algorithm, also known as a hash function or digest function, is a mathematical function or algorithm that converts data of arbitrary length (called a "message") into a fixed-length string (called a "hash value" or simply a "hash"). A hash algorithm can convert an input of arbitrary length (also called a message or plaintext) into a fixed-length output (also called a hash value, digest) through a hash operation.
[0081] Red-black trees: Red-black trees are a specialized type of AVL tree (balanced binary tree). Both red-black trees and AVL trees maintain the balance of the binary search tree through specific operations, thereby achieving high search performance. Although red-black trees have a complex structure, their worst-case running time is still very good, and search, insertion, and deletion operations can be completed in O(log n) time, where n is the number of elements in the tree.
[0082] The main characteristics of red-black trees include:
[0083] Each node is either red or black.
[0084] The root node is black.
[0085] Both children of a red node are black (there cannot be two consecutive red nodes on any path from a leaf to the root).
[0086] All paths from any node to each of its leaf nodes contain the same number of black nodes.
[0087] These properties ensure the key property of red-black trees: the longest possible path from the root to a leaf is no more than twice the length of the shortest possible path. Therefore, the tree is roughly balanced, allowing red-black trees to maintain high efficiency when performing operations such as insertion, deletion, and searching.
[0088] Least Recently Used (LRU) strategy: The Least Recently Used (LRU) strategy is a commonly used memory management strategy, especially in cache replacement algorithms. Its core idea is that if data hasn't been accessed recently, it's unlikely to be accessed again in the future. Therefore, when space is insufficient to accommodate new data, the least recently used data is evicted first.
[0089] The LRU strategy typically relies on a data structure, such as a linked list or a combination of a hash table and a linked list. In a linked list implementation, the most recently accessed data is moved to the head of the list, while the least recently accessed data is located at the tail. When data needs to be evicted, it is simply removed from the tail of the list.
[0090] Indexed Directly Readable (IDR) is a technique in the Linux kernel for managing finite-sized datasets. It provides a compact and efficient way to store and access data. The basic principle of IDR is to organize data into an array and use indices to access elements within the array. Each element has a unique index, allowing for quick retrieval or modification of the corresponding data. In the Linux kernel, IDR is commonly used to manage various resources or objects, such as device numbers, file descriptors, and network sockets. By using IDR, dynamically allocated memory for storing this data can be avoided, thus reducing memory overhead and the complexity of memory management.
[0091] Figure 1 The data transmission principle diagrams for user space and kernel space provided for comparison are as follows: Figure 1 As shown, in user space, many processes handle the same business logic. Each process writes certain data to the kernel-space resource pool. This data may be the same or different. Kernel processes read data from the resource pool. The data used by each process may be the same or different, and even the data used twice within the same process may be the same or different. They then use this data to perform business logic. Therefore, in existing technologies, user processes may write the same data to the kernel-space resource pool multiple times, resulting in significant system overhead.
[0092] To address this technical problem, embodiments of this application provide a data transmission method, apparatus, electronic device, and storage medium. Before transmitting data, the user process determines whether the data has already been sent to the kernel. If it has, transmission is stopped. If the user process determines that the data has not been sent to the kernel, it checks the kernel to see if the data already exists. If it does, transmission is also stopped. This reduces the transmission of redundant data and lowers system overhead.
[0093] It is understood that the data transmission method provided in this application can be applied to electronic devices, which may be Linux systems, Windows systems, Unix systems, etc. This application uses a Linux system as an example for the following description. A Linux system includes a Central Processing Unit (CPU). The CPU switches between kernel mode and user mode by executing specific instructions or when specific events (such as interrupts) occur. When a user program needs to execute a system call or an exception occurs, the CPU switches to kernel mode to execute the corresponding handler. After processing, the CPU switches back to user mode to continue executing the user program.
[0094] Figure 2 This is a schematic flowchart of a data transmission method provided in an embodiment of this application, such as... Figure 2 As shown, the method includes:
[0095] Step 201: Obtain the first data identifier of the data through user process calculation.
[0096] In this system, before sending data from the user process to the kernel, the Linux system calculates a first data identifier for the data. For example, the digest (key value) of the data can be calculated using the MD5 algorithm, and the resulting digest serves as the first data identifier. The first data identifier is used to represent the uniqueness of the data. It is understood that, in addition to using the MD5 algorithm, hash algorithms or other algorithms can also be used for calculation; this application does not specifically limit the specific use of such algorithms.
[0097] Step 202: If the first data identifier does not exist in the record table corresponding to the user process, then send the first data identifier and the pointer corresponding to the data to the kernel mode; the pointer is used to represent the location information of the data storage.
[0098] In a Linux system, multiple user processes can run. Each user process maintains its own record table, which records relevant information about the data sent by the user process to the kernel, such as the first data identifier of the data that has been sent.
[0099] After calculating and obtaining the first data identifier, the Linux system checks if the first data identifier exists in the record table of the corresponding user process. If it does not exist, it means that the user process has not sent the data before. In order to send the data to the kernel space, the first data identifier and the pointer corresponding to the data are sent to the kernel space. The pointer represents the memory address where the data is stored in user space, and the corresponding data can be retrieved from user space based on this pointer.
[0100] Step 203: Query whether the resource pool in the kernel mode contains the first data identifier. If it does, then it is not necessary to transfer the data in the user process to the kernel mode. If it does not, then obtain the data from the user process according to the pointer and transfer the data to the kernel mode.
[0101] The kernel-mode resource pool is used to store data sent from user mode, and this data is stored in a certain data structure, such as a red-black tree.
[0102] The Linux system checks if the first data identifier is present in the kernel-mode resource pool. If it is, it means the data is stored in kernel mode, and there's no need to resend the data from user mode to kernel mode. If the first data identifier is not in the resource pool, it means the data is not in kernel mode. The Linux system can then retrieve the data from user-mode memory using a pointer and write it to the kernel-mode resource pool to complete the data transfer from user mode to kernel mode.
[0103] This application maintains a record table in the user process to record which data has been sent from user mode to kernel mode, thereby determining whether data needs to be sent to kernel mode again. Additionally, data is also maintained in kernel mode. For a specific piece of data, if user mode has not previously sent the data to kernel mode, but a first data identifier indicates that the data already exists in kernel mode, then user mode does not need to send the data again. Therefore, when determining whether data exists in kernel mode, this application uses a first data identifier comparison method instead of a data comparison method, reducing system overhead. Furthermore, reducing the transmission of duplicate data further reduces system overhead.
[0104] Based on the above embodiments, for the case where the resource pool contains a first data identifier, the method further includes:
[0105] Obtain the second data identifier and the first reference count value corresponding to the first data identifier in the kernel state; wherein, the second data identifier is used to uniquely represent the data;
[0106] Update the first reference count value and return the second data identifier to the user process;
[0107] The user process stores the second data identifier and the first data identifier in the record table.
[0108] In the specific implementation process, if the resource pool contains the first data identifier, the corresponding second data identifier and first reference count value can be obtained from the resource pool. The second data identifier is also used to represent the uniqueness of the data. The reason for using the second data identifier is that, in scenarios where the first data identifier is an MD5 value (16 bytes) and the second data identifier is an int type (4 bytes) ID, since searching and deleting in the kernel-mode red-black tree are determined by the key, the function comparing whether the keys are the same should be as short as possible. Comparing an int type data is more efficient than comparing an MD5 value. Therefore, a second data identifier is introduced in the kernel mode. Both data identifiers can be used to represent unique data. The first reference count value is used to represent the processing (or use) status of the data in the kernel mode. If the first reference count value is positive, it means that the data still needs to be processed a certain number of times. If the first reference count is 0, it means that the data has been processed completely.
[0109] In kernel mode, after the first data identifier is found, it means that the data corresponding to the first data identifier needs to be processed. Therefore, the first reference count value corresponding to the data should be updated, for example, by incrementing it by 1.
[0110] After obtaining the second data identifier, the second data identifier is returned to the user process. The user process then stores the second data identifier and the first data identifier in a record table to indicate that the data has been sent to the kernel.
[0111] It should be noted that if a kernel process in kernel mode uses this data, the first reference count value corresponding to that data will be decremented by 1.
[0112] In this embodiment of the application, if the first data identifier is not present in the record table but is present in the resource pool, it means that the data already exists in the kernel mode and there is no need to transmit the data again. In order to facilitate data management, the reference count value of the data is maintained in both the resource pool and the record table. On the one hand, this facilitates monitoring the processing status of each data and on the other hand, it avoids repeated processing of the data.
[0113] Based on the above embodiments, for cases where a first data identifier exists in the record table, the method further includes:
[0114] If the first data identifier exists in the record table corresponding to the user process, then the second data identifier corresponding to the first data identifier is obtained from the record table;
[0115] Send the second data identifier to the kernel mode;
[0116] The corresponding first reference count value is obtained from the resource pool in the kernel state according to the second data identifier;
[0117] Update the first reference count value.
[0118] In the specific implementation process, if the first data identifier exists in the record table corresponding to the user process, it means that the user process has sent the data to the kernel space, and will not send data to the kernel space again this time. The record table stores relevant information about the data that the user process has sent to the kernel space, such as the first data identifier and the second data identifier corresponding to the data. Therefore, the corresponding second data identifier can be obtained from the record table based on the first data identifier.
[0119] After obtaining the second data identifier, it is sent to the kernel space. This updates the first reference count value corresponding to that data in the kernel space. Specifically, the first reference count value can be incremented by 1. The purpose of incrementing by 1 is to indicate the number of times the data corresponding to the second data identifier needs to be processed.
[0120] In this embodiment of the application, when the first data identifier exists in the record table, it is not necessary to send the data to the kernel state. Instead, the second data identifier is sent to the kernel state, and the kernel state updates the first reference count value to indicate that the data is to be processed.
[0121] Based on the above embodiments, the kernel-mode resource pool includes a red-black tree; the step of transmitting the data to the kernel-mode includes:
[0122] Generate a second data identifier corresponding to the data;
[0123] The data, along with the corresponding first data identifier, second data identifier, and first reference count value, are stored in the red-black tree.
[0124] In the specific implementation process, the data in the kernel-mode resource pool is stored in a red-black tree data structure. Therefore, after the data is obtained from the user mode to the kernel mode, the obtained data, the first data identifier, the second data identifier, and the first reference count value corresponding to the data can be stored as a data entry in the red-black tree of the resource pool.
[0125] The method for inserting data into a red-black tree is as follows:
[0126] 1. Define the nodes of a red-black tree:
[0127] Each node must contain at least the following attributes:
[0128] Key-value (md5): Used for lookup storage using md5 as the key.
[0129] Key-value (id): Used for searching and deleting using id as the key.
[0130] Reference count: Represents the count of how many processes use data.
[0131] Data pointer (data): The memory address information that stores the actual data.
[0132] Color: Red or black.
[0133] Left child: A pointer to the left child node.
[0134] Right child: A pointer to the right child node.
[0135] Parent node: A pointer to the parent node (may be needed in some implementations for quick access to the parent node when recoloring or rotating).
[0136] 2. Insertion operation:
[0137] To perform a normal binary search tree insertion: First, as with a normal binary search tree, find the position where the new node should be inserted.
[0138] Set the color of new nodes to red: newly inserted nodes are always red to maintain the properties of a red-black tree.
[0139] Repairing the properties of red-black trees: After inserting a new node, it may be necessary to perform some operations to maintain the five properties of red-black trees:
[0140] Each node is either red or black.
[0141] The root node is black.
[0142] Each leaf node (NIL node, empty node) is black.
[0143] If a node is red, then both of its child nodes are black.
[0144] For each node, the simple path from that node to all its descendant leaf nodes contains the same number of black nodes.
[0145] To fix these properties, one or more of the following operations may be required:
[0146] Color Flip: Swaps the colors of the red node with its black parent node.
[0147] Left rotation: Rotate to the left around a given node.
[0148] Right rotation: Rotate to the right around a given node.
[0149] In this embodiment of the application, since the kernel mode includes multiple processes, the same process may use the same data twice. Since the data is stored in the red-black tree with a first data identifier, a second data identifier, and a first reference count value, the second data identifier of the data to be used twice can be compared instead of the content of the data, thus reducing the system overhead.
[0150] Based on the above embodiments, the method further includes:
[0151] If a kernel process in the kernel mode uses the data, the first reference count value of the data is updated.
[0152] In the specific implementation process, if a kernel process in kernel space is detected to have used the data once, the first reference count value corresponding to that data can be updated, for example, by decrementing the first reference count value by 1. The first reference count value indicates how many user processes notify the kernel process to use this data when performing business operations; the second reference count value indicates the number of times the same data is used multiple times within the same process. When there is only one user process, even if the user process uses the same data multiple times, since it first looks up the data in the user space hash table each time, the second reference count value only needs to be incremented once per use. When multiple user processes are executing the same business, since the resources of different processes are isolated from each other, they will all look up the data in kernel space. Therefore, there will be scenarios where the same data is used by multiple processes. In this case, the first reference count value is incremented by 1 before each process uses the data and decremented by 1 after each use.
[0153] When the first reference count value corresponding to the data is 0, it means that the data has been completely used or processed.
[0154] This application embodiment can monitor the usage of data in kernel mode by updating the first reference count value.
[0155] Based on the above embodiments, the first data identifier calculated by the user process for the obtained data includes:
[0156] The user process uses the MD5 algorithm to calculate the first data identifier of the data.
[0157] In specific implementation, to obtain an identifier that characterizes the uniqueness of the data, the MD5 algorithm can be used to calculate the data content to obtain a first data identifier. It should be noted that the specific method for calculating the first data identifier using the MD5 algorithm is not specifically limited in the embodiments of this application.
[0158] Besides using the MD5 algorithm, other methods can be used, such as hashing the data.
[0159] This application embodiment calculates the first data identifier of the data using the MD5 algorithm. This first data identifier can be used to determine whether the user process has sent the data to the kernel space from the record table, and can also be used to determine whether the data exists in the kernel space from the resource pool. This determines whether the data needs to be sent to the kernel space, reducing redundant data transmission.
[0160] Based on the above embodiments, the record table includes a second reference count value; the second reference count value is used to characterize the number of times the corresponding data needs to be processed; the method further includes:
[0161] If the amount of data recorded in the record table is greater than a preset value, then data to be removed is obtained from the record table based on the least recently used strategy; the data to be removed includes a first data identifier to be removed and a second data identifier to be removed;
[0162] The data to be removed is deleted from the record table, and the data to be removed is sent to the kernel state;
[0163] Query whether the second data identifier to be removed exists in the resource pool in the kernel mode. If it exists and the reference count value corresponding to the second data identifier to be removed is 0, then delete the data corresponding to the second data identifier to be removed from the resource pool.
[0164] In practice, to prevent excessive data from being stored in kernel mode, data can be deleted according to certain rules. Additionally, the record table in user mode also has a pre-set capacity; when the stored content exceeds the preset limit, a deletion operation is also performed.
[0165] The data volume refers to the number of records in the record table that consist of a first data identifier, a second data identifier, and a second reference count. Assuming a preset value of 128, a data eviction operation is triggered when the data volume in the record table corresponding to a user process exceeds 128 records. During data eviction, a Least Recently Used (LRU) strategy is used to select data to be evictioned. This means that the number of records to be evictioned can be one or more. A second reference count value of 0 for a record to be eviction indicates that the data has already been processed.
[0166] After identifying the data to be removed, delete it from the record table. Then, send the second data identifier of the data to be processed to the kernel space. Check if the first reference count value corresponding to this second data identifier in the kernel space resource pool is 0. If it is 0, it means the data corresponding to the second data identifier has been processed and can be deleted. If it is not 0, it means the data has not been processed and no deletion operation is performed.
[0167] In this embodiment of the application, data in the record table and resource pool are deleted according to preset rules, thereby reducing the data pressure in the record table and resource pool.
[0168] In another embodiment, the data deletion operation can also be initiated in the kernel mode. That is, the LRU policy is used to determine the data to be deleted from the resource pool in the kernel mode, the data to be deleted is deleted, and the second data identifier corresponding to the data to be deleted is sent to each user mode. If the record table in the user mode contains the second data identifier and the second reference count value corresponding to the second data identifier is 0, then the data corresponding to the second data identifier is deleted.
[0169] Figure 3 This is a schematic diagram of another data transmission method provided in an embodiment of this application. Figure 4 A data transmission principle diagram provided for an embodiment of this application, such as... Figure 3 and Figure 4 As shown, the method includes:
[0170] Step 301: Calculate the key value of the data; Before writing data from the user process to the kernel process, a digest is first calculated based on the content of the data using the MD5 algorithm to obtain a key value (first data identifier).
[0171] Step 302: Does the key value exist in the hash table? Query the hash table maintained by the user process to see if the key value exists, in order to determine whether the data has been used. If the key value exists in the hash table, obtain the corresponding id (second data identifier) based on the key value and proceed to step 308; otherwise, proceed to step 303.
[0172] Step 303: If the key value is not found in the hash table, send the key value and a pointer to the data to the kernel process.
[0173] Step 304: Check if the key value exists in the red-black tree; the kernel process searches for the key value in the red-black tree. If found, it means that the data already exists in the resource pool, and proceed to step 305; otherwise, proceed to step 307.
[0174] Step 305: Increase the first reference count value Ref of the data corresponding to the key value to indicate that the data will be used. At the same time, return the id corresponding to the key value to the user-mode process, so that the user-mode process no longer needs to pass the content of the specific data Data to the kernel mode.
[0175] Step 306: After obtaining the id, the user-space process inserts the key value and the id into the hash table maintained by the user-space process.
[0176] Step 307: If not found, it means this data is not in the resource pool. The kernel process retrieves the data from user space based on the pointer, generates the corresponding ID, and stores the data, key, ID, and first reference count in a red-black tree. The kernel then returns the key and ID to user space. It should be noted that the kernel can generate the ID based on the IDR (Incremental Reference Count), i.e., generating a unique ID for the data by incrementing the IDR.
[0177] Step 308: If the ID exists in the record table, send the ID to the kernel process. The kernel process then searches for the ID in the resource pool. Generally, an ID stored in the user-space record table can be found in the resource pool. After finding the corresponding ID, increment the first reference count value corresponding to that ID by 1.
[0178] In this embodiment, since the kernel also involves multiple processes retrieving data from the red-black tree, the data used previously and the data used this time may be the same. In this case, only the corresponding IDs need to be compared, without comparing the data content, thus reducing system overhead. Furthermore, to prevent excessive data storage in the kernel, data with a first reference count of 0 is deleted once a certain amount of data is reached. Therefore, through the above scheme, during data transfer between user-mode and kernel-mode processes, the overall overhead is reduced by minimizing data copying during multiple transfers of the same data and reducing the overhead of comparing data when the kernel process uses the same data twice, ultimately achieving performance optimization.
[0179] Figure 5 This is a schematic diagram of a data transmission device structure provided in an embodiment of this application. The device can be a module, program segment, or code on an electronic device. It should be understood that this device is similar to the one described above. Figure 2 The method implementation corresponds to this and can be executed. Figure 2 The various steps involved in the method embodiment, and the specific functions of the device, can be found in the description above. To avoid repetition, detailed descriptions are appropriately omitted here. The device includes: an identifier calculation module 501, a first data transmission module 502, and a second data transmission module 503, wherein:
[0180] The identifier calculation module 501 is used to calculate the first data identifier of the data obtained through the user process;
[0181] The first data sending module 502 is used to send the first data identifier and the pointer corresponding to the data to the kernel if the first data identifier does not exist in the record table corresponding to the user process; the pointer is used to represent the location information of the data storage.
[0182] The second data sending module 503 is used to query whether the resource pool contains the first data identifier through the kernel mode. If it contains the first data identifier, then it is not necessary to transfer the data in the user process to the kernel mode. If it does not contain the first data identifier, then the data is obtained from the user process according to the pointer and transferred to the kernel mode.
[0183] Based on the above embodiments, the device further includes a first update module, used for:
[0184] If the resource pool contains the first data identifier, obtain the second data identifier and the first reference count value corresponding to the first data identifier in the kernel state; wherein, the second data identifier is used to uniquely represent the data;
[0185] Update the first reference count value and return the second data identifier to the user process;
[0186] The user process stores the second data identifier and the first data identifier in the record table.
[0187] Based on the above embodiments, the device further includes a second update module, used for:
[0188] If the first data identifier exists in the record table corresponding to the user process, then the second data identifier corresponding to the first data identifier is obtained from the record table;
[0189] Send the second data identifier to the kernel mode;
[0190] The corresponding first reference count value is obtained from the resource pool in the kernel state according to the second data identifier;
[0191] Update the first reference count value.
[0192] Based on the above embodiments, the kernel-mode resource pool includes a red-black tree; the second data sending module 503 is specifically used for:
[0193] Generate a second data identifier corresponding to the data;
[0194] The data, along with the corresponding first data identifier, second data identifier, and first reference count value, are stored in the red-black tree.
[0195] Based on the above embodiments, the device further includes a third update module, used for:
[0196] If a kernel process in the kernel mode uses the data, the first reference count value of the data is updated.
[0197] Based on the above embodiments, the identifier calculation module 501 is specifically used for:
[0198] The user process uses the MD5 algorithm to calculate the first data identifier of the data.
[0199] Based on the above embodiments, the record table includes a second reference count value; the second reference count value is used to characterize the number of times the corresponding data needs to be processed; the device also includes a deletion module, used for:
[0200] If the amount of data recorded in the record table is greater than a preset value, then data to be removed is obtained from the record table based on the least recently used strategy; the data to be removed includes a first data identifier to be removed and a second data identifier to be removed;
[0201] The data to be removed is deleted from the record table, and the data to be removed is sent to the kernel state;
[0202] If the second data identifier to be removed exists in the resource pool in the kernel mode, and if it exists and the first reference count value corresponding to the second data identifier to be removed is 0, then the data corresponding to the second data identifier to be removed is deleted from the resource pool.
[0203] Figure 6 This is a schematic diagram of the physical structure of the electronic device provided in the embodiments of this application, such as... Figure 6 As shown, the electronic device includes: a processor 601, a memory 602, and a bus 603; wherein,
[0204] The processor 601 and the memory 602 communicate with each other through the bus 603;
[0205] The processor 601 is used to call program instructions in the memory 602 to execute the methods provided in the above method embodiments, such as: calculating and obtaining a first data identifier of data through a user process; if the first data identifier does not exist in the record table corresponding to the user process, sending the first data identifier and a pointer corresponding to the data to the kernel state; the pointer is used to represent the location information of the data storage; querying whether the resource pool in the kernel state contains the first data identifier; if it does, then it is not necessary to transfer the data in the user process to the kernel state; if it does not, then obtaining the data from the user process according to the pointer and transferring the data to the kernel state.
[0206] Processor 601 can be an integrated circuit chip with signal processing capabilities. The processor 601 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of this application. The general-purpose processor can be a microprocessor or any conventional processor.
[0207] The memory 602 may include, but is not limited to, random access memory (RAM), read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), etc.
[0208] This embodiment discloses a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the methods provided in the above-described method embodiments, such as: obtaining a first data identifier of data through a user process; if the first data identifier does not exist in the record table corresponding to the user process, sending the first data identifier and a pointer corresponding to the data to the kernel state; the pointer is used to represent the location information of the data storage; querying whether the resource pool in the kernel state contains the first data identifier; if it does, then it is not necessary to transfer the data in the user process to the kernel state; if it does not, then obtaining the data from the user process according to the pointer and transferring the data to the kernel state.
[0209] This embodiment provides a non-transitory computer-readable storage medium storing computer instructions that cause the computer to execute the methods provided in the above-described method embodiments. These instructions include, for example: calculating and obtaining a first data identifier for data through a user process; if the first data identifier does not exist in the record table corresponding to the user process, sending the first data identifier and a pointer corresponding to the data to the kernel state; the pointer representing the location information of the data storage; querying whether the resource pool in the kernel state contains the first data identifier; if it does, then it is not necessary to transfer the data from the user process to the kernel state; if it does not, then obtaining the data from the user process according to the pointer and transferring the data to the kernel state.
[0210] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some communication interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms.
[0211] Furthermore, the units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0212] Furthermore, the functional modules in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0213] In this document, relational terms such as first and second are used only to distinguish one entity or operation from another entity or operation, without necessarily requiring or implying any such actual relationship or order between these entities or operations.
[0214] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.
Claims
1. A data transmission method, characterized in that, include: The first data identifier is obtained by calculating the data through the user process; If the first data identifier does not exist in the record table corresponding to the user process, the first data identifier and the pointer corresponding to the data are sent to the kernel mode; the pointer is used to represent the location information of the data storage. Check whether the resource pool in the kernel mode contains the first data identifier. If it does, then there is no need to transfer the data in the user process to the kernel mode. If it does not, then obtain the data from the user process according to the pointer and transfer the data to the kernel mode. If the resource pool contains the first data identifier, the method further includes: Obtain the second data identifier and the first reference count value corresponding to the first data identifier in the kernel state; wherein, the second data identifier is used to uniquely represent the data; Update the first reference count value and return the second data identifier to the user process; The user process stores the second data identifier and the first data identifier in the record table.
2. The method according to claim 1, characterized in that, The method further includes: If the first data identifier exists in the record table corresponding to the user process, then the second data identifier corresponding to the first data identifier is obtained from the record table; Send the second data identifier to the kernel mode; The corresponding first reference count value is obtained from the resource pool in the kernel state according to the second data identifier; Update the first reference count value.
3. The method according to claim 1, characterized in that, The kernel-mode resource pool includes a red-black tree; the process of transferring the data to the kernel-mode includes: Generate a second data identifier corresponding to the data; The data, along with the corresponding first data identifier, second data identifier, and first reference count value, are stored in the red-black tree.
4. The method according to claim 1, characterized in that, The method further includes: If a kernel process in the kernel mode uses the data, the first reference count value of the data is updated.
5. The method according to claim 1, characterized in that, The first data identifier obtained by calculating data through a user process includes: The user process uses the MD5 algorithm to calculate the first data identifier of the data.
6. The method according to claim 1, characterized in that, The record table includes a second reference count value; the second reference count value is used to characterize the number of times the corresponding data needs to be processed; the method further includes: If the amount of data recorded in the record table is greater than a preset value, then data to be removed is obtained from the record table based on the least recently used strategy; the data to be removed includes a first data identifier to be removed and a second data identifier to be removed; The data to be removed is deleted from the record table, and the data to be removed is sent to the kernel state; If the second data identifier to be removed exists in the resource pool in the kernel mode, and if it exists and the first reference count value corresponding to the second data identifier to be removed is 0, then the data corresponding to the second data identifier to be removed is deleted from the resource pool.
7. A data transmission device, characterized in that, include: The identifier calculation module is used to calculate and obtain the first data identifier of the data through the user process. The first data sending module is configured to send the first data identifier and a pointer corresponding to the data to the kernel if the first data identifier does not exist in the record table corresponding to the user process; the pointer is used to represent the location information of the data storage. The second data sending module is used to query the resource pool in the kernel mode to see if the first data identifier is contained. If it is contained, then it is not necessary to transmit the data in the user process to the kernel mode. If it is not contained, then the data is obtained from the user process according to the pointer and transmitted to the kernel mode. The device further includes a first update module for: If the resource pool contains the first data identifier, obtain the second data identifier and the first reference count value corresponding to the first data identifier in the kernel state; wherein, the second data identifier is used to uniquely represent the data; Update the first reference count value and return the second data identifier to the user process; The user process stores the second data identifier and the first data identifier in the record table.
8. An electronic device, characterized in that, include: Processor, memory, and bus, among which, The processor and the memory communicate with each other via the bus; The memory stores program instructions that can be executed by the processor, and the processor can execute the method as described in any one of claims 1-6 by calling the program instructions.
9. A non-transitory computer-readable storage medium, characterized in that, The non-transitory computer-readable storage medium stores computer instructions, which, when executed by a computer, cause the computer to perform the method as described in any one of claims 1-6.
10. A computer program product, characterized in that, It includes computer program instructions, which, when read and executed by a processor, perform the method as described in any one of claims 1-6.
Citation Information
Patent Citations
Kernel-mode data access method, device and system
CN103544324A