Hot spot analysis method and device for program code

Generating a function call relationship tree through static analysis and instrumentation technology solves the problem that it is difficult to achieve hot spot analysis manually in embedded systems, realizes automated hot spot analysis and detailed report generation, and improves development efficiency and accuracy sex.

CN112445512BInactive Publication Date: 2024-12-17SHANGHAI GOLDWAY INTELLIGENT TRANSPORTATION SYST CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN201910810378.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2019-08-29
Publication Date
2024-12-17
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

Under the embedded system platform, the lack of effective Profiler tools makes it difficult to manually add time-consuming statistical code to achieve hot spot analysis based on function call relationship tree, errors are prone to and the generated reports are simple and inconsistent, and it is difficult to support module optimization evaluation.

Method used

Generate function call relationship diagram through static analysis, destroy loops or recursive calls, form a function call relationship tree, and add timing codes and hot spot analysis report generation algorithms in the program, calculate the run time and number of calls of subfunctions, and generate detailed Hot topic analysis report.

Benefits of technology

It realizes automated hot spot analysis and generates simple and intuitive reports, helping developers quickly analyze hot spots in the use of programs, avoiding the errors and complexity of manually adding code, and saving time and cost.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112445512B_ABST
    Figure CN112445512B_ABST
Patent Text Reader

Abstract

The present application provides a method and device for hot spot analysis of program code, including: statically analyzing the program code with a designated function as the main function to generate a function call relationship graph; destroying the loop call or recursive call that appears in the function call relationship graph to generate a function call relationship tree; adding a first type of code before and after each sub-function call, which is used to calculate the running time of the corresponding sub-function when it is called by the function in the function call relationship tree during the program code running process, and save the running time to the leaf node corresponding to the sub-function; adding a second type of code to the program code, which is used to generate a hot spot analysis report based on the running time saved in each leaf node and the number of times the corresponding sub-function is called; running the program code to generate a hot spot analysis report of the function call relationship tree. This technical solution solves the problem that it is impossible to perform hot spot analysis on program code based on the function call relationship tree by manually adding time-consuming statistical code.
Need to check novelty before this filing date? Find Prior Art

Description

[Technical field]

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for hot spot analysis of program codes. [Background technology]

[0002] In computers, especially embedded systems, computing resources are usually extremely limited. Therefore, computationally intensive algorithms such as image algorithms, deep learning algorithms, image encoding and decoding, etc. often have relatively high efficiency requirements, and often require programmers to deeply optimize the programs.

[0003] Typically, more than 90% of the time spent in a program is concentrated in 10% of the code segments. Therefore, a more effective optimization method is to analyze the hot spots of the program and optimize the time-consuming code segments in a targeted manner (such as algorithm reconstruction, adjusting the calculation order, using specialized hardware units, parallelization, etc.) to improve optimization efficiency.

[0004] However, currently, in embedded system platforms (such as ARM non-Linux kernel, DSP, etc.), there is often a lack of necessary Profiler tools to perform hot spot analysis on algorithms or programs. Currently, when analyzing hot spots for algorithms, code is added to the code segment to calculate the timestamp of the runtime of each module, and then the runtime of each code segment is calculated based on the timestamp.

[0005] This time-consuming method of manually adding code tests has the following disadvantages:

[0006] 1) Manually added code is very error-prone. In many cases, errors will occur in time-consuming statistics due to careless writing.

[0007] 2) Generally, manually added time-consuming statistical code makes it difficult to implement statistical analysis based on function call graphs and call trees. Statistical analysis means that a large number of global variables need to be established, and the amount of code writing is huge.

[0008] 3) Manually written time-consuming statistics code is difficult to generate detailed and easy-to-read reports. As a temporary added module, developers usually do not spend a lot of time on the time-consuming statistics module, so the generated reports are often very simple and the format is not uniform, which brings a certain degree of difficulty to the evaluation and collaboration of module optimization. [Summary of the invention]

[0009] In view of this, an embodiment of the present invention provides a hot spot analysis method for program code, which is used to solve the problem in the prior art that manually adding time-consuming statistical code cannot realize hot spot analysis of program code based on a function call relationship tree.

[0010] On the one hand, an embodiment of the present invention provides a hot spot analysis method for program code, including: performing static analysis on the program code with a designated function as the main function to generate a function call relationship graph; destroying the loop call or recursive call appearing in the function call relationship graph to obtain a function call relationship tree; wherein the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function; adding a first type of code before and after the call of each sub-function; wherein the first type of code is used to calculate the running time corresponding to each call of the sub-function during the running of the program code, and save the running time to the leaf node corresponding to the sub-function; adding a second type of code to the program code; wherein the second type of code is used to generate a hot spot analysis report based on the running time saved in each leaf node in the function call relationship tree and the number of calls of the corresponding sub-function; running the program code after adding the first type of code and the second type of code to generate a hot spot analysis report of the function call relationship tree.

[0011] Optionally, destroying the loop calls or recursive calls appearing in the function call relationship graph to obtain a function call relationship tree includes: setting the main function as the root node of the function call relationship tree; based on the function call relationship graph, if the main function has a first sub-function, setting the first sub-function as a leaf node of the root node; if the first sub-function also calls a second sub-function, determining whether the second sub-function is a loop call or a recursive call; if the second sub-function is a loop call or a recursive call, setting the second sub-function as the leaf node of the leaf node, and stopping traversing the calling relationship of the second sub-function in the function call relationship graph; if the second sub-function is not a loop call or a recursive call, setting the second sub-function as the leaf node of the leaf node, and continuing to traverse the function call relationship graph.

