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" |
| 16 | void (*FreeHook)(void *) = NULL; |
| 17 | void* (*CallocHook)(size_t, size_t) = NULL; |
| 18 | uint32_t VPBufferSize = 0; |
| 19 | |
Xinliang David Li | baf55d8 | 2015-12-22 18:57:15 +0000 | [diff] [blame] | 20 | /* The buffer writer is reponsponsible in keeping writer state |
| 21 | * across the call. |
| 22 | */ |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 23 | COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataIOVec *IOVecs, |
| 24 | uint32_t NumIOVecs, |
| 25 | void **WriterCtx) { |
Xinliang David Li | baf55d8 | 2015-12-22 18:57:15 +0000 | [diff] [blame] | 26 | 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 Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 35 | |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 36 | static 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 | |
| 45 | COMPILER_RT_VISIBILITY ProfBufferIO * |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 46 | lprofCreateBufferIO(WriterCallback FileWriter, void *File, uint32_t BufferSz) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 47 | 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 Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 57 | COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 58 | FreeHook(BufferIO->BufferStart); |
| 59 | FreeHook(BufferIO); |
| 60 | } |
| 61 | |
| 62 | COMPILER_RT_VISIBILITY int |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 63 | lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 64 | /* Buffer is not large enough, it is time to flush. */ |
| 65 | if (Size + BufferIO->CurOffset > BufferIO->BufferSz) { |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 66 | if (lprofBufferIOFlush(BufferIO) != 0) |
| 67 | return -1; |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 68 | } |
| 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 Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 77 | lprofBufferWriter(IO, 1, (void **)&Buffer); |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 78 | BufferIO->CurOffset = Buffer - BufferIO->BufferStart; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 83 | COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 84 | 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 | |
Renato Golin | 26642b4 | 2016-05-07 20:07:09 +0000 | [diff] [blame] | 94 | COMPILER_RT_VISIBILITY int lprofWriteData(WriterCallback Writer, |
| 95 | void *WriterCtx, |
| 96 | ValueProfData **ValueDataArray, |
| 97 | const uint64_t ValueDataSize) { |
| 98 | /* Match logic in __llvm_profile_write_buffer(). */ |
| 99 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); |
| 100 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); |
| 101 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); |
| 102 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); |
| 103 | const char *NamesBegin = __llvm_profile_begin_names(); |
| 104 | const char *NamesEnd = __llvm_profile_end_names(); |
| 105 | return lprofWriteDataImpl(Writer, WriterCtx, DataBegin, DataEnd, |
| 106 | CountersBegin, CountersEnd, ValueDataArray, |
| 107 | ValueDataSize, NamesBegin, NamesEnd); |
| 108 | } |
| 109 | |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 110 | #define VP_BUFFER_SIZE 8 * 1024 |
| 111 | static int writeValueProfData(WriterCallback Writer, void *WriterCtx, |
Renato Golin | 26642b4 | 2016-05-07 20:07:09 +0000 | [diff] [blame] | 112 | ValueProfData **ValueDataBegin, |
| 113 | uint64_t NumVData) { |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 114 | ProfBufferIO *BufferIO; |
Renato Golin | 26642b4 | 2016-05-07 20:07:09 +0000 | [diff] [blame] | 115 | uint32_t I = 0, BufferSz; |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 116 | |
Renato Golin | 26642b4 | 2016-05-07 20:07:09 +0000 | [diff] [blame] | 117 | if (!ValueDataBegin) |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | |
| 120 | BufferSz = VPBufferSize ? VPBufferSize : VP_BUFFER_SIZE; |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 121 | BufferIO = lprofCreateBufferIO(Writer, WriterCtx, BufferSz); |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 122 | |
Renato Golin | 26642b4 | 2016-05-07 20:07:09 +0000 | [diff] [blame] | 123 | for (I = 0; I < NumVData; I++) { |
| 124 | ValueProfData *CurVData = ValueDataBegin[I]; |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 125 | if (!CurVData) |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 126 | continue; |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 127 | if (lprofBufferIOWrite(BufferIO, (const uint8_t *)CurVData, |
| 128 | CurVData->TotalSize) != 0) |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 132 | if (lprofBufferIOFlush(BufferIO) != 0) |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 133 | return -1; |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 134 | lprofDeleteBufferIO(BufferIO); |
Xinliang David Li | cda3bc2 | 2015-12-29 23:54:41 +0000 | [diff] [blame] | 135 | |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Xinliang David Li | cf1a8d6 | 2016-03-06 04:18:13 +0000 | [diff] [blame] | 139 | COMPILER_RT_VISIBILITY int |
| 140 | lprofWriteDataImpl(WriterCallback Writer, void *WriterCtx, |
| 141 | const __llvm_profile_data *DataBegin, |
| 142 | const __llvm_profile_data *DataEnd, |
| 143 | const uint64_t *CountersBegin, const uint64_t *CountersEnd, |
Renato Golin | 26642b4 | 2016-05-07 20:07:09 +0000 | [diff] [blame] | 144 | ValueProfData **ValueDataBegin, const uint64_t ValueDataSize, |
| 145 | const char *NamesBegin, const char *NamesEnd) { |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 146 | |
| 147 | /* Calculate size of sections. */ |
Vedant Kumar | b850251 | 2016-02-26 02:49:41 +0000 | [diff] [blame] | 148 | const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 149 | const uint64_t CountersSize = CountersEnd - CountersBegin; |
| 150 | const uint64_t NamesSize = NamesEnd - NamesBegin; |
| 151 | const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize); |
| 152 | |
| 153 | /* Enough zeroes for padding. */ |
| 154 | const char Zeroes[sizeof(uint64_t)] = {0}; |
| 155 | |
| 156 | /* Create the header. */ |
| 157 | __llvm_profile_header Header; |
| 158 | |
| 159 | if (!DataSize) |
| 160 | return 0; |
| 161 | |
Renato Golin | 26642b4 | 2016-05-07 20:07:09 +0000 | [diff] [blame] | 162 | /* Initialize header struture. */ |
Xinliang David Li | 6376db5 | 2015-11-23 18:36:40 +0000 | [diff] [blame] | 163 | #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init; |
| 164 | #include "InstrProfData.inc" |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 165 | |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 166 | /* Write the data. */ |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 167 | ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1}, |
| 168 | {DataBegin, sizeof(__llvm_profile_data), DataSize}, |
| 169 | {CountersBegin, sizeof(uint64_t), CountersSize}, |
| 170 | {NamesBegin, sizeof(uint8_t), NamesSize}, |
| 171 | {Zeroes, sizeof(uint8_t), Padding}}; |
Joerg Sonnenberger | d6487b2 | 2015-12-04 00:40:07 +0000 | [diff] [blame] | 172 | if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx)) |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 173 | return -1; |
Xinliang David Li | 54dd683 | 2015-12-29 07:13:59 +0000 | [diff] [blame] | 174 | |
Renato Golin | 26642b4 | 2016-05-07 20:07:09 +0000 | [diff] [blame] | 175 | return writeValueProfData(Writer, WriterCtx, ValueDataBegin, DataSize); |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 176 | } |