Methods, devices, electronic equipment, and storage media for calculating CPU utilization

By obtaining the CPU execution time and available cores of a process, and combining the frequency point duration and the maximum processing capacity of the CPU, the CPU utilization is calculated, which solves the problem of overheating and stuttering caused by prolonged CPU stress and realizes the automation and accuracy improvement of CPU utilization.

CN114625620BActive Publication Date: 2026-03-10BEIJING DAJIA INTERNET INFORMATION TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-02-28
Publication Date
2026-03-10

AI Technical Summary

Technical Problem

In the existing technology, the CPU of electronic devices is under pressure for a long time, which leads to problems such as overheating and lag. There is a lack of effective methods for calculating CPU utilization to carry out targeted maintenance.

Method used

By obtaining the CPU execution time corresponding to the process, and combining it with the number of available cores, frequency duration, and maximum CPU processing capacity, the CPU utilization rate of the process is calculated using the formula (p_jiffies/a_timediff)*(CpuFreqUsed/CpuFreqMax).

Benefits of technology

It enables automatic calculation of CPU utilization for processes, improving the realism and accuracy of CPU utilization, reflecting the actual situation more accurately, reducing resource consumption, and improving computing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114625620B_ABST
    Figure CN114625620B_ABST
Patent Text Reader

Abstract

This disclosure relates to a method, apparatus, electronic device, and storage medium for calculating CPU utilization. The method for calculating CPU utilization includes: obtaining the CPU execution time corresponding to a process; determining the total CPU time corresponding to the process based on the CPU execution time and the number of available cores of the process; determining CPU usage information based on the duration of each available core at each frequency; determining the maximum CPU processing capacity based on the total CPU time and the frequency corresponding to each core in the CPU; and determining the CPU utilization rate corresponding to the process based on the CPU execution time, total CPU time, CPU usage information, and maximum CPU processing capacity. The method provided by this disclosure can automatically calculate the CPU utilization rate corresponding to a process, improving the accuracy and reliability of CPU utilization.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates to the technical field of computer, and particularly relates to a CPU usage rate calculation method and device, electronic equipment and storage medium. BACKGROUND

[0002] In the related art, with the continuous development of computer technology, application services are also growing. At the same time, the resources consumed by the application running on the electronic equipment are also increasing, and the heavy task demand generated by the application running will bring great pressure to the CPU of the electronic equipment. If the CPU of the electronic equipment is in a pressure state for a long time, the electronic equipment will easily generate heat, lag, trigger high-temperature protection measures and the like. In order to enable the developer to carry out application maintenance in a targeted manner to avoid or alleviate the situation that the CPU is in a pressure state, a method for calculating the CPU usage rate is urgently needed. SUMMARY

[0003] The present disclosure provides a CPU usage rate calculation method, device, electronic equipment and storage medium, which can calculate the CPU usage rate. The technical solution of the present disclosure is as follows:

[0004] According to a first aspect of an embodiment of the present disclosure, a CPU usage rate calculation method is provided, comprising:

[0005] obtaining a CPU execution duration corresponding to a process; wherein the CPU execution duration is an interval duration between a current sampling time and a last sampling time;

[0006] determining a total CPU duration corresponding to the process according to the CPU execution duration and a number of available cores of the process; wherein the number of available cores is at least one;

[0007] determining CPU usage information according to a duration of each of the available cores at each frequency point;

[0008] determining a maximum processing capacity of the CPU according to the total CPU duration and a frequency point corresponding to each of the cores of the CPU;

[0009] determining a CPU usage rate corresponding to the process according to the CPU execution duration, the total CPU duration, the CPU usage information and the maximum processing capacity of the CPU.

[0010] In a possible implementation, the determining of the total CPU duration corresponding to the process according to the CPU execution duration and the number of available cores of the process comprises:

[0011] multiply the CPU execution duration and the available core number of the process to obtain a total CPU duration corresponding to the process.

[0012] In a possible implementation, the CPU usage information is determined according to the duration of each available core at each frequency point, and the CPU usage information includes:

[0013] For the ith available core, a first product of the duration of the ith available core at the nth frequency point and the frequency of the nth frequency point is calculated; i∈A, A is a set of available cores of the target process, and n∈N*.

[0014] A second product of each first product and a weight corresponding to the ith available core is calculated.

[0015] A sum of all second products is calculated to obtain the CPU usage information.

[0016] In a possible implementation, the CPU maximum processing capability is determined according to the total CPU duration, a frequency point corresponding to each core of all cores of the CPU, and the CPU maximum processing capability includes:

[0017] A maximum frequency corresponding to each core of all cores of the CPU is determined.

[0018] A product of the total CPU duration, the maximum frequency corresponding to each core of all cores of the CPU, and a weight corresponding to each core of all cores of the CPU is calculated to obtain the CPU maximum processing capability.

[0019] In a possible implementation, the CPU usage rate corresponding to the process is determined according to the CPU execution duration, the total CPU duration, the CPU usage information, and the CPU maximum processing capability, and the CPU usage rate includes:

[0020] A quotient of the CPU execution duration and the total CPU duration is calculated to obtain a CPU execution time occupancy rate.

[0021] A quotient of the CPU usage information and the CPU maximum processing capability is calculated to obtain a CPU core occupancy rate.

[0022] A product of the CPU execution time occupancy rate and the CPU core occupancy rate is calculated to obtain the CPU usage rate corresponding to the process.