[0012] Optionally, destroying the circular calls or recursive calls appearing in the function call relationship graph to obtain a function call relationship tree also includes: setting a maximum call depth; if the first sub-function also calls the second sub-function, determining whether the maximum call depth has been reached; if so, setting the second sub-function to be the leaf node of the leaf node, and stopping traversing the call relationship of the second sub-function in the function call relationship graph; if not, setting the second sub-function to be the leaf node of the leaf node, and continuing to traverse the call relationship of the second sub-function in the function call relationship graph.

[0013] Optionally, the hot spot analysis method further includes: assigning a number to each node in the function call relationship tree; wherein the root node is numbered 1, and each leaf node is numbered in sequence according to a breadth-first principle.

[0014] Optionally, the running of the program code after adding the first category code and the second category code to generate a hotspot analysis report of the function call relationship tree includes: during the running of the program code, when the sub-function is called by other functions, using the first category code corresponding to the sub-function to determine whether the other function is a function in the function call relationship tree; if so, the first category code corresponding to the sub-function calculates the running time of this call to the sub-function, and saves the running time to the leaf node corresponding to the sub-function; if not, the running time of this call to the sub-function is not calculated; after running the program code, using the second category code to determine the hotspot distribution of each sub-function according to the running time of each call of the sub-function saved in each leaf node in the function call relationship tree, and the number of times the sub-function is called.

[0015] Optionally, the hotspot analysis method also includes: adding the first type of code before and after the call of the main function; the first type of code is used to calculate the running time of each execution of the main function during the running of the program code, and save the running time to the root node; the second type of code is also used to generate a hotspot analysis report based on the running time saved in the root node in the function call relationship tree and the number of times the main function is called.

[0016] Optionally, the running of the program code after adding the first category code and the second category code to generate a hotspot analysis report of the function call relationship tree also includes: during the running of the program code, using the main function corresponding to the first category code to calculate the running time of each execution of the main function, and saving the running time to the root node; after running the program code, using the second category code according to the running time of each execution of the main function saved in the root node of the function call relationship tree to determine the hotspot distribution of the longest running time of the main function.

[0017] Optionally, the hotspot analysis method also includes: adding a first type of deletion marker and a second type of deletion marker to the program code; wherein the first type of deletion marker and the second type of deletion marker are respectively used to indicate the deletion of the first type of code and the second type of code after generating a hotspot analysis report of the function call relationship tree.

[0018] Optionally, after running the program code after adding the first category code and the second category code to generate a hotspot analysis report of the function call relationship tree, it also includes: based on the first category deletion mark and the second category deletion mark, instructing to delete the first category code and the second category code to restore the program code.

[0019] On the other hand, an embodiment of the present invention further provides a hot spot analysis device for program code, comprising: a static analysis module, used to perform static analysis on the program code with a specified function as the main function to generate a function call relationship graph; a function call relationship tree generation module, used to destroy the loop call or recursive call appearing in the function call relationship graph to obtain a function call relationship tree; wherein the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function; a first-category code adding module, used to add the first-category code before and after the call of each sub-function; wherein the first-category code is used to calculate the running time of the corresponding sub-function each time it is called by the function in the function call relationship tree during the running of the program code, and save the running time to the leaf node corresponding to the sub-function; a second-category code adding module, used to add the second-category code to the program code; wherein the second-category code is used to generate a hot spot analysis report according to the running time saved in each leaf node in the function call relationship tree and the number of times the corresponding sub-function is called; a hot spot analysis module, used to run the program code after adding the first-category code and the second-category code to generate a hot spot analysis report of the function call relationship tree.

[0020] Compared with the prior art, this technical solution has at least the following beneficial effects:

[0021] According to the hot spot analysis method of the program code provided by the embodiment of the present invention, a function in the program code (the user can specify the function arbitrarily) is used as the main function for static analysis to obtain a function call relationship graph, and the loop structure (i.e., the situation where a loop call or a recursive call occurs) in the function call relationship graph is destroyed based on the function call relationship graph, thereby generating a function call relationship tree. Among them, the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function. Further, each node in the function call relationship tree is assigned a number, wherein the root node is numbered 1, and each leaf node is numbered in sequence according to the breadth-first principle.

[0022] The timing function (i.e., the first type of code) and the code of the hotspot analysis report generation algorithm (i.e., the second type of code) are added to the program code by the plugging method. In the process of running the program code, the code of the timing function can calculate the running time of each sub-function when it is called by the function in the function call relationship tree and save the running time to the corresponding leaf node. And since each node in the function call relationship tree is assigned a number, this number is used as the global ID of the sub-function in the function call relationship tree. Therefore, when the timing function is run, when other functions outside the function call relationship tree call the sub-function in the function call relationship tree, since the other function does not have the number in the function call relationship tree (i.e., there is no global ID), the timing function will not calculate the running time of the sub-function, thereby avoiding the interference of external calls on the hotspot analysis of the function call relationship tree. Furthermore, the code of the hotspot analysis report generation algorithm can generate a hotspot analysis report of the function call relationship tree according to the running time of each sub-function saved by each leaf node in the function call relationship tree.

[0023] Therefore, the hot spots of functions are analyzed based on the call relationship tree. The results are simple and intuitive, which can help developers quickly analyze the hot spots of program usage. Since the code for adding timing functions and the code for generating hot spot analysis reports in the program code are automatically completed through the instrumentation method, there is no need to add them manually, which saves time costs.

[0024] Furthermore, the timing function code is also added before and after the main function is called. During the execution of the program code, the timing function can be run to calculate the running time of each execution of the main function, and the running time is saved to the root node. Then, the code of the hotspot analysis report generation algorithm can be run to determine the hotspot distribution of the longest running time of the main function.

[0025] Furthermore, a first type of deletion mark and a second type of deletion mark are added to the program code, which are used to indicate the deletion of the timing function code and the hot spot analysis report generation algorithm code respectively after generating the hot spot analysis report of the function call relationship tree, without manual deletion.

Brief Description of the Drawings

