xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingInternal.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 *NamesBegin,
25     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 *NamesBegin, const char *NamesEnd);
40 
41 /*!
42  * The data structure describing the data to be written by the
43  * low level writer callback function.
44  *
45  * If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
46  * 0, the write is skipped (the writer simply advances ElmSize*NumElm bytes).
47  *
48  * If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
49  * nonzero, ElmSize*NumElm zero bytes are written.
50  */
51 typedef struct ProfDataIOVec {
52   const void *Data;
53   size_t ElmSize;
54   size_t NumElm;
55   int UseZeroPadding;
56 } ProfDataIOVec;
57 
58 struct ProfDataWriter;
59 typedef uint32_t (*WriterCallback)(struct ProfDataWriter *This, ProfDataIOVec *,
60                                    uint32_t NumIOVecs);
61 
62 typedef struct ProfDataWriter {
63   WriterCallback Write;
64   void *WriterCtx;
65 } ProfDataWriter;
66 
67 /*!
68  * The data structure for buffered IO of profile data.
69  */
70 typedef struct ProfBufferIO {
71   ProfDataWriter *FileWriter;
72   uint32_t OwnFileWriter;
73   /* The start of the buffer. */
74   uint8_t *BufferStart;
75   /* Total size of the buffer. */
76   uint32_t BufferSz;
77   /* Current byte offset from the start of the buffer. */
78   uint32_t CurOffset;
79 } ProfBufferIO;
80 
81 /* The creator interface used by testing.  */
82 ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
83 
84 /*!
85  * This is the interface to create a handle for buffered IO.
86  */
87 ProfBufferIO *lprofCreateBufferIO(ProfDataWriter *FileWriter);
88 
89 /*!
90  * The interface to destroy the bufferIO handle and reclaim
91  * the memory.
92  */
93 void lprofDeleteBufferIO(ProfBufferIO *BufferIO);
94 
95 /*!
96  * This is the interface to write \c Data of \c Size bytes through
97  * \c BufferIO. Returns 0 if successful, otherwise return -1.
98  */
99 int lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,
100                        uint32_t Size);
101 /*!
102  * The interface to flush the remaining data in the buffer.
103  * through the low level writer callback.
104  */
105 int lprofBufferIOFlush(ProfBufferIO *BufferIO);
106 
107 /* The low level interface to write data into a buffer. It is used as the
108  * callback by other high level writer methods such as buffered IO writer
109  * and profile data writer.  */
110 uint32_t lprofBufferWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
111                            uint32_t NumIOVecs);
112 void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer);
113 
114 struct ValueProfData;
115 struct ValueProfRecord;
116 struct InstrProfValueData;
117 struct ValueProfNode;
118 
119 /*!
120  * The class that defines a set of methods to read value profile
121  * data for streaming/serialization from the instrumentation runtime.
122  */
123 typedef struct VPDataReaderType {
124   uint32_t (*InitRTRecord)(const __llvm_profile_data *Data,
125                            uint8_t *SiteCountArray[]);
126   /* Function pointer to getValueProfRecordHeader method. */
127   uint32_t (*GetValueProfRecordHeaderSize)(uint32_t NumSites);
128   /* Function pointer to getFristValueProfRecord method. */
129   struct ValueProfRecord *(*GetFirstValueProfRecord)(struct ValueProfData *);
130   /* Return the number of value data for site \p Site.  */
131   uint32_t (*GetNumValueDataForSite)(uint32_t VK, uint32_t Site);
132   /* Return the total size of the value profile data of the
133    * current function.  */
134   uint32_t (*GetValueProfDataSize)(void);
135   /*!
136    * Read the next \p N value data for site \p Site and store the data
137    * in \p Dst. \p StartNode is the first value node to start with if
138    * it is not null. The function returns the pointer to the value
139    * node pointer to be used as the \p StartNode of the next batch reading.
140    * If there is nothing left, it returns NULL.
141    */
142   struct ValueProfNode *(*GetValueData)(uint32_t ValueKind, uint32_t Site,
143                                         struct InstrProfValueData *Dst,
144                                         struct ValueProfNode *StartNode,
145                                         uint32_t N);
146 } VPDataReaderType;
147 
148 /* Write profile data to destination. If SkipNameDataWrite is set to 1,
149    the name data is already in destination, we just skip over it. */
150 int lprofWriteData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader,
151                    int SkipNameDataWrite);
152 int lprofWriteDataImpl(ProfDataWriter *Writer,
153                        const __llvm_profile_data *DataBegin,
154                        const __llvm_profile_data *DataEnd,
155                        const char *CountersBegin, const char *CountersEnd,
156                        VPDataReaderType *VPDataReader, const char *NamesBegin,
157                        const char *NamesEnd, int SkipNameDataWrite);
158 
159 /* Merge value profile data pointed to by SrcValueProfData into
160  * in-memory profile counters pointed by to DstData.  */
161 void lprofMergeValueProfData(struct ValueProfData *SrcValueProfData,
162                              __llvm_profile_data *DstData);
163 
164 VPDataReaderType *lprofGetVPDataReader();
165 
166 /* Internal interface used by test to reset the max number of
167  * tracked values per value site to be \p MaxVals.
168  */
169 void lprofSetMaxValsPerSite(uint32_t MaxVals);
170 void lprofSetupValueProfiler();
171 
172 /* Return the profile header 'signature' value associated with the current
173  * executable or shared library. The signature value can be used to for
174  * a profile name that is unique to this load module so that it does not
175  * collide with profiles from other binaries. It also allows shared libraries
176  * to dump merged profile data into its own profile file. */
177 uint64_t lprofGetLoadModuleSignature();
178 
179 /*
180  * Return non zero value if the profile data has already been
181  * dumped to the file.
182  */
183 unsigned lprofProfileDumped(void);
184 void lprofSetProfileDumped(unsigned);
185 
186 COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
187 COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
188 COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;
189 COMPILER_RT_VISIBILITY extern uint32_t VPMaxNumValsPerSite;
190 /* Pointer to the start of static value counters to be allocted. */
191 COMPILER_RT_VISIBILITY extern ValueProfNode *CurrentVNode;
192 COMPILER_RT_VISIBILITY extern ValueProfNode *EndVNode;
193 extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);
194 
195 /*
196  * Write binary ids into profiles if writer is given.
197  * Return -1 if an error occurs, otherwise, return total size of binary ids.
198  */
199 int __llvm_write_binary_ids(ProfDataWriter *Writer);
200 
201 #endif
202