[compiler-rt] r269453 - [profile] Eliminate dynamic memory allocation for buffered writer

Xinliang David Li via llvm-commits llvm-commits at lists.llvm.org
Fri May 13 11:26:27 PDT 2016


Author: davidxl
Date: Fri May 13 13:26:26 2016
New Revision: 269453

URL: http://llvm.org/viewvc/llvm-project?rev=269453&view=rev
Log:
[profile] Eliminate dynamic memory allocation for buffered writer

With this change, dynamic memory allocation is only used
for testing purpose. This change is one of the many steps to
make instrument profiler dynamic allocation free.


Modified:
    compiler-rt/trunk/lib/profile/InstrProfilingFile.c
    compiler-rt/trunk/lib/profile/InstrProfilingInternal.h
    compiler-rt/trunk/lib/profile/InstrProfilingWriter.c
    compiler-rt/trunk/test/profile/instrprof-bufferio.c

Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=269453&r1=269452&r2=269453&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Fri May 13 13:26:26 2016
@@ -32,18 +32,24 @@ static uint32_t fileWriter(ProfDataIOVec
 
 COMPILER_RT_VISIBILITY ProfBufferIO *
 lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
-  CallocHook = calloc;
-  FreeHook = free;
-  return lprofCreateBufferIO(fileWriter, File, BufferSz);
+  FreeHook = &free;
+  DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
+  VPBufferSize = BufferSz;
+  return lprofCreateBufferIO(fileWriter, File);
 }
 
-static int writeFile(FILE *File) {
+static void setupIOBuffer() {
   const char *BufferSzStr = 0;
-  FreeHook = &free;
-  CallocHook = &calloc;
   BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");
-  if (BufferSzStr && BufferSzStr[0])
+  if (BufferSzStr && BufferSzStr[0]) {
     VPBufferSize = atoi(BufferSzStr);
+    DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);
+  }
+}
+
+static int writeFile(FILE *File) {
+  FreeHook = &free;
+  setupIOBuffer();
   return lprofWriteData(fileWriter, File, lprofGatherValueProfData);
 }
 

Modified: compiler-rt/trunk/lib/profile/InstrProfilingInternal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingInternal.h?rev=269453&r1=269452&r2=269453&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingInternal.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingInternal.h Fri May 13 13:26:26 2016
@@ -68,12 +68,13 @@ typedef struct ProfBufferIO {
 } ProfBufferIO;
 
 /* The creator interface used by testing.  */
-ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t DefaultBufferSz);
+ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
+
 /*!
  * This is the interface to create a handle for buffered IO.
  */
