Program running duration detection method and device, electronic equipment and readable medium
By selecting an appropriate time source based on the type of time source supported by the CPU for program runtime detection, the problem of inaccurate detection in existing technologies is solved, achieving higher detection accuracy and performance evaluation accuracy.
Patent Information
- Application Number
- CN202410932574.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-11
- Publication Date
- 2024-11-12
AI Technical Summary
Existing methods for detecting program runtime have limitations, leading to inaccurate detection.
Based on the time source type supported by the CPU, the first and second time sources are selected for priority detection. If the CPU does not support them, the default time source is used to ensure the accuracy of the detection indicators.
By prioritizing the use of better time sources for detection, the accuracy of program runtime is improved, the limitations of detection methods are reduced, and program performance is more accurately evaluated during development.
Smart Images

Figure CN118939501A_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of computer technology, and in particular to a method, device, electronic device and readable medium for detecting program execution time. Background Art
[0002] At present, in order to optimize a program, it is necessary to detect the performance indicators of the program so as to perform performance evaluation based on the performance indicators. As an important indicator for measuring program performance, the running time of the program often needs to be detected.
[0003] In the prior art, the default time source is usually directly used to detect the running time of the program. In this way, the detection method has great limitations and there is a problem that the detected running time is inaccurate. Summary of the invention
[0004] The embodiments of the present invention provide a method, device, electronic device and readable medium for detecting the running time of a program, which can reduce the limitations of the detection method and improve the accuracy of the duration detection.
[0005] In order to solve the above problem, an embodiment of the present invention discloses a method for detecting program running time, the method comprising:
[0006] In response to the duration detection instruction, if the CPU of the electronic device supports the first time source, detecting the running duration of the target program based on the first time source;
[0007] If the CPU does not support the first time source, and the CPU supports the second time source, detecting the running time of the target program based on the second time source;
[0008] If the CPU does not support the first time source and the second time source, the running time of the target program is detected based on the default time source; wherein the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source.
[0009] On the other hand, an embodiment of the present invention discloses a program running time detection device, the device comprising:
[0010] A first detection module, configured to respond to the duration detection instruction and, if the CPU of the electronic device supports a first time source, detect the running duration of the target program based on the first time source;
[0011] a second detection module, configured to detect the running time of the target program based on the second time source if the CPU does not support the first time source and the CPU supports the second time source;
[0012] The third detection module is used to detect the running time of the target program based on the default time source if the CPU does not support the first time source and the second time source; wherein the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source.
[0013] On the other hand, an embodiment of the present invention discloses an electronic device, comprising: a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the aforementioned method.
[0014] The embodiment of the present invention further discloses a machine-readable medium on which instructions are stored. When executed by one or more processors, the processors are enabled to execute the method described above.
[0015] The embodiment of the present invention includes the following advantages: In the program running time detection method provided by the embodiment of the present invention, in response to the time detection instruction, if the CPU of the electronic device supports the first time source, the running time of the target program is detected based on the first time source. If the CPU does not support the first time source, and the CPU supports the second time source, the running time of the target program is detected based on the second time source. If the CPU does not support the first time source and the second time source, the running time of the target program is detected based on the default time source. Compared with the method of directly using the default time source for detection, the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source. In the embodiment of the present invention, the analysis and judgment are performed by detecting whether the CPU supports the first time source and the second time source. When the CPU supports the first time source and the second time source, the time source with better detection indicators is preferentially used to detect the program running time. In this way, the limitations of the detection method can be reduced, and the detected program running time can be made more accurate, so as to more accurately evaluate the performance of the program in development. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative labor.
[0017] Figure 1 This is a flowchart of a method for detecting program running time provided by an embodiment of the present invention;
[0018] Figure 2It is a schematic diagram of a processing flow provided by an embodiment of the present invention;
[0019] Figure 3 is a block diagram of a program running time detection device provided by an embodiment of the present invention;
[0020] Figure 4 It is a structural schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0021] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0022] Reference Figure 1 , shows a flowchart of a method for detecting program running time provided by an embodiment of the present invention, the method for detecting program running time can be applied to electronic devices, such as Figure 1 As shown, the processing method may specifically include the following steps:
[0023] Step 101: In response to a duration detection instruction, if the CPU of the electronic device supports a first time source, the running duration of the target program is detected based on the first time source.
[0024] Step 102: If the CPU does not support the first time source, but supports the second time source, the running time of the target program is detected based on the second time source.
[0025] Step 103: If the CPU does not support the first time source and the second time source, the running time of the target program is detected based on the default time source; wherein the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source.
[0026] In an embodiment of the present invention, a duration detection instruction may be sent when it is necessary to detect the running duration of a target program. The target program may run locally on an electronic device. Specifically, the target program may be a program to be tested whose running duration needs to be detected, and the target program may be any program that needs to be detected in a code design and development scenario. The duration detection instruction may carry a program identifier. Accordingly, after receiving the duration detection instruction, the electronic device may extract the program identifier therein and determine the program indicated by the program identifier as the target program. A time source may refer to software or hardware for providing timing information. The duration detection index corresponding to the time source may refer to: an index for measuring the pros and cons of a duration detection method using the time source. The specified duration detection index may be one or more indicators in the duration detection index. The specified duration detection index may be set according to actual conditions. For example, the specified duration detection index may include duration detection overhead, duration detection accuracy, and the like.
[0027] Among them, the duration detection overhead corresponding to the time source may refer to: the detection overhead when the time source is used for duration detection. The detection accuracy corresponding to the time source may refer to: the detection accuracy when the time source is used for duration detection. Take the first time source and the second time source as time sources with duration detection overheads better than the default time source as an example, that is, the duration detection overheads corresponding to the first time source and the second time source are less than the duration detection overhead corresponding to the default time source. The first time source and the second time source may be hardware time sources, that is, the specific values of the first time source and the second time source may be directly read from the hardware. Specifically, due to the inherent characteristics of the first time source and the second time source, the specific values of the first time source and the second time source may be directly read from the registers corresponding to the first time source and the second time source by direct register access, and the reading speed is faster. In this way, since there is no need to perform a system call, it is equivalent to bypassing the operating system kernel. Therefore, the duration detection overheads corresponding to the first time source and the second time source may be less than the duration detection overhead corresponding to the default time source.
[0028] In actual application scenarios, the CPUs equipped with different electronic devices may be different, and the time sources supported by different CPUs may be different. For example, some models of CPUs support the first time source, some models of CPUs do not support the first time source, some models of CPUs support the second time source, and some models of CPUs do not support the second time source. In an embodiment of the present invention, the time sources supported by various models of CPUs can be collected in advance, and then a time source whose specified duration detection index is better than the specified duration detection index corresponding to the default time source is selected from these time sources, for example, a time source whose duration detection overhead is less than the duration detection overhead corresponding to the default time source is selected. Then, the first time source and the second time source are determined based on the selected time source. Exemplarily, the time source with the highest accuracy can be selected as the first time source, and then the time source with the highest accuracy can be selected from the remaining time sources as the second time source. Alternatively, the time sources can be sorted in order from small to large according to the duration detection overhead, and the two front time sources can be selected according to the sorting result as the first time source and the second time source respectively. Alternatively, the time source with the smallest duration detection overhead can be directly selected as the first time source, and the remaining other time sources can be determined as the second time source. Exemplarily, the duration detection overhead of the time source can be measured using the detection duration. Based on a preset duration detection code, the duration required for duration detection of different time sources can be predetermined as the duration detection overhead corresponding to the different time sources. The longer the required duration, the greater the duration detection overhead.
[0029] Accordingly, in the case where there are multiple second time sources, if the CPU of the electronic device supports at least one of the multiple second time sources, then one of the at least one second time source supported by the CPU is selected to perform duration detection. Alternatively, a time source with the highest detection accuracy is selected from the at least one second time source supported by the CPU to detect the running duration of the target program, which is not limited in the embodiment of the present invention. Among them, the first time source, the second time source, and the default time source are different time sources.
[0030] Correspondingly, in an embodiment of the present invention, when it is necessary to detect the running time, first detect whether the CPU in the electronic device supports the first time source and the second time source. If the CPU of the electronic device supports the first time source, the first time source can be directly used to detect the running time of the target program. If the CPU of the electronic device does not support the first time source, but supports the second time source, the second time source can be used to detect the running time of the target program. If both the first time source and the second time source are not supported, the default time source is used to detect the running time of the target program. In an embodiment of the present invention, the detection index corresponding to the first time source is better than the detection index corresponding to the second time source. Since the detection index corresponding to the first time source is better than the detection index corresponding to the second time source, the detection index corresponding to the second time source is better than the detection index corresponding to the default time source. In this way, it can be ensured as much as possible that the time source with better detection index is given priority, that is, the time source with better detection effect is given priority.
[0031] In summary, in the program running time detection method provided by the embodiment of the present invention, in response to the time detection instruction, if the CPU of the electronic device supports the first time source, the running time of the target program is detected based on the first time source. If the CPU does not support the first time source, and the CPU supports the second time source, the running time of the target program is detected based on the second time source. If the CPU does not support the first time source and the second time source, the running time of the target program is detected based on the default time source. Compared with the method of directly using the default time source for detection, the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source. In the embodiment of the present invention, the analysis and judgment are performed by detecting whether the CPU supports the first time source and the second time source. When the CPU supports the first time source and the second time source, the time source with better detection indicators is preferentially used to detect the program running time. In this way, the limitations of the detection method can be reduced, and the detected program running time can be made more accurate, so as to more accurately evaluate the performance of the program in development.
[0032] Optionally, in an embodiment of the present invention, a first preset variable and a second preset variable may be pre-configured in the electronic device, wherein the first preset variable is used to indicate whether the CPU supports the first time source, and the second preset variable is used to indicate whether the CPU supports the second time source.
[0033] The first preset variable and the second preset variable may be variables newly added to the electronic device. The specific representation forms of the first preset variable and the second preset variable may be set according to actual needs, and the embodiment of the present invention is not limited to this. Further, the first preset variable and the second preset variable may be variables pre-configured at the operating system level. Exemplarily, the first preset variable and the second preset variable may be environment variables for storing configuration information. The configuration file of the operating system may be opened first, and then the first preset variable and the second preset variable may be added to the configuration file.
[0034] For example, the first preset variable and the second preset variable can be used as command parameters, respectively, and the first preset variable and the second preset variable can be added to the configuration file using the exportMyNewVariable command. It is possible to pre-detect whether the CPU of the current electronic device supports the first time source, and set the specific value of the first preset variable as an environment variable for storing configuration information according to the detection result. Also, pre-detect whether the CPU of the current electronic device supports the second time source, and set the specific value of the second preset variable as an environment variable for storing configuration information according to the detection result. Accordingly, each time a runtime detection is required, it is only necessary to refer to the specific values of the first preset variable and the second preset variable to know whether the current CPU supports the first time source and whether it supports the second time source. In this way, the efficiency of judging whether the CPU supports the first time source and whether it supports the second time source can be improved to a certain extent.
[0035] Optionally, in an embodiment of the present invention, the first time source may be an invariant time stamp counter (invariant TSC). The invariant TSC runs at a constant rate and is not affected by the voltage state (P-state), sleep state (C-state) and temperature state (T-state) of the CPU. That is, no matter how the clock frequency of the CPU changes, or whether the CPU enters a low power state, the invariant TSC increases at the same rate, so the invariant TSC can provide a stable and accurate time reference. And it can be directly accessed through a first preset CPU command (for example, rdtsc instruction), with very low access latency. That is, when obtaining the clock cycle value of the invariant TSC statistics, the first preset CPU command can be used to bypass the kernel of the operating system and directly read from the corresponding register in the hardware. Therefore, in an embodiment of the present invention, the invariant TSC is used as the first time source, and in the processing process, the first time source is used as much as possible, which can improve the detection speed while ensuring accuracy, and to a certain extent can ensure the accuracy of the detected duration and reduce the time overhead of the detection.
[0036] The value of the first preset variable can be preset in the following way:
[0037] Step S21: Detect whether there is a characteristic flag bit corresponding to the unchanged timestamp counter in the flag bit of the information file of the CPU.
[0038] Step S22: If it exists, setting the first preset variable to the first specified value; the first specified value is used to indicate that the CPU supports the first time source;
[0039] Step S23: If it does not exist, setting the first preset variable to the second specified value; the second specified value is used to indicate that the CPU does not support the first time source.
[0040] Specifically, the feature flag may be a feature flag for characterizing support for invariant TSC. The feature flag may include: nonstop_tsc and constant_tsc. Among them, nonstop_tsc is used to characterize that the CPU has the characteristic that even if it enters C-state, the TSC of the CPU will continue to count at a constant rate. Constant_tsc is used to characterize that the CPU has the characteristic that the TSC runs at a fixed frequency and the CPU supports the constant rate TSC. Constant_tsc is specifically used to characterize that the frequency of the TSC used between different cores of the same CPU, or between different cores of different CPUs, is fixed and synchronized. Accordingly, when the CPU supports both constant_tsc and nonstop_tsc features, it can be considered that the CPU supports the invariant TSC feature, that is, the TSC in the CPU increases at a constant rate when working, regardless of the CPU frequency, and the TSC in the CPU is an invariant TSC. In an embodiment of the present invention, the frequency of the time source may refer to the counting rate of the time source, that is, the number of counts increased by the time source per unit time.
[0041] The information file of the CPU may be a file for storing detailed information of the supported operating system. The information file of the CPU may provide detailed information about the CPU in the form of an operating system kernel interface. Exemplarily, the information file of the CPU may be: / proc / cpuinfo. Accordingly, it is possible to search for the presence of the nonstop_tsc field and the constant_tsc field in the flag field (e.g., the flag field) of / proc / cpuinfo. In the case where the nonstop_tsc field and the constant_tsc field exist in the flag field, that is, the current system supports nonstop_tsc and constant_tsc, the value of the first preset variable is set to the first specified value. Otherwise, the value of the first preset variable may be set to the second specified value. The specific values of the first specified value and the second specified value may be set as required, and the embodiment of the present invention does not limit this. Exemplarily, 1 may be used as the first specified value and 0 may be used as the second specified value. Alternatively, true may be used as the first specified value and false may be used as the second specified value. It should be noted that the value of the first preset variable represents the hardware characteristics of the CPU, that is, the value of the first preset variable is set according to whether the CPU actually supports invariant TSC. Therefore, the value of the first preset variable is the first specified value (indicating that the CPU supports invariant TSC) or the second specified value (indicating that the CPU does not support invariant TSC). Therefore, in an embodiment of the present invention, the value of the first preset variable can be initialized to the first specified value or the second specified value in advance. In this way, in the link of presetting the first preset variable, if the value to be set is the initialized value, the numerical setting operation can be omitted, thereby improving the setting efficiency to a certain extent. Exemplarily, it is assumed that the value of the first preset variable is initialized to the second specified value. In this way, when the nonstop_tsc field and the constant_tsc field exist in the flag field, a modification operation can be performed on the first preset variable to modify the value of the first preset variable to the first specified value. In the case where the nonstop_tsc field and the constant_tsc field do not exist in the flag field, the value of the first preset variable can be set to the second specified value without performing an operation, thereby improving the setting efficiency to a certain extent.
[0042] In the embodiment of the present invention, it is pre-determined whether there is a characteristic flag bit corresponding to the invariant timestamp counter in the flag bit of the CPU information file. The value of the first preset variable is pre-set based on the judgment result, so that the value of the first preset variable can accurately reflect whether the CPU supports the first time source, so that it is possible to directly know whether the CPU supports the first time source based on the value of the first preset variable, thereby improving the subsequent processing efficiency to a certain extent.
[0043] Optionally, in an embodiment of the present invention, the second time source may be a constant frequency timer (stablecounter). The constant frequency timer is a hardware timer that is independent of the clock frequency of the processor core. The constant frequency timer runs at a fixed frequency and automatically increases by 1 with each counting clock cycle, and is not affected by the clock frequency of the processor core. The duration detection overhead of the invariant TSC may be lower than the duration detection overhead of the constant frequency timer, and the duration detection accuracy of the invariant TSC may be higher than the duration detection accuracy of the constant frequency timer. When using a constant frequency timer, it may be directly accessed through a second preset CPU command (e.g., rdtime.d instruction), that is, when obtaining the clock cycle value counted by the constant frequency timer, the second preset CPU command may be used to read directly from the corresponding register in the hardware, so that, since no system call is required, it is equivalent to bypassing the operating system kernel. Therefore, in an embodiment of the present invention, the constant frequency timer is used as the second time source, and in the processing process, the second time source is used preferentially compared to the default time source, so that the detection speed can be improved under the premise of ensuring accuracy, and the accuracy of the detected duration can be ensured to a certain extent and the time overhead of the detection can be reduced. The value of the second preset variable may be preset in the following manner:
[0044] Step S31, detecting whether the CPU has a dedicated register corresponding to the constant frequency timer.
[0045] Step S32: If so, setting the second preset variable to the third specified value; the third specified value is used to indicate that the CPU supports the second time source.
[0046] Step S33: If it does not exist, setting the second preset variable to the fourth specified value; the fourth specified value is used to indicate that the CPU does not support the second time source.
[0047] Specifically, the dedicated register corresponding to the constant frequency timer can be used to record the number of clock cycles accumulated by the constant frequency timer. Exemplarily, the dedicated register corresponding to the constant frequency timer can be the cntvct_el0 register, wherein cntvct_el0 can be used to map the value of the constant frequency timer. Accordingly, the technical document corresponding to the CPU can be searched. If the cntvct_el0 register is defined in the technical document, the value of the second preset variable is set to the third specified value, and it is determined that there is a dedicated register corresponding to the constant frequency timer, and it is determined that the CPU supports the second time source. Otherwise, the value of the second preset variable is set to the fourth specified value, and it is determined that it does not exist, and the CPU does not support the second time source. Alternatively, based on the system call for reading the register value, the cntvct_el0 register is read. If the system call is executed successfully and the illegal instruction exception is not triggered, the value of the second preset variable is set to the third specified value, and it is determined that there is a dedicated register corresponding to the constant frequency timer, and it is determined that the CPU supports the second time source. Otherwise, the value of the second preset variable is set to the fourth specified value, and it is determined that it does not exist, and the CPU does not support the second time source.
[0048] Further, if the dedicated register exists, that is, a constant frequency timer currently exists, the value of the second preset variable is set to the third specified value. Otherwise, the value of the second preset variable can be set to the fourth specified value. The specific values of the third specified value and the fourth specified value can be set according to the requirements, and the embodiment of the present invention does not limit this. Exemplarily, 1 can be used as the third specified value and 0 can be used as the fourth specified value. Or, true can be used as the third specified value and false can be used as the fourth specified value. It should be noted that the value of the second preset variable represents the hardware characteristics of the CPU, that is, the value of the second preset variable is set according to whether the CPU actually supports the constant frequency timer. Therefore, the value of the second preset variable is the third specified value (indicating that the CPU supports the constant frequency timer) or the fourth specified value (indicating that the CPU does not support the constant frequency timer). Therefore, in the embodiment of the present invention, the value of the second preset variable can be initialized to the third specified value or the fourth specified value in advance. In this way, in the link of presetting the second preset variable, if the value to be set is the initialized value, the numerical setting operation can be omitted, and the setting efficiency can be improved to a certain extent. Exemplarily, it is assumed that the value of the second preset variable is initialized to the fourth specified value. In this way, if the special register exists, the modification operation can be performed on the second preset variable to modify the value of the second preset variable to the third specified value. In the case where the special register does not exist in the flag field, the value of the second preset variable can be set to the fourth specified value without performing an operation, thereby improving the setting efficiency to a certain extent.
[0049] It should be noted that the above steps S31 to S33 can be performed simultaneously with the above steps S21 to S23, or they can be performed one after another, and the embodiment of the present invention does not limit this. After the value of the second preset variable is set based on the above steps S31 to S33, each subsequent response to the duration detection operation can be directly based on the value of the second preset variable to determine whether the CPU supports the second time source. After the value of the first preset variable is set based on the above steps S21 to S23, each subsequent response to the duration detection operation can be directly based on the value of the first preset variable to determine whether the CPU supports the first time source. In this way, the response speed can be ensured and the duration detection efficiency can be improved.
[0050] In the embodiment of the present invention, it is determined in advance whether the CPU has a dedicated register corresponding to the constant frequency timer. The value of the second preset variable is set in advance based on the determination result, so that the value of the second preset variable can accurately reflect whether the CPU supports the second time source, so that it is possible to directly determine whether the CPU supports the second time source based on the value of the second preset variable, thereby improving the subsequent processing efficiency to a certain extent.
[0051] In an embodiment of the present invention, the default time source may be a monotonic clock. A monotonic clock is a clock source that is not dependent on the system time setting or adjustment. Using a monotonic clock can be accurate to nanoseconds. However, when using a monotonic clock, a system call (e.g., clock_gettime()) is required to read the value of the monotonic clock. In this way, the duration required for reading is long, and the reading overhead is large, resulting in a large duration detection overhead and a slow duration detection speed, which in turn results in the final detected runtime being inaccurate, resulting in damage to the performance of the target program code. Since the values of the invariantTSC and the constant frequency timer can be read directly from the hardware, compared to the monotonic clock method, the duration required for reading is shorter and the reading overhead is smaller, which makes the duration detection overhead smaller and the duration detection speed faster, thereby obtaining a more accurate runtime.
[0052] Optionally, the embodiment of the present invention may further include the following steps:
[0053] Step S41: When the first preset variable is a first specified value, determine whether the CPU supports the first time source.
[0054] Step S42: When the first preset variable is a second specified value, determine that the CPU does not support the first time source; and when the second preset variable is a third specified value, determine that the CPU supports the second time source.
[0055] Step S43: When the second preset variable is a fourth specified value, determine that the CPU does not support the second time source.
[0056] In an embodiment of the present invention, the specific value of the first preset variable can be read from the configuration file of the operating system. For example, the variable value of the variable name of the first preset variable in the configuration file can be directly read to obtain the first variable value. Further, in an embodiment of the present invention, the value of the first preset variable can be a first specified value or a second specified value. Among them, the first specified value is a value indicating that the CPU supports the first time source, and the second specified value is a value indicating that the CPU does not support the first time source. Accordingly, when the value of the first variable is the first specified value, it can be determined that the CPU supports the first time source. When the value of the first variable is the second specified value, it is determined that the CPU does not support the first time source.
[0057] Further, in the case where the first variable value is the second specified value, the second variable value can be read again. Specifically, the specific value of the second preset variable can be read from the configuration file of the operating system. For example, the variable value of the variable name of the second preset variable in the configuration file can be directly read to obtain the second variable value. Further, in an embodiment of the present invention, the value of the second preset variable can be a third specified value or a fourth specified value. Among them, the third specified value is a value indicating that the CPU supports the second time source, and the fourth specified value is a value indicating that the CPU does not support the second time source. The specific values of the third specified value and the fourth specified value can be set according to requirements, and the embodiment of the present invention does not limit this. Exemplarily, the third specified value can be the same as the above-mentioned first specified value, and the fourth specified value can be the same as the above-mentioned second specified value.
[0058] Further, when the second variable value is the third specified value, it can be determined that the CPU supports the second time source. When the second variable value is the fourth specified value, it is determined that the CPU does not support the second time source. Since the second variable value is read when the CPU does not support the first time source, it can be determined that the CPU does not support the first time source, but supports the second time source when the second variable value is the third specified value. When the second variable value is the fourth specified value, it is determined that the CPU does not support the first time source and does not support the second time source.
[0059] In the embodiment of the present invention, the value of the first preset variable is first used as the first variable value, and whether the CPU supports the first time source is first determined based on the first variable value. When the CPU does not support the first time source, that is, when the first variable value is the second specified value, the operation of determining whether the CPU supports the second time source based on the second variable value is further performed. In this way, it is possible to avoid executing unnecessary operations in each response to the duration detection instruction to a certain extent, thereby saving processing resources and improving processing efficiency.
[0060] Optionally, in the embodiment of the present invention, the step of detecting the running time of the target program based on the first time source may specifically include:
[0061] Step 1011, obtaining the frequency of the first time source as a first frequency; the first frequency is read from a kernel file of the electronic device based on a preset read command, and represents the number of clock cycles counted by the first time source in a unit time.
[0062] Step 1012: Before and after the target program runs, read the clock cycle values recorded by the first time source respectively to obtain a first clock cycle value and a second clock cycle value; the first clock cycle value represents the number of clock cycles counted by the first time source before the target program runs, and the second clock cycle value represents the number of clock cycles counted by the first time source after the target program runs.
[0063] Step 1013: Calculate the running time of the target program based on the first clock cycle value, the second clock cycle value and the first frequency.
[0064] The first frequency may represent the actual counting frequency of the first time source, and the unit time may be nanoseconds or seconds, which is not limited in the embodiment of the present invention. The first frequency may represent the number of clock cycles counted by the first time source in the unit time. The preset read command may be an instruction for reading the frequency of the constant timestamp counter. Specifically, the preset read command may be a parameter value of a parameter representing the frequency of the constant timestamp counter in a kernel file read by a gdb tool. Accordingly, the kernel file identifier and the parameter name of the parameter representing the frequency of the constant timestamp counter may be used as command parameters of the preset read command, and the preset read command may be executed. Accordingly, the execution result of the preset read command may be obtained to obtain the parameter value of the parameter representing the frequency of the constant timestamp counter. The execution result is the value read by the preset read command. In the embodiment of the present invention, the first frequency can be obtained quickly and conveniently based on the preset read command. In this way, the efficiency of using the first time source to calculate the running time can be ensured. The kernel file may be a file provided by the kernel of the operating system. For example, the kernel file identifier may be: / proc / kcore, and the parameter name representing the frequency of the constant timestamp counter may be: tsc_khz.
[0065] Accordingly, the preset read command can be expressed as:
[0066] gdb / dev / null / proc / kcore-ex'x / uw 0x'$(grep'\<tsc_khz\> ' / proc / kallsyms|cut-d”-f1)-batch 2> / dev / null|tail-n 1|cut-f2
[0067] Furthermore, since the unit used by the tsc_khz parameter is Khz, therefore, in an embodiment of the present invention, after obtaining the execution result, the execution result can be multiplied by 1000 to obtain a first frequency in hz. It should be noted that in an embodiment of the present invention, the frequency of the constant timestamp counter can also be read from the kernel file of the electronic device in advance based on a preset read command, and then the read frequency can be converted into a frequency in hz as the first frequency and stored in a designated location, which is used to calculate the running time of the target program in subsequent steps. Accordingly, when the acquisition operation of step 1011 is subsequently executed, the first frequency in hz can be obtained by directly reading from the designated location. In this way, the processing efficiency can be improved.
[0068] Furthermore, before starting to run the target program, that is, when the next instruction to be executed is the first instruction in the target program, a first preset CPU command (e.g., Read Time Stamp Counter, rdtsc command) can be executed to read the clock cycle value recorded by the constant timestamp counter as the first clock cycle value. After the target program is finished running, that is, when the currently executed instruction is the last instruction in the target program, the first preset CPU command is executed again to read the clock cycle value recorded by the constant timestamp counter as the second clock cycle value. Then, the difference between the second clock cycle value and the first clock cycle value is calculated to obtain a first difference, wherein the first difference can represent the number of clock cycles consumed by the target program this time.
[0069] Exemplarily, start_count represents the first clock cycle value, end_start represents the second clock cycle value, and time_count represents the first difference value. Accordingly, in order to obtain the first difference value, start_count, end_count, hi, lo, and time_count variables may be defined first. Among them, these variables may be variables of uint64_t type, and hi and lo are variables used to store the execution result of the rdtsc command.
[0070] Accordingly, before starting to run the target program, the first command line can be executed to store the current accumulated clock cycle number of the first time source into the hi and lo variables. The first command line can be expressed as: __asm____volatile__("rdtsc":"=a"(lo),"=d"(hi));
[0071] Next, execute the second command line to assign the values of hi and lo variables to start_count. The second command line can be expressed as: start_count = ((uint64_t)hi<<32)|lo;
[0072] After the target program is finished running, the third command line is executed to store the current accumulated clock cycle number of the first time source into the hi and lo variables. The third command line can be expressed as: __asm____volatile__("rdtsc":"=a"(lo),"=d"(hi));
[0073] Next, the fourth command line is executed to assign the values of the hi and lo variables to end_start. The fourth command line can be expressed as: end_count = ((uint64_t)hi<<32)|lo;
[0074] The fifth command line can be executed to calculate the first difference. The fifth command line can be expressed as: time_count=end_count-start_count; which represents the number of clock cycles consumed by the target program in this operation.
[0075] Further, the first difference / first frequency can be calculated to obtain the duration consumed by the target program for this operation, that is, the duration of the target program's operation. Exemplarily, tsc_freq represents the first frequency, and time represents the duration consumed by the target program for this operation. In order to achieve the duration consumed by the target program for this operation. The time and tsc_freq variables can be defined first. Among them, these variables can be of double type. Accordingly, the sixth command line can be executed to determine the number of nanoseconds represented by the integer part of time_count divided by tsc_freq. Among them, the sixth command line can be expressed as: quot_ns=(double)time_count / tsc_freq*1000000000; then, the seventh command line is executed to determine the number of nanoseconds represented by the remainder part of time_count divided by tsc_freq. The seventh command line can be expressed as: remain_ns = (double) time_count% tsc_freq / tsc_freq * 1000000000; the eighth command line is executed to add the nanoseconds represented by the integer part and the nanoseconds represented by the remainder part to obtain the total execution time time of the target program. The eighth command line can be expressed as: time = quot_ns + remain_ns.
[0076] In one implementation, when using an invariant timestamp counter, the CPU frequency is often used by default to calculate the running time. However, the TSC frequency is not necessarily equal to the current running frequency of the CPU, and there is no direct relationship between the two. Therefore, the method of using the CPU frequency will reduce the accuracy of the calculated duration. In an embodiment of the present invention, by first obtaining the actual frequency of the invariant timestamp counter, that is, obtaining the first frequency, the running time is not calculated using the current running frequency of the CPU, and the running time of the target program is calculated based on the first frequency, the accuracy of the calculation can be improved.
[0077] Optionally, in the embodiment of the present invention, the step of detecting the running time of the target program based on the second time source may specifically include:
[0078] Step 1021, obtain the frequency of the second time source as the second frequency; the second frequency is determined based on the crystal frequency, multiplication factor and division factor corresponding to the clock used by the constant frequency timer, and represents the number of clock cycles counted by the second time source in unit time.
[0079] Step 1022: Before and after the target program is run, read the clock cycle value of the second time source respectively to obtain a third clock cycle value and a fourth clock cycle value; the third clock cycle value represents the number of clock cycles counted by the second time source before the target program is run, and the fourth clock cycle value represents the number of clock cycles counted by the second time source after the target program is run.
[0080] Step 1023: Calculate the running time of the target program based on the third clock cycle value, the fourth clock cycle value and the second frequency.
[0081] The second frequency may represent the actual counting frequency of the second time source, and the unit time may be nanoseconds or seconds, which is not limited in the embodiment of the present invention. The second frequency may represent the number of clock cycles counted by the second time source in the unit time. The frequency of the constant frequency timer may be calculated in advance according to the crystal oscillator frequency, multiplication factor, and division factor corresponding to the clock used by the constant frequency timer. Accordingly, when executing step 1021, the frequency of the constant frequency timer calculated in advance may be directly read as the second frequency.
[0082] Specifically, when calculating the frequency of the constant frequency timer, the third preset command can be executed to obtain the crystal frequency, multiplication factor and division factor corresponding to the clock used by the constant frequency timer. Among them, the division factor can also be called the division coefficient, and the third preset command can be the CPUCFG command. Specifically, the crystal frequency can be stored in the CPUCGF.0x4 register, the multiplication factor can be located in the lower 16 bits of the CPUCGF.0x5 register, and the division factor can be located in the upper 16 bits of the CPUCGF.0x5 register. Accordingly, the third preset command can be executed to read the crystal frequency from the register of configuration word No. 4, read the multiplication factor from the lower 16 bits of the register of configuration word No. 5, and read the division factor from the upper 16 bits of the register of configuration word No. 5. Then calculate the crystal frequency * multiplication factor / division factor to obtain the frequency used by the constant frequency timer.
[0083] Exemplarily, CCFreq represents the crystal frequency, CFD represents the multiplication factor, and CFM represents the division factor. Accordingly, in order to calculate the second frequency. You can first define the CCFreq, CCFMFD, CFD, and CFM variables. Among them, these variables can be variables of uint64_t type, and these variables can be used to store the crystal frequency, the content in the register of configuration word No. 5, the multiplication factor, and the division factor, respectively. Accordingly, the following command line can be executed to read the value of the register of configuration word No. 4 into the CCFreq variable. Among them, the command line can be expressed as: __asm____volatile__("CPUCFG%0,%1":"=r"(CCFreq),"r"(4));
[0084] Next, execute the following command line to read the value of the register of configuration word 5 into the CCFMFD variable. The command line can be expressed as: __asm____volatile__("CPUCFG%0,%1":"=r"(CCFMFD),"r"(5));
[0085] Next, execute the following two command lines to extract the upper 16 bits of CCFMFD through bit operation and assign them to CFD, and extract the lower 16 bits of CCFMFD through bit operation and assign them to CFM: CFD = 0xFFFF0000 & CCFMFD; CFM = 0x0000FFFF & CCFMFD; Finally, the following command line can be executed to calculate the frequency of the constant frequency timer: doublestable_counter_freq = CCFreq*CFM / CFD.
[0086] Further, before starting to run the target program, that is, when the next instruction to be executed is the first instruction in the target program, a second preset CPU command (e.g., rdtime.d command) can be executed to read the clock cycle value recorded by the constant frequency timer as the third clock cycle value. After the target program is finished running, that is, when the instruction currently executed is the last instruction in the target program, the second preset CPU command is executed again to read the clock cycle value recorded by the constant frequency timer as the fourth clock cycle value. Then, the difference between the fourth clock cycle value and the third clock cycle value is calculated to obtain a second difference, wherein the second difference can represent the number of clock cycles consumed by the target program this time. Wherein, the clock cycle value used to calculate the first difference is read from the register corresponding to the first time source, and the clock cycle value used to calculate the second difference is read from the register corresponding to the second time source.
[0087] Exemplarily, the third clock cycle value is represented by start_stable_counter, the fourth clock cycle value is represented by end_stable_counter, and the second difference value is represented by time_count. Accordingly, in order to obtain the second difference value. The start_stable_counter, end_stable_counter and time_count variables can be defined first. Among them, these variables can be variables of type uint64_t. Accordingly, before starting to run the target program, the following command line can be executed to store the current accumulated number of clock cycles of the second time source into the start_stable_counter variable, so that the variable can be conveniently used to represent the third clock cycle value when the second difference value is calculated later. The command line can be expressed as __asm____volatile__("rdtime.d%0,%1":"=r"(start_stable_counter),"=r"(counter_id)); then, after the target program is finished running, the following command line is executed to store the current accumulated number of clock cycles of the second time source into the end_stable_counter variable, so that the variable can be conveniently used to represent the fourth clock cycle value when the second difference value is calculated later. The command line can be expressed as __asm____volatile__("rdtime.d%0,%1":"=r"(end_stable_counter),"=r"(counter_id)); the following command line can be executed to calculate the second difference. The command line can be expressed as: time_count=end_stable_counter-start_stable_counter, which indicates the number of clock cycles consumed by the target program in this run.
[0088] Furthermore, the second difference / the second frequency may be calculated to obtain the time duration consumed by the current running of the target program, that is, the running time duration of the target program.
[0089] Exemplarily, stable_counter_freq represents the second frequency, and time_count represents the duration of the current run of the target program. The following command line can be executed to determine the number of nanoseconds represented by the integer part of time_count divided by stable_counter_freq. The command line can be expressed as: quot_ns = (double) time_count / stable_counter_freq * 1000000000; then, the following command line is executed to determine the number of nanoseconds represented by the remainder part of time_count divided by stable_counter_freq. Among them, the command line can be expressed as: remaining_ns = (double) time_count% stable_counter_freq / stable_counter_freq * 1000000000; execute the following command line to add the number of nanoseconds represented by the integer part and the number of nanoseconds represented by the remainder part to obtain the total execution time time of the target program. Among them, the command line can be expressed as: time_ns = quot_ns + remaining_ns.
[0090] In an embodiment of the present invention, the frequency of the constant frequency timer predetermined according to the crystal frequency, the multiplication factor and the division factor corresponding to the clock used by the constant frequency timer is obtained. Based on the second frequency and the clock cycle value obtained by the constant frequency timer, the running time of the target program is calculated. In this way, the accuracy of the frequency used can be ensured, and the accuracy of the calculation can be ensured to a certain extent. At the same time, the second frequency required for the calculation can be obtained by directly obtaining the predetermined frequency, which can improve the calculation efficiency to a certain extent.
[0091] Of course, when executing step 1021, an operation of determining the frequency of the constant frequency timer based on the crystal oscillator frequency, multiplication factor, and division factor corresponding to the clock used by the constant frequency timer may be performed to ensure the real-time performance of the acquired frequency. This embodiment of the present invention does not limit this.
[0092] Optionally, in an embodiment of the present invention, if the CPU does not support the first time source and the second time source, the step of detecting the running time of the target program based on the default time source may specifically include: reading the time value of the monotonic clock before and after the target program is run, respectively, to obtain the first time value and the second time value. Calculate the difference between the second time value and the first time value to obtain the running time of the target program.
[0093] Specifically, before starting to run the target program, the monotonic clock can be called based on a preset function (for example, clock_gettime()) to obtain the current time value recorded by the monotonic clock as the first time value. After the target program ends, the preset function is executed again to obtain the current time value recorded by the monotonic clock as the second time value. Then, the difference between the second time value and the first time value is calculated to obtain the running time of the target program. Exemplarily, two variables time_start and time_end of the timespec structure can be defined first. Before starting to run the target program, execute the following command line to store the current time value of the monotonic clock into time_start. The command line can be expressed as: clock_gettime(CLOCK_MONOTONIC,&time_start); after the target program ends, execute the following command line to store the current time value of the monotonic clock into time_end. The command line can be expressed as: clock_gettime(CLOCK_MONOTONIC,&time_end); then, time_start and time_end are converted to nanoseconds respectively through the following two command lines:
[0094] time_start_ns=time_start.tv_sec*1000000000+time_start.tv_nsec;
[0095] time_end_ns=time_end.tv_sec*1000000000+time_end.tv_nsec; Finally, execute the following command line to obtain the running time: time_ns=time_end_ns-time_start_ns.
[0096] The first preset variable is represented by tsc_invariant, and the second preset variable is represented by stable_freq. Figure 2 is a schematic diagram of a processing flow provided by an embodiment of the present invention, such as Figure 2As shown, tsc_invariant and stable_freq can be initialized to default values. Exemplarily, the default value can be a value indicating unsupported, for example, the default value can be false. Then determine whether the current system supports nonstop_tsc and constant_tsc. If so, set tsc_invariant to true. And, determine whether there is a constant frequency timer in the current system. If so, set stable_freq to true. Accordingly, when duration detection is required, determine whether the value of tsc_invariant is true. If so, use invariant TSC to calculate the running time. If not, determine whether the value of stable_freq is true. If so, use the constant frequency timer to calculate the running time. If not, use the monotonic clock to calculate the running time.
[0097] In the embodiment of the present invention, the optimal method is selected to calculate the time according to the specific situation of the system CPU support characteristics. When it is determined that the CPU does not support invariant TSC, it is first determined whether the constant frequency timer is supported, and if it is supported, the constant frequency timer is used preferentially. If neither is supported, the monotonic clock is used. In this way, it can be ensured as much as possible that the invariant TSC and the constant frequency timer are used preferentially, thereby improving the detection speed while ensuring the calculation accuracy.
[0098] Reference Figure 3 , shows a block diagram of a program running time detection device provided by an embodiment of the present invention, the device can be applied to electronic devices, such as Figure 3 As shown, the device may specifically include:
[0099] The first detection module 201 is used to respond to the duration detection instruction and detect the running duration of the target program based on the first time source if the CPU of the electronic device supports the first time source;
[0100] A second detection module 202, configured to detect the running time of the target program based on the second time source if the CPU does not support the first time source and the CPU supports the second time source;
[0101] The third detection module 203 is used to detect the running time of the target program based on the default time source if the CPU does not support the first time source and the second time source; wherein the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source.
[0102] Optionally, a first preset variable and a second preset variable are pre-configured in the electronic device; the first preset variable is used to indicate whether the CPU supports the first time source, and the second preset variable is used to indicate whether the CPU supports the second time source; the device further includes:
[0103] A first determining module, configured to determine that the CPU supports the first time source when the first preset variable is a first specified value;
[0104] a second determining module, configured to determine that the CPU does not support the first time source when the first preset variable is a second specified value, and to determine that the CPU supports the second time source when the second preset variable is a third specified value;
[0105] The third determining module is configured to determine that the CPU does not support the second time source when the second preset variable is a fourth specified value.
[0106] Optionally, the first time source is a constant timestamp counter; and the value of the first preset variable is preset by the following module:
[0107] A fourth detection module, used for detecting whether there is a characteristic flag bit corresponding to the unchanged timestamp counter in the flag bit of the information file of the CPU;
[0108] A first setting module, configured to set the first preset variable to the first specified value if it exists; the first specified value is used to indicate that the CPU supports the first time source;
[0109] The second setting module is used to set the first preset variable to the second specified value if it does not exist; the second specified value is used to indicate that the CPU does not support the first time source.
[0110] Optionally, the second time source is a constant frequency timer; and the value of the second preset variable is preset by the following module:
[0111] A fifth detection module, used for detecting whether the CPU has a dedicated register corresponding to the constant frequency timer;
[0112] A third setting module, configured to set the second preset variable to the third specified value if it exists; the third specified value is used to indicate that the CPU supports the second time source;
[0113] The fourth setting module is used to set the second preset variable to the fourth specified value if it does not exist; the fourth specified value is used to indicate that the CPU does not support the second time source.
[0114] Optionally, the first detection module 201 is specifically configured to:
[0115] Acquire the frequency of the first time source as a first frequency; the first frequency is read from a kernel file of the electronic device based on a preset read command, and represents the number of clock cycles counted by the first time source within a unit time;
[0116] Before and after the target program is run, the clock cycle values recorded by the first time source are read respectively to obtain a first clock cycle value and a second clock cycle value; the first clock cycle value represents the number of clock cycles counted by the first time source before the target program is run, and the second clock cycle value represents the number of clock cycles counted by the first time source after the target program is run;
[0117] The running time of the target program is calculated based on the first clock cycle value, the second clock cycle value and the first frequency.
[0118] Optionally, the second detection module 202 is specifically configured to:
[0119] Acquire the frequency of the second time source as the second frequency; the second frequency is determined based on the crystal frequency, multiplication factor and division factor corresponding to the clock used by the constant frequency timer, and represents the number of clock cycles counted by the second time source in unit time;
[0120] Before and after the target program is run, the clock cycle value of the second time source is read respectively to obtain a third clock cycle value and a fourth clock cycle value; the third clock cycle value represents the number of clock cycles counted by the second time source before the target program is run, and the fourth clock cycle value represents the number of clock cycles counted by the second time source after the target program is run;
[0121] The running time of the target program is calculated based on the third clock cycle value, the fourth clock cycle value and the second frequency.
[0122] In summary, the program running time detection device provided in the embodiment of the present invention responds to the time detection instruction, if the CPU of the electronic device supports the first time source, then based on the first time source, the running time of the target program is detected. If the CPU does not support the first time source, and the CPU supports the second time source, the running time of the target program is detected based on the second time source. If the CPU does not support the first time source and the second time source, the running time of the target program is detected based on the default time source. Compared with the method of directly using the default time source for detection, the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source. In the embodiment of the present invention, the analysis and judgment are performed by detecting whether the CPU supports the first time source and the second time source. When the CPU supports the first time source and the second time source, the time source with better detection indicators is preferentially used to detect the program running time. In this way, the limitations of the detection method can be reduced, and the detected program running time can be made more accurate, so as to more accurately evaluate the performance of the program in development.
[0123] Reference Figure 4 , is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. Figure 4 As shown, the electronic device includes: a processor, a memory, a communication interface and a communication bus, and the processor, the memory and the communication interface communicate with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the program running time detection method of the above-mentioned embodiment.
[0124] An embodiment of the present invention provides a machine-readable medium having instructions stored thereon, which, when executed by one or more processors, enables the processors to execute the program running time detection method of the aforementioned embodiment.
[0125] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0126] It will be appreciated by those skilled in the art that the embodiments of the present invention may be provided as methods, devices, or computer program products. Therefore, the embodiments of the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0127] It should be noted that all actions of acquiring signals, information or data in this application are carried out in compliance with the relevant data protection laws and policies of the country where they are located and with the authorization given by the owner of the corresponding device.
[0128] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0129] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing terminal device to operate in a predictable manner, so that the instructions stored in the computer readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0130] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0131] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0132] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0133] The above is a detailed introduction to a program running time detection method, a program running time detection device, an electronic device and one or more machine-readable media provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for general technical personnel in this field, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A method for detecting program running time, It is characterized in that The method comprises: In response to the duration detection instruction, if the CPU of the electronic device supports the first time source, detecting the running duration of the target program based on the first time source; If the CPU does not support the first time source, and the CPU supports the second time source, detecting the running time of the target program based on the second time source; If the CPU does not support the first time source and the second time source, the running time of the target program is detected based on the default time source; wherein the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source.
2. The method according to claim 1, It is characterized in that The electronic device is pre-configured with a first preset variable and a second preset variable; the first preset variable is used to indicate whether the CPU supports the first time source, and the second preset variable is used to indicate whether the CPU supports the second time source; The method further comprises: When the first preset variable is a first specified value, determining that the CPU supports the first time source; When the first preset variable is a second specified value, determining that the CPU does not support the first time source, and when the second preset variable is a third specified value, determining that the CPU supports the second time source; When the second preset variable is a fourth specified value, it is determined that the CPU does not support the second time source.
3. The method according to claim 2, It is characterized in that The first time source is an invariant timestamp counter; the value of the first preset variable is preset in the following manner: Detecting whether there is a characteristic flag bit corresponding to the unchanged timestamp counter in the flag bit of the information file of the CPU; If so, setting the first preset variable to the first specified value; The first specified value is used to indicate that the CPU supports the first time source; If not present, the first preset variable is set to the second specified value; the second specified value is used to indicate that the CPU does not support the first time source.
4. The method according to claim 2, It is characterized in that The second time source is a constant frequency timer; the value of the second preset variable is preset in the following manner: Detecting whether the CPU has a dedicated register corresponding to the constant frequency timer; If so, setting the second preset variable to the third specified value; the third specified value is used to indicate that the CPU supports the second time source; If not present, the second preset variable is set to the fourth specified value; the fourth specified value is used to indicate that the CPU does not support the second time source.
5. The method according to any one of claims 1 to 4, It is characterized in that The detecting the running time of the target program based on the first time source includes: Acquire the frequency of the first time source as a first frequency; the first frequency is read from a kernel file of the electronic device based on a preset read command, and represents the number of clock cycles counted by the first time source within a unit time; Before and after the target program is run, the clock cycle values recorded by the first time source are read respectively to obtain a first clock cycle value and a second clock cycle value; the first clock cycle value represents the number of clock cycles counted by the first time source before the target program is run, and the second clock cycle value represents the number of clock cycles counted by the first time source after the target program is run; The running time of the target program is calculated based on the first clock cycle value, the second clock cycle value and the first frequency.
6. The method according to any one of claims 1 to 4, It is characterized in that The detecting, based on the second time source, the running time of the target program includes: Acquire the frequency of the second time source as the second frequency; the second frequency is determined based on the crystal frequency, multiplication factor and division factor corresponding to the clock used by the second time source, and represents the number of clock cycles counted by the second time source in unit time; Before and after the target program is run, the clock cycle value of the second time source is read respectively to obtain a third clock cycle value and a fourth clock cycle value; the third clock cycle value represents the number of clock cycles counted by the second time source before the target program is run, and the fourth clock cycle value represents the number of clock cycles counted by the second time source after the target program is run; The running time of the target program is calculated based on the third clock cycle value, the fourth clock cycle value and the second frequency.
7. A program running time detection device, It is characterized in that The device comprises: A first detection module, configured to respond to the duration detection instruction and, if the CPU of the electronic device supports a first time source, detect the running duration of the target program based on the first time source; a second detection module, configured to detect the running time of the target program based on the second time source if the CPU does not support the first time source and the CPU supports the second time source; The third detection module is used to detect the running time of the target program based on the default time source if the CPU does not support the first time source and the second time source; wherein the detection indicators corresponding to the first time source and the second time source are better than the detection indicators corresponding to the default time source.
8. The device according to claim 7, It is characterized in that The electronic device is pre-configured with a first preset variable and a second preset variable; the first preset variable is used to indicate whether the CPU supports the first time source, and the second preset variable is used to indicate whether the CPU supports the second time source; The device also includes: A first determining module, configured to determine that the CPU supports the first time source when the first preset variable is a first specified value; a second determining module, configured to determine that the CPU does not support the first time source when the first preset variable is a second specified value, and to determine that the CPU supports the second time source when the second preset variable is a third specified value; The third determining module is configured to determine that the CPU does not support the second time source when the second preset variable is a fourth specified value.
9. The device according to claim 8, It is characterized in that The first time source is a constant timestamp counter; the value of the first preset variable is preset by the following module: A fourth detection module, used for detecting whether there is a characteristic flag bit corresponding to the unchanged timestamp counter in the flag bit of the information file of the CPU; A first setting module, configured to set the first preset variable to the first specified value if it exists; The first specified value is used to indicate that the CPU supports the first time source; a second setting module, configured to set the first preset variable to the second specified value if the first preset variable does not exist; The second specified value is used to indicate that the CPU does not support the first time source.
10. The device according to claim 8, It is characterized in that The second time source is a constant frequency timer; the value of the second preset variable is preset by the following module: A fifth detection module, used for detecting whether the CPU has a dedicated register corresponding to the constant frequency timer; A third setting module, configured to set the second preset variable to the third specified value if it exists; the third specified value is used to indicate that the CPU supports the second time source; The fourth setting module is used to set the second preset variable to the fourth specified value if it does not exist; the fourth specified value is used to indicate that the CPU does not support the second time source.
11. The device according to any one of claims 7 to 10, It is characterized in that The first detection module is specifically used to: Acquire the frequency of the first time source as a first frequency; the first frequency is read from a kernel file of the electronic device based on a preset read command, and represents the number of clock cycles counted by the first time source within a unit time; Before and after the target program is run, respectively read the clock cycle value recorded by the first time source to obtain a first clock cycle value and a second clock cycle value; The first clock cycle value represents the number of clock cycles counted by the first time source before the target program runs, and the second clock cycle value represents the number of clock cycles counted by the first time source after the target program runs; The running time of the target program is calculated based on the first clock cycle value, the second clock cycle value and the first frequency.
12. The device according to any one of claims 7 to 10, It is characterized in that The second detection module is specifically used for: Acquire the frequency of the second time source as the second frequency; the second frequency is determined based on the crystal frequency, multiplication factor and division factor corresponding to the clock used by the second time source, and represents the number of clock cycles counted by the second time source in unit time; Before and after the target program is run, respectively read the clock cycle value of the second time source to obtain a third clock cycle value and a fourth clock cycle value; The third clock cycle value represents the number of clock cycles counted by the second time source before the target program runs, and the fourth clock cycle value represents the number of clock cycles counted by the second time source after the target program runs; The running time of the target program is calculated based on the third clock cycle value, the fourth clock cycle value and the second frequency.
13. An electronic device, It is characterized in that include: A processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other via the communication bus; The memory is used to store executable instructions, and the executable instructions enable the processor to execute the method according to any one of claims 1 to 6.
14. One or more machine-readable media, It is characterized in that Instructions are stored thereon, which, when executed by one or more processors, cause the processors to perform the method according to any one of claims 1 to 6.
