blob: db944e20f612fd887b2c9bd8a418b69df83b4af1 [file] [log] [blame]
Xinliang David Li1d8d46a2015-11-18 21:08:03 +00001/*===- InstrProfilingWriter.c - Write instrumentation to a file or buffer -===*\
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#include "InstrProfiling.h"
11#include "InstrProfilingInternal.h"
Xinliang David Libaf55d82015-12-22 18:57:15 +000012#include <string.h>
13
Xinliang David Li54dd6832015-12-29 07:13:59 +000014#define INSTR_PROF_VALUE_PROF_DATA
15#include "InstrProfData.inc"
Xinliang David Li609fae32016-05-13 18:26:26 +000016
Xinliang David Lib5a2b3a2016-05-09 19:01:19 +000017COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
Xinliang David Li609fae32016-05-13 18:26:26 +000018static ProfBufferIO TheBufferIO;
19#define VP_BUFFER_SIZE 8 * 1024
20static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];
21COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;
22COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +000023
Xinliang David Libaf55d82015-12-22 18:57:15 +000024/* The buffer writer is reponsponsible in keeping writer state
25 * across the call.
26 */
Xinliang David Licf1a8d62016-03-06 04:18:13 +000027COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs,
28 uint32_t NumIOVecs,
29 void **WriterCtx) {
Xinliang David Libaf55d82015-12-22 18:57:15 +000030 uint32_t I;
31 char **Buffer = (char **)WriterCtx;
32 for (I = 0; I < NumIOVecs; I++) {
33 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
34 memcpy(*Buffer, IOVecs[I].Data, Length);
35 *Buffer += Length;
36 }
37 return 0;
38}
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000039
Xinliang David Licda3bc22015-12-29 23:54:41 +000040static void llvmInitBufferIO(ProfBufferIO *BufferIO, WriterCallback FileWriter,
41 void *File, uint8_t *Buffer, uint32_t BufferSz) {
42 BufferIO->File = File;
43 BufferIO->FileWriter = FileWriter;
44 BufferIO->BufferStart = Buffer;
45 BufferIO->BufferSz = BufferSz;
46 BufferIO->CurOffset = 0;
47}
48
49COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Li609fae32016-05-13 18:26:26 +000050lprofCreateBufferIO(WriterCallback FileWriter, void *File) {
51 uint8_t *Buffer = DynamicBufferIOBuffer;
52 uint32_t BufferSize = VPBufferSize;
Xinliang David Licda3bc22015-12-29 23:54:41 +000053 if (!Buffer) {
Xinliang David Li609fae32016-05-13 18:26:26 +000054 Buffer = &BufferIOBuffer[0];
55 BufferSize = sizeof(BufferIOBuffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000056 }
Xinliang David Li609fae32016-05-13 18:26:26 +000057 llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize);
58 return &TheBufferIO;
Xinliang David Licda3bc22015-12-29 23:54:41 +000059}
60
Xinliang David Licf1a8d62016-03-06 04:18:13 +000061COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
Xinliang David Li609fae32016-05-13 18:26:26 +000062 if (DynamicBufferIOBuffer)
63 FreeHook(DynamicBufferIOBuffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000064}
65
66COMPILER_RT_VISIBILITY int
Xinliang David Licf1a8d62016-03-06 04:18:13 +000067lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000068 /* Buffer is not large enough, it is time to flush. */
69 if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
Xinliang David Licf1a8d62016-03-06 04:18:13 +000070 if (lprofBufferIOFlush(BufferIO) != 0)
71 return -1;
Xinliang David Licda3bc22015-12-29 23:54:41 +000072 }
73 /* Special case, bypass the buffer completely. */
74 ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}};
75 if (Size > BufferIO->BufferSz) {
76 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
77 return -1;
78 } else {
79 /* Write the data to buffer */
80 uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
Xinliang David Licf1a8d62016-03-06 04:18:13 +000081 lprofBufferWriter(IO, 1, (void **)&Buffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000082 BufferIO->CurOffset = Buffer - BufferIO->BufferStart;
83 }
84 return 0;
85}
86
Xinliang David Licf1a8d62016-03-06 04:18:13 +000087COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000088 if (BufferIO->CurOffset) {
89 ProfDataIOVec IO[] = {
90 {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}};
91 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
92 return -1;
93 BufferIO->CurOffset = 0;
94 }
95 return 0;
96}
97
Xinliang David Li911af1c2016-05-12 21:18:41 +000098static int writeOneValueProfData(ProfBufferIO *BufferIO,
99 VPGatherHookType VPDataGatherer,
100 const __llvm_profile_data *Data) {
101 ValueProfData *CurVData = VPDataGatherer(Data);
102 if (!CurVData)
103 return 0;
104 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)CurVData,
105 CurVData->TotalSize) != 0)
106 return -1;
107 FreeHook(CurVData);
108 return 0;
109}
110
Xinliang David Li54dd6832015-12-29 07:13:59 +0000111static int writeValueProfData(WriterCallback Writer, void *WriterCtx,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000112 VPGatherHookType VPDataGatherer,
113 const __llvm_profile_data *DataBegin,
114 const __llvm_profile_data *DataEnd) {
Xinliang David Licda3bc22015-12-29 23:54:41 +0000115 ProfBufferIO *BufferIO;
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000116 const __llvm_profile_data *DI = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000117
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000118 if (!VPDataGatherer)
Xinliang David Li54dd6832015-12-29 07:13:59 +0000119 return 0;
120
Xinliang David Li609fae32016-05-13 18:26:26 +0000121 BufferIO = lprofCreateBufferIO(Writer, WriterCtx);
Xinliang David Li54dd6832015-12-29 07:13:59 +0000122
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000123 for (DI = DataBegin; DI < DataEnd; DI++) {
Xinliang David Li911af1c2016-05-12 21:18:41 +0000124 if (writeOneValueProfData(BufferIO, VPDataGatherer, DI))
Xinliang David Li54dd6832015-12-29 07:13:59 +0000125 return -1;
126 }
127
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000128 if (lprofBufferIOFlush(BufferIO) != 0)
Xinliang David Licda3bc22015-12-29 23:54:41 +0000129 return -1;
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000130 lprofDeleteBufferIO(BufferIO);
Xinliang David Licda3bc22015-12-29 23:54:41 +0000131
Xinliang David Li54dd6832015-12-29 07:13:59 +0000132 return 0;
133}
134
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000135COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer,
136 void *WriterCtx,
137 VPGatherHookType VPDataGatherer) {
138 /* Match logic in __llvm_profile_write_buffer(). */
139 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
140 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
141 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
142 const uint64_t *CountersEnd = __llvm_profile_end_counters();
143 const char *NamesBegin = __llvm_profile_begin_names();
144 const char *NamesEnd = __llvm_profile_end_names();
145 return lprofWriteDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
146 CountersBegin, CountersEnd, VPDataGatherer,
147 NamesBegin, NamesEnd);
148}
149
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000150COMPILER_RT_VISIBILITY int
151lprofWriteDataImpl(WriterCallback Writer, void *WriterCtx,
152 const __llvm_profile_data *DataBegin,
153 const __llvm_profile_data *DataEnd,
154 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000155 VPGatherHookType VPDataGatherer, const char *NamesBegin,
156 const char *NamesEnd) {
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000157
158 /* Calculate size of sections. */
Vedant Kumarb8502512016-02-26 02:49:41 +0000159 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000160 const uint64_t CountersSize = CountersEnd - CountersBegin;
161 const uint64_t NamesSize = NamesEnd - NamesBegin;
162 const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
163
164 /* Enough zeroes for padding. */
165 const char Zeroes[sizeof(uint64_t)] = {0};
166
167 /* Create the header. */
168 __llvm_profile_header Header;
169
170 if (!DataSize)
171 return 0;
172
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000173/* Initialize header structure. */
Xinliang David Li6376db52015-11-23 18:36:40 +0000174#define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
175#include "InstrProfData.inc"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000176
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000177 /* Write the data. */
Xinliang David Li54dd6832015-12-29 07:13:59 +0000178 ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1},
179 {DataBegin, sizeof(__llvm_profile_data), DataSize},
180 {CountersBegin, sizeof(uint64_t), CountersSize},
181 {NamesBegin, sizeof(uint8_t), NamesSize},
182 {Zeroes, sizeof(uint8_t), Padding}};
Joerg Sonnenbergerd6487b22015-12-04 00:40:07 +0000183 if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx))
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000184 return -1;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000185
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000186 return writeValueProfData(Writer, WriterCtx, VPDataGatherer, DataBegin,
187 DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000188}