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 #include "InstrProfilingPort.h"
12 
13 /* When continuous mode is enabled (%c), this parameter is set to 1.
14  *
15  * This parameter is defined here in InstrProfilingBuffer.o, instead of in
16  * InstrProfilingFile.o, to sequester all libc-dependent code in
17  * InstrProfilingFile.o. The test `instrprof-without-libc` will break if this
18  * layering is violated. */
19 static int ContinuouslySyncProfile = 0;
20 
21 COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) {
22   return ContinuouslySyncProfile;
23 }
24 
25 COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {
26   ContinuouslySyncProfile = 1;
27 }
28 
29 COMPILER_RT_VISIBILITY
30 uint64_t __llvm_profile_get_size_for_buffer(void) {
31   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
32   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
33   const uint64_t *CountersBegin = __llvm_profile_begin_counters();
34   const uint64_t *CountersEnd = __llvm_profile_end_counters();
35   const char *NamesBegin = __llvm_profile_begin_names();
36   const char *NamesEnd = __llvm_profile_end_names();
37 
38   return __llvm_profile_get_size_for_buffer_internal(
39       DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
40 }
41 
42 COMPILER_RT_VISIBILITY
43 uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
44                                       const __llvm_profile_data *End) {
45   intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
46   return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
47          sizeof(__llvm_profile_data);
48 }
49 
50 /// Calculate the number of padding bytes needed to add to \p Offset in order
51 /// for (\p Offset + Padding) to be page-aligned.
52 static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset,
53                                                 unsigned PageSize) {
54   uint64_t OffsetModPage = Offset % PageSize;
55   if (OffsetModPage > 0)
56     return PageSize - OffsetModPage;
57   return 0;
58 }
59 
60 COMPILER_RT_VISIBILITY
61 void __llvm_profile_get_padding_sizes_for_counters(
62     uint64_t DataSize, uint64_t CountersSize, uint64_t NamesSize,
63     uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
64     uint64_t *PaddingBytesAfterNames) {
65   if (!__llvm_profile_is_continuous_mode_enabled()) {
66     *PaddingBytesBeforeCounters = 0;
67     *PaddingBytesAfterCounters = 0;
68     *PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);
69     return;
70   }
71 
72   // In continuous mode, the file offsets for headers and for the start of
73   // counter sections need to be page-aligned.
74   unsigned PageSize = getpagesize();
75   uint64_t DataSizeInBytes = DataSize * sizeof(__llvm_profile_data);
76   uint64_t CountersSizeInBytes = CountersSize * sizeof(uint64_t);
77   *PaddingBytesBeforeCounters = calculateBytesNeededToPageAlign(
78       sizeof(__llvm_profile_header) + DataSizeInBytes, PageSize);
79   *PaddingBytesAfterCounters =
80       calculateBytesNeededToPageAlign(CountersSizeInBytes, PageSize);
81   *PaddingBytesAfterNames =
82       calculateBytesNeededToPageAlign(NamesSize, PageSize);
83 }
84 
85 COMPILER_RT_VISIBILITY
86 uint64_t __llvm_profile_get_size_for_buffer_internal(
87     const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
88     const uint64_t *CountersBegin, const uint64_t *CountersEnd,
89     const char *NamesBegin, const char *NamesEnd) {
90   /* Match logic in __llvm_profile_write_buffer(). */
91   const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
92   uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
93   uint64_t CountersSize = CountersEnd - CountersBegin;
94 
95   /* Determine how much padding is needed before/after the counters and after
96    * the names. */
97   uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
98       PaddingBytesAfterNames;
99   __llvm_profile_get_padding_sizes_for_counters(
100       DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
101       &PaddingBytesAfterCounters, &PaddingBytesAfterNames);
102 
103   return sizeof(__llvm_profile_header) +
104          (DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters +
105          (CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters +
106          NamesSize + PaddingBytesAfterNames;
107 }
108 
109 COMPILER_RT_VISIBILITY
110 void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) {
111   BufferWriter->Write = lprofBufferWriter;
112   BufferWriter->WriterCtx = Buffer;
113 }
114 
115 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
116   ProfDataWriter BufferWriter;
117   initBufferWriter(&BufferWriter, Buffer);
118   return lprofWriteData(&BufferWriter, 0, 0);
119 }
120 
121 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
122     char *Buffer, const __llvm_profile_data *DataBegin,
123     const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
124     const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
125   ProfDataWriter BufferWriter;
126   initBufferWriter(&BufferWriter, Buffer);
127   return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin,
128                             CountersEnd, 0, NamesBegin, NamesEnd, 0);
129 }
130