xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingBuffer.c (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
10b57cec5SDimitry Andric /*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "InstrProfiling.h"
100b57cec5SDimitry Andric #include "InstrProfilingInternal.h"
11*480093f4SDimitry Andric #include "InstrProfilingPort.h"
12*480093f4SDimitry Andric 
13*480093f4SDimitry Andric /* When continuous mode is enabled (%c), this parameter is set to 1.
14*480093f4SDimitry Andric  *
15*480093f4SDimitry Andric  * This parameter is defined here in InstrProfilingBuffer.o, instead of in
16*480093f4SDimitry Andric  * InstrProfilingFile.o, to sequester all libc-dependent code in
17*480093f4SDimitry Andric  * InstrProfilingFile.o. The test `instrprof-without-libc` will break if this
18*480093f4SDimitry Andric  * layering is violated. */
19*480093f4SDimitry Andric static int ContinuouslySyncProfile = 0;
20*480093f4SDimitry Andric 
21*480093f4SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) {
22*480093f4SDimitry Andric   return ContinuouslySyncProfile;
23*480093f4SDimitry Andric }
24*480093f4SDimitry Andric 
25*480093f4SDimitry Andric COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {
26*480093f4SDimitry Andric   ContinuouslySyncProfile = 1;
27*480093f4SDimitry Andric }
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
300b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer(void) {
310b57cec5SDimitry Andric   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
320b57cec5SDimitry Andric   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
330b57cec5SDimitry Andric   const uint64_t *CountersBegin = __llvm_profile_begin_counters();
340b57cec5SDimitry Andric   const uint64_t *CountersEnd = __llvm_profile_end_counters();
350b57cec5SDimitry Andric   const char *NamesBegin = __llvm_profile_begin_names();
360b57cec5SDimitry Andric   const char *NamesEnd = __llvm_profile_end_names();
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   return __llvm_profile_get_size_for_buffer_internal(
390b57cec5SDimitry Andric       DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
430b57cec5SDimitry Andric uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
440b57cec5SDimitry Andric                                       const __llvm_profile_data *End) {
450b57cec5SDimitry Andric   intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
460b57cec5SDimitry Andric   return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
470b57cec5SDimitry Andric          sizeof(__llvm_profile_data);
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
50*480093f4SDimitry Andric /// Calculate the number of padding bytes needed to add to \p Offset in order
51*480093f4SDimitry Andric /// for (\p Offset + Padding) to be page-aligned.
52*480093f4SDimitry Andric static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset,
53*480093f4SDimitry Andric                                                 unsigned PageSize) {
54*480093f4SDimitry Andric   uint64_t OffsetModPage = Offset % PageSize;
55*480093f4SDimitry Andric   if (OffsetModPage > 0)
56*480093f4SDimitry Andric     return PageSize - OffsetModPage;
57*480093f4SDimitry Andric   return 0;
58*480093f4SDimitry Andric }
59*480093f4SDimitry Andric 
60*480093f4SDimitry Andric COMPILER_RT_VISIBILITY
61*480093f4SDimitry Andric void __llvm_profile_get_padding_sizes_for_counters(
62*480093f4SDimitry Andric     uint64_t DataSize, uint64_t CountersSize, uint64_t NamesSize,
63*480093f4SDimitry Andric     uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
64*480093f4SDimitry Andric     uint64_t *PaddingBytesAfterNames) {
65*480093f4SDimitry Andric   if (!__llvm_profile_is_continuous_mode_enabled()) {
66*480093f4SDimitry Andric     *PaddingBytesBeforeCounters = 0;
67*480093f4SDimitry Andric     *PaddingBytesAfterCounters = 0;
68*480093f4SDimitry Andric     *PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);
69*480093f4SDimitry Andric     return;
70*480093f4SDimitry Andric   }
71*480093f4SDimitry Andric 
72*480093f4SDimitry Andric   // In continuous mode, the file offsets for headers and for the start of
73*480093f4SDimitry Andric   // counter sections need to be page-aligned.
74*480093f4SDimitry Andric   unsigned PageSize = getpagesize();
75*480093f4SDimitry Andric   uint64_t DataSizeInBytes = DataSize * sizeof(__llvm_profile_data);
76*480093f4SDimitry Andric   uint64_t CountersSizeInBytes = CountersSize * sizeof(uint64_t);
77*480093f4SDimitry Andric   *PaddingBytesBeforeCounters = calculateBytesNeededToPageAlign(
78*480093f4SDimitry Andric       sizeof(__llvm_profile_header) + DataSizeInBytes, PageSize);
79*480093f4SDimitry Andric   *PaddingBytesAfterCounters =
80*480093f4SDimitry Andric       calculateBytesNeededToPageAlign(CountersSizeInBytes, PageSize);
81*480093f4SDimitry Andric   *PaddingBytesAfterNames =
82*480093f4SDimitry Andric       calculateBytesNeededToPageAlign(NamesSize, PageSize);
83*480093f4SDimitry Andric }
84*480093f4SDimitry Andric 
850b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
860b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer_internal(
870b57cec5SDimitry Andric     const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
880b57cec5SDimitry Andric     const uint64_t *CountersBegin, const uint64_t *CountersEnd,
890b57cec5SDimitry Andric     const char *NamesBegin, const char *NamesEnd) {
900b57cec5SDimitry Andric   /* Match logic in __llvm_profile_write_buffer(). */
910b57cec5SDimitry Andric   const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
92*480093f4SDimitry Andric   uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
93*480093f4SDimitry Andric   uint64_t CountersSize = CountersEnd - CountersBegin;
94*480093f4SDimitry Andric 
95*480093f4SDimitry Andric   /* Determine how much padding is needed before/after the counters and after
96*480093f4SDimitry Andric    * the names. */
97*480093f4SDimitry Andric   uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
98*480093f4SDimitry Andric       PaddingBytesAfterNames;
99*480093f4SDimitry Andric   __llvm_profile_get_padding_sizes_for_counters(
100*480093f4SDimitry Andric       DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
101*480093f4SDimitry Andric       &PaddingBytesAfterCounters, &PaddingBytesAfterNames);
102*480093f4SDimitry Andric 
1030b57cec5SDimitry Andric   return sizeof(__llvm_profile_header) +
104*480093f4SDimitry Andric          (DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters +
105*480093f4SDimitry Andric          (CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters +
106*480093f4SDimitry Andric          NamesSize + PaddingBytesAfterNames;
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric COMPILER_RT_VISIBILITY
1100b57cec5SDimitry Andric void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) {
1110b57cec5SDimitry Andric   BufferWriter->Write = lprofBufferWriter;
1120b57cec5SDimitry Andric   BufferWriter->WriterCtx = Buffer;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
1160b57cec5SDimitry Andric   ProfDataWriter BufferWriter;
1170b57cec5SDimitry Andric   initBufferWriter(&BufferWriter, Buffer);
1180b57cec5SDimitry Andric   return lprofWriteData(&BufferWriter, 0, 0);
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
1220b57cec5SDimitry Andric     char *Buffer, const __llvm_profile_data *DataBegin,
1230b57cec5SDimitry Andric     const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
1240b57cec5SDimitry Andric     const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
1250b57cec5SDimitry Andric   ProfDataWriter BufferWriter;
1260b57cec5SDimitry Andric   initBufferWriter(&BufferWriter, Buffer);
1270b57cec5SDimitry Andric   return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin,
1280b57cec5SDimitry Andric                             CountersEnd, 0, NamesBegin, NamesEnd, 0);
1290b57cec5SDimitry Andric }
130