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 char *CountersBegin
, const char *CountersEnd
, const char *BitmapBegin
,
25 const char *BitmapEnd
, 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 char *CountersBegin
,
39 const char *CountersEnd
, const char *BitmapBegin
, const char *BitmapEnd
,
40 const char *NamesBegin
, const char *NamesEnd
);
43 * The data structure describing the data to be written by the
44 * low level writer callback function.
46 * If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
47 * 0, the write is skipped (the writer simply advances ElmSize*NumElm bytes).
49 * If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
50 * nonzero, ElmSize*NumElm zero bytes are written.
52 typedef struct ProfDataIOVec
{
59 struct ProfDataWriter
;
60 typedef uint32_t (*WriterCallback
)(struct ProfDataWriter
*This
, ProfDataIOVec
*,
63 typedef struct ProfDataWriter
{
69 * The data structure for buffered IO of profile data.
71 typedef struct ProfBufferIO
{
72 ProfDataWriter
*FileWriter
;
73 uint32_t OwnFileWriter
;
74 /* The start of the buffer. */
76 /* Total size of the buffer. */
78 /* Current byte offset from the start of the buffer. */
82 /* The creator interface used by testing. */
83 ProfBufferIO
*lprofCreateBufferIOInternal(void *File
, uint32_t BufferSz
);
86 * This is the interface to create a handle for buffered IO.
88 ProfBufferIO
*lprofCreateBufferIO(ProfDataWriter
*FileWriter
);
91 * The interface to destroy the bufferIO handle and reclaim
94 void lprofDeleteBufferIO(ProfBufferIO
*BufferIO
);
97 * This is the interface to write \c Data of \c Size bytes through
98 * \c BufferIO. Returns 0 if successful, otherwise return -1.
100 int lprofBufferIOWrite(ProfBufferIO
*BufferIO
, const uint8_t *Data
,
103 * The interface to flush the remaining data in the buffer.
104 * through the low level writer callback.
106 int lprofBufferIOFlush(ProfBufferIO
*BufferIO
);
108 /* The low level interface to write data into a buffer. It is used as the
109 * callback by other high level writer methods such as buffered IO writer
110 * and profile data writer. */
111 uint32_t lprofBufferWriter(ProfDataWriter
*This
, ProfDataIOVec
*IOVecs
,
113 void initBufferWriter(ProfDataWriter
*BufferWriter
, char *Buffer
);
115 struct ValueProfData
;
116 struct ValueProfRecord
;
117 struct InstrProfValueData
;
118 struct ValueProfNode
;
121 * The class that defines a set of methods to read value profile
122 * data for streaming/serialization from the instrumentation runtime.
124 typedef struct VPDataReaderType
{
125 uint32_t (*InitRTRecord
)(const __llvm_profile_data
*Data
,
126 uint8_t *SiteCountArray
[]);
127 /* Function pointer to getValueProfRecordHeader method. */
128 uint32_t (*GetValueProfRecordHeaderSize
)(uint32_t NumSites
);
129 /* Function pointer to getFristValueProfRecord method. */
130 struct ValueProfRecord
*(*GetFirstValueProfRecord
)(struct ValueProfData
*);
131 /* Return the number of value data for site \p Site. */
132 uint32_t (*GetNumValueDataForSite
)(uint32_t VK
, uint32_t Site
);
133 /* Return the total size of the value profile data of the
134 * current function. */
135 uint32_t (*GetValueProfDataSize
)(void);
137 * Read the next \p N value data for site \p Site and store the data
138 * in \p Dst. \p StartNode is the first value node to start with if
139 * it is not null. The function returns the pointer to the value
140 * node pointer to be used as the \p StartNode of the next batch reading.
141 * If there is nothing left, it returns NULL.
143 struct ValueProfNode
*(*GetValueData
)(uint32_t ValueKind
, uint32_t Site
,
144 struct InstrProfValueData
*Dst
,
145 struct ValueProfNode
*StartNode
,
149 /* Write profile data to destination. If SkipNameDataWrite is set to 1,
150 the name data is already in destination, we just skip over it. */
151 int lprofWriteData(ProfDataWriter
*Writer
, VPDataReaderType
*VPDataReader
,
152 int SkipNameDataWrite
);
153 int lprofWriteDataImpl(ProfDataWriter
*Writer
,
154 const __llvm_profile_data
*DataBegin
,
155 const __llvm_profile_data
*DataEnd
,
156 const char *CountersBegin
, const char *CountersEnd
,
157 const char *BitmapBegin
, const char *BitmapEnd
,
158 VPDataReaderType
*VPDataReader
, const char *NamesBegin
,
159 const char *NamesEnd
, int SkipNameDataWrite
);
161 /* Merge value profile data pointed to by SrcValueProfData into
162 * in-memory profile counters pointed by to DstData. */
163 void lprofMergeValueProfData(struct ValueProfData
*SrcValueProfData
,
164 __llvm_profile_data
*DstData
);
166 VPDataReaderType
*lprofGetVPDataReader();
168 /* Internal interface used by test to reset the max number of
169 * tracked values per value site to be \p MaxVals.
171 void lprofSetMaxValsPerSite(uint32_t MaxVals
);
172 void lprofSetupValueProfiler();
174 /* Return the profile header 'signature' value associated with the current
175 * executable or shared library. The signature value can be used to for
176 * a profile name that is unique to this load module so that it does not
177 * collide with profiles from other binaries. It also allows shared libraries
178 * to dump merged profile data into its own profile file. */
179 uint64_t lprofGetLoadModuleSignature();
182 * Return non zero value if the profile data has already been
183 * dumped to the file.
185 unsigned lprofProfileDumped(void);
186 void lprofSetProfileDumped(unsigned);
188 COMPILER_RT_VISIBILITY
extern void (*FreeHook
)(void *);
189 COMPILER_RT_VISIBILITY
extern uint8_t *DynamicBufferIOBuffer
;
190 COMPILER_RT_VISIBILITY
extern uint32_t VPBufferSize
;
191 COMPILER_RT_VISIBILITY
extern uint32_t VPMaxNumValsPerSite
;
192 /* Pointer to the start of static value counters to be allocted. */
193 COMPILER_RT_VISIBILITY
extern ValueProfNode
*CurrentVNode
;
194 COMPILER_RT_VISIBILITY
extern ValueProfNode
*EndVNode
;
195 extern void (*VPMergeHook
)(struct ValueProfData
*, __llvm_profile_data
*);
198 * Write binary ids into profiles if writer is given.
199 * Return -1 if an error occurs, otherwise, return total size of binary ids.
201 int __llvm_write_binary_ids(ProfDataWriter
*Writer
);
204 * Write binary id length and then its data, because binary id does not
205 * have a fixed length.
207 int lprofWriteOneBinaryId(ProfDataWriter
*Writer
, uint64_t BinaryIdLen
,
208 const uint8_t *BinaryIdData
,
209 uint64_t BinaryIdPadding
);