xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/profile/InstrProfilingBuffer.c (revision ef84fd3bd8895f4e6be1e38baf19e6dc3255bc64)
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
__llvm_profile_get_size_for_buffer(void)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 #define PROFILE_RANGE_SIZE(Range) (Range##End - Range##Begin)
27 
28 COMPILER_RT_VISIBILITY
__llvm_profile_get_size_for_buffer_internal(const __llvm_profile_data * DataBegin,const __llvm_profile_data * DataEnd,const uint64_t * CountersBegin,const uint64_t * CountersEnd,const char * NamesBegin,const char * NamesEnd)29 uint64_t __llvm_profile_get_size_for_buffer_internal(
30     const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
31     const uint64_t *CountersBegin, const uint64_t *CountersEnd,
32     const char *NamesBegin, const char *NamesEnd) {
33   /* Match logic in __llvm_profile_write_buffer(). */
34   const uint64_t NamesSize = PROFILE_RANGE_SIZE(Names) * sizeof(char);
35   const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
36   return sizeof(__llvm_profile_header) +
37          PROFILE_RANGE_SIZE(Data) * sizeof(__llvm_profile_data) +
38          PROFILE_RANGE_SIZE(Counters) * sizeof(uint64_t) + NamesSize + Padding;
39 }
40 
__llvm_profile_write_buffer(char * Buffer)41 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
42   return llvmWriteProfData(llvmBufferWriter, Buffer, 0, 0);
43 }
44 
__llvm_profile_write_buffer_internal(char * Buffer,const __llvm_profile_data * DataBegin,const __llvm_profile_data * DataEnd,const uint64_t * CountersBegin,const uint64_t * CountersEnd,const char * NamesBegin,const char * NamesEnd)45 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
46     char *Buffer, const __llvm_profile_data *DataBegin,
47     const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
48     const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
49   return llvmWriteProfDataImpl(llvmBufferWriter, Buffer, DataBegin, DataEnd,
50                                CountersBegin, CountersEnd, 0, 0, NamesBegin,
51                                NamesEnd);
52 }
53