Home | History | Annotate | Download | only in profile
      1 /*===- InstrProfilingBuffer.c - Write instrumentation to a memory 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 
     13 COMPILER_RT_VISIBILITY
     14 uint64_t __llvm_profile_get_size_for_buffer(void) {
     15   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
     16   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
     17   const uint64_t *CountersBegin = __llvm_profile_begin_counters();
     18   const uint64_t *CountersEnd = __llvm_profile_end_counters();
     19   const char *NamesBegin = __llvm_profile_begin_names();
     20   const char *NamesEnd = __llvm_profile_end_names();
     21 
     22   return __llvm_profile_get_size_for_buffer_internal(
     23       DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
     24 }
     25 
     26 COMPILER_RT_VISIBILITY
     27 uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
     28                                       const __llvm_profile_data *End) {
     29   intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
     30   return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
     31          sizeof(__llvm_profile_data);
     32 }
     33 
     34 COMPILER_RT_VISIBILITY
     35 uint64_t __llvm_profile_get_size_for_buffer_internal(
     36     const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
     37     const uint64_t *CountersBegin, const uint64_t *CountersEnd,
     38     const char *NamesBegin, const char *NamesEnd) {
     39   /* Match logic in __llvm_profile_write_buffer(). */
     40   const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
     41   const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
     42   return sizeof(__llvm_profile_header) +
     43          (__llvm_profile_get_data_size(DataBegin, DataEnd) *
     44           sizeof(__llvm_profile_data)) +
     45          (CountersEnd - CountersBegin) * sizeof(uint64_t) + NamesSize + Padding;
     46 }
     47 
     48 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
     49   return lprofWriteData(lprofBufferWriter, Buffer, 0);
     50 }
     51 
     52 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
     53     char *Buffer, const __llvm_profile_data *DataBegin,
     54     const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
     55     const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
     56   return lprofWriteDataImpl(lprofBufferWriter, Buffer, DataBegin, DataEnd,
     57                             CountersBegin, CountersEnd, 0, NamesBegin,
     58                             NamesEnd);
     59 }
     60