xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingBuffer.c (revision 84e404763fa5a7e00105ff86e6caff5aebe0e55e)
1 /*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 \*===----------------------------------------------------------------------===*/
8 
9 #include "InstrProfiling.h"
10 #include "InstrProfilingInternal.h"
11 
12 COMPILER_RT_VISIBILITY
13 uint64_t __llvm_profile_get_size_for_buffer(void) {
14   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
15   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
16   const uint64_t *CountersBegin = __llvm_profile_begin_counters();
17   const uint64_t *CountersEnd = __llvm_profile_end_counters();
18   const char *NamesBegin = __llvm_profile_begin_names();
19   const char *NamesEnd = __llvm_profile_end_names();
20 
21   return __llvm_profile_get_size_for_buffer_internal(
22       DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
23 }
24 
25 COMPILER_RT_VISIBILITY
26 uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
27                                       const __llvm_profile_data *End) {
28   intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
29   return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
30          sizeof(__llvm_profile_data);
31 }
32 
33 COMPILER_RT_VISIBILITY
34 uint64_t __llvm_profile_get_size_for_buffer_internal(
35     const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
36     const uint64_t *CountersBegin, const uint64_t *CountersEnd,
37     const char *NamesBegin, const char *NamesEnd) {
38   /* Match logic in __llvm_profile_write_buffer(). */
39   const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
40   const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
41   return sizeof(__llvm_profile_header) +
42          (__llvm_profile_get_data_size(DataBegin, DataEnd) *
43           sizeof(__llvm_profile_data)) +
44          (CountersEnd - CountersBegin) * sizeof(uint64_t) + NamesSize + Padding;
45 }
46 
47 COMPILER_RT_VISIBILITY
48 void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) {
49   BufferWriter->Write = lprofBufferWriter;
50   BufferWriter->WriterCtx = Buffer;
51 }
52 
53 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
54   ProfDataWriter BufferWriter;
55   initBufferWriter(&BufferWriter, Buffer);
56   return lprofWriteData(&BufferWriter, 0, 0);
57 }
58 
59 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
60     char *Buffer, const __llvm_profile_data *DataBegin,
61     const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
62     const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
63   ProfDataWriter BufferWriter;
64   initBufferWriter(&BufferWriter, Buffer);
65   return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin,
66                             CountersEnd, 0, NamesBegin, NamesEnd, 0);
67 }
68