[0023] According to a second aspect of the embodiments of the present disclosure, a CPU usage rate calculation apparatus is provided, including:

[0024] An obtaining module configured to obtain a CPU execution duration corresponding to a process; wherein the CPU execution duration is an interval duration between a current sampling time and a last sampling time.

[0025] The first determining module is configured to determine a total CPU duration corresponding to the process according to the CPU execution duration and the number of available cores of the process; and the number of available cores is at least one.

[0026] The second determining module is configured to determine CPU usage information according to the duration of each available core at each frequency point.

[0027] The third determining module is configured to determine a maximum CPU processing capacity according to the total CPU duration, the frequency point corresponding to each core of all cores of the CPU, and the weight corresponding to each core of all cores of the CPU.

[0028] The fourth determining module is configured to determine a CPU usage rate corresponding to the process according to the CPU execution duration, the total CPU duration, the CPU usage information, and the maximum CPU processing capacity.

[0029] In a possible implementation, the first determining module is specifically configured to:

[0030] Calculate the product of the CPU execution duration and the number of available cores of the process to obtain the total CPU duration corresponding to the process.

[0031] In a possible implementation, the second determining module includes:

[0032] The first calculating unit is configured to calculate, for the ith available core, the first product of the duration of the ith available core at the nth frequency point and the frequency of the nth frequency point; i∈A, A is the set of available cores of the target process, and n∈N*.

[0033] The second calculating unit is configured to calculate the second product of each first product and the weight corresponding to the ith available core.

[0034] The third calculating unit is configured to calculate the sum of all second products to obtain the CPU usage information.

[0035] In a possible implementation, the third determining module includes:

[0036] The first determining unit is configured to determine the maximum frequency corresponding to each core of all cores of the CPU.

[0037] The fourth calculating unit is configured to calculate the product of the total CPU duration, the maximum frequency corresponding to each core of all cores of the CPU, and the weight corresponding to each core of all cores of the CPU to obtain the maximum CPU processing capacity.

[0038] In a possible implementation, the fourth determining module comprises:

[0039] The fifth calculating unit is configured to calculate a quotient of the CPU execution duration and the total CPU duration, to obtain a CPU execution time occupancy rate;

[0040] The sixth calculating unit is configured to calculate a quotient of the CPU usage information and the maximum processing capability of the CPU, to obtain a CPU core occupancy rate;

[0041] The seventh calculating unit is configured to calculate a product of the CPU execution time occupancy rate and the CPU core occupancy rate, to obtain the CPU usage rate corresponding to the process.

[0042] According to a third aspect of the embodiments of the present disclosure, an electronic device is provided, comprising:

[0043] a processor;

[0044] a memory for storing instructions executable by the processor;

[0045] The processor is configured to execute the instructions to implement the method for calculating the CPU usage rate according to any one of the first aspect.

[0046] According to a fourth aspect of the embodiments of the present disclosure, a storage medium is provided, when instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method for calculating the CPU usage rate according to any one of the first aspect.

[0047] According to a fifth aspect of the embodiments of the present disclosure, a computer program product is provided, comprising a computer program, when the computer program is executed by a processor, the method for calculating the CPU usage rate according to any one of the first aspect is implemented.

[0048] The technical solutions provided by the embodiments of the present disclosure at least bring the following beneficial effects:

[0049] In the embodiments of the present disclosure, the CPU execution duration corresponding to the process is acquired, the CPU total duration is determined according to the CPU execution duration and the available core quantity of the process, the CPU usage information is determined according to the duration of each available core at each frequency point, the CPU maximum processing capacity is determined according to the CPU total duration and the frequency point corresponding to each core in all cores of the CPU, and finally the CPU usage rate corresponding to the process is determined according to the CPU execution duration, the CPU total duration, the CPU usage information and the CPU maximum processing capacity. In this way, on the one hand, the automatic calculation of the CPU usage rate corresponding to the process can be realized. On the other hand, the CPU core, the available core of the process, the frequency point, the duration and other factors can be combined to normalize the calculation of the CPU usage rate corresponding to the process, so that the calculated CPU usage rate can more truly reflect the actual situation, and the authenticity and accuracy of the CPU usage rate can be improved.

[0050] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present disclosure. BRIEF DESCRIPTION OF DRAWINGS

[0051] The accompanying drawings incorporated in and forming a part of the specification illustrate embodiments consistent with the present disclosure and serve to explain the principles of the present disclosure, and do not constitute an undue limitation on the present disclosure.

[0052] Figure 1 is a flowchart of a CPU usage rate calculation method according to an exemplary embodiment.

[0053] Figure 2 is a flowchart of another CPU usage rate calculation method according to an exemplary embodiment.

[0054] Figure 3 is a block diagram of a CPU usage rate calculation device according to an exemplary embodiment.

[0055] Figure 4 is a block diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION

[0056] In order for those skilled in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be described clearly and completely below with reference to the drawings.

[0057] It should be noted that the terms "first", "second", and the like in the description and claims of the present disclosure and the foregoing drawings are used to distinguish between similar objects and are not necessarily used to describe a particular sequential or chronological order. It should be understood that the data thus used can be interchanged, where appropriate, so that the embodiments of the present disclosure described herein can be carried out in other sequences than those illustrated or described herein. The implementations described in the following example embodiments are not meant to represent all implementations consistent with the present disclosure. Rather, they are merely examples of apparatuses and methods consistent with some aspects of the present disclosure as detailed in the appended claims.

