blob: 2c34dc9916208fae58b796a27639900a94085628 [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#
njnec0c27a2005-12-10 23:11:28 +000032# You can specify individual files to test, or whole directories, or both.
33# Directories are traversed recursively, except for ones named, for example,
34# CVS/ or docs/.
35#
36# Each test is defined in a file <test>.vgperf, containing one or more of the
37# following lines, in any order:
38# - prog: <prog to run> (compulsory)
njnec0c27a2005-12-10 23:11:28 +000039# - args: <args for prog> (default: none)
40# - vgopts: <Valgrind options> (default: none)
41# - prereq: <prerequisite command> (default: none)
42# - cleanup: <post-test cleanup cmd to run> (default: none)
43#
44# The prerequisite command, if present, must return 0 otherwise the test is
45# skipped.
philippe215b2972012-07-24 19:47:46 +000046# Sometimes it is useful to run all the tests at a high sanity check
47# level or with arbitrary other flags. To make this simple, extra
48# options, applied to all tests run, are read from $EXTRA_REGTEST_OPTS,
49# and handed to valgrind prior to any other flags specified by the
50# .vgperf file. Note: the env var is the same as vg_regtest.
njnec0c27a2005-12-10 23:11:28 +000051#----------------------------------------------------------------------------
52
53use warnings;
54use strict;
55
56#----------------------------------------------------------------------------
57# Global vars
58#----------------------------------------------------------------------------
njnee45f1d2005-12-13 21:55:16 +000059my $usage = <<END
60usage: vg_perf [options] [files or dirs]
njnec0c27a2005-12-10 23:11:28 +000061
njnee45f1d2005-12-13 21:55:16 +000062 options for the user, with defaults in [ ], are:
63 -h --help show this message
njn3fbf17a2006-11-22 00:39:08 +000064 --reps=<n> number of repeats for each program [1]
njn768cd5d2006-11-22 00:52:00 +000065 --tools=<t1,t2,t3> tools to run [Nulgrind and Memcheck]
philippe14ab1a32012-04-08 19:52:38 +000066 --vg Valgrind(s) to measure [Valgrind in the current directory]
67 (can be specified multiple times).
68 The "in-place" build is used.
69
70 --outer-valgrind: run these Valgrind(s) under the given outer valgrind.
71 These Valgrind(s) must be configured with --enable-inner.
72 --outer-tool: tool to use by the outer valgrind (default cachegrind).
73 --outer-args: use this as outer tool args.
njn768cd5d2006-11-22 00:52:00 +000074
75 Any tools named in --tools must be present in all directories specified
76 with --vg. (This is not checked.)
philippe215b2972012-07-24 19:47:46 +000077 Use EXTRA_REGTEST_OPTS to supply extra args for all tests
njnee45f1d2005-12-13 21:55:16 +000078END
79;
njnec0c27a2005-12-10 23:11:28 +000080
81# Test variables
82my $vgopts; # valgrind options
83my $prog; # test prog
84my $args; # test prog args
85my $prereq; # prerequisite test to satisfy before running test
86my $cleanup; # cleanup command to run
njnec0c27a2005-12-10 23:11:28 +000087
njn57781862005-12-14 02:58:23 +000088# Command line options
njn3fbf17a2006-11-22 00:39:08 +000089my $n_reps = 1; # Run each test $n_reps times and choose the best one.
90my @vgdirs; # Dirs of the various Valgrinds being measured.
njn768cd5d2006-11-22 00:52:00 +000091my @tools = ("none", "memcheck"); # tools being measured
njnec0c27a2005-12-10 23:11:28 +000092
philippe14ab1a32012-04-08 19:52:38 +000093# Outer valgrind to use, and args to use for it.
94# If this is set, --valgrind should be set to the installed inner valgrind,
95# and --valgrind-lib will be ignore
96my $outer_valgrind;
97my $outer_tool = "cachegrind";
98my $outer_args;
99
100
njnec0c27a2005-12-10 23:11:28 +0000101my $num_tests_done = 0;
102my $num_timings_done = 0;
103
104# Starting directory
105chomp(my $tests_dir = `pwd`);
106
njnec0c27a2005-12-10 23:11:28 +0000107#----------------------------------------------------------------------------
108# Process command line, setup
109#----------------------------------------------------------------------------
110
111# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
112#
113# 1. Can prepend "." onto programs to avoid trouble with users who don't have
114# "." in their path (by making $dir = ".")
115# 2. Can prepend the current dir to make the command absolute to avoid
116# subsequent trouble when we change directories.
117#
118# Also checks the program exists and is executable.
119sub validate_program ($$$$)
120{
121 my ($dir, $prog, $must_exist, $must_be_executable) = @_;
122
123 # If absolute path, leave it alone. If relative, make it
124 # absolute -- by prepending current dir -- so we can change
125 # dirs and still use it.
126 $prog = "$dir/$prog" if ($prog !~ /^\//);
127 if ($must_exist) {
128 (-f $prog) or die "vg_perf: '$prog' not found or not a file ($dir)\n";
129 }
130 if ($must_be_executable) {
131 (-x $prog) or die "vg_perf: '$prog' not executable ($dir)\n";
132 }
133
134 return $prog;
135}
136
njn57781862005-12-14 02:58:23 +0000137sub add_vgdir($)
138{
139 my ($vgdir) = @_;
140 if ($vgdir !~ /^\//) { $vgdir = "$tests_dir/$vgdir"; }
njn57781862005-12-14 02:58:23 +0000141 push(@vgdirs, $vgdir);
142}
143
njnec0c27a2005-12-10 23:11:28 +0000144sub process_command_line()
145{
njnec0c27a2005-12-10 23:11:28 +0000146 my @fs;
147
148 for my $arg (@ARGV) {
149 if ($arg =~ /^-/) {
njn1a95e052009-07-01 04:50:41 +0000150 if ($arg =~ /^--reps=(\d+)$/) {
njnee45f1d2005-12-13 21:55:16 +0000151 $n_reps = $1;
njn30e23ac2005-12-15 17:22:37 +0000152 if ($n_reps < 1) { die "bad --reps value: $n_reps\n"; }
njn57781862005-12-14 02:58:23 +0000153 } elsif ($arg =~ /^--vg=(.+)$/) {
154 # Make dir absolute if not already
155 add_vgdir($1);
njn3fbf17a2006-11-22 00:39:08 +0000156 } elsif ($arg =~ /^--tools=(.+)$/) {
157 @tools = split(/,/, $1);
philippe14ab1a32012-04-08 19:52:38 +0000158 } elsif ($arg =~ /^--outer-valgrind=(.*)$/) {
159 $outer_valgrind = $1;
160 } elsif ($arg =~ /^--outer-tool=(.*)$/) {
161 $outer_tool = $1;
162 } elsif ($arg =~ /^--outer-args=(.*)$/) {
163 $outer_args = $1;
njnec0c27a2005-12-10 23:11:28 +0000164 } else {
165 die $usage;
166 }
167 } else {
168 push(@fs, $arg);
169 }
170 }
njn57781862005-12-14 02:58:23 +0000171
172 # If no --vg options were specified, use the current tree.
173 if (0 == @vgdirs) {
174 add_vgdir($tests_dir);
175 }
njnec0c27a2005-12-10 23:11:28 +0000176
njnec0c27a2005-12-10 23:11:28 +0000177 (0 != @fs) or die "No test files or directories specified\n";
178
179 return @fs;
180}
181
182#----------------------------------------------------------------------------
183# Read a .vgperf file
184#----------------------------------------------------------------------------
185sub read_vgperf_file($)
186{
187 my ($f) = @_;
188
189 # Defaults.
190 ($vgopts, $prog, $args, $prereq, $cleanup)
191 = ("", undef, "", undef, undef, undef, undef);
192
193 open(INPUTFILE, "< $f") || die "File $f not openable\n";
194
195 while (my $line = <INPUTFILE>) {
196 if ($line =~ /^\s*#/ || $line =~ /^\s*$/) {
197 next;
198 } elsif ($line =~ /^\s*vgopts:\s*(.*)$/) {
199 $vgopts = $1;
200 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
njnc9582a22005-12-13 21:44:48 +0000201 $prog = validate_program(".", $1, 1, 1);
njnec0c27a2005-12-10 23:11:28 +0000202 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
203 $args = $1;
204 } elsif ($line =~ /^\s*prereq:\s*(.*)$/) {
205 $prereq = $1;
206 } elsif ($line =~ /^\s*cleanup:\s*(.*)$/) {
207 $cleanup = $1;
208 } else {
209 die "Bad line in $f: $line\n";
210 }
211 }
212 close(INPUTFILE);
213
214 if (!defined $prog) {
215 $prog = ""; # allow no prog for testing error and --help cases
216 }
217 if (0 == @tools) {
218 die "vg_perf: missing 'tools' line in $f\n";
219 }
220}
221
222#----------------------------------------------------------------------------
223# Do one test
224#----------------------------------------------------------------------------
225# Since most of the program time is spent in system() calls, need this to
226# propagate a Ctrl-C enabling us to quit.
227sub mysystem($)
228{
njnecb47442005-12-13 16:54:58 +0000229 my ($cmd) = @_;
230 my $retval = system($cmd);
231 if ($retval == 2) {
232 exit 1;
233 } else {
234 return $retval;
235 }
njnec0c27a2005-12-10 23:11:28 +0000236}
237
sewardjb4860be2006-10-17 02:30:17 +0000238# Run program N times, return the best user time. Use the POSIX
239# -p flag on /usr/bin/time so as to get something parseable on AIX.
njnec0c27a2005-12-10 23:11:28 +0000240sub time_prog($$)
241{
242 my ($cmd, $n) = @_;
243 my $tmin = 999999;
244 for (my $i = 0; $i < $n; $i++) {
njnecb47442005-12-13 16:54:58 +0000245 mysystem("echo '$cmd' > perf.cmd");
246 my $retval = mysystem("$cmd > perf.stdout 2> perf.stderr");
247 (0 == $retval) or
njn57781862005-12-14 02:58:23 +0000248 die "\n*** Command returned non-zero ($retval)"
249 . "\n*** See perf.{cmd,stdout,stderr} to determine what went wrong.\n";
njnecb47442005-12-13 16:54:58 +0000250 my $out = `cat perf.stderr`;
njnae7f6822007-02-02 23:23:01 +0000251 ($out =~ /[Uu]ser +([\d\.]+)/) or
njn02383bc2005-12-13 20:23:38 +0000252 die "\n*** missing usertime in perf.stderr\n";
njnae7f6822007-02-02 23:23:01 +0000253 $tmin = $1 if ($1 < $tmin);
njnec0c27a2005-12-10 23:11:28 +0000254 }
njn30e23ac2005-12-15 17:22:37 +0000255 # Avoid divisions by zero!
256 return (0 == $tmin ? 0.01 : $tmin);
njnec0c27a2005-12-10 23:11:28 +0000257}
258
259sub do_one_test($$)
260{
261 my ($dir, $vgperf) = @_;
262 $vgperf =~ /^(.*)\.vgperf/;
263 my $name = $1;
njn30e23ac2005-12-15 17:22:37 +0000264 my %first_tTool; # For doing percentage speedups when comparing
265 # multiple Valgrinds
njnec0c27a2005-12-10 23:11:28 +0000266
267 read_vgperf_file($vgperf);
268
269 if (defined $prereq) {
270 if (system("$prereq") != 0) {
271 printf("%-16s (skipping, prereq failed: $prereq)\n", "$name:");
272 return;
273 }
274 }
275
sewardjb4860be2006-10-17 02:30:17 +0000276 my $timecmd = "/usr/bin/time -p";
njnec0c27a2005-12-10 23:11:28 +0000277
278 # Do the native run(s).
njn57781862005-12-14 02:58:23 +0000279 printf("-- $name --\n") if (@vgdirs > 1);
njnec0c27a2005-12-10 23:11:28 +0000280 my $cmd = "$timecmd $prog $args";
njnee45f1d2005-12-13 21:55:16 +0000281 my $tNative = time_prog($cmd, $n_reps);
njnec0c27a2005-12-10 23:11:28 +0000282
philippe14ab1a32012-04-08 19:52:38 +0000283 if (defined $outer_valgrind) {
284 $outer_valgrind = validate_program($tests_dir, $outer_valgrind, 1, 1);
285 foreach my $vgdir (@vgdirs) {
286 validate_program($vgdir, "./coregrind/valgrind", 1, 1);
287 }
288 } else {
289 foreach my $vgdir (@vgdirs) {
290 validate_program($vgdir, "./coregrind/valgrind", 1, 1);
291 }
292 }
293
philippe215b2972012-07-24 19:47:46 +0000294 # Pull any extra options (for example, --sanity-level=4)
295 # from $EXTRA_REGTEST_OPTS.
296 my $maybe_extraopts = $ENV{"EXTRA_REGTEST_OPTS"};
297 my $extraopts = $maybe_extraopts ? $maybe_extraopts : "";
298
njn57781862005-12-14 02:58:23 +0000299 foreach my $vgdir (@vgdirs) {
300 # Benchmark name
301 printf("%-8s ", $name);
njnec0c27a2005-12-10 23:11:28 +0000302
njn57781862005-12-14 02:58:23 +0000303 # Print the Valgrind version if we are measuring more than one.
304 my $vgdirname = $vgdir;
305 chomp($vgdirname = `basename $vgdir`);
306 printf("%-10s:", $vgdirname);
307
308 # Native execution time
sewardj8e339a12006-10-14 14:04:42 +0000309 printf("%4.2fs", $tNative);
njnec0c27a2005-12-10 23:11:28 +0000310
njn57781862005-12-14 02:58:23 +0000311 foreach my $tool (@tools) {
njn341f98b2006-11-03 19:37:50 +0000312 # First two chars of toolname for abbreviation
313 my $tool_abbrev = $tool;
314 $tool_abbrev =~ s/(..).*/$1/;
njn341f98b2006-11-03 19:37:50 +0000315 printf(" %s:", $tool_abbrev);
philippe14ab1a32012-04-08 19:52:38 +0000316 my $run_outer_args = "";
317 if (not defined $outer_args) {
318 $run_outer_args =
319 " -v --command-line-only=yes"
320 . " --run-libc-freeres=no --sim-hints=enable-outer"
321 . " --smc-check=all-non-file"
322 . " --vgdb=no --trace-children=yes --read-var-info=no"
323 . " --suppressions=../tests/outer_inner.supp"
324 . " --memcheck:leak-check=full --memcheck:show-reachable=no"
325 . " --cachegrind:cache-sim=yes --cachegrind:branch-sim=yes"
326 . " --cachegrind:cachegrind-out-file=cachegrind.out.$vgdirname.$tool_abbrev.$name.%p"
327 . " --callgrind:cache-sim=yes --callgrind:branch-sim=yes"
328 . " --callgrind:dump-instr=yes --callgrind:collect-jumps=yes"
329 . " --callgrind:callgrind-out-file=callgrind.out.$vgdirname.$tool_abbrev.$name.%p"
330 . " ";
331 } else {
332 $run_outer_args = $outer_args;
333 }
334
335 my $vgsetup = "";
njn57781862005-12-14 02:58:23 +0000336 my $vgcmd = "$vgdir/coregrind/valgrind "
philippe215b2972012-07-24 19:47:46 +0000337 . "--command-line-only=yes --tool=$tool $extraopts -q "
njn276054a2006-11-03 19:30:33 +0000338 . "--memcheck:leak-check=no "
339 . "--trace-children=yes "
njn57781862005-12-14 02:58:23 +0000340 . "$vgopts ";
philippe14ab1a32012-04-08 19:52:38 +0000341 # Do the tool run(s).
342 if (defined $outer_valgrind ) {
343 # in an outer-inner setup, only set VALGRIND_LIB_INNER
344 $vgsetup = "VALGRIND_LIB_INNER=$vgdir/.in_place ";
345 $vgcmd = "$outer_valgrind "
346 . "--tool=" . $outer_tool . " "
347 . "$run_outer_args "
348 . "--log-file=" . "$outer_tool.outer.log.$vgdirname.$tool_abbrev.$name.%p "
349 . $vgcmd;
350 } else {
351 # Set both VALGRIND_LIB and VALGRIND_LIB_INNER
352 # in case this Valgrind was configured with --enable-inner. And
353 # also VALGRINDLIB, which was the old name for the variable, to
354 # allow comparison against old Valgrind versions (eg. 2.4.X).
355 $vgsetup = "VALGRINDLIB=$vgdir/.in_place "
356 . "VALGRIND_LIB=$vgdir/.in_place "
357 . "VALGRIND_LIB_INNER=$vgdir/.in_place ";
358 }
njn57781862005-12-14 02:58:23 +0000359 my $cmd = "$vgsetup $timecmd $vgcmd $prog $args";
360 my $tTool = time_prog($cmd, $n_reps);
njn30e23ac2005-12-15 17:22:37 +0000361 printf("%4.1fs (%4.1fx,", $tTool, $tTool/$tNative);
362
363 # If it's the first timing for this tool on this benchmark,
364 # record the time so we can get the percentage speedup of the
365 # subsequent Valgrinds. Otherwise, compute and print
366 # the speedup.
367 if (not defined $first_tTool{$tool}) {
368 $first_tTool{$tool} = $tTool;
njn715cc592006-03-27 00:39:43 +0000369 print(" -----)");
njn30e23ac2005-12-15 17:22:37 +0000370 } else {
371 my $speedup = 100 - (100 * $tTool / $first_tTool{$tool});
njn715cc592006-03-27 00:39:43 +0000372 printf("%5.1f%%)", $speedup);
njn30e23ac2005-12-15 17:22:37 +0000373 }
njn57781862005-12-14 02:58:23 +0000374
375 $num_timings_done++;
376
377 if (defined $cleanup) {
378 (system("$cleanup") == 0) or
379 print(" ($name cleanup operation failed: $cleanup)\n");
380 }
381 }
382 printf("\n");
njnec0c27a2005-12-10 23:11:28 +0000383 }
384
385 $num_tests_done++;
386}
387
388#----------------------------------------------------------------------------
389# Test one directory (and any subdirs)
390#----------------------------------------------------------------------------
391sub test_one_dir($$); # forward declaration
392
393sub test_one_dir($$)
394{
395 my ($dir, $prev_dirs) = @_;
396 $dir =~ s/\/$//; # trim a trailing '/'
397
njn356ffa32006-11-03 19:35:20 +0000398 chomp(my $initial_dir = `pwd`); # record where we started
399
njnec0c27a2005-12-10 23:11:28 +0000400 # Ignore dirs into which we should not recurse.
401 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
402
403 chdir($dir) or die "Could not change into $dir\n";
404
405 # Nb: Don't prepend a '/' to the base directory
406 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
407 my $dashes = "-" x (50 - length $full_dir);
408
409 my @fs = glob "*";
410 my $found_tests = (0 != (grep { $_ =~ /\.vgperf$/ } @fs));
411
412 if ($found_tests) {
413 print "-- Running tests in $full_dir $dashes\n";
414 }
415 foreach my $f (@fs) {
416 if (-d $f) {
417 test_one_dir($f, $full_dir);
418 } elsif ($f =~ /\.vgperf$/) {
419 do_one_test($full_dir, $f);
420 }
421 }
422 if ($found_tests) {
423 print "-- Finished tests in $full_dir $dashes\n";
424 }
425
njn356ffa32006-11-03 19:35:20 +0000426 chdir("$initial_dir");
njnec0c27a2005-12-10 23:11:28 +0000427}
428
429#----------------------------------------------------------------------------
430# Summarise results
431#----------------------------------------------------------------------------
432sub summarise_results
433{
434 printf("\n== %d programs, %d timings =================\n\n",
435 $num_tests_done, $num_timings_done);
436}
437
438#----------------------------------------------------------------------------
439# main()
440#----------------------------------------------------------------------------
philippe215b2972012-07-24 19:47:46 +0000441sub warn_about_EXTRA_REGTEST_OPTS()
442{
443 print "WARNING: \$EXTRA_REGTEST_OPTS is set. You probably don't want\n";
444 print "to run the perf tests with it set, unless you are doing some\n";
445 print "strange experiment, and/or you really know what you are doing.\n";
446 print "\n";
447}
njnec0c27a2005-12-10 23:11:28 +0000448
449# nuke VALGRIND_OPTS
450$ENV{"VALGRIND_OPTS"} = "";
451
philippe215b2972012-07-24 19:47:46 +0000452if ($ENV{"EXTRA_REGTEST_OPTS"}) {
453 print "\n";
454 warn_about_EXTRA_REGTEST_OPTS();
455}
456
njnec0c27a2005-12-10 23:11:28 +0000457my @fs = process_command_line();
458foreach my $f (@fs) {
459 if (-d $f) {
460 test_one_dir($f, "");
461 } else {
462 # Allow the .vgperf suffix to be given or omitted
463 if ($f =~ /.vgperf$/ && -r $f) {
464 # do nothing
465 } elsif (-r "$f.vgperf") {
466 $f = "$f.vgperf";
467 } else {
468 die "`$f' neither a directory nor a readable test file/name\n"
469 }
470 my $dir = `dirname $f`; chomp $dir;
471 my $file = `basename $f`; chomp $file;
472 chdir($dir) or die "Could not change into $dir\n";
473 do_one_test($dir, $file);
474 chdir($tests_dir);
475 }
476}
477summarise_results();
478
philippe215b2972012-07-24 19:47:46 +0000479if ($ENV{"EXTRA_REGTEST_OPTS"}) {
480 warn_about_EXTRA_REGTEST_OPTS();
481}
482
njnec0c27a2005-12-10 23:11:28 +0000483##--------------------------------------------------------------------##
484##--- end ---##
485##--------------------------------------------------------------------##