<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 13, 2016 at 11:26 AM, Xinliang David Li via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Author: davidxl<br>
Date: Fri May 13 13:26:26 2016<br>
New Revision: 269453<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=269453&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=269453&view=rev</a><br>
Log:<br>
[profile] Eliminate dynamic memory allocation for buffered writer<br>
<br>
With this change, dynamic memory allocation is only used<br>
for testing purpose. This change is one of the many steps to<br>
make instrument profiler dynamic allocation free.<br>
<br>
<br>
Modified:<br>
    compiler-rt/trunk/lib/profile/InstrProfilingFile.c<br>
    compiler-rt/trunk/lib/profile/InstrProfilingInternal.h<br>
    compiler-rt/trunk/lib/profile/InstrProfilingWriter.c<br>
    compiler-rt/trunk/test/profile/instrprof-bufferio.c<br>
<br>
Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=269453&r1=269452&r2=269453&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=269453&r1=269452&r2=269453&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)<br>
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Fri May 13 13:26:26 2016<br>
@@ -32,18 +32,24 @@ static uint32_t fileWriter(ProfDataIOVec<br>
<br>
 COMPILER_RT_VISIBILITY ProfBufferIO *<br>
 lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {<br>
-  CallocHook = calloc;<br>
-  FreeHook = free;<br>
-  return lprofCreateBufferIO(fileWriter, File, BufferSz);<br>
+  FreeHook = &free;<br>
+  DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);<br>
+  VPBufferSize = BufferSz;<br>
+  return lprofCreateBufferIO(fileWriter, File);<br>
 }<br>
<br>
-static int writeFile(FILE *File) {<br>
+static void setupIOBuffer() {<br>
   const char *BufferSzStr = 0;<br>
-  FreeHook = &free;<br>
-  CallocHook = &calloc;<br>
   BufferSzStr = getenv("LLVM_VP_BUFFER_SIZE");<br>
-  if (BufferSzStr && BufferSzStr[0])<br>
+  if (BufferSzStr && BufferSzStr[0]) {<br>
     VPBufferSize = atoi(BufferSzStr);<br>
+    DynamicBufferIOBuffer = (uint8_t *)calloc(VPBufferSize, 1);<br>
+  }<br>
+}<br>
+<br>
+static int writeFile(FILE *File) {<br>
+  FreeHook = &free;<br>
+  setupIOBuffer();<br>
   return lprofWriteData(fileWriter, File, lprofGatherValueProfData);<br>
 }<br>
<br>
<br>
Modified: compiler-rt/trunk/lib/profile/InstrProfilingInternal.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingInternal.h?rev=269453&r1=269452&r2=269453&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingInternal.h?rev=269453&r1=269452&r2=269453&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/profile/InstrProfilingInternal.h (original)<br>
+++ compiler-rt/trunk/lib/profile/InstrProfilingInternal.h Fri May 13 13:26:26 2016<br>
@@ -68,12 +68,13 @@ typedef struct ProfBufferIO {<br>
 } ProfBufferIO;<br>
<br>
 /* The creator interface used by testing.  */<br>
-ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t DefaultBufferSz);<br>
+ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);<br>
+<br>
 /*!<br>
  * This is the interface to create a handle for buffered IO.<br>
  */<br>
