A method and device for obtaining the consumption of code instructions in a hardware environment

By obtaining the difference in clock cycles of target instructions in the hardware environment, the problem of difficulty in accurately obtaining specific code instructions in the prior art is solved, and accurate instruction consumption data is provided for software and hardware optimization.

CN114201375BActive Publication Date: 2025-07-11BEIJING ESWIN COMPUTING TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202111501649.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-09
Publication Date
2025-07-11
Estimated Expiration
2041-12-09

AI Technical Summary

Technical Problem

Existing performance analysis tools are difficult to accurately obtain the corresponding instruction consumption of a certain line or multiple lines of specific code in the hardware environment, making it difficult to provide effective instruction consumption analysis data, affecting the design and optimization of software and hardware.

Method used

By running program instructions in a hardware environment, the preset instruction run order obtains the number of clock cycles before and after the target instruction, and calculates the consumption of the target instruction.

Benefits of technology

It realizes the specific consumption of accurately obtaining the corresponding instructions of a certain line or multiple lines of specific code in the hardware environment, and guides the design and optimization of software and hardware.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114201375B_ABST
    Figure CN114201375B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for obtaining the instruction consumption of code instructions in a hardware environment, which relates to the technical field of code debugging and analysis, and can obtain the specific instruction consumption when the instructions corresponding to a certain line or multiple lines of specific code of a program instruction are running in an actual hardware environment. The main technical solution of the present invention is as follows: Run the program instruction in the hardware environment, where the program instruction includes a plurality of instructions to be run written according to a preset instruction running order, and at least one of the plurality of instructions to be run is a target instruction; According to the preset instruction running order, before running the target instruction, obtain the cumulative number of clock cycles consumed by running other instructions to be run as the first number of clock cycles; According to the preset instruction running order, obtain the cumulative number of clock cycles consumed after running the target instruction is completed as the second number of clock cycles; Determine the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code debugging and analysis, and in particular, to a method and device for obtaining the consumption of code instructions in a hardware environment. Background Art

[0002] Each instruction executed by the CPU requires a certain number of clock cycles (Cycles) to complete. The clock cycle, also known as the oscillation period, is defined as the reciprocal of the clock frequency. The clock cycle is the most basic and smallest time unit in a computer. For an instruction executed by the CPU, the number of clock cycles required is the consumption of the instruction.

[0003] Currently, for some instructions, the number of clock cycles consumed by each instruction is fixed. However, for other instructions (for example, mostly instructions related to memory access), the number of clock cycles consumed by each instruction is not fixed, mainly because there is a certain range of variation according to the different program code contexts and the actual hardware environment, for example, floating within the range of 3 - 6 cycles.

[0004] However, existing performance analysis tools can only test the execution efficiency of the entire program code or a series of test codes. Each instruction corresponds to a certain line or multiple lines of specific code. Then, by averaging according to multiple instructions, the consumption of each instruction on average is obtained. However, for developers or users, the actual consumption of the instructions corresponding to a certain line or multiple lines of specific code is unknown, so it is difficult to obtain effective analysis data on instruction consumption, which is difficult to provide valuable instruction consumption analysis data for subsequent software and hardware designs. Summary of the Invention

[0005] In view of this, the present invention provides a method and device for obtaining the consumption of code instructions in a hardware environment, and the main purpose is to be able to obtain how much the specific instruction consumption is when the instructions corresponding to a certain line or multiple lines of specific code of the program instructions are running in the actual hardware environment, so as to be applicable to guiding the design and optimization of software and hardware.

[0006] In order to achieve the above object, the present invention mainly provides the following technical solutions:

[0007] A first aspect of the present application provides a method for obtaining the consumption of code instructions in a hardware environment, and the method includes:

[0008] Running program instructions in a hardware environment, where the program instructions include a plurality of instructions to be run written in a preset instruction running order, each instruction to be run includes one or more lines of code, and at least one target instruction is included in the plurality of instructions to be run;

[0009] Before running the target instruction according to the preset instruction execution sequence, obtain the cumulative number of clock cycles consumed by running other to-be-run instructions as the first number of clock cycles;

[0010] According to the preset instruction execution sequence, obtain the cumulative number of clock cycles consumed after running the target instruction is completed as the second number of clock cycles;

[0011] Determine the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles.

[0012] In some alternative embodiments of the first aspect of the present application, before running the program instructions in the hardware environment, the method further includes:

[0013] Obtain a program source code, where the program source code at least includes a target code segment for which the instruction consumption is to be obtained;

[0014] Add a corresponding preset function attribute to the target code segment, where the preset function attribute is a function attribute for obtaining instruction consumption;

[0015] Use a compiler to compile the program source code to obtain program instructions corresponding to an executable file, where the program instructions at least include target instructions corresponding to the target code segment.

[0016] In some alternative embodiments of the first aspect of the present application, the using a compiler to compile the program source code to obtain program instructions corresponding to an executable file includes:

[0017] Use the compiler to identify the preset function attribute in the program source code;

[0018] According to the identified preset function attribute, determine the target position of the target code segment in the program source code;

[0019] According to the target position, insert code corresponding to a first clock cycle reading instruction before the target code segment and insert code corresponding to a second clock cycle reading instruction after the target code segment to obtain a modified program source code;

[0020] Obtain program instructions corresponding to the executable file by compiling the modified program source code.

[0021] In some alternative embodiments of the first aspect of the present application, the before running the target instruction according to the preset instruction execution sequence, obtaining the cumulative number of clock cycles consumed by running other to-be-run instructions as the first number of clock cycles includes:

[0022] Run all other to-be-run instructions sorted before the target instruction according to the preset instruction running order;

[0023] Run the first read clock cycle instruction to read the currently recorded number of clock cycles from the preset register, where the preset register is used to store the value of the accumulated consumed clock cycles corresponding to the currently completed to-be-run instruction during the running of the program instruction;

[0024] Determine the number of consumed clock cycles corresponding before running the target instruction according to the currently recorded number of clock cycles as the first number of clock cycles.

[0025] In some modified implementation manners of the first aspect of the present application, the obtaining the accumulated consumed clock cycles corresponding after running the target instruction is completed as the second number of clock cycles according to the preset instruction running order includes:

[0026] Run the target instruction according to the preset instruction running order;

[0027] Run the second read clock cycle instruction to read the currently recorded number of clock cycles from the preset register;

[0028] Determine the number of consumed clock cycles corresponding to the completion of running the target instruction according to the currently recorded number of clock cycles as the second number of clock cycles.

[0029] In some modified implementation manners of the first aspect of the present application, the determining the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles includes:

[0030] Take the difference between the second number of clock cycles and the first number of clock cycles to obtain the third number of clock cycles;

[0031] Determine the instruction consumption corresponding to the target instruction according to the third number of clock cycles.

[0032] The second aspect of the present application provides a device for obtaining code instruction consumption in a hardware environment, and the device includes:

[0033] A running unit for running program instructions in a hardware environment, where the program instructions include a plurality of to-be-run instructions written according to a preset instruction running order, each to-be-run instruction includes one or more lines of code, and at least one target instruction is included in the plurality of to-be-run instructions;

[0034] A first obtaining unit for obtaining the accumulated consumed clock cycles corresponding to running other to-be-run instructions as the first number of clock cycles according to the preset instruction running order before running the target instruction;

[0035] The first acquisition unit is further configured to obtain, according to the preset instruction running sequence, the number of clock cycles cumulatively consumed after running the target instruction is completed, as the second number of clock cycles;

[0036] The determination unit is configured to determine the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles.

[0037] In some modified implementation manners of the second aspect of the present application, the device further includes:

[0038] A second acquisition unit, configured to obtain a program source code before running program instructions in the hardware environment, where the program source code includes at least one target code segment for which instruction consumption is to be acquired;

[0039] An adding unit, configured to add a corresponding preset function attribute to the target code segment, where the preset function attribute is a function attribute for acquiring instruction consumption;

[0040] A compiling unit, configured to compile the program source code by using a compiler to obtain program instructions corresponding to an executable file, where the program instructions include at least target instructions corresponding to the target code segment.

[0041] In some modified implementation manners of the second aspect of the present application, the compiling unit includes:

[0042] An identification module, configured to identify the preset function attribute in the program source code by using the compiler;

[0043] A judgment module, configured to judge the target position of the target code segment in the program source code according to the identified preset function attribute;

[0044] An insertion module, configured to insert code corresponding to a first read clock cycle instruction before the target code segment and insert code corresponding to a second read clock cycle instruction after the target code segment according to the target position, to obtain a modified program source code;

[0045] An acquisition module, configured to obtain program instructions corresponding to an executable file by compiling the modified program source code.

[0046] In some modified implementation manners of the second aspect of the present application, the first acquisition unit includes:

[0047] An operation module, configured to run all other to-be-run instructions sorted before the target instruction according to the preset instruction running sequence;

[0048] The running module is further configured to run the first clock cycle reading instruction, read the currently recorded number of clock cycles from the preset register, where the preset register is used to store the value of the accumulated clock cycles consumed for running the current instruction to be run during the process of running the program instruction;

[0049] The determining module is configured to determine, according to the currently recorded number of clock cycles, the number of clock cycles consumed before running the target instruction as the first number of clock cycles.

[0050] In some alternative implementation manners of the second aspect of the present application, the first obtaining unit includes:

[0051] The running module is configured to run the target instruction according to the preset instruction running order;

[0052] The running module is further configured to run the second clock cycle reading instruction, read the currently recorded number of clock cycles from the preset register;

[0053] The determining module is configured to determine, according to the currently recorded number of clock cycles, the number of clock cycles consumed after running the target instruction as the second number of clock cycles.

