blob: fbd287c612154e6b016cd40be55bcff0ac821fa8 [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#----------------------------------------------------------------------------
30# usage: vg_perf [options] <dirs | files>
31#
32# Options:
33# --all: run tests in all subdirs
34# --valgrind: valgrind to use (the directory it's in). Default is the one
35# in the current tree.
36#
37# The easiest way is to run all tests in valgrind/ with (assuming you installed
38# in $PREFIX):
39#
40# perl perf/vg_perf --all
41#
42# You can specify individual files to test, or whole directories, or both.
43# Directories are traversed recursively, except for ones named, for example,
44# CVS/ or docs/.
45#
46# Each test is defined in a file <test>.vgperf, containing one or more of the
47# following lines, in any order:
48# - prog: <prog to run> (compulsory)
49# - tools: <Valgrind tools> (compulsory)
50# - args: <args for prog> (default: none)
51# - vgopts: <Valgrind options> (default: none)
52# - prereq: <prerequisite command> (default: none)
53# - cleanup: <post-test cleanup cmd to run> (default: none)
54#
55# The prerequisite command, if present, must return 0 otherwise the test is
56# skipped.
57#----------------------------------------------------------------------------
58
59use warnings;
60use strict;
61
62#----------------------------------------------------------------------------
63# Global vars
64#----------------------------------------------------------------------------
65my $usage="vg_perf [--all, --valgrind]\n";
66
67my $tmp="vg_perf.tmp.$$";
68
69# Test variables
70my $vgopts; # valgrind options
71my $prog; # test prog
72my $args; # test prog args
73my $prereq; # prerequisite test to satisfy before running test
74my $cleanup; # cleanup command to run
75my @tools; # which tools are we measuring the program with
76
77# Abbreviations used in output
78my %toolnames = (
79 none => "nl",
80 memcheck => "mc",
81 cachegrind => "cg",
82 massif => "ms"
83);
84
85# We run each program this many times and choose the best time.
86my $n_runs = 3;
87
88my $num_tests_done = 0;
89my $num_timings_done = 0;
90
91# Starting directory
92chomp(my $tests_dir = `pwd`);
93
94# Directory of the Valgrind being measured. Default is the one in the
95# current tree.
96my $vg_dir = $tests_dir;
97
98#----------------------------------------------------------------------------
99# Process command line, setup
100#----------------------------------------------------------------------------
101
102# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
103#
104# 1. Can prepend "." onto programs to avoid trouble with users who don't have
105# "." in their path (by making $dir = ".")
106# 2. Can prepend the current dir to make the command absolute to avoid
107# subsequent trouble when we change directories.
108#
109# Also checks the program exists and is executable.
110sub validate_program ($$$$)
111{
112 my ($dir, $prog, $must_exist, $must_be_executable) = @_;
113
114 # If absolute path, leave it alone. If relative, make it
115 # absolute -- by prepending current dir -- so we can change
116 # dirs and still use it.
117 $prog = "$dir/$prog" if ($prog !~ /^\//);
118 if ($must_exist) {
119 (-f $prog) or die "vg_perf: '$prog' not found or not a file ($dir)\n";
120 }
121 if ($must_be_executable) {
122 (-x $prog) or die "vg_perf: '$prog' not executable ($dir)\n";
123 }
124
125 return $prog;
126}
127
128sub validate_tools($)
129{
130 # XXX: should check they exist!
131 my ($toolnames) = @_;
132 my @t = split(/\s+/, $toolnames);
133 return @t;
134}
135
136sub process_command_line()
137{
138 my $alldirs = 0;
139 my @fs;
140
141 for my $arg (@ARGV) {
142 if ($arg =~ /^-/) {
143 if ($arg =~ /^--all$/) {
144 $alldirs = 1;
145 } elsif ($arg =~ /^--valgrind=(.*)$/) {
146 $vg_dir = $1;
147 } else {
148 die $usage;
149 }
150 } else {
151 push(@fs, $arg);
152 }
153 }
154 # Make $vg_dir absolute if not already
155 if ($vg_dir !~ /^\//) { $vg_dir = "$tests_dir/$vg_dir"; }
156 validate_program($vg_dir, "./coregrind/valgrind", 1, 1);
157
158 if ($alldirs) {
159 @fs = ();
160 foreach my $f (glob "*") {
161 push(@fs, $f) if (-d $f);
162 }
163 }
164
165 (0 != @fs) or die "No test files or directories specified\n";
166
167 return @fs;
168}
169
170#----------------------------------------------------------------------------
171# Read a .vgperf file
172#----------------------------------------------------------------------------
173sub read_vgperf_file($)
174{
175 my ($f) = @_;
176
177 # Defaults.
178 ($vgopts, $prog, $args, $prereq, $cleanup)
179 = ("", undef, "", undef, undef, undef, undef);
180
181 open(INPUTFILE, "< $f") || die "File $f not openable\n";
182
183 while (my $line = <INPUTFILE>) {
184 if ($line =~ /^\s*#/ || $line =~ /^\s*$/) {
185 next;
186 } elsif ($line =~ /^\s*vgopts:\s*(.*)$/) {
187 $vgopts = $1;
188 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
njnc9582a22005-12-13 21:44:48 +0000189 $prog = validate_program(".", $1, 1, 1);
njnec0c27a2005-12-10 23:11:28 +0000190 } elsif ($line =~ /^\s*tools:\s*(.*)$/) {
191 @tools = validate_tools($1);
192 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
193 $args = $1;
194 } elsif ($line =~ /^\s*prereq:\s*(.*)$/) {
195 $prereq = $1;
196 } elsif ($line =~ /^\s*cleanup:\s*(.*)$/) {
197 $cleanup = $1;
198 } else {
199 die "Bad line in $f: $line\n";
200 }
201 }
202 close(INPUTFILE);
203
204 if (!defined $prog) {
205 $prog = ""; # allow no prog for testing error and --help cases
206 }
207 if (0 == @tools) {
208 die "vg_perf: missing 'tools' line in $f\n";
209 }
210}
211
212#----------------------------------------------------------------------------
213# Do one test
214#----------------------------------------------------------------------------
215# Since most of the program time is spent in system() calls, need this to
216# propagate a Ctrl-C enabling us to quit.
217sub mysystem($)
218{
njnecb47442005-12-13 16:54:58 +0000219 my ($cmd) = @_;
220 my $retval = system($cmd);
221 if ($retval == 2) {
222 exit 1;
223 } else {
224 return $retval;
225 }
njnec0c27a2005-12-10 23:11:28 +0000226}
227
njn02383bc2005-12-13 20:23:38 +0000228# Run program N times, return the best user time.
njnec0c27a2005-12-10 23:11:28 +0000229sub time_prog($$)
230{
231 my ($cmd, $n) = @_;
232 my $tmin = 999999;
233 for (my $i = 0; $i < $n; $i++) {
njnecb47442005-12-13 16:54:58 +0000234 mysystem("echo '$cmd' > perf.cmd");
235 my $retval = mysystem("$cmd > perf.stdout 2> perf.stderr");
236 (0 == $retval) or
njn02383bc2005-12-13 20:23:38 +0000237 die "\n*** Command returned non-zero: $cmd"
njnecb47442005-12-13 16:54:58 +0000238 . "\n*** See perf.{cmd,stdout,stderr} to diagnose what went wrong.\n";
239 my $out = `cat perf.stderr`;
njn02383bc2005-12-13 20:23:38 +0000240 ($out =~ /usertime: ([\d\.]+)s/) or
241 die "\n*** missing usertime in perf.stderr\n";
njnec0c27a2005-12-10 23:11:28 +0000242 $tmin = $1 if ($1 < $tmin);
243 }
244 return $tmin;
245}
246
247sub do_one_test($$)
248{
249 my ($dir, $vgperf) = @_;
250 $vgperf =~ /^(.*)\.vgperf/;
251 my $name = $1;
252
253 read_vgperf_file($vgperf);
254
255 if (defined $prereq) {
256 if (system("$prereq") != 0) {
257 printf("%-16s (skipping, prereq failed: $prereq)\n", "$name:");
258 return;
259 }
260 }
261
262 printf("%-12s", "$name:");
263
njn02383bc2005-12-13 20:23:38 +0000264 my $timecmd = "/usr/bin/time -f 'usertime: %Us'";
njnec0c27a2005-12-10 23:11:28 +0000265
266 # Do the native run(s).
267 printf("nt:");
268 my $cmd = "$timecmd $prog $args";
269 my $tNative = time_prog($cmd, $n_runs);
270 printf("%4.1fs ", $tNative);
271
272 foreach my $tool (@tools) {
273 (defined $toolnames{$tool}) or
274 die "unknown tool $tool, please add to %toolnames\n";
275
276 # Do the tool run(s). Set both VALGRIND_LIB and VALGRIND_LIB_INNER
277 # in case this Valgrind was configured with --enable-inner.
278 printf("%s:", $toolnames{$tool});
279 my $vgsetup = "VALGRIND_LIB=$vg_dir/.in_place "
280 . "VALGRIND_LIB_INNER=$vg_dir/.in_place ";
281 my $vgcmd = "$vg_dir/coregrind/valgrind "
282 . "--command-line-only=yes --tool=$tool -q "
283 . "--memcheck:leak-check=no --addrcheck:leak-check=no "
284 . "$vgopts ";
285 my $cmd = "$vgsetup $timecmd $vgcmd $prog $args";
286 my $tTool = time_prog($cmd, $n_runs);
287 printf("%4.1fs (%4.1fx) ", $tTool, $tTool/$tNative);
288
289 $num_timings_done++;
290 }
291 printf("\n");
292
293 if (defined $cleanup) {
294 (system("$cleanup") == 0) or
295 print(" ($name cleanup operation failed: $cleanup)\n");
296 }
297
298 $num_tests_done++;
299}
300
301#----------------------------------------------------------------------------
302# Test one directory (and any subdirs)
303#----------------------------------------------------------------------------
304sub test_one_dir($$); # forward declaration
305
306sub test_one_dir($$)
307{
308 my ($dir, $prev_dirs) = @_;
309 $dir =~ s/\/$//; # trim a trailing '/'
310
311 # Ignore dirs into which we should not recurse.
312 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
313
314 chdir($dir) or die "Could not change into $dir\n";
315
316 # Nb: Don't prepend a '/' to the base directory
317 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
318 my $dashes = "-" x (50 - length $full_dir);
319
320 my @fs = glob "*";
321 my $found_tests = (0 != (grep { $_ =~ /\.vgperf$/ } @fs));
322
323 if ($found_tests) {
324 print "-- Running tests in $full_dir $dashes\n";
325 }
326 foreach my $f (@fs) {
327 if (-d $f) {
328 test_one_dir($f, $full_dir);
329 } elsif ($f =~ /\.vgperf$/) {
330 do_one_test($full_dir, $f);
331 }
332 }
333 if ($found_tests) {
334 print "-- Finished tests in $full_dir $dashes\n";
335 }
336
337 chdir("..");
338}
339
340#----------------------------------------------------------------------------
341# Summarise results
342#----------------------------------------------------------------------------
343sub summarise_results
344{
345 printf("\n== %d programs, %d timings =================\n\n",
346 $num_tests_done, $num_timings_done);
347}
348
349#----------------------------------------------------------------------------
350# main()
351#----------------------------------------------------------------------------
352
353# nuke VALGRIND_OPTS
354$ENV{"VALGRIND_OPTS"} = "";
355
356my @fs = process_command_line();
357foreach my $f (@fs) {
358 if (-d $f) {
359 test_one_dir($f, "");
360 } else {
361 # Allow the .vgperf suffix to be given or omitted
362 if ($f =~ /.vgperf$/ && -r $f) {
363 # do nothing
364 } elsif (-r "$f.vgperf") {
365 $f = "$f.vgperf";
366 } else {
367 die "`$f' neither a directory nor a readable test file/name\n"
368 }
369 my $dir = `dirname $f`; chomp $dir;
370 my $file = `basename $f`; chomp $file;
371 chdir($dir) or die "Could not change into $dir\n";
372 do_one_test($dir, $file);
373 chdir($tests_dir);
374 }
375}
376summarise_results();
377
378##--------------------------------------------------------------------##
379##--- end ---##
380##--------------------------------------------------------------------##