-ProfBufferIO *lprofCreateBufferIO(WriterCallback FileWriter, void *File,<br>
-                                  uint32_t DefaultBufferSz);<br>
+ProfBufferIO *lprofCreateBufferIO(WriterCallback FileWriter, void *File);<br>
+<br>
 /*!<br>
  * The interface to destroy the bufferIO handle and reclaim<br>
  * the memory.<br>
@@ -119,8 +120,8 @@ void lprofMergeValueProfData(struct Valu<br>
<br>
 COMPILER_RT_VISIBILITY extern char *(*GetEnvHook)(const char *);<br>
 COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);<br>
-COMPILER_RT_VISIBILITY extern void *(*CallocHook)(size_t, size_t);<br>
+COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;<br>
+COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;<br>
 extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);<br>
-extern uint32_t VPBufferSize;<br>
<br>
 #endif<br>
<br>
Modified: compiler-rt/trunk/lib/profile/InstrProfilingWriter.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingWriter.c?rev=269453&r1=269452&r2=269453&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingWriter.c?rev=269453&r1=269452&r2=269453&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/profile/InstrProfilingWriter.c (original)<br>
+++ compiler-rt/trunk/lib/profile/InstrProfilingWriter.c Fri May 13 13:26:26 2016<br>
@@ -13,9 +13,13 @@<br>
<br>
 #define INSTR_PROF_VALUE_PROF_DATA<br>
 #include "InstrProfData.inc"<br>
+<br>
 COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;<br>
-COMPILER_RT_VISIBILITY void *(*CallocHook)(size_t, size_t) = NULL;<br>
-uint32_t VPBufferSize = 0;<br>
+static ProfBufferIO TheBufferIO;<br>
+#define VP_BUFFER_SIZE 8 * 1024<br>
+static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];<br>
+COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;<br>
+COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;<br>
<br>
 /* The buffer writer is reponsponsible in keeping writer state<br>
  * across the call.<br>
@@ -43,20 +47,20 @@ static void llvmInitBufferIO(ProfBufferI<br>
 }<br>
<br>
 COMPILER_RT_VISIBILITY ProfBufferIO *<br>
-lprofCreateBufferIO(WriterCallback FileWriter, void *File, uint32_t BufferSz) {<br>
-  ProfBufferIO *BufferIO = (ProfBufferIO *)CallocHook(1, sizeof(ProfBufferIO));<br>
-  uint8_t *Buffer = (uint8_t *)CallocHook(1, BufferSz);<br>
+lprofCreateBufferIO(WriterCallback FileWriter, void *File) {<br>
+  uint8_t *Buffer = DynamicBufferIOBuffer;<br>
+  uint32_t BufferSize = VPBufferSize;<br>
   if (!Buffer) {<br>
-    FreeHook(BufferIO);<br>
-    return 0;<br>
+    Buffer = &BufferIOBuffer[0];<br>
+    BufferSize = sizeof(BufferIOBuffer);<br>
   }<br>
-  llvmInitBufferIO(BufferIO, FileWriter, File, Buffer, BufferSz);<br>
-  return BufferIO;<br>
+  llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize);<br>
+  return &TheBufferIO;<br>
 }<br>
<br>
 COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {<br>
-  FreeHook(BufferIO->BufferStart);<br>
-  FreeHook(BufferIO);<br>
+  if (DynamicBufferIOBuffer)<br>
+    FreeHook(DynamicBufferIOBuffer);<br></blockquote><div><br></div><div>Should we set DynamicBufferIOBuffer to NULL to avoid trying to free it again?</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
 }<br>
<br>
 COMPILER_RT_VISIBILITY int<br>
@@ -91,8 +95,6 @@ COMPILER_RT_VISIBILITY int lprofBufferIO<br>
   return 0;<br>
 }<br>
<br>
-#define VP_BUFFER_SIZE 8 * 1024<br>
-<br>
 static int writeOneValueProfData(ProfBufferIO *BufferIO,<br>
                                  VPGatherHookType VPDataGatherer,<br>
                                  const __llvm_profile_data *Data) {<br>
@@ -111,14 +113,12 @@ static int writeValueProfData(WriterCall<br>
                               const __llvm_profile_data *DataBegin,<br>
                               const __llvm_profile_data *DataEnd) {<br>
   ProfBufferIO *BufferIO;<br>
-  uint32_t BufferSz;<br>
   const __llvm_profile_data *DI = 0;<br>
<br>
   if (!VPDataGatherer)<br>
     return 0;<br>
<br>
-  BufferSz = VPBufferSize ? VPBufferSize : VP_BUFFER_SIZE;<br>
-  BufferIO = lprofCreateBufferIO(Writer, WriterCtx, BufferSz);<br>
+  BufferIO = lprofCreateBufferIO(Writer, WriterCtx);<br>
<br>
   for (DI = DataBegin; DI < DataEnd; DI++) {<br>
     if (writeOneValueProfData(BufferIO, VPDataGatherer, DI))<br>
<br>
Modified: compiler-rt/trunk/test/profile/instrprof-bufferio.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/instrprof-bufferio.c?rev=269453&r1=269452&r2=269453&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/instrprof-bufferio.c?rev=269453&r1=269452&r2=269453&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/profile/instrprof-bufferio.c (original)<br>
+++ compiler-rt/trunk/test/profile/instrprof-bufferio.c Fri May 13 13:26:26 2016<br>
@@ -11,7 +11,7 @@<br>
 #include <string.h><br>
<br>
 typedef struct ProfBufferIO ProfBufferIO;<br>
-ProfBufferIO *lprofCreateBufferIOInternal(FILE *File, uint32_t DefaultBufferSz);<br>
+ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);<br>
 void lprofDeleteBufferIO(ProfBufferIO *BufferIO);<br>
<br>
 int lprofBufferIOWrite(ProfBufferIO *BufferIO, const char *Data, uint32_t Size);<br>
@@ -44,7 +44,8 @@ int main(int argc, const char *argv[]) {<br>
<br>
     BufferIO = lprofCreateBufferIOInternal(File[J], IOBufferSize[J]);<br>
<br>
-    lprofBufferIOWrite(BufferIO, "Short Strings:\n", strlen("Short Strings:\n"));<br>
+    lprofBufferIOWrite(BufferIO, "Short Strings:\n",<br>
+                       strlen("Short Strings:\n"));<br>
     for (I = 0; I < 1024; I++) {<br>
       lprofBufferIOWrite(BufferIO, SmallData, strlen(SmallData));<br>
     }<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>