[0054] In some alternative implementation manners of the second aspect of the present application, the determining unit includes:

[0055] The obtaining module is configured to subtract the first number of clock cycles from the second number of clock cycles to obtain a third number of clock cycles;

[0056] The determining module is configured to determine the instruction consumption corresponding to the target instruction according to the third number of clock cycles.

[0057] The third aspect of the present application provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the method for obtaining the code instruction consumption in the hardware environment as described above is implemented.

[0058] The fourth aspect of the present application provides an electronic device, including: a memory, a processor, and a computer program stored on the memory and executable on the processor, and when the processor executes the computer program, the method for obtaining the code instruction consumption in the hardware environment as described above is implemented.

[0059] By means of the above technical solutions, the technical solutions provided by the present invention have at least the following advantages:

[0060] The present invention provides a method and device for obtaining the consumption of code instructions in a hardware environment. The present invention runs program instructions in a hardware environment. The program instructions include a plurality of instructions to be run written in a preset instruction running order. At least one of the instructions to be run is a target instruction, and the target instruction includes one or more lines of code. Then, according to the preset instruction running order, during the process of running the program instructions, the cumulative number of clock cycles consumed by running all other instructions to be run before running the target instruction can be obtained as the first number of clock cycles, and the cumulative number of clock cycles consumed after running the target instruction is completed can also be obtained as the second number of clock cycles. Subsequently, based on the difference between the second number of clock cycles and the first number of clock cycles, the number of clock cycles consumed by the target instruction, that is, the actual instruction consumption, can be known. Compared with the prior art, the technical problem of being difficult to know the actual consumption of instructions corresponding to a certain line or multiple lines of specific code is solved. The present invention can obtain the specific instruction consumption when the instructions corresponding to a certain line or multiple lines of specific code in the program instructions are run in the actual hardware environment, so as to be applied to guiding the design and optimization of software and hardware.

[0061] The above description is only an overview of the technical solution of the present invention. In order to be able to understand the technical means of the present invention more clearly, it can be implemented according to the content of the specification. And in order to make the above and other purposes, features and advantages of the present invention more obvious and understandable, the following specifically describes the embodiments of the present invention. Brief Description of the Drawings

[0062] By reading the detailed description of the preferred embodiments below, various other advantages and benefits will become clear to those of ordinary skill in the art. The drawings are only for the purpose of showing the preferred embodiments and are not considered to be a limitation of the present invention. And throughout the drawings, the same reference numerals are used to represent the same components. In the drawings:

[0063] Figure 1 It is a flowchart of a method for obtaining the consumption of code instructions in a hardware environment provided by an embodiment of the present invention;

[0064] Figure 2 It is a flowchart of another method for obtaining the consumption of code instructions in a hardware environment provided by an embodiment of the present invention;

[0065] Figure 3 It is a block diagram of the composition of a device for obtaining the consumption of code instructions in a hardware environment provided by an embodiment of the present invention;

[0066] Figure 4 It is a block diagram of the composition of another device for obtaining the consumption of code instructions in a hardware environment provided by an embodiment of the present invention. Detailed Embodiments

[0067] Exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present invention are shown in the drawings, it should be understood that the present invention can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided so that the present invention can be more thoroughly understood and the scope of the present invention can be fully conveyed to those skilled in the art.

[0068] An embodiment of the present invention provides a method for obtaining the instruction consumption of code instructions in a hardware environment, as Figure 1 shown, this method can obtain how much the specific instruction consumption is when the instructions corresponding to a certain line or multiple lines of specific code of the program instructions are running in the actual hardware environment. For this, the embodiment of the present invention provides the following specific steps:

[0069] 101. Run the program instructions in the hardware environment.

[0070] Among them, the program instructions refer to the executable files obtained by compiling the program source code using a compiler. The program instructions include multiple instructions to be run written in accordance with a preset instruction running order. Each instruction to be run contains one or more lines of code. At least one target instruction is included in the multiple instructions to be run. The target instruction can contain one or more lines of code, serving as the target code segment. The embodiment of the present invention is to obtain the instruction consumption corresponding to this target code segment.

[0071] Among them, the preset instruction running order refers to the order in which a large number of instructions to be run included in the program instructions are executed. Since the program instructions specifically contain a large amount of code composed of multiple lines, then this preset instruction running order is equivalent to the order in which the large amount of code is executed in the order of multiple lines, and this also includes the situation where the same instruction is called back one or more times.

[0072] In the embodiment of the present invention, the program instructions may include one or more target instructions. For multiple target instructions, these target instructions may include different target code segments or the same target code segment. If it is the different situation, it means that in the actual operation in the hardware environment, it is necessary to obtain the actual instruction consumption of different target code segments respectively; if it is the same situation, it means that in the actual operation in the hardware environment, it is fully considered that the timing of the same target code segment being called can be different (for example, the target instruction corresponding to the target code segment can be called back one or more times at different positions during the program operation), then the actual instruction consumption of each target code segment will also be different. Accordingly, the embodiment of the present invention respectively obtains the actual instruction consumption of these same target code segments.