[0058] As can be known from the background, in the related art, there is an urgent need for a method capable of calculating CPU usage. Based on this, the embodiments of the present disclosure provide a CPU usage calculation method, device, electronic device and storage medium, which can not only realize automatic calculation of process corresponding CPU usage, but also can normalize the calculation of process corresponding CPU usage by combining multiple factors such as available cores, frequency points, time length and the like of the process. In this way, the calculated CPU usage can more truly reflect the actual situation, and improve the authenticity and accuracy of the CPU usage.

[0059] The CPU usage calculation method, device, electronic device and storage medium provided by the embodiments of the present disclosure will be described in detail below in combination with the accompanying drawings.

[0060] Figure 1 A flowchart of a CPU usage calculation method according to an example embodiment is shown. The CPU usage calculation method can be applied to an electronic device, for example, a mobile phone, a tablet computer, a computer. As shown in Figure 1 The method can include the following steps.

[0061] In step S101, the CPU execution time corresponding to the process is obtained.

[0062] The CPU execution time is the interval time between the current sampling time and the last sampling time.

[0063] In the embodiments of the present disclosure, when calculating the CPU usage corresponding to the process, the electronic device can first obtain the CPU execution time corresponding to the process. For example, the current time can be obtained from the proc / self / stat file as the current sampling time, the historical sampling time with the shortest interval time from the current time can be obtained as the last sampling time, and the interval time between the current sampling time and the last sampling time can be calculated as the CPU execution time. For example, assuming that the current sampling time is 10:00:00 and the last sampling time is 09:59:59, the CPU execution time can be calculated as 1 second.

[0064] It can be understood that the electronic device can periodically calculate the CPU usage rate, such as setting the period to 1 second. At the same time, in order to facilitate calculation, the time length can also be converted into a standard time for calculation, for example, it can be converted into 10 millisecond level, that is, "* 10ms". Moreover, after obtaining the CPU execution time length corresponding to the process, the current sampling time can also be saved to provide a data basis for calculating the CPU execution time length for the next calculation of the CPU usage rate.

[0065] In step S102, the CPU total time length corresponding to the process is determined according to the CPU execution time length and the number of available cores of the process.

[0066] Among them, the number of available cores is at least one.

[0067] The CPU total time length can be the total time length of all available cores of the process running within the CPU execution time length.

[0068] In the embodiments of the present disclosure, considering that the process is usually limited to run on one or several cores, therefore, after obtaining the CPU execution time length corresponding to the process, the electronic device can obtain the available CPU core of the process, that is, the available core of the process, and count the number of available cores of the process. Then, the CPU total time length corresponding to the process can be determined according to the CPU execution time length and the number of available cores of the process.

[0069] It can be understood that considering that some applications may not have the permission to obtain the current sampling time and the last sampling time through the proc / self / stat file, therefore, the CPU execution time length can also be obtained through SystemClock.elapsedRealtime(), that is, the current sampling time and the last sampling time are obtained through SystemClock.elapsedRealtime(). In this way, the situation of data acquisition failure due to application permission problem can be avoided, so as to improve the success rate of calculating the CPU usage rate and improve the calculation efficiency.

[0070] In step S103, the CPU usage information is determined according to the time length of each available core at each frequency point.

[0071] In the embodiments of the present disclosure, after determining the CPU total duration corresponding to the process according to the CPU execution duration and the available core quantity of the process, the electronic device can obtain the frequency point corresponding to each available core and the duration of each available core running at each frequency point corresponding to the available core (for example, the duration of each core at each frequency point can be obtained by using the CPU information provided by the Linux operating system), that is, the duration of each available core at each frequency point. It can be understood that the duration of each available core at each frequency point can be the duration of each available core running at each frequency point corresponding to the available core in the CPU execution period (that is, the period between the current sampling time and the last sampling time). Then, the CPU usage information corresponding to the process can be determined according to the duration of each available core running at each frequency point corresponding to the available core. The CPU usage information can be used to indicate the specific running condition of each available core at each frequency point corresponding to the available core in the CPU execution period.

[0072] It can be understood that before determining the CPU usage information according to the duration of each available core at each frequency point, it can be judged whether there is a file for obtaining the duration of each core at each frequency point. Only in the case that there is a file for obtaining the duration of each core at each frequency point, the duration of each available core at each frequency point is obtained. Otherwise, the operation of calculating the CPU usage rate can be stopped, and optionally, a prompt information can be output to prompt the developer that the current electronic device does not have a file for obtaining the duration of each core at each frequency point, and the CPU usage rate calculation fails.

[0073] In step S104, the CPU maximum processing capacity is determined according to the CPU total duration and the frequency point corresponding to each core of all cores of the CPU.

[0074] The CPU total duration is determined in combination with the available core of the process, and the CPU maximum processing capacity is determined in combination with all cores of the CPU.

[0075] In the embodiments of the present disclosure, after determining the CPU usage information according to the duration of each available core at each frequency point, the CPU maximum processing capacity in the CPU total duration can be determined according to the CPU total duration and the frequency point corresponding to each core of all cores of the CPU. That is, the CPU maximum processing capacity can be used to indicate the sum of the maximum processing capacities of all cores of the CPU in the CPU total duration.

[0076] In step S105, the CPU usage rate corresponding to the process is determined according to the CPU execution duration, the CPU total duration, the CPU usage information and the CPU maximum processing capacity.

