CPU idle time determination method and device, electronic equipment and medium
By judging the process of the polling task within each clock cycle and obtaining the shared memory mark value, the problem of inaccurate CPU idle time recording is solved, and the accurate evaluation of CPU utilization is achieved, avoiding misjudgment of the CPU busy situation.
Patent Information
- Application Number
- CN202411275906.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-11
- Publication Date
- 2025-01-03
Smart Images

Figure CN119248479A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of communication technology, and in particular to a method, device, electronic device and medium for determining CPU idle time. Background Art
[0002] The amount of traffic that the Internet and other interconnected networks must carry is growing continuously. To cope with this growth, the Transmission Control Protocol / Internet Protocol (TCP / IP) architecture has developed a Quality of Service (QoS) framework to support various types of traffic with various QoS requirements. The data plane is a plane in the QoS framework that can implement mechanisms for operating data flows such as queue management, queue scheduling, and congestion avoidance during the communication process, and plays a vital role in improving the efficiency of network communications.
[0003] For the data plane of network devices, in the traditional method, when the network card receives a data frame, it will store the data frame into the pre-allocated kernel buffer through direct memory access (DMA), and then the network card will update the receive descriptor ring (Buffer Descriptor Ring) to indicate that there is a message to be processed. The receive descriptor ring is a memory address negotiated between the network card and the operating system, which is used to describe the information of the message. The network card then reminds the operating system through an interrupt notification, so that the operating system obtains and processes the data frame based on the updated receive descriptor. The kernel of the operating system needs to switch the interrupt context to process the data frame. The system calls and data copying operations involved in this process will cause performance loss.
[0004] In order to reduce performance loss, an optimization solution for polling packet sending and receiving has emerged, such as the Data Plane Development Kit (DPDK). With DPDK, the CPU can poll to determine whether there are messages that need to be processed, without the need for interrupt context switching, and can reduce the overhead of data copying through user space I / O technology (Userspace I / O, UIO) and large page memory. However, the use of polling judgment will increase CPU consumption. When there are no messages that need to be processed, the CPU is still executing polling actions and is not processing data. In this case, the CPU is equivalent to idling, but the CPU utilization will be considered to be 100%, that is, the evaluation of the actual CPU utilization is inaccurate, and it is difficult to know the actual idle time of the CPU. Summary of the invention
[0005] The purpose of the embodiments of the present application is to provide a method, device, electronic device and medium for determining CPU idle time, so as to accurately determine the actual idle time of the CPU. The specific technical solution is as follows:
[0006] In a first aspect, an embodiment of the present application provides a method for determining CPU idle time, comprising:
[0007] At the beginning of each CPU clock cycle, the kernel determines whether there is a process of the polling task running;
[0008] If so, the kernel obtains the mark value of the polling task from the shared memory, where the shared memory is a physical memory shared by the user state and the kernel state. When the polling result of the polling task is that there is no message to be processed, the mark value is an idle mark;
[0009] If the flag value is an idle flag, the kernel records the current clock cycle as the idle time of the CPU.
[0010] In a possible implementation manner, after obtaining the tag value of the polling task from the shared memory through the kernel, the method further includes:
[0011] If the flag value is not an idle flag, the kernel records the current clock cycle as the execution time of the CPU.
[0012] In a possible implementation, the determining, by the kernel, whether there is a running polling task process includes:
[0013] Determining, by the kernel, whether there is a polling task mark in the shared memory;
[0014] If so, it is determined that there is a process of the polling task running;
[0015] If not, it is determined that there is no process of the polling task running.
[0016] In a possible implementation, the method further includes:
[0017] When the process of the polling task is started, a polling task mark is set in the shared memory by the process of the polling task;
[0018] If the polling result of the polling task is that there is no message to be processed, then judging whether the mark value in the shared memory is an idle mark through the process of the polling task, and if not, setting the mark value in the shared memory to the idle mark;
[0019] If the polling result of the polling task is that there is a message that needs to be processed, then the process of the polling task determines whether the mark value in the shared memory is an idle mark, and if so, cancels the idle mark.
[0020] In a possible implementation, the method further includes:
[0021] When the process of the polling task is started, the shared memory is applied to the kernel through the process of the polling task.
[0022] In a second aspect, an embodiment of the present application provides a device for determining CPU idle time, including:
[0023] The first judgment module is used to judge whether there is a running polling task process through the kernel at the beginning of each clock cycle of the CPU;
[0024] an acquisition module, configured to acquire, if the judgment result of the first judgment module is yes, a mark value of the polling task from a shared memory through the kernel, wherein the shared memory is a physical memory shared by a user state and a kernel state, and when the polling result of the polling task is that there is no message to be processed, the mark value is an idle mark;
[0025] A recording module is used to record the current clock cycle as the idle time of the CPU through the kernel if the mark value is an idle mark.
[0026] In a possible implementation, the recording module is further configured to record the current clock cycle as the execution time of the CPU through the kernel if the mark value is not an idle mark.
[0027] In a possible implementation, the first determination module is specifically configured to:
[0028] Determining, by the kernel, whether there is a polling task mark in the shared memory;
[0029] If so, it is determined that there is a process of the polling task running;
[0030] If not, it is determined that there is no process of the polling task running.
[0031] In a possible implementation manner, the device further includes:
[0032] A setting module, used for setting a polling task mark in the shared memory through the process of the polling task when the process of the polling task is started;
[0033] A second judgment module is used to judge whether the mark value in the shared memory is an idle mark through the process of the polling task if the polling result of the polling task is that there is no message to be processed;
[0034] The setting module is further configured to set the mark value in the shared memory as an idle mark if the judgment result of the second judgment module is no;
[0035] A third judgment module is used to judge whether the mark value in the shared memory is an idle mark through the process of the polling task if the polling result of the polling task is that there is a message that needs to be processed;
[0036] The setting module is further configured to cancel the idling mark if the judgment result of the third judgment module is yes.
[0037] In a possible implementation manner, the device further includes:
[0038] The application module is used to apply for the shared memory from the kernel through the process of the polling task when the process of the polling task is started.
[0039] In a third aspect, an embodiment of the present application further provides an electronic device, including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0040] Memory, used to store computer programs;
[0041] The processor is used to implement the method described in the first aspect when executing the program stored in the memory.
[0042] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method described in the first aspect is implemented.
[0043] In a fifth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute the method described in the first aspect above.
[0044] By adopting the above technical solution, in each clock cycle of the CPU, if there is a process of a polling task being run, the kernel can obtain the tag value of the polling task from the shared memory. Since the shared memory is shared between the user state and the kernel state, there is no need to notify the kernel by way of an interrupt, and there will be no interrupt context switching, which will not cause performance loss. Moreover, if the tag value obtained by the kernel is an idle tag, it means that the polling result of the current polling task is that there is no message to be processed, that is, the CPU is in an idle state in the current clock cycle, and the current clock cycle is recorded as the idle time of the CPU. In this way, the idle time of the CPU can be accurately recorded as the idle time, and the actual idle time of the CPU can be accurately obtained.
[0045] Of course, implementing any product or method of the present application does not necessarily require achieving all of the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the embodiments of the present application 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 described below are only some embodiments of the present application, and for ordinary technicians in this field, other embodiments can also be obtained based on these drawings.
[0047] Figure 1 A flowchart of a method for determining CPU idle time provided in an embodiment of the present application;
[0048] Figure 2 A schematic diagram of a processing flow of a polling task provided in an embodiment of the present application;
[0049] Figure 3 A flowchart of another method for determining CPU idle time provided in an embodiment of the present application;
[0050] Figure 4 A schematic diagram of the structure of a device for determining CPU idle time provided in an embodiment of the present application;
[0051] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0052] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field based on the present application belong to the scope of protection of the present application.
[0053] To facilitate understanding, the relevant terms involved in the embodiments of the present application are first introduced.
[0054] DPDK: is a framework consisting of various user space libraries and drivers. It is a collection of function libraries and drivers for fast data packet processing, which can greatly improve data processing performance and throughput, and improve the work efficiency of data plane applications.
[0055] DMA: refers to direct data transmission without going through the CPU.
[0056] QoS: refers to a network's ability to use various basic technologies to provide better service capabilities for specified network communications. It is a network security mechanism and a technology used to solve problems such as network delays and congestion.
[0057] Non-Maskable Interrupt (NMI): A hardware interrupt that cannot be disabled or masked and is triggered when a serious fault or emergency occurs in the system.
[0058] Large page memory: refers to dividing the system memory into pages (usually 2MB or 1GB) that are larger than normal pages (usually 4KB). Using large page memory can improve system performance, reduce the kernel's overhead of tracking page table entries, and reduce the probability of Translation Lookaside Buffer (TLB) misses.
[0059] TLB: is a high-speed cache memory management hardware uses to improve the speed of virtual address to physical address translation.
[0060] Clock tick: refers to the pulses emitted by the clock signal used in the system at a fixed frequency. These clock pulses are used to synchronize various components to ensure that they work according to a coordinated timeline.
[0061] CPU idling: This means that although the CPU is executing instructions in the running state, it is not actually processing business data, that is, it is not performing any useful computing work. In fact, the CPU can be considered to be in an idle state.
[0062] In the polling scheme for sending and receiving packets, the CPU is also performing polling tasks when there is no message processing. In this case, the CPU will be idle. Although the CPU is not currently processing messages, the CPU is still in operation when idling, and the CPU utilization rate is still considered to be 100%. In this way, it is difficult for developers to determine the bottleneck of device processing messages through CPU utilization, and for device users, they may mistakenly believe that the CPU is already too busy and cannot process more business.
[0063] In the related art, in order to more accurately determine the actual idle time of the CPU and then accurately determine the actual utilization of the CPU, the perf tool can be used to obtain the hot spot ratio of the CPU, and then use it to infer the actual idle time of the CPU and then infer the CPU utilization.
[0064] Then, the perf tool relies on NMI interrupts, which will interrupt the execution of business processes and is not suitable for use in the entire process of a production environment. In addition, if you need to count the actual idle time caused by CPU idling, you need to rely on developers to conduct further analysis in combination with the code of CPU hotspot services, which is relatively complex to implement. Therefore, it is still difficult to know the actual idle time of the CPU.
[0065] In order to solve the above problems, an embodiment of the present application provides a method for determining CPU idle time, which is applied to a CPU of an electronic device, and the electronic device can be an electronic device capable of processing messages, such as a router, a switch, etc. Figure 1 As shown, the method includes:
[0066] S101. At the beginning of each CPU clock cycle, the kernel determines whether there is a running polling task process.
[0067] If yes, execute S102; if no, wait for the next clock cycle and execute S101 in a loop.
[0068] S102: Obtain the tag value of the polling task from the shared memory through the kernel.
[0069] The shared memory is a physical memory shared by the user state and the kernel state. When the polling result of the polling task is that there is no message to be processed, the mark value is an idle mark.
[0070] S103: If the flag value is an idle flag, the kernel records the current clock cycle as the idle time of the CPU.
[0071] By adopting this method, in each clock cycle of the CPU, if there is a process of a polling task running, the mark value of the polling task can be obtained from the shared memory through the kernel. Since the shared memory is shared by the user state and the kernel state, there is no need to notify the kernel by interruption, and there will be no interrupt context switching, and thus no performance loss will be caused. Moreover, if the mark value obtained by the kernel is an idle mark, it means that the polling result of the current polling task is that there is no message to be processed, that is, the CPU is in an idle state in the current clock cycle, and then the current clock cycle is recorded as the idle time of the CPU. In this way, the idle time of the CPU can be accurately recorded as the idle time, and the actual idle time of the CPU can be accurately obtained.
[0072] In some embodiments of the present application, after the above S102, obtaining the tag value of the polling task from the shared memory through the kernel, the method further includes:
[0073] If the flag value is not the idle flag, the kernel records the current clock cycle as the CPU execution time.
[0074] It is understandable that if the tag value is not an idle tag, it means that the CPU is not in an idle state in the current clock cycle, but is processing received messages. Therefore, it can be determined that the CPU is truly utilized in the current clock cycle, and the current clock cycle is recorded as the CPU execution time.
[0075] In this way, when the CPU utilization is subsequently counted, the CPU execution time can be counted as the time when the CPU is actually used, instead of counting the CPU idling time as the time when the CPU is actually used, which can improve the accuracy of the subsequent CPU utilization statistics.
[0076] In some embodiments of the present application, in the above S101, the kernel determines whether there is a process of the polling task being run, which can be specifically implemented as follows:
[0077] The kernel determines whether there is a polling task mark in the shared memory. If yes, it is determined that there is a process of the running polling task; if not, it is determined that there is no process of the running polling task.
[0078] In this way, the kernel can directly obtain the polling task mark from the shared memory without interrupt context switching, and the process does not involve data copying between user state and kernel state, which can reduce performance loss and enable the kernel to easily and quickly determine whether there is a polling task process currently running.
[0079] In some embodiments of the present application, an idle flag and a polling task flag may be set in a shared memory by a process of a polling task, that is, the method further includes the following steps:
[0080] Step 1: When the process of the polling task is started, the polling task flag is set in the shared memory by the process of the polling task.
[0081] When the process of the polling task is started, the shared memory can be applied to the kernel through the process of the polling task, and then the kernel will allocate a section of physical memory shared by the user state and the kernel state as the shared memory.
[0082] Then the process of the polling task can set the polling task flag in the shared memory. As an example, the polling task flag can occupy 1 bit, and the value of the polling task flag can be 1.
[0083] It is understandable that after the polling task mark is set, the process of the polling task can normally execute the polling task, that is, determine whether there is a message that needs to be processed.
[0084] Step 2: If the polling result of the polling task is that there is no message to be processed, the process of the polling task determines whether the tag value in the shared memory is an idle tag. If not, the tag value in the shared memory is set to the idle tag.
[0085] Among them, if the polling result of the polling task is that there is no message that needs to be processed, it means that the CPU is currently idling. If the tag value in the current shared memory is the idle mark, there is no need to change the tag value and continue to execute the polling task; if the tag value in the shared memory is not the idle mark, the tag value is set to the idle mark.
[0086] As an example, the value of the idle flag is 1. That is, when the flag value in the shared memory is 1, it indicates that the CPU is in an idle state, and when the flag value in the shared memory is not 1, it indicates that the CPU is not in an idle state.
[0087] Step 3: If the polling result of the polling task is that there is a message that needs to be processed, the process of the polling task determines whether the mark value in the shared memory is an idle mark. If so, the idle mark is cancelled.
[0088] Among them, if the polling result of the polling task is that there is a message that needs to be processed, it means that the CPU is not currently in an idling state. If the tag value in the current shared memory is an idle tag, the idle tag is canceled, for example, the tag value can be set to 0, and then the message can be parsed and processed; if the tag value in the current shared memory is not an idle tag, the message can be directly parsed and processed.
[0089] By adopting this method, the process of the polling task can apply for shared memory when the polling task is started, and then the polling task can set the polling task flag and the idle flag in the shared memory. There is no need to notify the kernel of this information through system calls, etc., and it will not affect the processing of the business itself, which can reduce performance loss.
[0090] The following introduces the method provided in the embodiment of the present application by taking a polling task as an example.
[0091] like Figure 2 As shown, Figure 2 The process of processing the progress of a polling task includes the following steps:
[0092] S201, the process of the polling task is started.
[0093] S202: Set a polling task mark in the shared memory.
[0094] S203: Determine whether there is a message that needs to be processed.
[0095] Among them, when the process of the polling task is executed, it will interpret the information of the receive descriptor ring updated by the network card in the kernel buffer. If the receive descriptor ring indicates that there is a new message, it is determined that there is a message to be processed; if the receive descriptor ring indicates that there is no new message, it is determined that there is no message to be processed at present.
[0096] If yes, execute S206; if no, execute S204;
[0097] S204: Determine whether an idle flag is set in the shared memory.
[0098] If yes, return to S203; if no, execute S205.
[0099] S205: Set an idle flag in the shared memory.
[0100] After S205, S203 is executed.
[0101] S206: Determine whether an idle flag is set in the shared memory.
[0102] If yes, execute S207; if no, execute S208.
[0103] S207, cancel the idling mark.
[0104] After S207, continue to execute S208.
[0105] S208: parse and process the message.
[0106] By adopting this method, during the process of sending and receiving messages by the electronic device, the process of the polling task will poll to determine whether there are messages that need to be processed. If there are no messages that need to be processed, polling will continue. Through this method, the process of the polling task can set the polling time when there is no message processing as the idle time, thereby distinguishing the idle time from the time when the message processing is actually performed. By setting the idle mark, the actual time and ability of the CPU to process the message can be obtained.
[0107] During the polling task process execution Figure 2 Based on the process shown in FIG. 1 , the kernel can count the idle time or execution time of the CPU in each clock cycle according to the CPU clock cycle, such as Figure 3 As shown, Figure 3 The arrow from left to right in the figure indicates the direction of time increase. The kernel can record the time at each time beat (that is, at the beginning of each clock cycle). Figure 3 Taking one of the time beats as an example, the recording process includes the following steps:
[0108] S301: Determine whether a polling task process is currently running.
[0109] Among them, the kernel can search for the polling task mark from the shared memory. If the polling task mark exists, it means that the process of the polling task is currently running, and then S301 is executed; if the polling task mark does not exist, the process ends, that is, there is no need to record the idle time or execution time of the CPU in the current clock cycle in the manner of the embodiment of the present application. The current clock cycle can be directly counted according to the actual usage of the CPU in the current clock cycle according to the method in the relevant technology. For example, if the polling task is not executed in the current clock cycle, but the CPU is processing other services, the current clock cycle can be recorded as the actual usage time of the CPU according to the method in the relevant technology.
[0110] S302: Determine whether there is an idle mark in the shared memory.
[0111] If yes, execute S303; if no, execute S304.
[0112] S303: Record the current clock cycle as the idle time of the CPU.
[0113] S304: Record the current clock cycle as the CPU execution time.
[0114] By adopting this method, the kernel can record the idle time and execution time of the CPU according to the clock beat, that is, it can use the clock beat function of the operating system, which can reduce performance loss and obtain the actual idle time of the CPU without affecting the performance of existing services, thereby obtaining the actual CPU utilization.
[0115] Since the kernel records whether each clock cycle is CPU idle time or execution time, users can easily obtain detailed task time of polling tasks and more accurately obtain CPU time utilization. For example, when users query CPU usage, the CPU usage in each clock cycle can be displayed to users in the form of a file.
[0116] Furthermore, the use of the embodiments of the present application does not require modification of the open source software. The idle time and execution time of the CPU obtained by statistics in the embodiments of the present application can be monitored through existing software to calculate the actual utilization of the CPU, and the operation of the software will not affect the business.
[0117] Corresponding to the above method embodiment, the present application embodiment also provides a device for determining CPU idle time, such as Figure 4 As shown, the device comprises:
[0118] The first judgment module 401 is used to judge whether there is a running polling task process through the kernel at the beginning of each clock cycle of the CPU;
[0119] The acquisition module 402 is used for acquiring the mark value of the polling task from the shared memory through the kernel if the judgment result of the first judgment module 401 is yes, the shared memory is a physical memory shared by the user state and the kernel state, and when the polling result of the polling task is that there is no message to be processed, the mark value is an idle mark;
[0120] The recording module 403 is configured to record the current clock cycle as the idle time of the CPU through the kernel if the mark value is an idle mark.
[0121] Optionally, the recording module 403 is further configured to record the current clock cycle as the execution time of the CPU through the kernel if the mark value is not an idle mark.
[0122] Optionally, the first determination module 401 is specifically configured to:
[0123] Determining, by the kernel, whether there is a polling task mark in the shared memory;
[0124] If so, it is determined that there is a process of the polling task running;
[0125] If not, it is determined that there is no process of the polling task running.
[0126] Optionally, the device further comprises:
[0127] A setting module, used for setting a polling task mark in the shared memory through the process of the polling task when the process of the polling task is started;
[0128] A second judgment module is used to judge whether the mark value in the shared memory is an idle mark through the process of the polling task if the polling result of the polling task is that there is no message to be processed;
[0129] The setting module is further configured to set the mark value in the shared memory as an idle mark if the judgment result of the second judgment module is no;
[0130] A third judgment module is used to judge whether the mark value in the shared memory is an idle mark through the process of the polling task if the polling result of the polling task is that there is a message that needs to be processed;
[0131] The setting module is further configured to cancel the idling mark if the judgment result of the third judgment module is yes.
[0132] Optionally, the device further comprises:
[0133] The application module is used to apply for the shared memory from the kernel through the process of the polling task when the process of the polling task is started.
[0134] The present application also provides an electronic device, such as Figure 5 As shown, it includes a processor 501 , a communication interface 502 , a memory 503 and a communication bus 504 , wherein the processor 501 , the communication interface 502 , and the memory 503 communicate with each other via the communication bus 504 .
[0135] Memory 503, used for storing computer programs;
[0136] The processor 501 is used to implement the method steps in the above method embodiment when executing the program stored in the memory 503.
[0137] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0138] The communication interface is used for communication between the above electronic device and other devices.
[0139] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0140] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0141] In another embodiment provided in the present application, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above-mentioned methods for determining CPU idle time are implemented.
[0142] In another embodiment provided by the present application, a computer program product including instructions is also provided, which, when executed on a computer, enables the computer to execute any method for determining the CPU idle time in the above embodiments.
[0143] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive Solid State Disk (SSD)), etc.
[0144] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0145] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0146] The above description is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application are included in the protection scope of the present application.
Claims
1. A method for determining CPU idle time, characterized in that: include: At the beginning of each CPU clock cycle, the kernel determines whether there is a process of the polling task running; If so, the kernel obtains the mark value of the polling task from the shared memory, where the shared memory is a physical memory shared by the user state and the kernel state. When the polling result of the polling task is that there is no message to be processed, the mark value is an idle mark; If the flag value is an idle flag, the kernel records the current clock cycle as the idle time of the CPU.
2. The method according to claim 1, characterized in that After obtaining the tag value of the polling task from the shared memory through the kernel, the method further includes: If the flag value is not an idle flag, the kernel records the current clock cycle as the execution time of the CPU.
3. The method according to claim 1 or 2, characterized in that: The process of judging whether there is a polling task being run by the kernel includes: Determining, by the kernel, whether there is a polling task mark in the shared memory; If so, it is determined that there is a process of the polling task running; If not, it is determined that there is no process of the polling task running.
4. The method according to claim 1, characterized in that The method further comprises: When the process of the polling task is started, a polling task mark is set in the shared memory by the process of the polling task; If the polling result of the polling task is that there is no message to be processed, then judging whether the mark value in the shared memory is an idle mark through the process of the polling task, and if not, setting the mark value in the shared memory to the idle mark; If the polling result of the polling task is that there is a message that needs to be processed, then the process of the polling task determines whether the mark value in the shared memory is an idle mark, and if so, cancels the idle mark.
5. The method according to claim 4, characterized in that The method further comprises: When the process of the polling task is started, the shared memory is applied to the kernel through the process of the polling task.
6. A device for determining CPU idle time, characterized in that: include: The first judgment module is used to judge whether there is a running polling task process through the kernel at the beginning of each clock cycle of the CPU; an acquisition module, configured to acquire, if the judgment result of the first judgment module is yes, a mark value of the polling task from a shared memory through the kernel, wherein the shared memory is a physical memory shared by a user state and a kernel state, and when the polling result of the polling task is that there is no message to be processed, the mark value is an idle mark; A recording module is used to record the current clock cycle as the idle time of the CPU through the kernel if the mark value is an idle mark.
7. The device according to claim 6, characterized in that The recording module is further configured to record the current clock cycle as the execution time of the CPU through the kernel if the mark value is not an idle mark.
8. The device according to claim 6 or 7, characterized in that The first judgment module is specifically used to: Determining, by the kernel, whether there is a polling task mark in the shared memory; If so, it is determined that there is a process of the polling task running; If not, it is determined that there is no process of the polling task running.
9. The device according to claim 6, characterized in that The device also includes: A setting module, used for setting a polling task mark in the shared memory through the process of the polling task when the process of the polling task is started; A second judgment module is used to judge whether the mark value in the shared memory is an idle mark through the process of the polling task if the polling result of the polling task is that there is no message to be processed; The setting module is further configured to set the mark value in the shared memory as an idle mark if the judgment result of the second judgment module is no; A third judgment module is used to judge whether the mark value in the shared memory is an idle mark through the process of the polling task if the polling result of the polling task is that there is a message that needs to be processed; The setting module is further configured to cancel the idling mark if the judgment result of the third judgment module is yes.
10. The device according to claim 9, characterized in that The device also includes: The application module is used to apply for the shared memory from the kernel through the process of the polling task when the process of the polling task is started.
11. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, for implementing the method steps described in any one of claims 1 to 5 when executing a program stored in a memory.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in any one of claims 1 to 5 are implemented.