[0073] 102. According to the preset instruction running order, before running the target instruction, obtain the cumulative number of clock cycles consumed for running other to-be-run instructions as the first number of clock cycles.

[0074] 103. According to the preset instruction running order, obtain the cumulative number of clock cycles consumed corresponding to after running the target instruction is completed as the second number of clock cycles.

[0075] In the embodiments of the present invention, for steps 102 - 103, an explanation is given. The program instructions are written according to the preset instruction running order, and the program instructions contain multiple to-be-run instructions. The CPU needs to consume clock cycles for running each to-be-run instruction. Then, during the process of running the program instructions, as the CPU runs the instructions one by one according to the preset instruction running order, the number of consumed clock cycles is continuously cumulatively increased.

[0076] Accordingly, in the embodiments of the present invention, in combination with this preset instruction running order, it is fully considered that: if there is one or more to-be-run instructions (i.e., other instructions) before the target instruction, these other instructions need to be run first before the target instruction can be run. Therefore, a certain number of clock cycles are cumulatively consumed before running the target instruction, which can be marked as the first number of clock cycles, and after executing the target instruction, another number of clock cycles can be cumulatively consumed, marked as the second number of clock cycles.

[0077] It should be noted that here in the embodiments of the present invention, the use of the words "first" and "second" is only for conveniently referring to and identifying different numbers of clock cycles, and there is no other meaning of sequence.

[0078] 104. Determine the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles.

[0079] In the embodiments of the present invention, for the process of running the program instructions, according to the preset instruction running order, the first number of clock cycles is the cumulative number of clock cycles consumed up to before running the target instruction, and the second number of clock cycles is the cumulative number of clock cycles consumed up to after running the target instruction is completed. Then, in fact, the difference between the second number of clock cycles and the first number of clock cycles is equivalent to exactly one target instruction that is run. Therefore, by subtracting the first number of clock cycles from the second number of clock cycles, the number of clock cycles consumed for running the target instruction is obtained, which is the actual instruction consumption corresponding to the target instruction.

[0080] As described above, the embodiments of the present invention provide a method for obtaining the consumption of code instructions in a hardware environment. The embodiments of the present invention run program instructions in a hardware environment. The program instructions include a plurality of instructions to be run written in a preset instruction running order. At least one of the instructions to be run is a target instruction, and the target instruction includes one or more lines of code. Then, according to the preset instruction running order, during the running of the program instructions, the cumulative number of clock cycles consumed by running all other instructions to be run before running the target instruction can be obtained as the first number of clock cycles, and the cumulative number of clock cycles consumed after running the target instruction is completed can also be obtained as the second number of clock cycles. Then, based on the difference between the second number of clock cycles and the first number of clock cycles, the number of clock cycles consumed by the target instruction, that is, the actual instruction consumption, can be known. Compared with the prior art, the technical problem of difficult to know the actual consumption of instructions corresponding to a specific line or multiple lines of code is solved. The embodiments of the present invention can obtain the specific instruction consumption of the instructions corresponding to a specific line or multiple lines of code in the program instructions when running in the actual hardware environment, so as to be applied to guiding the design and optimization of software and hardware.

[0081] To make a more detailed description of the above embodiments, the embodiments of the present invention also provide another method for obtaining the consumption of code instructions in a hardware environment, as Figure 2 shown. This method is a refined and supplementary statement of the above embodiments. The embodiments of the present invention provide the following specific steps:

[0082] 201. Obtain the program source code, and the program source code includes at least one target code segment for which the instruction consumption is to be obtained.

[0083] Among them, the program source code, also known as the source program, refers to an uncompiled text file written in accordance with certain programming language specifications, and is a series of computer language instructions that are human-readable.

[0084] In the embodiments of the present invention, the program source code includes at least one target code segment for which the instruction consumption is to be obtained, and the target code segment can be a specific line or multiple lines of code in the program source code.

[0085] 202. Add the corresponding preset function attribute to the target code segment.

[0086] Among them, the preset function attribute is a function attribute for obtaining instruction consumption. In the embodiments of the present invention, it can be but is not limited to using the attribute mechanism of the C language to set a function attribute for obtaining instruction consumption, that is, the GetCost_xxx attribute.

[0087] An attribute is a commonly used mechanism in C language, which can be used to set function attributes (FunctionAttribute), variable attributes (VariableAttribute), and type attributes (TypeAttribute). In the program source code, its position is restricted to be placed before the semicolon ";" at the end of the declaration. The writing feature of attribute is that there are two underscores before and after attribute, and it will be followed by a pair of parentheses, and the corresponding __attribute__ parameters are inside the parentheses. The syntax format of attribute is: attribute((attribute-list)).

