Memory usage method, electronic device and storage medium
By increasing the reference count in the memory block, the use of memory blocks is managed. Memory is only requested once and reused when the reference count reaches the initial value. This solves the problems of high processor usage and memory overflow caused by frequent memory request and release, and achieves more efficient memory management.
Patent Information
- Application Number
- CN202111541646.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-16
- Publication Date
- 2025-09-30
- Estimated Expiration
- 2041-12-16
AI Technical Summary
Frequent memory allocation and release increases the processor usage of electronic devices and may cause memory overflow exceptions.
Manage the use of memory blocks by increasing the reference count in the memory block, apply for memory only once and reuse it when the reference count is at the initial value, and avoid frequent application and release of memory.
It reduces the processor occupancy rate, avoids memory overflow exceptions, and improves memory usage efficiency and stability.
Smart Images

Figure CN114218130B_ABST
Abstract
Description
Technical field
[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a memory usage method, an electronic device, and a storage medium. [Background Technology]
[0002] As network bandwidth continues to increase, electronic devices are sending and receiving network packets more frequently per unit time. However, the sending and receiving of network packets requires the allocation and release of memory. Frequent allocation and release of memory not only increases the processor utilization of electronic devices, but also may cause out-of-memory (OOM) exceptions in electronic devices. [Summary of the invention]
[0003] The embodiments of the present application provide a memory usage method, an electronic device, and a storage medium, which can avoid frequent memory requests and releases when the electronic device receives network data packets, reduce the occupancy rate of the electronic device processor, and avoid memory overflow exceptions.
[0004] In a first aspect, an embodiment of the present application provides a memory usage method, applied to an electronic device, comprising:
[0005] The address of at least one memory block requested for allocation is stored in a local linked list, wherein the reference count of the at least one memory block is an initial value greater than zero;
[0006] When a structure SKB is constructed in the at least one memory block, a reference count of the memory block corresponding to the structure SKB is increased, and the structure SKB is used to transmit network data packets;
[0007] Each time one of the structures SKB is released, the reference count of the memory block corresponding to the released structure SKB is reduced. When the reference count of the memory block in the local linked list is reduced to the initial value, the structure SKB is allowed to be constructed on the memory block whose reference count is the initial value.
[0008] The above-mentioned memory usage method provided in the embodiment of the present application can apply for memory allocation only once, and by increasing the reference count each time a structure SKB is constructed and reducing the reference count when releasing, the reference count of the memory block applied for allocation is not zero, which is equivalent to not releasing the memory block applied for allocation. Then, a new structure SKB can be constructed on the memory block with the reference count as the initial value, and the memory block that has been applied for allocation can be reused repeatedly, which can avoid frequent application and release of memory when sending and receiving network data, and reduce the processor occupancy rate of the electronic device 100. Moreover, when the structure SKB passed by the electronic device 100 is not released in time, the electronic device 100 can use the memory block with the reference count reduced to the initial value to receive new data packets, which will not cause a large amount of memory accumulation and avoid the occurrence of memory overflow exceptions.
[0009] In one possible implementation, when each time a structure SKB is constructed in the at least one memory block, after increasing a reference count of the memory block corresponding to the structure SKB, the method further includes:
[0010] Generate a descriptor corresponding to the structure SKB, where the descriptor is used to represent parameters required for data transfer between the network data packet and the structure SKB;
[0011] Obtaining the number of consumptions, where the number of consumptions is the number of descriptors consumed by data transfer between the network data packet and the structure SKB;
[0012] When the number of consumption times is greater than or equal to a first threshold, memory recycling is performed.
[0013] In one possible implementation, when the number of consumption times is greater than or equal to a first threshold, performing memory recycling includes:
[0014] Traversing the local linked list to determine whether there is a memory block whose reference count is reduced to the initial value;
[0015] When there is a memory block whose reference count is reduced to the initial value, the structure SKB is constructed on the memory block whose reference count is reduced to the initial value, and the reference count of the memory block whose reference count is reduced to the initial value is increased.
[0016] In one possible implementation, after constructing the structure SKB on the memory block whose reference count is reduced to the initial value and increasing the reference count of the memory block whose reference count is reduced to the initial value, the method further includes:
[0017] Obtain a first quantity, where the first quantity is the number of memory blocks where the reference count is reduced to the initial value and the structure SKB is constructed;
[0018] updating the consumption times according to the difference between the consumption times and the first quantity;
[0019] When the updated consumption count is greater than or equal to the second threshold, a memory recycling step is triggered.
[0020] In a second aspect, an embodiment of the present application provides an electronic device, including:
[0021] A storage module, configured to store the address of at least one memory block applied for allocation into a local linked list, wherein the reference count of the at least one memory block is an initial value greater than zero;
[0022] A first operation module is configured to increase a reference count of a memory block corresponding to the structure SKB when constructing a structure SKB in the at least one memory block, wherein the structure SKB is used to transmit a network data packet;
[0023] The second operation module is used to reduce the reference count of the memory block corresponding to the released structure SKB each time the structure SKB is released, wherein when the reference count of the memory block in the local linked list is reduced to the initial value, it is allowed to construct the structure SKB on the memory block with the reference count being the initial value.
[0024] In one possible implementation, the electronic device further includes:
[0025] A generation module, configured to generate a descriptor corresponding to the structure SKB, wherein the descriptor is used to represent parameters required for data transfer between a network data packet and the structure SKB;
[0026] An acquisition module, configured to acquire a consumption count, where the consumption count is the number of descriptors consumed by data transfer between the network data packet and the structure SKB;
[0027] The memory recycling module is used to recycle memory when the number of consumption times is greater than or equal to a first threshold.
[0028] In one possible implementation, the memory recycling module includes:
[0029] A traversal unit, configured to traverse the local linked list to determine whether there is a memory block whose reference count is reduced to the initial value;
[0030] The construction unit constructs the structure SKB on the memory block whose reference count is reduced to the initial value when there is a memory block whose reference count is reduced to the initial value, and increases the reference count of the memory block whose reference count is reduced to the initial value.
[0031] In one possible implementation, the memory recycling module further includes:
[0032] An acquiring unit, configured to acquire a first quantity, where the first quantity is the number of memory blocks whose reference counts are reduced to the initial value and on which the structure SKB is constructed;
[0033] an updating unit, configured to update the number of consumptions according to a difference between the number of consumptions and the first quantity;
[0034] The trigger unit is used to trigger the step of memory recycling when the updated consumption count is greater than or equal to a second threshold.
[0035] In a third aspect, an embodiment of the present application provides an electronic device, including:
[0036] At least one processor; and at least one memory communicatively connected to the processor, wherein: the memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to execute the method provided by the first aspect.
[0037] In a fourth aspect, an embodiment of the present application provides a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer instructions, and the computer instructions enable the computer to execute the method provided in the first aspect.
[0038] It should be understood that the second to fifth aspects of the embodiments of the present application are consistent with the technical solutions of the first aspect of the embodiments of the present application, and the beneficial effects achieved by each aspect and the corresponding feasible implementation methods are similar and will not be repeated here.
Brief Description of the Drawings
[0039] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0040] Figure 1 This is a schematic diagram of an application scenario provided by an embodiment of the present application;
[0041] Figure 2 is a schematic diagram of the software structure of the electronic device provided in an embodiment of the present application;
[0042] Figure 3 This is a schematic diagram of the hierarchical structure of the network subsystem provided in an embodiment of the present application;
[0043] Figure 4This is a flowchart of a memory usage method provided by an embodiment of the present application;
[0044] Figure 5 This is a flowchart of a memory usage method provided by another embodiment of the present application;
[0045] Figure 6 This is a flowchart of a memory usage method provided by another embodiment of the present application;
[0046] Figure 7 A schematic diagram of the structure of an electronic device provided in accordance with one embodiment of the present invention;
[0047] Figure 8 This is a schematic structural diagram of an electronic device provided in another embodiment of this specification. [Specific implementation method]
[0048] In order to better understand the technical solutions of this specification, the embodiments of the present application are described in detail below with reference to the accompanying drawings.
[0049] It should be clear that the embodiments described are only part of the embodiments of this specification, not all of the embodiments. Based on the embodiments in this specification, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this specification.
[0050] The terms used in the examples of this application are for the purpose of describing specific embodiments only and are not intended to limit this specification. The singular forms "a," "an," "the," and "the" used in the examples of this application and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise.
[0051] Figure 1 This is a schematic diagram of an application scenario provided by an embodiment of the present application. Figure 1 As shown, the electronic device 100 can connect to the service device 101 in the network through a wireless network or a wired network, and then send and receive network data.
[0052] See also Figure 1 The electronic device 100 can receive and send network packets through a network interface card (NIC). A network packet is a data unit in the transmission control protocol / internet protocol (TCP / IP) communication transmission.
[0053] Exemplarily, the electronic device 100 may be a device with an internally integrated network card, such as a mobile phone, a personal computer, a tablet computer, a notebook, a desktop computer, a server, a smart wearable device, a smart TV, etc.
[0054] In an embodiment of the present application, the network card can also be independent of the electronic device 100, and the electronic device 100 can be connected to the network card through a communication interface. For example, the electronic device 100 can be connected to the network card through a universal serial bus (USB) interface to send and receive network data packets.
[0055] It is understandable that the software system of the electronic device 100 can adopt a layered architecture, an event-driven architecture, a micro-kernel architecture, a micro-server architecture, or a cloud architecture. The software system of the electronic device 100 is deployed with a driver corresponding to the network card, and the electronic device 100 can drive the network card to send and receive network data packets through the driver.
[0056] This application takes a Linux kernel-based system in a layered architecture as an example to exemplify the software structure of the electronic device 100 .
[0057] Figure 2 1 is a schematic diagram of the software structure of the electronic device 100 provided in the embodiment of the present application. Figure 2 As shown in the figure, the system based on the Linux kernel can be divided into three layers: user process layer, Linux kernel layer, and hardware layer.
[0058] User process layer: represents the application programs running in the electronic device 100. The application programs run in the user space and are uniformly managed by the Linux kernel.
[0059] For example, the user process layer includes applications, an application framework (Java API Framework), and the like. Applications can include browsers, desktop launchers, maps, and the like. The application framework provides an application programming interface (API) and programming framework for applications. The application framework includes some predefined functions for providing system services. These predefined functions may include a phone manager, a power manager, a window manager, and the like.
[0060] Linux kernel layer: The Linux kernel layer is the layer between hardware and software. It includes at least display drivers, sensor drivers, and network card drivers. The Linux kernel also implements security management, memory management, process management, and network management.
[0061] Hardware layer: includes hardware resources such as processor, memory, hard disk, and network card.
[0062] The code related to network management in the Linux kernel layer can be a relatively independent subsystem, which can be called the network subsystem. The network card needs to send and receive network data through the network subsystem. The network subsystem can support different protocol families (such as INET, INET6, UNIX, NETLINK, etc.), support different network devices, support a unified API socket, etc., and thus need to shield the differences in protocols, hardware, and APIs. Therefore, the network subsystem can adopt a layered structure, such as Figure 3 shown.
[0063] See also Figure 3 The network subsystem resides in kernel space and can interact with applications in user space. From top to bottom, the network subsystem can be divided into the following layers: system call interface, protocol agnostic interface, network protocols layer, device agnostic interface, and device drivers layer.
[0064] System call interface: This is an interface library for user-space applications, providing a network service interface for these applications. User-space applications can access kernel space through the system call interface. This means that user-space applications can send and receive network data from the network card to kernel space through the system call interface.
[0065] Protocol-independent interface: Also known as the protocol interface layer, it is essentially the socket layer. The purpose of the protocol-independent layer is to shield the various network protocols in the network protocol layer, providing a simple and unified interface to the system call interface. Regardless of the type of network protocol used by the application, a socket must be established through the system call interface. A socket can be a large StructSocket structure that connects to the network protocol layer below. This shields the different network protocols in the network protocol layer, presenting only the data portion to the application through the system call interface.
[0066] For example, protocol-independent interfaces include BSD Socket and INET Socket. BSD Socket provides a unified socket operation interface and is closely related to the Struct Socket structure. INET Socket is a unified interface for calling IP protocols and is closely related to the Struct Socket structure.
[0067] Network protocol layer: This corresponds to the IP layer and transport layer in the TCP / IP network protocol framework. The network protocol layer can implement multiple network protocols, such as IP, ICMP, ARP, RARP, TCP, UDP, etc.
[0068] Device-independent interface: This interface provides an abstract driver interface that is independent of the specific device. The purpose of a device-independent interface is to unify the interfaces between different network card drivers and the network protocol layer. For example, the functions of different drivers can be abstracted into a few specific actions, such as Open, Close, and Initialize.
[0069] Device driver layer: Also known as the driver layer. The device driver layer can be used to connect to the network card to transmit network data packets.
[0070] After the network card receives the network data packet, it needs to synchronize the network data packet to the kernel space, combined with Figure 3 The process of the electronic device 100 receiving a network data packet through the network card is exemplified. The process includes:
[0071] In step 1, the network card driver applies to allocate several memory blocks in the memory (RAM) of the electronic device 100 as data buffers, and generates a descriptor queue corresponding to each memory block in the network card according to the allocated memory blocks.
[0072] For example, multiple SKB structures (Struct sk_buffer) are constructed on several memory blocks, and then descriptor queues corresponding to the multiple SKB structures are generated and written to the registers of the network card. The descriptors can point to the physical addresses where the multiple SKB structures are located.
[0073] It can be understood that the register of the network card can be a storage area shared by the network card and the network card driver.
[0074] It should be noted that the electronic device 100 can receive network data packets transmitted by the network card through the SKB structure. The SKB structure is a structure used to manage network data packets based on the Linux kernel system. The SKB structure can be a buffer area for sending and receiving packets in the TCP / IP stack. The SKB structure is copied twice when receiving network data packets to improve performance. For example, the network data packet is copied once after entering the network card driver and again when it is handed from kernel space to the user space application. The encapsulation and decapsulation of all data packets in the network subsystem are performed through the SKB structure.
[0075] Step 2: After the network card receives the network data packet, it can obtain the physical address of the structure SKB through the descriptor queue, and then write the network data packet into the memory space where the structure SKB is located through direct memory access (DMA).
[0076] In step 3, after the network card driver writes the network data packet into the structure SKB, since it is DMA write, the Linux kernel does not monitor the writing status of the network data packet and does not process the network data packet. The network card driver needs to initiate a hard interrupt to notify the processor of the electronic device 100 to process the network data packet written into the memory space where the structure SKB is located.
[0077] For example, the network card driver hard interrupt can perform the following operations:
[0078] 1. Match the network data packet corresponding to the descriptor with the structure SKB;
[0079] 2. Set the structure SKB to complete the encapsulation of the network data packet corresponding to the descriptor;
[0080] 3. Pass the encapsulated structure SKB to the device driver layer of the network subsystem.
[0081] Step 4: The network card driver of the device driver layer can call the interface function netif_rx() to pass the structure SKB to the device-independent interface.
[0082] It is understood that the structure SKB encapsulating the network data packet can be passed to the application in the user space through the device-independent interface, the network protocol layer, the protocol-independent interface, and the system call interface in sequence. After the network subsystem has finished using the structure SKB, the processor of the electronic device 100 can release the structure SKB and the memory block where the structure SKB is located.
[0083] It should be noted that the process of sending network data packets by the electronic device 100 is the opposite of the process of receiving network data packets. That is, the network data is transmitted by the application in user space through the system call interface, protocol-independent interface, network protocol layer, device-independent interface, and device driver layer within the network subsystem to the network card, and then the network card sends the data. The network data packets sent by the electronic device 100 are also transmitted within the network subsystem through multiple structures SKB constructed on the requested memory blocks, and then written to the network card using the structures SKB and corresponding descriptors.
[0084] When electronic device 100 transmits and receives network data, its processor must request and release memory. However, when transmitting and receiving large amounts of data, such as at a 1Gbps throughput, approximately 100 packets must be processed in 1ms, resulting in 100 memory requests and releases, leading to high processor utilization. Furthermore, if the transmitted SKB structure is not released promptly, the network card driver continues to request memory, potentially leading to a memory overflow exception.
[0085] Based on the above problems, an embodiment of the present application provides a memory usage method, which can apply for memory as a data buffer only once when the network card is initialized or when sending and receiving data for the first time, and then use the applied memory in a circular manner to avoid excessive processor occupancy and memory overflow.
[0086] Figure 4 This is a flow chart of a memory usage method provided by an embodiment of the present application. Figure 4 As shown, the above memory usage method can be applied to the electronic device 100, including:
[0087] Step 401: Store the address of at least one memory block requested for allocation into a local linked list, and the reference count of at least one memory block is an initial value greater than zero.
[0088] It is understandable that the electronic device 100 can send and receive network data packets through the network card. Before sending and receiving the data packets, it is necessary to apply for allocation of memory for the kernel space to synchronize the network data packets on the network card with the corresponding network data packets in the kernel space.
[0089] Optionally, the electronic device 100 may apply for allocation of at least one memory block before the network card sends or receives data, for transmitting network data packets in the kernel space.
[0090] Optionally, the electronic device 100 may apply for allocation of at least one memory block when the network card sends or receives data for the first time.
[0091] It should be noted that in the prior art, the electronic device 100 only applies for allocation of memory blocks to the network card when there is a need to transmit or receive network data, and thus needs to frequently apply for memory blocks. The memory usage method provided in the embodiment of the present application can allocate memory to the network card when there is no network data being transmitted or received, or can apply for allocation of memory to the network card when receiving or sending data for the first time.
[0092] Optionally, the electronic device 100 may utilize a buddy system to request allocation of at least one memory block. The buddy system may allocate memory using a power-of-two allocator. The memory block may be a physical memory page in the memory of the electronic device 100, and the size of the memory block may be a power-of-two KB, such as 64KB, 128KB, or 256KB.
[0093] Optionally, after a memory block is requested, its reference count changes from zero to one, indicating that the memory block is used by a process within the electronic device 100. When the process that references the memory block finishes using the memory block, its reference count changes to zero, indicating that the memory block is released and can be used by other processes.
[0094] Optionally, after the electronic device 100 applies for allocation of at least one memory block, the reference count of the at least one memory block is an initial value greater than zero. The initial value may be 1.
[0095] Optionally, in another possible implementation, after applying for allocation of at least one memory block, the electronic device 100 may set the reference count of the at least one memory block to an initial value greater than zero, such as 2, 3, 4, etc.
[0096] Alternatively, the electronic device 100 may use DMA to directly read and write data between the network card and the memory. The electronic device uses DMA to read and write data between the network card and the memory without going through the processor of the electronic device 100 and without the intervention of the processor of the electronic device 100.
[0097] For example, the electronic device 100 may be provided with at least one DMA controller, and the electronic device 100 may directly read and write data between the register of the network card and the memory in the kernel space through the DMA controller.
[0098] Optionally, at least one memory block is DMA-coherently mapped.
[0099] It should be noted that since the data area is readable and writable by processes in electronic device 100, while instruction data is read-only, the compiled program source code is divided into two segments: "program instructions" and "program data." This partitioned virtual memory facilitates permission management. When multiple instances of a process are running in the system of electronic device 100, since the instructions are identical, only one copy of the program's instruction portion needs to be stored in memory. The processor of electronic device 100 may employ a Harvard architecture that aligns with the idea of program segmentation, separating instructions and data into I-Cache and D-Cache, respectively, each with independent read and write ports. For example, the I-Cache is read-only and does not require a write port. If a data exchange between the memory and the network card changes the contents of the DMA buffer area in the memory, that is, changes the contents of at least one memory block allocated by electronic device 100, and if the cache contains a memory block in the memory corresponding to the DMA buffer, if there is no mechanism to ensure that the contents of the cache are updated (or invalidated) by the new DMA buffer data, then the cache and the corresponding memory block in the memory will have inconsistent contents. If the processor of the electronic device 100 tries to obtain the data in the DMA buffer transferred from the network card to the memory at this time, the processor of the electronic device 100 will obtain the data directly from the cache, which is obviously not what is expected because the data in the memory corresponding to the cache has been updated.
[0100] Exemplarily, the electronic device 100 may use the Dma_map_singal function to perform DMA consistency mapping.
[0101] Optionally, the electronic device 100 may store the address of at least one memory segment requested for allocation in a local linked list. This configuration facilitates the electronic device 100 to manage the memory requested by the network card driver.
[0102] Step 402: When a structure SKB is constructed in at least one memory block, a reference count of the memory block corresponding to the constructed structure SKB is increased.
[0103] Optionally, the SKB structure is used to transmit network data packets.
[0104] Optionally, each time a structure SKB is constructed in at least one memory block, a reference count of the memory block corresponding to the constructed structure SKB is increased by 1.
[0105] For example, if the initial value of the first memory block in at least one memory block is 1, and a structure SKB is constructed on the first memory block, the reference count of the first memory block changes from the initial value 1 to the initial value 1 plus 1, which is 2. If the electronic device 100 constructs two structures SKB on the first memory block, the reference count of the first memory block is 3. Similarly, if the electronic device 100 constructs a total of 27 structures SKB on the first memory block, the reference count of the first memory block is 28.
[0106] Step 403: When a structure SKB is released, the reference count of the memory block corresponding to the released structure SKB is reduced.
[0107] Optionally, when all structures SKB are released, the reference count of the memory block corresponding to the released structure SKB is reduced to the initial value.
[0108] For example, if the electronic device 100 constructs a total of 27 structures SKB on the first memory block, the reference count corresponding to the first memory block is 28. When one of the 27 structures SKB on the first memory block is released, the reference count of the first memory block changes from 28 to 27. When all 27 structures SKB are released, the reference count of the first memory block decreases to the initial value 1.
[0109] It should be noted that the embodiment of the present application does not specifically limit the value by which the reference count of the memory block is increased and the value by which the reference count of the memory block is decreased. It can be increased by 1, increased by 2, increased by 3, or decreased by 1, decreased by 2, decreased by 3, etc., but the value by which the reference count of the memory block is increased must be equal to the value by which the reference count of the memory block is decreased.
[0110] Optionally, when the reference count of the memory block in the local linked list is reduced to the initial value, the structure SKB is allowed to be constructed on the memory block with the reference count as the initial value.
[0111] The above-mentioned memory usage method provided in the embodiment of the present application can apply for memory allocation only once, and by increasing the reference count each time a structure SKB is constructed and reducing the reference count when releasing, the reference count of the memory block applied for allocation is not zero, which is equivalent to not releasing the memory block applied for allocation. Then, a new structure SKB can be constructed on the memory block with the reference count as the initial value, and the memory block that has been applied for allocation can be reused repeatedly, which can avoid frequent application and release of memory when sending and receiving network data, and reduce the processor occupancy rate of the electronic device 100. Moreover, when the structure SKB passed by the electronic device 100 is not released in time, the electronic device 100 can use the memory block with the reference count reduced to the initial value to receive new data packets, which will not cause a large amount of memory accumulation and avoid the occurrence of memory overflow exceptions.
[0112] Figure 5 This is a flow chart of a memory usage method provided by another embodiment of the present application. Figure 5 As shown, in step 402, when a structure SKB is constructed in at least one memory block, after increasing the reference count of the memory block corresponding to the constructed structure SKB, the method further includes:
[0113] Step 501: Generate a descriptor corresponding to the structure SKB. The descriptor is used to represent parameters required for data transfer between the network data packet and the structure SKB.
[0114] It is understandable that when the electronic device 100 uses DMA to synchronize the data received and sent by the network card with the kernel space, the address of the memory block where the structure SKB is located can be pointed to through the descriptor.
[0115] For example, the descriptor may include at least the address of the memory block where the structure SKB is located and the size of the structure SKB. The descriptor may also include a control bit for indicating whether the processor or DMA control is valid.
[0116] Optionally, the descriptor may correspond one-to-one with the SKB structure, for example, one SKB structure corresponds to one descriptor.
[0117] Optionally, the electronic device 100 stores the generated descriptor in a storage medium of the network card. For example, the electronic device 100 may store the descriptor in a register of the network card. The electronic device 100 may also store the generated descriptor in an IP hardware accelerator. The IP hardware accelerator is used to read network data at high speed and can be integrated inside or outside the network card.
[0118] Optionally, the network card may store multiple descriptors in a queue manner.
[0119] It is understandable that in order to increase processing speed and efficiency, the network card can process multiple network data packets in batches at one time, so multiple descriptors are required for processing.
[0120] Optionally, it is determined whether the number of generated descriptors is less than a descriptor number threshold. The descriptor threshold may be a maximum number of descriptors that can be batch processed at one time within the network card.
[0121] Exemplarily, the descriptor threshold may be 32, 64, 128, etc. The embodiment of the present application does not impose any specific limitation on the descriptor quantity threshold.
[0122] Optionally, when the number of generated descriptors is less than a descriptor number threshold, proceed to step 402 .
[0123] As will be appreciated, the network card can receive and send data through a first-in, first-out (FIFO) register, where Tx FIFO represents the transmit FIFO register and Rx FIFO represents the receive FIFO register. The total size of the at least one memory block requested for allocation by the electronic device 100 must be at least greater than or equal to the fill depth of the network card FIFO register.
[0124] Optionally, it is determined whether the total size of the at least one memory block requested for allocation is less than the filling depth of the FIFO register.
[0125] Optionally, when the total size of at least one memory block applied for allocation is smaller than the filling depth of the FIFO register, step 401 is entered.
[0126] Optionally, when the total size of at least one memory block applied for allocation is greater than or equal to the filling depth of the FIFO register, the network data packet can be received or sent.
[0127] Step 502: Obtain the number of consumptions, where the number of consumptions is the number of descriptors consumed by data transfer between the network data packet and the structure SKB.
[0128] It can be understood that the synchronization of network data between the network card and the kernel space of the electronic device 100 requires the description of the address indicating the network data transmission.
[0129] For example, when a network card receives a network data packet, the process is as follows:
[0130] 1. Determine whether there is data in the FIFO register. If there is data, the DMA controller searches for available descriptors in the descriptor queue;
[0131] 2. Write the data in the network packet to the physical address of the structure SKB pointed to by the available descriptor, that is, match the address pointed to by the descriptor with the address of the structure SKB corresponding to the descriptor;
[0132] 3. Set the structure SKB to complete the encapsulation of the network data packet corresponding to the descriptor, such as data reservation skb_reserve and data alignment skb_put;
[0133] 4. Pass the encapsulated structure SKB to the device driver layer of the network subsystem.
[0134] Optionally, after the network data packet is written to the structure SKB pointed to by the usable descriptor, the usable descriptor can be put back into the descriptor queue and the next usable descriptor in the descriptor queue can be searched.
[0135] Optionally, the electronic device 100 may count the number of descriptors consumed when network data in the network packet is written to the physical address of the corresponding structure SKB. For example, the electronic device 100 may use atomic operations to increase the number of descriptors consumed by 1 when a descriptor is consumed.
[0136] Step 503: When the number of consumption times is greater than or equal to the first threshold, memory recycling is performed.
[0137] Optionally, the first threshold may be 64, 128, 256, etc. This application does not limit the specific value of the first threshold.
[0138] Exemplarily, the first threshold is 64. When the current consumption count is 68 times, the electronic device 100 is triggered to recycle memory and reuse the memory block whose reference count in the local linked list is reduced to the initial value to receive network data in the network card.
[0139] It should be noted that the process of sending network data is similar to the process of receiving network data, except that the transmission direction of the network data is opposite. For example, when the network data is sent, the driver of the electronic device 100 transfers the data of the structure SKB to the FIFO register of the network card pointed to by the descriptor, which will not be repeated here.
[0140] Through the above-mentioned memory usage method, the embodiment of the present application can determine the timing for the electronic device 100 to recycle memory by counting the number of consumption times, and then can repeatedly reuse at least one memory block that was previously allocated, avoiding the electronic device 100 from releasing the allocated memory block and avoiding the allocation of new memory blocks, thereby reducing the occupancy rate of the processor of the electronic device 100 and avoiding memory overflow anomalies, thereby improving user experience.
[0141] Figure 6 This is a flow chart of a memory usage method provided by another embodiment of the present application. Figure 6 As shown, in step 503, when the number of consumption times is greater than or equal to the first threshold, memory recycling is performed, including:
[0142] Step 601: traverse the local linked list to determine whether there is a memory block whose reference count has been reduced to the initial value.
[0143] Optionally, when there is no memory block in the local linked list whose reference count is reduced to the initial value, step 601 may be re-entered after waiting for the first time.
[0144] For example, the first time period may be 5ms, 6ms, or 7ms. The embodiments of the present application do not limit the specific value of the first time period. When the local linked list does not contain a memory block whose reference count has been reduced to the initial value, the electronic device 100 may wait for 5ms and then re-traverse the local linked list to determine whether there is a memory block whose reference count has been reduced to the initial value.
[0145] Step 602 : When there is a memory block whose reference count is reduced to the initial value, a structure SKB is constructed on the memory block whose reference count is reduced to the initial value, and a reference count of the memory block whose reference count is reduced to the initial value is increased.
[0146] Optionally, each time a structure SKB is constructed on a memory block whose reference count is reduced to an initial value, the reference count of the memory block whose reference count is reduced to the initial value is increased.
[0147] Through the above-mentioned memory usage method, the embodiment of the present application can traverse the local linked list to check whether there is a memory block whose reference count has been reduced to the initial value, and obtain whether there is a reusable memory block in at least one memory block that has been applied for allocation. If so, the structure SKB can be constructed on the reusable memory block and the reference count can be increased, so that the reference count of the reusable memory block will not become zero and be completely released, and can be recycled again.
[0148] Optionally, when there is a memory block whose reference count is reduced to the initial value, after constructing the structure SKB on the memory block whose reference count is reduced to the initial value and increasing the reference count of the memory block whose reference count is reduced to the initial value, the following may be further included:
[0149] Step 603: Obtain a first quantity, where the first quantity is the number of structures SKB constructed on the memory blocks whose reference counts are reduced to the initial value.
[0150] Step 604: Update the number of consumptions according to the difference between the number of consumptions and the first quantity.
[0151] It is understood that the memory usage method provided in this embodiment of the present application counts the cumulative number of consumptions. Therefore, after memory is reclaimed, the cumulative number of consumptions must be updated to prevent the number of consumptions from being consistently greater than the first threshold. Furthermore, step 602 generates a first number of new descriptors for consumption, which is equivalent to reducing the first number of consumptions required.
[0152] Step 605: When the updated consumption count is greater than or equal to the second threshold, a memory recycling step is triggered. The memory recycling step may include steps 601 to 604.
[0153] Optionally, the second threshold is smaller than the first threshold. For example, when the first threshold is 64, the second threshold may be 32. When the first threshold is 128, the second threshold may be 32 or 64, etc.
[0154] Optionally, the second threshold may be a difference between the first threshold and the first data amount. For example, when the first threshold is 64 and the first data amount is 32, the second threshold may be 32.
[0155] The above-mentioned memory usage method provided in the embodiment of the present application can further cyclically reduce the number of consumptions by triggering the step of memory recycling when the number of consumptions after the update is greater than or equal to the second threshold, so that the number of consumptions after the update is greatly reduced, and more memory blocks whose reference counts are reduced to the initial value can be recycled, thereby increasing the utilization efficiency of the allocated memory blocks and avoiding frequent memory recycling.
[0156] For example, if the current consumption count is 66, the first threshold is 64, the first quantity is 32, and the second threshold is 32, then the consumption count 66 is greater than the first threshold 64, and memory recycling is performed. The updated consumption count is the difference between the consumption count 66 and the first quantity 32, which is 34. If step 605 is not performed, the updated consumption count is 34. At this time, there may still be memory blocks in the local linked list whose reference counts have been reduced to the initial value and have not been recycled. On the other hand, when the number of descriptors consumed for receiving network data is 30, the current consumption count is 64, which is equal to the first threshold 64, and memory recycling is triggered again, that is, step 601 is re-entered, causing the electronic device 100 to frequently recycle memory, increasing the processor occupancy of the electronic device 100. However, if step 605 is performed, the updated consumption count 34 is greater than the second threshold 32, and step 601 is re-entered. Step 605 is entered again, and the updated consumption count is 2, which is less than the second threshold 32. There is no need to re-enter step 601. This not only allows more memory blocks with reference counts reduced to the initial value to be utilized as much as possible, but also avoids frequent memory recycling.
[0157] Optionally, before obtaining the first quantity in step 603, the memory usage method may further include:
[0158] Step 606: Generate a descriptor corresponding to the structure SKB.
[0159] Step 607: Determine whether the number of generated descriptors is less than a descriptor number threshold. The descriptor number threshold may be the maximum number of descriptors that can be batch processed in the network card at one time.
[0160] Step 608 : When it is determined that the number of generated descriptors is less than the descriptor number threshold, proceed to step 602 .
[0161] Step 609 : When it is determined that the number of generated descriptors is greater than or equal to the descriptor number threshold, proceed to step 603 .
[0162] The memory usage method provided in the embodiment of the present application can ensure that the first number of the structure SKB constructed in step 602 is the descriptor number threshold through steps 606 to 607.
[0163] Optionally, the second threshold may also be a descriptor quantity threshold.
[0164] The foregoing description of this specification describes specific embodiments. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0165] Figure 7 This is a schematic diagram of the structure of an electronic device provided in one embodiment of the present invention. Figure 7 As shown, the electronic device 100 includes: a storage module 701 , a first operating module 702 and a second operating module 703 .
[0166] The storage module 701 is used to store the address of at least one memory block applied for allocation into a local linked list, and the reference count of at least one memory block is an initial value greater than zero.
[0167] The first operation module 702 is configured to increase a reference count of a memory block corresponding to the constructed structure SKB each time a structure SKB is constructed in at least one memory block. The structure SKB is used to transmit network data packets.
[0168] The second operation module 703 is configured to reduce the reference count of the memory block corresponding to each released SKB structure. When the reference count of the memory block in the local linked list is reduced to the initial value, the SKB structure is allowed to be constructed on the memory block with the initial reference count.
[0169] Optionally, the electronic device 100 further includes:
[0170] The generation module is used to generate the descriptor of the corresponding structure SKB. The descriptor is used to represent the parameters required for data transfer between the network data packet and the structure SKB.
[0171] The acquisition module is used to obtain the number of consumption times, which is the number of descriptors consumed by data transfer between the network data packet and the structure SKB.
[0172] The memory recycling module is used to recycle memory when the number of consumption times is greater than or equal to a first threshold.
[0173] Optionally, the memory recycling module includes:
[0174] The traversal unit is used to traverse the local linked list to determine whether there is a memory block whose reference count is reduced to the initial value.
[0175] Construction unit, when there is a memory block whose reference count is reduced to the initial value, construct structure SKB on the memory block whose reference count is reduced to the initial value, and increase the reference count of the memory block whose reference count is reduced to the initial value.
[0176] Optionally, the memory recycling module further includes:
[0177] An acquisition unit is used to acquire a first quantity, where the first quantity is the number of structures SKB constructed on the memory block whose reference count is reduced to an initial value.
[0178] an updating unit, configured to update the number of consumptions according to a difference between the number of consumptions and the first number;
[0179] The trigger unit is used to trigger the step of memory recycling when the updated consumption count is greater than or equal to a second threshold.
[0180] Figure 7 The electronic device 100 provided in the embodiment shown can be used to execute the present invention. Figures 4 to 6 The technical solution of the method embodiment shown, its implementation principle and technical effects can be further referred to the relevant description in the method embodiment.
[0181] The embodiment of the present application also provides a chip system, comprising: a communication interface for inputting and / or outputting information; a processor for executing a computer executable program so that a device equipped with the chip system executes the following Figures 4 to 6 The processor may be a chip or a chip module.
[0182] The debugging method provided in the embodiments of the present application can be executed by the following devices: a chip or a chip module. Regarding the various modules / units contained in the various devices and products described in the above embodiments, they can be software modules / units, hardware modules / units, or partly software modules / units and partly hardware modules / units. For example, for various devices and products applied to or integrated in a chip, the various modules / units contained therein can all be implemented in the form of hardware such as circuits, or at least some of the modules / units can be implemented in the form of software programs, which run on a processor integrated inside the chip. Different modules / units may be located in the same component (e.g., chip, circuit module, etc.) or in different components of the chip module, or at least some of the modules / units may be implemented in the form of a software program that runs on a processor integrated inside the chip module, and the remaining (if any) modules / units may be implemented in the form of hardware such as circuits; for various devices and products applied to or integrated in a terminal, the various modules / units contained therein may all be implemented in the form of hardware such as circuits, and different modules / units may be located in the same component (e.g., chip, circuit module, etc.) or in different components within the terminal, or at least some of the modules / units may be implemented in the form of a software program that runs on a processor integrated inside the terminal, and the remaining (if any) modules / units may be implemented in the form of hardware such as circuits.
[0183] Figure 8 This is a schematic diagram of the structure of an electronic device provided in another embodiment of this specification. Figure 8 As shown, the electronic device 100 may include at least one processor; and at least one memory in communication with the processor, wherein: the memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to execute the instructions of this specification. Figures 4 to 6 The illustrated embodiment provides a memory usage method.
[0184] The electronic device 100 may be a mobile phone, a tablet, a notebook, a desktop computer, a server, a smart TV, a router, etc. This embodiment does not limit the form of the electronic device 100.
[0185] For example, Figure 8 The structural diagram of the electronic device 100 is shown by taking a notebook as an example. Figure 8As shown, the processor 801 may include one or more processing units, for example, the processor 801 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), etc. Different processing units may be independent devices or integrated into one or more processors.
[0186] The memory 802 may be used to store computer executable program codes, which include instructions. The processor 801 executes the instructions stored in the memory 802 to execute the instructions in this specification. Figures 4 to 6 The code merging method and steps shown. The memory 802 may include a program storage area and a data storage area. The program storage area may store an operating system, an application required for at least one function (such as a version control tool, a code storage function, etc.), etc. The data storage area may store data created during the code merging process (such as merging size, adjusted pre-storage space, etc.), etc. In addition, the memory 802 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0187] The present invention provides a non-transitory computer-readable storage medium that stores computer instructions that enable a computer to execute the instructions in this specification. Figures 4 to 6 The debugging method provided by the illustrated embodiment.
[0188] The above-mentioned non-transitory computer-readable storage medium can adopt any combination of one or more computer-readable media. The computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination thereof. More specific examples (non-exhaustive list) of computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM) or a flash memory, an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In this document, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by an instruction execution system, device or device or used in combination with it.
[0189] Computer program code for carrying out the operations of this specification may be written in one or more programming languages, or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, and C++, as well as conventional procedural programming languages such as "C" or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer, or entirely on the remote computer or server.
[0190] In the description of the embodiments of the present invention, the reference terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" mean that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of this specification. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described can be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art can combine and combine different embodiments or examples described in this specification and features of different embodiments or examples without contradiction.
[0191] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of the technical features being referred to. Thus, a feature specified as "first" or "second" may explicitly or implicitly include at least one such feature. Throughout this specification, "plurality" means at least two, such as two or three, unless otherwise specifically defined.
[0192] The above are only preferred embodiments of this specification and are not intended to limit this specification. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of this specification should be included in the scope of protection of this specification.
Claims
1. A memory usage method, characterized in that: Used in electronic equipment, including: The address of at least one memory block requested for allocation is stored in a local linked list, wherein the reference count of the at least one memory block is an initial value greater than zero; When a structure SKB is constructed in the at least one memory block, a reference count of the memory block corresponding to the structure SKB is increased, and the structure SKB is used to transmit network data packets; When each of the structures SKB is released, the reference count of the memory block corresponding to the released structure SKB is reduced, wherein when the reference count of the memory block in the local linked list is reduced to the initial value, the structure SKB is allowed to be constructed on the memory block with the reference count being the initial value; After increasing the reference count of the memory block corresponding to the structure SKB each time a structure SKB is constructed in the at least one memory block, the method further includes: Generate a descriptor corresponding to the structure SKB, where the descriptor is used to represent parameters required for data transfer between the network data packet and the structure SKB; Obtaining the number of consumptions, where the number of consumptions is the number of descriptors consumed by data transfer between the network data packet and the structure SKB; When the number of consumption times is greater than or equal to a first threshold, memory recycling is performed.
2. The method according to claim 1, characterized in that When the number of consumption times is greater than or equal to a first threshold, memory recycling is performed, including: Traversing the local linked list to determine whether there is a memory block whose reference count is reduced to the initial value; When there is a memory block whose reference count is reduced to the initial value, the structure SKB is constructed on the memory block whose reference count is reduced to the initial value, and the reference count of the memory block whose reference count is reduced to the initial value is increased.
3. The method according to claim 2, characterized in that After constructing the structure SKB on the memory block whose reference count is reduced to the initial value and increasing the reference count of the memory block whose reference count is reduced to the initial value, the method further includes: Obtain a first quantity, where the first quantity is the number of memory blocks where the reference count is reduced to the initial value and the structure SKB is constructed; updating the consumption times according to the difference between the consumption times and the first quantity; When the updated consumption count is greater than or equal to the second threshold, a memory recycling step is triggered.
4. An electronic device, characterized in that: include: A storage module, configured to store the address of at least one memory block applied for allocation into a local linked list, wherein the reference count of the at least one memory block is an initial value greater than zero; A first operation module is configured to increase a reference count of a memory block corresponding to the structure SKB when constructing a structure SKB in the at least one memory block, wherein the structure SKB is used to transmit a network data packet; A second operation module is configured to reduce the reference count of the memory block corresponding to the released structure SKB each time the structure SKB is released, wherein when the reference count of the memory block in the local linked list is reduced to the initial value, the structure SKB is allowed to be constructed on the memory block with the reference count being the initial value; The electronic device further includes: A generation module, configured to generate a descriptor corresponding to the structure SKB, wherein the descriptor is used to represent parameters required for data transfer between a network data packet and the structure SKB; An acquisition module, configured to acquire a consumption count, where the consumption count is the number of descriptors consumed by data transfer between the network data packet and the structure SKB; The memory recycling module is used to recycle memory when the number of consumption times is greater than or equal to a first threshold.
5. The electronic device according to claim 4, characterized in that The memory recycling module includes: A traversal unit, configured to traverse the local linked list to determine whether there is a memory block whose reference count is reduced to the initial value; The construction unit constructs the structure SKB on the memory block whose reference count is reduced to the initial value when there is a memory block whose reference count is reduced to the initial value, and increases the reference count of the memory block whose reference count is reduced to the initial value.
6. The electronic device according to claim 5, characterized in that The memory recycling module further includes: An acquiring unit, configured to acquire a first quantity, where the first quantity is the number of memory blocks whose reference counts are reduced to the initial value and on which the structure SKB is constructed; an updating unit, configured to update the number of consumptions according to a difference between the number of consumptions and the first quantity; The trigger unit is used to trigger the step of memory recycling when the updated consumption count is greater than or equal to a second threshold.
7. An electronic device, characterized in that: include: at least one processor; as well as at least one memory in communication with the processor, wherein: The memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to execute the method according to any one of claims 1 to 3.
8. A non-transitory computer-readable storage medium, characterized in that The non-transitory computer-readable storage medium stores computer instructions, which cause the computer to execute the method according to any one of claims 1 to 3.
Citation Information
Patent Citations
SKB (Struct sk_buff) recycling method and device
CN105939293A
Socket cache resource management method and apparatus
CN108121598A
Caching socket connections while ensuring that there are file descriptors available for the applications
US20130066928A1