bug-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add the --nice argument to make


From: Palmer Dabbelt
Subject: [PATCH] Add the --nice argument to make
Date: Mon, 16 Sep 2019 18:08:23 -0700

I call make quite regularly, and I almost always want to invoke it with
at least a bit of nice to avoid it eating my whole machine.  Since I can
stick "-j$(nproc)" in my shell's default MAKEFLAGS I frequently end up
being lazy and just typing "make", which I then have to kill in order to
re-run as "nice -n10 make".

That's a lot of typing, so this patch adds a "--nice" argument that,
when stuck in my makeflags, allows me to be lazy and avoid trashing my
whole machine.  I've never contributed to make before, so I'm not sure
what to do in terms of collateral -- I've just modified the usage text
and make.1.

* main.c (arg_nice): New variable.
  (usage): Add --nice argument.
  (switches): Add --nice argument.
  (main): Check arg_nice, and call nice() on POSIX systems.  Print a
  warning on non-POSIX systems or when nice() had no effect.
---
 doc/make.1 |  5 +++++
 src/main.c | 20 ++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/doc/make.1 b/doc/make.1
index c9b60a3..33faeb2 100644
--- a/doc/make.1
+++ b/doc/make.1
@@ -264,6 +264,11 @@ Do not run any commands, or print anything; just return an 
exit status
 that is zero if the specified targets are already up to date, nonzero
 otherwise.
 .TP 0.5i
+\fB\-\-nice\fR=\fIincrement\fR
+Runs this
+.BR make
+instance with the given nice increment.
+.TP 0.5i
 \fB\-r\fR, \fB\-\-no\-builtin\-rules\fR
 Eliminate use of the built\-in implicit rules.
 Also clear out the default list of suffixes for suffix rules.
diff --git a/src/main.c b/src/main.c
index 04d6ba5..abc8faf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -47,6 +47,9 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef HAVE_FCNTL_H
 # include <fcntl.h>
 #endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
 
 #ifdef _AMIGA
 int __stack = 20000; /* Make sure we have 20K of stack space */
@@ -325,6 +328,9 @@ struct variable shell_var;
 
 char cmd_prefix = '\t';
 
+/* The value make will nice itself to. */
+static int arg_nice = 0;
+
 
 /* The usage output.  We write it this way to make life easier for the
    translators, especially those trying to translate to right-to-left
@@ -381,6 +387,10 @@ static const char *const usage[] =
   -p, --print-data-base       Print make's internal database.\n"),
     N_("\
   -q, --question              Run no recipe; exit status says if up to 
date.\n"),
+#ifdef HAVE_UNISTD_H
+    N_("\
+  --nice                      Set a nice value.\n"),
+#endif
     N_("\
   -r, --no-builtin-rules      Disable the built-in implicit rules.\n"),
     N_("\
@@ -464,6 +474,7 @@ static const struct command_switch switches[] =
     { CHAR_MAX+7, string, &sync_mutex, 1, 1, 0, 0, 0, "sync-mutex" },
     { CHAR_MAX+8, flag_off, &silent_flag, 1, 1, 0, 0, &default_silent_flag, 
"no-silent" },
     { CHAR_MAX+9, string, &jobserver_auth, 1, 0, 0, 0, 0, "jobserver-fds" },
+    { CHAR_MAX+10, positive_int, &arg_nice, 1, 0, 0, 0, 0, "nice" },
     { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
   };
 
@@ -1483,6 +1494,15 @@ main (int argc, char **argv, char **envp)
       arg_job_slots = env_slots;
   }
 
+  /* Nice ourselves, if requested. */
+  if (arg_nice)
+    {
+#ifdef HAVE_UNISTD_H
+      if (nice(0) == nice(arg_nice))
+#endif
+        O (error, NILF, _("warning: unable to nice"));
+    }
+
   /* Set a variable specifying whether stdout/stdin is hooked to a TTY.  */
 #ifdef HAVE_ISATTY
   if (isatty (fileno (stdout)))
-- 
2.21.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]