[PATCH] D29960: [PGO] Delay directory creation until write time

David Li via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 14 12:41:50 PST 2017


davidxl created this revision.

Some process starts as root but  drops the privileges later.  Profile runtime currently eagerly creates profile file directory on program start up. This can cause problems later when profiling  APIs (such as set filename, cleanup dir etc) are used to do profile dumping when the process privilege is dropped -- the directory can not be cleaned up before/after profile dumping.

In this patch, the dir creation is delayed until profile writing time when merge mode is on. No other external behavior changes should be expected.


https://reviews.llvm.org/D29960

Files:
  lib/profile/InstrProfilingFile.c


Index: lib/profile/InstrProfilingFile.c
===================================================================
--- lib/profile/InstrProfilingFile.c
+++ lib/profile/InstrProfilingFile.c
@@ -172,6 +172,16 @@
   return 0;
 }
 
+/* Create the directory holding the file, if needed. */
+static void createProfileDir(const char *Filename) {
+  size_t Length = strlen(Filename);
+  if (lprofFindFirstDirSeparator(Filename)) {
+    char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
+    strncpy(Copy, Filename, Length + 1);
+    __llvm_profile_recursive_mkdir(Copy);
+  }
+}
+
 /* Open the profile data for merging. It opens the file in r+b mode with
  * file locking.  If the file has content which is compatible with the
  * current process, it also reads in the profile data in the file and merge
@@ -184,6 +194,7 @@
   FILE *ProfileFile;
   int rc;
 
+  createProfileDir(ProfileFileName);
   ProfileFile = lprofOpenFileEx(ProfileFileName);
   if (!ProfileFile)
     return NULL;
@@ -233,18 +244,13 @@
   if (!Filename)
     return;
 
-  /* Create the directory holding the file, if needed. */
-  if (lprofFindFirstDirSeparator(Filename)) {
-    char *Copy = (char *)COMPILER_RT_ALLOCA(Length + 1);
-    strncpy(Copy, Filename, Length + 1);
-    __llvm_profile_recursive_mkdir(Copy);
-  }
-
   /* By pass file truncation to allow online raw profile
    * merging. */
   if (lprofCurFilename.MergePoolSize)
     return;
 
+  createProfileDir(Filename);
+
   /* Truncate the file.  Later we'll reopen and append. */
   File = fopen(Filename, "w");
   if (!File)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29960.88417.patch
Type: text/x-patch
Size: 1555 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170214/30c08891/attachment.bin>


More information about the llvm-commits mailing list