[0077] In embodiments of the present disclosure, after determining the maximum processing capacity of the CPU according to the total CPU time length, the frequency corresponding to each core of all cores of the CPU, the electronic device can determine the CPU usage rate corresponding to the process according to the determined CPU execution time length, the total CPU time length, the CPU usage information, and the maximum processing capacity of the CPU. In this way, the CPU usage rate corresponding to the process can be normalized and calculated in combination with multiple factors such as all cores of the CPU, available cores of the process, frequency, time length, and the like. It can be understood that after determining the CPU usage rate corresponding to the process, the CPU usage rate corresponding to the process can be displayed in the form of a chart or the like, so that the developer can view the CPU usage rate corresponding to the process, and perform data analysis and maintenance based on the CPU usage rate corresponding to the process.

[0078] In further embodiments, the specific implementation of determining the CPU usage rate corresponding to the process according to the CPU execution time length, the total CPU time length, the CPU usage information, and the maximum processing capacity of the CPU in step S104 can include the following steps:

[0079] calculating the quotient of the CPU execution time length and the total CPU time length to obtain a CPU execution time occupancy rate;

[0080] calculating the quotient of the CPU usage information and the maximum processing capacity of the CPU to obtain a CPU core occupancy rate;

[0081] calculating the product of the CPU execution time occupancy rate and the CPU core occupancy rate to obtain the CPU usage rate corresponding to the process.

[0082] In embodiments of the present disclosure, when determining the CPU usage rate corresponding to the process according to the CPU execution time length, the total CPU time length, the CPU usage information, and the maximum processing capacity of the CPU, the electronic device can first calculate the quotient of the CPU execution time length and the total CPU time length, i.e., calculate the proportion of the CPU execution time length in the total CPU time length, to obtain a CPU execution time occupancy rate. Then, the quotient of the CPU usage information and the maximum processing capacity of the CPU can be calculated, i.e., the proportion of the CPU usage information in the maximum processing capacity of the CPU is calculated, to obtain a CPU core occupancy rate. After calculating the CPU execution time occupancy rate and the CPU core occupancy rate, the product of the CPU execution time occupancy rate and the CPU core occupancy rate can be calculated, and the product is taken as the CPU usage rate corresponding to the process. Specifically, the calculation formula of the CPU usage corresponding to the process can be as shown in formula (1).

[0083] (p_jiffies / a_timediff)*(CpuFreqUsed / CpuFreqMax) (1)

[0084] Wherein, p_jiffies represents CPU execution duration, a_timediff represents CPU total duration, CpuFreqUsed represents CPU usage information, and CpuFreqMax represents CPU maximum processing capability.

[0085] In the embodiments of the present disclosure, by acquiring the CPU execution duration corresponding to the process, the CPU total duration is determined according to the CPU execution duration and the available core number of the process, the CPU usage information is determined according to the duration of each available core at each frequency point, the CPU maximum processing capability is determined according to the CPU total duration and the frequency point corresponding to each core in all cores of the CPU, and finally the CPU usage rate corresponding to the process is determined according to the CPU execution duration, the CPU total duration, the CPU usage information and the CPU maximum processing capability. In this way, on the one hand, the automatic calculation of the CPU usage rate corresponding to the process can be realized. On the other hand, the CPU usage rate corresponding to the process can be normalized by combining multiple factors such as CPU core, available core of the process, frequency point and duration, so that the calculated CPU usage rate can more truly reflect the actual situation, and the authenticity and accuracy of the CPU usage rate can be improved.

[0086] In further embodiments, after determining the CPU usage rate corresponding to the process according to the CPU execution duration, the CPU total duration, the CPU usage information and the CPU maximum processing capability in step S104, the following processing can also be performed:

[0087] Acquire the first sampling time when the CPU execution duration corresponding to the process is acquired for the first time after the electronic device is started this time;

[0088] Determine whether the interval duration between the first sampling time and the current sampling time is greater than or equal to a preset duration threshold;

[0089] In the case where the interval duration between the first sampling time and the current sampling time is greater than or equal to the preset duration threshold, stop acquiring the CPU execution duration corresponding to the process;

[0090] In the case where the interval duration between the first sampling time and the current sampling time is less than the preset duration threshold, acquire the CPU execution duration corresponding to the process.

[0091] In embodiments of the present disclosure, considering that a process usually stops running after running for a period of time, it is not meaningful to calculate the CPU usage at this time. Therefore, after determining the CPU usage corresponding to the process according to the CPU execution duration, the total CPU duration, the CPU usage information, and the maximum processing capacity of the CPU, the electronic device can obtain a sampling time at which the CPU execution duration corresponding to the process is obtained for the first time after the electronic device is started this time, that is, a first sampling time. Then, the interval duration between the first sampling time and the current sampling time can be calculated, and the interval duration is compared with the preset interval duration to determine whether the interval duration is greater than or equal to the preset duration threshold. The preset duration threshold can be a specific duration set in advance, for example, 30 minutes, 60 minutes, etc.