[0088] In the embodiments of the present invention, in the program source code, modify the target code segment that needs to obtain the instruction consumption, and use the attribute mechanism of C language to add the GetCost_xxx attribute. For example:

[0089] Example 1, for a certain line of the target code segment, add the attribute, that is, the GetCost_xxx attribute, as follows:

[0090] __attribute__((GetCost))c = a + b;

[0091] Example 2, for the target code segment of multiple lines of specific code, add the GetCost_xxx attribute, as follows:

[0092] __attribute__((GetCost_Start))c = a + b;

[0093] d = a * b;

[0094] …

[0095] __attribute__((GetCost_End))e = a / b;

[0096] Furthermore, Example 3, for the target code segment of multiple lines of specific code, if it is a loop body, then add the GetCost_xxx attribute, as follows:

[0097] __attribute__((GetCost))for(i = 0, i < 100, i++)

[0098] {

[0099] …

[0100] }

[0101] As mentioned above, an example where the target code segment is one or more specific lines of code has been given. It should be noted that the attribute mechanism of the C language is a relatively mature mechanism currently in use, and the processing flow of the attribute mechanism can also be recognized by the compiler. Therefore, the embodiments of the present invention can directly utilize the attribute mechanism to construct and obtain the preset function attributes consumed by the instruction, and then implement the application function consumed by the obtained instruction.

[0102] However, it should be noted that if the attribute mechanism is not used, the embodiments of the present invention can also customize the keywords or characters for setting function attributes, and subsequently, the compilation processing flow of the keywords or characters needs to be configured in the compiler to achieve the application function equivalent to setting the GetCost_xxx attribute using the attribute mechanism as described above. Although it is not as efficient as directly using the attribute mechanism to implement the application function in this way, using custom keywords or characters can handle the problem of obtaining instruction consumption more flexibly according to the actual needs of users.

[0103] 203. Compile the program source code using a compiler to obtain the program instructions corresponding to the executable file, and the program instructions at least include the target instructions corresponding to the target code segment.

[0104] In the embodiments of the present invention, in this step, the compiler is used to compile the program source code and translate it into binary instructions that can be executed by a computer, that is, the program instructions corresponding to the executable file. This step can be further elaborated and explained as follows:

[0105] Firstly, the compiler is used to identify the preset function attributes in the program source code, and based on the identified preset function attributes, the target position of the target code segment in the program source code is determined.

[0106] In the embodiments of the present invention, in combination with the preset function attributes exemplified above, the GetCost_xxx attribute. If the target code segment is a single line of code segment, then the identified preset function attribute is "__attribute__((GetCost))"; however, if the target code segment is multiple specific lines of code, then the identified preset function attributes are "__attribute__((GetCost_Start))" and "__attribute__((GetCost_End))"; and further, if the target code segment is a loop body, then the identified preset function attribute is "__attribute__((GetCost))".

[0107] Next, according to the target position, insert the code corresponding to the first read clock cycle instruction before the target code segment and insert the code corresponding to the second read clock cycle instruction after the target code segment to obtain the modified program source code.

[0108] In the embodiment of the present invention, according to the target position of the target code segment in the program source code, insert the code corresponding to an instruction for reading the currently accumulated consumed clock cycles before and after this target position respectively. To distinguish these two read instructions, the read instruction corresponding to the code inserted before the target position is marked as the first read clock cycle instruction, and the read instruction corresponding to the code inserted after the target position is marked as the second read clock cycle instruction. The above obtains the modified program source code.

[0109] Furthermore, compile the modified program source code to obtain the program instructions corresponding to the executable file.

[0110] It should be noted that in the embodiment of the present invention, for the code corresponding to the two read instructions inserted at the target position, these are all operations completed within the compiler. Then, by compiling the modified program source code by the compiler, the program instructions will include these two read instructions and multiple instructions to be run, and among the multiple instructions to be run, there are target instructions corresponding to the target code segment. And for the compiled program instructions, in terms of the instruction execution order, a read instruction will be run first before running the target instruction, and a read instruction will also be run after running the target instruction.

[0111] 204. Run the program instructions in a hardware environment. The program instructions include multiple instructions to be run written according to a preset instruction execution order. Each instruction to be run includes one or more lines of code, and at least one target instruction is included in the multiple instructions to be run.

[0112] In the embodiment of the present invention, for the explanation of this step, refer to step 101, which will not be elaborated here.

[0113] 205. According to the preset instruction execution order, before running the target instruction, obtain the number of clock cycles accumulated for running other instructions to be run as the first number of clock cycles.

[0114] In the embodiment of the present invention, the specific detailed explanation of this step is as follows:

[0115] First, according to the preset instruction execution order, run all other instructions to be run sorted before the target instruction.

[0116] Next, run the first read clock cycle instruction to read the currently recorded number of clock cycles from a preset register, which is used to store the value of the accumulated clock cycles consumed for running the current instruction to be run during the execution of program instructions.