[0026] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0027] Figure 1 It is a flowchart of a specific embodiment of the hot spot analysis method of program code provided by an embodiment of the present invention;

[0028] Figure 2 yes Figure 1 A schematic diagram of a specific embodiment of a function call relationship diagram in a hot spot analysis method of a program code shown;

[0029] Figure 3 yes Figure 1 A schematic diagram of a specific embodiment of a function call relationship tree in a hot spot analysis method of a program code shown;

[0030] Figure 4 is a flowchart of another specific embodiment of the hot spot analysis method of program code provided by an embodiment of the present invention;

[0031] Figure 5 It is a structural diagram of a specific embodiment of a hot spot analysis device for program codes provided in an embodiment of the present invention. [Specific implementation method]

[0032] In order to better understand the technical solution of the present invention, the embodiments of the present invention are described in detail below with reference to the accompanying drawings.

[0033] It should be clear that the described embodiments are only some embodiments of the present invention, not all 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.

[0034] Figure 1 FIG. 1 is a flow chart of a specific embodiment of a method for hot spot analysis of program code provided by an embodiment of the present invention. Figure 1 , the hot spot analysis method comprises the following steps:

[0035] Step 101, statically analyzing the program code with a designated function as a main function to generate a function call relationship graph;

[0036] Step 102: destroy the circular calls or recursive calls appearing in the function call relationship graph to obtain a function call relationship tree; wherein the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function;

[0037] Step 103, adding a first type of code before and after the call of each sub-function; wherein the first type of code is used to calculate the running time of the corresponding sub-function when it is called by the function in the function call relationship tree during the program code running process, and save the running time to the leaf node corresponding to the sub-function;

[0038] Step 104: adding a second type of code to the program code; wherein the second type of code is used to generate a hotspot analysis report according to the running time saved by each leaf node in the function call relationship tree and the number of times the corresponding sub-function is called;

[0039] Step 105 , running the program code after adding the first category code and the second category code to generate a hot spot analysis report of the function call relationship tree.

[0040] In this embodiment, the program code may be a code written in assembly language or a high-level programming language such as C language, C++ language, Java, etc. Usually, the program code has an entry function (i.e., main function), and the calls of other functions and methods are driven from the main function. However, in this embodiment, since the user may only need to perform hot spot analysis on a part of the entire program code (e.g., a certain functional module in the program code), the user can arbitrarily specify a function as the main function, and the specified function is not the main function.

[0041] As described in step 101, static analysis is performed on the program code with the designated function as the main function to generate a function call relationship graph.

[0042] It should be noted that before executing this step, the source file can be preprocessed, which includes filtering out preprocessing code segments such as comments and macros, and then scanning the program code using a code scanning tool to identify all functions in the program code and sub-functions called by the functions.

[0043] According to a function selected by the user (as the designated function), the code segment where the designated function is located is found from the program code, and then the program code is statically analyzed with the main function. It is understood by those skilled in the art that static analysis can be used to scan the program code without running the program code through lexical analysis, syntax analysis, control flow, data flow analysis and other technologies to verify whether the program code meets the indicators of standardization, security, reliability, maintainability and the like.

[0044] Specifically in this embodiment, a function call relationship diagram can be obtained by statically analyzing the program code according to the main function selected by the user. The function call relationship diagram depicts the various call relationships between the main function calling the sub-function and the sub-function calling the sub-function. According to the logic of the program design, recursive calls or cyclic calls between functions may appear in the function call relationship diagram. Among them, recursive calls refer to the situation where a function directly calls itself; cyclic calls refer to the situation where a function calls other functions and then other functions call itself.

[0045] Figure 2 Shown is Figure 1 The diagram is a schematic diagram of a specific embodiment of a function call relationship diagram in a hot spot analysis method for program codes.

[0046] refer to Figure 2 , the main function is root function 201; the main function root function 201 calls sub-function fun_b() 202, sub-function fun_a() 203 and sub-function fun_c() 204 respectively; wherein,

[0047] 1) Sub-function fun_b() 202 calls sub-function fun_d() 205 .

[0048] 2) The sub-function fun_a() 203 shares four branches, namely:

[0049] (1) Sub-function fun_a() 203 calls sub-function fun_c() 206, and sub-function fun_c() 206 re-circulates and calls sub-function fun_a() 203;

[0050] (2) Subfunction fun_a() 203 recursively calls itself;

[0051] (3) Sub-function fun_a() 203 calls sub-function fun_d() 207;

[0052] (4) Sub-function fun_a() 203 calls sub-function fun_e() 208, sub-function fun_e() 208 calls sub-function fun_f() 212, sub-function fun_f() 212 calls sub-function fun_g() 215, sub-function fun_h() 216, and sub-function fun_i() 217 ​​respectively.

[0053] 3) The sub-function fun_c() 204 shares two branches, namely:

[0054] (1) Sub-function fun_c() 204 calls sub-function fun_a() 209;

[0055] (2) The sub-function fun_c() 204 calls the sub-function fun_b() 210 , and the sub-function fun_b() 210 calls the sub-function fun_d() 213 .

[0056] It can be seen that Figure 2 The function call relationship graph shown includes recursive calls (for example, sub-function fun_a() 203 recursively calls itself) and loop calls (for example, sub-function fun_a() 203 calls sub-function fun_c() 206, and sub-function fun_c() 206 then loops and calls sub-function fun_a() 203).

[0057] As described in step 102, the loop calls or recursive calls appearing in the function call relationship graph are destroyed to obtain a function call relationship tree; wherein the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function.

[0058] In this embodiment, when recursive calls and loop calls appear in the function call relationship graph, the nodes of the recursive calls and loop calls are destroyed so that subsequent hot spot analysis of the program code is not affected by the loop calls or recursive calls between functions.

[0059] Specifically, this step includes:

