]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/profile/InstrProfilingInternal.h
MFV r336851:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / profile / InstrProfilingInternal.h
1 /*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
2 |*
3 |*                     The LLVM Compiler Infrastructure
4 |*
5 |* This file is distributed under the University of Illinois Open Source
6 |* License. See LICENSE.TXT for details.
7 |*
8 \*===----------------------------------------------------------------------===*/
9
10 #ifndef PROFILE_INSTRPROFILING_INTERNALH_
11 #define PROFILE_INSTRPROFILING_INTERNALH_
12
13 #include <stddef.h>
14
15 #include "InstrProfiling.h"
16
17 /*!
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.
22  */
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);
27
28 /*!
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.
33  *
34  * \pre \c Buffer is the start of a buffer at least as big as \a
35  * __llvm_profile_get_size_for_buffer_internal().
36  */
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);
41
42 /*!
43  * The data structure describing the data to be written by the
44  * low level writer callback function.
45  */
46 typedef struct ProfDataIOVec {
47   const void *Data;
48   size_t ElmSize;
49   size_t NumElm;
50 } ProfDataIOVec;
51
52 struct ProfDataWriter;
53 typedef uint32_t (*WriterCallback)(struct ProfDataWriter *This, ProfDataIOVec *,
54                                    uint32_t NumIOVecs);
55
56 typedef struct ProfDataWriter {
57   WriterCallback Write;
58   void *WriterCtx;
59 } ProfDataWriter;
60
61 /*!
62  * The data structure for buffered IO of profile data.
63  */
64 typedef struct ProfBufferIO {
65   ProfDataWriter *FileWriter;
66   uint32_t OwnFileWriter;
67   /* The start of the buffer. */
68   uint8_t *BufferStart;
69   /* Total size of the buffer. */
70   uint32_t BufferSz;
71   /* Current byte offset from the start of the buffer. */
72   uint32_t CurOffset;
73 } ProfBufferIO;
74
75 /* The creator interface used by testing.  */
76 ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
77
78 /*!
79  * This is the interface to create a handle for buffered IO.
80  */
81 ProfBufferIO *lprofCreateBufferIO(ProfDataWriter *FileWriter);
82
83 /*!
84  * The interface to destroy the bufferIO handle and reclaim
85  * the memory.
86  */
87 void lprofDeleteBufferIO(ProfBufferIO *BufferIO);
88
89 /*!
90  * This is the interface to write \c Data of \c Size bytes through
91  * \c BufferIO. Returns 0 if successful, otherwise return -1.
92  */
93 int lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,
94                        uint32_t Size);
95 /*!
96  * The interface to flush the remaining data in the buffer.
97  * through the low level writer callback.
98  */
99 int lprofBufferIOFlush(ProfBufferIO *BufferIO);
100
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,
105                            uint32_t NumIOVecs);
106 void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer);
107
108 struct ValueProfData;
109 struct ValueProfRecord;
110 struct InstrProfValueData;
111 struct ValueProfNode;
112
113 /*!
114  * The class that defines a set of methods to read value profile
115  * data for streaming/serialization from the instrumentation runtime.
116  */
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);
129   /*! 
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.
135    */
136   struct ValueProfNode *(*GetValueData)(uint32_t ValueKind, uint32_t Site,
137                                         struct InstrProfValueData *Dst,
138                                         struct ValueProfNode *StartNode,
139                                         uint32_t N);
140 } VPDataReaderType;
141
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);
153
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);
158
159 VPDataReaderType *lprofGetVPDataReader();
160
161 /* Internal interface used by test to reset the max number of 
162  * tracked values per value site to be \p MaxVals.
163  */
164 void lprofSetMaxValsPerSite(uint32_t MaxVals);
165 void lprofSetupValueProfiler();
166
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();
173
174 /* 
175  * Return non zero value if the profile data has already been
176  * dumped to the file.
177  */
178 unsigned lprofProfileDumped();
179 void lprofSetProfileDumped();
180
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 *);
189
190 #endif