[0117] In the embodiment of the present invention, according to the compiler's compilation, the running order of the first read clock cycle instruction is before the target instruction, and the function of this first read clock cycle instruction is: during the execution of program instructions, read the currently accumulated consumed clock cycles. That is, through this first read clock cycle instruction, the number of clock cycles accumulated by the CPU for running instructions before running the target instruction can be obtained.

[0118] Exemplarily, taking the RISC-V instruction set as an example, RISC-V is an open-source instruction set architecture based on the principle of Reduced Instruction Set Computer (RISC), and the rdcycle instruction can be used to read the currently stored number of clock cycles from a register. It should be noted that for such a register, as the CPU continuously runs instructions, it will synchronously store the accumulated clock cycles consumed until the current instruction is completed. Then, by using such an rdcycle instruction for the first read clock cycle instruction, the number of clock cycles accumulated by the CPU for running instructions corresponding to the current target instruction before running the target instruction can be obtained by reading the currently updated recorded value of the clock cycles in the register.

[0119] 206. According to the preset instruction running order, obtain the number of clock cycles accumulated after running the target instruction is completed as the second number of clock cycles.

[0120] In the embodiment of the present invention, this step is specifically elaborated and explained as including the following:

[0121] First, run the target instruction according to the preset instruction running order. After running the target instruction, run the second read clock cycle instruction to read the currently recorded number of clock cycles from the preset register.

[0122] Second, determine the number of clock cycles consumed after running the target instruction according to the currently recorded number of clock cycles as the second number of clock cycles.

[0123] In an embodiment of the present invention, the running order of the second read clock cycle instruction obtained according to the compilation of the compiler is after the target instruction, and the function of the second read clock cycle instruction is: during the running of the program instruction, read the currently accumulated consumed clock cycles. That is, through the second read clock cycle instruction, it is possible to obtain the number of clock cycles accumulated by the CPU when running instructions after running the target instruction.

[0124] Exemplarily, still taking the RISC-V instruction set as an example, then for the second read clock cycle instruction using an rdcycle instruction, the number of clock cycles currently updated and recorded in the register can be read, and thus the number of clock cycles accumulated by the CPU when running instructions corresponding to the current target instruction after running can be obtained.

[0125] 207. Use the difference between the second clock cycle number and the first clock cycle number to obtain a third clock cycle number, and determine the instruction consumption corresponding to the target instruction according to the third clock cycle number.

[0126] In an embodiment of the present invention, the third clock cycle number obtained by using the difference between the second clock cycle number and the first clock cycle number means that during the running of the program instruction, according to the preset instruction running order, as the CPU continuously runs instructions, the difference in the clock cycles consumed after and before running the target instruction is obtained, and then this difference is the actual number of clock cycles consumed when running the target instruction, that is, the actual instruction consumption corresponding to the target instruction is obtained.

[0127] Further, as an implementation of the above Figure 1 、 Figure 2 shown method, an embodiment of the present invention provides a device for obtaining the consumption of code instructions in a hardware environment. This device embodiment corresponds to the foregoing method embodiment. For the convenience of reading, the details in the foregoing method embodiment will not be described one by one in this device embodiment, but it should be clear that the device in this embodiment can correspondingly implement all the contents in the foregoing method embodiment. This device is applied to obtain how much the specific instruction consumption is when a certain line or multiple lines of specific code corresponding to the program instruction runs in the actual hardware environment, specifically as Figure 3 shown, this device includes:

[0128] An operation unit 31, configured to run program instructions in a hardware environment, where the program instructions include multiple to-be-run instructions written according to a preset instruction running order, each to-be-run instruction includes one or more lines of code, and at least one target instruction is included in the multiple to-be-run instructions;

[0129] The first acquisition unit 32 is configured to, according to the preset instruction execution sequence, before executing the target instruction, acquire the cumulative number of clock cycles consumed by executing other to-be-executed instructions as the first number of clock cycles;

[0130] The first acquisition unit 32 is further configured to, according to the preset instruction execution sequence, acquire the cumulative number of clock cycles consumed corresponding to after finishing executing the target instruction as the second number of clock cycles;

[0131] The determination unit 33 is configured to determine the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles.

[0132] Further, as Figure 4 shown, the apparatus further includes:

[0133] The second acquisition unit 34 is configured to, before executing program instructions in the hardware environment, acquire program source code, where the program source code at least includes a target code segment for which the instruction consumption is to be acquired;

[0134] The addition unit 35 is configured to add a corresponding preset function attribute to the target code segment, where the preset function attribute is a function attribute for acquiring instruction consumption;

[0135] The compilation unit 36 is configured to compile the program source code by using a compiler to obtain program instructions corresponding to an executable file, where the program instructions at least include target instructions corresponding to the target code segment.

[0136] Further, as Figure 4 shown, the compilation unit 36 includes:

[0137] The recognition module 361 is configured to use the compiler to recognize the preset function attribute in the program source code;

