From patchwork Tue Apr 16 18:13:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Baines X-Patchwork-Id: 3301 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 3B8ED16E96; Tue, 16 Apr 2019 19:14:07 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.0 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTP id 17A8016E94 for ; Tue, 16 Apr 2019 19:14:06 +0100 (BST) Received: from localhost ([127.0.0.1]:40621 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGSar-0002uY-HH for patchwork@mira.cbaines.net; Tue, 16 Apr 2019 14:14:05 -0400 Received: from eggs.gnu.org ([209.51.188.92]:47218) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGSap-0002uQ-6p for guix-patches@gnu.org; Tue, 16 Apr 2019 14:14:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hGSao-0003xj-5o for guix-patches@gnu.org; Tue, 16 Apr 2019 14:14:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:52665) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hGSan-0003xA-VO for guix-patches@gnu.org; Tue, 16 Apr 2019 14:14:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hGSan-0007RE-KW for guix-patches@gnu.org; Tue, 16 Apr 2019 14:14:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#34982] [PATCH] guile-build-system: Support building in parallel. References: <20190324212345.4697-1-mail@cbaines.net> In-Reply-To: <20190324212345.4697-1-mail@cbaines.net> Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 16 Apr 2019 18:14:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 34982 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 34982@debbugs.gnu.org Received: via spool by 34982-submit@debbugs.gnu.org id=B34982.155543841428548 (code B ref 34982); Tue, 16 Apr 2019 18:14:01 +0000 Received: (at 34982) by debbugs.gnu.org; 16 Apr 2019 18:13:34 +0000 Received: from localhost ([127.0.0.1]:37976 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hGSaM-0007QN-8D for submit@debbugs.gnu.org; Tue, 16 Apr 2019 14:13:34 -0400 Received: from li622-129.members.linode.com ([212.71.249.129]:36418 helo=mira.cbaines.net) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hGSaH-0007QC-GV for 34982@debbugs.gnu.org; Tue, 16 Apr 2019 14:13:32 -0400 Received: from localhost (cpc102582-walt20-2-0-cust14.13-2.cable.virginm.net [86.27.34.15]) by mira.cbaines.net (Postfix) with ESMTPSA id A163016E94 for <34982@debbugs.gnu.org>; Tue, 16 Apr 2019 19:13:26 +0100 (BST) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id 8ea8c650 for <34982@debbugs.gnu.org>; Tue, 16 Apr 2019 18:13:26 +0000 (UTC) From: Christopher Baines Date: Tue, 16 Apr 2019 19:13:26 +0100 Message-Id: <20190416181326.2416-1-mail@cbaines.net> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/build/guile-build-system.scm (build): Use invoke-each, instead of for-each, to use multiple cores if available. (invoke-each, report-build-process): New procedures. --- guix/build/guile-build-system.scm | 96 +++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/guix/build/guile-build-system.scm b/guix/build/guile-build-system.scm index 0bed049436..5ad728361a 100644 --- a/guix/build/guile-build-system.scm +++ b/guix/build/guile-build-system.scm @@ -23,6 +23,7 @@ #:use-module (ice-9 match) #:use-module (ice-9 popen) #:use-module (ice-9 rdelim) + #:use-module (ice-9 threads) #:use-module (guix build utils) #:export (target-guile-effective-version %standard-phases @@ -65,6 +66,59 @@ Return #false if it cannot be determined." (setenv "GUIX_LOCPATH" (string-append locales "/lib/locale")) #t))) +(define* (invoke-each commands + #:key (max-processes (current-processor-count)) + report-progress) + "Run each command in COMMANDS in a separate process, using up to +MAX-PROCESSES processes in parallel. Call REPORT-PROGRESS at each step. +Raise an error if one of the processes exit with non-zero." + (define total + (length commands)) + + (define (wait-for-one-process) + (match (waitpid WAIT_ANY) + ((_ . status) + (unless (zero? (status:exit-val status)) + (error "process failed" status))))) + + (define (fork-and-run-command command) + (match (primitive-fork) + (0 + (apply execlp command)) + (pid + #t))) + + (let loop ((commands commands) + (running 0) + (completed 0)) + (match commands + (() + (or (zero? running) + (let ((running (- running 1)) + (completed (+ completed 1))) + (wait-for-one-process) + (report-progress total completed) + (loop commands running completed)))) + ((command . rest) + (if (< running max-processes) + (let ((running (+ 1 running))) + (fork-and-run-command command) + (report-progress total completed) + (loop rest running completed)) + (let ((running (- running 1)) + (completed (+ completed 1))) + (wait-for-one-process) + (report-progress total completed) + (loop commands running completed))))))) + +(define* (report-build-progress total completed + #:optional (log-port (current-error-port))) + "Report that COMPLETED out of TOTAL files have been completed." + (display #\cr log-port) + (format log-port "compiling...\t~5,1f% of ~d files" ;FIXME: i18n + (* 100. (/ completed total)) total) + (force-output log-port)) + (define* (build #:key outputs inputs native-inputs (source-directory ".") (compile-flags '()) @@ -101,24 +155,30 @@ Return #false if it cannot be determined." (match (getenv "GUILE_LOAD_COMPILED_PATH") (#f "") (path (string-append ":" path))))) - (for-each (lambda (file) - (let* ((go (string-append go-dir - (file-sans-extension file) - ".go"))) - ;; Install source module. - (install-file (string-append source-directory "/" file) - (string-append module-dir - "/" (dirname file))) - - ;; Install and compile module. - (apply invoke guild "compile" "-L" source-directory - "-o" go - (string-append source-directory "/" file) - flags))) - - ;; Arrange to strip SOURCE-DIRECTORY from file names. - (with-directory-excursion source-directory - (find-files "." scheme-file-regexp))) + + (let ((source-files + (with-directory-excursion source-directory + (find-files "." scheme-file-regexp)))) + (invoke-each + (map (lambda (file) + (cons* guild + "guild" "compile" + "-L" source-directory + "-o" (string-append go-dir + (file-sans-extension file) + ".go") + (string-append source-directory "/" file) + flags)) + source-files) + #:max-processes (parallel-job-count) + #:report-progress report-build-progress) + + (for-each + (lambda (file) + (install-file (string-append source-directory "/" file) + (string-append module-dir + "/" (dirname file)))) + source-files)) #t)) (define* (install-documentation #:key outputs