xref: /aosp_15_r20/external/compiler-rt/lib/profile/InstrProfilingWriter.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /*===- InstrProfilingWriter.c - Write instrumentation to a file or buffer -===*\
2*7c3d14c8STreehugger Robot |*
3*7c3d14c8STreehugger Robot |*                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot |*
5*7c3d14c8STreehugger Robot |* This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot |* License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot |*
8*7c3d14c8STreehugger Robot \*===----------------------------------------------------------------------===*/
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot #include "InstrProfiling.h"
11*7c3d14c8STreehugger Robot #include "InstrProfilingInternal.h"
12*7c3d14c8STreehugger Robot #ifdef _MSC_VER
13*7c3d14c8STreehugger Robot /* For _alloca */
14*7c3d14c8STreehugger Robot #include <malloc.h>
15*7c3d14c8STreehugger Robot #endif
16*7c3d14c8STreehugger Robot #include <string.h>
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot #define INSTR_PROF_VALUE_PROF_DATA
19*7c3d14c8STreehugger Robot #include "InstrProfData.inc"
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
22*7c3d14c8STreehugger Robot static ProfBufferIO TheBufferIO;
23*7c3d14c8STreehugger Robot #define VP_BUFFER_SIZE 8 * 1024
24*7c3d14c8STreehugger Robot static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];
25*7c3d14c8STreehugger Robot static InstrProfValueData VPDataArray[16];
26*7c3d14c8STreehugger Robot static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray);
27*7c3d14c8STreehugger Robot 
28*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;
29*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot /* The buffer writer is reponsponsible in keeping writer state
32*7c3d14c8STreehugger Robot  * across the call.
33*7c3d14c8STreehugger Robot  */
lprofBufferWriter(ProfDataIOVec * IOVecs,uint32_t NumIOVecs,void ** WriterCtx)34*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs,
35*7c3d14c8STreehugger Robot                                                   uint32_t NumIOVecs,
36*7c3d14c8STreehugger Robot                                                   void **WriterCtx) {
37*7c3d14c8STreehugger Robot   uint32_t I;
38*7c3d14c8STreehugger Robot   char **Buffer = (char **)WriterCtx;
39*7c3d14c8STreehugger Robot   for (I = 0; I < NumIOVecs; I++) {
40*7c3d14c8STreehugger Robot     size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
41*7c3d14c8STreehugger Robot     memcpy(*Buffer, IOVecs[I].Data, Length);
42*7c3d14c8STreehugger Robot     *Buffer += Length;
43*7c3d14c8STreehugger Robot   }
44*7c3d14c8STreehugger Robot   return 0;
45*7c3d14c8STreehugger Robot }
46*7c3d14c8STreehugger Robot 
llvmInitBufferIO(ProfBufferIO * BufferIO,WriterCallback FileWriter,void * File,uint8_t * Buffer,uint32_t BufferSz)47*7c3d14c8STreehugger Robot static void llvmInitBufferIO(ProfBufferIO *BufferIO, WriterCallback FileWriter,
48*7c3d14c8STreehugger Robot                              void *File, uint8_t *Buffer, uint32_t BufferSz) {
49*7c3d14c8STreehugger Robot   BufferIO->File = File;
50*7c3d14c8STreehugger Robot   BufferIO->FileWriter = FileWriter;
51*7c3d14c8STreehugger Robot   BufferIO->BufferStart = Buffer;
52*7c3d14c8STreehugger Robot   BufferIO->BufferSz = BufferSz;
53*7c3d14c8STreehugger Robot   BufferIO->CurOffset = 0;
54*7c3d14c8STreehugger Robot }
55*7c3d14c8STreehugger Robot 
56*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY ProfBufferIO *
lprofCreateBufferIO(WriterCallback FileWriter,void * File)57*7c3d14c8STreehugger Robot lprofCreateBufferIO(WriterCallback FileWriter, void *File) {
58*7c3d14c8STreehugger Robot   uint8_t *Buffer = DynamicBufferIOBuffer;
59*7c3d14c8STreehugger Robot   uint32_t BufferSize = VPBufferSize;
60*7c3d14c8STreehugger Robot   if (!Buffer) {
61*7c3d14c8STreehugger Robot     Buffer = &BufferIOBuffer[0];
62*7c3d14c8STreehugger Robot     BufferSize = sizeof(BufferIOBuffer);
63*7c3d14c8STreehugger Robot   }
64*7c3d14c8STreehugger Robot   llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize);
65*7c3d14c8STreehugger Robot   return &TheBufferIO;
66*7c3d14c8STreehugger Robot }
67*7c3d14c8STreehugger Robot 
lprofDeleteBufferIO(ProfBufferIO * BufferIO)68*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
69*7c3d14c8STreehugger Robot   if (DynamicBufferIOBuffer) {
70*7c3d14c8STreehugger Robot     FreeHook(DynamicBufferIOBuffer);
71*7c3d14c8STreehugger Robot     DynamicBufferIOBuffer = 0;
72*7c3d14c8STreehugger Robot     VPBufferSize = 0;
73*7c3d14c8STreehugger Robot   }
74*7c3d14c8STreehugger Robot }
75*7c3d14c8STreehugger Robot 
76*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY int
lprofBufferIOWrite(ProfBufferIO * BufferIO,const uint8_t * Data,uint32_t Size)77*7c3d14c8STreehugger Robot lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
78*7c3d14c8STreehugger Robot   /* Buffer is not large enough, it is time to flush.  */
79*7c3d14c8STreehugger Robot   if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
80*7c3d14c8STreehugger Robot     if (lprofBufferIOFlush(BufferIO) != 0)
81*7c3d14c8STreehugger Robot       return -1;
82*7c3d14c8STreehugger Robot   }
83*7c3d14c8STreehugger Robot   /* Special case, bypass the buffer completely. */
84*7c3d14c8STreehugger Robot   ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}};
85*7c3d14c8STreehugger Robot   if (Size > BufferIO->BufferSz) {
86*7c3d14c8STreehugger Robot     if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
87*7c3d14c8STreehugger Robot       return -1;
88*7c3d14c8STreehugger Robot   } else {
89*7c3d14c8STreehugger Robot     /* Write the data to buffer */
90*7c3d14c8STreehugger Robot     uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
91*7c3d14c8STreehugger Robot     lprofBufferWriter(IO, 1, (void **)&Buffer);
92*7c3d14c8STreehugger Robot     BufferIO->CurOffset = Buffer - BufferIO->BufferStart;
93*7c3d14c8STreehugger Robot   }
94*7c3d14c8STreehugger Robot   return 0;
95*7c3d14c8STreehugger Robot }
96*7c3d14c8STreehugger Robot 
lprofBufferIOFlush(ProfBufferIO * BufferIO)97*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) {
98*7c3d14c8STreehugger Robot   if (BufferIO->CurOffset) {
99*7c3d14c8STreehugger Robot     ProfDataIOVec IO[] = {
100*7c3d14c8STreehugger Robot         {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}};
101*7c3d14c8STreehugger Robot     if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
102*7c3d14c8STreehugger Robot       return -1;
103*7c3d14c8STreehugger Robot     BufferIO->CurOffset = 0;
104*7c3d14c8STreehugger Robot   }
105*7c3d14c8STreehugger Robot   return 0;
106*7c3d14c8STreehugger Robot }
107*7c3d14c8STreehugger Robot 
108*7c3d14c8STreehugger Robot /* Write out value profile data for function specified with \c Data.
109*7c3d14c8STreehugger Robot  * The implementation does not use the method \c serializeValueProfData
110*7c3d14c8STreehugger Robot  * which depends on dynamic memory allocation. In this implementation,
111*7c3d14c8STreehugger Robot  * value profile data is written out to \c BufferIO piecemeal.
112*7c3d14c8STreehugger Robot  */
writeOneValueProfData(ProfBufferIO * BufferIO,VPDataReaderType * VPDataReader,const __llvm_profile_data * Data)113*7c3d14c8STreehugger Robot static int writeOneValueProfData(ProfBufferIO *BufferIO,
114*7c3d14c8STreehugger Robot                                  VPDataReaderType *VPDataReader,
115*7c3d14c8STreehugger Robot                                  const __llvm_profile_data *Data) {
116*7c3d14c8STreehugger Robot   unsigned I, NumValueKinds = 0;
117*7c3d14c8STreehugger Robot   ValueProfData VPHeader;
118*7c3d14c8STreehugger Robot   uint8_t *SiteCountArray[IPVK_Last + 1];
119*7c3d14c8STreehugger Robot 
120*7c3d14c8STreehugger Robot   for (I = 0; I <= IPVK_Last; I++) {
121*7c3d14c8STreehugger Robot     if (!Data->NumValueSites[I])
122*7c3d14c8STreehugger Robot       SiteCountArray[I] = 0;
123*7c3d14c8STreehugger Robot     else {
124*7c3d14c8STreehugger Robot       uint32_t Sz =
125*7c3d14c8STreehugger Robot           VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
126*7c3d14c8STreehugger Robot           offsetof(ValueProfRecord, SiteCountArray);
127*7c3d14c8STreehugger Robot       /* Only use alloca for this small byte array to avoid excessive
128*7c3d14c8STreehugger Robot        * stack growth.  */
129*7c3d14c8STreehugger Robot       SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz);
130*7c3d14c8STreehugger Robot       memset(SiteCountArray[I], 0, Sz);
131*7c3d14c8STreehugger Robot     }
132*7c3d14c8STreehugger Robot   }
133*7c3d14c8STreehugger Robot 
134*7c3d14c8STreehugger Robot   /* If NumValueKinds returned is 0, there is nothing to write, report
135*7c3d14c8STreehugger Robot      success and return. This should match the raw profile reader's behavior. */
136*7c3d14c8STreehugger Robot   if (!(NumValueKinds = VPDataReader->InitRTRecord(Data, SiteCountArray)))
137*7c3d14c8STreehugger Robot     return 0;
138*7c3d14c8STreehugger Robot 
139*7c3d14c8STreehugger Robot   /* First write the header structure. */
140*7c3d14c8STreehugger Robot   VPHeader.TotalSize = VPDataReader->GetValueProfDataSize();
141*7c3d14c8STreehugger Robot   VPHeader.NumValueKinds = NumValueKinds;
142*7c3d14c8STreehugger Robot   if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPHeader,
143*7c3d14c8STreehugger Robot                          sizeof(ValueProfData)))
144*7c3d14c8STreehugger Robot     return -1;
145*7c3d14c8STreehugger Robot 
146*7c3d14c8STreehugger Robot   /* Make sure nothing else needs to be written before value profile
147*7c3d14c8STreehugger Robot    * records. */
148*7c3d14c8STreehugger Robot   if ((void *)VPDataReader->GetFirstValueProfRecord(&VPHeader) !=
149*7c3d14c8STreehugger Robot       (void *)(&VPHeader + 1))
150*7c3d14c8STreehugger Robot     return -1;
151*7c3d14c8STreehugger Robot 
152*7c3d14c8STreehugger Robot   /* Write out the value profile record for each value kind
153*7c3d14c8STreehugger Robot    * one by one. */
154*7c3d14c8STreehugger Robot   for (I = 0; I <= IPVK_Last; I++) {
155*7c3d14c8STreehugger Robot     uint32_t J;
156*7c3d14c8STreehugger Robot     ValueProfRecord RecordHeader;
157*7c3d14c8STreehugger Robot     /* The size of the value prof record header without counting the
158*7c3d14c8STreehugger Robot      * site count array .*/
159*7c3d14c8STreehugger Robot     uint32_t RecordHeaderSize = offsetof(ValueProfRecord, SiteCountArray);
160*7c3d14c8STreehugger Robot     uint32_t SiteCountArraySize;
161*7c3d14c8STreehugger Robot 
162*7c3d14c8STreehugger Robot     if (!Data->NumValueSites[I])
163*7c3d14c8STreehugger Robot       continue;
164*7c3d14c8STreehugger Robot 
165*7c3d14c8STreehugger Robot     /* Write out the record header.  */
166*7c3d14c8STreehugger Robot     RecordHeader.Kind = I;
167*7c3d14c8STreehugger Robot     RecordHeader.NumValueSites = Data->NumValueSites[I];
168*7c3d14c8STreehugger Robot     if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&RecordHeader,
169*7c3d14c8STreehugger Robot                            RecordHeaderSize))
170*7c3d14c8STreehugger Robot       return -1;
171*7c3d14c8STreehugger Robot 
172*7c3d14c8STreehugger Robot     /* Write out the site value count array including padding space. */
173*7c3d14c8STreehugger Robot     SiteCountArraySize =
174*7c3d14c8STreehugger Robot         VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
175*7c3d14c8STreehugger Robot         RecordHeaderSize;
176*7c3d14c8STreehugger Robot     if (lprofBufferIOWrite(BufferIO, SiteCountArray[I], SiteCountArraySize))
177*7c3d14c8STreehugger Robot       return -1;
178*7c3d14c8STreehugger Robot 
179*7c3d14c8STreehugger Robot     /* Write out the value profile data for each value site.  */
180*7c3d14c8STreehugger Robot     for (J = 0; J < Data->NumValueSites[I]; J++) {
181*7c3d14c8STreehugger Robot       uint32_t NRead, NRemain;
182*7c3d14c8STreehugger Robot       ValueProfNode *NextStartNode = 0;
183*7c3d14c8STreehugger Robot       NRemain = VPDataReader->GetNumValueDataForSite(I, J);
184*7c3d14c8STreehugger Robot       if (!NRemain)
185*7c3d14c8STreehugger Robot         continue;
186*7c3d14c8STreehugger Robot       /* Read and write out value data in small chunks till it is done. */
187*7c3d14c8STreehugger Robot       do {
188*7c3d14c8STreehugger Robot         NRead = (NRemain > VPDataArraySize ? VPDataArraySize : NRemain);
189*7c3d14c8STreehugger Robot         NextStartNode =
190*7c3d14c8STreehugger Robot             VPDataReader->GetValueData(I, /* ValueKind */
191*7c3d14c8STreehugger Robot                                        J, /* Site */
192*7c3d14c8STreehugger Robot                                        &VPDataArray[0], NextStartNode, NRead);
193*7c3d14c8STreehugger Robot         if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPDataArray[0],
194*7c3d14c8STreehugger Robot                                NRead * sizeof(InstrProfValueData)))
195*7c3d14c8STreehugger Robot           return -1;
196*7c3d14c8STreehugger Robot         NRemain -= NRead;
197*7c3d14c8STreehugger Robot       } while (NRemain != 0);
198*7c3d14c8STreehugger Robot     }
199*7c3d14c8STreehugger Robot   }
200*7c3d14c8STreehugger Robot   /* All done report success.  */
201*7c3d14c8STreehugger Robot   return 0;
202*7c3d14c8STreehugger Robot }
203*7c3d14c8STreehugger Robot 
writeValueProfData(WriterCallback Writer,void * WriterCtx,VPDataReaderType * VPDataReader,const __llvm_profile_data * DataBegin,const __llvm_profile_data * DataEnd)204*7c3d14c8STreehugger Robot static int writeValueProfData(WriterCallback Writer, void *WriterCtx,
205*7c3d14c8STreehugger Robot                               VPDataReaderType *VPDataReader,
206*7c3d14c8STreehugger Robot                               const __llvm_profile_data *DataBegin,
207*7c3d14c8STreehugger Robot                               const __llvm_profile_data *DataEnd) {
208*7c3d14c8STreehugger Robot   ProfBufferIO *BufferIO;
209*7c3d14c8STreehugger Robot   const __llvm_profile_data *DI = 0;
210*7c3d14c8STreehugger Robot 
211*7c3d14c8STreehugger Robot   if (!VPDataReader)
212*7c3d14c8STreehugger Robot     return 0;
213*7c3d14c8STreehugger Robot 
214*7c3d14c8STreehugger Robot   BufferIO = lprofCreateBufferIO(Writer, WriterCtx);
215*7c3d14c8STreehugger Robot 
216*7c3d14c8STreehugger Robot   for (DI = DataBegin; DI < DataEnd; DI++) {
217*7c3d14c8STreehugger Robot     if (writeOneValueProfData(BufferIO, VPDataReader, DI))
218*7c3d14c8STreehugger Robot       return -1;
219*7c3d14c8STreehugger Robot   }
220*7c3d14c8STreehugger Robot 
221*7c3d14c8STreehugger Robot   if (lprofBufferIOFlush(BufferIO) != 0)
222*7c3d14c8STreehugger Robot     return -1;
223*7c3d14c8STreehugger Robot   lprofDeleteBufferIO(BufferIO);
224*7c3d14c8STreehugger Robot 
225*7c3d14c8STreehugger Robot   return 0;
226*7c3d14c8STreehugger Robot }
227*7c3d14c8STreehugger Robot 
lprofWriteData(WriterCallback Writer,void * WriterCtx,VPDataReaderType * VPDataReader)228*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer,
229*7c3d14c8STreehugger Robot                                           void *WriterCtx,
230*7c3d14c8STreehugger Robot                                           VPDataReaderType *VPDataReader) {
231*7c3d14c8STreehugger Robot   /* Match logic in __llvm_profile_write_buffer(). */
232*7c3d14c8STreehugger Robot   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
233*7c3d14c8STreehugger Robot   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
234*7c3d14c8STreehugger Robot   const uint64_t *CountersBegin = __llvm_profile_begin_counters();
235*7c3d14c8STreehugger Robot   const uint64_t *CountersEnd = __llvm_profile_end_counters();
236*7c3d14c8STreehugger Robot   const char *NamesBegin = __llvm_profile_begin_names();
237*7c3d14c8STreehugger Robot   const char *NamesEnd = __llvm_profile_end_names();
238*7c3d14c8STreehugger Robot   return lprofWriteDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
239*7c3d14c8STreehugger Robot                             CountersBegin, CountersEnd, VPDataReader,
240*7c3d14c8STreehugger Robot                             NamesBegin, NamesEnd);
241*7c3d14c8STreehugger Robot }
242*7c3d14c8STreehugger Robot 
243*7c3d14c8STreehugger Robot COMPILER_RT_VISIBILITY int
lprofWriteDataImpl(WriterCallback Writer,void * WriterCtx,const __llvm_profile_data * DataBegin,const __llvm_profile_data * DataEnd,const uint64_t * CountersBegin,const uint64_t * CountersEnd,VPDataReaderType * VPDataReader,const char * NamesBegin,const char * NamesEnd)244*7c3d14c8STreehugger Robot lprofWriteDataImpl(WriterCallback Writer, void *WriterCtx,
245*7c3d14c8STreehugger Robot                    const __llvm_profile_data *DataBegin,
246*7c3d14c8STreehugger Robot                    const __llvm_profile_data *DataEnd,
247*7c3d14c8STreehugger Robot                    const uint64_t *CountersBegin, const uint64_t *CountersEnd,
248*7c3d14c8STreehugger Robot                    VPDataReaderType *VPDataReader, const char *NamesBegin,
249*7c3d14c8STreehugger Robot                    const char *NamesEnd) {
250*7c3d14c8STreehugger Robot 
251*7c3d14c8STreehugger Robot   /* Calculate size of sections. */
252*7c3d14c8STreehugger Robot   const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
253*7c3d14c8STreehugger Robot   const uint64_t CountersSize = CountersEnd - CountersBegin;
254*7c3d14c8STreehugger Robot   const uint64_t NamesSize = NamesEnd - NamesBegin;
255*7c3d14c8STreehugger Robot   const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
256*7c3d14c8STreehugger Robot 
257*7c3d14c8STreehugger Robot   /* Enough zeroes for padding. */
258*7c3d14c8STreehugger Robot   const char Zeroes[sizeof(uint64_t)] = {0};
259*7c3d14c8STreehugger Robot 
260*7c3d14c8STreehugger Robot   /* Create the header. */
261*7c3d14c8STreehugger Robot   __llvm_profile_header Header;
262*7c3d14c8STreehugger Robot 
263*7c3d14c8STreehugger Robot   if (!DataSize)
264*7c3d14c8STreehugger Robot     return 0;
265*7c3d14c8STreehugger Robot 
266*7c3d14c8STreehugger Robot /* Initialize header structure.  */
267*7c3d14c8STreehugger Robot #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
268*7c3d14c8STreehugger Robot #include "InstrProfData.inc"
269*7c3d14c8STreehugger Robot 
270*7c3d14c8STreehugger Robot   /* Write the data. */
271*7c3d14c8STreehugger Robot   ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1},
272*7c3d14c8STreehugger Robot                            {DataBegin, sizeof(__llvm_profile_data), DataSize},
273*7c3d14c8STreehugger Robot                            {CountersBegin, sizeof(uint64_t), CountersSize},
274*7c3d14c8STreehugger Robot                            {NamesBegin, sizeof(uint8_t), NamesSize},
275*7c3d14c8STreehugger Robot                            {Zeroes, sizeof(uint8_t), Padding}};
276*7c3d14c8STreehugger Robot   if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx))
277*7c3d14c8STreehugger Robot     return -1;
278*7c3d14c8STreehugger Robot 
279*7c3d14c8STreehugger Robot   return writeValueProfData(Writer, WriterCtx, VPDataReader, DataBegin,
280*7c3d14c8STreehugger Robot                             DataEnd);
281*7c3d14c8STreehugger Robot }
282