[0060] Step 1021, setting the main function as the root node of the function call relationship tree;

[0061] Step 1022: Based on the function call relationship graph, if the main function has a first sub-function, set the first sub-function as a leaf node of the root node;

[0062] Step 1023: If the first sub-function also calls a second sub-function, determine whether the second sub-function is a loop call or a recursive call;

[0063] Step 1024: If the second sub-function is a loop call or a recursive call, after setting the second sub-function as the leaf node of the leaf node, stop traversing the call relationship of the second sub-function in the function call relationship graph;

[0064] Step 1025: If the second sub-function is not a loop call or a recursive call, the second sub-function is set as the leaf node of the leaf node, and the function call relationship graph is continued to be traversed.

[0065] Figure 3 yes Figure 1 The diagram is a schematic diagram of a specific embodiment of a function call relationship tree in a hot spot analysis method of a program code.

[0066] Combined with reference Figure 2 and Figure 3 For example, the function call relationship diagram obtained by statically analyzing the program code with the specified function as the main function is as follows: Figure 2 shown.

[0067] As described in step 1021, the main function is set as the root node of the function call relationship tree.

[0068] For example, in Figure 2 In the function call relationship diagram shown in FIG. 1 , the main function is root function 201; Figure 3In the function call relationship tree shown, the main function root function 301 is set as the root node of the function call relationship tree.

[0069] As described in step 1022, based on the function call relationship graph, if the main function has a first sub-function, the first sub-function is set as a leaf node of the root node.

[0070] For example, in Figure 2 In the function call relationship diagram shown, the main function root function 201 has three first sub-functions, namely sub-function fun_b() 202, sub-function fun_a() 203 and sub-function fun_c() 204. Figure 3 In the function call relationship tree shown, sub-function fun_b() 302 , sub-function fun_a() 303 , and sub-function fun_c() 304 are respectively set as leaf nodes of the root node.

[0071] As described in step 1023, if the first sub-function also calls the second sub-function, it is determined whether the second sub-function is a loop call or a recursive call.

[0072] For example, in Figure 2 In the function call relationship diagram shown, the first sub-functions (i.e., sub-function fun_b() 202, sub-function fun_a() 203, and sub-function fun_c() 204) all call the second sub-function. Among them, sub-function fun_b() 202 calls sub-function fun_d() 205, sub-function fun_a() 203 calls sub-function fun_d() 207, sub-function fun_c() 204 calls sub-function fun_a() 209, etc. For details, please refer to the above about Figure 2 Detailed description.

[0073] As described in step 1024, if the second sub-function is a loop call or a recursive call, after setting the second sub-function as the leaf node of the leaf node, stop traversing the calling relationship of the second sub-function in the function call relationship graph.

[0074] For example, in Figure 2 In the function call relationship diagram shown in FIG. 1 , sub-function fun_a() 203 calls sub-function fun_c() 206, and sub-function fun_c() 206 recycles and calls sub-function fun_a() 203. Among them, sub-function fun_c() 206 recycles and calls sub-function fun_a() 203, which results in a cyclic call. Figure 3In the function call relationship tree shown, after fun_a() 313 is set as a leaf node of fun_c() 307 , the traversal of the call relationship of fun_a() 203 in the function call relationship graph is stopped.

[0075] For example, in Figure 2 In the function call relationship diagram shown in FIG. 1 , the sub-function fun_a() 203 recursively calls itself, that is, a recursive call occurs. Figure 3 In the function call relationship tree shown, after setting the sub-function fun_a() 308 as the leaf node of the sub-function fun_a() 303 , the traversal of the call relationship of fun_a() 308 in the function call relationship graph is stopped.

[0076] As described in step 1025, if the second sub-function is not a loop call or a recursive call, the second sub-function is set as the leaf node of the leaf node, and the function call relationship graph is continued to be traversed.

[0077] For example, in Figure 2 In the function call relationship diagram shown in FIG. 1 , the sub-function fun_c() 204 calls the sub-function fun_b() 210, and the sub-function fun_b() 210 is not a loop call or a recursive call. Figure 3 In the function call relationship tree shown in FIG. 1 , after setting the sub-function fun_b() 306 as the leaf node of the sub-function fun_c() 302 , the function call relationship graph is traversed continuously. The sub-function fun_b() 210 then calls the sub-function fun_d() 213 . Figure 3 In the function call relationship tree shown, the sub-function fun_d() 312 is set as a leaf node of the sub-function fun_b() 306 .

[0078] Furthermore, the step 102 further includes:

[0079] Step 1026, setting the maximum call depth;

[0080] Step 1027: If the first sub-function also calls the second sub-function, determine whether the maximum calling depth is reached;

[0081] Step 1028. If yes, then after setting the second sub-function as the leaf node of the leaf node, stop traversing the calling relationship of the second sub-function in the function call relationship graph; if not, after setting the second sub-function as the leaf node of the leaf node, continue traversing the calling relationship of the second sub-function in the function call relationship graph.

[0082] Among them, the specific depth value of the maximum call depth can be set according to the needs of hot spot analysis. Since there may be multiple layers of sub-functions calling sub-functions in the function call relationship graph, it is not necessary to traverse to particularly deep sub-functions according to the needs of hot spot analysis. Therefore, a maximum call depth can be set in advance. If the first sub-function also calls the second sub-function, in addition to determining whether the second sub-function is a loop call or a recursive call, it is also necessary to determine whether the maximum call depth has been reached; if so, the second sub-function is set as the leaf node of the leaf node, and the traversal of the call relationship of the second sub-function in the function call relationship graph is stopped; if not, the second sub-function is set as the leaf node of the leaf node, and the traversal of the call relationship of the second sub-function in the function call relationship graph continues.

[0083] According to the above method, we can Figure 2 The function call graph shown is converted to Figure 3 The function call relationship tree shown.

