CN111414295A - CPU occupancy rate statistical method, device, equipment and medium - Google Patents

CPU occupancy rate statistical method, device, equipment and medium Download PDF

Info

Publication number
CN111414295A
CN111414295A CN202010157228.7A CN202010157228A CN111414295A CN 111414295 A CN111414295 A CN 111414295A CN 202010157228 A CN202010157228 A CN 202010157228A CN 111414295 A CN111414295 A CN 111414295A
Authority
CN
China
Prior art keywords
time
time length
thread
code segment
sub
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.)
Granted
Application number
CN202010157228.7A
Other languages
Chinese (zh)
Other versions
CN111414295B (en
Inventor
修岩
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhejiang Dahua Technology Co Ltd
Original Assignee
Zhejiang Dahua Technology Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Zhejiang Dahua Technology Co Ltd filed Critical Zhejiang Dahua Technology Co Ltd
Priority to CN202010157228.7A priority Critical patent/CN111414295B/en
Publication of CN111414295A publication Critical patent/CN111414295A/en
Application granted granted Critical
Publication of CN111414295B publication Critical patent/CN111414295B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/30Monitoring
    • G06F11/3051Monitoring arrangements for monitoring the configuration of the computing system or of the computing system component, e.g. monitoring the presence of processing resources, peripherals, I/O links, software programs
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/30Monitoring
    • G06F11/3003Monitoring arrangements specially adapted to the computing system or computing system component being monitored
    • G06F11/3024Monitoring arrangements specially adapted to the computing system or computing system component being monitored where the computing system component is a central processing unit [CPU]
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computing Systems (AREA)
  • Physics & Mathematics (AREA)
  • Quality & Reliability (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Physics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The invention discloses a statistical method, a statistical device and a statistical medium for CPU occupancy rate, which are used for solving the problem that the CPU occupancy rate with a function to be detected cannot be determined at present. In the embodiment of the invention, if a counting start instruction for the CPU occupancy rate of the function to be detected is received, the first time when the counting start instruction is received is recorded, wherein the counting start instruction comprises the information of the code segment of the function to be detected; for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length for running the sub-code segment by the thread each time; if a counting ending instruction for the CPU occupancy rate of the code segment is received, recording second time for receiving the counting ending instruction; and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time. Thereby, the running condition of the code segment with the function to be detected can be monitored.

Description

CPU occupancy rate statistical method, device, equipment and medium
Technical Field
The invention relates to the technical field of computers, in particular to a method, a device, equipment and a medium for counting CPU occupancy rate.
Background
In the field of computer technology, CPU utilization is a key fundamental data for monitoring computer system performance, optimizing computer system performance, and detecting computer system failures. How to count the CPU occupancy is a relatively concerned problem in recent years.
In the related art, a method of counting CPU occupancy includes: counting the time length of the CPU occupied by the process or the thread in a set time period, or detecting the time length of the CPU occupied by the process in each set time length. The method only considers the time length of the CPU occupied by the process in the set time length, whether the same function is realized for the function realized by the code segment operated by the process in the set time length, and the stage of the realized function execution are not considered, so that the CPU occupancy rate counted by the method in the follow-up process cannot monitor the operation condition of the code segment realizing the function to be detected.
Therefore, how to determine the occupancy rate of the CPU with the function to be detected is an urgent problem to be solved.
Disclosure of Invention
The embodiment of the invention provides a statistical method, a statistical device and a statistical medium of CPU occupancy rate, which are used for solving the problem that the CPU occupancy rate of a function to be detected cannot be determined at present.
The embodiment of the invention provides a statistical method of CPU occupancy rate, which comprises the following steps:
if a counting starting instruction for the CPU occupancy rate of the function to be detected is received, recording the first time when the counting starting instruction is received, wherein the counting starting instruction comprises the information of the code segment of the function to be detected;
for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length of the thread for running the sub-code segment each time;
if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording second time for receiving the counting ending instruction;
and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time.
Further, the determining the duration of each time the sub-code segment is run by the thread includes:
for each operation of the thread, if the thread operates to the initial code line of the sub-code segment, acquiring a first time length counted by a timer, and updating the stored initial time length by adopting the first time length, wherein the first time length marks the time length from the start of the operation of the thread to the operation of the initial code line of the sub-code segment; if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by a timer, and updating the stored end time length by adopting the second time length, wherein the second time length marks the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time; and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the currently stored starting time length and ending time length.
Further, the current satisfied duration determination requirement includes:
after the end duration is updated, determining that the current meeting duration determination requirement is met; or
And receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
Further, before determining the time length of the thread running the sub-code segment at this time according to the currently stored start time length and end time length, the method further includes:
if the currently stored starting time length is a preset initial value, updating the stored starting time length by adopting a third time length obtained by counting the timer when the counting starting instruction is received, wherein the third time length marks the running time length of the thread from the start to the first time;
and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length obtained by counting the timer when the counting ending instruction is received, wherein the fourth time length marks the running time length of the thread from the start to the second time.
Further, the determining, according to the currently stored start duration and end duration, the duration of the thread running the sub-code segment at this time includes:
and determining the time length of the thread for running the subcode segment at the time according to the difference value of the currently stored ending time length and the starting time length.
Further, the determining the CPU occupancy of the function to be detected according to each determined duration and the time period determined by the second time and the first time includes:
and acquiring the ratio of the sum of each determined time length to the time period, and determining the CPU occupancy rate of the function to be detected.
The embodiment of the invention also provides a device for counting the CPU occupancy rate, which comprises:
the device comprises a receiving unit and a processing unit, wherein the receiving unit is used for recording the first time of receiving a counting starting instruction if the counting starting instruction of the CPU occupancy rate of the function to be detected is received, wherein the counting starting instruction comprises the information of a code segment of the function to be detected; if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording second time for receiving the counting ending instruction;
the determining unit is used for determining a thread for running each sub-code segment contained in the code segment and determining the time length of the thread for running the sub-code segment each time;
and the processing unit is used for determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time.
Further, the determining unit is configured to, for each operation of the thread, obtain a first time length counted by a timer if the thread runs to the start code line of the sub-code segment this time, update the stored start time length with the first time length, where the first time length identifies a time length from when the thread starts to run to when the thread runs to the start code line of the sub-code segment this time; if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by a timer, and updating the stored end time length by adopting the second time length, wherein the second time length marks the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time; and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the currently stored starting time length and ending time length.
Further, the determining unit is configured to determine that the current meeting duration determination requirement is met after the ending duration is updated; or receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
Further, the determining unit is further configured to, before determining the time length during which the sub-code segment is operated by the thread this time according to the currently stored start time length and end time length, if the currently stored start time length is a preset initial value, update the stored start time length by using a third time length counted by the timer, which is obtained when the counting start instruction is received, where the third time length identifies the operation time length of the thread from the start to the first time; and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length obtained by counting the timer when the counting ending instruction is received, wherein the fourth time length marks the running time length of the thread from the start to the second time.
Further, the determining unit is configured to determine, according to a difference between the currently stored ending time length and the starting time length, a time length for the thread to run the sub-code segment at this time.
Further, the processing unit is configured to obtain a ratio of the sum of each determined time duration to the time period, and determine the CPU occupancy of the function to be detected.
The embodiment of the present invention further provides an electronic device, where the electronic device at least includes a processor and a memory, and the processor is configured to implement the steps of any of the above CPU occupancy rate statistics methods when executing a computer program stored in the memory.
The embodiment of the present invention further provides a computer-readable storage medium, which stores a computer program, and when the computer program is executed by a processor, the computer program implements the steps of any of the above methods for counting CPU occupancy rates.
In the embodiment of the invention, if a counting start instruction for the CPU occupancy rate of the function to be detected is received, the first time when the counting start instruction is received is recorded, wherein the counting start instruction comprises the information of the code segment of the function to be detected; determining a thread for running the sub-code segments according to each sub-code segment contained in the code segment, and determining the time length for running the sub-code segment by the thread each time; if a counting ending instruction for the CPU occupancy rate of the code segment is received, recording second time for receiving the counting ending instruction; and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time. Thereby, the running condition of the code segment with the function to be detected can be monitored.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings needed to be used in the description of the embodiments will be briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts.
Fig. 1 is a schematic diagram of a statistical process of CPU occupancy according to an embodiment of the present invention;
FIG. 2 is a schematic diagram of a start code line and an end code line of a certain sub-code segment according to an embodiment of the present invention;
FIG. 3 is a schematic diagram illustrating a time duration for analyzing the sub-code segment of the thread during the current operation according to an embodiment of the present invention;
fig. 4 is a schematic diagram of a specific statistical flow of CPU occupancy rates according to an embodiment of the present invention;
fig. 5 is a schematic diagram illustrating a statistical flow of yet another specific CPU occupancy rate according to an embodiment of the present invention;
FIG. 6 is a diagram illustrating a thread structure for executing each sub-code segment according to an embodiment of the present invention;
fig. 7 is a schematic diagram of a statistical method for detecting CPU occupation time length of a function to be detected according to an embodiment of the present invention;
fig. 8 is a schematic structural diagram of a CPU occupancy rate statistics apparatus according to an embodiment of the present invention;
fig. 9 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
The present invention will be described in further detail with reference to the attached drawings, and it should be understood that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The embodiment of the invention provides a statistical method, a statistical device and a statistical medium of CPU occupancy rate, which are used for determining the CPU occupancy rate of a function to be detected.
Example 1:
fig. 1 is a schematic diagram of a statistical process of CPU occupancy provided in an embodiment of the present invention, where the process includes:
s101: if a counting starting instruction for the CPU occupancy rate of the function to be detected is received, recording the first time of receiving the counting starting instruction, wherein the counting starting instruction comprises the information of the code segment of the function to be detected.
In the embodiment of the invention, when the electronic equipment receives a counting start instruction for the CPU occupancy rate of the function to be detected, the code segment of the function to be detected for counting the CPU occupancy rate is determined according to the information of the code segment of the function to be detected contained in the counting start instruction, the first time for receiving the counting start instruction is recorded, and corresponding processing is carried out subsequently on the basis of the first time and the determined code segment of the function to be detected, so that the CPU occupancy rate of the code segment of the function to be detected is determined.
The information of the code segment to be tested may be a storage location of the program containing the function to be tested, and a number of lines of the code segment in the program, such as: counting the program stored in a certain file a, the code segment of the program in lines 200 and 400 is a code segment for implementing the function to be tested, and may also be a storage location of the program including the code segment for implementing the function to be tested, and the specific content of the code segment. Specifically, the setting can be flexibly performed according to actual requirements, and is not specifically limited herein.
S102: for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length of the thread for running the sub-code segment each time.
In the embodiment of the present invention, since the functional code segments to be checked may include sub-code segments for implementing different stages of the function to be checked, after the code segment to be checked is determined, at least one sub-code segment included is determined according to the code segment, for example, after the code segment for implementing the function of calculating the sum of products is determined, a sub-code segment including the product and a summed sub-code segment can be determined according to the code segment.
For each sub-code segment comprised by the code segment, the thread running the sub-code segment is determined, which may be the same as the thread running the other sub-code segments, i.e. it is possible that at least two sub-code segments run on the same thread, but it is also possible that each sub-code segment runs on a different thread. Still taking the above as an example, the thread running the sub-code segment for product calculation is ThreadN, the thread running the sub-code segment for summation is ThreadM, or the thread running the sub-code segment for product calculation and the thread running the sub-code segment for summation are ThreadM.
It should be noted that determining, according to the sub-code segment, a thread running the sub-code segment is the prior art, and details are not described herein again.
After the statistics starting instruction in the above embodiment is received, the number of times that each sub-code segment is run on the corresponding thread is more than once, so after the thread of each sub-code segment is determined to run, for each sub-code, the time length that the thread runs the sub-code segment each time is determined, so that the CPU occupancy of the function to be tested is determined according to each determined time length in the following.
S103: and if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording a second time for receiving the counting ending instruction.
The electronic equipment continuously counts the time length of the thread corresponding to each sub-code segment for running the corresponding sub-code segment every time, stops counting the time length of the thread corresponding to each sub-code segment for running the corresponding sub-code segment every time after receiving a counting ending instruction for the CPU occupancy rate of the function to be detected, and records the second time for receiving the counting ending instruction.
S104: and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time.
And when a counting ending instruction for the CPU occupancy rate of the function to be detected is received and the second time for receiving the counting ending instruction is recorded, the CPU occupancy rate of the function to be detected can be determined according to each determined time length, the time period determined by the second time and the first time.
Specifically, the determining the CPU occupancy of the function to be detected according to the determination of each of the durations and the time period determined by the second time and the first time includes:
and acquiring the ratio of the sum of each determined time length to the time period, and determining the CPU occupancy rate of the function to be detected.
If the code segment to be detected only comprises one sub-code segment, the CPU occupancy rate of the function to be detected is directly determined according to the ratio of the sum of the time length of the sub-code segment operated by the thread operating the sub-code segment each time and the time period determined by the second time and the first time.
For example, the time period determined by the second time and the first time is 10min, the code segment implementing the function of product calculation only contains one sub-code segment, the sub-code segment runs on the thread ThreadA, and the sum of the time periods of the thread ThreadA running the sub-code segment of product calculation each time is 8min, so that the CPU occupancy rate of the function to be detected is determined to be 8/10 ═ 0.8.
And if the code segment to be detected comprises at least two sub-code segments, determining the CPU occupancy rate of the function to be detected according to the ratio of the sum of the time lengths of the corresponding sub-code segments operated by the thread corresponding to each sub-code segment and the time period determined by the second time and the first time.
Still taking the above as an example, the code segment implementing the function of summing products comprises sub-code segments of summing products and sub-code segments of summing, wherein the sub-code segments of summing products operate on thread ThreadB, the sub-code segments of summing operate on thread ThreadD, the sum of the durations of the sub-code segments of summing products operated by thread ThreadB each time is determined to be 3min, the sum of the durations of the sub-code segments operated by thread ThreadD each time is determined to be 4min, the sum of the durations of the corresponding sub-code segments operated by each thread each time is determined to be 3+4 ═ 7min, and the CPU occupancy of the function to be detected is determined to be 7/10 ═ 0.7.
In the embodiment of the invention, if a counting start instruction for the CPU occupancy rate of the function to be detected is received, the first time when the counting start instruction is received is recorded, wherein the counting start instruction comprises the information of the code segment of the function to be detected; for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length for running the sub-code segment by the thread each time; if a counting ending instruction for the CPU occupancy rate of the code segment is received, recording second time for receiving the counting ending instruction; and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time, so that the running condition of the code segment with the function to be detected can be monitored.
Example 2:
in order to accurately determine the CPU occupancy of the function under test, on the basis of the foregoing embodiment, in an embodiment of the present invention, the determining a time length for the thread to run the sub-code segment each time includes:
for each operation of the thread, if the thread operates to the initial code line of the sub-code segment, acquiring a first time length counted by a timer, and updating the stored initial time length by adopting the first time length, wherein the first time length marks the time length from the start of the operation of the thread to the operation of the initial code line of the sub-code segment; if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by a timer, and updating the stored end time length by adopting the second time length, wherein the second time length marks the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time; and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the currently stored starting time length and ending time length.
For convenience of counting, a timer is preset, the timer can count the corresponding time length of the thread corresponding to each sub-code segment from being started to running at each time point, and at any time point, the time length of the thread from being started to running at the current time point can be determined according to the time length of the thread counted by the timer. Based on this, in the embodiment of the present invention, after determining the thread for running each sub-code segment, for each run of the thread corresponding to each sub-code segment, a first duration of the thread counted by a timer is obtained when the thread runs to the start code line of the sub-code segment, the first duration identifying a duration from starting running to the start code line of the sub-code segment when the thread runs to the end code line of the sub-code segment, a second duration of the thread counted by the timer is obtained when the thread runs to the end code line of the sub-code segment, and the second duration identifying a duration from starting running to the end code line of the sub-code segment when the thread runs to the end code line of the sub-code segment.
Determining the start code line and the end code line of each sub-code segment belongs to the prior art, and is not described herein.
Fig. 2 is a schematic diagram of a start code line and an end code line of a certain sub-code section according to an embodiment of the present invention. When the sub-code segment FuncA2() - > FuncA3() is determined, the start code line and the end code line of the sub-code segment FuncA2() - > FuncA3() are determined.
In addition, in the embodiment of the present invention, for each sub-code segment corresponding to a thread, a storage space is further provided for specifically storing a start duration and an end duration of each time the thread runs the sub-code segment, and an initial value is set in advance for the start duration and the end duration stored in the storage space, and preferably, the initial value is 0.
Subsequently, for each operation of the thread, if the thread operates to the initial code line of the sub-code segment, acquiring a first time length counted by a timer, and updating the stored starting time length by adopting the first time length, namely, storing the first time length as the starting time length in a corresponding space; and if the thread runs to the termination code line of the sub-code segment, acquiring a second time length counted by the timer, and updating the stored termination time length by adopting the second time length, namely, storing the second time length serving as the termination time length in a corresponding space.
For each run of the thread, when the thread runs to the end code line of the sub-code segment, the end of the run is determined, a statistical end instruction may be received at any time in the process of running the thread, and if an end instruction is received in the process of running the sub-code segment by the thread, namely the end instruction is received when the end code line of the sub-code segment is not yet run, the run is actually ended at this time. Therefore, in the embodiment of the present invention, according to the currently saved start duration and end duration, before determining the duration of the thread running the sub-code segment this time, it is determined whether the duration determination requirement is currently met. If the current time length determination requirement is met, the time length of the thread for running the subcode segment at the time can be determined according to the currently stored starting time length and ending time length.
Wherein the current satisfied duration determination requirement comprises:
after the end duration is updated, determining that the current meeting duration determination requirement is met; or
And receiving a counting ending instruction of the CPU occupancy rate of the function to be detected, and determining that the current time duration is met.
In the process of the running of the thread, if the ending time length is updated, the running of the thread is indicated to be run to the ending code line of the sub-code segment, and the running of the thread is finished, the current meeting time length determination requirement is determined, and the time length of the thread running the sub-code segment at the time can be accurately determined according to the currently stored starting time length and ending time length.
In the process of the thread running, the thread may not run to the stop code line of the sub-code segment, and a statistical ending instruction for the CPU occupancy of the function to be detected is received, and at this time, the time length of the thread running the sub-code segment at this time needs to be counted.
For each run of the thread, it cannot be guaranteed that the first time length counted by the timer when the thread runs the sub-code segment this time, and the time when the thread runs to the initial code line of the sub-code segment this time can be definitely obtained, because the time when the counting start instruction is received is unpredictable, and when the counting start instruction for the CPU occupancy rate of the function to be detected is received, the thread may already run to the middle part of the sub-code segment this time, and therefore the first time length when the thread runs to the initial code line of the sub-code segment this time cannot be counted. Correspondingly, for each operation of the thread, it cannot be guaranteed that the thread runs the sub-code segment at the time, and the second time counted by the timer can be definitely obtained when the thread runs to the termination code line of the sub-code segment at the time, because the time of receiving the counting end instruction is also unpredictable, when the thread runs to the middle part of the sub-code segment at the time, the counting end instruction for the CPU occupancy rate of the function to be detected is received, and therefore the second time running to the termination code line of the sub-code segment at the time by the thread cannot be counted. In order to accurately determine the length of time that the thread runs the sub-code segment each time, if the start length or the end length stored in the memory space is not determined or the determination is inaccurate, the length of time that the thread runs the sub-code segment each time cannot be accurately determined.
Therefore, in order to ensure the accuracy of the determined time length for the thread to run the sub-code segment each time, in an embodiment of the present invention, before determining the time length for the thread to run the sub-code segment this time according to the currently saved start time length and end time length, the method further includes:
if the currently stored starting time length is a preset initial value, updating the stored starting time length by adopting a third time length obtained by counting the timer when the counting starting instruction is received, wherein the third time length marks the running time length of the thread from the start to the first time;
and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length obtained by counting the timer when the counting ending instruction is received, wherein the fourth time length marks the running time length of the thread from the start to the second time.
If the preset initial value is stored in the storage space of the starting time length, it is very likely that the counting starting instruction for the CPU occupancy rate of the function to be detected is received only when the thread runs the sub-code segment this time, and the counting time length of the timer when the thread runs the initial code line of the sub-code segment this time is not obtained, so that in order to ensure the accuracy of the subsequent CPU occupancy rate counting, the stored starting time length is updated by using the third time length counted by the acquired timer when the counting starting instruction is received, wherein the third time length identifies the running time length of the thread from the starting to the first time.
If the ending duration stored in the ending duration storage space is not greater than the starting duration stored in the starting duration storage space, it is very likely that the thread runs into the subcode segment this time, only when a counting end instruction of the CPU occupancy rate of the function to be detected is received and the end code line of the thread running to the subcode segment at the time is not obtained, counting the second time length by the timer to ensure that when the ending code line running to the subcode segment last time according to the thread is still stored in the storage space of the ending time length, the obtained second time length counted by the timer is updated to obtain the ending time length, so in order to ensure the accuracy of the follow-up CPU occupancy rate statistics, and updating the stored ending time length by adopting a fourth time length counted by the acquired timer when the counting ending instruction is received, wherein the fourth time length identifies the running time length of the thread from the starting to the second time.
Fig. 3 is a schematic diagram of analyzing the time length of the thread running the sub-code segment at this time according to the embodiment of the present invention, as shown in fig. 3, for the counted thread running the sub-code segment for the first time, whether the saved starting time length is a preset initial value is determined, if yes, it is very likely to indicate that the counting starting instruction for the CPU occupancy of the function to be detected is received only during the process of the thread running the sub-code segment at this time, so that the counted time length of the timer is not obtained when the initial code line of the sub-code segment which is run by the thread at this time is run, and the saved starting time length is updated by using the third counted time length of the timer which is obtained when the counting starting instruction is received; and for each time when the sub-code segment is completely run by the thread, updating the stored starting time length according to the first time length, and updating the stored finishing time length according to the second time length.
And for the last operation of the thread receiving the counting ending instruction, judging whether the currently stored ending time is not greater than the currently stored starting time, if so, indicating that the counting ending instruction of the CPU occupancy rate of the function to be detected is received only in the process of the thread running to the subcode section, and if not, acquiring the ending code line of the thread running to the subcode section, counting the second time by using the timer, so that the ending time obtained by updating the acquired second time counted by using the timer when the ending code line of the subcode section is run last time by using the thread is still stored in the storage space of the ending time, and updating the stored ending time by using the acquired fourth time counted by using the timer when the counting ending instruction is received.
And after the stored start time and the stored end time are updated, determining the time length of the thread for running the subcode segment at the time according to the currently stored start time and end time. Specifically, the determining, according to the currently stored start duration and end duration, the duration of the thread running the sub-code segment at this time includes:
and determining the time length of the thread for running the subcode segment at the time according to the difference value of the currently stored ending time length and the starting time length.
And updating the stored starting time and the stored ending time so as to ensure that the determined time length of the thread for running the sub-code segment at the time is more accurate, and even if the thread is scheduled and does not occupy the CPU when running the sub-code segment, the time length of the thread for running the sub-code segment at the time can be counted according to each acquired time length so as to help the CPU occupancy rate of the function to be detected to be determined more accurately subsequently.
Example 3:
fig. 4 is a schematic diagram of a specific statistical process of CPU occupancy rate, where the process includes:
s401: and if a counting starting instruction for the CPU occupancy rate of the function to be detected is received, recording the first time when the counting starting instruction is received.
Wherein, the counting start instruction comprises the information of the code segment with the function to be detected.
S402: from each of the child code sections that the code section contains, a thread is determined for running the child code section.
S403: aiming at each operation of the thread, if the thread operates to the initial code line of the sub-code segment, acquiring a first time length counted by a timer, and updating the stored starting time length by adopting the first time length, wherein the first time length marks the time length from the start of the operation of the thread to the operation of the initial code line of the sub-code segment; and if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by the timer, updating the stored ending time length by adopting the second time length, and identifying the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time.
S404: and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the difference value of the currently stored ending time length and the starting time length.
Wherein the current meeting duration determination requirement comprises: after the end duration is updated, determining that the current time duration determination requirement is met; or receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
In addition, before the time length of the thread for running the subcode segment at the time is determined according to the difference value between the currently stored ending time length and the currently stored starting time length, if the currently stored starting time length is a preset initial value, the stored starting time length is updated by adopting a third time length obtained by counting by the timer when a counting starting instruction is received, and the third time length identifies the running time length of the thread from the starting to the first time;
and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length counted by the acquired timer when the counting ending instruction is received, wherein the fourth time length identifies the running time length of the thread from the starting to the second time.
S405: and if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording a second time for receiving the counting ending instruction.
S406: and acquiring the ratio of the sum of each determined time length to the time period, and determining the CPU occupancy rate of the function to be detected.
Fig. 5 is a schematic diagram of a statistical process of yet another specific CPU occupancy rate provided in the embodiment of the present invention, where the process includes three parts, namely determination of a code segment of a function to be checked, statistics of a duration, and calculation of the CPU occupancy rate of the function to be checked, and each part is described in detail below:
first, the determination of the first part of the function-under-test code segment comprises: the electronic device may preset a statistical thread dedicated to receiving the interactive instruction, and when the statistical thread receives a statistical start instruction for the CPU occupancy rate of the function to be tested, the statistical thread enters sleep waiting. The electronic equipment records the first time when the counting thread receives the counting starting instruction, and determines the code segment needing counting the CPU occupancy rate currently according to the information of the code segment with the function to be detected contained in the counting starting instruction.
Secondly, the statistics of the duration of the second part include: for each operation of a thread corresponding to each sub-code segment, if the thread operates to the initial code line of the sub-code segment, acquiring a first time length counted by a timer, and updating the stored starting time length by adopting the first time length, wherein the first time length marks the time length from the start of the thread to the operation of the thread to the initial code line of the sub-code segment; and if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by the timer, updating the stored ending time length by adopting the second time length, and identifying the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time. And if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the difference value of the currently stored ending time length and the starting time length.
Wherein the current meeting duration determination requirement comprises: after the end duration is updated, determining that the current time duration determination requirement is met; or receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
In addition, before the time length of the thread for running the subcode segment at the time is determined according to the difference value between the currently stored ending time length and the currently stored starting time length, if the currently stored starting time length is a preset initial value, the stored starting time length is updated by adopting a third time length obtained by counting by the timer when a counting starting instruction is received, and the third time length identifies the running time length of the thread from the starting to the first time;
and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length counted by the acquired timer when the counting ending instruction is received, wherein the fourth time length identifies the running time length of the thread from the starting to the second time.
Finally, the calculation of the CPU occupancy rate of the third part of the function to be tested comprises the following steps: and after the preset counting thread receives a counting ending instruction for the CPU occupancy rate of the function to be detected, the counting thread stops dormancy waiting. The electronic equipment stops counting the time length of each sub-code segment corresponding to the thread running the corresponding sub-code segment each time, and records the second time of receiving the counting ending instruction. And acquiring the ratio of the sum of each determined time length to the time period, and determining the CPU occupancy rate of the function to be detected.
FIG. 6 is a schematic diagram of a thread structure for executing each sub-code segment according to an embodiment of the present invention. As shown in fig. 6, the code segment to be tested includes sub-code segment FuncA2() - > FuncA3(), and sub-code segment FuncB2() - > FuncB3(), where the thread running sub-code segment FuncA2() - > FuncA3() is ThreadA and the thread running sub-code segment FuncB2() - > FuncB3() is ThreadB.
Fig. 7 is a schematic diagram of a statistical method for counting the CPU time duration occupied by the function to be detected according to an embodiment of the present invention, as shown in fig. 7, after receiving a statistical start instruction for the CPU occupancy of the function to be detected, the statistical thread ThreadC enters sleep waiting, the electronic device records a first time when the statistical start instruction is received, and starts to count the time duration that the thread ThreadA runs the subcode segment FuncA2() - > FuncA3() each time, until after receiving a statistical end instruction for the CPU occupancy of the function to be detected, the thread ThreadC terminates the sleep waiting, the electronic device records a second time when the statistical end instruction is received, and ends to count the CPU time duration occupied by the function to be detected.
In the embodiment of the invention, after the CPU occupancy rate of the function to be detected is determined, the average CPU occupancy rate of the function to be detected is obtained according to the determined time and the CPU occupancy rate of the function to be detected determined before the time, the average CPU occupancy rate of the function to be detected is stored, and when the next code segment of the function to be detected runs, the electronic equipment reserves the CPU duration which is not lower than the average CPU occupancy rate for the code segment of the function to be detected.
And meanwhile, according to the stored average CPU occupancy rate of the function to be detected, the running condition of the code segment of the function to be detected can be monitored, if the difference value between the CPU occupancy rate of the function to be detected and the stored CPU occupancy rate is not within a preset range, the running abnormality of the code segment of the function to be detected is determined, abnormality alarm can be carried out, and related workers are informed to carry out inspection.
Example 4:
fig. 8 is a schematic structural diagram of a CPU occupancy rate statistics apparatus provided in an embodiment of the present invention, where the apparatus includes:
the receiving unit 81 is configured to record a first time when a counting start instruction for the CPU occupancy of the function to be tested is received, where the counting start instruction includes information of the code segment of the function to be tested; if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording second time for receiving the counting ending instruction;
a determining unit 82, configured to determine, for each sub-code segment included in the code segment, a thread for running the sub-code segment, and determine a duration of each time the thread runs the sub-code segment;
and the processing unit 83 is configured to determine the CPU occupancy of the function to be detected according to each determined time length, and the time period determined by the second time and the first time.
Further, the determining unit 82 is configured to, for each running of the thread, obtain a first time length counted by a timer if the thread runs to the start code line of the sub-code segment this time, update the saved start time length with the first time length, where the first time length identifies a time length from when the thread runs from the start to when the thread runs to the start code line of the sub-code segment this time; if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by a timer, and updating the stored end time length by adopting the second time length, wherein the second time length marks the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time; and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the currently stored starting time length and ending time length.
Further, the determining unit 82 is configured to determine that the duration determination requirement is currently met after the ending duration is updated; or receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
Further, the determining unit 82 is further configured to, before determining the time length during which the sub-code segment is operated by the thread this time according to the currently stored starting time length and ending time length, if the currently stored starting time length is a preset initial value, update the stored starting time length by using a third time length obtained by counting by the timer when the counting start instruction is received, where the third time length identifies the operation time length of the thread from the start to the first time; and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length obtained by counting the timer when the counting ending instruction is received, wherein the fourth time length marks the running time length of the thread from the start to the second time.
Further, the determining unit 82 is configured to determine, according to a difference between the currently stored ending time length and the starting time length, a time length for the thread to run the sub-code segment at this time.
Further, the processing unit 83 is configured to obtain a ratio of the sum of each determined time duration to the time period, and determine the CPU occupancy of the function to be detected.
In the embodiment of the invention, if a counting start instruction for the CPU occupancy rate of the function to be detected is received, the first time when the counting start instruction is received is recorded, wherein the counting start instruction comprises the information of the code segment of the function to be detected; for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length for running the sub-code segment by the thread each time; if a counting ending instruction for the CPU occupancy rate of the code segment is received, recording second time for receiving the counting ending instruction; and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time. Thereby, the running condition of the code segment with the function to be detected can be monitored.
Example 5:
fig. 9 is a schematic structural diagram of an electronic device according to an embodiment of the present invention, and on the basis of the foregoing embodiments, an embodiment of the present invention further provides an electronic device, as shown in fig. 9, including: the system comprises a processor 91, a communication interface 92, a memory 93 and a communication bus 94, wherein the processor 91, the communication interface 92 and the memory 93 are communicated with each other through the communication bus 94;
the memory 93 has stored therein a computer program which, when executed by the processor 91, causes the processor 91 to perform the steps of:
if a counting starting instruction for the CPU occupancy rate of the function to be detected is received, recording the first time when the counting starting instruction is received, wherein the counting starting instruction comprises the information of the code segment of the function to be detected;
for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length of the thread for running the sub-code segment each time;
if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording second time for receiving the counting ending instruction;
and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time.
Further, the processor 91 is configured to, for each operation of the thread, obtain a first time length counted by a timer if the thread runs to the start code line of the sub-code segment this time, update the stored start time length with the first time length, where the first time length identifies a time length from when the thread runs from the start to when the thread runs to the start code line of the sub-code segment this time; if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by a timer, and updating the stored end time length by adopting the second time length, wherein the second time length marks the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time; and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the currently stored starting time length and ending time length.
Further, the processor 91 is configured to determine that the current meeting duration determination requirement is met after the ending duration is updated; or receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
Further, the processor 91 is further configured to, before determining the time length during which the sub-code segment is operated by the thread this time according to the currently stored start time length and end time length, if the currently stored start time length is a preset initial value, update the stored start time length by using a third time length obtained by counting by the timer when the counting start instruction is received, where the third time length identifies the operation time length of the thread from the start to the first time; and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length obtained by counting the timer when the counting ending instruction is received, wherein the fourth time length marks the running time length of the thread from the start to the second time.
Further, the processor 91 is configured to determine, according to a difference between the currently stored ending time length and the starting time length, a time length for the thread to run the sub-code segment at this time.
Further, the processor 91 is configured to obtain a ratio of the sum of each determined time duration to the time period, and determine the CPU occupancy of the function under test.
Because the principle of the electronic device for solving the problem is similar to the statistical method of the CPU occupancy rate, the implementation of the electronic device may refer to the implementation of the method, and repeated details are not repeated.
The communication bus mentioned in the electronic device may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is shown, but this does not mean that there is only one bus or one type of bus.
The communication interface 92 is used for communication between the above-described electronic apparatus and other apparatuses.
The Memory may include a Random Access Memory (RAM) or a Non-Volatile Memory (NVM), such as at least one disk Memory. Alternatively, the memory may be at least one memory device located remotely from the processor.
The processor may be a general-purpose processor, including a central processing unit, a Network Processor (NP), and the like; but may also be a Digital instruction processor (DSP), an application specific integrated circuit, a field programmable gate array or other programmable logic device, discrete gate or transistor logic, discrete hardware components, or the like.
In the embodiment of the invention, if a counting start instruction for the CPU occupancy rate of the function to be detected is received, the first time when the counting start instruction is received is recorded, wherein the counting start instruction comprises the information of the code segment of the function to be detected; for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length for running the sub-code segment by the thread each time; if a counting ending instruction for the CPU occupancy rate of the code segment is received, recording second time for receiving the counting ending instruction; and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time. Thereby, the running condition of the code segment with the function to be detected can be monitored.
Example 6:
on the basis of the foregoing embodiments, an embodiment of the present invention provides a computer-readable storage medium, in which a computer program executable by an electronic device is stored, and when the program is run on the electronic device, the electronic device is caused to execute the following steps:
if a counting starting instruction for the CPU occupancy rate of the function to be detected is received, recording the first time when the counting starting instruction is received, wherein the counting starting instruction comprises the information of the code segment of the function to be detected;
for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length of the thread for running the sub-code segment each time;
if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording second time for receiving the counting ending instruction;
and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time.
Further, the determining the duration of each time the sub-code segment is run by the thread includes:
for each operation of the thread, if the thread operates to the initial code line of the sub-code segment, acquiring a first time length counted by a timer, and updating the stored initial time length by adopting the first time length, wherein the first time length marks the time length from the start of the operation of the thread to the operation of the initial code line of the sub-code segment; if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by a timer, and updating the stored end time length by adopting the second time length, wherein the second time length marks the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time; and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the currently stored starting time length and ending time length.
Further, the current satisfied duration determination requirement includes:
after the end duration is updated, determining that the current meeting duration determination requirement is met; or
And receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
Further, before determining the time length of the thread running the sub-code segment at this time according to the currently stored start time length and end time length, the method further includes:
if the currently stored starting time length is a preset initial value, updating the stored starting time length by adopting a third time length obtained by counting the timer when the counting starting instruction is received, wherein the third time length marks the running time length of the thread from the start to the first time;
and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length obtained by counting the timer when the counting ending instruction is received, wherein the fourth time length marks the running time length of the thread from the start to the second time.
Further, the determining, according to the currently stored start duration and end duration, the duration of the thread running the sub-code segment at this time includes:
and determining the time length of the thread for running the subcode segment at the time according to the difference value of the currently stored ending time length and the starting time length.
Further, the determining the CPU occupancy of the function to be detected according to each determined duration and the time period determined by the second time and the first time includes:
and acquiring the ratio of the sum of each determined time length to the time period, and determining the CPU occupancy rate of the function to be detected.
The computer readable storage medium may be any available medium or data storage device that can be accessed by a processor in an electronic device, including but not limited to magnetic memory such as floppy disks, hard disks, magnetic tape, magneto-optical disks (MO), etc., optical memory such as CDs, DVDs, BDs, HVDs, etc., and semiconductor memory such as ROMs, EPROMs, EEPROMs, non-volatile memory (NANDF L ASH), Solid State Disks (SSDs), etc.
In the embodiment of the invention, if a counting start instruction for the CPU occupancy rate of the function to be detected is received, the first time when the counting start instruction is received is recorded, wherein the counting start instruction comprises the information of the code segment of the function to be detected; for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length for running the sub-code segment by the thread each time; if a counting ending instruction for the CPU occupancy rate of the code segment is received, recording second time for receiving the counting ending instruction; and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time. Thereby, the running condition of the code segment with the function to be detected can be monitored.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to the application. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present application without departing from the spirit and scope of the application. Thus, if such modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is intended to include such modifications and variations as well.

Claims (14)

1. A statistical method for CPU occupancy, the method comprising:
if a counting starting instruction for the CPU occupancy rate of the function to be detected is received, recording the first time when the counting starting instruction is received, wherein the counting starting instruction comprises the information of the code segment of the function to be detected;
for each sub-code segment contained in the code segment, determining a thread for running the sub-code segment, and determining the time length of the thread for running the sub-code segment each time;
if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording second time for receiving the counting ending instruction;
and determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time.
2. The method of claim 1, wherein determining the length of time that the thread runs the subcode segment each time comprises:
for each operation of the thread, if the thread operates to the initial code line of the sub-code segment, acquiring a first time length counted by a timer, and updating the stored initial time length by adopting the first time length, wherein the first time length marks the time length from the start of the operation of the thread to the operation of the initial code line of the sub-code segment; if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by a timer, and updating the stored end time length by adopting the second time length, wherein the second time length marks the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time; and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the currently stored starting time length and ending time length.
3. The method of claim 2, wherein the current satisfied duration determination requirement comprises:
after the end duration is updated, determining that the current meeting duration determination requirement is met; or
And receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
4. The method of claim 2, wherein before determining the duration of the sub-code segment executed by the thread this time according to the currently saved start duration and end duration, the method further comprises:
if the currently stored starting time length is a preset initial value, updating the stored starting time length by adopting a third time length obtained by counting the timer when the counting starting instruction is received, wherein the third time length marks the running time length of the thread from the start to the first time;
and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length obtained by counting the timer when the counting ending instruction is received, wherein the fourth time length marks the running time length of the thread from the start to the second time.
5. The method of claim 2, wherein determining the duration of the sub-code segment executed by the thread this time according to the currently saved start duration and end duration comprises:
and determining the time length of the thread for running the subcode segment at the time according to the difference value of the currently stored ending time length and the starting time length.
6. The method according to claim 1, wherein determining the CPU occupancy of the function under test based on each of the determined time periods and the time periods determined by the second time and the first time comprises:
and acquiring the ratio of the sum of each determined time length to the time period, and determining the CPU occupancy rate of the function to be detected.
7. An apparatus for counting CPU occupancy, the apparatus comprising:
the device comprises a receiving unit and a processing unit, wherein the receiving unit is used for recording the first time of receiving a counting starting instruction if the counting starting instruction of the CPU occupancy rate of the function to be detected is received, wherein the counting starting instruction comprises the information of a code segment of the function to be detected; if a counting ending instruction for the CPU occupancy rate of the function to be detected is received, recording second time for receiving the counting ending instruction;
the determining unit is used for determining a thread for running each sub-code segment contained in the code segment and determining the time length of the thread for running the sub-code segment each time;
and the processing unit is used for determining the CPU occupancy rate of the function to be detected according to each determined time length and the time period determined by the second time and the first time.
8. The apparatus of claim 7, wherein the determining unit is configured to, for each run of the thread, obtain a first time length counted by a timer if the thread runs to the start code line of the sub-code segment this time, update the saved start time length with the first time length, where the first time length identifies a time length from starting the run of the thread to the start code line of the sub-code segment this time; if the thread runs to the stop code line of the sub-code segment at the time, acquiring a second time length counted by a timer, and updating the stored end time length by adopting the second time length, wherein the second time length marks the time length from the start of the thread to the time when the thread runs to the stop code line of the sub-code segment at the time; and if the current time length determination requirement is met, determining the time length of the thread for running the subcode segment at the time according to the currently stored starting time length and ending time length.
9. The apparatus according to claim 8, wherein the determining unit is configured to determine that the duration determination requirement is currently satisfied after the end duration is updated; or receiving a counting ending instruction of the CPU occupancy rate of the code segment, and determining that the current time duration determination requirement is met.
10. The apparatus according to claim 8, wherein the determining unit is further configured to determine, according to a currently stored start duration and an end duration, that is before the duration in which the thread runs the sub-code segment this time, if the currently stored start duration is a preset initial value, update the stored start duration with a third duration counted by the timer, obtained when the counting start instruction is received, where the third duration identifies a running duration of the thread from start to the first time; and if the currently stored ending time length is not greater than the currently stored starting time length, updating the stored ending time length by adopting a fourth time length obtained by counting the timer when the counting ending instruction is received, wherein the fourth time length marks the running time length of the thread from the start to the second time.
11. The apparatus of claim 8, wherein the determining unit is configured to determine the time duration for the thread to run the subcode segment at the time according to a difference between a currently saved ending time duration and a starting time duration.
12. The apparatus according to claim 7, wherein the processing unit is configured to obtain a ratio of a sum of each of the determined time periods to the time period to determine the CPU occupancy of the function under test.
13. An electronic device, characterized in that the electronic device comprises at least a processor and a memory, the processor being configured to implement the steps of the statistical method of CPU occupancy as claimed in any one of claims 1-6 when executing a computer program stored in the memory.
14. A computer-readable storage medium, characterized in that it stores a computer program which, when executed by a processor, implements the steps of the statistical method of CPU occupancy according to any one of claims 1 to 6.
CN202010157228.7A 2020-03-09 2020-03-09 Statistical method, device, equipment and medium for CPU occupancy rate Active CN111414295B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010157228.7A CN111414295B (en) 2020-03-09 2020-03-09 Statistical method, device, equipment and medium for CPU occupancy rate

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010157228.7A CN111414295B (en) 2020-03-09 2020-03-09 Statistical method, device, equipment and medium for CPU occupancy rate

Publications (2)

Publication Number Publication Date
CN111414295A true CN111414295A (en) 2020-07-14
CN111414295B CN111414295B (en) 2023-06-30

Family

ID=71490959

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010157228.7A Active CN111414295B (en) 2020-03-09 2020-03-09 Statistical method, device, equipment and medium for CPU occupancy rate

Country Status (1)

Country Link
CN (1) CN111414295B (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112083931A (en) * 2020-09-15 2020-12-15 上海眼控科技股份有限公司 Program processing method, device and equipment
CN114201353A (en) * 2020-09-02 2022-03-18 北京字节跳动网络技术有限公司 Processing method, device and storage medium for program time-consuming data

Citations (14)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060212867A1 (en) * 2005-03-17 2006-09-21 Microsoft Corporation Determining an actual amount of time a processor consumes in executing a portion of code
CN101145125A (en) * 2006-09-11 2008-03-19 中兴通讯股份有限公司 Method for measuring task CPU occupancy rate in multitasking operation system
US20090265691A1 (en) * 2008-04-18 2009-10-22 International Business Machines Corporation Granular measurement of processor performance in executing sections of software code
US20120084787A1 (en) * 2010-10-05 2012-04-05 Hyun Ku Jeong Apparatus and method for controlling a resource utilization policy in a virtual environment
US20120179898A1 (en) * 2011-01-10 2012-07-12 Apple Inc. System and method for enforcing software security through cpu statistics gathered using hardware features
CN103455407A (en) * 2013-09-03 2013-12-18 小米科技有限责任公司 Mobile terminal CPU (Central Processing Unit) occupation rate monitoring method and device and mobile terminal
WO2015042964A1 (en) * 2013-09-30 2015-04-02 华为技术有限公司 Software test method and apparatus
WO2017016074A1 (en) * 2015-07-30 2017-02-02 中兴通讯股份有限公司 Cpu occupation rate detection method and apparatus
CN107368400A (en) * 2017-08-01 2017-11-21 广东欧珀移动通信有限公司 CPU monitoring methods, device, computer-readable recording medium and mobile terminal
CN108733561A (en) * 2018-04-25 2018-11-02 北京五八信息技术有限公司 Application program launching duration test method, device, electronic equipment and storage medium
CN109766267A (en) * 2018-12-15 2019-05-17 中国平安人寿保险股份有限公司 CPU high consumes Code location method, apparatus, electronic equipment and storage medium
US20190220326A1 (en) * 2018-01-17 2019-07-18 Salesforce.Com, Inc. Method and system for synchronizing and joining multiple calls to the same asynchronous code segment
CN110309036A (en) * 2018-03-27 2019-10-08 华为技术有限公司 A kind of CPU usage detection method and detection device
CN110580220A (en) * 2019-08-12 2019-12-17 百富计算机技术(深圳)有限公司 method for measuring execution time of code segment and terminal equipment

Patent Citations (15)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060212867A1 (en) * 2005-03-17 2006-09-21 Microsoft Corporation Determining an actual amount of time a processor consumes in executing a portion of code
CN101145125A (en) * 2006-09-11 2008-03-19 中兴通讯股份有限公司 Method for measuring task CPU occupancy rate in multitasking operation system
US20090265691A1 (en) * 2008-04-18 2009-10-22 International Business Machines Corporation Granular measurement of processor performance in executing sections of software code
US20120084787A1 (en) * 2010-10-05 2012-04-05 Hyun Ku Jeong Apparatus and method for controlling a resource utilization policy in a virtual environment
US20120179898A1 (en) * 2011-01-10 2012-07-12 Apple Inc. System and method for enforcing software security through cpu statistics gathered using hardware features
CN103455407A (en) * 2013-09-03 2013-12-18 小米科技有限责任公司 Mobile terminal CPU (Central Processing Unit) occupation rate monitoring method and device and mobile terminal
WO2015042964A1 (en) * 2013-09-30 2015-04-02 华为技术有限公司 Software test method and apparatus
WO2017016074A1 (en) * 2015-07-30 2017-02-02 中兴通讯股份有限公司 Cpu occupation rate detection method and apparatus
CN106708709A (en) * 2015-07-30 2017-05-24 中兴通讯股份有限公司 CPU occupancy rate detection method and device
CN107368400A (en) * 2017-08-01 2017-11-21 广东欧珀移动通信有限公司 CPU monitoring methods, device, computer-readable recording medium and mobile terminal
US20190220326A1 (en) * 2018-01-17 2019-07-18 Salesforce.Com, Inc. Method and system for synchronizing and joining multiple calls to the same asynchronous code segment
CN110309036A (en) * 2018-03-27 2019-10-08 华为技术有限公司 A kind of CPU usage detection method and detection device
CN108733561A (en) * 2018-04-25 2018-11-02 北京五八信息技术有限公司 Application program launching duration test method, device, electronic equipment and storage medium
CN109766267A (en) * 2018-12-15 2019-05-17 中国平安人寿保险股份有限公司 CPU high consumes Code location method, apparatus, electronic equipment and storage medium
CN110580220A (en) * 2019-08-12 2019-12-17 百富计算机技术(深圳)有限公司 method for measuring execution time of code segment and terminal equipment

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
刘燕生,张建伟,卓贤林: "空管***中监视CPU占用率的一种算法实现", 中国民航飞行学院学报, no. 03 *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114201353A (en) * 2020-09-02 2022-03-18 北京字节跳动网络技术有限公司 Processing method, device and storage medium for program time-consuming data
CN112083931A (en) * 2020-09-15 2020-12-15 上海眼控科技股份有限公司 Program processing method, device and equipment

Also Published As

Publication number Publication date
CN111414295B (en) 2023-06-30

Similar Documents

Publication Publication Date Title
CN107066365B (en) System abnormity monitoring method and device
CN112311611B (en) Data anomaly monitoring method and device and electronic equipment
CN111414295A (en) CPU occupancy rate statistical method, device, equipment and medium
CN113590429A (en) Server fault diagnosis method and device and electronic equipment
CN110647547A (en) Consumption delay monitoring method and device, electronic equipment and computer readable storage medium
CN109992408B (en) Resource allocation method, device, electronic equipment and storage medium
CN113127314A (en) Method and device for detecting program performance bottleneck and computer equipment
CN109284193B (en) Distributed data processing method based on multithreading and server
CN110347572B (en) Method, device, system, equipment and medium for outputting performance log
CN111399849B (en) Method and system for determining application program installation package
CN110134219B (en) Application program background power consumption processing method and device
CN116737084A (en) Queue statistics method and device, electronic equipment and storage medium
CN110855484A (en) Method, system, electronic device and storage medium for automatically detecting traffic change
CN110058811A (en) Information processing unit, data management system, method and computer-readable medium
CN115525426A (en) Processor utilization rate statistical method, system, equipment and medium
CN111708670B (en) Method and device for determining task time parameters in real-time operation system and vehicle
CN110569182B (en) Crash rate calculation method and device, computer equipment and storage medium
CN112822166A (en) Abnormal process detection method, device, equipment and medium
CN113810743A (en) Live broadcast room flow monitoring method and device
CN112685390A (en) Database instance management method and device and computing equipment
CN113568822B (en) Service resource monitoring method, device, computing equipment and storage medium
CN114676012A (en) Service link monitoring method and device, electronic equipment and storage medium
CN111752786A (en) Data storage method, data summarization method, equipment and medium in pressure test process
CN114385387B (en) Access information generation method and device and computer equipment
CN111737281B (en) Database query method, device, electronic equipment and readable storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant