Skip to content

[compiler-rt] During profile flushing, setup SIGKILL mask earlier #68622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

serge-sans-paille
Copy link
Collaborator

In multi threaded application, it is possible for one thread to terminate the program while another is flushing profile information. We setup a signal mask to delay SIGKILL so that we can safely flush the profile.

This patch setups the mask earlier: it reduces the window during which a SIGKILL can end the computation prematurely.

This fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1850940, where a profiled firefox was encountering several:

LLVM Profile Error: Failed to write file "default_*.profraw": Broken pipe

In multi threaded application, it is possible for one thread to
terminate the program while another is flushing profile information. We
setup a signal mask to delay SIGKILL so that we can safely flush the profile.

This patch setups the mask earlier: it reduces the window during which a
SIGKILL can end the computation prematurely.

This fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1850940, where
a profiled firefox was encountering several:

    LLVM Profile Error: Failed to write file "default_*.profraw": Broken pipe
@llvmbot llvmbot added compiler-rt PGO Profile Guided Optimizations labels Oct 9, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 9, 2023

@llvm/pr-subscribers-pgo

Changes

In multi threaded application, it is possible for one thread to terminate the program while another is flushing profile information. We setup a signal mask to delay SIGKILL so that we can safely flush the profile.

This patch setups the mask earlier: it reduces the window during which a SIGKILL can end the computation prematurely.

This fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1850940, where a profiled firefox was encountering several:

LLVM Profile Error: Failed to write file "default_*.profraw": Broken pipe

Full diff: https://github.com/llvm/llvm-project/pull/68622.diff

1 Files Affected:

  • (modified) compiler-rt/lib/profile/InstrProfilingFile.c (+16-8)
diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c
index 5556eccbf578790..ae7872c63f796c0 100644
--- a/compiler-rt/lib/profile/InstrProfilingFile.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -1034,10 +1034,14 @@ int __llvm_profile_write_file(void) {
   int rc, Length;
   const char *Filename;
   char *FilenameBuf;
-  int PDeathSig = 0;
+
+  // Temporarily suspend getting SIGKILL when the parent exits.
+  int PDeathSig = lprofSuspendSigKill();
 
   if (lprofProfileDumped() || __llvm_profile_is_continuous_mode_enabled()) {
     PROF_NOTE("Profile data not written to file: %s.\n", "already written");
+    if (PDeathSig == 1)
+      lprofRestoreSigKill();
     return 0;
   }
 
@@ -1048,6 +1052,8 @@ int __llvm_profile_write_file(void) {
   /* Check the filename. */
   if (!Filename) {
     PROF_ERR("Failed to write file : %s\n", "Filename not set");
+    if (PDeathSig == 1)
+      lprofRestoreSigKill();
     return -1;
   }
 
@@ -1057,12 +1063,11 @@ int __llvm_profile_write_file(void) {
              "expected %d, but get %d\n",
              INSTR_PROF_RAW_VERSION,
              (int)GET_VERSION(__llvm_profile_get_version()));
+    if (PDeathSig == 1)
+      lprofRestoreSigKill();
     return -1;
   }
 
-  // Temporarily suspend getting SIGKILL when the parent exits.
-  PDeathSig = lprofSuspendSigKill();
-
   /* Write profile data to the file. */
   rc = writeFile(Filename);
   if (rc)
@@ -1095,7 +1100,9 @@ int __llvm_orderfile_write_file(void) {
   int rc, Length, LengthBeforeAppend, SuffixLength;
   const char *Filename;
   char *FilenameBuf;
-  int PDeathSig = 0;
+
+  // Temporarily suspend getting SIGKILL when the parent exits.
+  int PDeathSig = lprofSuspendSigKill();
 
   SuffixLength = strlen(OrderFileSuffix);
   Length = getCurFilenameLength() + SuffixLength;
@@ -1105,6 +1112,8 @@ int __llvm_orderfile_write_file(void) {
   /* Check the filename. */
   if (!Filename) {
     PROF_ERR("Failed to write file : %s\n", "Filename not set");
+    if (PDeathSig == 1)
+      lprofRestoreSigKill();
     return -1;
   }
 
@@ -1119,12 +1128,11 @@ int __llvm_orderfile_write_file(void) {
              "expected %d, but get %d\n",
              INSTR_PROF_RAW_VERSION,
              (int)GET_VERSION(__llvm_profile_get_version()));
+    if (PDeathSig == 1)
+      lprofRestoreSigKill();
     return -1;
   }
 
-  // Temporarily suspend getting SIGKILL when the parent exits.
-  PDeathSig = lprofSuspendSigKill();
-
   /* Write order data to the file. */
   rc = writeOrderFile(Filename);
   if (rc)

@serge-sans-paille serge-sans-paille merged commit 97b989b into llvm:main Oct 10, 2023
@serge-sans-paille
Copy link
Collaborator Author

Thanks @ZequanWu for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants