[PATCH] D29954: [PGO] Suspend SIGKILL for PR_SET_PDEATHSIG in profile-write

Rong Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 14 11:39:55 PST 2017


xur created this revision.

We found a nondeterministic behavior when doing online profile merging
for multi-process applications. The application forks a sub-process and
sub-process sets to get SIGKILL when the parent process exits,

The first process gets the lock, and dumps the profile. The second one
will mmap the file, do the merge and write out the file. Note that before
the merged write, we truncate the profile.

Depending on the timing, the child process might be terminated
abnormally when the parent exits first. If this happens:
(1) before the truncation, we will get the profile for the main process
(2) after the truncation, and before write-out the profile,  we will get
0 size profile.
(3) after the merged write, we get merged profile.

This patch temporarily suspend the SIGKILL for PR_SET_PDEATHSIG
before profile-write and restore it after the write.

This patch only applies to Linux system.


https://reviews.llvm.org/D29954

Files:
  lib/profile/InstrProfilingFile.c


Index: lib/profile/InstrProfilingFile.c
===================================================================
--- lib/profile/InstrProfilingFile.c
+++ lib/profile/InstrProfilingFile.c
@@ -28,6 +28,8 @@
 #include <unistd.h>
 #if defined(__linux__)
 #include <sys/types.h>
+#include <signal.h>
+#include <sys/prctl.h>
 #endif
 #endif
 
@@ -524,6 +526,9 @@
   int rc, Length;
   const char *Filename;
   char *FilenameBuf;
+#if defined(__linux__)
+   int PDeachSig = 0;
+#endif
 
   if (lprofProfileDumped()) {
     PROF_NOTE("Profile data not written to file: %s.\n", 
@@ -550,10 +555,22 @@
     return -1;
   }
 
+#if defined(__linux__)
+  // Temporarily suspend getting SIGKILL when the parent exits.
+  if (prctl(PR_GET_PDEATHSIG, &PDeachSig) == 0 && PDeachSig == SIGKILL)
+    prctl(PR_SET_PDEATHSIG, 0);
+#endif
+
   /* Write profile data to the file. */
   rc = writeFile(Filename);
   if (rc)
     PROF_ERR("Failed to write file \"%s\": %s\n", Filename, strerror(errno));
+
+#if defined(__linux__)
+  // Restore PR_SET_PDEATHSIG.
+  if (PDeachSig == SIGKILL)
+    prctl(PR_SET_PDEATHSIG, SIGKILL);
+#endif
   return rc;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29954.88410.patch
Type: text/x-patch
Size: 1128 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170214/3188222f/attachment.bin>


More information about the llvm-commits mailing list