1 /*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
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
7 \*===----------------------------------------------------------------------===*/
9 #ifndef PROFILE_INSTRPROFILING_INTERNALH_
10 #define PROFILE_INSTRPROFILING_INTERNALH_
14 #include "InstrProfiling.h"
17 * \brief Write instrumentation data to the given buffer, given explicit
18 * pointers to the live data in memory. This function is probably not what you
19 * want. Use __llvm_profile_get_size_for_buffer instead. Use this function if
20 * your program has a custom memory layout.
22 uint64_t __llvm_profile_get_size_for_buffer_internal(
23 const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
24 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
25 const char *NamesBegin, const char *NamesEnd);
28 * \brief Write instrumentation data to the given buffer, given explicit
29 * pointers to the live data in memory. This function is probably not what you
30 * want. Use __llvm_profile_write_buffer instead. Use this function if your
31 * program has a custom memory layout.
33 * \pre \c Buffer is the start of a buffer at least as big as \a
34 * __llvm_profile_get_size_for_buffer_internal().
36 int __llvm_profile_write_buffer_internal(
37 char *Buffer, const __llvm_profile_data *DataBegin,
38 const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
39 const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd);
42 * The data structure describing the data to be written by the
43 * low level writer callback function.
45 typedef struct ProfDataIOVec {
51 struct ProfDataWriter;
52 typedef uint32_t (*WriterCallback)(struct ProfDataWriter *This, ProfDataIOVec *,
55 typedef struct ProfDataWriter {
61 * The data structure for buffered IO of profile data.
63 typedef struct ProfBufferIO {
64 ProfDataWriter *FileWriter;
65 uint32_t OwnFileWriter;
66 /* The start of the buffer. */
68 /* Total size of the buffer. */
70 /* Current byte offset from the start of the buffer. */
74 /* The creator interface used by testing. */
75 ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
78 * This is the interface to create a handle for buffered IO.
80 ProfBufferIO *lprofCreateBufferIO(ProfDataWriter *FileWriter);
83 * The interface to destroy the bufferIO handle and reclaim
86 void lprofDeleteBufferIO(ProfBufferIO *BufferIO);
89 * This is the interface to write \c Data of \c Size bytes through
90 * \c BufferIO. Returns 0 if successful, otherwise return -1.
92 int lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,
95 * The interface to flush the remaining data in the buffer.
96 * through the low level writer callback.
98 int lprofBufferIOFlush(ProfBufferIO *BufferIO);
100 /* The low level interface to write data into a buffer. It is used as the
101 * callback by other high level writer methods such as buffered IO writer
102 * and profile data writer. */
103 uint32_t lprofBufferWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
105 void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer);
107 struct ValueProfData;
108 struct ValueProfRecord;
109 struct InstrProfValueData;
110 struct ValueProfNode;
113 * The class that defines a set of methods to read value profile
114 * data for streaming/serialization from the instrumentation runtime.
116 typedef struct VPDataReaderType {
117 uint32_t (*InitRTRecord)(const __llvm_profile_data *Data,
118 uint8_t *SiteCountArray[]);
119 /* Function pointer to getValueProfRecordHeader method. */
120 uint32_t (*GetValueProfRecordHeaderSize)(uint32_t NumSites);
121 /* Function pointer to getFristValueProfRecord method. */
122 struct ValueProfRecord *(*GetFirstValueProfRecord)(struct ValueProfData *);
123 /* Return the number of value data for site \p Site. */
124 uint32_t (*GetNumValueDataForSite)(uint32_t VK, uint32_t Site);
125 /* Return the total size of the value profile data of the
126 * current function. */
127 uint32_t (*GetValueProfDataSize)(void);
129 * Read the next \p N value data for site \p Site and store the data
130 * in \p Dst. \p StartNode is the first value node to start with if
131 * it is not null. The function returns the pointer to the value
132 * node pointer to be used as the \p StartNode of the next batch reading.
133 * If there is nothing left, it returns NULL.
135 struct ValueProfNode *(*GetValueData)(uint32_t ValueKind, uint32_t Site,
136 struct InstrProfValueData *Dst,
137 struct ValueProfNode *StartNode,
141 /* Write profile data to destinitation. If SkipNameDataWrite is set to 1,
142 the name data is already in destintation, we just skip over it. */
143 int lprofWriteData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader,
144 int SkipNameDataWrite);
145 int lprofWriteDataImpl(ProfDataWriter *Writer,
146 const __llvm_profile_data *DataBegin,
147 const __llvm_profile_data *DataEnd,
148 const uint64_t *CountersBegin,
149 const uint64_t *CountersEnd,
150 VPDataReaderType *VPDataReader, const char *NamesBegin,
151 const char *NamesEnd, int SkipNameDataWrite);
153 /* Merge value profile data pointed to by SrcValueProfData into
154 * in-memory profile counters pointed by to DstData. */
155 void lprofMergeValueProfData(struct ValueProfData *SrcValueProfData,
156 __llvm_profile_data *DstData);
158 VPDataReaderType *lprofGetVPDataReader();
160 /* Internal interface used by test to reset the max number of
161 * tracked values per value site to be \p MaxVals.
163 void lprofSetMaxValsPerSite(uint32_t MaxVals);
164 void lprofSetupValueProfiler();
166 /* Return the profile header 'signature' value associated with the current
167 * executable or shared library. The signature value can be used to for
168 * a profile name that is unique to this load module so that it does not
169 * collide with profiles from other binaries. It also allows shared libraries
170 * to dump merged profile data into its own profile file. */
171 uint64_t lprofGetLoadModuleSignature();
174 * Return non zero value if the profile data has already been
175 * dumped to the file.
177 unsigned lprofProfileDumped();
178 void lprofSetProfileDumped();
180 COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
181 COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
182 COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;
183 COMPILER_RT_VISIBILITY extern uint32_t VPMaxNumValsPerSite;
184 /* Pointer to the start of static value counters to be allocted. */
185 COMPILER_RT_VISIBILITY extern ValueProfNode *CurrentVNode;
186 COMPILER_RT_VISIBILITY extern ValueProfNode *EndVNode;
187 extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);