[0084] Furthermore, after the function call relationship tree is generated, a number is assigned to each node in the function call relationship tree, wherein the root node is numbered 1, and each leaf node is numbered in sequence according to the breadth-first principle, which is a horizontal priority principle, starting from the root node and traversing each leaf node of the tree along the width of the function call relationship tree.

[0085] Continue to refer Figure 3 , root function 301 is numbered 1, fun_c() 302 is numbered 2, fun_a() 303 is numbered 3, fun_b() 304 is numbered 4, fun_a() 305 is numbered 5, fun_b() 306 is numbered 6, fun_c() 307 is numbered 7, fun_a() 308 is numbered 8, fun_a() 309 is numbered 9, fun_e() 310 is numbered 10, fun_d()311 is numbered 11, fun_d()312 is numbered 12, fun_a()313 is numbered 13, fun_b()314 is numbered 14, fun_f()315 is numbered 15, fun_b()316 is numbered 16, fun_g()317 is numbered 17, fun_h()318 is numbered 18 and fun_i()319 is numbered 19.

[0086] After numbering, the number of each leaf node can be used as a global ID in the function call relationship tree, that is, calls between functions in the function call relationship tree (including main function calling sub-functions and calls between sub-functions) can find the corresponding calling function according to the global ID.

[0087] As described in step 103, a first type of code is added before and after the call of each sub-function; wherein, the first type of code is used to calculate the running time of the corresponding sub-function when it is called by the function in the function call relationship tree during the execution of the program code, and save the running time to the leaf node corresponding to the sub-function.

[0088] Specifically, in the program code, the first type of code is added before and after the call of each sub-function by means of the stub method. The first type of code inserted by the stub method will be compiled into the executable file together, and the first type of code will also be executed during the running of the program code. In this embodiment, the first type of code is the code of the timing function, which is used to calculate the running time of the corresponding sub-function when it is called by the function in the function call relationship tree during the running of the program code, and save the running time to the leaf node corresponding to the sub-function.

[0089] The execution process of the first type of code will be described in detail in step 105.

[0090] As described in step 104, a second type of code is added to the program code; wherein the second type of code is used to generate a hot spot analysis report based on the running time saved by each leaf node in the function call relationship tree and the number of times the corresponding sub-function is called.

[0091] Specifically, the second type of code is added to the program code by inserting a stub, and the second type of code inserted by the inserting stub will be compiled into the executable file together, and the second type of code will also be executed during the running of the program code. In this embodiment, the second type of code is the code of the hotspot analysis report generation algorithm, which is used to generate the hotspot analysis report of the function call relationship tree.

[0092] The execution process of the second type of code will be described in detail in step 105.

[0093] As described in step 105, the program code after adding the first category of code and the second category of code is run to generate a hot spot analysis report of the function call relationship tree.

[0094] As described above, the first category code and the second category code added by means of insertion will be compiled into an executable file together, and then the program code after adding the first category code and the second category code will be run.

[0095] Specifically, this step includes:

[0096] Step 1051: during the execution of the program code, when the sub-function is called by other functions, using the first type code corresponding to the sub-function to determine whether the other function is a function in the function call relationship tree;

[0097] Step 1052: If yes, the first type code corresponding to the sub-function calculates the running time of calling the sub-function this time, and saves the running time to the leaf node corresponding to the sub-function; if no, the running time of calling the sub-function this time is not calculated;

[0098] Step 1053: After running the program code, use the second type of code to determine the hot spot distribution of each sub-function based on the running time of each sub-function called and the number of times the sub-function is called stored in each leaf node in the function call relationship tree.

[0099] It should be noted that each function in the function call relationship tree is only a part of the entire program code, that is, the code segment with the specified function selected by the user as the main function and its sub-functions. Therefore, in the process of running the entire program code, when a sub-function in the function call relationship tree is called by other functions, it may be a call to the sub-function by a function in the function call relationship tree, or it may be a call to the sub-function by a function outside the function call relationship tree. Since the function call relationship tree is to be subjected to hot spot analysis in this embodiment, it is not necessary to count the running time of the sub-function when the function outside the function call relationship tree calls the sub-function.

[0100] Therefore, as described in step 1051, during the execution of the program code, when the sub-function is called by other functions, the first type code corresponding to the sub-function is used to determine whether the other function is a function in the function call relationship tree.

[0101] As described in step 1052, if yes, the first type code corresponding to the sub-function calculates the running time of calling the sub-function this time, and saves the running time to the leaf node corresponding to the sub-function; if no, the running time of calling the sub-function this time is not calculated;

[0102] Continue to refer Figure 3The function call relationship tree shown in the figure has a root node numbered 1, and other leaf nodes are numbered according to the breadth-first principle. These numbers can be regarded as the global ID of each node in the function call relationship tree. Therefore, when a sub-function in the function call relationship tree is called by other functions, if the other functions are called by other functions outside the function call relationship tree, since no global ID is set, the first type of code corresponding to the sub-function will not calculate the running time of this call to the sub-function. On the contrary, if the other functions are called by other functions in the function call relationship tree, the first type of code corresponding to the sub-function calculates the running time of this call to the sub-function and saves the running time to the leaf node corresponding to the sub-function.

[0103] As described in step 1053, after the program code is run, the second type of code is used to determine the hot spot distribution of each sub-function based on the running time of each sub-function called and the number of times the sub-function is called stored in each leaf node in the function call relationship tree.

[0104] Specifically, after running the first type of code to save the running time of each sub-function in the function call relationship tree to the corresponding leaf node, the second type of code (i.e., the code of the hot spot analysis report generation algorithm) can be used to count the running time of each sub-function when called by other functions in the function call relationship tree during the running of the program code, as well as the number of times the sub-function is called, thereby determining the hot spot distribution of each sub-function.

