Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 1 | /*===- 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 Li | baf55d8 | 2015-12-22 18:57:15 +0000 | [diff] [blame] | 12 | #include <string.h> |
| 13 | |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 14 | #define INSTR_PROF_VALUE_PROF_DATA |
| 15 | #include "InstrProfData.inc" |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 16 | |
Xinliang David Li | b5a2b3a | 2016-05-09 19:01:19 +0000 | [diff] [blame] | 17 | COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL; |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 18 | static ProfBufferIO TheBufferIO; |
| 19 | #define VP_BUFFER_SIZE 8 * 1024 |
| 20 | static uint8_t BufferIOBuffer[VP_BUFFER_SIZE]; |
| 21 | COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0; |
| 22 | COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0; |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 23 | |
Xinliang David Li | baf55d8 | 2015-12-22 18:57:15 +0000 | [diff] [blame] | 24 | /* The buffer writer is reponsponsible in keeping writer state |
| 25 | * across the call. |
| 26 | */ |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 27 | COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs, |
| 28 | uint32_t NumIOVecs, |
| 29 | void **WriterCtx) { |
Xinliang David Li | baf55d8 | 2015-12-22 18:57:15 +0000 | [diff] [blame] | 30 | 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 Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 39 | |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 40 | static 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 | |
| 49 | COMPILER_RT_VISIBILITY ProfBufferIO * |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 50 | lprofCreateBufferIO(WriterCallback FileWriter, void *File) { |
| 51 | uint8_t *Buffer = DynamicBufferIOBuffer; |
| 52 | uint32_t BufferSize = VPBufferSize; |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 53 | if (!Buffer) { |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 54 | Buffer = &BufferIOBuffer[0]; |
| 55 | BufferSize = sizeof(BufferIOBuffer); |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 56 | } |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 57 | llvmInitBufferIO(&TheBufferIO, FileWriter, File, Buffer, BufferSize); |
| 58 | return &TheBufferIO; |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 61 | COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) { |
Xinliang David Li | a16c754 | 2016-05-14 03:16:47 +0000 | [diff] [blame^] | 62 | if (DynamicBufferIOBuffer) { |
| 63 | DynamicBufferIOBuffer = 0; |
| 64 | VPBufferSize = 0; |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 65 | FreeHook(DynamicBufferIOBuffer); |
Xinliang David Li | a16c754 | 2016-05-14 03:16:47 +0000 | [diff] [blame^] | 66 | } |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | COMPILER_RT_VISIBILITY int |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 70 | lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 71 | /* Buffer is not large enough, it is time to flush. */ |
| 72 | if (Size + BufferIO->CurOffset > BufferIO->BufferSz) { |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 73 | if (lprofBufferIOFlush(BufferIO) != 0) |
| 74 | return -1; |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 75 | } |
| 76 | /* Special case, bypass the buffer completely. */ |
| 77 | ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size}}; |
| 78 | if (Size > BufferIO->BufferSz) { |
| 79 | if (BufferIO->FileWriter(IO, 1, &BufferIO->File)) |
| 80 | return -1; |
| 81 | } else { |
| 82 | /* Write the data to buffer */ |
| 83 | uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset; |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 84 | lprofBufferWriter(IO, 1, (void **)&Buffer); |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 85 | BufferIO->CurOffset = Buffer - BufferIO->BufferStart; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 90 | COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 91 | if (BufferIO->CurOffset) { |
| 92 | ProfDataIOVec IO[] = { |
| 93 | {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset}}; |
| 94 | if (BufferIO->FileWriter(IO, 1, &BufferIO->File)) |
| 95 | return -1; |
| 96 | BufferIO->CurOffset = 0; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
Xinliang David Li | 911af1c | 2016-05-12 21:18:41 +0000 | [diff] [blame] | 101 | static int writeOneValueProfData(ProfBufferIO *BufferIO, |
| 102 | VPGatherHookType VPDataGatherer, |
| 103 | const __llvm_profile_data *Data) { |
| 104 | ValueProfData *CurVData = VPDataGatherer(Data); |
| 105 | if (!CurVData) |
| 106 | return 0; |
| 107 | if (lprofBufferIOWrite(BufferIO, (const uint8_t *)CurVData, |
| 108 | CurVData->TotalSize) != 0) |
| 109 | return -1; |
| 110 | FreeHook(CurVData); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 114 | static int writeValueProfData(WriterCallback Writer, void *WriterCtx, |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 115 | VPGatherHookType VPDataGatherer, |
| 116 | const __llvm_profile_data *DataBegin, |
| 117 | const __llvm_profile_data *DataEnd) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 118 | ProfBufferIO *BufferIO; |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 119 | const __llvm_profile_data *DI = 0; |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 120 | |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 121 | if (!VPDataGatherer) |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 122 | return 0; |
| 123 | |
Xinliang David Li | 609fae3 | 2016-05-13 18:26:26 +0000 | [diff] [blame] | 124 | BufferIO = lprofCreateBufferIO(Writer, WriterCtx); |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 125 | |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 126 | for (DI = DataBegin; DI < DataEnd; DI++) { |
Xinliang David Li | 911af1c | 2016-05-12 21:18:41 +0000 | [diff] [blame] | 127 | if (writeOneValueProfData(BufferIO, VPDataGatherer, DI)) |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 128 | return -1; |
| 129 | } |
| 130 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 131 | if (lprofBufferIOFlush(BufferIO) != 0) |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 132 | return -1; |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 133 | lprofDeleteBufferIO(BufferIO); |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 134 | |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 138 | COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer, |
| 139 | void *WriterCtx, |
| 140 | VPGatherHookType VPDataGatherer) { |
| 141 | /* Match logic in __llvm_profile_write_buffer(). */ |
| 142 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); |
| 143 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); |
| 144 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); |
| 145 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); |
| 146 | const char *NamesBegin = __llvm_profile_begin_names(); |
| 147 | const char *NamesEnd = __llvm_profile_end_names(); |
| 148 | return lprofWriteDataImpl(Writer, WriterCtx, DataBegin, DataEnd, |
| 149 | CountersBegin, CountersEnd, VPDataGatherer, |
| 150 | NamesBegin, NamesEnd); |
| 151 | } |
| 152 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 153 | COMPILER_RT_VISIBILITY int |
| 154 | lprofWriteDataImpl(WriterCallback Writer, void *WriterCtx, |
| 155 | const __llvm_profile_data *DataBegin, |
| 156 | const __llvm_profile_data *DataEnd, |
| 157 | const uint64_t *CountersBegin, const uint64_t *CountersEnd, |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 158 | VPGatherHookType VPDataGatherer, const char *NamesBegin, |
| 159 | const char *NamesEnd) { |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 160 | |
| 161 | /* Calculate size of sections. */ |
Vedant Kumar | b850251 | 2016-02-26 02:49:41 +0000 | [diff] [blame] | 162 | const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 163 | const uint64_t CountersSize = CountersEnd - CountersBegin; |
| 164 | const uint64_t NamesSize = NamesEnd - NamesBegin; |
| 165 | const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize); |
| 166 | |
| 167 | /* Enough zeroes for padding. */ |
| 168 | const char Zeroes[sizeof(uint64_t)] = {0}; |
| 169 | |
| 170 | /* Create the header. */ |
| 171 | __llvm_profile_header Header; |
| 172 | |
| 173 | if (!DataSize) |
| 174 | return 0; |
| 175 | |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 176 | /* Initialize header structure. */ |
Xinliang David Li | 6376db5 | 2015-11-23 18:36:40 +0000 | [diff] [blame] | 177 | #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init; |
| 178 | #include "InstrProfData.inc" |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 179 | |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 180 | /* Write the data. */ |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 181 | ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1}, |
| 182 | {DataBegin, sizeof(__llvm_profile_data), DataSize}, |
| 183 | {CountersBegin, sizeof(uint64_t), CountersSize}, |
| 184 | {NamesBegin, sizeof(uint8_t), NamesSize}, |
| 185 | {Zeroes, sizeof(uint8_t), Padding}}; |
Joerg Sonnenberger | d6487b2 | 2015-12-04 00:40:07 +0000 | [diff] [blame] | 186 | if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx)) |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 187 | return -1; |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 188 | |
Xinliang David Li | e9a8574d | 2016-05-10 00:17:31 +0000 | [diff] [blame] | 189 | return writeValueProfData(Writer, WriterCtx, VPDataGatherer, DataBegin, |
| 190 | DataEnd); |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 191 | } |