[0138] The judgment module 362 is configured to judge the target position of the target code segment in the program source code according to the recognized preset function attribute;

[0139] The insertion module 363 is configured to, according to the target position, insert code corresponding to a first read clock cycle instruction before the target code segment and insert code corresponding to a second read clock cycle instruction after the target code segment to obtain a modified program source code;

[0140] The acquisition module 364 is configured to obtain program instructions corresponding to an executable file by compiling the modified program source code.

[0141] Further, as Figure 4 shown, the first acquisition unit 32 includes:

[0142] The running module 321 is configured to run all other to-be-run instructions sorted before the target instruction according to the preset instruction running sequence;

[0143] The running module 321 is further configured to run the first clock cycle reading instruction to read the currently recorded number of clock cycles from the preset register, where the preset register is used to store the value of the accumulated clock cycles consumed for running the current to-be-run instruction during the process of running the program instruction;

[0144] The determining module 322 is configured to determine the number of clock cycles consumed corresponding to before running the target instruction according to the currently recorded number of clock cycles, as the first number of clock cycles.

[0145] Further, as Figure 4 shown, the first obtaining unit 32 includes:

[0146] The running module 321 is configured to run the target instruction according to the preset instruction running sequence;

[0147] The running module 321 is further configured to run the second clock cycle reading instruction to read the currently recorded number of clock cycles from the preset register;

[0148] The determining module 322 is configured to determine the number of clock cycles consumed corresponding to after running the target instruction according to the currently recorded number of clock cycles, as the second number of clock cycles.

[0149] Further, as Figure 4 shown, the determining unit 33 includes:

[0150] The obtaining module 331 is configured to subtract the first number of clock cycles from the second number of clock cycles to obtain a third number of clock cycles;

[0151] The determining module 332 is configured to determine the instruction consumption corresponding to the target instruction according to the third number of clock cycles.

[0152] In summary, the embodiments of the present invention provide a method and device for obtaining the consumption of code instructions in a hardware environment. In the embodiments of the present invention, program instructions are run in a hardware environment. The program instructions include a plurality of to-be-run instructions written in accordance with a preset instruction running order, and at least one of the to-be-run instructions is a target instruction, and the target instruction includes one or more lines of code. Then, in accordance with the preset instruction running order, during the process of running the program instructions, the number of clock cycles accumulated for running all other to-be-run instructions before running the target instruction can be obtained as the first number of clock cycles, and the number of clock cycles accumulated correspondingly after running the target instruction can also be obtained as the second number of clock cycles. Subsequently, based on the difference between the second number of clock cycles and the first number of clock cycles, the number of clock cycles consumed by the target instruction, that is, the actual instruction consumption, can be known. Compared with the prior art, the technical problem of being difficult to know the actual consumption of instructions corresponding to a specific line or multiple lines of code is solved. The embodiments of the present invention can obtain the specific instruction consumption of instructions corresponding to a specific line or multiple lines of code in a program instruction when running in an actual hardware environment, so as to be applied to guiding the design and optimization of software and hardware.

[0153] The device for obtaining the consumption of code instructions in a hardware environment includes a processor and a memory. The above-mentioned running unit, obtaining unit, determining unit, etc. are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to implement corresponding functions.

[0154] The processor includes a kernel, and the kernel retrieves the corresponding program units from the memory. One or more kernels can be set, and by adjusting the kernel parameters, the specific instruction consumption of instructions corresponding to a specific line or multiple lines of code in a program instruction when running in an actual hardware environment can be obtained, so as to be applied to guiding the design and optimization of software and hardware.

[0155] The embodiments of the present invention provide a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method for obtaining the consumption of code instructions in a hardware environment as described above is implemented.

[0156] The embodiments of the present invention provide an electronic device, including: a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the computer program, the method for obtaining the consumption of code instructions in a hardware environment as described above is implemented.

[0157] This application is described with reference to the flowcharts and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, and combinations of flows and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to the processors of general purpose computers, special purpose computers, embedded processors, or other programmable data processing devices to produce a machine, such that the instructions executed by the processors of the computer or other programmable data processing devices produce means for implementing the functions specified in one Figure 1 flow or multiple flows and / or blocks Figure 1 or multiple blocks.

[0158] In a typical configuration, the device includes one or more processors (CPUs), a memory, and a bus. The device may also include an input / output interface, a network interface, etc.

[0159] The memory may include non-permanent memory in the form of computer-readable media, random access memory (RAM) and / or non-volatile memory such as read-only memory (ROM) or flash RAM, and the memory includes at least one storage chip. The memory is an example of computer-readable media.

[0160] Computer-readable media includes both permanent and non-permanent, removable and non-removable media and can store information by any method or technology. The information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transitory medium that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.

[0161] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0162] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt 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.) that contain computer-usable program code.

[0163] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent insertion, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.

Claims

