Subject: Debugging parallel builds - tweaking job token
To: None <tech-toolchain@netbsd.org>
From: Simon Gerraty <sjg@juniper.net>
List: tech-toolchain
Date: 09/26/2007 09:47:07
This should benefit anyone trying to debug parallel builds.
Eg. when you have a slew of --- some-target --- messages in a row,
it can be impossible to see the relationship.

Also I often find, the --- job-token ---'s at the end of a line.

The patch below allows one to do:

.MAKE.JOB.PREFIX='${.newline}---${.MAKE:T}[${.MAKE.PID}]'

which would give output like:

---make[4884] some-target ---

--sjg

Index: job.c
===================================================================
RCS file: /cvsroot/src/usr.bin/make/job.c,v
retrieving revision 1.124
diff -u -p -r1.124 job.c
--- job.c	27 Oct 2006 21:00:19 -0000	1.124
+++ job.c	26 Sep 2007 16:39:27 -0000
@@ -305,15 +305,16 @@ STATIC GNode   	*lastNode;	/* The node f
 STATIC const char *targFmt;   	/* Format string to use to head output from a
 				 * job when it's not the most-recent job heard
 				 * from */
+static char *targPrefix = NULL; /* What we print at the start of targFmt */
 static Job tokenWaitJob;	/* token wait pseudo-job */
 
 static Job childExitJob;	/* child exit pseudo-job */
 #define	CHILD_EXIT	"."
 #define	DO_JOB_RESUME	"R"
 
-#define TARG_FMT  "--- %s ---\n" /* Default format */
+#define TARG_FMT  "%s %s ---\n" /* Default format */
 #define MESSAGE(fp, gn) \
-	(void)fprintf(fp, targFmt, gn->name)
+	(void)fprintf(fp, targFmt, targPrefix, gn->name)
 
 static sigset_t caught_signals;	/* Set of signals we handle */
 #if defined(SYSV)
@@ -2053,6 +2054,20 @@ Shell_GetNewline(void)
     return commandShell->newline;
 }
 
+void
+Job_SetPrefix(void)
+{
+    char tmp[64];
+    
+    if (targPrefix) {
+	free(targPrefix);
+    } else if (!Var_Exists(MAKEJOBPREFIX, VAR_GLOBAL)) {
+	Var_Set(MAKEJOBPREFIX, "---", VAR_GLOBAL, 0);
+    }
+    strncpy(tmp, "${" MAKEJOBPREFIX "}", sizeof(tmp));
+    targPrefix = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+}
+
 /*-
  *-----------------------------------------------------------------------
  * Job_Init --
Index: job.h
===================================================================
RCS file: /cvsroot/src/usr.bin/make/job.h,v
retrieving revision 1.33
diff -u -p -r1.33 job.h
--- job.h	11 Oct 2006 07:01:44 -0000	1.33
+++ job.h	26 Sep 2007 16:39:27 -0000
@@ -258,5 +258,6 @@ void JobFlagForMigration(int);
 void Job_TokenReturn(void);
 Boolean Job_TokenWithdraw(void);
 void Job_ServerStart(int, int, int);
+void Job_SetPrefix(void);
 
 #endif /* _JOB_H_ */
Index: main.c
===================================================================
RCS file: /cvsroot/src/usr.bin/make/main.c,v
retrieving revision 1.141
diff -u -p -r1.141 main.c
--- main.c	1 Jan 2007 21:29:01 -0000	1.141
+++ main.c	26 Sep 2007 16:39:27 -0000
@@ -866,6 +866,19 @@ main(int argc, char **argv)
 	Var_Set(".ALLTARGETS", "", VAR_GLOBAL, 0);
 
 	/*
+	 * Set some other useful macros
+	 */
+	{
+	    char tmp[64];
+
+	    snprintf(tmp, sizeof(tmp), "%u", getpid());
+	    Var_Set(".MAKE.PID", tmp, VAR_GLOBAL, 0);
+	    snprintf(tmp, sizeof(tmp), "%u", getppid());
+	    Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL, 0);
+	}
+	Job_SetPrefix();
+
+	/*
 	 * First snag any flags out of the MAKE environment variable.
 	 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
 	 * in a different format).
Index: make.h
===================================================================
RCS file: /cvsroot/src/usr.bin/make/make.h,v
retrieving revision 1.67
diff -u -p -r1.67 make.h
--- make.h	1 Jan 2007 21:48:43 -0000	1.67
+++ make.h	26 Sep 2007 16:39:27 -0000
@@ -397,6 +397,7 @@ extern char	*progname;	/* The program na
 
 #define	MAKEFLAGS	".MAKEFLAGS"
 #define	MAKEOVERRIDES	".MAKEOVERRIDES"
+#define	MAKEJOBPREFIX	".MAKE.JOB.PREFIX"
 
 /*
  * debug control:
Index: parse.c
===================================================================
RCS file: /cvsroot/src/usr.bin/make/parse.c,v
retrieving revision 1.134
diff -u -p -r1.134 parse.c
--- parse.c	23 May 2007 19:03:56 -0000	1.134
+++ parse.c	26 Sep 2007 16:39:27 -0000
@@ -1614,6 +1614,8 @@ Parse_DoVar(char *line, GNode *ctxt)
 	 */
 	Dir_InitCur(cp);
 	Dir_SetPATH();
+    } else if (strcmp(line, MAKEJOBPREFIX) == 0) {
+	Job_SetPrefix();
     }
     if (freeCp)
 	free(cp);