On 7/31/19 11:12 AM, Jan Hubicka wrote:
>> and see that in some cases jobserver is 0 and in other cases jobserver is 1.
>> Examples of both:
>> 0 kw -j2 --jobserver-auth=3,6 --
>> total 0
>> lrwx------. 1 jakub jakub 64 Jul 31 10:41 0 -> /dev/pts/4
>> l-wx------. 1 jakub jakub 64 Jul 31 10:41 1 -> pipe:[75026106]
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 10 ->
>> /usr/src/gcc/obj/gcc/libgcc_s.so
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 13 -> /usr/lib64/libc.so
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 18 ->
>> /usr/src/gcc/obj/gcc/libgcc_s.so
>> l-wx------. 1 jakub jakub 64 Jul 31 10:41 2 -> /tmp/ccX4y4r3.le
>> l-wx------. 1 jakub jakub 64 Jul 31 10:41 9 -> pipe:[46167136]
>> 1 kw -j2 --jobserver-auth=3,6 --
>> total 0
>> lrwx------. 1 jakub jakub 64 Jul 31 10:41 0 -> /dev/pts/4
>> l-wx------. 1 jakub jakub 64 Jul 31 10:41 1 -> /tmp/cchSvmBt
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 10 -> /usr/lib64/crtn.o
>> lrwx------. 1 jakub jakub 64 Jul 31 10:41 2 -> /dev/pts/4
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 3 -> /usr/lib64/crt1.o
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 4 -> /usr/lib64/crti.o
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 5 ->
>> /usr/src/gcc/obj/gcc/crtbegin.o
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 6 ->
>> /usr/src/gcc/obj/gcc/testsuite/gcc/c_lto_20081125_0.o
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 7 ->
>> /usr/src/gcc/obj/gcc/testsuite/gcc/c_lto_20081125_1.o
>> lr-x------. 1 jakub jakub 64 Jul 31 10:41 8 -> /usr/src/gcc/obj/gcc/crtend.o
>> l-wx------. 1 jakub jakub 64 Jul 31 10:41 9 -> pipe:[46167136]
>>
>> so, if one is lucky enough and at least one of the two file descriptors in
>> MAKEFLAGS --jobserver-auth= is closed, it will work fine, but if they are
>> open, even when they have nothing to do with make, it will fail miserably.
>
> I see, so the problem is that lto-wrapper is executed via plugin from
> gold which already used the file descriptors for something else?
>
> This seems a bit of problem since gold opens it before we get to
> lto-plugin (most probably) so i do not see how to reliably detect
> presence of make server.
Thank you Jakub for debugging!
>
> I think the gcc binary can look for jobserv environment, detect jobserv
> and if it is not available remove jobserver option from it prior going
> to ld. I think this works since I think link-time gcc can only be
> executed via gcc/g++/gfortran... wrappers.
Do you mean something like what I'm attaching?
Anyway, the auto-detection of jobserver seems very ugly to me :/
I tend to remove the support and start working on a proper implementation
that can be used not only by make build system.
Martin
>
> (unlike lto plugin invoked implicitly for ar/nm etc).
>
> Honza
>>
>> As a partial workaround, I wonder if jobserver_active_p couldn't check (with
>> fstat/fstat64) if the file descriptors are pipes. It will still not be 100%
>> reliable, as when you are unlucky the file descriptor could be some
>> completely unrelated pipe, but at least better than the current state.
>>
>> Jakub
diff --git a/gcc/gcc.c b/gcc/gcc.c
index a4323eb146e..e43f46246ad 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -8268,6 +8268,44 @@ driver::maybe_run_linker (const char *argv0) const
{
int tmp = execution_count;
+ /* Detect jobserver and drop it if it's not working. */
+ const char *makeflags = env.get ("MAKEFLAGS");
+ if (makeflags != NULL)
+ {
+ const char *needle = "--jobserver-auth=";
+ const char *n = strstr (makeflags, needle);
+ if (n != NULL)
+ {
+ int rfd = -1;
+ int wfd = -1;
+
+ bool jobserver
+ = ((sscanf(n, "--jobserver-auth=%d,%d", &rfd, &wfd) == 2)
+ && rfd > 0
+ && wfd > 0
+ && fcntl (rfd, F_GETFD) >= 0
+ && fcntl (wfd, F_GETFD) >= 0);
+
+ /* Drop the jobserver if it's not working now. */
+ if (!jobserver)
+ {
+ unsigned offset = n - makeflags;
+ char *dup = xstrdup (makeflags);
+ dup[offset] = '\0';
+
+ const char *space = strchr (makeflags + offset, ' ');
+ if (space != NULL)
+ strcpy (dup + offset, space);
+ xputenv (concat ("MAKEFLAGS=", dup, NULL));
+
+ // TODO: remove
+ FILE *f = fopen ("/tmp/111x", "a");
+ fprintf (f, "dropping MAKEFLAGS\n");
+ fclose (f);
+ }
+ }
+ }
+
if (! have_c)
{
#if HAVE_LTO_PLUGIN > 0
diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c
index 353187c6043..10b74d84af7 100644
--- a/gcc/lto-wrapper.c
+++ b/gcc/lto-wrapper.c
@@ -1430,6 +1430,13 @@ run_gcc (unsigned argc, char *argv[])
jobserver or number of cores. */
auto_parallel = 1;
jobserver = jobserver_active_p ();
+
+FILE *f = fopen ("/tmp/111x", "a");
+fprintf (f, "%d %s\n", jobserver, getenv ("MAKEFLAGS"));
+fclose (f);
+char buf[1024];
+sprintf (buf, "ls -l /proc/%d/fd >> /tmp/111x", getpid ());
+system (buf);
}
if (linker_output)