#!/usr/bin/perl use strict; use Getopt::Long; use IPC::Run qw( run timeout ) ; use Data::Dumper; use constant true => (1==1); use constant false => (1==0); use constant CPU_day => 86400; use constant CPU_month => 86400*365.25/12; use constant CPU_quarter=> 86400*365.25/4; use constant CPU_year => 86400*365.25; my (@cmd1,@cmd2,@cmd3,@lines,@line); my ($rc,$percent,$units,$current,$qacct,$qstat,$totals,$users,$days); my ($months,$quarters,$years,$units_string); my ($in,$out,$err,$cpu,$mem,$user ); $units = 1; # report usage in units of seconds $units_string = "second(s)"; $qacct = "qacct"; # path to qacct, might need full one $qstat = "qstat"; # path to qstat, might need full one $rc = GetOptions ( "year" => \$years, "day" => \$days, "month" => \$months, "quarter" => \$quarters, "cpu" => \$cpu, "mem" => \$mem ); $units = CPU_day if ($days); $units = CPU_month if ($months); $units = CPU_quarter if ($quarters); $units = CPU_year if ($years); $units_string = "day(s)" if ($days); $units_string = "month(s)" if ($months); $units_string = "quarter(s)" if ($quarters); $units_string = "year(s)" if ($years); push @cmd1,$qacct; push @cmd2,($qacct , "-o"); # get the total queue usage and put into hash run \@cmd1, \$in, \$out, \$err, timeout( 30 ) or die "qacct: $?"; @lines = split(/\n/,$out); $lines[3] =~ s/^\s+//; @line = split(/\s+/,$lines[3]); $totals->{wallclock} = $line[0]; $totals->{utime} = $line[1]; $totals->{stime} = $line[2]; $totals->{cpu} = $line[3]; $totals->{mem} = $line[4]; $totals->{io} = $line[5]; $totals->{ipw} = $line[6]; # get the queue usage by user and put into hash undef $out; run \@cmd2, \$in, \$out, \$err, timeout( 30 ) or die "qacct: $?"; @lines = split(/\n/,$out); shift @lines; shift @lines; foreach $user (@lines) { $user =~ s/^\s+//; @line = split(/\s+/,$user); $users->{$line[0]}->{wallclock} = $line[1]; $users->{$line[0]}->{utime} = $line[2]; $users->{$line[0]}->{stime} = $line[3]; $users->{$line[0]}->{cpu} = $line[4]; $users->{$line[0]}->{mem} = $line[5]; $users->{$line[0]}->{io} = $line[6]; $users->{$line[0]}->{ipw} = $line[7]; } # now report totals ... printf "Total usage: (in units of %s)\n",$units_string; printf "\twallclock :\t%12.3f %s\n",$totals->{wallclock}/$units,$units_string; printf "\tuser time :\t%12.3f %s [%.2f\%]\n",$totals->{utime}/$units,$units_string,100*$totals->{utime}/$totals->{wallclock}; printf "\tsystem time:\t%12.3f %s [%.2f\%]\n",$totals->{stime}/$units,$units_string,100*$totals->{stime}/$totals->{wallclock}; printf "\tcpu time :\t%12.3f %s [%.2f\%]\n",$totals->{cpu}/$units,$units_string,100*$totals->{cpu}/$totals->{wallclock}; #loop over users ... printf "\nuser\t\twallclock\tuser time\tsystem time\tcpu time\tmemory\t\tpercent of total time\n"; foreach $user (keys %{$users}) { printf "%-16s%-16.3f%-16.3f%-16.3f%-16.3f%-16.3f%-16.3f\n", $user, $users->{$user}->{wallclock}/$units, $users->{$user}->{utime}/$units, $users->{$user}->{stime}/$units, $users->{$user}->{cpu}/$units, $users->{$user}->{memory}, 100*$users->{$user}->{wallclock}/$totals->{wallclock}; }