Using Code Coverage with Optimized Programs. Compuware Code Coverage
Xpediter/Code Coverage helps you understand the quality of your code and make sure the tests you run are thorough enough to cover every part of it. You'll be able to see which parts of the code are being exercised by your testing, and which are not. This will allow you to fix any potential problems before your application goes live and prevent any costly errors that could disrupt your operations.
Advertisement
Advertisement
6-1
Chapter 6.
Using Code Coverage with Optimized Programs
This chapter discusses certain considerations that apply to the use of Xpediter/Code
Coverage with programs that have been compiled using an optimizing compiler.
Overview
When programmers are building applications, they tend to envision the program logic in terms of the high-level language in which they plan to do the coding. The completed source code is translated by a compiler into machine instructions, which are then normally stored in a library as an executable load module. During execution of the module, it can be difficult to tell whether the program was written in COBOL, Assembler,
PL/I, or some other language. One of the primary functions of Code Coverage is to map those compiler-generated machine instructions back to the high-level language of the source code in order to identify which parts of the application have been executed during testing.
When Code Coverage collects data during program testing, it uses information obtained from the compiler to relate the machine instructions back to their corresponding source lines. If a non-optimizing compiler was used, there is typically a one-to-one relationship between machine instructions and source lines. The output from an optimizing compiler, however, may lack that one-to-one correspondence between source and machine code.
For this reason, when Code Coverage data is collected for a program that has been compiled with an optimizing compiler, it can sometimes seem as though the results don’t make sense. The following sections explain why this happens and the ways in which
Code Coverage attempts to clarify those results.
Optimization Benefits and Methods
A non-optimizing compiler individually translates each statement or verb into one or more corresponding machine instructions. An optimizing compiler, however, examines several source lines at one time, then builds a more efficient series of machine code instructions. The resulting load module will exhibit better resource utilization. It may be more compact, execute more quickly, or both.
Three of the more common optimization techniques compilers employ include:
1. Not generating machine code for a source line if it can never be reached or its purpose has already been accomplished.
In this example,
100 MOVE 10 TO AMOUNT.
200 IF AMOUNT > 20
300 ADD 15 TO SURCHARGE.
400 MOVE 10 TO AMOUNT.
the compiler might decide that because line 200 will always evaluate to false, there is no need to generate machine code for lines 200 and 300. In addition, it might decide not to generate code for line 400, since the operation requested there was already accomplished in line 100.
2. Merging operations from multiple source lines into a single machine instruction.
6-2 Xpediter/Code Coverage Mainframe User/Reference Guide
Consider the following source code:
100 01 OUTPUT-REC.
110 02 TYPE-CODE PIC XX.
120 02 CUST-NUM PIC X(5).
130 02 STATE PIC X(2).
.
.
.
500 MOVE ’AA’ TO TYPE-CODE.
600 MOVE ’10331’ TO CUST-NUM.
700 MOVE ’IA’ TO STATE.
Instead of generating three machine instructions, the compiler could generate one machine instruction to accomplish the assignments requested in lines 500 through
700.
3. Relocating all or part of the logic required to accomplish a source operation.
In the following example,
100 PERFORM UNTIL REC-SUB < 10
200 MOVE TABLE-VAL (REC-SUB) TO OUT-VALUE
300 MOVE CUR-DATE TO OUT-DATE
400 WRITE PRINT-LINE FROM OUT-LINE AFTER 1
500 ADD 1 TO REC-SUB
600 END-PERFORM.
it would be redundant to execute line 300 on each iteration of the perform loop, so the compiler might relocate the machine code for that line prior to the machine code for line 100.
When any of these optimization techniques are used, however, the one-to-one correspondence between source instructions and machine code sequences is likely to break down. The load module produced by an optimizing compiler may contain no logic for some source instructions and multiple, identical instances of machine code for others. In fact, the logical flow of the load module may bear little resemblance to that of the original high-level language program.
Difficulties Caused by Optimization
When Code Coverage analyzes the execution of an optimized program, it faces the same challenge as a programmer analyzing dump output after an abend: determining which line of the source code was being executed. Relating a memory offset to a source line can be nearly impossible when statements or verbs have been eliminated, moved, dissected, or combined.
There are two reasons for this difficulty:
1. The optimization technique used by the compiler may appear to vary from line to line or section to section.
Looking at the same example from above,
100 MOVE 10 TO AMOUNT.
200 IF AMOUNT > 20
300 ADD 15 TO SURCHARGE.
400 MOVE 10 TO AMOUNT.
it is practically impossible to predict what the compiled machine instructions will be.
Theoretically, line 100 is the only instruction that would be kept. But even though the compiler doesn’t need to generate code for lines 200 through 400, it may:
– keep the IF logic for line 200 but eliminate line 300.
– suppress the generation of machine code for line 400.
Using Code Coverage with Optimized Programs 6-3
– not perform any optimization and instead generate distinctive code for each of the four source lines.
2. The compiler produces almost no specific information on what was optimized and how.
For example, when an optimizing compiler suppresses the generation of machine code for a particular line of source code, it will do one of two things:
– Eliminate all references to the source line from its summary reports.
– Indicate that the underlying logic for the source line — which was never actually generated — occurs at an offset shared with another source line.
It would seem reasonable to assume that when a source line is completely eliminated from a compiler report, no code is generated for that line. But when two lines of source code map to the same offset, it does not necessarily imply that the logic for one of them was eliminated. In fact, those lines may be combined into a single machine instruction that represents the logic of both source lines.
This inconsistency and lack of compiler information make it hard to illustrate the actual logic flow or summarize execution coverage using the source program as a template.
Code Coverage Statistics
Xpediter/Code Coverage tracks executable COBOL verbs and branches, PL/I statements, and Assembler instructions. Code Coverage defines an executable statement, verb, or instruction as a source line for which underlying machine code has been generated by the compiler and a branch as one of two paths taken for certain conditional verbs.
Therefore, you cannot determine with any certainty the number of executable verbs and branches just by looking at a source code image, because the concepts themselves are by definition tied to the machine code generated by a compiler.
In fact, the number of executable verbs derived from a source program can vary based on the compiler used and the compile options chosen. If a non-optimizing compiler is used, the traditional one-to-one correspondence between source verbs and machine code sequences makes it easy to determine the number of verbs and branches by simply counting them in the source code. With optimized code, however, a source line might not have any underlying code and therefore wouldn’t be considered a verb by Code
Coverage. Conversely, the underlying code generated for a single line of source code might be duplicated and exist at two distinct offsets. In that case, one source line would be counted as two executable verbs, each with the potential to be executed or not.
Branch coverage statistics for optimized programs can also be confusing. Although a conditional source line may have underlying machine code — implying that two potential paths are included in the branch coverage tally — the compiler may have generated machine code in such a way that only one path could ever be taken, no matter what data passes through the program. In such cases, it is logically impossible to achieve
100% coverage.
With this in mind, you can see how a Code Coverage report might vary depending on the compiler and compile options used. With a non-optimizing compiler, we might see
machine code generation similar to that shown in Table 6-1:
Table 6-1.
Machine Code from Non-Optimizing Compiler
Source Line Machine Code Generated
100 MOVE 10 TO AMOUNT.
D2016013D188
200 IF AMOUNT > 20
D5016013D18A
47D0B284
300 ADD 15 TO SURCHARGE
F273D2106022
FA20D215C066
F3326022D215
6-4 Xpediter/Code Coverage Mainframe User/Reference Guide
Table 6-1.
Machine Code from Non-Optimizing Compiler
Source Line Machine Code Generated
400 MOVE 10 TO AMOUNT.
D2016013D188
For this portion of the program, the Code Coverage report would reflect four verbs and one conditional with its two branches.
If optimization is employed during compilation, however, the output can be quite
different, as shown in Table 6-2:
Table 6-2.
Machine Code from Optimizing Compiler
Source Line Machine Code Generated
100 MOVE 10 TO AMOUNT.
D2016013D188
200 IF AMOUNT > 20 None
300 ADD 15 TO SURCHARGE None
400 MOVE 10 TO AMOUNT.
None
For this portion of the program, the Code Coverage report would reflect just one verb and no branches from conditionals.
Optimization, Inferences, and Annotation
Because an optimized load module may in no way resemble the flow and logic of its original source code, annotated listings showing Code Coverage results for optimized programs may often look peculiar. It can seem strange to see a single COBOL MOVE verb marked as executed when the verbs immediately before and after it are marked unexecuted.
How could this happen? With optimization, it may be due to the fact that the machine instructions generated on behalf of that executed MOVE verb ended up nowhere near the instructions generated for the surrounding unexecuted verbs.
In the Code Coverage annotated listing for a non-optimized program, every executable source line is marked E (executed), U (unexecuted), or Bn (branch). To make listings for optimized programs more readable, two special forms of annotation have been added to designate lines of source code for which there is no directly related machine code in the load module: the letter X and an asterisk (*). Even though such source lines are annotated in the listing, they are excluded from counts and percentages in Code Coverage summary
reports. For detailed descriptions of the Code Coverage annotated listing flags, see Table
Source lines annotated with the letter X have been completely “optimized out” of the compiler output. As discussed earlier, this may have been done because the compiler concluded that the operation called for in that source line was redundant, that the line was unreachable, or that its execution depended on a condition that would never occur.
An asterisk (*) designates a line of source code that has been “optimized out” but shares an offset with another source line. The compiler doesn’t indicate whether logic to support the source line’s operation was completely omitted from the load module or just integrated with the machine code supporting another source line. It does, however, provide an offset associated with the line.
When multiple verbs or statements share an offset, only the last one is included in the
Code Coverage summary counts and annotated as a standard verb or statement with an E or U. Code Coverage infers that any other source lines with the same offset must have the same execution status, so it marks them with the same E or U as the last statement and adds an asterisk to indicate that they share a collective offset. A verb or statement
Using Code Coverage with Optimized Programs 6-5 annotated E* is associated with a collective offset, and the machine instruction at that offset was executed. A verb or statement annotated U* also has a shared offset, but the machine code at that offset was not executed.
Here is another example. If four MOVE verbs assign constants to contiguous fields, an optimizing compiler might generate just one machine instruction to accomplish the same thing. Assuming execution reached this instruction sequence, the annotated listing produced by Code Coverage would look like this:
E* 500 MOVE ’11’ TO FIELD-1.
E* 600 MOVE ’ABC’ TO FIELD-2.
E* 700 MOVE ’X’ TO FIELD-3.
E 800 MOVE ’901’ TO FIELD-4.
All four lines share the same offset, and the machine code at that offset was executed.
Conclusion
Optimizing compilers introduce certain idiosyncrasies that can make Code Coverage results difficult to understand. If you require a more direct representation of your program’s execution, simply recompile it without optimization. It is generally preferable, however, to perform testing on the actual load module that will eventually be moved into production.
Even though an annotated listing may be confusing, it is important to remember that the summarized counts and percentages reported by Xpediter/Code Coverage are an accurate reflection of the executed load module structure. In fact, the Code Coverage annotated listing can provide unique insights into areas of unreachable or unnecessary source code.
6-6 Xpediter/Code Coverage Mainframe User/Reference Guide
Download
Advertisement
Key features
Code coverage analysis
Report generation
Mainframe platform support
Multiple programming language support
Frequently asked questions
Xpediter Code Coverage is a software tool that helps you to test and analyze the code coverage of your mainframe applications.
It collects data about the code coverage of your programs and generates reports that show you which parts of your code have been executed and which parts have not been executed.
It supports a variety of mainframe platforms, including z/OS, z/VM, and z/VSE.
It supports a variety of programming languages, including COBOL, PL/I, and Assembler.