[0105] Figure 4 FIG. 1 is a flow chart of another specific embodiment of the hot spot analysis method of program code provided by an embodiment of the present invention. Figure 4 , the hot spot analysis method comprises the following steps:

[0106] Step 401, statically analyzing the program code with a designated function as a main function to generate a function call relationship graph;

[0107] Step 402: destroy the circular call or recursive call appearing in the function call relationship graph to obtain a function call relationship tree; wherein the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function;

[0108] Step 403: adding a first type of code before and after each sub-function is called; wherein the first type of code is used to calculate the running time of the corresponding sub-function when it is called by the function in the function call relationship tree during the program code running process, and save the running time to the leaf node corresponding to the sub-function;

[0109] Step 404: adding a second type of code to the program code; wherein the second type of code is used to generate a hotspot analysis report according to the running time saved by each leaf node in the function call relationship tree and the number of times the corresponding sub-function is called;

[0110] Step 405: adding the first type of code before and after the calling of the main function; the first type of code is used to calculate the running time of each execution of the main function during the running of the program code, and save the running time to the root node; the second type of code is also used to determine the hot spot distribution of the main function according to the running time saved in the root node in the function call relationship tree and the number of times the main function is called;

[0111] Step 406: Run the program code after adding the first category code and the second category code to generate a hot spot analysis report of the function call relationship tree.

[0112] In this embodiment, step 401, step 402, step 403 and step 404 are the same as those described above. Figure 1 The embodiments described are the same, please refer to the above for details Figure 1 The embodiments of the present invention will not be described in detail here.

[0113] and Figure 1 The difference between the above embodiments is that, in this embodiment, step 405 is added to add the first type of code before and after the call of the main function. The first type of code is used to calculate the running time of the main function each time it is executed during the running of the program code, and save the running time to the root node. The second type of code is also used to determine the hotspot distribution of the main function based on the running time saved by the root node in the function call relationship tree and the number of times the main function is called.

[0114] It should be noted that the hotspot analysis method described in this embodiment needs to exclude the running time of the sub-function in the function call relationship tree when it is called by other functions (outside the function call relationship tree), but the running time of the main function in the function call relationship tree is still required to be calculated. Therefore, the first type of code is also added before and after the call of the main function. During the execution of the program code, the first type of code is used to calculate the running time of each execution of the main function and save the running time to the root node. Furthermore, the second type of code added to the program code is also used to determine the hotspot distribution of the main function based on the running time saved by the root node in the function call relationship tree and the number of times the main function is called.

[0115] Therefore, step 406 includes, during the running of the program code, using the first type of code corresponding to the main function to calculate the running time of each execution of the main function, and saving the running time to the root node. After running the program code, using the second type of code according to the running time of each execution of the main function saved in the root node in the function call relationship tree, to determine the hotspot distribution of the longest running time of the main function.

[0116] According to the needs of hot spot analysis, the generated hot spot analysis report includes the time consumption ratio of the main function including all sub-functions in the function call relationship tree (relative to the time consumption of the main function), as well as the number of calls, maximum time consumption, average time consumption, etc. Further, the hot spot analysis report will also count the longest running time of the main function, as well as the time consumption ratio of the sub-functions called at that time (i.e. the longest running time of the main function) and the sub-functions called by the sub-functions (relative to the maximum time consumption of the main function), as well as the number of calls, maximum time consumption and average time consumption of each sub-function at that time (i.e. the longest running time of the main function).

[0117] It should be noted that those skilled in the art can use programming technology to implement the code writing of the first category of codes and the second category of codes, and this embodiment does not limit the specific code implementation.

[0118] Furthermore, in this embodiment, a first type of deletion mark and a second type of deletion mark may be added to the program code. The first type of deletion mark and the second type of deletion mark are used to indicate the deletion of the first type of code and the second type of code respectively after generating a hot spot analysis report of the function call relationship tree, so that manual deletion is not required, further saving labor costs.

[0119] Figure 5 FIG. 1 is a schematic diagram of a specific embodiment of a hot spot analysis device for program code provided by an embodiment of the present invention. Figure 5The hotspot analysis device 5 includes: a static analysis module 51, which is used to perform static analysis on the program code with the specified function as the main function to generate a function call relationship graph. A function call relationship tree generation module 52, which is used to destroy the loop call or recursive call appearing in the function call relationship graph to obtain a function call relationship tree; wherein the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function. A first-category code adding module 53, which is used to add the first-category code before and after the call of each sub-function; wherein the first-category code is used to calculate the running time of the corresponding sub-function each time it is called by the function in the function call relationship tree during the program code running process, and save the running time to the leaf node corresponding to the sub-function. A second-category code adding module 54, which is used to add the second-category code to the program code; wherein the second-category code is used to generate a hotspot analysis report according to the running time saved by each leaf node in the function call relationship tree and the number of times the corresponding sub-function is called. A hotspot analysis module 55, which is used to run the program code after adding the first-category code and the second-category code to generate a hotspot analysis report of the function call relationship tree.

[0120] The function call relationship tree generation module 52 includes: a root node setting unit 521, which is used to set the main function as the root node of the function call relationship tree. A leaf node setting unit 522, which is used to set the first sub-function as the leaf node of the root node based on the function call relationship graph if the main function has a first sub-function. A loop structure judgment unit 523, which is used to judge whether the second sub-function is a loop call or a recursive call if the first sub-function also calls the second sub-function. A loop structure processing unit 524, which is used to set the second sub-function as the leaf node of the leaf node if the second sub-function is a loop call or a recursive call, and then stop traversing the call relationship of the second sub-function in the function call relationship graph; if the second sub-function is not a loop call or a recursive call, then set the second sub-function as the leaf node of the leaf node and continue traversing the function call relationship graph.

