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