[PATCH] Add the --nice argument to make

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Add the --nice argument to make

Palmer Dabbelt
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


_______________________________________________
Bug-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/bug-make
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Add the --nice argument to make

Philip Guenther-2
On Mon, Sep 16, 2019 at 4:17 PM Palmer Dabbelt <[hidden email]> wrote:
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".

This sounds like a perfect case to use a shell alias or function in your shell's rcfile, or a shell script in your person bin directory.  i.e., something like
    alias make='nice -n10 make'
or maybe
    make() { nice -n10 /usr/bin/make "$@"; }


Philip Guenther


_______________________________________________
Bug-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/bug-make
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Add the --nice argument to make

Eli Zaretskii
In reply to this post by Palmer Dabbelt
> Date: Mon, 16 Sep 2019 18:08:23 -0700
> From: Palmer Dabbelt <[hidden email]>
> Cc: Palmer Dabbelt <[hidden email]>
>
> +#ifdef HAVE_UNISTD_H
> +      if (nice(0) == nice(arg_nice))
> +#endif

This is not portable enough, because having unistd.h doesn't yet mean
you have 'nice'.  E.g., MinGW, which produces native MS-Windows
programs, does have unistd.h, but no 'nice'.

_______________________________________________
Bug-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/bug-make
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Add the --nice argument to make

Andreas Schwab
In reply to this post by Palmer Dabbelt
On Sep 16 2019, Palmer Dabbelt <[hidden email]> wrote:

> @@ -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))

It is unspecified which operand is evaluated first.

Andreas.

--
Andreas Schwab, SUSE Labs, [hidden email]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

_______________________________________________
Bug-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/bug-make