xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingInternal.h (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 /*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
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 #ifndef PROFILE_INSTRPROFILING_INTERNALH_
10 #define PROFILE_INSTRPROFILING_INTERNALH_
11 
12 #include <stddef.h>
13 
14 #include "InstrProfiling.h"
15 
16 /*!
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.
21  */
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);
26 
27 /*!
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.
32  *
33  * \pre \c Buffer is the start of a buffer at least as big as \a
34  * __llvm_profile_get_size_for_buffer_internal().
35  */
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);
41 
42 /*!
43  * The data structure describing the data to be written by the
44  * low level writer callback function.
45  *
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).
48  *
49  * If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
50  * nonzero, ElmSize*NumElm zero bytes are written.
51  */
52 typedef struct ProfDataIOVec {
53   const void *Data;
54   size_t ElmSize;
55   size_t NumElm;
56   int UseZeroPadding;
57 } ProfDataIOVec;
58 
59 struct ProfDataWriter;
60 typedef uint32_t (*WriterCallback)(struct ProfDataWriter *This, ProfDataIOVec *,
61                                    uint32_t NumIOVecs);
62 
63 typedef struct ProfDataWriter {
64   WriterCallback Write;
65   void *WriterCtx;
66 } ProfDataWriter;
67 
68 /*!
69  * The data structure for buffered IO of profile data.
70  */
71 typedef struct ProfBufferIO {
72   ProfDataWriter *FileWriter;
73   uint32_t OwnFileWriter;
74   /* The start of the buffer. */
75   uint8_t *BufferStart;
76   /* Total size of the buffer. */
77   uint32_t BufferSz;
78   /* Current byte offset from the start of the buffer. */
79   uint32_t CurOffset;
80 } ProfBufferIO;
81 
82 /* The creator interface used by testing.  */
83 ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
84 
85 /*!
86  * This is the interface to create a handle for buffered IO.
87  */
88 ProfBufferIO *lprofCreateBufferIO(ProfDataWriter *FileWriter);
89 
90 /*!
91  * The interface to destroy the bufferIO handle and reclaim
92  * the memory.
93  */
94 void lprofDeleteBufferIO(ProfBufferIO *BufferIO);
95 
96 /*!
97  * This is the interface to write \c Data of \c Size bytes through
98  * \c BufferIO. Returns 0 if successful, otherwise return -1.
99  */
100 int lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,
101                        uint32_t Size);
102 /*!
103  * The interface to flush the remaining data in the buffer.
104  * through the low level writer callback.
105  */
106 int lprofBufferIOFlush(ProfBufferIO *BufferIO);
107 
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,
112                            uint32_t NumIOVecs);
113 void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer);
114 
115 struct ValueProfData;
116 struct ValueProfRecord;
117 struct InstrProfValueData;
118 struct ValueProfNode;
119 
120 /*!
121  * The class that defines a set of methods to read value profile
122  * data for streaming/serialization from the instrumentation runtime.
123  */
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);
136   /*!
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.
142    */
143   struct ValueProfNode *(*GetValueData)(uint32_t ValueKind, uint32_t Site,
144                                         struct InstrProfValueData *Dst,
145                                         struct ValueProfNode *StartNode,
146                                         uint32_t N);
147 } VPDataReaderType;
148 
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);
160 
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);
165 
166 VPDataReaderType *lprofGetVPDataReader();
167 
168 /* Internal interface used by test to reset the max number of
169  * tracked values per value site to be \p MaxVals.
170  */
171 void lprofSetMaxValsPerSite(uint32_t MaxVals);
172 void lprofSetupValueProfiler();
173 
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();
180 
181 /*
182  * Return non zero value if the profile data has already been
183  * dumped to the file.
184  */
185 unsigned lprofProfileDumped(void);
186 void lprofSetProfileDumped(unsigned);
187 
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 *);
196 
197 /*
198  * Write binary ids into profiles if writer is given.
199  * Return -1 if an error occurs, otherwise, return total size of binary ids.
200  */
201 int __llvm_write_binary_ids(ProfDataWriter *Writer);
202 
203 /*
204  * Write binary id length and then its data, because binary id does not
205  * have a fixed length.
206  */
207 int lprofWriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,
208                           const uint8_t *BinaryIdData,
209                           uint64_t BinaryIdPadding);
210 
211 #endif
212