[compiler-rt] r204414 - PGO: Substitute pid for %p in filename

Duncan P. N. Exon Smith dexonsmith at apple.com
Thu Mar 20 17:27:48 PDT 2014


Author: dexonsmith
Date: Thu Mar 20 19:27:48 2014
New Revision: 204414

URL: http://llvm.org/viewvc/llvm-project?rev=204414&view=rev
Log:
PGO: Substitute pid for %p in filename

Add logic to do a printf-style substitution of %p for the process pid in
the filename.

It's getting increasingly awkward to work on lib/profile without test
infrastructure.  This needs to be fixed!

<rdar://problem/16383358>

Modified:
    compiler-rt/trunk/lib/profile/InstrProfilingExtras.c

Modified: compiler-rt/trunk/lib/profile/InstrProfilingExtras.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingExtras.c?rev=204414&r1=204413&r2=204414&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingExtras.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingExtras.c Thu Mar 20 19:27:48 2014
@@ -8,6 +8,7 @@
 \*===----------------------------------------------------------------------===*/
 
 #include "InstrProfiling.h"
+#include <string.h>
 
 static void __llvm_profile_write_file_with_name(const char *OutputName) {
   FILE *OutputFile;
@@ -29,16 +30,60 @@ void __llvm_profile_set_filename(const c
   CurrentFilename = Filename;
 }
 
+int getpid(void);
 void __llvm_profile_write_file(void) {
-  const char *Filename = CurrentFilename;
+  char *AllocatedFilename = NULL;
+  int I, J;
+
+#define MAX_PID_SIZE 16
+  char PidChars[MAX_PID_SIZE] = { 0 };
+  int PidLength = 0;
+  int NumPids = 0;
 
+  // Get the filename.
+  const char *Filename = CurrentFilename;
 #define UPDATE_FILENAME(NextFilename) \
   if (!Filename || !Filename[0]) Filename = NextFilename
   UPDATE_FILENAME(getenv("LLVM_PROFILE_FILE"));
   UPDATE_FILENAME("default.profdata");
 #undef UPDATE_FILENAME
 
+  // Check the filename for "%p", which indicates a pid-substitution.
+  for (I = 0; Filename[I]; ++I)
+    if (Filename[I] == '%' && Filename[++I] == 'p')
+      if (!NumPids++) {
+        PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid());
+        if (PidLength <= 0)
+          return;
+      }
+  if (NumPids) {
+    // Allocate enough space for the substituted filename.
+    AllocatedFilename = (char*)malloc(I + NumPids*(PidLength - 2) + 1);
+    if (!AllocatedFilename)
+      return;
+
+    // Construct the new filename.
+    for (I = 0, J = 0; Filename[I]; ++I)
+      if (Filename[I] == '%') {
+        if (Filename[++I] == 'p') {
+          memcpy(AllocatedFilename + J, PidChars, PidLength);
+          J += PidLength;
+        }
+        // Drop any unknown substitutions.
+      } else
+        AllocatedFilename[J++] = Filename[I];
+    AllocatedFilename[J] = 0;
+
+    // Actually use the computed name.
+    Filename = AllocatedFilename;
+  }
+
+  // Write the file.
   __llvm_profile_write_file_with_name(Filename);
+
+  // Free the filename.
+  if (AllocatedFilename)
+    free(AllocatedFilename);
 }
 
 void __llvm_profile_register_write_file_atexit(void) {





More information about the llvm-commits mailing list