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