[debhelper-devel] [debhelper] 01/04: Dh_Lib: Add on_each_pkg_in_parallel

Niels Thykier nthykier at moszumanska.debian.org
Sun Jun 4 19:15:12 UTC 2017


This is an automated email from the git hooks/post-receive script.

nthykier pushed a commit to branch parallel_dh_tools
in repository debhelper.

commit def5eda0c8bb2d1bf76538907c6efa50e0d29d03
Author: Niels Thykier <niels at thykier.net>
Date:   Sun Jun 4 18:58:42 2017 +0000

    Dh_Lib: Add on_each_pkg_in_parallel
    
    Signed-off-by: Niels Thykier <niels at thykier.net>
---
 Debian/Debhelper/Dh_Lib.pm | 56 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 55 insertions(+), 1 deletion(-)

diff --git a/Debian/Debhelper/Dh_Lib.pm b/Debian/Debhelper/Dh_Lib.pm
index 6cfada3..b8d83fa 100644
--- a/Debian/Debhelper/Dh_Lib.pm
+++ b/Debian/Debhelper/Dh_Lib.pm
@@ -51,12 +51,14 @@ use vars qw(@EXPORT %dh);
 	    &generated_file &autotrigger &package_section
 	    &restore_file_on_clean &restore_all_files
 	    &open_gz &reset_perm_and_owner &deprecated_functionality
-	    &log_installed_files &buildarch
+	    &log_installed_files &buildarch &on_each_pkg_in_parallel
 );
 
 # The Makefile changes this if debhelper is installed in a PREFIX.
 my $prefix="/usr";
 
+my $MAX_PROCS = get_buildoption("parallel") || 1;
+
 sub init {
 	my %params=@_;
 
@@ -1564,6 +1566,58 @@ sub log_installed_files {
 	return 1;
 }
 
+sub on_each_pkg_in_parallel(&) {
+	my ($code) = @_;
+	my @pkgs = @{$dh{DOPACKAGES}};
+	my %pids;
+	my $parallel = $MAX_PROCS;
+	my $count_per_proc = int(scalar(@pkgs) / $parallel);
+	my $exit = 0;
+	if ($count_per_proc < 1) {
+		$count_per_proc = 1;
+		if (@pkgs > 3) {
+			# Forking has a considerable overhead, so bulk the number
+			# a bit.  We do not do this unconditionally, because we
+			# want parallel issues (if any) to appear already with 2
+			# packages and two procs (because people are lazy when
+			# testing).
+			#
+			# Same reason for also unconditionally forking with 1 pkg
+			# in 1 proc.
+			$count_per_proc = 2;
+		}
+	}
+	# Assertion, $count_per_proc * $parallel >= scalar(@pkgs)
+	while (@pkgs) {
+		my @batch = splice(@pkgs, 0, $count_per_proc);
+		my $pid = fork() // error("fork: $!");
+		if (not $pid) {
+			eval {
+				$code->(@batch);
+			};
+			if (my $err = $@) {
+				$err =~ s/\n$//;
+				print STDERR "$err\n";
+				exit(2);
+			}
+			exit(0);
+		}
+		$pids{$pid} = 1;
+	}
+	while (%pids) {
+		my $pid = wait;
+		error("wait() failed: $!") if $pid == -1;
+		delete($pids{$pid});
+		if ($? != 0) {
+			$exit = 1;
+		}
+	}
+	if ($exit) {
+		error("Aborting due to earlier error");
+	}
+	return;
+}
+
 1
 
 # Local Variables:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debhelper/debhelper.git




More information about the debhelper-devel mailing list