1. A method for obtaining the consumption of code instructions in a hardware environment, characterized in that, The method includes: Obtain the program source code, where the program source code at least includes a target code segment consumed by a to-be-obtained instruction; Add a corresponding preset function attribute to the target code segment, where the preset function attribute is the function attribute consumed by the obtain instruction; Use a compiler to compile the program source code to obtain program instructions corresponding to an executable file, where the program instructions at least include target instructions corresponding to the target code segment; The using the compiler to compile the program source code to obtain program instructions corresponding to an executable file includes: Use the compiler to identify the preset function attribute in the program source code; According to the identified preset function attribute, determine the target position of the target code segment in the program source code; According to the target position, insert code corresponding to a first read clock cycle instruction before the target code segment and insert code corresponding to a second read clock cycle instruction after the target code segment to obtain a modified program source code; Obtain program instructions corresponding to an executable file by compiling the modified program source code; Run the program instructions in a hardware environment, where the program instructions include multiple to-be-run instructions written according to a preset instruction running order, each to-be-run instruction includes one or more lines of code, and at least one of the multiple to-be-run instructions is a target instruction; According to the preset instruction running order, before running the target instruction, obtain the cumulative number of clock cycles consumed by running other to-be-run instructions as a first number of clock cycles; According to the preset instruction running order, obtain the cumulative number of clock cycles consumed up to the completion of running the target instruction as a second number of clock cycles; Determine the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles.

2. The method according to claim 1, wherein The according to the preset instruction running order, before running the target instruction, obtain the cumulative number of clock cycles consumed by running other to-be-run instructions as a first number of clock cycles includes: According to the preset instruction running order, run all other to-be-run instructions sorted before the target instruction; Run the first read clock cycle instruction to read the currently recorded number of clock cycles from a preset register, where the preset register is used to store the value of the cumulative number of clock cycles consumed by running the current to-be-run instruction during the running of the program instructions; According to the currently recorded number of clock cycles, determine the number of clock cycles consumed before running the target instruction as a first number of clock cycles.

3. The method according to claim 1, wherein The according to the preset instruction running order, obtain the cumulative number of clock cycles consumed up to the completion of running the target instruction as a second number of clock cycles includes: According to the preset instruction running order, run the target instruction; Run the second read clock cycle instruction to read the currently recorded number of clock cycles from the preset register; According to the currently recorded number of clock cycles, determine the number of clock cycles consumed upon completion of running the target instruction as a second number of clock cycles.

4. The method according to any one of claims 1 to 3, characterized in that, Determining the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles includes: Taking the difference between the second number of clock cycles and the first number of clock cycles to obtain a third number of clock cycles; Determining the instruction consumption corresponding to the target instruction according to the third number of clock cycles.

5. A device for obtaining the consumption of code instructions in a hardware environment, characterized in that, The device includes: A second acquisition unit, configured to acquire a program source code before running program instructions in the hardware environment, where the program source code includes at least one target code segment for which instruction consumption is to be acquired; An adding unit, configured to add a corresponding preset function attribute to the target code segment, where the preset function attribute is a function attribute for acquiring instruction consumption; A compiling unit, configured to compile the program source code by using a compiler to obtain program instructions corresponding to an executable file, where the program instructions include at least target instructions corresponding to the target code segment; The compiling unit includes: An identifying module, configured to identify the preset function attribute in the program source code by using the compiler; A judging module, configured to judge the target position of the target code segment in the program source code according to the identified preset function attribute; An inserting module, configured to insert code corresponding to a first read clock cycle instruction before the target code segment and insert code corresponding to a second read clock cycle instruction after the target code segment according to the target position to obtain a modified program source code; An obtaining module, configured to obtain program instructions corresponding to an executable file by compiling the modified program source code; A running unit, configured to run program instructions in a hardware environment, where the program instructions include a plurality of to-be-run instructions written according to a preset instruction running order, each to-be-run instruction includes one or more lines of code, and at least one target instruction is included in the plurality of to-be-run instructions; A first acquisition unit, configured to acquire, according to the preset instruction running order, the number of clock cycles consumed cumulatively for running other to-be-run instructions before running the target instruction as a first number of clock cycles; The first acquisition unit is further configured to acquire, according to the preset instruction running order, the number of clock cycles consumed cumulatively after running the target instruction is completed as a second number of clock cycles; A determining unit, configured to determine the instruction consumption corresponding to the target instruction according to the second number of clock cycles and the first number of clock cycles.

6. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the method for acquiring code instruction consumption on a hardware environment as described in any one of claims 1-4 is implemented.

7. An electronic device, characterized in that, Including: A memory, a processor, and a computer program stored on the memory and executable on the processor, and when the processor executes the computer program, the method for acquiring code instruction consumption on a hardware environment as described in any one of claims 1-4 is implemented.

Citation Information

Patent Citations

  • Nanosecond timing method and system based on ARM framework

    CN105404535A