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. */
19static int ContinuouslySyncProfile = 0;
20
21COMPILER_RT_VISIBILITY int __llvm_profile_is_continuous_mode_enabled(void) {
22  return ContinuouslySyncProfile;
23}
24
25COMPILER_RT_VISIBILITY void __llvm_profile_enable_continuous_mode(void) {
26  ContinuouslySyncProfile = 1;
27}
28
29COMPILER_RT_VISIBILITY
30uint64_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
42COMPILER_RT_VISIBILITY
43uint64_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.
52static 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
60COMPILER_RT_VISIBILITY
61void __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      lprofRuntimeCounterRelocation()) {
67    *PaddingBytesBeforeCounters = 0;
68    *PaddingBytesAfterCounters = 0;
69    *PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);
70    return;
71  }
72
73  // In continuous mode, the file offsets for headers and for the start of
74  // counter sections need to be page-aligned.
75  unsigned PageSize = getpagesize();
76  uint64_t DataSizeInBytes = DataSize * sizeof(__llvm_profile_data);
77  uint64_t CountersSizeInBytes = CountersSize * sizeof(uint64_t);
78  *PaddingBytesBeforeCounters = calculateBytesNeededToPageAlign(
79      sizeof(__llvm_profile_header) + DataSizeInBytes, PageSize);
80  *PaddingBytesAfterCounters =
81      calculateBytesNeededToPageAlign(CountersSizeInBytes, PageSize);
82  *PaddingBytesAfterNames =
83      calculateBytesNeededToPageAlign(NamesSize, PageSize);
84}
85
86COMPILER_RT_VISIBILITY
87uint64_t __llvm_profile_get_size_for_buffer_internal(
88    const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
89    const uint64_t *CountersBegin, const uint64_t *CountersEnd,
90    const char *NamesBegin, const char *NamesEnd) {
91  /* Match logic in __llvm_profile_write_buffer(). */
92  const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
93  uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
94  uint64_t CountersSize = CountersEnd - CountersBegin;
95
96  /* Determine how much padding is needed before/after the counters and after
97   * the names. */
98  uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
99      PaddingBytesAfterNames;
100  __llvm_profile_get_padding_sizes_for_counters(
101      DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
102      &PaddingBytesAfterCounters, &PaddingBytesAfterNames);
103
104  return sizeof(__llvm_profile_header) +
105         (DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters +
106         (CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters +
107         NamesSize + PaddingBytesAfterNames;
108}
109
110COMPILER_RT_VISIBILITY
111void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer) {
112  BufferWriter->Write = lprofBufferWriter;
113  BufferWriter->WriterCtx = Buffer;
114}
115
116COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
117  ProfDataWriter BufferWriter;
118  initBufferWriter(&BufferWriter, Buffer);
119  return lprofWriteData(&BufferWriter, 0, 0);
120}
121
122COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
123    char *Buffer, const __llvm_profile_data *DataBegin,
124    const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
125    const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
126  ProfDataWriter BufferWriter;
127  initBufferWriter(&BufferWriter, Buffer);
128  return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin,
129                            CountersEnd, 0, NamesBegin, NamesEnd, 0);
130}
131