GitLab maintenance scheduled for Tomorrow, 2020-03-31, from 17:00 to 18:00 CT - Services will be unavailable during this time.

parallel-fsstats.pl 5.69 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl -w
#
#  (C) 2010 by Argonne National Laboratory.
#      See COPYRIGHT in top-level directory.
#

use Cwd;
use Getopt::Long;
use English;
10
use File::Temp qw(tempfile);
11 12 13

my $hostfile;
my $dirfile;
14 15
my $restartfile;
my $shareddir;
16 17 18 19
my @hosts;
my @dirs;
my %pidmapping = ();
my %pidmapping_dir = ();
20 21 22
my %pidmapping_time = ();
my %dirtiming = ();
my %chkpntlog = ();
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

process_args();

open(FILE, $hostfile) or die("Error opening hostfile.");
# read file into an array
@hosts = <FILE>;
chomp(@hosts);
close(FILE);

open(FILE, $dirfile) or die("Error opening dirfile.");
# read file into an array
@dirs = <FILE>;
chomp(@dirs);
close(FILE);

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
open(FILE, $restartfile) or goto SKIP;
while (<FILE>)
{
    my $dir;
    my $checkpoint;
    my $status;
    my $elapsedtime;
    chomp;
    ($status, $elapsedtime, $checkpoint, $dir) = split(/,/, $_, 4);
    $chkpntlog{$dir} = [$checkpoint, $status, $elapsedtime];
}
close(FILE);
SKIP:

my $process_forked = 0;

54
# loop as long as we still have directories to process or outstanding jobs
55
ITERATION: while ($#dirs > -1 || keys(%pidmapping))
56 57 58 59 60 61
{
    if($#hosts > -1 && $#dirs > -1)
    {
        # we have work to do and a free host to do it on

        # grab a directory and host out of the lists
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        my $dir  = pop(@dirs);
        my $need_restart = 0;

        if (not defined $chkpntlog{$dir})
        {
            my ($fd, $chkname) = tempfile("checkpoints/chkXXXXXXXX", DIR=>$shareddir,
                                          SUFFIX=>".fsstats");
            close($fd);
            $chkpntlog{$dir} = [$chkname, 0, 0];
            $need_restart = 0;
        }
        else
        {
            $need_restart = 1;
        }

        my $info = $chkpntlog{$dir};

        $dirtiming{$dir} = $info->[2];

        #
        # Check to see if this dir is completed
        #
        if ($info->[1] == 1)
        {
            print("dir completed: $dir\n");
            next ITERATION;
        }
        if ($need_restart == 1 && $info->[1] == 0)
        {
            print("restarting dir: $dir\n");
        }

95 96 97 98 99 100 101 102
        my $host = pop(@hosts);

        my $pid = fork();
        if (not defined $pid) 
        {
            die("Error: could not fork.");
        }
            
103
        my $cmd = "./fsstats-runner.bash $host $dir $info->[0] $need_restart";
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

        if($pid == 0)
        {
            # child runs remote command
            my $error_code = 0;
            system($cmd);

            # look for exec problem, signal, or command error code
            if (($? == -1) || ($? & 127) || (($? >> 8) != 0))
            {
                $error_code = 1;
            }
            else
            {
                $error_code = 0;
            }

            # propagate an error code to parent
            exit($error_code);
        }
        else
        {
            print("fsstats of $dir on $host started...\n");
            # parent keeps up with what child is running where
            $pidmapping{$pid} = $host;
            $pidmapping_dir{$pid} = $dir;
130 131
            $pidmapping_time{$pid} = time();
            $process_forked = 1;
132 133 134 135 136 137
        }
    }
    else
    {
        # we have launched as much as we can; wait for something to finish

138 139 140
        if ($process_forked)
        {

141 142 143 144 145 146
        my $child = waitpid(-1, 0);
        if($child < 1)
        {
            die("Error: lost track of a child process.\n");
        }
        my $child_error_code = $?;
147
        my $elapsedtime = time() - $pidmapping_time{$child};
148 149 150 151 152 153 154
        print("fsstats of $pidmapping_dir{$child} on $pidmapping{$child} ");
        if($child_error_code == 0)
        {
            print(" SUCCESS.\n");
        }
        else
        {
155
            print(" FAILURE [$child_error_code] (continuing).\n");
156
        }
157 158 159 160 161 162 163

        # update checkpoint
        my $update = $chkpntlog{$pidmapping_dir{$child}};
        $update->[1] = 1;
        $update->[2] = $elapsedtime;
        $chkpntlog{$pidmapping_dir{$child}} = $update;
        dump_checkpoint($restartfile, \%chkpntlog);
164
        
165 166 167
        # store total time to process directory
        $dirtiming{$pidmapping_dir{$child}} = $elapsedtime;

168 169 170 171
        # put the host back on the queue
        push(@hosts, $pidmapping{$child});
        delete($pidmapping{$child});
        delete($pidmapping_dir{$child});
172 173 174
        delete($pidmapping_time{$child});

        }
175 176 177
    }
}

178 179 180 181 182 183 184 185 186 187
foreach my $key (sort { $dirtiming{$b} cmp $dirtiming{$a} } keys %dirtiming)
{
    print "$key $dirtiming{$key}\n";
}

exit 0;

#
# Argument Parsing
#
188 189
sub process_args
{
190
    my $help_switch;
191 192

    Getopt::Long::Configure("no_ignore_case", "bundling");
193 194 195
    GetOptions( "help" => \$help_switch,
                "restart=s" => \$restartfile,
                "shareddir=s" => \$shareddir );
196

197
    if($help_switch)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    {
        print_help();
        exit(0);
    }

    # there should be two remaining arguments (hostfile and dirfile)
    if($#ARGV != 1)
    {
        print "Error: invalid arguments.\n";
        print_help();
        exit(1);
    }

    $hostfile = $ARGV[0];
    $dirfile = $ARGV[1];

    return;
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
#
# Dumps checkpoint to restart file.
# Rewrites the whole file.
#
sub dump_checkpoint
{
    my $file = shift;
    my $hash = shift;

    open (CHKPNT, ">", $file) or die "checkpoint failed: $file\n";
    foreach my $key (keys %$hash)
    {
        my $item = $hash->{$key};
        #print CHKPNT "$key,$item->[0],$item->[1]\n";
        print CHKPNT "$item->[1],$item->[2],$item->[0],$key\n";
    }
    close (CHKPNT);
}

#
# Usage info
#
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
sub print_help
{
    print <<EOF;

Usage: $PROGRAM_NAME <hostfile> <dirfile>

    --help          Prints this help message

Purpose:

    This script runs parallel copies of fsstats on each directory listed in
    the <dirfile>.  The <hostfile> specifies a list of hosts to run fsstats
    on via ssh.

EOF
    return;
}