public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Martin Liska <marxin@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/users/marxin/heads/jobserver-refactoring)] lto: respect jobserver in parallel WPA streaming
Date: Tue,  9 Aug 2022 10:59:42 +0000 (GMT)	[thread overview]
Message-ID: <20220809105942.D890C3857401@sourceware.org> (raw)

https://gcc.gnu.org/g:5b9daacfb0bba4379bd24260658dbc35dc2dbed7

commit 5b9daacfb0bba4379bd24260658dbc35dc2dbed7
Author: Martin Liska <mliska@suse.cz>
Date:   Tue Aug 9 12:57:00 2022 +0200

    lto: respect jobserver in parallel WPA streaming
    
            PR lto/106328
    
    gcc/ChangeLog:
    
            * jobserver.h (struct jobserver_info): Add pipefd.
            (jobserver_info::connect): New.
            (jobserver_info::disconnect): Likewise.
            (jobserver_info::get_token): Likewise.
            (jobserver_info::return_token): Likewise.
    
    gcc/lto/ChangeLog:
    
            * lto.cc (wait_for_child): Decrement nruns once a process
            finishes.
            (stream_out_partitions): Use job server if active.
            (do_whole_program_analysis): Likewise.

Diff:
---
 gcc/jobserver.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gcc/lto/lto.cc  | 48 +++++++++++++++++++++++++++++++++++-------------
 2 files changed, 89 insertions(+), 13 deletions(-)

diff --git a/gcc/jobserver.h b/gcc/jobserver.h
index 856e326ddfc..2a7dc9f4113 100644
--- a/gcc/jobserver.h
+++ b/gcc/jobserver.h
@@ -31,6 +31,18 @@ struct jobserver_info
   /* Default constructor.  */
   jobserver_info ();
 
+  /* Connect to the server.  */
+  void connect ();
+
+  /* Disconnect from the server.  */
+  void disconnect ();
+
+  /* Get token from the server.  */
+  bool get_token ();
+
+  /* Return token to the server.  */
+  void return_token ();
+
   /* Error message if there is a problem.  */
   string error_msg = "";
   /* Skipped MAKEFLAGS where --jobserver-auth is skipped.  */
@@ -41,6 +53,8 @@ struct jobserver_info
   int wfd = -1;
   /* Named pipe path.  */
   string pipe_path = "";
+  /* Pipe file descriptor.  */
+  int pipefd = -1;
   /* Return true if jobserver is active.  */
   bool is_active = false;
 };
@@ -97,4 +111,44 @@ jobserver_info::jobserver_info ()
     error_msg = "jobserver is not available: " + error_msg;
 }
 
+void
+jobserver_info::connect ()
+{
+  if (!pipe_path.empty ())
+    pipefd = open (pipe_path.c_str (), O_RDWR);
+}
+
+void
+jobserver_info::disconnect ()
+{
+  if (!pipe_path.empty ())
+    {
+      gcc_assert (close (pipefd) == 0);
+      pipefd = -1;
+    }
+}
+
+bool
+jobserver_info::get_token ()
+{
+  int fd = pipe_path.empty () ? rfd : pipefd;
+  char c;
+  unsigned n = read (fd, &c, 1);
+  if (n != 1)
+    {
+      gcc_assert (errno == EAGAIN);
+      return false;
+    }
+  else
+    return true;
+}
+
+void
+jobserver_info::return_token ()
+{
+  int fd = pipe_path.empty () ? wfd : pipefd;
+  char c = 'G';
+  gcc_assert (write (fd, &c, 1) == 1);
+}
+
 #endif /* GCC_JOBSERVER_H */
diff --git a/gcc/lto/lto.cc b/gcc/lto/lto.cc
index 31b0c1862f7..721fbbed1dc 100644
--- a/gcc/lto/lto.cc
+++ b/gcc/lto/lto.cc
@@ -54,11 +54,17 @@ along with GCC; see the file COPYING3.  If not see
 #include "attribs.h"
 #include "builtins.h"
 #include "lto-common.h"
-
+#include "jobserver.h"
 
 /* Number of parallel tasks to run, -1 if we want to use GNU Make jobserver.  */
 static int lto_parallelism;
 
+/* Number of active WPA streaming processes.  */
+static int nruns = 0;
+
+/* GNU make's jobserver info.  */
+static jobserver_info *jinfo = NULL;
+
 /* Return true when NODE has a clone that is analyzed (i.e. we need
    to load its body even if the node itself is not needed).  */
 
@@ -205,6 +211,12 @@ wait_for_child ()
 		     "streaming subprocess was killed by signal");
     }
   while (!WIFEXITED (status) && !WIFSIGNALED (status));
+
+    --nruns;
+
+    /* Return token to the jobserver if active.  */
+    if (jinfo != NULL && jinfo->is_active)
+      jinfo->return_token ();
 }
 #endif
 
@@ -228,25 +240,28 @@ stream_out_partitions (char *temp_filename, int blen, int min, int max,
 		       bool ARG_UNUSED (last))
 {
 #ifdef HAVE_WORKING_FORK
-  static int nruns;
-
   if (lto_parallelism <= 1)
     {
       stream_out_partitions_1 (temp_filename, blen, min, max);
       return;
     }
 
-  /* Do not run more than LTO_PARALLELISM streamings
-     FIXME: we ignore limits on jobserver.  */
   if (lto_parallelism > 0 && nruns >= lto_parallelism)
-    {
-      wait_for_child ();
-      nruns --;
-    }
+    wait_for_child ();
+
   /* If this is not the last parallel partition, execute new
      streaming process.  */
   if (!last)
     {
+      if (jinfo != NULL && jinfo->is_active)
+	while (true)
+	  {
+	    if (jinfo->get_token ())
+	      break;
+	    if (nruns > 0)
+	      wait_for_child ();
+	  }
+
       pid_t cpid = fork ();
 
       if (!cpid)
@@ -264,10 +279,12 @@ stream_out_partitions (char *temp_filename, int blen, int min, int max,
   /* Last partition; stream it and wait for all children to die.  */
   else
     {
-      int i;
       stream_out_partitions_1 (temp_filename, blen, min, max);
-      for (i = 0; i < nruns; i++)
+      while (nruns > 0)
 	wait_for_child ();
+
+      if (jinfo != NULL && jinfo->is_active)
+	jinfo->disconnect ();
     }
   asm_nodes_output = true;
 #else
@@ -460,9 +477,14 @@ do_whole_program_analysis (void)
 
   lto_parallelism = 1;
 
-  /* TODO: jobserver communication is not supported, yet.  */
   if (!strcmp (flag_wpa, "jobserver"))
-    lto_parallelism = param_max_lto_streaming_parallelism;
+    {
+      jinfo = new jobserver_info ();
+      if (jinfo->is_active)
+	jinfo->connect ();
+
+      lto_parallelism = param_max_lto_streaming_parallelism;
+    }
   else
     {
       lto_parallelism = atoi (flag_wpa);


             reply	other threads:[~2022-08-09 10:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-09 10:59 Martin Liska [this message]
2022-08-09 11:03 Martin Liska
2022-08-09 11:13 Martin Liska
2022-08-10  7:15 Martin Liska
2022-08-10  7:18 Martin Liska
2022-08-10 11:09 Martin Liska

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220809105942.D890C3857401@sourceware.org \
    --to=marxin@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).