diff mbox series

[OSSTEST,28/60] parallel by fork: Break out into HistoryReport

Message ID 20200814172205.9624-29-ian.jackson@eu.citrix.com (mailing list archive)
State New, archived
Headers show
Series Speed up sg-report-job-history | expand

Commit Message

Ian Jackson Aug. 14, 2020, 5:21 p.m. UTC
Move this code from sg-report-host-history to HistoryReport, so that
it can be reused.

No significant functional change.  Some changes to debug messages.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
 Osstest/HistoryReport.pm | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
 sg-report-host-history   | 49 +++++++++-------------------------------------
 2 files changed, 60 insertions(+), 40 deletions(-)
diff mbox series

Patch

diff --git a/Osstest/HistoryReport.pm b/Osstest/HistoryReport.pm
index 6a23cfba..0b206de4 100644
--- a/Osstest/HistoryReport.pm
+++ b/Osstest/HistoryReport.pm
@@ -19,6 +19,9 @@  package Osstest::HistoryReport;
 use strict;
 use warnings;
 
+use Osstest;
+use Osstest::Executive;
+
 BEGIN {
     use Exporter ();
     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@@ -32,6 +35,7 @@  BEGIN {
 			 cacheable_fn
 			 cache_write_entry
 			 cache_finish
+			 parallel_by_fork
 		    );
     %EXPORT_TAGS = ();
 
@@ -213,4 +217,51 @@  sub cache_finish ($$) {
     print ::DEBUG " extra=$rows_extra\n";
 }
 
+our %children;
+our $worst = 0;
+our $whoami;
+
+sub wait_for_max_children ($) {
+    my ($lim) = @_;
+    while (keys(%children) > $lim) {
+	$!=0; $?=0; my $got = wait;
+	die "$! $got $?" unless exists $children{$got};
+	my $host = $children{$got};
+	delete $children{$got};
+	$worst = $? if $? > $worst;
+	if ($?) {
+	    print STDERR $whoami."[$$]: [$got] failed for $host: $?\n";
+	} else {
+	    print ::DEBUG "REAPED [$got] $host\n";
+	}
+    }
+}
+
+sub parallel_by_fork ($$$$) {
+    my ($set_whoami, $maxjobs, $tasks, $fn) = @_;
+    # does   $fn->($_) foreach @$tasks
+    # but in parallal and then exits.
+    # entries in @$taskarray should be suitable for print in messages.
+    # db is reopened in each child.
+
+    $whoami = $set_whoami;
+    undef $dbh_tests;
+
+    foreach my $task (@$tasks) {
+	wait_for_max_children($maxjobs);
+
+	my $pid = fork // die $!;
+	if (!$pid) {
+	    opendb_tests();
+	    $fn->($task);
+	    exit 0;
+	}
+	print ::DEBUG "SPAWNED [$pid] $task\n";
+	$children{$pid} = $task;
+    }
+
+    wait_for_max_children(0);
+    exit $worst;
+}
+
 1;
diff --git a/sg-report-host-history b/sg-report-host-history
index 6bf14aa2..0a2e9904 100755
--- a/sg-report-host-history
+++ b/sg-report-host-history
@@ -377,43 +377,12 @@  db_retry($dbh_tests, [], sub {
     computeflightsrange();
 });
 
-undef $dbh_tests;
-
-our %children;
-our $worst = 0;
-
-sub wait_for_max_children ($) {
-    my ($lim) = @_;
-    while (keys(%children) > $lim) {
-	$!=0; $?=0; my $got = wait;
-	die "$! $got $?" unless exists $children{$got};
-	my $host = $children{$got};
-	delete $children{$got};
-	$worst = $? if $? > $worst;
-	if ($?) {
-	    print STDERR "sg-report-flight[: [$got] failed for $host: $?\n";
-	} else {
-	    print DEBUG "REAPED [$got] $host\n";
-	}
-    }
-}
-
-foreach my $host (sort keys %hosts) {
-    wait_for_max_children($maxjobs);
-
-    my $pid = fork // die $!;
-    if (!$pid) {
-	opendb_tests();
-	cache_read_previous("$htmlout/$host.html") if $read_previous;
-	db_retry($dbh_tests, [], sub {
-            mainquery($host);
-	    reporthost $host;
-	});
-	exit(0);
-    }
-    print DEBUG "SPAWNED [$pid] $host\n";
-    $children{$pid} = $host;
-}
-
-wait_for_max_children(0);
-exit $worst;
+parallel_by_fork('sg-report-flight', $maxjobs, [ sort keys %hosts ], sub {
+    my ($host) = @_;
+    cache_read_previous("$htmlout/$host.html") if $read_previous;
+    db_retry($dbh_tests, [], sub {
+        mainquery($host);
+	reporthost $host;
+    });
+    exit(0);
+});