CN102110043A - Method and device for computing CPU occupancy rate - Google Patents
Method and device for computing CPU occupancy rate Download PDFInfo
- Publication number
- CN102110043A CN102110043A CN2010106141081A CN201010614108A CN102110043A CN 102110043 A CN102110043 A CN 102110043A CN 2010106141081 A CN2010106141081 A CN 2010106141081A CN 201010614108 A CN201010614108 A CN 201010614108A CN 102110043 A CN102110043 A CN 102110043A
- Authority
- CN
- China
- Prior art keywords
- task
- cpu
- time
- takies
- sampling period
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Abstract
The invention discloses a method for computing central processing unit (CPU) occupancy rate, which comprises the following steps of: selecting a task; specifying a sampling period Tc; acquiring the time length Tt of accumulatively occupying the CPU in the sampling period by the task; and when the sampling period is ended, computing the CPU occupancy rate of the task, namely dividing the time length Tt of accumulatively occupying the CPU in one period by the total time Tc of the period. The statistics is implemented for each task, so the CPU occupancy rate of each task can be accurately obtained; and the method is not affected by hardware, and is operated independently.
Description
Technical field
The present invention relates to communication and field of computer technology, relate in particular to a kind of computing method and device of CPU usage.
Background technology
CPU usage is an important indicator must paying close attention in the computer hardware.Mainly contain two kinds of algorithms at present and can realize obtaining of CPU usage, be respectively the method for CPU burning and in Tick interrupts, the task among the RTOS carried out the method for sampling.
The method of CPU burning at first will be set up the CPU criterion of full load in the Measuring Time, and this criterion generally is a count value.Common way is in the Measuring Time of regulation, forbids the switching of external interrupt and task, allows CPU carry out a kind of computing consuming time, and this count value is added up, and this count value is exactly the CPU criterion of full load in the Measuring Time at last.The title of CPU burning derives from this computing consuming time, and floating-point operation is generally chosen in computing consuming time, but under the conditional situation of image size, can choose integer arithmetic.After standard is set up and to be finished, setting up the task of a cpu busy percentage again, is minimum just passable in the system as long as guarantee its priority.The groundwork of this task is in the Measuring Time of regulation, carries out computing consuming time and count value and adds up, and begin again after each Measuring Time end.Owing to have other task in the system, so in certain Measuring Time, this count value can not surpass fully loaded CPU criterion, so in certain Measuring Time: CPU idleness=count value/fully loaded CPU criterion; Cpu load=100%-CPU idleness.
In Tick interrupts the task among the RTOS being carried out the method for sampling is after system enters operation, current carrying out of the task of sampling when each Tick interruption takes place, if CPU is in the HALT attitude, haltTimes then adds up; Periodically calculate CPU usage then, calculate once general p.s., and remove the occurrence frequency that haltTimes:(tickIntFrequance represents that Tick interrupts); RealTime CPU Load=haltTimes/tickIntFrequance.Certain task number of times/tickIntFrequance*100% that this task in CPU usage=one-period is sampled then.Wherein RTOS is a real time operating system.
The shortcoming of above-mentioned two kinds of methods is, but CPU usage burning algorithm realizes simple can not obtain the CPU usage of each task, but in Tick interrupts to RTOS in the task sampling algorithm can to obtain the CPU usage error of each task substantially too big.
Summary of the invention
Technical matters to be solved by this invention is, a kind of computing method and device of CPU usage is provided, and can accurately obtain the CPU usage of each task.
In order to address the above problem, the invention provides a kind of computing method of CPU usage, comprising: choose a task; Specify a sampling period Tc; Obtain the time span Tt that described task accumulative total in the described sampling period takies CPU; Sampling period, the CPU usage of calculating described task was that described task accumulative total takies the T.T. Tc of the time span Tt of CPU divided by the described cycle in the one-period when finishing.
As optional technical scheme, the step of described acquisition time span Tt further comprises: task is switched fashionable, obtains current cut-in timing Tb; Task is switched when, and obtains the current time T e that cuts out; This task of calculating takies the time span Ta=Te-Tb of CPU; The time span Ta that each described task in the described sampling period is taken CPU adds up, and obtains this task accumulative total and takies CPU time Tt.
As optional technical scheme, the described step of obtaining cut-in timing Tb and cutting out time T e all adopts the microsecond timer to obtain the time.
The present invention further provides a kind of calculation element of CPU usage, having comprised: task is selected the unit, is used to choose a task; Cycle is formulated the unit, is used to specify a sampling period Tc; The time statistic unit is used to obtain the time span Tt that described task accumulative total in the described sampling period takies CPU; Occupancy computing unit, the CPU usage that is used to calculate described task are that described task accumulative total takies the T.T. Tc of the time span Tt of CPU divided by the described cycle in the one-period.
As optional technical scheme, described time statistic unit further comprises: the cut-in timing acquisition module, be used for switching fashionable in task, and obtain current cut-in timing Tb; Cut out time-obtaining module, be used for switching when, obtain the current time T e that cuts out in task; CPU holding time computing module is used to calculate the time span Ta=Te-Tb that this task takies CPU; The time accumulator module, the time span Ta that each described task in the described sampling period is taken CPU adds up, and obtains this task accumulative total and takies CPU time Tt.
As optional technical scheme, described cut-in timing acquisition module and cut out time-obtaining module all calls the microsecond timer and obtains the time.
The invention has the advantages that, implement statistics, therefore can accurately obtain the CPU usage of each task, and said method is not subjected to the influence of hardware, work alone at each task.
Description of drawings
It shown in the accompanying drawing 1 the implementation step synoptic diagram of the embodiment of the method for the invention.
It shown in the accompanying drawing 2 the system architecture synoptic diagram of the embodiment of device of the present invention.
Embodiment
Next in conjunction with the accompanying drawings the computing method of a kind of CPU usage of the present invention and the embodiment of device are made a detailed description.
Provide the embodiment of the method for the invention at first in conjunction with the accompanying drawings.Be the implementation step synoptic diagram of present embodiment shown in the accompanying drawing 1, comprise: step S100, choose a task; Step S110 specifies a sampling period Tc; Step S121, task is switched fashionable, obtains current cut-in timing Tb; Step S122, task is switched when, and obtains the current time T e that cuts out; Step S123 calculates the time span Ta=Te-Tb that this task takies CPU; Step S124, the time span Ta that each described task in the described sampling period is taken CPU adds up, and obtains this task accumulative total and takies CPU time Tt; Step S130, the sampling period, the CPU usage of calculating described task was that described task accumulative total takies the T.T. Tc of the time span Tt of CPU divided by the described cycle in the one-period when finishing.
Step S100 chooses a task.This task can be that any one takies the task that CPU handles in the computing machine.
Step S110 specifies a sampling period Tc.Tc can be the arbitrary value of time, for example 1 second, 10 seconds or one minute etc.
Step S121, task is switched fashionable, obtains current cut-in timing Tb.So-called task switch into, the task of being meant switches to the time point that CPU carries out data processing, the cut-in timing Tb of this method record this moment, promptly CPU begins to handle the time of this task.
Step S122, task is switched when, and obtains the current time T e that cuts out.So-called task is switched, and is meant the time point that CPU disposes task, this method record this moment cut out time T b, i.e. CPU time that this task is disposed.
Step S123 calculates the time span Ta=Te-Tb that this task takies CPU.Obviously, the difference of above-mentioned two timing nodes is this task and takies the time that CPU carries out data processing.
Step S124, the time span Ta that each described task in the described sampling period is taken CPU adds up, and obtains this task accumulative total and takies CPU time Tt.This step can adopt definition one numerical value Tt in computing machine, after each task disposes, all the time span Ta that handles is added to numerical value Tt, promptly carries out computing Tt=Tt+Ta.Obviously, when beginning to add up, the initial value of Tt is 0.
Step S130, the sampling period, the CPU usage of calculating described task was that described task accumulative total takies the T.T. Tc of the time span Tt of CPU divided by the described cycle in the one-period when finishing.The CPU usage of task promptly can obtain this result with Tt divided by Tc so the name think of should be this task takies CPU in the unit interval a amount only.
In order to accurately calculate each task executions time, can to adopt microsecond timer acquisition cut-in timing Tb and cut out time T e, thereby accurately calculate the execution time separately of each task.The microsecond timer is meant the timer of timing precision less than 1 microsecond.
If want to add up the CPU usage of a plurality of tasks, also only need implement said method respectively to each task, and the occupancy result who is obtained added and, promptly can obtain total CPU usage of a plurality of tasks.
The advantage of the described technical scheme of above embodiment is, implements statistics at each task, therefore can accurately obtain the CPU usage of each task, and said method is not subjected to the influence of hardware, works alone.
Next provide the embodiment of device of the present invention in conjunction with the accompanying drawings.Be the configuration diagram of this device shown in the accompanying drawing 2, comprise:
Task is selected unit 200, is used to choose a task;
Cycle is formulated unit 210, is used to specify a sampling period Tc;
Wherein time statistic unit 220 further comprises: cut-in timing acquisition module 221, be used for switching fashionable in task, and obtain current cut-in timing Tb; Cut out time-obtaining module 222, be used for switching when, obtain the current time T e that cuts out in task; CPU holding time computing module 223 is used to calculate the time span Ta=Te-Tb that this task takies CPU; Time accumulator module 224, the time span Ta that each described task in the described sampling period is taken CPU adds up, and obtains this task accumulative total and takies CPU time Tt.
Cut-in timing acquisition module 221 and cut out time-obtaining module 222 and all call the microsecond timer and obtain the time.
More than the mutual relationship and the principle of work of each assembly please refer to last embodiment about method, be omitted herein.
The above only is a preferred implementation of the present invention; should be pointed out that for those skilled in the art, under the prerequisite that does not break away from the principle of the invention; can also make some improvements and modifications, these improvements and modifications also should be considered as protection scope of the present invention.
Claims (6)
1. the computing method of a CPU usage is characterized in that, comprising:
Choose a task;
Specify a sampling period Tc;
Obtain the time span Tt that described task accumulative total in the described sampling period takies CPU;
Sampling period, the CPU usage of calculating described task was that described task accumulative total takies the T.T. Tc of the time span Tt of CPU divided by the described cycle in the one-period when finishing.
2. the computing method of CPU usage according to claim 1 is characterized in that, the step of described acquisition time span Tt further comprises:
Task is switched fashionable, obtains current cut-in timing Tb;
Task is switched when, and obtains the current time T e that cuts out;
This task of calculating takies the time span Ta=Te-Tb of CPU;
The time span Ta that each described task in the described sampling period is taken CPU adds up, and obtains this task accumulative total and takies CPU time Tt.
3. the computing method of CPU usage according to claim 2 is characterized in that, the described step of obtaining cut-in timing Tb and cutting out time T e all adopts the microsecond timer to obtain the time.
4. the calculation element of a CPU usage is characterized in that, comprising:
Task is selected the unit, is used to choose a task;
Cycle is formulated the unit, is used to specify a sampling period Tc;
The time statistic unit is used to obtain the time span Tt that described task accumulative total in the described sampling period takies CPU;
Occupancy computing unit, the CPU usage that is used to calculate described task are that described task accumulative total takies the T.T. Tc of the time span Tt of CPU divided by the described cycle in the one-period.
5. the calculation element of CPU usage according to claim 4 is characterized in that, described time statistic unit further comprises:
The cut-in timing acquisition module is used for switching in task fashionable, obtains current cut-in timing Tb;
Cut out time-obtaining module, be used for switching when, obtain the current time T e that cuts out in task;
CPU holding time computing module is used to calculate the time span Ta=Te-Tb that this task takies CPU;
The time accumulator module is used for the time span Ta that each described task in the described sampling period takies CPU is added up, and obtains this task accumulative total and takies CPU time Tt.
6. the calculation element of CPU usage according to claim 5 is characterized in that, described cut-in timing acquisition module and cut out time-obtaining module all calls the microsecond timer and obtains the time.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN2010106141081A CN102110043A (en) | 2010-12-30 | 2010-12-30 | Method and device for computing CPU occupancy rate |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN2010106141081A CN102110043A (en) | 2010-12-30 | 2010-12-30 | Method and device for computing CPU occupancy rate |
Publications (1)
Publication Number | Publication Date |
---|---|
CN102110043A true CN102110043A (en) | 2011-06-29 |
Family
ID=44174210
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN2010106141081A Pending CN102110043A (en) | 2010-12-30 | 2010-12-30 | Method and device for computing CPU occupancy rate |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN102110043A (en) |
Cited By (16)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN103164321A (en) * | 2013-03-20 | 2013-06-19 | 华为技术有限公司 | Occupancy rate measuring method and device of central processing unit |
CN104021046A (en) * | 2014-05-29 | 2014-09-03 | 深圳市深信服电子科技有限公司 | Method and device for processing applications |
CN104077209A (en) * | 2013-03-29 | 2014-10-01 | 通用汽车环球科技运作有限责任公司 | Method of calculating cpu utilization |
CN104810920A (en) * | 2014-12-04 | 2015-07-29 | 国家电网公司 | Measurement-control device and method of intelligent transformer station with accurate calculation of CPU utilization rate |
CN105337791A (en) * | 2014-08-14 | 2016-02-17 | 腾讯科技(深圳)有限公司 | Data monitoring method and device |
CN106161543A (en) * | 2015-04-13 | 2016-11-23 | 腾讯科技(深圳)有限公司 | CGI load monitoring method and apparatus |
CN106528318A (en) * | 2016-11-29 | 2017-03-22 | 迈普通信技术股份有限公司 | Thread endless loop detection method and device |
CN107220166A (en) * | 2016-03-22 | 2017-09-29 | 大唐移动通信设备有限公司 | The statistical method and device of a kind of CPU usage |
CN107368400A (en) * | 2017-08-01 | 2017-11-21 | 广东欧珀移动通信有限公司 | CPU monitoring methods, device, computer-readable recording medium and mobile terminal |
CN107682190A (en) * | 2017-09-29 | 2018-02-09 | 郑州云海信息技术有限公司 | A kind of network interface card port busy extent computational methods, device, equipment and storage medium |
CN108196995A (en) * | 2016-12-08 | 2018-06-22 | 成都华为技术有限公司 | A kind of determining processing task is averaged the method and apparatus of duration |
CN110083495A (en) * | 2018-01-26 | 2019-08-02 | 上海新岸线电子技术有限公司 | A method of CPU is occupied based on MIPS analysis thread |
CN110309036A (en) * | 2018-03-27 | 2019-10-08 | 华为技术有限公司 | A kind of CPU usage detection method and detection device |
CN111913805A (en) * | 2020-07-31 | 2020-11-10 | 北京天融信网络安全技术有限公司 | CPU utilization rate calculation method and device |
CN112732540A (en) * | 2020-12-11 | 2021-04-30 | 地平线征程(杭州)人工智能科技有限公司 | Method and device for determining hardware utilization rate, storage medium and electronic equipment |
CN113419917A (en) * | 2021-06-23 | 2021-09-21 | 蔚然(南京)动力科技有限公司 | CPU load rate calculation method for tasks and interrupts of embedded operating system |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1340751A (en) * | 2000-09-01 | 2002-03-20 | Lg电子株式会社 | Central processing unit dispatch method and device |
CN1403912A (en) * | 2001-09-06 | 2003-03-19 | Lg电子株式会社 | Method and apparatus for measuring task occupancy of centhal processor in real-time system |
CN101178688A (en) * | 2007-11-29 | 2008-05-14 | 中兴通讯股份有限公司 | CPU occupancy rate detection method and system of system task |
-
2010
- 2010-12-30 CN CN2010106141081A patent/CN102110043A/en active Pending
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1340751A (en) * | 2000-09-01 | 2002-03-20 | Lg电子株式会社 | Central processing unit dispatch method and device |
CN1403912A (en) * | 2001-09-06 | 2003-03-19 | Lg电子株式会社 | Method and apparatus for measuring task occupancy of centhal processor in real-time system |
CN101178688A (en) * | 2007-11-29 | 2008-05-14 | 中兴通讯股份有限公司 | CPU occupancy rate detection method and system of system task |
Cited By (21)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN103164321A (en) * | 2013-03-20 | 2013-06-19 | 华为技术有限公司 | Occupancy rate measuring method and device of central processing unit |
CN104077209A (en) * | 2013-03-29 | 2014-10-01 | 通用汽车环球科技运作有限责任公司 | Method of calculating cpu utilization |
CN104021046A (en) * | 2014-05-29 | 2014-09-03 | 深圳市深信服电子科技有限公司 | Method and device for processing applications |
CN105337791A (en) * | 2014-08-14 | 2016-02-17 | 腾讯科技(深圳)有限公司 | Data monitoring method and device |
CN104810920A (en) * | 2014-12-04 | 2015-07-29 | 国家电网公司 | Measurement-control device and method of intelligent transformer station with accurate calculation of CPU utilization rate |
CN106161543A (en) * | 2015-04-13 | 2016-11-23 | 腾讯科技(深圳)有限公司 | CGI load monitoring method and apparatus |
CN107220166A (en) * | 2016-03-22 | 2017-09-29 | 大唐移动通信设备有限公司 | The statistical method and device of a kind of CPU usage |
CN107220166B (en) * | 2016-03-22 | 2019-09-17 | 大唐移动通信设备有限公司 | A kind of statistical method and device of CPU usage |
CN106528318A (en) * | 2016-11-29 | 2017-03-22 | 迈普通信技术股份有限公司 | Thread endless loop detection method and device |
CN106528318B (en) * | 2016-11-29 | 2020-02-04 | 迈普通信技术股份有限公司 | Thread dead loop detection method and device |
CN108196995A (en) * | 2016-12-08 | 2018-06-22 | 成都华为技术有限公司 | A kind of determining processing task is averaged the method and apparatus of duration |
CN108196995B (en) * | 2016-12-08 | 2021-06-01 | 成都华为技术有限公司 | Method and equipment for determining average duration of processing tasks |
CN107368400A (en) * | 2017-08-01 | 2017-11-21 | 广东欧珀移动通信有限公司 | CPU monitoring methods, device, computer-readable recording medium and mobile terminal |
CN107682190A (en) * | 2017-09-29 | 2018-02-09 | 郑州云海信息技术有限公司 | A kind of network interface card port busy extent computational methods, device, equipment and storage medium |
CN110083495A (en) * | 2018-01-26 | 2019-08-02 | 上海新岸线电子技术有限公司 | A method of CPU is occupied based on MIPS analysis thread |
CN110309036A (en) * | 2018-03-27 | 2019-10-08 | 华为技术有限公司 | A kind of CPU usage detection method and detection device |
CN111913805A (en) * | 2020-07-31 | 2020-11-10 | 北京天融信网络安全技术有限公司 | CPU utilization rate calculation method and device |
CN111913805B (en) * | 2020-07-31 | 2023-11-28 | 北京天融信网络安全技术有限公司 | CPU utilization rate calculation method and device |
CN112732540A (en) * | 2020-12-11 | 2021-04-30 | 地平线征程(杭州)人工智能科技有限公司 | Method and device for determining hardware utilization rate, storage medium and electronic equipment |
CN112732540B (en) * | 2020-12-11 | 2023-02-21 | 地平线征程(杭州)人工智能科技有限公司 | Method and device for determining hardware utilization rate, storage medium and electronic equipment |
CN113419917A (en) * | 2021-06-23 | 2021-09-21 | 蔚然(南京)动力科技有限公司 | CPU load rate calculation method for tasks and interrupts of embedded operating system |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN102110043A (en) | Method and device for computing CPU occupancy rate | |
US8504753B2 (en) | Suspendable interrupts for processor idle management | |
US8402463B2 (en) | Hardware threads processor core utilization | |
US20120246652A1 (en) | Processor Management Via Thread Status | |
JP2010191937A (en) | Computer device and method for sampling power consumption | |
CN100501694C (en) | Processor availability measuring device and method | |
US20120311544A1 (en) | System aware performance counters | |
CN101510165B (en) | Watchdog circuit dog feeding method, apparatus and veneer | |
CN103853625A (en) | Realizing device and method for multi-task watchdog | |
US9971603B2 (en) | Causing an interrupt based on event count | |
CN101853191B (en) | Method for detecting task endless loop in operating system and operating system | |
CN103676727A (en) | Preemptible and nestable interrupt control method based on embedded chip | |
CN102799212A (en) | Global clock system for multi-core multi-processor parallel system, and application method thereof | |
CN101599663B (en) | Real-time control system for intelligent controller | |
US20140013091A1 (en) | Causing an interrupt based on event count | |
CN102339029B (en) | Method for realizing timing protection of embedded operating system | |
CN108292236B (en) | Information processing method and device | |
CN103034480B (en) | A kind of embedded system timer implementation method | |
CA2767782C (en) | Suspendable interrupts for processor idle management | |
CN102929714B (en) | uC/OS-II-based hardware task manager | |
CN103218256B (en) | A kind of backing method of host batch and system | |
CN108509255A (en) | The treating method and apparatus of hardware interrupts | |
WO2012052733A1 (en) | Exception control in a multiprocessor system | |
US20140075417A1 (en) | Method and Arrangement for Enabling Analysis of a Computer Program Execution | |
CN106933535B (en) | A method of realizing that high-precision is delayed in SCM system |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
C06 | Publication | ||
PB01 | Publication | ||
C10 | Entry into substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
C12 | Rejection of a patent application after its publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20110629 |