blob: d1de2cb2cd4c45b9e5204e6a3a9010a5f858ff3e [file] [log] [blame]
njnec0c27a2005-12-10 23:11:28 +00001#! @PERL@
2##--------------------------------------------------------------------##
3##--- Valgrind performance testing script vg_perf ---##
4##--------------------------------------------------------------------##
5
6# This file is part of Valgrind, a dynamic binary instrumentation
7# framework.
8#
9# Copyright (C) 2005 Nicholas Nethercote
10# njn@valgrind.org
11#
12# This program is free software; you can redistribute it and/or
13# modify it under the terms of the GNU General Public License as
14# published by the Free Software Foundation; either version 2 of the
15# License, or (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful, but
18# WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20# General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25# 02111-1307, USA.
26#
27# The GNU General Public License is contained in the file COPYING.
28
29#----------------------------------------------------------------------------
njn57781862005-12-14 02:58:23 +000030# usage: see usage message.
njnec0c27a2005-12-10 23:11:28 +000031#
32# The easiest way is to run all tests in valgrind/ with (assuming you installed
33# in $PREFIX):
34#
35# perl perf/vg_perf --all
36#
37# You can specify individual files to test, or whole directories, or both.
38# Directories are traversed recursively, except for ones named, for example,
39# CVS/ or docs/.
40#
41# Each test is defined in a file <test>.vgperf, containing one or more of the
42# following lines, in any order:
43# - prog: <prog to run> (compulsory)
44# - tools: <Valgrind tools> (compulsory)
45# - args: <args for prog> (default: none)
46# - vgopts: <Valgrind options> (default: none)
47# - prereq: <prerequisite command> (default: none)
48# - cleanup: <post-test cleanup cmd to run> (default: none)
49#
50# The prerequisite command, if present, must return 0 otherwise the test is
51# skipped.
52#----------------------------------------------------------------------------
53
54use warnings;
55use strict;
56
57#----------------------------------------------------------------------------
58# Global vars
59#----------------------------------------------------------------------------
njnee45f1d2005-12-13 21:55:16 +000060my $usage = <<END
61usage: vg_perf [options] [files or dirs]
njnec0c27a2005-12-10 23:11:28 +000062
njnee45f1d2005-12-13 21:55:16 +000063 options for the user, with defaults in [ ], are:
64 -h --help show this message
65 --all run all tests under this directory
66 --reps number of repeats for each program [3]
njn57781862005-12-14 02:58:23 +000067 --vg Valgrind(s) to measure (can be specified multiple
68 times). The "in-place" build is used.
69 [Valgrind in the current directory]
njnee45f1d2005-12-13 21:55:16 +000070END
71;
njnec0c27a2005-12-10 23:11:28 +000072
73# Test variables
74my $vgopts; # valgrind options
75my $prog; # test prog
76my $args; # test prog args
77my $prereq; # prerequisite test to satisfy before running test
78my $cleanup; # cleanup command to run
79my @tools; # which tools are we measuring the program with
80
81# Abbreviations used in output
82my %toolnames = (
83 none => "nl",
84 memcheck => "mc",
85 cachegrind => "cg",
86 massif => "ms"
87);
88
njn57781862005-12-14 02:58:23 +000089# Command line options
njnfbfbd4f2005-12-23 23:08:04 +000090my $n_reps = 1; # Run each program $n_reps times and choose the best one.
njn57781862005-12-14 02:58:23 +000091my @vgdirs; # Dirs of the various Valgrinds being measured.
njnec0c27a2005-12-10 23:11:28 +000092
93my $num_tests_done = 0;
94my $num_timings_done = 0;
95
96# Starting directory
97chomp(my $tests_dir = `pwd`);
98
njnec0c27a2005-12-10 23:11:28 +000099#----------------------------------------------------------------------------
100# Process command line, setup
101#----------------------------------------------------------------------------
102
103# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
104#
105# 1. Can prepend "." onto programs to avoid trouble with users who don't have
106# "." in their path (by making $dir = ".")
107# 2. Can prepend the current dir to make the command absolute to avoid
108# subsequent trouble when we change directories.
109#
110# Also checks the program exists and is executable.
111sub validate_program ($$$$)
112{
113 my ($dir, $prog, $must_exist, $must_be_executable) = @_;
114
115 # If absolute path, leave it alone. If relative, make it
116 # absolute -- by prepending current dir -- so we can change
117 # dirs and still use it.
118 $prog = "$dir/$prog" if ($prog !~ /^\//);
119 if ($must_exist) {
120 (-f $prog) or die "vg_perf: '$prog' not found or not a file ($dir)\n";
121 }
122 if ($must_be_executable) {
123 (-x $prog) or die "vg_perf: '$prog' not executable ($dir)\n";
124 }
125
126 return $prog;
127}
128
129sub validate_tools($)
130{
131 # XXX: should check they exist!
132 my ($toolnames) = @_;
133 my @t = split(/\s+/, $toolnames);
134 return @t;
135}
136
njn57781862005-12-14 02:58:23 +0000137sub add_vgdir($)
138{
139 my ($vgdir) = @_;
140 if ($vgdir !~ /^\//) { $vgdir = "$tests_dir/$vgdir"; }
141 validate_program($vgdir, "./coregrind/valgrind", 1, 1);
142 push(@vgdirs, $vgdir);
143}
144
njnec0c27a2005-12-10 23:11:28 +0000145sub process_command_line()
146{
147 my $alldirs = 0;
148 my @fs;
149
150 for my $arg (@ARGV) {
151 if ($arg =~ /^-/) {
152 if ($arg =~ /^--all$/) {
153 $alldirs = 1;
njnb2b4f462005-12-13 22:00:17 +0000154 } elsif ($arg =~ /^--reps=(\d+)$/) {
njnee45f1d2005-12-13 21:55:16 +0000155 $n_reps = $1;
njn30e23ac2005-12-15 17:22:37 +0000156 if ($n_reps < 1) { die "bad --reps value: $n_reps\n"; }
njn57781862005-12-14 02:58:23 +0000157 } elsif ($arg =~ /^--vg=(.+)$/) {
158 # Make dir absolute if not already
159 add_vgdir($1);
njnec0c27a2005-12-10 23:11:28 +0000160 } else {
161 die $usage;
162 }
163 } else {
164 push(@fs, $arg);
165 }
166 }
njn57781862005-12-14 02:58:23 +0000167
168 # If no --vg options were specified, use the current tree.
169 if (0 == @vgdirs) {
170 add_vgdir($tests_dir);
171 }
njnec0c27a2005-12-10 23:11:28 +0000172
173 if ($alldirs) {
174 @fs = ();
175 foreach my $f (glob "*") {
176 push(@fs, $f) if (-d $f);
177 }
178 }
179
180 (0 != @fs) or die "No test files or directories specified\n";
181
182 return @fs;
183}
184
185#----------------------------------------------------------------------------
186# Read a .vgperf file
187#----------------------------------------------------------------------------
188sub read_vgperf_file($)
189{
190 my ($f) = @_;
191
192 # Defaults.
193 ($vgopts, $prog, $args, $prereq, $cleanup)
194 = ("", undef, "", undef, undef, undef, undef);
195
196 open(INPUTFILE, "< $f") || die "File $f not openable\n";
197
198 while (my $line = <INPUTFILE>) {
199 if ($line =~ /^\s*#/ || $line =~ /^\s*$/) {
200 next;
201 } elsif ($line =~ /^\s*vgopts:\s*(.*)$/) {
202 $vgopts = $1;
203 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
njnc9582a22005-12-13 21:44:48 +0000204 $prog = validate_program(".", $1, 1, 1);
njnec0c27a2005-12-10 23:11:28 +0000205 } elsif ($line =~ /^\s*tools:\s*(.*)$/) {
206 @tools = validate_tools($1);
207 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
208 $args = $1;
209 } elsif ($line =~ /^\s*prereq:\s*(.*)$/) {
210 $prereq = $1;
211 } elsif ($line =~ /^\s*cleanup:\s*(.*)$/) {
212 $cleanup = $1;
213 } else {
214 die "Bad line in $f: $line\n";
215 }
216 }
217 close(INPUTFILE);
218
219 if (!defined $prog) {
220 $prog = ""; # allow no prog for testing error and --help cases
221 }
222 if (0 == @tools) {
223 die "vg_perf: missing 'tools' line in $f\n";
224 }
225}
226
227#----------------------------------------------------------------------------
228# Do one test
229#----------------------------------------------------------------------------
230# Since most of the program time is spent in system() calls, need this to
231# propagate a Ctrl-C enabling us to quit.
232sub mysystem($)
233{
njnecb47442005-12-13 16:54:58 +0000234 my ($cmd) = @_;
235 my $retval = system($cmd);
236 if ($retval == 2) {
237 exit 1;
238 } else {
239 return $retval;
240 }
njnec0c27a2005-12-10 23:11:28 +0000241}
242
sewardjb4860be2006-10-17 02:30:17 +0000243# Run program N times, return the best user time. Use the POSIX
244# -p flag on /usr/bin/time so as to get something parseable on AIX.
njnec0c27a2005-12-10 23:11:28 +0000245sub time_prog($$)
246{
247 my ($cmd, $n) = @_;
248 my $tmin = 999999;
249 for (my $i = 0; $i < $n; $i++) {
njnecb47442005-12-13 16:54:58 +0000250 mysystem("echo '$cmd' > perf.cmd");
251 my $retval = mysystem("$cmd > perf.stdout 2> perf.stderr");
252 (0 == $retval) or
njn57781862005-12-14 02:58:23 +0000253 die "\n*** Command returned non-zero ($retval)"
254 . "\n*** See perf.{cmd,stdout,stderr} to determine what went wrong.\n";
njnecb47442005-12-13 16:54:58 +0000255 my $out = `cat perf.stderr`;
sewardjb4860be2006-10-17 02:30:17 +0000256 ($out =~ /[Uu]ser([ ]+)([\d\.]+)/) or
njn02383bc2005-12-13 20:23:38 +0000257 die "\n*** missing usertime in perf.stderr\n";
sewardjb4860be2006-10-17 02:30:17 +0000258 $tmin = $2 if ($2 < $tmin);
njnec0c27a2005-12-10 23:11:28 +0000259 }
njn30e23ac2005-12-15 17:22:37 +0000260 # Avoid divisions by zero!
261 return (0 == $tmin ? 0.01 : $tmin);
njnec0c27a2005-12-10 23:11:28 +0000262}
263
264sub do_one_test($$)
265{
266 my ($dir, $vgperf) = @_;
267 $vgperf =~ /^(.*)\.vgperf/;
268 my $name = $1;
njn30e23ac2005-12-15 17:22:37 +0000269 my %first_tTool; # For doing percentage speedups when comparing
270 # multiple Valgrinds
njnec0c27a2005-12-10 23:11:28 +0000271
272 read_vgperf_file($vgperf);
273
274 if (defined $prereq) {
275 if (system("$prereq") != 0) {
276 printf("%-16s (skipping, prereq failed: $prereq)\n", "$name:");
277 return;
278 }
279 }
280
sewardjb4860be2006-10-17 02:30:17 +0000281 my $timecmd = "/usr/bin/time -p";
njnec0c27a2005-12-10 23:11:28 +0000282
283 # Do the native run(s).
njn57781862005-12-14 02:58:23 +0000284 printf("-- $name --\n") if (@vgdirs > 1);
njnec0c27a2005-12-10 23:11:28 +0000285 my $cmd = "$timecmd $prog $args";
njnee45f1d2005-12-13 21:55:16 +0000286 my $tNative = time_prog($cmd, $n_reps);
njnec0c27a2005-12-10 23:11:28 +0000287
njn57781862005-12-14 02:58:23 +0000288 foreach my $vgdir (@vgdirs) {
289 # Benchmark name
290 printf("%-8s ", $name);
njnec0c27a2005-12-10 23:11:28 +0000291
njn57781862005-12-14 02:58:23 +0000292 # Print the Valgrind version if we are measuring more than one.
293 my $vgdirname = $vgdir;
294 chomp($vgdirname = `basename $vgdir`);
295 printf("%-10s:", $vgdirname);
296
297 # Native execution time
sewardj8e339a12006-10-14 14:04:42 +0000298 printf("%4.2fs", $tNative);
njnec0c27a2005-12-10 23:11:28 +0000299
njn57781862005-12-14 02:58:23 +0000300 foreach my $tool (@tools) {
301 (defined $toolnames{$tool}) or
302 die "unknown tool $tool, please add to %toolnames\n";
njnec0c27a2005-12-10 23:11:28 +0000303
njn57781862005-12-14 02:58:23 +0000304 # Do the tool run(s). Set both VALGRIND_LIB and VALGRIND_LIB_INNER
njn8c20d872006-04-06 22:59:35 +0000305 # in case this Valgrind was configured with --enable-inner. And
306 # also VALGRINDLIB, which was the old name for the variable, to
307 # allow comparison against old Valgrind versions (eg. 2.4.X).
njn715cc592006-03-27 00:39:43 +0000308 printf(" %s:", $toolnames{$tool});
njn8c20d872006-04-06 22:59:35 +0000309 my $vgsetup = "VALGRINDLIB=$vgdir/.in_place "
310 . "VALGRIND_LIB=$vgdir/.in_place "
njn57781862005-12-14 02:58:23 +0000311 . "VALGRIND_LIB_INNER=$vgdir/.in_place ";
312 my $vgcmd = "$vgdir/coregrind/valgrind "
313 . "--command-line-only=yes --tool=$tool -q "
314 . "--memcheck:leak-check=no --addrcheck:leak-check=no "
315 . "$vgopts ";
316 my $cmd = "$vgsetup $timecmd $vgcmd $prog $args";
317 my $tTool = time_prog($cmd, $n_reps);
njn30e23ac2005-12-15 17:22:37 +0000318 printf("%4.1fs (%4.1fx,", $tTool, $tTool/$tNative);
319
320 # If it's the first timing for this tool on this benchmark,
321 # record the time so we can get the percentage speedup of the
322 # subsequent Valgrinds. Otherwise, compute and print
323 # the speedup.
324 if (not defined $first_tTool{$tool}) {
325 $first_tTool{$tool} = $tTool;
njn715cc592006-03-27 00:39:43 +0000326 print(" -----)");
njn30e23ac2005-12-15 17:22:37 +0000327 } else {
328 my $speedup = 100 - (100 * $tTool / $first_tTool{$tool});
njn715cc592006-03-27 00:39:43 +0000329 printf("%5.1f%%)", $speedup);
njn30e23ac2005-12-15 17:22:37 +0000330 }
njn57781862005-12-14 02:58:23 +0000331
332 $num_timings_done++;
333
334 if (defined $cleanup) {
335 (system("$cleanup") == 0) or
336 print(" ($name cleanup operation failed: $cleanup)\n");
337 }
338 }
339 printf("\n");
njnec0c27a2005-12-10 23:11:28 +0000340 }
341
342 $num_tests_done++;
343}
344
345#----------------------------------------------------------------------------
346# Test one directory (and any subdirs)
347#----------------------------------------------------------------------------
348sub test_one_dir($$); # forward declaration
349
350sub test_one_dir($$)
351{
352 my ($dir, $prev_dirs) = @_;
353 $dir =~ s/\/$//; # trim a trailing '/'
354
355 # Ignore dirs into which we should not recurse.
356 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
357
358 chdir($dir) or die "Could not change into $dir\n";
359
360 # Nb: Don't prepend a '/' to the base directory
361 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
362 my $dashes = "-" x (50 - length $full_dir);
363
364 my @fs = glob "*";
365 my $found_tests = (0 != (grep { $_ =~ /\.vgperf$/ } @fs));
366
367 if ($found_tests) {
368 print "-- Running tests in $full_dir $dashes\n";
369 }
370 foreach my $f (@fs) {
371 if (-d $f) {
372 test_one_dir($f, $full_dir);
373 } elsif ($f =~ /\.vgperf$/) {
374 do_one_test($full_dir, $f);
375 }
376 }
377 if ($found_tests) {
378 print "-- Finished tests in $full_dir $dashes\n";
379 }
380
381 chdir("..");
382}
383
384#----------------------------------------------------------------------------
385# Summarise results
386#----------------------------------------------------------------------------
387sub summarise_results
388{
389 printf("\n== %d programs, %d timings =================\n\n",
390 $num_tests_done, $num_timings_done);
391}
392
393#----------------------------------------------------------------------------
394# main()
395#----------------------------------------------------------------------------
396
397# nuke VALGRIND_OPTS
398$ENV{"VALGRIND_OPTS"} = "";
399
400my @fs = process_command_line();
401foreach my $f (@fs) {
402 if (-d $f) {
403 test_one_dir($f, "");
404 } else {
405 # Allow the .vgperf suffix to be given or omitted
406 if ($f =~ /.vgperf$/ && -r $f) {
407 # do nothing
408 } elsif (-r "$f.vgperf") {
409 $f = "$f.vgperf";
410 } else {
411 die "`$f' neither a directory nor a readable test file/name\n"
412 }
413 my $dir = `dirname $f`; chomp $dir;
414 my $file = `basename $f`; chomp $file;
415 chdir($dir) or die "Could not change into $dir\n";
416 do_one_test($dir, $file);
417 chdir($tests_dir);
418 }
419}
420summarise_results();
421
422##--------------------------------------------------------------------##
423##--- end ---##
424##--------------------------------------------------------------------##