[0092] In the case where the interval duration between the first sampling time and the current sampling time is greater than or equal to the preset duration threshold, the electronic device can stop obtaining the CPU execution duration corresponding to the process. Conversely, in the case where the interval duration between the first sampling time and the current sampling time is less than the preset duration threshold, the electronic device can continue to obtain the CPU execution duration corresponding to the process, determine the total CPU duration corresponding to the process according to the CPU execution duration and the number of available cores of the process, determine the CPU usage information according to the duration of each available core at each frequency point, determine the maximum processing capacity of the CPU according to the total CPU duration, the frequency point corresponding to each core in all cores of the CPU, and determine the CPU usage corresponding to the process according to the CPU execution duration, the total CPU duration, the CPU usage information, and the maximum processing capacity of the CPU. That is, in the case where the interval duration between the first sampling time and the current sampling time is greater than or equal to the preset duration threshold, the electronic device can stop performing the process of calculating the CPU usage of steps S101-S104; conversely, in the case where the interval duration between the first sampling time and the current sampling time is less than the preset duration threshold, the electronic device can continue to perform the process of calculating the CPU usage of steps S101-S104.

[0093] It can be understood that in the case where the calculation of the CPU usage is periodically performed, in the case where the interval duration between the first sampling time and the current sampling time is less than the preset duration threshold, the electronic device can continue to perform the process of calculating the CPU usage of steps S101-S104 in the next period.

[0094] In this way, only in the case where the interval duration between the first sampling time and the current sampling time is less than the preset duration threshold, the calculation process of the CPU usage is continued, which can avoid meaningless calculation process of the CPU usage. In this way, resource consumption can be effectively reduced, and the calculation efficiency and effectiveness of the CPU usage can be improved.

[0095] In a possible implementation, a specific implementation of determining the CPU total duration corresponding to the target process based on the CPU execution duration and the available core number of the target process in the step S102 can be as follows:

[0096] The product of the CPU execution duration and the available core number of the process is calculated to obtain the CPU total duration corresponding to the process.

[0097] In the embodiment of the present disclosure, when determining the CPU total duration corresponding to the target process based on the CPU execution duration and the available core number of the target process, the electronic device can multiply the CPU execution duration and the available core number of the process to obtain the product of the CPU execution duration and the available core number of the process, and determine the product as the CPU total duration corresponding to the process. That is, the CPU total duration can be the total runtime of all available cores of the process within the CPU execution duration. Specifically, a specific formula for calculating the CPU total duration corresponding to the process can be as shown in formula (2).

[0098] a_timediff = (time1-time2) *m (2)

[0099] Wherein, a_timediff represents the CPU total duration, time1 represents the current sampling time, time2 represents the last sampling time, and m represents the available core number of the process.

[0100] In a possible implementation, a specific implementation of determining the CPU usage information according to the duration of each available core at each frequency point in the step S103 can be as follows:

[0101] For the ith available core, the first product of the duration of the ith available core at the nth frequency point and the frequency of the nth frequency point is calculated; i∈A, A is the available core set of the target process, and n∈N*;

[0102] The second product of each first product and the weight corresponding to the ith available core is calculated;

[0103] The sum of all second products is calculated to obtain the CPU usage information.

[0104] In an embodiment of the present disclosure, when determining the CPU usage information according to the time length of each available core at each frequency point, for the ith available core, a product of the time length of the ith available core at the nth frequency point and the nth frequency point can be calculated first, that is, a product of the time length of the ith available core at the nth frequency point and the specific frequency of the nth frequency point is calculated, and the product is a first product. Then, the weight corresponding to the ith available core can be obtained, and a product of each first product and the weight corresponding to the ith available core, that is, a second product, is calculated. After the second product of each first product and the weight corresponding to the ith available core is calculated, all the second products are added up, and a sum of all the second products is calculated, and the sum is taken as the CPU usage information. Specifically, the specific formula for determining the CPU usage information according to the time length of each available core at each frequency point can be shown in formula (3).

[0105]

[0106] wherein CpuFreqUsed represents the CPU usage information, i represents the number of cores, A represents the available core set of the process, such as that an electronic device has 8 cores 0, 1, 2, 3, 4, 5, 6, 7, and the process is limited to run on cores 0-4, then A = {0, 1, 2, 3, 4, 5, 6, 7} - {5, 6, 7} = {0, 1, 2, 3, 4}, n represents the frequency of the nth frequency point, timediff(n) represents the time length of running at the nth frequency point, and weight(i) represents the weight corresponding to the ith core.

[0107] In this way, the CPU usage information can be calculated based on the combination of the core, the specific frequency of the frequency point, the time length, and the weight of the core. In this way, the accuracy and authenticity of the CPU usage information can be improved, and thus the authenticity and accuracy of the calculated CPU usage rate can be further improved.

[0108] In a possible implementation, the specific implementation of determining the maximum processing capacity of the CPU according to the total CPU time length and the frequency point corresponding to each core of the CPU in step S103 can be as follows:

[0109] determining the maximum frequency corresponding to each core of the CPU;

[0110] calculating the product of the total CPU time length, the maximum frequency corresponding to each core of the CPU, and the weight corresponding to each core of the CPU, to obtain the maximum processing capacity of the CPU.

[0111] In an embodiment of the present disclosure, when determining the CPU maximum processing capability according to the CPU total time length, the frequency corresponding to each core of all the cores of the CPU, the electronic device can first acquire all the frequencies corresponding to each core of all the cores of the CPU, determine the maximum frequency corresponding to each core of all the cores of the CPU from all the frequencies corresponding to each core of all the cores of the CPU, and acquire the weight corresponding to each core of all the cores of the CPU. Then, the CPU total time length, the maximum frequency corresponding to each core of all the cores of the CPU, and the weight corresponding to each core of all the cores of the CPU can be multiplied to obtain the product of the CPU total time length, the maximum frequency corresponding to each core of all the cores of the CPU, and the weight corresponding to each core of all the cores of the CPU, and the product is determined as the CPU maximum processing capability. Specifically, the specific calculation formula of the CPU maximum processing capability can be as shown in formula (4).

