darshan-gather-intervals.pl 7.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/usr/bin/perl -w

# This script will go through all of the darshan logs in a given
# subdirectory and produce a file summarizing each job (id, start unix,
# nprocs, bytes read, bytes written, perf estimate) and then for each job
# generate two text files listing the read and write intervals (start, end,
# nbytes)

use strict;
use File::Find;

sub wanted
{
    my $file = $_;
    my $line;
    my $version = 0.0;
    my $nprocs = 0;
    my $start = 0;
    my $end = 0;
    my $start_a = "";
    my $end_a = "";
    my $jobid = 0;
    my $bytes_r = 0;
    my $bytes_w = 0;
    my $perf = 0.0;
Philip Carns's avatar
Philip Carns committed
26 27 28 29
    my $read_interval = {};
    my $write_interval = {};
    my @read_interval_array = ();
    my @write_interval_array = ();
Philip Carns's avatar
Philip Carns committed
30 31
    my @read_merged = ();
    my @write_merged = ();
Philip Carns's avatar
Philip Carns committed
32
    my @fields = ();
Philip Carns's avatar
Philip Carns committed
33 34 35 36 37
    my $idle_after;
    my $duration;
    my $rbytes = 0;
    my $wbytes = 0;
    my $uid = 0;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    # only operate on darshan log files
    $file =~ /\.darshan\.gz$/ or return;    

    print("current file: $File::Find::name\n");

    # grab jobid from name, old logs don't store it in the file
    if($file =~ /_id(\d+)_/) {
        $jobid = $1;
    }

    if(!(open(PARSE, "darshan-parser $file |")))
    {
        print(STDERR "Failed to parse $File::Find::name\n");
        return;
    }

    while ($line = <PARSE>) {
Philip Carns's avatar
Philip Carns committed
56 57 58
 
        if ($line =~ /^\s*$/) {
            # ignore blank lines
59
        }
Philip Carns's avatar
Philip Carns committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        elsif ($line =~ /^#/) {
            if($line =~ /nprocs: (\S+)/) {
                $nprocs = $1;
            }
            if($line =~ /start_time: (\S+)/) {
                $start = $1;
            }
            if($line =~ /end_time: (\S+)/) {
                $end = $1;
            }
            if($line =~ /start_time_asci: (.+)/) {
                $start_a = "$1";
            }
            if($line =~ /end_time_asci: (.+)/) {
                $end_a = "$1";
            }
Philip Carns's avatar
Philip Carns committed
76 77 78
            if($line =~ /uid: (\S+)/) {
                $uid = "$1";
            }
79
        }
Philip Carns's avatar
Philip Carns committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        else {
            # parse line
            @fields = split(/[\t ]+/, $line);
	
            if ($fields[2] eq "CP_BYTES_READ") {
                $read_interval->{bytes} = $fields[3];
                #print("read interval bytes: $fields[3]\n");
            }
            if ($fields[2] eq "CP_BYTES_WRITTEN") {
                $write_interval->{bytes} = $fields[3];
                #print("write interval bytes: $fields[3]\n");
            }
            if ($fields[2] eq "CP_F_READ_START_TIMESTAMP") {
                $read_interval->{start} = $fields[3];
            }
            if ($fields[2] eq "CP_F_WRITE_START_TIMESTAMP") {
                $write_interval->{start} = $fields[3];
            }
            if ($fields[2] eq "CP_F_READ_END_TIMESTAMP") {
                $read_interval->{end} = $fields[3];
                if($read_interval->{bytes} > 0) {
                    #print("pushing read interval bytes $read_interval->{bytes}\n");
                    push(@read_interval_array, $read_interval);
                    $read_interval = {};
                }
            }
            if ($fields[2] eq "CP_F_WRITE_END_TIMESTAMP") {
                $write_interval->{end} = $fields[3];
                if($write_interval->{bytes} > 0) {
                    #print("pushing write interval bytes $write_interval->{bytes}\n");
                    push(@write_interval_array, $write_interval);
                    $write_interval = {};
                }
            }

115
        }
Philip Carns's avatar
Philip Carns committed
116

117 118 119 120 121 122 123
    }

    if(!(close(PARSE)))
    {
        print(STDERR "Failed to parse $File::Find::name\n");
        return;
    }
Philip Carns's avatar
Philip Carns committed
124 125 126 127

    @read_interval_array = sort { $a->{start} <=> $b->{start} } @read_interval_array;
    @write_interval_array = sort { $a->{start} <=> $b->{start} } @write_interval_array;

Philip Carns's avatar
Philip Carns committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    my $i;
    if($#read_interval_array > -1){
        push(@read_merged, $read_interval_array[0]);
    }
    if($#read_interval_array > 0){
        for $i (1 .. $#read_interval_array)
        {
            if($read_interval_array[$i]->{start} <=
                $read_merged[$#read_merged]->{end})
            {
                $read_merged[$#read_merged]->{bytes} +=
                    $read_interval_array[$i]->{bytes};
                if($read_interval_array[$i]->{end} > $read_merged[$#read_merged]->{end}) {
                    $read_merged[$#read_merged]->{end} = 
                        $read_interval_array[$i]->{end};
                }
            }
            else
            {
                push(@read_merged, $read_interval_array[$i]);
            }
        }
    }

    if($#write_interval_array > -1){
        push(@write_merged, $write_interval_array[0]);
    }
    if($#write_interval_array > 0){
        for $i (1 .. $#write_interval_array)
        {
            if($write_interval_array[$i]->{start} <=
                $write_merged[$#write_merged]->{end})
            {
                $write_merged[$#write_merged]->{bytes} +=
                    $write_interval_array[$i]->{bytes};
                if($write_interval_array[$i]->{end} > $write_merged[$#write_merged]->{end}) {
                    $write_merged[$#write_merged]->{end} = 
                        $write_interval_array[$i]->{end};
                }
            }
            else
            {
                push(@write_merged, $write_interval_array[$i]);
            }
        }
    }

175
    if(!(open(WFILE, ">$jobid-$start-write-intervals.dat")))
Philip Carns's avatar
Philip Carns committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    {
        print(STDERR "Failed to open $jobid-write-intervals.dat\n");
        return;
    }
    print(WFILE "#<bytes>\t<start>\t<end>\t<duration>\t<idle time after>\n");

    if($#write_merged > -1) {
        for $i (0 .. $#write_merged)
        {
            if($i == $#write_merged) {
                $idle_after = ($end-$start + 1)-$write_merged[$i]->{end};
            }
            else {
                $idle_after = $write_merged[$i+1]->{start} -
                    $write_merged[$i]->{end};
            }
            $wbytes += $write_merged[$i]->{bytes};
            $duration = $write_merged[$i]->{end} - $write_merged[$i]->{start};
            print(WFILE
            "$write_merged[$i]->{bytes}\t$write_merged[$i]->{start}\t$write_merged[$i]->{end}\t$duration\t$idle_after\n");
        }
    }

    close(WFILE);

201
    if(!(open(RFILE, ">$jobid-$start-read-intervals.dat")))
Philip Carns's avatar
Philip Carns committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    {
        print(STDERR "Failed to open $jobid-read-intervals.dat\n");
        return;
    }
    print(RFILE "#<bytes>\t<start>\t<end>\t<duration>\t<idle time after>\n");

    if($#read_merged > -1) {
        for $i (0 .. $#read_merged)
        {
            if($i == $#read_merged) {
                $idle_after = ($end-$start + 1)-$read_merged[$i]->{end};
            }
            else {
                $idle_after = $read_merged[$i+1]->{start} -
                    $read_merged[$i]->{end};
            }
            $rbytes += $read_merged[$i]->{bytes};
            $duration = $read_merged[$i]->{end} - $read_merged[$i]->{start};
            print(RFILE
            "$read_merged[$i]->{bytes}\t$read_merged[$i]->{start}\t$read_merged[$i]->{end}\t$duration\t$idle_after\n");
        }
    }

    close(RFILE);

    print(SUMMARY
    "$uid\t$jobid\t\"$start_a\"\t\"$end_a\"\t$start\t$end\t$nprocs\t$rbytes\t$wbytes\n"); 
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

sub main
{
    my @paths;

    if($#ARGV < 0) {
        die("usage: darshan-gather-intervals.pl <one or more log directories>\n");
    }

    @paths = @ARGV;

    if(!(open(SUMMARY, ">>summary.txt")))
    {
        print(STDERR "Failed to open summary.txt\n");
        return;
    }

Philip Carns's avatar
Philip Carns committed
247
    print(SUMMARY "#<uid>\t<jobid>\t<start ascii>\t<end ascii>\t<start unix>\t<end unix>\t<nprocs>\t<read bytes>\t<write bytes>\n"); 
248

Philip Carns's avatar
Philip Carns committed
249
    find({wanted=>\&wanted, no_chdir => 1}, @paths);
250 251 252 253 254 255 256 257 258 259 260

}

main();

# Local variables:
#  c-indent-level: 4
#  c-basic-offset: 4
# End:
#  
# vim: ts=8 sts=4 sw=4 expandtab