OSDN Git Service

Support procedures for testing profile-directed optimizations.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / profopt.exp
1 #   Copyright (C) 2001 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
16 #
17 # This script was submitted by Janis Johnson <janis187@us.ibm.com>.
18
19 # Test the functionality and optionally, performance improvement, of
20 # programs compiled with profile-directed optimizations.  Compile and
21 # run a test with profile options, compile it with options using the
22 # profile feedback, and then run the test again.  Optionally compile
23 # and run a third time without the profile-directed optimization and
24 # compare timing results of the program with normal optimization and
25 # with the profile-directed optimization. Each test is run using
26 # multiple sets of optimization and/or code generation options in
27 # addition to the profiling and feedback options.
28
29 # If perf_ext is defined and the performance value for the
30 # profile-directed test run is non-zero then the performance check will
31 # be done.
32
33 global PROFOPT_OPTIONS perf_delta
34
35 # The including .exp file must define these.
36 global tool profile_option feedback_option prof_ext
37 if ![info exists tool] {
38     error "Tools is not specified."
39 }
40 if ![info exists profile_option] {
41     error "No profile option specified for first compile."
42 }
43 if ![info exists feedback_option] {
44     error "No feedback option specified for second compile."
45 }
46 if ![info exists prof_ext] {
47     error "No profile data file extension specified."
48 }
49
50 # The maximum perforance degradation can be defined in the including file.
51 if ![info exists perf_delta] {
52     set perf_delta 4
53 }
54
55 # The default option list can be overridden by
56 # PROFOPT_OPTIONS="{ { list1 } ... { list2 } }"
57
58 if ![info exists PROFOPT_OPTIONS] {
59     set PROFOPT_OPTIONS [list \
60         { -g } \
61         { -O0 } \
62         { -O1 } \
63         { -O2 } \
64         { -O3 } \
65         { -O3 -g } \
66         { -Os } ]
67 }
68
69 set option_list $PROFOPT_OPTIONS
70
71 #
72 # profopt-cleanup -- remove profiling or performance results files.
73 #
74 # EXT is the extension of files to remove
75 #
76 proc profopt-cleanup { ext } {
77     set files [glob -nocomplain *.$ext]
78     if { $files != "" } {
79         eval "remote_file build delete $files"
80     }
81 }
82
83 #
84 # profopt-perf-value -- get performance value for a test
85 #
86 # TESTCASE is the name of the test
87 # PERF_EXT is the extension of the performance result file
88 # OPTSTR is the string of compiler options
89 #
90 proc profopt-perf-value { testcase perf_ext optstr } {
91     set basename [file tail $testcase]
92     set base [file rootname $basename]
93     set files [glob -nocomplain $base.$perf_ext]
94     # The file doesn't exist; let the caller decide if that's a problem.
95     if { $files == "" } {
96         return -2 
97     }
98     remote_upload host $base.$perf_ext $base.$perf_ext
99     set fd [open $base.$perf_ext r]
100     gets $fd line
101     set val -2
102     if [regexp "TIME" $line] {
103         if [regexp "TIME -1" $line] {
104             fail "$testcase perf check: no consistent time available, $optstr"
105             set val -1
106         } elseif ![regexp "(\[0-9\]+)" "$line" val] {
107             set val -2
108         }
109     }
110     # Report problems with an existing file.
111     if { $val == -2 } {
112         fail "$testcase perf check: file $base.$perf_ext has wrong format, $optstr"
113     }
114     close $fd
115     profopt-cleanup $perf_ext
116     return $val
117 }
118
119 #
120 # c-prof-execute -- compile for profiling and then feedback, then normal
121 #
122 # SRC is the full pathname of the testcase.
123 #
124 proc profopt-execute { src } {
125     global srcdir tmpdir
126     global option_list
127     global tool profile_option feedback_option prof_ext perf_ext perf_delta
128     global verbose
129
130     regsub "^$srcdir/?" $src "" testcase
131     # If we couldn't rip $srcdir out of `src' then just do the best we can.
132     # The point is to reduce the unnecessary noise in the logs.  Don't strip
133     # out too much because different testcases with the same name can confuse
134     # `test-tool'.
135     if [string match "/*" $testcase] {
136         set testcase "[file tail [file dirname $src]]/[file tail $src]"
137     }
138
139     set executable $tmpdir/[file tail [file rootname $src].x]
140
141     set count 0
142     foreach option $option_list {
143         set execname1 "${executable}${count}1"
144         set execname2 "${executable}${count}2"
145         set execname3 "${executable}${count}3"
146         incr count
147
148         remote_file build delete $execname1
149         remote_file build delete $execname2
150         remote_file build delete $execname3
151         verbose "Testing $testcase, $option" 1
152
153         # Compile for profiling.
154
155         set options ""
156         lappend options "additional_flags=$option $profile_option"
157         set optstr "$option $profile_option"
158         set comp_output [${tool}_target_compile "$src" "$execname1" executable $options];
159         if ![${tool}_check_compile "$testcase compilation" $optstr $execname1 $comp_output] {
160             unresolved "$testcase execution,   $optstr"
161             unresolved "$testcase compilation, $option $feedback_option"
162             unresolved "$testcase execution,   $option $feedback_option"
163             continue
164         }
165
166         # Run the profiled test.
167
168         set result [${tool}_load $execname1 "" ""]
169         set status [lindex $result 0]
170         # Make sure the profile data was generated, and fail if not.
171         if { $status == "pass" } {
172             set basename [file tail $testcase]
173             set base [file rootname $basename]
174             set files [glob -nocomplain $base.$prof_ext]
175             if { $files == "" } {
176                 set status "fail"
177                 fail "$testcase execution: file $base.$prof_ext does not exist, $option $profile_option"
178             } else {
179                 $status "$testcase execution,   $optstr"
180             }
181         } else {
182             $status "$testcase execution,   $optstr"
183         }
184         # Quit for this round if it failed
185         if { $status != "pass" } {
186             unresolved "$testcase compilation, $option $feedback_option"
187             unresolved "$testcase execution,   $option $feedback_option"
188             continue
189         }
190         remote_file build delete $execname1
191
192         # Compile with feedback-directed optimizations.
193
194         set options ""
195         lappend options "additional_flags=$option $feedback_option"
196         set optstr "$option $feedback_option"
197         set comp_output [${tool}_target_compile "$src" "$execname2" "executable" $options];
198         if ![${tool}_check_compile "$testcase compilation" $optstr $execname2 $comp_output] {
199             unresolved "$testcase execution,   $optstr"
200             continue
201         }
202
203         # Run the profile-directed optimized test.
204
205         set result [${tool}_load "$execname2" "" ""]
206         set status [lindex $result 0]
207         $status "$testcase execution,   $optstr"
208         if { $status != "pass" } {
209             continue
210         }
211
212         # Remove the profiling data files.
213         profopt-cleanup $prof_ext
214
215         # If the test is not expected to produce performance data then
216         # we're done now.
217         if ![info exists perf_ext] {
218             remote_file build delete $execname2
219             continue
220         }
221
222         # Get the performance data from the test built with
223         # profile-directed optimization.  If the file doesn't exist or if
224         # the value is zero, skip the performance comparison.
225         set val2 [profopt-perf-value $testcase $perf_ext $optstr]
226         if { $val2 <= 0 } {
227             remote_file build delete $execname2
228             continue
229         }
230
231         # Compile with normal optimizations.
232
233         set options ""
234         lappend options "additional_flags=$option"
235         set optstr "$option"
236         set comp_output [${tool}_target_compile "$src" "$execname3" "executable" $options];
237         if ![${tool}_check_compile "$testcase compilation" $optstr $execname3 $comp_output] {
238             unresolved "$testcase execution,   $optstr"
239             unresolved "$testcase perf check,  $optstr"
240             continue
241         }
242
243         # Run the test with normal optimizations.
244
245         set result [${tool}_load "$execname3" "" ""]
246         set status [lindex $result 0]
247         $status "$testcase execution,   $optstr"
248         if { $status != "pass" } {
249             unresolved "$testcase perf check, $optstr"
250             continue
251         }
252
253         # Get the performance data from the test built with normal
254         # optimization.
255         set val1 [profopt-perf-value $testcase $perf_ext $optstr]
256         if { $val1 < 0 } {
257             if { $val1 == -2 } {
258                 # The data file existed with the profile-directed
259                 # optimization so this one should, too.
260                 fail "$testcase perf check: file $base.$perf_ext does not exist, $optstr"
261             }
262             continue
263         }
264
265         # Compare results of the two runs and fail if the time with the
266         # profile-directed optimization is significantly more than the time
267         # without it.
268         set status "pass"
269         if { $val2 > $val1 } {
270             # Check for a performance degration outside of allowable limits.
271             if { [expr $val2 - $val1] > [expr [expr $val1 * $perf_delta] / 100] } {
272                 set status "fail"
273             }
274         }
275         if { $status == "fail" } {
276             fail "$testcase perf check: orig: $val1  new: $val2, $optstr"
277         } else {
278             $status "$testcase perf check,  $optstr"
279             verbose "$testcase orig: $val1  new: $val2, $optstr" 2
280             remote_file build delete $execname2
281             remote_file build delete $execname3
282         }
283     }
284 }