[0112]

[0113] wherein CpuFreqMax represents the CPU maximum processing capability, maxfreq(i) represents the maximum frequency corresponding to the ith CPU core of all the cores of the CPU, C represents the set of all the cores of the CPU of the CPU, a_timediff represents the CPU total time length, and weight(i) represents the weight corresponding to the ith core of all the cores of the CPU.

[0114] In this way, the CPU maximum processing capability can be calculated in combination with the weight corresponding to each core of all the cores of the CPU. In this way, the accuracy and authenticity of the calculated CPU maximum processing capability can be improved, so that the authenticity and accuracy of the CPU usage rate can be further improved, and the calculated CPU usage rate of the process can more accurately and authentically reflect the actual use of the CPU.

[0115] To make the CPU usage rate calculation method provided by the embodiments of the present disclosure clearer, the following describes the CPU usage rate calculation method provided by the embodiments of the present disclosure with reference to the accompanying drawings. Figure 2 The CPU usage rate calculation method provided by the embodiments of the present disclosure is described as follows. Figure 2 As shown in the formula (1), the CPU usage rate calculation method can include the following steps:

[0116] In step S201, the CPU execution time length corresponding to the process is acquired.

[0117] In step S202, the product of the CPU execution time length and the number of available cores of the process is calculated to obtain the CPU total time length corresponding to the process.

[0118] In step S203, for the ith available core, the first product of the time length of the ith available core at the nth frequency point and the frequency of the nth frequency point is calculated.

[0119] In step S204, a second product of each first product and the weight corresponding to the i-th available core is calculated.

[0120] In step S205, a sum of all second products is calculated to obtain the CPU usage information.

[0121] In step S206, the maximum frequency corresponding to each core of all cores of the CPU is determined.

[0122] In step S207, a product of the total duration of the CPU, the maximum frequency corresponding to each core of all cores of the CPU, and the weight corresponding to each core of all cores of the CPU is calculated to obtain the maximum processing capacity of the CPU.

[0123] In step S208, a quotient of the CPU execution duration and the total duration of the CPU is calculated to obtain the CPU execution time occupancy rate.

[0124] In step S209, a quotient of the CPU usage information and the maximum processing capacity of the CPU is calculated to obtain the CPU core occupancy rate.

[0125] In step S2010, a product of the CPU execution time occupancy rate and the CPU core occupancy rate is calculated to obtain the CPU usage rate corresponding to the process.

[0126] In step S2011, a first sampling time point at which the CPU execution duration corresponding to the process is acquired for the first time after the electronic device is started this time is obtained.

[0127] In step S2012, it is determined whether the interval duration between the first sampling time point and the current sampling time point is greater than or equal to a preset duration threshold.

[0128] When the interval duration between the first sampling time point and the current sampling time point is greater than or equal to the preset duration threshold, step S2013 is executed, otherwise, step S2014 is executed.

[0129] In step S2013, the acquisition of the CPU execution duration corresponding to the process is stopped.

[0130] In step S2014, the CPU execution duration corresponding to the process is acquired.

[0131] The specific implementation manners and technical effects of the steps in this embodiment are similar to those of the above-mentioned embodiments, and for the sake of brevity, will not be described here.

[0132] Figure 3 is a block diagram of a CPU usage rate calculation device according to an exemplary embodiment. Referring to Figure 3 , the CPU usage rate calculation device 300 can include:

[0133] The acquisition module 310 can be configured to acquire a CPU execution duration corresponding to a process; wherein the CPU execution duration can be an interval duration between a current sampling time and a last sampling time;

[0134] The first determination module 320 can be configured to determine a CPU total duration corresponding to the process according to the CPU execution duration and an available core number of the process; wherein the available core number can be at least one;

[0135] The second determination module 330 can be configured to determine CPU usage information according to a duration of each of the available cores at each frequency point;

[0136] The third determination module 340 can be configured to determine a CPU maximum processing capacity according to the CPU total duration, a frequency point corresponding to each of all cores of the CPU;

[0137] The fourth determination module 350 can be configured to determine a CPU usage rate corresponding to the process according to the CPU execution duration, the CPU total duration, the CPU usage information and the CPU maximum processing capacity.

[0138] In a possible implementation, the first determination module 320 can be specifically configured to:

[0139] calculate a product of the CPU execution duration and the available core number of the process to obtain the CPU total duration corresponding to the process.

[0140] In a possible implementation, the second determination module 330 can include:

[0141] The first calculation unit can be configured to calculate, for an i-th available core, a first product of a duration of the i-th available core at an n-th frequency point and a frequency of the n-th frequency point; i∈A, A is an available core set of the target process, and n∈N*;

[0142] The second calculation unit can be configured to calculate a second product of each of the first products and a weight corresponding to the i-th available core;

[0143] The third calculation unit can be configured to calculate a sum of all second products to obtain the CPU usage information.

[0144] In a possible implementation, the third determination module 340 can include:

[0145] The first determination unit can be configured to determine a maximum frequency corresponding to each of all cores of the CPU;

