Home | History | Annotate | Line # | Download | only in profile
      1 /*===- InstrProfilingWriter.c - Write instrumentation to a file or buffer -===*\
      2 |*
      3 |*                     The LLVM Compiler Infrastructure
      4 |*
      5 |* This file is distributed under the University of Illinois Open Source
      6 |* License. See LICENSE.TXT for details.
      7 |*
      8 \*===----------------------------------------------------------------------===*/
      9 
     10 #include "InstrProfiling.h"
     11 #include "InstrProfilingInternal.h"
     12 #include <string.h>
     13 
     14 #define INSTR_PROF_VALUE_PROF_DATA
     15 #include "InstrProfData.inc"
     16 void (*FreeHook)(void *) = NULL;
     17 void* (*CallocHook)(size_t, size_t) = NULL;
     18 uint32_t VPBufferSize = 0;
     19 
     20 /* The buffer writer is reponsponsible in keeping writer state
     21  * across the call.
     22  */
     23 COMPILER_RT_VISIBILITY uint32_t llvmBufferWriter(ProfDataIOVec *IOVecs,
     24                                                  uint32_t NumIOVecs,
     25                                                  void **WriterCtx) {
     26   uint32_t I;
     27   char **Buffer = (char **)WriterCtx;
     28   for (I = 0; I < NumIOVecs; I++) {
     29     size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
     30     memcpy(*Buffer, IOVecs[I].Data, Length);
     31     *Buffer += Length;
     32   }
     33   return 0;
     34 }
     35 
     36 static void llvmInitBufferIO(ProfBufferIO *BufferIO, WriterCallback FileWriter,
     37                              void *File, uint8_t *Buffer, uint32_t BufferSz) {
     38   BufferIO->File = File;
     39   BufferIO->FileWriter = FileWriter;
     40   BufferIO->BufferStart = Buffer;
     41   BufferIO->BufferSz = BufferSz;
     42   BufferIO->CurOffset = 0;
     43 }
     44 
     45 COMPILER_RT_VISIBILITY ProfBufferIO *
     46 llvmCreateBufferIO(WriterCallback FileWriter, void *File, uint32_t BufferSz) {
     47   ProfBufferIO *BufferIO = (ProfBufferIO *)CallocHook(1, sizeof(ProfBufferIO));
     48   uint8_t *Buffer = (uint8_t *)CallocHook(1, BufferSz);
     49   if (!Buffer) {
     50     FreeHook(BufferIO);
     51     return 0;
     52   }
     53   llvmInitBufferIO(BufferIO, FileWriter, File, Buffer, BufferSz);
     54   return BufferIO;
     55 }
     56 
     57 COMPILER_RT_VISIBILITY void llvmDeleteBufferIO(ProfBufferIO *BufferIO) {
     58   FreeHook(BufferIO->BufferStart);
     59   FreeHook(BufferIO);
     60 }
     61 
     62 COMPILER_RT_VISIBILITY int
     63 llvmBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
     64   /* Buffer is not large enough, it is time to flush.  */
     65   if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
     66      if (llvmBufferIOFlush(BufferIO) != 0)
     67        return -1;
     68   }
     69   /* Special case, bypass the buffer completely. */
     70   ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}};
     71   if (Size > BufferIO->BufferSz) {
     72     if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
     73       return -1;
     74   } else {
     75     /* Write the data to buffer */
     76     uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
     77     llvmBufferWriter(IO, 1, (void **)&Buffer);
     78     BufferIO->CurOffset = Buffer - BufferIO->BufferStart;
     79   }
     80   return 0;
     81 }
     82 
     83 COMPILER_RT_VISIBILITY int llvmBufferIOFlush(ProfBufferIO *BufferIO) {
     84   if (BufferIO->CurOffset) {
     85     ProfDataIOVec IO[] = {
     86         {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}};
     87     if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
     88       return -1;
     89     BufferIO->CurOffset = 0;
     90   }
     91   return 0;
     92 }
     93 
     94 COMPILER_RT_VISIBILITY int llvmWriteProfData(WriterCallback Writer,
     95                                              void *WriterCtx,
     96                                              ValueProfData **ValueDataArray,
     97                                              const uint64_t ValueDataSize) {
     98   /* Match logic in __llvm_profile_write_buffer(). */
     99   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
    100   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
    101   const uint64_t *CountersBegin = __llvm_profile_begin_counters();
    102   const uint64_t *CountersEnd = __llvm_profile_end_counters();
    103   const char *NamesBegin = __llvm_profile_begin_names();
    104   const char *NamesEnd = __llvm_profile_end_names();
    105   return llvmWriteProfDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
    106                                CountersBegin, CountersEnd, ValueDataArray,
    107                                ValueDataSize, NamesBegin, NamesEnd);
    108 }
    109 
    110 #define VP_BUFFER_SIZE 8 * 1024
    111 static int writeValueProfData(WriterCallback Writer, void *WriterCtx,
    112                               ValueProfData **ValueDataBegin,
    113                               uint64_t NumVData) {
    114   ProfBufferIO *BufferIO;
    115   uint32_t I = 0, BufferSz;
    116 
    117   if (!ValueDataBegin)
    118     return 0;
    119 
    120   BufferSz = VPBufferSize ? VPBufferSize : VP_BUFFER_SIZE;
    121   BufferIO = llvmCreateBufferIO(Writer, WriterCtx, BufferSz);
    122 
    123   for (I = 0; I < NumVData; I++) {
    124     ValueProfData *CurVData = ValueDataBegin[I];
    125     if (!CurVData)
    126       continue;
    127     if (llvmBufferIOWrite(BufferIO, (const uint8_t *)CurVData,
    128                           CurVData->TotalSize) != 0)
    129       return -1;
    130   }
    131 
    132   if (llvmBufferIOFlush(BufferIO) != 0)
    133     return -1;
    134   llvmDeleteBufferIO(BufferIO);
    135 
    136   return 0;
    137 }
    138 
    139 COMPILER_RT_VISIBILITY int llvmWriteProfDataImpl(
    140     WriterCallback Writer, void *WriterCtx,
    141     const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
    142     const uint64_t *CountersBegin, const uint64_t *CountersEnd,
    143     ValueProfData **ValueDataBegin, const uint64_t ValueDataSize,
    144     const char *NamesBegin, const char *NamesEnd) {
    145 
    146   /* Calculate size of sections. */
    147   const uint64_t DataSize = DataEnd - DataBegin;
    148   const uint64_t CountersSize = CountersEnd - CountersBegin;
    149   const uint64_t NamesSize = NamesEnd - NamesBegin;
    150   const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
    151 
    152   /* Enough zeroes for padding. */
    153   const char Zeroes[sizeof(uint64_t)] = {0};
    154 
    155   /* Create the header. */
    156   __llvm_profile_header Header;
    157 
    158   if (!DataSize)
    159     return 0;
    160 
    161   /* Initialize header struture.  */
    162 #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
    163 #include "InstrProfData.inc"
    164 
    165   /* Write the data. */
    166   ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1},
    167                            {DataBegin, sizeof(__llvm_profile_data), DataSize},
    168                            {CountersBegin, sizeof(uint64_t), CountersSize},
    169                            {NamesBegin, sizeof(uint8_t), NamesSize},
    170                            {Zeroes, sizeof(uint8_t), Padding}};
    171   if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx))
    172     return -1;
    173 
    174   return writeValueProfData(Writer, WriterCtx, ValueDataBegin, DataSize);
    175 }
    176