A method, system, device and medium for data packet capture with reduced CPU consumption
By using single-threaded packet capture on the K8S cluster node and adjusting the priority according to the interface frequency, the problem of high CPU resource consumption caused by multi-thread scheduling is solved, and more efficient CPU usage and processing capabilities are achieved.
Patent Information
- Application Number
- CN202210405143.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-18
- Publication Date
- 2024-09-03
- Estimated Expiration
- 2042-04-18
AI Technical Summary
When performing multi-interface packet capture on nodes of a K8S cluster, multi-thread scheduling in the existing technology leads to excessive consumption of CPU resources, affecting network performance monitoring and economic benefits.
A single thread is used to capture data packets, and by setting multiple priority interface object groups, the priority is adjusted according to the capture frequency of the interface, and the data packets are captured in a cyclic order from high to low priority to reduce the extra CPU caused by multi-thread scheduling. consumption.
It effectively reduces CPU resource consumption, avoids the problem of reduced processing capacity due to large differences in multi-interface traffic, and improves CPU usage efficiency under the same processing capacity.
Smart Images

Figure CN114661561B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data packet capture for multiple interfaces by nodes of a K8S cluster, and in particular to a method, system, device and medium for data packet capture that reduces CPU consumption. Background Art
[0002] Nodes are the basic nodes for providing cloud services. Their CPU resources directly determine the number of services that the node can provide, which in turn affects economic benefits. When monitoring K8S network performance, it is necessary to capture a large number of data packets on the virtual interface of the Pod on the node, which consumes the node's CPU resources for capture.
[0003] In the prior art, when data packets are captured for multiple interfaces, a corresponding thread is created for each interface, and data packet capture is performed in the thread corresponding to each interface. This method is affected by the scheduling of the operating system. When the data traffic of each interface is greatly different, the operating system will frequently schedule each thread, resulting in additional resource consumption. At the same time, when multiple threads are running simultaneously, multiple CPU resources may be occupied, resulting in a large amount of consumption. Therefore, reducing the consumption of CPU resources during data packet capture can bring direct benefits. Summary of the invention
[0004] In view of this, the present invention proposes a method, system, device and medium for data packet capture that reduces CPU consumption. Among them, the method for data packet capture that reduces CPU consumption proposed by the present invention runs in one thread and captures data packets from multiple interfaces of a node according to an algorithm, effectively reducing the problem of additional CPU consumption caused by multi-thread scheduling.
[0005] Based on the above purpose, an aspect of an embodiment of the present invention provides a method for data packet capture that reduces CPU consumption, comprising the following steps: setting a first thread to capture data packets from multiple interfaces of a node; setting multiple priority interface object groups in the first thread; obtaining the priority level corresponding to the priority interface object group according to the frequency of capturing data packets from the interfaces in the priority interface object group; cyclically capturing the interfaces of the priority interface object group in the first thread in order from high to low priority levels; and stopping capturing data packets from the multiple interfaces of the node in response to the first thread receiving a stop signal.
[0006] In some embodiments, the method for data packet capture to reduce CPU consumption also includes: counting the continuous capture success probability and the continuous capture failure probability of the node's interface; setting a continuous capture success probability threshold and a continuous capture failure probability threshold for the node's interface; in response to the priority interface object group to which the current interface belongs not being the highest priority interface object group, and the continuous capture success probability of the current interface being greater than the continuous capture success probability threshold, adding the current interface to a priority interface object group of a higher priority level; in response to the priority interface object group to which the current interface belongs not being the lowest priority interface object group, and the continuous capture failure probability of the current interface being greater than the continuous capture failure probability threshold, adding the current interface to a priority interface object group of a lower priority level.
[0007] In some embodiments, the method for data packet capture to reduce CPU consumption also includes: in response to the priority interface object group to which the current interface belongs being the highest priority interface object group, and the continuous capture success probability of the current interface being greater than the continuous capture success probability threshold, setting the priority interface object group to which the current interface belongs to remain unchanged.
[0008] In some embodiments, the method for data packet capture to reduce CPU consumption also includes: in response to the priority interface object group to which the current interface belongs being the lowest priority interface object group, and the continuous capture failure probability of the current interface being greater than the continuous capture failure probability threshold, setting the priority interface object group to which the current interface belongs to remain unchanged.
[0009] In some embodiments, the cyclic capture of the interfaces of the priority interface object group in the first thread in order of priority levels from high to low includes: setting the number of cycles of the current priority interface object group according to the priority of the current priority interface object group; and cyclically capturing the interfaces of the priority interface object group in the first thread in order of priority levels from high to low according to the number of cycles.
[0010] In some embodiments, the method for packet capture to reduce CPU consumption also includes: in response to adding a new interface to a cluster, adding the new interface to a priority interface object group one level lower than the highest priority, sorting the interfaces in the priority interface object group one level lower than the highest priority in the chronological order of their addition, and adjusting the priority interface object group to which the new interface belongs based on the probability of capturing a packet from the new interface.
[0011] In some embodiments, the method for packet capture with reduced CPU consumption further includes: in response to shutting down an old interface in a cluster, removing the old interface from a priority interface object group to which the old interface belongs.
[0012] Another aspect of an embodiment of the present invention also provides a system for packet capture that reduces CPU consumption, comprising the following modules: a first module, configured to set a first thread to capture packets from multiple interfaces of a node; a second module, configured to set multiple priority interface object groups in the first thread; a third module, configured to obtain the priority level corresponding to the priority interface object group based on the frequency of capturing packets from the interfaces in the priority interface object group; a fourth module, configured to cyclically capture the interfaces of the priority interface object group in the first thread in order from high to low priority levels; and a fifth module, configured to stop capturing packets from multiple interfaces of the node in response to the first thread receiving a stop signal.
[0013] Another aspect of an embodiment of the present invention provides a computer device, comprising at least one processor; and a memory, wherein the memory stores computer instructions executable on the processor, and the instructions implement the steps of any of the above methods when executed by the processor.
[0014] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided, which stores a computer program that implements any of the above method steps when executed by a processor.
[0015] The present invention has at least the following beneficial effects: the present invention proposes a method, system, device and medium for data packet capture that reduces CPU consumption, wherein the method for data packet capture that reduces CPU consumption provided by the present invention is implemented using a single thread, and multiple priority interface object groups are set in the thread, and the interfaces of the priority interface object groups in the thread are traversed in a loop in order from high to low priority to capture data packets. This method effectively reduces the consumption of additional CPUs caused by multi-threaded scheduling, and a node will only use resources on one CPU, thereby effectively reducing CPU consumption under the same processing power. At the same time, because multiple priority interface object groups are introduced in a single thread, the problem of reduced processing power due to large differences in multi-interface traffic is avoided. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other embodiments can be obtained based on these drawings without paying creative work.
[0017] Figure 1 A schematic diagram of an embodiment of a method for capturing data packets to reduce CPU consumption provided by the present invention;
[0018] Figure 2 A schematic diagram of another embodiment of a method for capturing data packets to reduce CPU consumption provided by the present invention;
[0019] Figure 3 A schematic diagram of an embodiment of a system for capturing data packets with reduced CPU consumption provided by the present invention;
[0020] Figure 4 A schematic diagram of an embodiment of a computer device provided by the present invention;
[0021] Figure 5 A schematic diagram of an embodiment of a computer-readable storage medium provided by the present invention. DETAILED DESCRIPTION
[0022] The following describes embodiments of the present invention. However, it should be understood that the disclosed embodiments are merely examples and other embodiments can take various alternative forms.
[0023] In addition, it should be noted that all expressions using "first" and "second" in the embodiments of the present invention are intended to distinguish two entities with the same name but not the same or non-identical parameters. It can be seen that "first" and "second" are only for the convenience of expression and should not be understood as limitations on the embodiments of the present invention. The subsequent embodiments will not explain this one by one. The terms "include", "comprise" or any other variation thereof are intended to cover non-exclusive inclusions, so that a process, method, article or device that includes a series of elements includes not only those elements, but also elements that are not explicitly listed or inherent to these processes, methods, articles or devices.
[0024] One or more embodiments of the present application will be described below with reference to the accompanying drawings.
[0025] Based on the above objectives, a first aspect of an embodiment of the present invention provides an embodiment of a method for capturing data packets with reduced CPU consumption. Figure 1 FIG. 2 is a schematic diagram of an embodiment of a method for capturing data packets to reduce CPU consumption provided by the present invention. Figure 1 As shown, a method for capturing data packets to reduce CPU consumption according to an embodiment of the present invention comprises the following steps:
[0026] S1, setting a first thread to capture packets from multiple interfaces of a node;
[0027] S2. Setting a plurality of priority interface object groups in the first thread;
[0028] S3, obtaining the priority level corresponding to the priority interface object group according to the frequency of capturing data packets from the interfaces in the priority interface object group;
[0029] S4, cyclically capturing the interfaces of the priority interface object group in the first thread in descending order of priority levels;
[0030] S5. In response to the first thread receiving a stop signal, stop capturing data packets from multiple interfaces of the node.
[0031] Figure 2 FIG. 2 is a schematic diagram of another embodiment of a method for capturing data packets to reduce CPU consumption provided by the present invention. Figure 2 As shown, a method for capturing data packets to reduce CPU consumption according to an embodiment of the present invention includes:
[0032] Set the data packet capture of multiple interfaces on any node of the K8S cluster in a process, and set multiple priority interface object groups in the thread, so that the behavior of capturing data packets on each interface can be realized according to the capture algorithm. Arrange multiple priority interface object groups in order from low to high priority, from 1 to N, with N being the highest priority. It is understandable that N can be configured according to actual conditions.
[0033] The interfaces in the multiple priority interface object groups of the thread are captured cyclically, and the multiple priority interface object groups of the thread are captured cyclically in the order of priority level from high to low. The higher the priority level of the priority interface object group, the more times the loop is performed. First, the priority interface object group with the highest priority level in the thread is captured cyclically, and the capture behavior is performed once for each interface in each loop, and a total of 2^(N-1) loops are performed; the interface object group with a lower priority level is captured cyclically, and the capture behavior is performed once for each interface in each loop, and a total of 2^(N-2) loops are performed; the capture is performed cyclically in the order of priority level from high to low, until the interface object group with priority level 1 is cycled once, and then the priority interface object group with the highest priority level is returned to the cyclic capture again, until the thread receives an external stop signal, and the cyclic capture of the thread ends.
[0034] When capturing data packets for each interface in the priority interface object group in the thread, the capture may be successful, that is, a data packet is captured, or the capture may fail (the current interface traffic is small, and there is no data packet to be captured in the driver). Therefore, the probability of continuous capture success and the probability of continuous capture failure of each interface are counted as the basis for upgrading the interface (adjusting the priority interface object group to which the interface belongs to a higher priority interface object group) or downgrading (adjusting the priority interface object group to which the interface belongs to a lower priority interface object group). When the interface is just started, the number of continuous capture successes and the number of continuous capture failures are initialized to 0. When the interface captures a data packet, the number of continuous capture successes is increased by 1, and the number of continuous capture failures is cleared to 0; when the interface fails to capture, the number of continuous capture failures is increased by 1, and the number of continuous capture successes is cleared to 0. Within a period of time, the probability of continuous capture success and the probability of continuous capture failure of the interface are calculated by the sum of the number of continuous capture successes and the sum of the number of continuous capture failures. According to the actual situation, the continuous capture success probability threshold and the continuous capture failure probability threshold of the interface in the thread are set to O and P respectively. When the probability of continuous capture success of the interface is greater than 0, the interface is removed from the priority interface object group to which the interface belongs, and upgraded, that is, the interface is upgraded to a higher priority interface object group, and the number of continuous capture successes and the number of continuous capture failures are initialized to 0; when the probability of continuous capture failure of the interface is greater than P, the interface is removed from the priority interface object group to which the interface belongs, and downgraded, that is, the interface is downgraded to a lower priority interface object group, and the number of continuous capture successes and the number of continuous capture failures are initialized to 0. At the same time, the upgrade operation will not be performed on the interfaces in the highest priority interface object group, and the downgrade operation will not be performed on the interfaces in the lowest priority interface object group.
[0035] Interfaces can be added to the thread dynamically. When a new interface is added to the thread, the new interface is set to be added to a priority interface object group one level lower than the highest priority, and the nodes are sorted in the priority interface object group in the order of the time each node joins. At the same time, when a new interface is added to the interface object group, in the subsequent cyclic capture, as the traffic changes, the new interface may be upgraded to a higher priority interface object group or a lower priority interface object group, so that the capture frequency of the new interface can be adjusted in real time, thereby optimizing CPU consumption.
[0036] The interface can dynamically exit the thread. When an old interface exits the thread, the interface is directly removed from the priority interface object group to which the interface belongs.
[0037] Based on the above objectives, a second aspect of an embodiment of the present invention provides a system for capturing data packets with reduced CPU consumption. Figure 3 FIG. 2 is a schematic diagram of an embodiment of a system for capturing data packets with reduced CPU consumption provided by the present invention. Figure 3 As shown, a system for data packet capture that reduces CPU consumption provided by the present invention includes the following modules: a first module 011, configured to set a first thread to capture data packets from multiple interfaces of a node; a second module 012, configured to set multiple priority interface object groups in the first thread; a third module 013, configured to obtain the priority level corresponding to the priority interface object group according to the frequency of capturing data packets from the interfaces in the priority interface object group; a fourth module 014, configured to cyclically capture the interfaces of the priority interface object group in the first thread in order from high to low priority levels; and a fifth module 015, configured to stop capturing data packets from multiple interfaces of the node in response to the first thread receiving a stop signal.
[0038] Based on the above objectives, a third aspect of an embodiment of the present invention provides a computer device, Figure 4 FIG. 2 is a schematic diagram of an embodiment of a computer device provided by the present invention. Figure 4 As shown, an embodiment of a computer device provided by the present invention includes the following modules: at least one processor 021; and a memory 022, wherein the memory 022 stores computer instructions 023 that can be executed on the processor 021, and the computer instructions 023 implement the steps of any of the above methods when executed by the processor 021.
[0039] The present invention also provides a computer-readable storage medium. Figure 5 FIG. 2 is a schematic diagram of an embodiment of a computer-readable storage medium provided by the present invention. Figure 5 As shown, the computer-readable storage medium 031 stores a computer program 032 that performs the above method when executed by a processor.
[0040] Finally, it should be noted that a person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program, and the program of the method for setting system parameters can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium of the program can be a disk, an optical disk, a read-only storage memory (ROM) or a random access memory (RAM), etc. The above-mentioned computer program embodiments can achieve the same or similar effects as the corresponding above-mentioned arbitrary method embodiments.
[0041] In addition, the method disclosed in the embodiment of the present invention can also be implemented as a computer program executed by a processor, and the computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the above functions defined in the method disclosed in the embodiment of the present invention are performed.
[0042] In addition, the above method steps and system units may also be implemented using a controller and a computer-readable storage medium for storing a computer program that enables the controller to implement the above steps or unit functions.
[0043] It will also be appreciated by those skilled in the art that various exemplary logic blocks, modules, circuits and algorithm steps described in conjunction with the disclosure herein can be implemented as electronic hardware, computer software or a combination of the two. In order to clearly illustrate this interchangeability of hardware and software, a general description has been given to the functions of various schematic components, blocks, modules, circuits and steps. Whether this function is implemented as software or hardware depends on specific applications and the design constraints imposed on the entire system. Those skilled in the art can implement the function in various ways for each specific application, but this implementation decision should not be interpreted as causing a departure from the disclosed scope of the embodiments of the present invention.
[0044] In one or more exemplary designs, the function can be implemented in hardware, software, firmware or any combination thereof. If implemented in software, the function can be stored on a computer-readable medium or transmitted by a computer-readable medium as one or more instructions or codes. Computer-readable media include computer storage media and communication media, and the communication media include any media that helps to transmit a computer program from one location to another. The storage medium can be any available medium that can be accessed by a general or special computer. As an example and not limiting, the computer-readable medium may include RAM, ROM, EEPROM, CD-ROM or other optical disk storage devices, magnetic disk storage devices or other magnetic storage devices, or any other medium that can be used to carry or store the required program code in the form of an instruction or data structure and can be accessed by a general or special computer or a general or special processor. In addition, any connection can be appropriately referred to as a computer-readable medium. For example, if a coaxial cable, optical fiber cable, twisted pair, digital subscriber line (D0L) or wireless technologies such as infrared, radio and microwaves are used to send software from a website, server or other remote source, the above-mentioned coaxial cable, optical fiber cable, twisted pair, D0L or wireless technologies such as infrared, radio and microwaves are all included in the definition of the medium. As used herein, disk and disc include compact disc (CD), laser disc, optical disc, digital versatile disc (DVD), floppy disk, Blu-ray disc, wherein disks usually reproduce data magnetically, while optical discs reproduce data optically with lasers. Combinations of the above should also be included within the scope of computer-readable media.
[0045] The above are exemplary embodiments disclosed in the present invention, but it should be noted that various changes and modifications may be made without departing from the scope disclosed in the embodiments of the present invention as defined in the claims. The functions, steps and / or actions of the method claims according to the disclosed embodiments described herein do not need to be performed in any particular order. In addition, although the elements disclosed in the embodiments of the present invention may be described or required in individual form, they may also be understood as multiple unless explicitly limited to the singular.
[0046] It should be understood that, as used herein, the singular forms "a", "an" are intended to include the plural forms as well, unless the context clearly supports an exception. It should also be understood that, as used herein, "and / or" refers to any and all possible combinations including one or more of the associated listed items.
[0047] The serial numbers of the embodiments disclosed in the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0048] A person skilled in the art will understand that all or part of the steps to implement the above embodiments may be accomplished by hardware or by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a disk or an optical disk, etc.
[0049] A person skilled in the art should understand that the discussion of any of the above embodiments is only exemplary and is not intended to imply that the scope of the disclosure of the embodiments of the present invention (including the claims) is limited to these examples; under the concept of the embodiments of the present invention, the technical features in the above embodiments or different embodiments can also be combined, and there are many other changes in different aspects of the embodiments of the present invention as above, which are not provided in detail for the sake of simplicity. Therefore, any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the embodiments of the present invention should be included in the protection scope of the embodiments of the present invention.
Claims
1. A method for data packet capture with reduced CPU consumption, characterized in that: include: Setting a first thread to perform packet capture from a plurality of interfaces of a node; Setting a plurality of priority interface object groups in the first thread; Obtaining a priority level corresponding to the priority interface object group according to a frequency of capturing data packets from interfaces in the priority interface object group; Circularly capturing the interfaces of the priority interface object group in the first thread in descending order of priority levels; In response to the first thread receiving a stop signal, stopping capturing data packets from a plurality of interfaces of the node; The method also includes: Count the success probability and failure probability of continuous capture of the interface of the node; Setting a continuous capture success probability threshold and a continuous capture failure probability threshold of the interface of the node; In response to the priority interface object group to which the current interface belongs not being the highest priority interface object group, and the continuous capture success probability of the current interface being greater than the continuous capture success probability threshold, adding the current interface to a priority interface object group of a higher priority level; In response to the priority interface object group to which the current interface belongs not being the lowest priority interface object group and the continuous capture failure probability of the current interface being greater than the continuous capture failure probability threshold, the current interface is added to a priority interface object group of a lower priority level.
2. The method for capturing data packets with reduced CPU consumption according to claim 1, characterized in that: The method also includes: in response to the priority interface object group to which the current interface belongs being the highest priority interface object group and the continuous capture success probability of the current interface being greater than the continuous capture success probability threshold, setting the priority interface object group to which the current interface belongs to remain unchanged.
3. The method for capturing data packets with reduced CPU consumption according to claim 1, characterized in that: The method also includes: in response to the priority interface object group to which the current interface belongs being the lowest priority interface object group and the continuous capture failure probability of the current interface being greater than the continuous capture failure probability threshold, setting the priority interface object group to which the current interface belongs to remain unchanged.
4. The method for capturing data packets with reduced CPU consumption according to claim 1, characterized in that: The step of sequentially and cyclically capturing the interfaces of the priority interface object group in the first thread in descending order of priority level includes: Set the number of cycles of the current priority interface object group according to the priority of the current priority interface object group; The interfaces of the priority interface object group in the first thread are cyclically captured in descending order of priority according to the number of cycles.
5. The method for capturing data packets with reduced CPU consumption according to claim 1, characterized in that: Also includes: In response to adding a new interface to the cluster, the new interface is added to a priority interface object group one level lower than the highest priority, the interfaces are sorted in the priority interface object group one level lower than the highest priority in the chronological order of their addition, and the priority interface object group to which the new interface belongs is adjusted based on the probability of capturing data packets from the new interface.
6. The method for capturing data packets with reduced CPU consumption according to claim 1, characterized in that: Also includes: In response to shutting down an old interface in the cluster, the old interface is removed from a priority interface object group to which the old interface belongs.
7. A system for packet capture with reduced CPU consumption, characterized in that: include: A first module configured to set up a first thread to perform packet capture from a plurality of interfaces of a node; A second module is configured to set a plurality of priority interface object groups in the first thread; A third module is configured to obtain a priority level corresponding to the priority interface object group according to a frequency of capturing data packets from an interface in the priority interface object group; A fourth module is configured to cyclically capture the interfaces of the priority interface object group in the first thread in descending order of priority levels; as well as a fifth module configured to stop capturing data packets from the plurality of interfaces of the node in response to the first thread receiving a stop signal; And modules that perform the following functions: Count the success probability and failure probability of continuous capture of the interface of the node; Setting a continuous capture success probability threshold and a continuous capture failure probability threshold of the interface of the node; In response to the priority interface object group to which the current interface belongs not being the highest priority interface object group, and the continuous capture success probability of the current interface being greater than the continuous capture success probability threshold, adding the current interface to a priority interface object group of a higher priority level; In response to the priority interface object group to which the current interface belongs not being the lowest priority interface object group and the continuous capture failure probability of the current interface being greater than the continuous capture failure probability threshold, the current interface is added to a priority interface object group of a lower priority level.
8. A computer device, characterized in that: include: at least one processor; as well as A memory storing computer instructions executable on the processor, wherein the instructions, when executed by the processor, implement the steps of the method according to any one of claims 1 to 6.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Single-thread multi-task processing method based on synchronous I / O multiplexing mechanism
CN104199730A
Earthquake industry cluster equipment active monitoring system and polling mechanism
CN104980314A
Efficient data acquisition method based on RS485 communication protocol
CN110502461A
Method and device for improving processing efficiency of collected data and storage medium
CN112286653A
Queue scheduling method and system, equipment and medium
CN113590030A