[0121] The function call relationship tree generation module 52 also includes: a call depth setting unit 525, which is used to set the maximum call depth. A maximum call depth judgment unit 526, which is used to judge whether the maximum call depth is reached if the first sub-function also calls the second sub-function. A call depth processing unit 527, which is used to set the second sub-function as the leaf node of the leaf node if the judgment result of the maximum call depth judgment unit is yes, and stop traversing the call relationship of the second sub-function in the function call relationship graph; if the judgment result of the maximum call depth judgment unit is no, set the second sub-function as the leaf node of the leaf node, and continue traversing the call relationship of the second sub-function in the function call relationship graph.

[0122] The hotspot analysis module 55 includes: a function call relationship judgment unit 551, which is used to use the first type of code corresponding to the sub-function to judge whether the other function is a function in the function call relationship tree when the sub-function is called by other functions during the execution of the program code. A running time calculation unit 552, which is used to calculate the running time of the sub-function this time by the first type of code corresponding to the sub-function if the judgment result of the function call relationship judgment unit is yes, and save the running time to the leaf node corresponding to the sub-function; if the judgment result of the function call relationship judgment unit is no, the running time of the sub-function this time is not calculated. A hotspot distribution processing unit 553, which is used to determine the hotspot distribution of each sub-function by using the second type of code according to the running time of each call of the sub-function and the number of times the sub-function is called stored in each leaf node in the function call relationship tree after the program code is executed.

[0123] The first-category code adding module 53 is also used to add the first-category code before and after the call of the main function; the first-category code is used to calculate the running time of the main function each time it is executed during the running of the program code, and save the running time to the root node; the second-category code is also used to determine the hotspot distribution of the main function based on the running time saved in the root node in the function call relationship tree and the number of times the main function is called.

[0124] The hotspot analysis module 55 is also used to calculate the running time of each execution of the main function using the first type of code corresponding to the main function during the execution of the program code, and save the running time to the root node; after running the program code, use the second type of code according to the running time of each execution of the main function saved in the root node in the function call relationship tree to determine the hotspot distribution of the longest running time of the main function.

[0125] In summary, according to the hot spot analysis method of the program code provided by the embodiment of the present invention, a function in the program code (the user can specify the function arbitrarily) is used as the main function for static analysis to obtain a function call relationship graph, and based on the function call relationship graph, the loop structure (i.e., the situation where a loop call or a recursive call occurs) is destroyed, thereby generating a function call relationship tree. Among them, the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function. Further, each node in the function call relationship tree is assigned a number, wherein the root node is numbered 1, and each leaf node is numbered in sequence according to the breadth-first principle.

[0126] The timing function (i.e., the first type of code) and the code of the hotspot analysis report generation algorithm (i.e., the second type of code) are added to the program code by the plugging method. In the process of running the program code, the code of the timing function can calculate the running time of each sub-function when it is called by the function in the function call relationship tree and save the running time to the corresponding leaf node. And since each node in the function call relationship tree is assigned a number, this number is used as the global ID of the sub-function in the function call relationship tree. Therefore, when the timing function is run, when other functions outside the function call relationship tree call the sub-function in the function call relationship tree, since the other function does not have the number in the function call relationship tree (i.e., there is no global ID), the timing function will not calculate the running time of the sub-function, thereby avoiding the interference of external calls on the hotspot analysis of the function call relationship tree. Furthermore, the code of the hotspot analysis report generation algorithm can generate a hotspot analysis report of the function call relationship tree according to the running time of each sub-function saved by each leaf node in the function call relationship tree.

[0127] Therefore, the hot spots of functions are analyzed based on the call relationship tree. The results are simple and intuitive, which can help developers quickly analyze the hot spots of program usage. Since the code for adding timing functions and the code for generating hot spot analysis reports in the program code are automatically completed through the instrumentation method, there is no need to add them manually, which saves time costs.

[0128] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A method for hot spot analysis of program code, characterized in that: include: Performing static analysis on the program code with a designated function as a main function to generate a function call relationship graph; Destroying the circular calls or recursive calls appearing in the function call relationship graph to obtain a function call relationship tree; wherein the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function; Adding a first type of code before and after each call of the sub-function; wherein the first type of code is used to calculate the running time of the corresponding sub-function when it is called by the function in the function call relationship tree during the execution of the program code, and save the running time to the leaf node corresponding to the sub-function; Adding a second type of code to the program code; wherein the second type of code is used to generate a hotspot analysis report according to the running time saved by each leaf node in the function call relationship tree and the number of times the corresponding sub-function is called; Running the program code after adding the first category of code and the second category of code to generate a hot spot analysis report of the function call relationship tree; Also includes: The first type of code is added before and after the call of the main function; the first type of code is used to calculate the running time of the main function each time it is executed during the running of the program code, and save the running time to the root node; the second type of code is also used to determine the hotspot distribution of the main function based on the running time saved in the root node in the function call relationship tree and the number of times the main function is called.

2. The method according to claim 1, characterized in that Destroying the loop call or recursive call appearing in the function call relationship graph to obtain a function call relationship tree comprises: Setting the main function as the root node of the function call relationship tree; Based on the function call relationship graph, if the main function has a first sub-function, setting the first sub-function as a leaf node of the root node; If the first sub-function also calls a second sub-function, determining whether the second sub-function is a loop call or a recursive call; If the second sub-function is a loop call or a recursive call, after setting the second sub-function as the leaf node of the leaf node, stop traversing the calling relationship of the second sub-function in the function call relationship graph; If the second sub-function is not a loop call or a recursive call, the second sub-function is set as the leaf node of the leaf node, and the function call relationship graph is continued to be traversed.

