[torquedev] showjobs reverse patch

Gareth.Williams at csiro.au Gareth.Williams at csiro.au
Mon Jan 23 23:02:50 MST 2012


Hi Ken,

I started getting annoyed about how long showjobs took to run with many jobs logged.  A simple optimization for what I think is the most common case (getting info about a single recent job) is to parse the logs in reverse order (of date) and stop parsing at the first match.  The patch below reverses the order if a single job is being queried (though it could be an array job) and fixes/simplifies the sub-setting if only a maximum number of logs is to be considered (-n option).  The -o|--oneonly option is needed to actually stop the search after the first match is found - otherwise the match will now usually be early in the search but the full search will be done anyway. 

It would be nice to eliminate or automatically set the oneonly flag.  This would be a bit tricky as I want to  allow for the case of showing all members of an array job. Perhaps it would be better to have an option for that case.

Cheers,

Gareth

--- showjobs.mgb        2012-01-19 15:46:36.319334573 +1100
+++ showjobs.rev        2012-01-24 16:44:49.665131378 +1100
@@ -25,7 +25,7 @@
 my (
     $account,        $endDate,   $full, $group,
     $help,           $man,       $num,  $queue,
-    $specifiedJobId, $startDate, $user
+    $specifiedJobId, $startDate, $user, $oneonly
 );
 GetOptions(
     'account=s'   => \$account,
@@ -39,6 +39,7 @@
     'num=i'       => \$num,
     'startDate=s' => \$startDate,
     'user=s'      => \$user,
+    'oneonly'     => \$oneonly,
 ) or pod2usage(2);
 
 # Display usage if necessary
@@ -83,7 +84,14 @@
   or die
   "Unable to change directory to job_logs directory (${torqueHomeDir}/job_logs): $!\n";
 my @jobFiles = glob("20*");
- at jobFiles = sort @jobFiles;
+if ($specifiedJobId)
+{
+    @jobFiles = reverse sort @jobFiles; # search from most recent log if a particular jobs is specified
+}
+else
+{
+    @jobFiles = sort @jobFiles;
+}
 if ($startDate)
 {
     if ($startDate =~ /^(\d{4})[\/\-](\d{2})[\/\-](\d{2})$/)
@@ -110,8 +118,17 @@
 }
 if ($num)
 {
-    my $firstIndex = ($num <= @jobFiles) ? @jobFiles - $num : 0;
-    my $lastIndex = $#jobFiles;
+    my ($firstIndex, $lastIndex);
+    if ($specifiedJobId)
+    {
+        $firstIndex = ($num <= @jobFiles) ? @jobFiles - $num : 0;
+        $lastIndex = $#jobFiles;
+    }
+    else
+    {
+        $firstIndex = 0;
+        $lastIndex = ($num < $#jobFiles) ? $num : $#jobFiles;
+    }
     @jobFiles = @jobFiles[$firstIndex .. $lastIndex];
 }
 
@@ -121,7 +138,7 @@
 my %jobAttr = ();
 my @jobs    = ();
 my $context = '';
-foreach my $jobFile (@jobFiles)
+OUTER: foreach my $jobFile (@jobFiles)
 {
     open JOBS, "< $jobFile"
       or die "Unable to open job file ($jobFile) for reading: $!\n";
@@ -299,6 +316,7 @@
             my %jobAttrCopy = %jobAttr;
             push @jobs, \%jobAttrCopy;
         }
+        last OUTER if @jobs and $oneonly;
     }
 }
 
@@ -491,6 +509,10 @@
 
 Show only job records matching the specified user.
 
+=item B<-o | --oneonly>
+
+Show only the first job record found. This will mostly be much faster and give the same result as if the flag is omitted if the search is for a specific non-array job or specific array job member.
+
 =item B<-? | --help>
 
 brief help message


More information about the torquedev mailing list