A method and device for counting CPU utilization
Through the method of scheduled interrupts and dynamic adjustment of interrupt frequency, the problem of hardware dependence and operating system modification of CPU utilization statistics in the existing technology is solved, high-precision CPU utilization statistics are achieved, and additional costs and system impacts are avoided.
Patent Information
- Application Number
- CN202011295305.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-18
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2040-11-18
AI Technical Summary
In the existing technology, counting CPU utilization requires additional independent hardware or modifying the source code of the operating system, which results in high cost, inoperability, and insufficient calculation refinement, making it impossible to count CPU utilization accurately and without dependency.
Use scheduled interrupts to pause the operating system, observe the task status at the moment of interruption, calculate the CPU utilization through the interrupt frequency and count value of the interrupt timer, and dynamically adjust the interrupt frequency to improve statistical accuracy and avoid additional hardware and source code modifications.
It achieves accurate CPU utilization statistics without additional hardware and operating system modifications, improves the credibility and accuracy of statistics, and reduces the burden on the system.
Smart Images

Figure CN114518991B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of embedded software programming, and in particular to a method and device for counting CPU utilization. Background Art
[0002] In a real-time operating system, it is necessary to know the specific situation of CPU (Central Processing Unit) utilization, including the CPU utilization of each task, interrupt, and kernel. CPU utilization is mainly used in three aspects: assisting the real-time system to reasonably design the priority of each task to achieve the purpose of real-time scheduling; optimizing performance: it can help to conduct targeted analysis of tasks with high CPU utilization and provide optimization solutions; in engineering, it is often encountered that the CPU utilization is abnormal due to the design and coding problems of programmers, and this kind of fault can be checked with the help of CPU utilization statistics.
[0003] However, the existing technology has the following disadvantages: using additional independent hardware to count CPU utilization not only increases hardware costs, but also often does not have operability at the debugging site because hardware devices need to be added; the calculated CPU utilization is physical utilization, which is not refined enough and has little significance for solving engineering problems. Using the statistical function provided by the operating system to indirectly count CPU utilization requires the operating system to provide statistical functions. If the operating system does not provide the corresponding statistical function, the operating system source code needs to be modified, which requires a deep understanding of the principles of the modified operating system, is technically difficult, and once the modification is wrong, it is very easy to cause the operating system to be unstable, which is not worth the loss.
[0004] Therefore, it is very important for real-time operating systems to design a method for counting CPU utilization that has good performance and is not dependent on the operating system. Summary of the invention
[0005] The embodiment of the present invention provides a method and device for counting CPU utilization, which is used to solve the problem that when counting CPU utilization, additional hardware needs to be purchased or the operating system source code needs to be modified, and the CPU utilization cannot be counted accurately and without dependencies.
[0006] In order to solve the above-mentioned technical problems, the present invention is achieved as follows:
[0007] In a first aspect, an embodiment of the present invention provides a method for counting CPU utilization, including:
[0008] When the interrupt timer is turned on, the operating system is interrupted according to the interrupt frequency of the interrupt timer;
[0009] Acquire the status of each task in the operating system at the interruption time, and count the status of each task at each interruption time to obtain a count value;
[0010] After the interrupt timer is turned off, the CPU utilization of each task is calculated according to the interrupt frequency and the count value of each task.
[0011] Optionally, before starting the interrupt timer, the method further includes: setting the interrupt frequency.
[0012] Optionally, setting the interrupt frequency includes: within the T period, floating the interrupt frequency upward or downward by a preset value to obtain a prime number closest to the preset value of the interrupt frequency.
[0013] Optionally, the status of the task includes: a running status and an interrupted status.
[0014] Optionally, the interrupt frequency is recorded as F, the count value is recorded as Fe, and the CPU utilization rate in the period T is Fe / F×100%.
[0015] In a second aspect, an embodiment of the present invention provides a device for counting CPU utilization, including:
[0016] An interrupt module is used to interrupt the operating system according to the interrupt frequency of the interrupt timer after the interrupt timer is turned on;
[0017] A counting module, used for obtaining the status of each task in the operating system at the interruption time, and counting the status of each task at each interruption time to obtain a count value;
[0018] The calculation module is used to calculate the CPU utilization of each task through the interrupt frequency and the count value of each task after the interrupt timer is turned off.
[0019] Optionally, the first processing module is used to set the interrupt frequency.
[0020] Optionally, the first processing module includes: a first processing submodule, used to float the interrupt frequency upward or downward by a preset value within a T period to obtain a prime number closest to the preset value of the interrupt frequency.
[0021] According to a third aspect, an electronic device is provided, comprising: a processor, a memory, and a program stored in the memory and executable on the processor, wherein when the program is executed by the processor, the steps of the method for counting CPU utilization as described in the first aspect are implemented.
[0022] In a fourth aspect, a computer-readable storage medium is provided, characterized in that a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the steps of the method for counting CPU utilization as described in the first aspect are implemented.
[0023] In an embodiment of the present invention, a timed interrupt method is used to suspend the operating system, and the operation status of the operating system is observed during the interruption, so as to count the CPU utilization rate. There is no need to purchase additional hardware or modify the operating system source code, so that the statistical process does not affect the operation of the system itself. At the same time, a method of dynamically adjusting the interrupt frequency is adopted to improve the accuracy of counting the CPU utilization rate. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present invention. Moreover, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0025] Figure 1 A schematic diagram of a method for counting CPU utilization provided by an embodiment of the present invention;
[0026] Figure 2 A schematic diagram of the architecture of a device for counting CPU utilization provided by an embodiment of the present invention;
[0027] Figure 3 An electronic device architecture diagram provided for an embodiment of the present invention. DETAILED DESCRIPTION
[0028] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0029] Please refer to Figure 1 , an embodiment of the present invention provides a method for counting CPU utilization, including:
[0030] Step 11: After the interrupt timer is started, the operating system is interrupted according to the interrupt frequency of the interrupt timer;
[0031] Step 12: Obtain the status of each task in the operating system at the interruption time, and count the status of each task at each interruption time to obtain a count value;
[0032] Step 13: After the interrupt timer is turned off, the CPU utilization of each task is calculated using the interrupt frequency and the count value of each task.
[0033] In an embodiment of the present invention, the CPU utilization is counted within a period T, and the period T is manually set according to the specific application and business needs. In step 11, the interrupt frequency is pre-set, and the higher the interrupt frequency of the timer is set, the smaller the observation interval is, and the more accurate and closer to the reality the statistical CPU utilization is, but an excessively high interrupt frequency will cause a large amount of CPU time to be consumed in the process of processing timer interrupts, resulting in deviations in the statistical results; since the resolution of the CPU utilization is one percent, the accuracy requirement itself is not high, and therefore the interrupt frequency selected to be set in the embodiment of the present invention is 1000HZ, the observation accuracy provided by the interrupt frequency is sufficient and the burden on the system can be ignored; in step 12, the status of each task in the operating system at the time of the interrupt is obtained through the API function provided by the operating system.
[0034] In the embodiment of the present invention, a timed interruption method is used to suspend the operating system, and the operation of the operating system is observed during the interruption, so as to count the CPU utilization rate. There is no need to purchase additional hardware or modify the operating system source code, so that the statistical process does not affect the operation of the system itself.
[0035] In the embodiment of the present invention, optionally, before starting the interrupt timer, the method further includes: setting an interrupt frequency.
[0036] In the embodiment of the present invention, optionally, setting the interrupt frequency includes: within the T period, floating the interrupt frequency upward or downward by a preset value to obtain a prime number closest to the preset value of the interrupt frequency.
[0037] In an embodiment of the present invention, in order to solve the problem that the timer frequency and the running frequency of the periodic task are easily in a multiple relationship due to the existence of a large number of periodic tasks in the operating system, a solution for dynamically adjusting the interrupt frequency is proposed, by making the interrupt frequency float up and down to a nearby prime number, for example: the preset value of the interrupt frequency is 1000HZ, and it can float up to 1001HZ or float down to 997HZ within a cycle, and ensure that the number of times the interrupt frequency floats up is roughly the same as the number of times it floats down, thereby avoiding the running frequency of the periodic task and improving the credibility of the CPU utilization statistics.
[0038] In the embodiment of the present invention, optionally, the state of the task includes: a running state and an interrupted state.
[0039] In the embodiment of the present invention, optionally, the interrupt frequency is recorded as F, the count value is recorded as Fe, and the CPU utilization rate in the period T is Fe / F×100%.
[0040] In an embodiment of the present invention, a timed interrupt method is used to suspend the operating system, and the operation status of the operating system is observed during the interruption, so as to count the CPU utilization rate. There is no need to purchase additional hardware or modify the operating system source code, so that the statistical process does not affect the operation of the system itself. At the same time, a method of dynamically adjusting the interrupt frequency is adopted to improve the accuracy of counting the CPU utilization rate.
[0041] Please refer to Figure 2 The embodiment of the present invention provides a device for counting CPU utilization, including:
[0042] An interrupt module 21 is used to interrupt the operating system according to the interrupt frequency of the interrupt timer after the interrupt timer is started;
[0043] The counting module 22 is used to obtain the status of each task in the operating system at the interruption time, and count the status of each task at each interruption time to obtain a count value;
[0044] The calculation module 23 is used to calculate the CPU utilization of each task according to the interrupt frequency and the count value of each task after the interrupt timer is turned off.
[0045] In an embodiment of the present invention, statistics are taken on CPU utilization within a period T, and the period T is manually set according to specific applications and business requirements. In the interrupt module 21, the interrupt frequency is pre-set, and the higher the interrupt frequency of the timer is set, the smaller the observation interval is, and the more accurate and closer to the truth the statistical CPU utilization is. However, an excessively high interrupt frequency will cause a large amount of CPU time to be consumed in the process of handling timed interrupts, resulting in deviations in the statistical results. Since the resolution of CPU utilization is one percent and the accuracy requirement itself is not high, the interrupt frequency selected and set in the embodiment of the present invention is 1000HZ, and the observation accuracy provided by the interrupt frequency is sufficient and the burden on the system can be ignored. In the counting module 22, the status of each task in the operating system at the time of interruption is obtained through the API function provided by the operating system.
[0046] In the embodiment of the present invention, a timed interruption method is used to suspend the operating system, and the operation of the operating system is observed during the interruption, so as to count the CPU utilization rate. There is no need to purchase additional hardware or modify the operating system source code, so that the statistical process does not affect the operation of the system itself.
[0047] In the embodiment of the present invention, optionally, the first processing module is used to set the interrupt frequency.
[0048] In the embodiment of the present invention, optionally, the first processing module includes: a first processing submodule, used to float the interrupt frequency upward or downward by a preset value within a T period to obtain a prime number closest to the preset value of the interrupt frequency.
[0049] In an embodiment of the present invention, in order to solve the problem that the timer frequency and the running frequency of the periodic task are easily in a multiple relationship due to the existence of a large number of periodic tasks in the operating system, a solution for dynamically adjusting the interrupt frequency is proposed, by making the interrupt frequency float up and down to a nearby prime number, for example: the preset value of the interrupt frequency is 1000HZ, and it can float up to 1001HZ or float down to 997HZ within a cycle, and ensure that the number of times the interrupt frequency floats up is roughly the same as the number of times it floats down, thereby avoiding the running frequency of the periodic task and improving the credibility of the CPU utilization statistics.
[0050] In an embodiment of the present invention, a timed interrupt method is used to suspend the operating system, and the operation status of the operating system is observed during the interruption, so as to count the CPU utilization rate. There is no need to purchase additional hardware or modify the operating system source code, so that the statistical process does not affect the operation of the system itself. At the same time, a method of dynamically adjusting the interrupt frequency is adopted to improve the accuracy of counting the CPU utilization rate.
[0051] Please refer to Figure 3 The embodiment of the present invention further provides an electronic device 30, including a processor 31, a memory 32, and a computer program stored in the memory 32 and executable on the processor 31. When the computer program is executed by the processor 31, each process of the above-mentioned method embodiment for counting CPU utilization is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be described here.
[0052] The embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, each process of the above-mentioned method embodiment for counting CPU utilization can be implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0053] It should be noted that, in this article, the terms "include", "comprises" or any other variations 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, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0054] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or an electronic device, etc.) to execute the methods described in each embodiment of the present invention.
[0055] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific implementation modes, which are merely illustrative rather than restrictive. Under the guidance of the present invention, ordinary technicians in this field can also make many forms without departing from the scope of protection of the present invention and the claims, all of which are within the protection of the present invention.
Claims
1. A method for counting CPU utilization, characterized in that: include: When the interrupt timer is turned on, the operating system is interrupted according to the interrupt frequency of the interrupt timer; Acquire the status of each task in the operating system at the interruption time, and count the status of each task at each interruption time to obtain a count value; After the interrupt timer is turned off, the CPU utilization of each task is calculated according to the interrupt frequency and the count value of each task; Before starting the interrupt timer, it also includes: Set the interrupt frequency; The interrupt frequency setting includes: In the T period, the interrupt frequency is floated upward or downward by a preset value to obtain a prime number closest to the preset value of the interrupt frequency.
2. The method for calculating CPU utilization statistics according to claim 1, characterized in that: The status of the task includes: a running status and an interrupted status.
3. The method for calculating CPU utilization statistics according to claim 1, characterized in that: The interruption frequency is recorded as F, and the count value is recorded as Fe; The CPU utilization rate in the period T is Fe / F×100%.
4. A device for counting CPU utilization, characterized in that: include: An interrupt module is used to interrupt the operating system according to the interrupt frequency of the interrupt timer after the interrupt timer is turned on; A counting module, used for obtaining the status of each task in the operating system at the interruption time, and counting the status of each task at each interruption time to obtain a count value; A calculation module, used for calculating the CPU utilization of each task according to the interrupt frequency and the count value of each task after the interrupt timer is turned off; A first processing module, used for setting an interrupt frequency; The first processing module comprises: The first processing submodule is used to float the interrupt frequency upward or downward by a preset value within a T period to obtain a prime number closest to the preset value of the interrupt frequency.
5. An electronic device, characterized in that: include: A processor, a memory, and a program stored in the memory and executable on the processor, wherein when the program is executed by the processor, the steps of the method for counting CPU utilization as claimed in any one of claims 1 to 3 are implemented.
6. 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 steps of the method for counting CPU utilization according to any one of claims 1 to 3 are implemented.
