A method for automatic detection of performance defects
By providing custom label rules and building inter-process control flow diagrams with labels, the problem of immature software performance defect detection in the prior art is solved, and automatic detection and repair of performance defects in C/C++ code is realized, and detection efficiency and accuracy are improved.
Patent Information
- Application Number
- CN202210060556.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-19
- Publication Date
- 2025-04-22
- Estimated Expiration
- 2042-01-19
AI Technical Summary
The prior art is not yet mature in detecting and repairing software performance defects, the breadth of static analysis detection is limited, while dynamic analysis consumes too high, making it difficult to effectively detect and repair performance defects.
Provide a common interface that allows users to customize the rules for building tags, automatically generate the corresponding tags for each instruction, and automatically detect performance defects by building labeled inter-process control flow diagrams and constraints.
It realizes automatic detection of potential performance defects in C/C++ code, provides a scalable unified interface, can detect a wider range of performance defect problems, and improves detection efficiency and accuracy.
Smart Images

Figure CN114490369B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a static analysis technology of a program and belongs to the field of software engineering. Background Art
[0002] Software performance is an aspect that developers pay most attention to besides the software functions. The defects caused by software performance defects refer to the fact that the memory requirements or running time of the software during operation exceed the objectively provided resources or do not meet the needs of users. Even very famous project software has many such performance defects and has caused serious losses. However, the existing detection or repair methods for software performance defects are still in their early stages and are not very mature compared to the detection and repair of functional defects. At present, the detection and repair of software performance defects are mainly in the following aspects. The first is static analysis, which detects or repairs a specific performance defect pattern, such as repeated traversal of data structures, unnecessary execution of code blocks under specific conditions, etc., so the detection breadth is limited. The other major category is dynamic analysis methods. Due to the advantage of dynamic analysis, it can accurately track the operation of the program, and performance defects are often related to repeated memory reading and writing or repeated execution of code. Therefore, dynamic analysis detection or repair of performance defects is more work than static analysis. Most of this kind of work is to read the program's memory access within a period of time and detect whether it is redundant. Some work explicitly constructs a value dependency graph to represent the value dependency relationship during the program running process, but the consumption of dynamic analysis is too high. Even if some optimization methods are adopted, such as sampling, reducing the size of the value dependency graph, etc., its consumption is far greater than static analysis technology.
[0003] Due to the flexibility of programming languages, developers have different programming foundations and programming styles, which leads to various performance problems in programs. At present, the technology based on static analysis and dynamic testing needs to design detection methods for defect patterns, which makes the development of detection tools difficult and the detection of performance defects lags behind. Context-free grammar-based reachability analysis is a solution. It is a static analysis technology that converts the defect pattern into a specific grammar and converts the detection problem into a graph reachability problem. However, there are several problems with using context-free grammars: First, although there are polynomial time algorithms to solve graph reachability problems, different defects have different grammars, and there is no mature tool that provides a unified interface that allows users to define different context-free grammars and detect them; second: not all defects can be represented by a context-free grammar. For code performance defects, they are often related to the repeated execution of certain code instructions. These repeated executions cause a waste of computing resources. Therefore, if there is a grammar, this grammar should need the feature of "recording the number of executions". However, context-free grammars cannot achieve "counting" in the general sense. Therefore, for this type of defect, there is no corresponding context-free grammar. Therefore, based on some performance defect problems, the present invention summarizes their similarities and can be used to detect a wider class of defects. Summary of the invention
[0004] In view of the technical problems existing in the prior art, the present invention aims to solve these two challenges.
[0005] In order to achieve the above-mentioned purpose of the invention, the technical solution adopted by the present invention is: providing a universal interface, allowing users to customize the rules for constructing labels, the present invention will automatically generate labels corresponding to each instruction, as long as rules for generating specific labels on some specific instructions are formulated, the present invention can generate labeled inter-procedural control flow graphs according to the rules; secondly, allowing customization of constraints on constructed labels, these constraints are generally divided into two categories: position and number constraints, when a sequence composed of labels is generated, then the positions and numbers of all its labels are determined, the present invention can detect whether this sequence meets the constraints. In essence, this is to delete some grammar sequences that do not meet the constraints from a large and imprecise context-free grammar through the establishment of constraints, thereby obtaining a grammar that meets the defects. Because of this mechanism, the framework can consider the numerical relationship between labels, which is impossible for all context-free grammar reachability analyses.
[0006] The present invention provides a method for automatically detecting performance defects, which is used to automatically detect potential performance defects in C / C++ codes, comprising the following steps:
[0007] Step 1: Based on the code features of the performance defect, define a label that can be used to represent the performance defect. Based on the statement sequence where the performance defect occurs, construct a label constraint set that represents the behavioral features of the performance defect. The specific steps are as follows:
[0008] Step 1.1: Based on the code features of performance defects, define all label objects used to express performance defects. The label object is represented by a string.
[0009] Step 1.2: Define instruction-based label construction rules. For the statement sequence where the performance defect problem to be detected occurs, that is, each instruction, generate a label object related to this instruction. The label is an abstract description of the instruction, describing the behavior of the instruction.
[0010] Step 1.3: Define four atomic constraints for constructing complex constraints of labels, namely number constraint: obtain the number of a label, position constraint: obtain the position of a label, matching constraint: determine whether two labels "match", and neighbor constraint: indicate that the positions of two labels are adjacent.
[0011] Step 1.4: Define composite constraints. Use the four atomic constraints in step 1.3 to build more complex constraints to describe the characteristics of defects, namely logical constraints: a constraint is composed of logical AND, logical OR and logical NOT to form a composite constraint, and Boolean constraints: two constraints are composed of a series of mathematical operations and return true or false.
[0012] Step 1.5: Construct a corresponding label sequence based on the statement sequence when the detected performance defect occurs. By analyzing this label sequence, use the composite constraint in step 1.4 to construct a constraint set that describes the characteristics of the performance defect.
[0013] Step 2: Perform program analysis on the source code to construct an inter-procedural control flow graph, and introduce performance labels based on the control flow graph and the rules constructed by the labels in step 1 to construct an inter-procedural control flow graph with labels. The specific steps are as follows:
[0014] Step 2.1: Construct a labeled inter-procedural control flow graph. A labeled inter-procedural control flow graph means that each node has several labels. Based on step 1, a certain number of labels are generated for each node of the control flow graph according to the construction rules.
[0015] Step 2.2: Compress the labeled inter-procedural control flow graph generated in step 2.1. This is because there may be no labels on many nodes of the graph, so these nodes are deleted after step 2.1. The specific process is: for nodes with empty labels, if the out-degree and in-degree of the node are both 1, directly connect the predecessor node to the successor node, and delete this node. Because each node in the labeled inter-procedural control flow graph generated by the present invention corresponds to an instruction, there are many redundant instructions, which is conducive to speeding up the subsequent detection steps.
[0016] Step 2.3: Attach the constructed constraints to the labeled inter-procedural control flow graph, indicating the constraints that the labels need to satisfy on the graph.
[0017] Step 3: Based on the constraint set in step 1, find a path that satisfies the constraints on the labeled inter-procedural control flow graph generated in step 2. The process is as follows: find a path in the graph by depth-first traversal, collect a set of sequences consisting of labels generated by the path, and determine whether the sequence satisfies the conditions given by the constraints based on the consistency between the label sequence and the label constraint set, that is, whether there is a performance defect, so as to report the corresponding performance defect and realize automatic detection of performance defects. The specific steps are as follows:
[0018] Step 3.1: Based on the labeled inter-procedural control flow graph and constraint set constructed in the above steps, perform a depth-first traversal of the graph to obtain all labels of the current node.
[0019] Step 3.2: For each label in step 3.1, obtain the constraints containing this label and make judgments for each constraint separately.
[0020] Step 3.3: Assume that a constraint in step 3.2 is an atomic constraint. If the constraint is a number constraint, obtain its number. If the constraint is a neighbor relationship, obtain its neighbor label. If the constraint is a position constraint, obtain its position. If the constraint is a matching constraint, the processing is slightly different. The present invention initializes a stack for each pair of matching labels. Each time a match is required, the stack is pushed or popped. When the stack is empty, it indicates that no match can be made.
[0021] Step 3.4: If the constraint is a composite constraint, the result is the corresponding combination of the atomic constraint judgment results that make up them. For example, if the constraint C=C1&&C2 is true, it means that constraints C1 and C2 are both true on the same path.
[0022] Step 3.5: After detecting a path that satisfies all the constraints as above, the path (the path consisting of all nodes) can be stored and reported.
[0023] Beneficial effect: The method for automatic detection of performance defects of the present invention mainly provides an extensible, unified interface for detecting a class of C / C++ program performance defect problems. This class of performance defect problems can be represented by the characteristics of the performance defect (the characteristics are represented by labels) and the constraints imposed on the characteristics, and a unified algorithm is provided for solving it. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 It is a framework diagram of a method for automatic detection of performance defects implemented by the present invention.
[0025] Figure 2 This is a sample of a labeled inter-procedural control flow graph for a performance defect problem "variable duplicate writing" generated by the implementation of the present invention.
[0026] Figure 3 The present invention constructs a defect set of a performance defect problem "variable repeated writing". DETAILED DESCRIPTION
[0027] The specific embodiments of the present invention are described in more detail below.
[0028] Performance problems in software systems during operation may be due to performance defects in the code, such as redundant operations, repeated variable assignments, etc. The present invention proposes a method for automatically detecting potential performance defects in C / C++ codes. Figure 1 The overall framework of an automatic detection method for performance defects is shown, and its specific implementation steps are:
[0029] Step 1: Based on the code features of the performance defect, define a label that can be used to represent the performance defect. Based on the statement sequence where the performance defect occurs, construct a label constraint set that represents the behavioral features of the performance defect, as shown in the attached figure. Figure 2 With attached Figure 3 As shown, the specific steps are as follows:
[0030] Step 1.1: Based on the code features of performance defects, define all label objects used to express performance defects. The label object is represented by a string.
[0031] Step 1.2: Define instruction-based label construction rules. For the statement sequence where the performance defect problem to be detected occurs, that is, each instruction, generate a label object related to this instruction. The label is an abstract description of the instruction, describing the behavior of the instruction.
[0032] Step 1.3: Define four atomic constraints for constructing complex constraints of labels, namely number constraint: obtain the number of a label, position constraint: obtain the position of a label, matching constraint: determine whether two labels "match", and neighbor constraint: indicate that the positions of two labels are adjacent.
[0033] Step 1.4: Define composite constraints. Use the four atomic constraints in step 1.3 to build more complex constraints to describe the characteristics of defects, namely logical constraints: a constraint is composed of logical AND, logical OR and logical NOT to form a composite constraint, and Boolean constraints: two constraints are composed of a series of mathematical operations and return true or false.
[0034] Step 1.5: Construct a corresponding label sequence based on the statement sequence when the detected performance defect occurs. By analyzing this label sequence, use the composite constraint in step 1.4 to construct a constraint set that describes the characteristics of the performance defect.
[0035] Step 2: Perform program analysis on the source code to construct an inter-procedural control flow graph, and introduce performance labels based on the control flow graph and the rules constructed by the labels in step 1 to construct an inter-procedural control flow graph with labels. The specific steps are as follows:
[0036] Step 2.1: Construct a labeled inter-procedural control flow graph. A labeled inter-procedural control flow graph means that each node has several labels. Based on step 1, a certain number of labels are generated for each node of the control flow graph according to the construction rules.
[0037] Step 2.2: Compress the labeled inter-procedural control flow graph generated in step 2.1. This is because there may be no labels on many nodes of the graph, so these nodes are deleted after step 2.1. The specific process is: for nodes with empty labels, if the out-degree and in-degree of the node are both 1, directly connect the predecessor node to the successor node, and delete this node. Because each node in the labeled inter-procedural control flow graph generated by the present invention corresponds to an instruction, there are many redundant instructions, which is conducive to speeding up the subsequent detection steps.
[0038] Step 2.3: Attach the constructed constraints to the labeled inter-procedural control flow graph, indicating the constraints that the labels need to satisfy on the graph.
[0039] Step 3: Based on the constraint set in step 1, find a path that satisfies the constraints on the labeled inter-procedural control flow graph generated in step 2. The process is as follows: find a path in the graph by depth-first traversal, collect a set of sequences consisting of labels generated by the path, and determine whether the sequence satisfies the conditions given by the constraints based on the consistency between the label sequence and the label constraint set, that is, whether there is a performance defect, so as to report the corresponding performance defect and realize automatic detection of performance defects. The specific steps are as follows:
[0040] Step 3.1: Based on the labeled inter-procedural control flow graph and constraint set constructed in the above steps, perform a depth-first traversal of the graph to obtain all labels of the current node.
[0041] Step 3.2: For each label in step 3.1, obtain the constraints containing this label and make judgments for each constraint separately.
[0042] Step 3.3: Assume that a constraint in step 3.2 is an atomic constraint. If the constraint is a number constraint, obtain its number. If the constraint is a neighbor relationship, obtain its neighbor label. If the constraint is a position constraint, obtain its position. If the constraint is a matching constraint, the processing is slightly different. The present invention initializes a stack for each pair of matching labels. Each time a match is required, the stack is pushed or popped. When the stack is empty, it indicates that no match can be made.
[0043] Step 3.4: If the constraint is a composite constraint, the result is the corresponding combination of the atomic constraint judgment results that make up them. For example, if the constraint C=C1&&C2 is true, it means that constraints C1 and C2 are both true on the same path.
[0044] Step 3.5: After detecting a path that satisfies all the constraints as above, the path (the path consisting of all nodes) can be stored and reported.
[0045] The above embodiments are only for explaining the technical ideas of the present invention, and cannot be used to limit the protection scope of the present invention. Any changes made on the basis of the technical solution according to the technical ideas proposed by the present invention shall fall within the protection scope of the present invention. The technologies not involved in the present invention can be implemented by existing technologies.
Claims
1. A method for automatically detecting performance defects, for automatically detecting potential performance defects in C / C++ code, the method comprising the following steps: Step 1: Based on the code features of the performance defect, define a label that can be used to represent the performance defect, and based on the statement sequence where the performance defect occurs, build a label constraint set that represents the behavioral features of the performance defect; The step 1 is specifically as follows: Step 1.1: Based on the code features of performance defects, define all label objects used to express performance defects. The label object is represented by a string. Step 1.2: Define the label construction rules based on instructions. For the statement sequence where the performance defect problem to be detected occurs, that is, each instruction, generate a label object related to this instruction. The label is an abstract description of the instruction, describing the behavior of the instruction. Step 1.3: Define four atomic constraints for building complex constraints of labels, namely, number constraint: get the number of a label, position constraint: get the position of a label, match constraint: determine whether two labels "match", and neighbor constraint: indicate that the positions of two labels are adjacent; Step 1.4: Define composite constraints. Use the four atomic constraints in step 1.3 to build more complex constraints to describe the characteristics of defects, namely logical constraints: a constraint is formed by logical AND, logical OR and logical NOT to form a composite constraint, and Boolean constraints: two constraints are formed by a series of mathematical operations and return true or false; Step 1.5: Construct a corresponding label sequence based on the statement sequence when the detected performance defect occurs. By analyzing this label sequence, use the composite constraint in step 1.4 to construct a constraint set that describes the characteristics of the performance defect. Step 2: Perform program analysis on the source code to construct an inter-procedural control flow graph, and introduce performance labels based on the control flow graph and the rules constructed by the labels in step 1 to construct an inter-procedural control flow graph with labels; The step 2 is specifically as follows: Step 2.1: Construct a labeled inter-procedural control flow graph. A labeled inter-procedural control flow graph means that each node has a number of labels. Based on step 1, a number of labels are generated for each node of the control flow graph according to the construction rules; Step 2.2: compress the labeled inter-procedural control flow graph generated in step 2.
1. This is because there may be no labels on many nodes of the graph, so these nodes are deleted after step 2.
1. The specific process is: for nodes with empty labels, if the out-degree and in-degree of the node are both 1, directly connect the predecessor node with the successor node and delete the node. Because each node of the labeled inter-procedural control flow graph generated by the present invention corresponds to an instruction, there are many redundant instructions, which is conducive to speeding up the subsequent detection steps; Step 2.3: Attach the constructed constraints to the labeled interprocedural control flow graph, indicating the constraints that the labels need to satisfy on the graph; Step 3: Based on the constraint set in step 1, find a path that satisfies the constraints on the labeled inter-procedural control flow graph generated in step 2. The process is as follows: find a path in the graph through depth-first traversal, collect a set of sequences composed of labels generated by the path, and judge whether the sequence satisfies the conditions given by the constraints based on the consistency between the label sequence and the label constraint set, that is, whether there is a performance defect, thereby reporting the corresponding performance defect and realizing automatic detection of performance defects.
2. A method for automatic detection of performance defects according to claim 1, characterized in that: The step 3 is as follows: Step 3.1: Based on the labeled inter-procedural control flow graph and constraint set constructed in the above steps, perform a depth-first traversal of the graph to obtain all labels of the current node; Step 3.2: For each label in step 3.1, obtain the constraints containing this label, and make judgments for each constraint separately; Step 3.3: Assume that a constraint in step 3.2 is an atomic constraint. If the constraint is a number constraint, get its number. If the constraint is a neighbor relationship, get its neighbor label. If the constraint is a position constraint, get its position. If the constraint is a matching constraint, the processing is slightly different. For each pair of matching labels, initialize a stack. Each time a match is required, push or pop the stack. When the stack is empty, it indicates that the match cannot be made. Step 3.4: If the constraint is a composite constraint, then the result is the corresponding combination of the atomic constraint judgment results that make up them. True to indicate a constraint and constraints are all true on the same path; Step 3.5: After detecting a path that satisfies all the constraints as above, the path consisting of all nodes is stored and reported.
3. The method for automatic detection of performance defects according to claim 1, characterized in that: The four atomic constraint features proposed in step 1.3 support feature checking of the number of labels, thus helping to detect performance defects; the four atomic constraints are: 1) Number constraint, given a label, obtain its number, denoted as ,2) Position constraint, denoted as ,3) Matching constraint, given two labels, determine whether the two labels are matched. Matching means that they satisfy the "bracket" matching relationship, recorded as ,4) Nearest neighbor constraint, given two labels, and an integer , determine whether the difference between their positions is equal to this integer , denoted as .
Citation Information
Patent Citations
Test method and system based on software defect mode
CN101286132A
Test based static analysis misinformation eliminating method
CN103294594A