1 /*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
3 |* The LLVM Compiler Infrastructure
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
8 \*===----------------------------------------------------------------------===*/
10 #ifndef PROFILE_INSTRPROFILING_INTERNALH_
11 #define PROFILE_INSTRPROFILING_INTERNALH_
15 #include "InstrProfiling.h"
18 * \brief Write instrumentation data to the given buffer, given explicit
19 * pointers to the live data in memory. This function is probably not what you
20 * want. Use __llvm_profile_get_size_for_buffer instead. Use this function if
21 * your program has a custom memory layout.
23 uint64_t __llvm_profile_get_size_for_buffer_internal(
24 const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
25 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
26 const char *NamesBegin, const char *NamesEnd);
29 * \brief Write instrumentation data to the given buffer, given explicit
30 * pointers to the live data in memory. This function is probably not what you
31 * want. Use __llvm_profile_write_buffer instead. Use this function if your
32 * program has a custom memory layout.
34 * \pre \c Buffer is the start of a buffer at least as big as \a
35 * __llvm_profile_get_size_for_buffer_internal().
37 int __llvm_profile_write_buffer_internal(
38 char *Buffer, const __llvm_profile_data *DataBegin,
39 const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
40 const uint64_t *CountersEnd, 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 typedef struct ProfDataIOVec {
52 struct ProfDataWriter;
53 typedef uint32_t (*WriterCallback)(struct ProfDataWriter *This, ProfDataIOVec *,
56 typedef struct ProfDataWriter {
62 * The data structure for buffered IO of profile data.
64 typedef struct ProfBufferIO {
65 ProfDataWriter *FileWriter;
66 uint32_t OwnFileWriter;
67 /* The start of the buffer. */
69 /* Total size of the buffer. */
71 /* Current byte offset from the start of the buffer. */
75 /* The creator interface used by testing. */
76 ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
79 * This is the interface to create a handle for buffered IO.
81 ProfBufferIO *lprofCreateBufferIO(ProfDataWriter *FileWriter);
84 * The interface to destroy the bufferIO handle and reclaim
87 void lprofDeleteBufferIO(ProfBufferIO *BufferIO);
90 * This is the interface to write \c Data of \c Size bytes through
91 * \c BufferIO. Returns 0 if successful, otherwise return -1.
93 int lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,
96 * The interface to flush the remaining data in the buffer.
97 * through the low level writer callback.
99 int lprofBufferIOFlush(ProfBufferIO *BufferIO);
101 /* The low level interface to write data into a buffer. It is used as the
102 * callback by other high level writer methods such as buffered IO writer
103 * and profile data writer. */
104 uint32_t lprofBufferWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
106 void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer);
108 struct ValueProfData;
109 struct ValueProfRecord;
110 struct InstrProfValueData;
111 struct ValueProfNode;
114 * The class that defines a set of methods to read value profile
115 * data for streaming/serialization from the instrumentation runtime.
117 typedef struct VPDataReaderType {
118 uint32_t (*InitRTRecord)(const __llvm_profile_data *Data,
119 uint8_t *SiteCountArray[]);
120 /* Function pointer to getValueProfRecordHeader method. */
121 uint32_t (*GetValueProfRecordHeaderSize)(uint32_t NumSites);
122 /* Function pointer to getFristValueProfRecord method. */
123 struct ValueProfRecord *(*GetFirstValueProfRecord)(struct ValueProfData *);
124 /* Return the number of value data for site \p Site. */
125 uint32_t (*GetNumValueDataForSite)(uint32_t VK, uint32_t Site);
126 /* Return the total size of the value profile data of the
127 * current function. */
128 uint32_t (*GetValueProfDataSize)(void);
130 * Read the next \p N value data for site \p Site and store the data
131 * in \p Dst. \p StartNode is the first value node to start with if
132 * it is not null. The function returns the pointer to the value
133 * node pointer to be used as the \p StartNode of the next batch reading.
134 * If there is nothing left, it returns NULL.
136 struct ValueProfNode *(*GetValueData)(uint32_t ValueKind, uint32_t Site,
137 struct InstrProfValueData *Dst,
138 struct ValueProfNode *StartNode,
142 /* Write profile data to destinitation. If SkipNameDataWrite is set to 1,
143 the name data is already in destintation, we just skip over it. */
144 int lprofWriteData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader,
145 int SkipNameDataWrite);
146 int lprofWriteDataImpl(ProfDataWriter *Writer,
147 const __llvm_profile_data *DataBegin,
148 const __llvm_profile_data *DataEnd,
149 const uint64_t *CountersBegin,
150 const uint64_t *CountersEnd,
151 VPDataReaderType *VPDataReader, const char *NamesBegin,
152 const char *NamesEnd, int SkipNameDataWrite);
154 /* Merge value profile data pointed to by SrcValueProfData into
155 * in-memory profile counters pointed by to DstData. */
156 void lprofMergeValueProfData(struct ValueProfData *SrcValueProfData,
157 __llvm_profile_data *DstData);
159 VPDataReaderType *lprofGetVPDataReader();
161 /* Internal interface used by test to reset the max number of
162 * tracked values per value site to be \p MaxVals.
164 void lprofSetMaxValsPerSite(uint32_t MaxVals);
165 void lprofSetupValueProfiler();
167 /* Return the profile header 'signature' value associated with the current
168 * executable or shared library. The signature value can be used to for
169 * a profile name that is unique to this load module so that it does not
170 * collide with profiles from other binaries. It also allows shared libraries
171 * to dump merged profile data into its own profile file. */
172 uint64_t lprofGetLoadModuleSignature();
175 * Return non zero value if the profile data has already been
176 * dumped to the file.
178 unsigned lprofProfileDumped();
179 void lprofSetProfileDumped();
181 COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
182 COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
183 COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;
184 COMPILER_RT_VISIBILITY extern uint32_t VPMaxNumValsPerSite;
185 /* Pointer to the start of static value counters to be allocted. */
186 COMPILER_RT_VISIBILITY extern ValueProfNode *CurrentVNode;
187 COMPILER_RT_VISIBILITY extern ValueProfNode *EndVNode;
188 extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);