[0146] The fourth calculating unit can be configured to calculate a product of the total CPU time length, a maximum frequency corresponding to each of all the cores of the CPU, and a weight corresponding to each of all the cores of the CPU, to obtain the maximum processing capacity of the CPU.

[0147] In a possible implementation, the fourth determining module 350 can include:

[0148] The fifth calculating unit can be configured to calculate a quotient of the CPU execution time length and the total CPU time length, to obtain a CPU execution time occupancy rate.

[0149] The sixth calculating unit can be configured to calculate a quotient of the CPU usage information and the maximum processing capacity of the CPU, to obtain a CPU core occupancy rate.

[0150] The seventh calculating unit can be configured to calculate a product of the CPU execution time occupancy rate and the CPU core occupancy rate, to obtain the CPU usage rate corresponding to the process.

[0151] As to the apparatus in the above-described embodiments, the specific manners in which various modules perform operations have been described in detail in the embodiments of the method, and thus will not be described in detail here.

[0152] According to embodiments of the present disclosure, the present disclosure further provides an electronic device, a readable storage medium, and a computer program product.

[0153] Figure 4 A schematic block diagram of an example electronic device 400 that can be used to implement embodiments of the present disclosure is shown. The electronic device 400 is intended to represent various forms of digital computers, such as laptops, desktops, tablets, personal digital assistants, servers, blade servers, mainframes, and other appropriate computers. The electronic device can also represent various forms of mobile devices, such as personal digital assistants, cellular telephones, smartphones, wearable devices, and other similar computing devices. The components shown here, their connections and relationships, and their functions, are meant to be examples only, and are not meant to limit implementations of the present disclosure described and / or claimed in this document.

[0154] As Figure 4As shown, the electronic device 400 includes a computing unit 401 that can perform various appropriate actions and processes in accordance with a computer program stored in a read-only memory (ROM) 402 or a computer program loaded into a random access memory (RAM) 403 from a storage unit 408. In the RAM 403, various programs and data required for the operation of the device 400 can also be stored. The computing unit 401, the ROM 402, and the RAM 403 are connected to each other through a bus 404. An input / output (I / O) interface 405 is also connected to the bus 404.

[0155] A plurality of components in the electronic device 400 are connected to the I / O interface 405, including: an input unit 406, such as a keyboard, a mouse, and the like; an output unit 407, such as various types of displays, a speaker, and the like; a storage unit 408, such as a magnetic disk, an optical disk, and the like; and a communication unit 409, such as a network card, a modem, a wireless communication transceiver, and the like. The communication unit 409 allows the electronic device 400 to exchange information / data with other devices through a computer network, such as the Internet, and / or various telecommunication networks.

[0156] The computing unit 401 can be various general-purpose and / or special-purpose processing components having processing and computing capabilities. Some examples of the computing unit 401 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various specialized artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, and the like. The computing unit 401 performs the various methods and processes described above, such as the CPU usage rate calculation method. For example, in some embodiments, the CPU usage rate calculation method can be implemented as a computer software program that is tangibly embodied in a machine-readable medium, such as the storage unit 408. In some embodiments, part or all of the computer program can be loaded and / or installed on the electronic device 400 via the ROM 402 and / or the communication unit 409. When the computer program is loaded into the RAM 403 and executed by the computing unit 401, one or more steps of the CPU usage rate calculation method described above can be performed. Alternatively, in other embodiments, the computing unit 401 can be configured to perform the CPU usage rate calculation method by any other appropriate means, such as by means of firmware.

[0157] The various embodiments of the systems and techniques described above can be implemented in digital electronic circuitry, integrated circuitry, a field programmable gate array (FPGA), an application specific integrated circuit (ASIC), a system on a chip (SOC), a load programmable logic device (CPLD), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include implementation in one or more computer programs that are executable and / or interpretable on a programmable system including at least one programmable processor, which can be special or general purpose, coupled to receive data and instructions from, and to transmit data and instructions to, a storage system, at least one input device, and at least one output device.

[0158] Program code for carrying out methods of the present disclosure can be written in any combination of one or more programming languages. The program code can be provided to a processor or controller of a general purpose computer, special purpose computer, or other programmable data processing apparatus to produce a machine, such that the program code, when executed by the processor or controller, produces the functions / operations specified in the flowcharts and / or the block diagrams. The program code can be executed entirely on a machine, partially on a machine, partially on a machine as a stand-alone software package, or entirely on a remote machine or server.

[0159] In the context of the present disclosure, a machine-readable medium can be a tangible medium that contains or stores a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include but is not limited to an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of the machine-readable storage medium will include one or more lines of electrical conductors, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0160] To provide for interaction with a user, the systems and techniques described here can be implemented on a computer having a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user and a keyboard and a pointing device (e.g., a mouse or a trackball) by which the user can provide input to the computer. Other kinds of devices can be used to provide for interaction with a user as well; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form, including acoustic, speech, or tactile input.

[0161] The systems and techniques described here can be implemented in a computing system that includes a back end component (e.g., as a data server), or that includes a middleware component (e.g., an application server), or that includes a front end component (e.g., a user computer having a graphical user interface or a Web browser through which a user can interact with an implementation of the systems and techniques described here), or any combination of such back end, middleware, or front end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network (LAN), a wide area network (WAN), the Internet, and a blockchain network.

