rlm@23: #!/usr/bin/env perl
rlm@23: # -*- perl -*-
rlm@23:   
rlm@23: #############################################################
rlm@23: #
rlm@23: # Benchmark setup for HAsim
rlm@23: #
rlm@23: # Usage: setup-bmark <bmark> <srcdir> <destdir>
rlm@23: #
rlm@23: # Setup benchmark to run in <destdir>
rlm@23: #
rlm@23: #############################################################
rlm@23: 
rlm@23: use strict;
rlm@23: use warnings;
rlm@23: use File::Basename;
rlm@23: use Getopt::Long;
rlm@23: 
rlm@23: sub Usage();
rlm@23: sub ErrorExit($);
rlm@23: 
rlm@23: #
rlm@23: # Turn on warnings
rlm@23: #
rlm@23: $^W = 1;
rlm@23: 
rlm@23: my $binary = undef;
rlm@23: my $compare = undef;
rlm@23: my $data = undef;
rlm@23: my $feeder = '';
rlm@23: my $group = undef;
rlm@23: my $isa = "unknown_ISA";
rlm@23: my $linkto = undef;
rlm@23: my $silent = undef;
rlm@23: 
rlm@23: #
rlm@23: # Find the root of the benchmarks directory tree.
rlm@23: #
rlm@23: my $benchmark_root = `awb-resolver --config=benchmarkdir`;
rlm@23: chomp($benchmark_root);
rlm@23: ErrorExit("Can't find benchmark root") if ($benchmark_root eq '');
rlm@23: 
rlm@23: #
rlm@23: # Parse the command line switches
rlm@23: #
rlm@23: if (! GetOptions('binary=s' => \$binary,
rlm@23:                  'compare=s' => \$compare,
rlm@23:                  'data=s' => \$data,
rlm@23:                  'feeder=s' => \$feeder,
rlm@23:                  'group=s' => \$group,
rlm@23:                  'isa=s' => \$isa,
rlm@23:                  'linkto=s' => \$linkto,
rlm@23:                  'silent=s' => \$silent,
rlm@23:                 )) {
rlm@23:     Usage();
rlm@23: }
rlm@23: 
rlm@23: if ($#ARGV != 2) {
rlm@23:     Usage();
rlm@23: }
rlm@23: 
rlm@23: my $bmark = $ARGV[0];
rlm@23: my $srcdir = $ARGV[1];
rlm@23: my $dstdir = $ARGV[2];
rlm@23: my $basedir = `pwd`;
rlm@23: chomp($basedir);
rlm@23: 
rlm@23: print "BMARK: $bmark\n";
rlm@23: print "SRC: $srcdir\n";
rlm@23: print "DST: $dstdir\n";
rlm@23: print "BASE: $basedir\n";
rlm@23: 
rlm@23: if (! defined($group)) {
rlm@23:     $group = $bmark;
rlm@23: }
rlm@23: 
rlm@23: 
rlm@23: chdir($dstdir) or ErrorExit("Failed to cd to $dstdir");
rlm@23: 
rlm@23: mkdir('hasim_debug') or ErrorExit("Failed to make 'hasim_debug' directory");
rlm@23: 
rlm@23: # Set up benchmark for non-null feeders
rlm@23: if ($feeder ne "none") {
rlm@23:     my $src_prog;
rlm@23: 
rlm@23:     mkdir('program') or ErrorExit("Failed to make 'program' directory");
rlm@23: 
rlm@23:     if (defined($binary)) {
rlm@23:         $src_prog = "${benchmark_root}/hasim/${isa}/${group}/${binary}";
rlm@23:     }
rlm@23:     elsif (-f "${benchmark_root}/hasim/${isa}/${group}/${bmark}.${isa}.bin") {
rlm@23:         $src_prog = "${benchmark_root}/hasim/${isa}/${group}/${bmark}.${isa}.bin";
rlm@23:     }
rlm@23:     elsif (-f "${benchmark_root}/hasim/${isa}/${group}/${bmark}.${isa}.vmh") {
rlm@23:         $src_prog = "${benchmark_root}/hasim/${isa}/${group}/${bmark}.${isa}.vmh";
rlm@23:     }
rlm@23:     else {
rlm@23:         print STDERR "Can't find binary in ${benchmark_root}/hasim/${isa}/${group}\n";
rlm@23:         exit(1);
rlm@23:     }
rlm@23: 
rlm@23:     my $dst_prog = "program/" . basename($src_prog);
rlm@23: 
rlm@23:     unlink($dst_prog);
rlm@23:     symlink($src_prog, $dst_prog) or ErrorExit("Failed to symlink $dst_prog => $src_prog");
rlm@23: }
rlm@23: 
rlm@23: # Copy input data files
rlm@23: if (defined($data)) {
rlm@23:    # No trailing slash. Just link to a single object
rlm@23:    print "data: $data\n";
rlm@23:    symlink("${srcdir}/${data}", "input.wav") or die("Failed to link to $data");
rlm@23: }
rlm@23: 
rlm@23: # Link to files or directories
rlm@23: if (defined($linkto)) {
rlm@23:     foreach my $tgt (split(':', $linkto)) {
rlm@23:         if ($tgt =~ /\/$/) {
rlm@23:             # Trailing slash means link to all the files individually within
rlm@23:             # a directory.
rlm@23:             if (-d $tgt) {
rlm@23:                 opendir(DIR, $tgt) || die("Cannot open directory for --linkto $tgt\n");
rlm@23:                 my @tgt_objs = readdir(DIR);
rlm@23:                 closedir(DIR);
rlm@23:                 foreach my $t (@tgt_objs) {
rlm@23:                     if (! (($t eq '.') || ($t eq '..'))) {
rlm@23:                         symlink("${srcdir}/${tgt}${t}", basename($t)) or die("Failed to link to ${tgt}${t}");
rlm@23:                     }
rlm@23:                 }
rlm@23:             }
rlm@23:         }
rlm@23:         else {
rlm@23:             # No trailing slash. Just link to a single object
rlm@23:             symlink("${srcdir}/${tgt}", basename($tgt)) or die("Failed to link to $tgt");
rlm@23:         }
rlm@23:     }
rlm@23: }
rlm@23: 
rlm@23: # Store compare rules to config
rlm@23: open(ENV, '>>config/env.sh') or die("Failed to open config/env.sh");
rlm@23: print ENV "ISA=\"${isa}\"\n";
rlm@23: if (defined($compare)) {
rlm@23:     print ENV "compare=\"${compare}\"\n";
rlm@23: }
rlm@23: close(ENV);
rlm@23: 
rlm@23: #store silent mode to config
rlm@23: open(ENV, '>>config/env.sh') or die("Failed to open config/env.sh");
rlm@23: if (defined($silent)) {
rlm@23:     print ENV "silent=1\n";
rlm@23: }
rlm@23: close(ENV);
rlm@23: 
rlm@23: # Set up m5 environment
rlm@23: if ($feeder eq 'm5') {
rlm@23:     system("(cd $srcdir; tar cf - ./m5_configs) | tar xf -");
rlm@23: }
rlm@23: 
rlm@23: system("cp -f ${srcdir}/run-freq.bmark run");
rlm@23: chmod(0755, "run");
rlm@23: 
rlm@23: exit(0);
rlm@23: 
rlm@23: 
rlm@23: 
rlm@23: sub Usage() {
rlm@23:     print STDERR "Usage: setup-bmark [--binary <name>]\n";
rlm@23:     print STDERR "                   [--compare <compare commands>]\n";
rlm@23:     print STDERR "                   [--data <tar file>]\n";
rlm@23:     print STDERR "                   [--group <name>]\n";
rlm@23:     print STDERR "                   [--isa <name>]\n";
rlm@23:     print STDERR "                   [--linkto <target0>:...:<targetN>]\n";
rlm@23:     print STDERR "                   [--silent] \n";
rlm@23:     print STDERR "                   <bmark> <srcdir> <dstdir>\n";
rlm@23:     exit(1);
rlm@23: }
rlm@23: 
rlm@23: 
rlm@23: sub ErrorExit($) {
rlm@23:     print STDERR @_ . "\n";
rlm@23:     exit(1);
rlm@23: }