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. This is
14  * incompatible with the in-process merging mode. Lifting this restriction
15  * may be complicated, as merging mode requires a lock on the profile, and
16  * mmap() mode would require that lock to be held for the entire process
17  * lifetime.
18  *
19  * This parameter is defined here in InstrProfilingBuffer.o, instead of in
20  * InstrProfilingFile.o, to sequester all libc-dependent code in
21  * InstrProfilingFile.o. The test `instrprof-without-libc` will break if this
22  * layering is violated. */
23 static int ContinuouslySyncProfile = 0;
24 
25 COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) {
26   return ContinuouslySyncProfile;
27 }
28 
29 COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {
30   ContinuouslySyncProfile = 1;
31 }
32 
33 COMPILER_RT_VISIBILITY
34 uint64_t __llvm_profile_get_size_for_buffer(void) {
35   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
36   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
37   const uint64_t *CountersBegin = __llvm_profile_begin_counters();
38   const uint64_t *CountersEnd = __llvm_profile_end_counters();
39   const char *NamesBegin = __llvm_profile_begin_names();
40   const char *NamesEnd = __llvm_profile_end_names();
41 
42   return __llvm_profile_get_size_for_buffer_internal(
43       DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
44 }
45 
46 COMPILER_RT_VISIBILITY
47 uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
48                                       const __llvm_profile_data *End) {
49   intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
50   return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
51          sizeof(__llvm_profile_data);
52 }
53 
54 /// Calculate the number of padding bytes needed to add to \p Offset in order
55 /// for (\p Offset + Padding) to be page-aligned.
56 static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset,
57                                                 unsigned PageSize) {
58   uint64_t OffsetModPage = Offset % PageSize;
59   if (OffsetModPage > 0)
60     return PageSize - OffsetModPage;
61   return 0;
62 }
63 
64 COMPILER_RT_VISIBILITY
65 void __llvm_profile_get_padding_sizes_for_counters(
66     uint64_t DataSize, uint64_t CountersSize, uint64_t NamesSize,
67     uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
68     uint64_t *PaddingBytesAfterNames) {
69   if (!__llvm_profile_is_continuous_mode_enabled()) {
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