[0162] The computer system can include clients and servers. A client and server are generally remote from each other and typically interact through a communication network. The relationship of client and server arises by virtue of computer programs running on the respective computers and having a client-server relationship to each other. A server can be a cloud server, also known as cloud computing server or cloud host, which is a host product in the cloud computing service system, to solve the defects of large management difficulty and weak business scalability in traditional physical host and VPS service ("Virtual Private Server", or simply "VPS"). The server can also be a server of a distributed system, or a server combined with a blockchain.

[0163] It should be understood that various forms of flow shown above can be used, with steps reordered, added, or removed. For example, the steps recited in the present disclosure can be performed in parallel, in series, or in a different order, without limitation herein, as long as the desired results of the technical solutions of the present disclosure can be achieved.

[0164] The above detailed description does not limit the scope of the disclosure. Various modifications, combinations, sub-combinations and alternatives can be made to the detailed description. Any modification, equivalent replacement and improvement etc. made within the spirit and principle of the disclosure shall be included in the scope of the disclosure.

Claims

1. A method of calculating CPU usage, characterized by, The method comprises: obtaining a CPU execution duration corresponding to a process; wherein the CPU execution duration is an interval duration between a current sampling time and a last sampling time; calculating a product of the CPU execution duration and an available core number of the process to obtain a CPU total duration corresponding to the process; wherein the available core is a CPU core subset to which the process is limited to use, and the available core number is at least one; For the i The available cores calculate the first... i The available core is in the [number]th [position]. n The duration of the frequency point and the first n The first product of the frequencies of each frequency point; i ∈A, where A is the available core set of the process. n ∈ N* , N* It is the set of positive integers; computing a second product of each of the first products with a weight corresponding to the available core; i computing a second product of each of the first products with a weight corresponding to the available core; calculating a sum of all second products to obtain the CPU usage information; determining a CPU maximum processing capacity according to the CPU total duration and a frequency point corresponding to each core of all CPU cores, wherein the CPU maximum processing capacity is a sum of maximum processing capacities of all cores within the CPU total duration; calculating a quotient of the CPU execution duration and the CPU total duration to obtain a CPU execution time occupancy rate; calculating a quotient of the CPU usage information and the CPU maximum processing capacity to obtain a CPU core occupancy rate; calculating a product of the CPU execution time occupancy rate and the CPU core occupancy rate to obtain a CPU usage rate corresponding to the process.

2. The CPU usage rate calculation method according to claim 1, characterized by, The determination of the CPU maximum processing capacity according to the CPU total duration and the frequency point corresponding to each core of all CPU cores comprises: determining a maximum frequency corresponding to each core of all CPU cores; calculating a product of the CPU total duration, the maximum frequency corresponding to each core of all CPU cores, and a weight corresponding to each core of all CPU cores to obtain the CPU maximum processing capacity.

3. A CPU usage rate calculating device characterized by comprising: The method comprises: an obtaining module configured to obtain a CPU execution duration corresponding to a process; wherein the CPU execution duration is an interval duration between a current sampling time and a last sampling time; a first determining module configured to calculate a product of the CPU execution duration and an available core number of the process to obtain a CPU total duration corresponding to the process; wherein the available core is a CPU core subset to which the process is limited to use, and the available core number is at least one; a second determining module configured to determine CPU usage information according to a time duration of each available core at each frequency point; a third determining module configured to determine a CPU maximum processing capacity according to the CPU total duration and a frequency point corresponding to each core of all CPU cores, wherein the CPU maximum processing capacity is a sum of maximum processing capacities of all cores within the CPU total duration; a fourth determining module configured to determine a CPU usage rate corresponding to the process according to the CPU execution duration, the CPU total duration, the CPU usage information, and the CPU maximum processing capacity; the second determining module comprises: The first computing unit is configured to perform calculations for the first... i The available cores calculate the first... i The available core is in the [number]th [position]. n The duration of the frequency point and the first n The first product of the frequencies of each frequency point; i ∈A, where A is the available core set of the process. n ∈ N* , N* It is the set of positive integers; The second calculation unit is configured to calculate each of the first product and the first... i The second product of the weights corresponding to each available core; a third calculating unit configured to calculate a sum of all second products to obtain the CPU usage information; the fourth determining module comprises: a fifth calculating unit configured to calculate a quotient of the CPU execution duration and the CPU total duration to obtain a CPU execution time occupancy rate; A sixth calculation unit configured to calculate a quotient of the CPU usage information and the CPU maximum processing capability to obtain a CPU core occupancy rate; A seventh calculation unit configured to calculate a product of the CPU execution time occupancy rate and the CPU core occupancy rate to obtain the CPU usage rate corresponding to the process.

4. The CPU usage rate calculating device according to claim 3, wherein The third determination module comprises: A first determination unit configured to determine a maximum frequency corresponding to each of the cores in the CPU all cores; A fourth calculation unit configured to calculate a product of the CPU total time length, the maximum frequency corresponding to each of the cores in the CPU all cores, and the weight corresponding to each of the cores in the CPU all cores to obtain the CPU maximum processing capability.

5. An electronic device, comprising: Comprise: A processor; A memory for storing instructions executable by the processor; Wherein the processor is configured to execute the instructions to implement the CPU usage rate calculation method according to any one of claims 1 to 2. 6.A storage medium, when instructions in the storage medium are executed by a processor of an electronic device, enabling the electronic device to perform the CPU usage rate calculation method according to any one of claims 1 to 2.

Citation Information

Patent Citations

  • Method and device for determining performance of multi-core processor and storage medium

    CN110347563A