Module Name:    src
Committed By:   sjg
Date:           Sat Sep  9 01:30:59 UTC 2023

Modified Files:
        src/usr.bin/make: main.c make.1
        src/usr.bin/make/unit-tests: varname-dot-make-jobs.exp
            varname-dot-make-jobs.mk

Log Message:
make: allow -j to compute a multiple of ncpu

If _SC_NPROCESSORS_ONLN is supported; and -j arg is a floating point
number or ends in 'C' compute .MAKE.JOBS as a multiple of _SC_NPROCESSORS_ONLN

Based on a suggestion from des at freebsd.org
Discussed with: rillig, christos


To generate a diff of this commit:
cvs rdiff -u -r1.593 -r1.594 src/usr.bin/make/main.c
cvs rdiff -u -r1.368 -r1.369 src/usr.bin/make/make.1
cvs rdiff -u -r1.2 -r1.3 \
    src/usr.bin/make/unit-tests/varname-dot-make-jobs.exp
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/make/unit-tests/varname-dot-make-jobs.mk

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.593 src/usr.bin/make/main.c:1.594
--- src/usr.bin/make/main.c:1.593	Tue Mar 28 14:39:31 2023
+++ src/usr.bin/make/main.c	Sat Sep  9 01:30:59 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.593 2023/03/28 14:39:31 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.594 2023/09/09 01:30:59 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.593 2023/03/28 14:39:31 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.594 2023/09/09 01:30:59 sjg Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -398,18 +398,37 @@ static void
 MainParseArgJobs(const char *argvalue)
 {
 	char *p;
+	char v[12];
 
 	forceJobs = true;
 	opts.maxJobs = (int)strtol(argvalue, &p, 0);
+#ifdef _SC_NPROCESSORS_ONLN
+	if (*p != '\0') {
+		double d;
+
+		if (*p == 'C') {
+			d = (opts.maxJobs > 0) ? opts.maxJobs : 1;
+		} else if (*p == '.') {
+			d = strtod(argvalue, &p);
+		} else
+			d = 0;
+		if (d > 0) {
+			p = strchr(argvalue, 0);
+			opts.maxJobs = sysconf(_SC_NPROCESSORS_ONLN);
+			opts.maxJobs = (int)(d * (double)opts.maxJobs);
+		}
+	}
+#endif
 	if (*p != '\0' || opts.maxJobs < 1) {
 		(void)fprintf(stderr,
 		    "%s: illegal argument to -j -- must be positive integer!\n",
 		    progname);
 		exit(2);	/* Not 1 so -q can distinguish error */
 	}
+	snprintf(v, sizeof(v), "%d", opts.maxJobs);
 	Global_Append(MAKEFLAGS, "-j");
-	Global_Append(MAKEFLAGS, argvalue);
-	Global_Set(".MAKE.JOBS", argvalue);
+	Global_Append(MAKEFLAGS, v);
+	Global_Set(".MAKE.JOBS", v);
 	maxJobTokens = opts.maxJobs;
 }
 

Index: src/usr.bin/make/make.1
diff -u src/usr.bin/make/make.1:1.368 src/usr.bin/make/make.1:1.369
--- src/usr.bin/make/make.1:1.368	Sun Aug 20 19:58:15 2023
+++ src/usr.bin/make/make.1	Sat Sep  9 01:30:59 2023
@@ -1,4 +1,4 @@
-.\"	$NetBSD: make.1,v 1.368 2023/08/20 19:58:15 sjg Exp $
+.\"	$NetBSD: make.1,v 1.369 2023/09/09 01:30:59 sjg Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	from: @(#)make.1	8.4 (Berkeley) 3/19/94
 .\"
-.Dd August 20, 2023
+.Dd September 9, 2023
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -267,6 +267,12 @@ cooperate to avoid overloading the syste
 Specify the maximum number of jobs that
 .Nm
 may have running at any one time.
+If
+.Ar max_jobs
+is a floating point number, or ends with
+.Ql C ,
+then the value is multiplied by the number of CPUs reported online by
+.Xr sysconf 3 .
 The value of
 .Ar max_jobs
 is saved in

Index: src/usr.bin/make/unit-tests/varname-dot-make-jobs.exp
diff -u src/usr.bin/make/unit-tests/varname-dot-make-jobs.exp:1.2 src/usr.bin/make/unit-tests/varname-dot-make-jobs.exp:1.3
--- src/usr.bin/make/unit-tests/varname-dot-make-jobs.exp:1.2	Wed Jan 26 22:47:03 2022
+++ src/usr.bin/make/unit-tests/varname-dot-make-jobs.exp	Sat Sep  9 01:30:59 2023
@@ -4,5 +4,5 @@ undefined
 5
 --- echo ---
 20
-00000000000000000000000000000001
+1
 exit status 0

Index: src/usr.bin/make/unit-tests/varname-dot-make-jobs.mk
diff -u src/usr.bin/make/unit-tests/varname-dot-make-jobs.mk:1.3 src/usr.bin/make/unit-tests/varname-dot-make-jobs.mk:1.4
--- src/usr.bin/make/unit-tests/varname-dot-make-jobs.mk:1.3	Wed Jan 26 22:47:03 2022
+++ src/usr.bin/make/unit-tests/varname-dot-make-jobs.mk	Sat Sep  9 01:30:59 2023
@@ -1,4 +1,4 @@
-# $NetBSD: varname-dot-make-jobs.mk,v 1.3 2022/01/26 22:47:03 rillig Exp $
+# $NetBSD: varname-dot-make-jobs.mk,v 1.4 2023/09/09 01:30:59 sjg Exp $
 #
 # Tests for the special .MAKE.JOBS variable, which is defined in jobs mode
 # only.  There it contains the number of jobs that may run in parallel.
@@ -15,10 +15,33 @@ all:
 	@${MAKE} -r -f ${MAKEFILE} echo -j20
 	@${MAKE} -r -f ${MAKEFILE} echo -j00000000000000000000000000000001
 
+.if !make(echo)
+# These results will not be static, we need NCPU
+# to compute expected results.
+# We only support -jC if _SC_NPROCESSORS_ONLN is defined,
+# otherwise we will get NCPU=0
+NCPU!= ${MAKE} -r -f /dev/null -jC -V .MAKE.JOBS 2> /dev/null || echo 0
+
+.if ${NCPU} > 0
+all:	jC
+
+# If -j arg is floating point or ends in C;
+# .MAKE.JOBS is a multiple of _SC_NPROCESSORS_ONLN
+# No news is good news here.
+jCvals ?= 1 1.2 2
+
+jC:
+	@for j in ${jCvals}; do \
+	e=`echo "${NCPU} * $$j" | bc | sed 's/\.[0-9]*//'`; \
+	g=`${MAKE} -r -f /dev/null -V .MAKE.JOBS -j$${j}C`; \
+	test $$g = $$e || echo "$$g != $$e"; \
+	done
+
+.endif
+.endif
+
 # expect: undefined
 # expect: 1
 # expect: 5
 # expect: 20
-# The value of .MAKE.JOBS is the exact text given in the command line, not the
-# canonical number.  This doesn't have practical consequences though.
-# expect: 00000000000000000000000000000001
+# expect: 1

Reply via email to