OSDN Git Service

* lib/profopt.exp: Treat prof_ext as a list.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / profopt.exp
1 #   Copyright (C) 2001, 2004 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 nonzero 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 prof_ext] {
41     error "No profile data file extensions specified."
42 }
43
44 # The maximum perforance degradation can be defined in the including file.
45 if ![info exists perf_delta] {
46     set perf_delta 4
47 }
48
49 # The default option list can be overridden by
50 # PROFOPT_OPTIONS="{ { list1 } ... { list2 } }"
51
52 if ![info exists PROFOPT_OPTIONS] {
53     set PROFOPT_OPTIONS [list \
54         { -g } \
55         { -O0 } \
56         { -O1 } \
57         { -O2 } \
58         { -O3 } \
59         { -O3 -g } \
60         { -Os } ]
61 }
62
63 set prof_option_list $PROFOPT_OPTIONS
64
65 #
66 # profopt-cleanup -- remove profiling or performance results files.
67 #
68 # TESTCASE is the name of the test
69 # EXT is the extensions of files to remove
70 #
71 proc profopt-cleanup { testcase extlist } {
72     set basename [file tail $testcase]
73     set base [file rootname $basename]
74     foreach ext $extlist {
75         set files [glob -nocomplain $base.$ext]
76         if { $files != "" } {
77             eval "remote_file build delete $files"
78         }
79     }
80 }
81
82 #
83 # profopt-perf-value -- get performance value for a test
84 #
85 # TESTCASE is the name of the test
86 # PERF_EXT is the extension of the performance result file
87 # OPTSTR is the string of compiler options
88 #
89 proc profopt-perf-value { testcase perf_ext optstr } {
90     set basename [file tail $testcase]
91     set base [file rootname $basename]
92     set files [glob -nocomplain $base.$perf_ext]
93     # The file doesn't exist; let the caller decide if that's a problem.
94     if { $files == "" } {
95         return -2 
96     }
97     remote_upload host $base.$perf_ext $base.$perf_ext
98     set fd [open $base.$perf_ext r]
99     gets $fd line
100     set val -2
101     if [regexp "TIME" $line] {
102         if [regexp "TIME -1" $line] {
103             fail "$testcase perf check: no consistent time available, $optstr"
104             set val -1
105         } elseif ![regexp "(\[0-9\]+)" "$line" val] {
106             set val -2
107         }
108     }
109     # Report problems with an existing file.
110     if { $val == -2 } {
111         fail "$testcase perf check: file $base.$perf_ext has wrong format, $optstr"
112     }
113     close $fd
114     profopt-cleanup $testcase $perf_ext
115     return $val
116 }
117
118 #
119 # c-prof-execute -- compile for profiling and then feedback, then normal
120 #
121 # SRC is the full pathname of the testcase.
122 #
123 proc profopt-execute { src } {
124     global srcdir tmpdir
125     global prof_option_list
126     global tool profile_option feedback_option prof_ext perf_ext perf_delta
127     global verbose
128
129     if ![info exists profile_option] {
130         error "No profile option specified for first compile."
131     }
132     if ![info exists feedback_option] {
133         error "No feedback option specified for second compile."
134     }
135
136     regsub "^$srcdir/?" $src "" testcase
137     # If we couldn't rip $srcdir out of `src' then just do the best we can.
138     # The point is to reduce the unnecessary noise in the logs.  Don't strip
139     # out too much because different testcases with the same name can confuse
140     # `test-tool'.
141     if [string match "/*" $testcase] {
142         set testcase "[file tail [file dirname $src]]/[file tail $src]"
143     }
144
145     set executable $tmpdir/[file tail [file rootname $src].x]
146
147     set count 0
148     foreach option $prof_option_list {
149         set execname1 "${executable}${count}1"
150         set execname2 "${executable}${count}2"
151         set execname3 "${executable}${count}3"
152         incr count
153
154         remote_file build delete $execname1
155         remote_file build delete $execname2
156         remote_file build delete $execname3
157         verbose "Testing $testcase, $option" 1
158
159         # Remove old profiling and performance data files.
160         profopt-cleanup $testcase $prof_ext
161         if [info exists perf_ext] {
162             profopt-cleanup $testcase $perf_ext
163         }
164
165         # Compile for profiling.
166
167         set options ""
168         lappend options "additional_flags=$option $profile_option"
169         set optstr "$option $profile_option"
170         set comp_output [${tool}_target_compile "$src" "$execname1" executable $options]
171         if ![${tool}_check_compile "$testcase compilation" $optstr $execname1 $comp_output] {
172             unresolved "$testcase execution,   $optstr"
173             unresolved "$testcase compilation, $option $feedback_option"
174             unresolved "$testcase execution,   $option $feedback_option"
175             continue
176         }
177
178         # Run the profiled test.
179
180         set result [${tool}_load $execname1 "" ""]
181         set status [lindex $result 0]
182         # Make sure the profile data was generated, and fail if not.
183         if { $status == "pass" } {
184             set basename [file tail $testcase]
185             set base [file rootname $basename]
186             foreach ext $prof_ext {
187                 set files [glob -nocomplain $base.$ext]
188                 if { $files == "" } {
189                     set status "fail"
190                     fail "$testcase execution: file $base.$ext does not exist, $option $profile_option"
191                 } else {
192                     $status "$testcase execution,   $optstr"
193                 }
194             }
195         } else {
196             $status "$testcase execution,   $optstr"
197         }
198         # Quit for this round if it failed
199         if { $status != "pass" } {
200             unresolved "$testcase compilation, $option $feedback_option"
201             unresolved "$testcase execution,   $option $feedback_option"
202             continue
203         }
204         remote_file build delete $execname1
205
206         # Compile with feedback-directed optimizations.
207
208         set options ""
209         lappend options "additional_flags=$option $feedback_option"
210         set optstr "$option $feedback_option"
211         set comp_output [${tool}_target_compile "$src" "$execname2" "executable" $options]
212         if ![${tool}_check_compile "$testcase compilation" $optstr $execname2 $comp_output] {
213             unresolved "$testcase execution,   $optstr"
214             continue
215         }
216
217         # Run the profile-directed optimized test.
218
219         set result [${tool}_load "$execname2" "" ""]
220         set status [lindex $result 0]
221         $status "$testcase execution,   $optstr"
222         if { $status != "pass" } {
223             continue
224         }
225
226         # Remove the profiling data files.
227         profopt-cleanup $testcase $prof_ext
228
229         # If the test is not expected to produce performance data then
230         # we're done now.
231         if ![info exists perf_ext] {
232             remote_file build delete $execname2
233             continue
234         }
235
236         # Get the performance data from the test built with
237         # profile-directed optimization.  If the file doesn't exist or if
238         # the value is zero, skip the performance comparison.
239         set val2 [profopt-perf-value $testcase $perf_ext $optstr]
240         if { $val2 <= 0 } {
241             remote_file build delete $execname2
242             continue
243         }
244
245         # Compile with normal optimizations.
246
247         set options ""
248         lappend options "additional_flags=$option"
249         set optstr "$option"
250         set comp_output [${tool}_target_compile "$src" "$execname3" "executable" $options]
251         if ![${tool}_check_compile "$testcase compilation" $optstr $execname3 $comp_output] {
252             unresolved "$testcase execution,   $optstr"
253             unresolved "$testcase perf check,  $optstr"
254             continue
255         }
256
257         # Run the test with normal optimizations.
258
259         set result [${tool}_load "$execname3" "" ""]
260         set status [lindex $result 0]
261         $status "$testcase execution,   $optstr"
262         if { $status != "pass" } {
263             unresolved "$testcase perf check, $optstr"
264             continue
265         }
266
267         # Get the performance data from the test built with normal
268         # optimization.
269         set val1 [profopt-perf-value $testcase $perf_ext $optstr]
270         if { $val1 < 0 } {
271             if { $val1 == -2 } {
272                 # The data file existed with the profile-directed
273                 # optimization so this one should, too.
274                 fail "$testcase perf check: file $base.$perf_ext does not exist, $optstr"
275             }
276             continue
277         }
278
279         # Compare results of the two runs and fail if the time with the
280         # profile-directed optimization is significantly more than the time
281         # without it.
282         set status "pass"
283         if { $val2 > $val1 } {
284             # Check for a performance degration outside of allowable limits.
285             if { [expr $val2 - $val1] > [expr [expr $val1 * $perf_delta] / 100] } {
286                 set status "fail"
287             }
288         }
289         if { $status == "fail" } {
290             fail "$testcase perf check: orig: $val1  new: $val2, $optstr"
291         } else {
292             $status "$testcase perf check,  $optstr"
293             verbose "$testcase orig: $val1  new: $val2, $optstr" 2
294             remote_file build delete $execname2
295             remote_file build delete $execname3
296         }
297     }
298 }