-ProfBufferIO *lprofCreateBufferIO(WriterCallback FileWriter, void *File,
-                                  uint32_t DefaultBufferSz);
+ProfBufferIO *lprofCreateBufferIO(WriterCallback FileWriter, void *File);
+
 /*!
  * The interface to destroy the bufferIO handle and reclaim
  * the memory.
@@ -119,8 +120,8 @@ void lprofMergeValueProfData(struct Valu
 
 COMPILER_RT_VISIBILITY extern char *(*GetEnvHook)(const char *);
 COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
-COMPILER_RT_VISIBILITY extern void *(*CallocHook)(size_t, size_t);
+COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
+COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;
 extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);
-extern uint32_t VPBufferSize;
 
 #endif

Modified: compiler-rt/trunk/lib/profile/InstrProfilingWriter.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingWriter.c?rev=269453&r1=269452&r2=269453&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingWriter.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingWriter.c Fri May 13 13:26:26 2016
@@ -13,9 +13,13 @@
 
 #define INSTR_PROF_VALUE_PROF_DATA
 #include "InstrProfData.inc"
+
 COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
-COMPILER_RT_VISIBILITY void *(*CallocHook)(size_t, size_t) = NULL;
-uint32_t VPBufferSize = 0;
+static ProfBufferIO TheBufferIO;
+#define VP_BUFFER_SIZE 8 * 1024
+static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];
+COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;
+COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;
 
 /* The buffer writer is reponsponsible in keeping writer state
  * across the call.
@@ -43,20 +47,20 @@ static void llvmInitBufferIO(ProfBufferI
 }
 
 COMPILER_RT_VISIBILITY ProfBufferIO *
-lprofCreateBufferIO(WriterCallback FileWriter, void *File, uint32_t BufferSz) {
-  ProfBufferIO *BufferIO = (ProfBufferIO *)CallocHook(1, sizeof(ProfBufferIO));
-  uint8_t *Buffer = (uint8_t *)CallocHook(1, BufferSz);
+lprofCreateBufferIO(WriterCallback FileWriter, void *File) {
+  uint8_t *Buffer = DynamicBufferIOBuffer;
+  uint32_t BufferSize = VPBufferSize;
   if (!Buffer) {
-    FreeHook(BufferIO);
-    return 0;
+    Buffer = &BufferIOBuffer[0];
+    BufferSize = sizeof(BufferIOBuffer);
   }
-  llvmInitBufferIO(BufferIO, FileWriter, File, Buffer, BufferSz);
-  return BufferIO;
+  llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize);
+  return &TheBufferIO;
 }
 
 COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
-  FreeHook(BufferIO->BufferStart);
-  FreeHook(BufferIO);
+  if (DynamicBufferIOBuffer)
+    FreeHook(DynamicBufferIOBuffer);
 }
 
 COMPILER_RT_VISIBILITY int
@@ -91,8 +95,6 @@ COMPILER_RT_VISIBILITY int lprofBufferIO
   return 0;
 }
 
-#define VP_BUFFER_SIZE 8 * 1024
-
 static int writeOneValueProfData(ProfBufferIO *BufferIO,
                                  VPGatherHookType VPDataGatherer,
                                  const __llvm_profile_data *Data) {
@@ -111,14 +113,12 @@ static int writeValueProfData(WriterCall
                               const __llvm_profile_data *DataBegin,
                               const __llvm_profile_data *DataEnd) {
   ProfBufferIO *BufferIO;
-  uint32_t BufferSz;
   const __llvm_profile_data *DI = 0;
 
   if (!VPDataGatherer)
     return 0;
 
-  BufferSz = VPBufferSize ? VPBufferSize : VP_BUFFER_SIZE;
-  BufferIO = lprofCreateBufferIO(Writer, WriterCtx, BufferSz);
+  BufferIO = lprofCreateBufferIO(Writer, WriterCtx);
 
   for (DI = DataBegin; DI < DataEnd; DI++) {
     if (writeOneValueProfData(BufferIO, VPDataGatherer, DI))

Modified: compiler-rt/trunk/test/profile/instrprof-bufferio.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/instrprof-bufferio.c?rev=269453&r1=269452&r2=269453&view=diff
==============================================================================
--- compiler-rt/trunk/test/profile/instrprof-bufferio.c (original)
+++ compiler-rt/trunk/test/profile/instrprof-bufferio.c Fri May 13 13:26:26 2016
@@ -11,7 +11,7 @@
 #include <string.h>
 
 typedef struct ProfBufferIO ProfBufferIO;
-ProfBufferIO *lprofCreateBufferIOInternal(FILE *File, uint32_t DefaultBufferSz);
+ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
 void lprofDeleteBufferIO(ProfBufferIO *BufferIO);
 
 int lprofBufferIOWrite(ProfBufferIO *BufferIO, const char *Data, uint32_t Size);
@@ -44,7 +44,8 @@ int main(int argc, const char *argv[]) {
 
     BufferIO = lprofCreateBufferIOInternal(File[J], IOBufferSize[J]);
 
-    lprofBufferIOWrite(BufferIO, "Short Strings:\n", strlen("Short Strings:\n"));
+    lprofBufferIOWrite(BufferIO, "Short Strings:\n",
+                       strlen("Short Strings:\n"));
     for (I = 0; I < 1024; I++) {
       lprofBufferIOWrite(BufferIO, SmallData, strlen(SmallData));
     }




More information about the llvm-commits mailing list