blob: 3bfcc6be02716f2a18d125c11a9e4ff0b0934c20 [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 Lib5a2b3a2016-05-09 19:01:19 +000016COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
17COMPILER_RT_VISIBILITY void *(*CallocHook)(size_t, size_t) = NULL;
Xinliang David Li54dd6832015-12-29 07:13:59 +000018uint32_t VPBufferSize = 0;
19
Xinliang David Libaf55d82015-12-22 18:57:15 +000020/* The buffer writer is reponsponsible in keeping writer state
21 * across the call.
22 */
Xinliang David Licf1a8d62016-03-06 04:18:13 +000023COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs,
24 uint32_t NumIOVecs,
25 void **WriterCtx) {
Xinliang David Libaf55d82015-12-22 18:57:15 +000026 uint32_t I;
27 char **Buffer = (char **)WriterCtx;
28 for (I = 0; I < NumIOVecs; I++) {
29 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
30 memcpy(*Buffer, IOVecs[I].Data, Length);
31 *Buffer += Length;
32 }
33 return 0;
34}
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000035
Xinliang David Licda3bc22015-12-29 23:54:41 +000036static void llvmInitBufferIO(ProfBufferIO *BufferIO, WriterCallback FileWriter,
37 void *File, uint8_t *Buffer, uint32_t BufferSz) {
38 BufferIO->File = File;
39 BufferIO->FileWriter = FileWriter;
40 BufferIO->BufferStart = Buffer;
41 BufferIO->BufferSz = BufferSz;
42 BufferIO->CurOffset = 0;
43}
44
45COMPILER_RT_VISIBILITY ProfBufferIO *
Xinliang David Licf1a8d62016-03-06 04:18:13 +000046lprofCreateBufferIO(WriterCallback FileWriter, void *File, uint32_t BufferSz) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000047 ProfBufferIO *BufferIO = (ProfBufferIO *)CallocHook(1, sizeof(ProfBufferIO));
48 uint8_t *Buffer = (uint8_t *)CallocHook(1, BufferSz);
49 if (!Buffer) {
50 FreeHook(BufferIO);
51 return 0;
52 }
53 llvmInitBufferIO(BufferIO, FileWriter, File, Buffer, BufferSz);
54 return BufferIO;
55}
56
Xinliang David Licf1a8d62016-03-06 04:18:13 +000057COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000058 FreeHook(BufferIO->BufferStart);
59 FreeHook(BufferIO);
60}
61
62COMPILER_RT_VISIBILITY int
Xinliang David Licf1a8d62016-03-06 04:18:13 +000063lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000064 /* Buffer is not large enough, it is time to flush. */
65 if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
Xinliang David Licf1a8d62016-03-06 04:18:13 +000066 if (lprofBufferIOFlush(BufferIO) != 0)
67 return -1;
Xinliang David Licda3bc22015-12-29 23:54:41 +000068 }
69 /* Special case, bypass the buffer completely. */
70 ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}};
71 if (Size > BufferIO->BufferSz) {
72 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
73 return -1;
74 } else {
75 /* Write the data to buffer */
76 uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
Xinliang David Licf1a8d62016-03-06 04:18:13 +000077 lprofBufferWriter(IO, 1, (void **)&Buffer);
Xinliang David Licda3bc22015-12-29 23:54:41 +000078 BufferIO->CurOffset = Buffer - BufferIO->BufferStart;
79 }
80 return 0;
81}
82
Xinliang David Licf1a8d62016-03-06 04:18:13 +000083COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000084 if (BufferIO->CurOffset) {
85 ProfDataIOVec IO[] = {
86 {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}};
87 if (BufferIO->FileWriter(IO, 1, &BufferIO->File))
88 return -1;
89 BufferIO->CurOffset = 0;
90 }
91 return 0;
92}
93
Xinliang David Li54dd6832015-12-29 07:13:59 +000094#define VP_BUFFER_SIZE 8 * 1024
95static int writeValueProfData(WriterCallback Writer, void *WriterCtx,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +000096 VPGatherHookType VPDataGatherer,
97 const __llvm_profile_data *DataBegin,
98 const __llvm_profile_data *DataEnd) {
Xinliang David Licda3bc22015-12-29 23:54:41 +000099 ProfBufferIO *BufferIO;
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000100 uint32_t BufferSz;
101 const __llvm_profile_data *DI = 0;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000102
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000103 if (!VPDataGatherer)
Xinliang David Li54dd6832015-12-29 07:13:59 +0000104 return 0;
105
106 BufferSz = VPBufferSize ? VPBufferSize : VP_BUFFER_SIZE;
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000107 BufferIO = lprofCreateBufferIO(Writer, WriterCtx, BufferSz);
Xinliang David Li54dd6832015-12-29 07:13:59 +0000108
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000109 for (DI = DataBegin; DI < DataEnd; DI++) {
110 ValueProfData *CurVData = VPDataGatherer(DI);
Xinliang David Licda3bc22015-12-29 23:54:41 +0000111 if (!CurVData)
Xinliang David Li54dd6832015-12-29 07:13:59 +0000112 continue;
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000113 if (lprofBufferIOWrite(BufferIO, (const uint8_t *)CurVData,
114 CurVData->TotalSize) != 0)
Xinliang David Li54dd6832015-12-29 07:13:59 +0000115 return -1;
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000116 FreeHook(CurVData);
Xinliang David Li54dd6832015-12-29 07:13:59 +0000117 }
118
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000119 if (lprofBufferIOFlush(BufferIO) != 0)
Xinliang David Licda3bc22015-12-29 23:54:41 +0000120 return -1;
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000121 lprofDeleteBufferIO(BufferIO);
Xinliang David Licda3bc22015-12-29 23:54:41 +0000122
Xinliang David Li54dd6832015-12-29 07:13:59 +0000123 return 0;
124}
125
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000126COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer,
127 void *WriterCtx,
128 VPGatherHookType VPDataGatherer) {
129 /* Match logic in __llvm_profile_write_buffer(). */
130 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
131 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
132 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
133 const uint64_t *CountersEnd = __llvm_profile_end_counters();
134 const char *NamesBegin = __llvm_profile_begin_names();
135 const char *NamesEnd = __llvm_profile_end_names();
136 return lprofWriteDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
137 CountersBegin, CountersEnd, VPDataGatherer,
138 NamesBegin, NamesEnd);
139}
140
Xinliang David Licf1a8d62016-03-06 04:18:13 +0000141COMPILER_RT_VISIBILITY int
142lprofWriteDataImpl(WriterCallback Writer, void *WriterCtx,
143 const __llvm_profile_data *DataBegin,
144 const __llvm_profile_data *DataEnd,
145 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000146 VPGatherHookType VPDataGatherer, const char *NamesBegin,
147 const char *NamesEnd) {
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000148
149 /* Calculate size of sections. */
Vedant Kumarb8502512016-02-26 02:49:41 +0000150 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000151 const uint64_t CountersSize = CountersEnd - CountersBegin;
152 const uint64_t NamesSize = NamesEnd - NamesBegin;
153 const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
154
155 /* Enough zeroes for padding. */
156 const char Zeroes[sizeof(uint64_t)] = {0};
157
158 /* Create the header. */
159 __llvm_profile_header Header;
160
161 if (!DataSize)
162 return 0;
163
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000164/* Initialize header structure. */
Xinliang David Li6376db52015-11-23 18:36:40 +0000165#define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
166#include "InstrProfData.inc"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000167
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000168 /* Write the data. */
Xinliang David Li54dd6832015-12-29 07:13:59 +0000169 ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1},
170 {DataBegin, sizeof(__llvm_profile_data), DataSize},
171 {CountersBegin, sizeof(uint64_t), CountersSize},
172 {NamesBegin, sizeof(uint8_t), NamesSize},
173 {Zeroes, sizeof(uint8_t), Padding}};
Joerg Sonnenbergerd6487b22015-12-04 00:40:07 +0000174 if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx))
Xinliang David Li2d5bf072015-11-21 04:16:42 +0000175 return -1;
Xinliang David Li54dd6832015-12-29 07:13:59 +0000176
Xinliang David Lie9a8574d2016-05-10 00:17:31 +0000177 return writeValueProfData(Writer, WriterCtx, VPDataGatherer, DataBegin,
178 DataEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +0000179}