3. The method according to claim 2, characterized in that The destroying the cyclic calls or recursive calls appearing in the function call relationship graph to obtain the function call relationship tree also includes: Set the maximum call depth; If the first sub-function also calls the second sub-function, determining whether the maximum calling depth is reached; If so, after setting the second sub-function as the leaf node of the leaf node, stop traversing the calling relationship of the second sub-function in the function call relationship graph; if not, after setting the second sub-function as the leaf node of the leaf node, continue traversing the calling relationship of the second sub-function in the function call relationship graph.

4. The method according to claim 1, characterized in that The step of running the program code after adding the first category of code and the second category of code to generate a hotspot analysis report of the function call relationship tree includes: During the execution of the program code, when the sub-function is called by other functions, the first type of code corresponding to the sub-function is used to determine whether the other function is a function in the function call relationship tree; If yes, the first type of code corresponding to the sub-function calculates the running time of calling the sub-function this time, and saves the running time to the leaf node corresponding to the sub-function; if no, the running time of calling the sub-function this time is not calculated; After running the program code, the second type of code is used to determine the hot spot distribution of each sub-function according to the running time of each call of the corresponding sub-function and the number of calls of the sub-function saved in each leaf node in the function call relationship tree.

5. The method according to claim 1, characterized in that The step of running the program code after adding the first category of code and the second category of code to generate a hotspot analysis report of the function call relationship tree further includes: During the execution of the program code, using the main function corresponding to the first type of code to calculate the running time of each execution of the main function, and saving the running time to the root node; After the program code is run, the second type of code is used to determine the hotspot distribution of the longest running time of the main function according to the running time of each execution of the main function saved in the root node of the function call relationship tree.

6. A hot spot analysis device for program code, characterized in that: include: A static analysis module, used to perform static analysis on the program code with a specified function as the main function to generate a function call relationship graph; A function call relationship tree generation module, used to destroy the circular calls or recursive calls appearing in the function call relationship graph to obtain a function call relationship tree; wherein the root node of the function call relationship tree corresponds to the main function, and each leaf node corresponds to a sub-function; A first-class code adding module is used to add the first-class code before and after each call of the sub-function; wherein the first-class code is used to calculate the running time of each time the corresponding sub-function is called by the function in the function call relationship tree during the program code running process, and save the running time to the leaf node corresponding to the sub-function; A second type of code adding module is used to add the second type of code to the program code; wherein the second type of code is used to generate a hotspot analysis report according to the running time saved by each leaf node in the function call relationship tree and the number of times the corresponding sub-function is called; A hotspot analysis module, used for running the program code after adding the first category of code and the second category of code to generate a hotspot analysis report of the function call relationship tree; The first-category code adding module is also used to add the first-category code before and after the call of the main function; the first-category code is used to calculate the running time of the main function each time it is executed during the running of the program code, and save the running time to the root node; the second-category code is also used to determine the hotspot distribution of the main function based on the running time saved in the root node in the function call relationship tree and the number of times the main function is called.

7. The device according to claim 6, characterized in that The function call relationship tree generation module includes: A root node setting unit, used for setting the main function as the root node of the function call relationship tree; A leaf node setting unit, configured to set the first sub-function as a leaf node of the root node if the main function has a first sub-function based on the function call relationship graph; A loop structure determination unit, configured to determine whether the second sub-function is a loop call or a recursive call if the first sub-function also calls the second sub-function; A loop structure processing unit is used to set the second sub-function as the leaf node of the leaf node if the second sub-function is a loop call or a recursive call, and then stop traversing the calling relationship of the second sub-function in the function call relationship graph; if the second sub-function is not a loop call or a recursive call, then set the second sub-function as the leaf node of the leaf node, and then continue traversing the function call relationship graph.

8. The device according to claim 7, characterized in that The function call relationship tree generation module also includes: A call depth setting unit is used to set the maximum call depth; a maximum call depth determination unit, configured to determine whether the maximum call depth is reached if the first sub-function also calls the second sub-function; A calling depth processing unit is used to set the second sub-function to the leaf node of the leaf node and stop traversing the calling relationship of the second sub-function in the function call relationship graph if the judgment result of the maximum calling depth judgment unit is yes; if the judgment result of the maximum calling depth judgment unit is no, set the second sub-function to the leaf node of the leaf node and continue traversing the calling relationship of the second sub-function in the function call relationship graph.

9. The device according to claim 6, characterized in that The hot spot analysis module includes: A function call relationship judgment unit, used for, during the execution of the program code, when the sub-function is called by other functions, using the first type code corresponding to the sub-function to judge whether the other function is a function in the function call relationship tree; A running time calculation unit, for, if the judgment result of the function call relationship judgment unit is yes, the first type code corresponding to the sub-function calculates the running time of the current call of the sub-function, and saves the running time to the leaf node corresponding to the sub-function; if the judgment result of the function call relationship judgment unit is no, the running time of the current call of the sub-function is not calculated; A hotspot distribution processing unit is used to determine the hotspot distribution of each sub-function after running the program code, using the second type of code according to the running time of each sub-function called and the number of times the sub-function is called saved in each leaf node in the function call relationship tree.

10. The device according to claim 6, characterized in that The hot spot analysis module is further used to calculate the running time of each execution of the main function by using the main function corresponding to the first type of code during the running of the program code, and save the running time to the root node; After the program code is run, the second type of code is used to determine the hotspot distribution of the longest running time of the main function according to the running time of each execution of the main function saved in the root node of the function call relationship tree.

11. A computer-readable storage medium storing a computer program, wherein the computer program is used to execute the hotspot analysis method of the program code in any one of claims 1 to 5.

12. An electronic device, comprising: processor; a memory for storing instructions executable by the processor; The processor is used to read the executable instructions from the memory and execute the instructions to implement the hot spot analysis method of the program code in any one of claims 1-5.

Citation Information

Patent Citations

  • A function call tree generation method and device

    CN109933365A