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" |
| 12 | |
| 13 | __attribute__((visibility("hidden"))) int |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 14 | llvmWriteProfData(WriterCallback Writer, void *WriterCtx, |
Xinliang David Li | e8efaea | 2015-11-19 07:21:47 +0000 | [diff] [blame] | 15 | const uint8_t *ValueDataBegin, const uint64_t ValueDataSize) { |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 16 | /* Match logic in __llvm_profile_write_buffer(). */ |
| 17 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); |
| 18 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); |
| 19 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); |
| 20 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); |
| 21 | const char *NamesBegin = __llvm_profile_begin_names(); |
| 22 | const char *NamesEnd = __llvm_profile_end_names(); |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 23 | return llvmWriteProfDataImpl(Writer, WriterCtx, DataBegin, DataEnd, |
Xinliang David Li | 6e55716 | 2015-11-18 21:11:46 +0000 | [diff] [blame] | 24 | CountersBegin, CountersEnd, ValueDataBegin, |
| 25 | ValueDataSize, NamesBegin, NamesEnd); |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | __attribute__((visibility("hidden"))) int llvmWriteProfDataImpl( |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 29 | WriterCallback Writer, void *WriterCtx, |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 30 | const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd, |
| 31 | const uint64_t *CountersBegin, const uint64_t *CountersEnd, |
| 32 | const uint8_t *ValueDataBegin, const uint64_t ValueDataSize, |
| 33 | const char *NamesBegin, const char *NamesEnd) { |
| 34 | |
| 35 | /* Calculate size of sections. */ |
| 36 | const uint64_t DataSize = DataEnd - DataBegin; |
| 37 | const uint64_t CountersSize = CountersEnd - CountersBegin; |
| 38 | const uint64_t NamesSize = NamesEnd - NamesBegin; |
| 39 | const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize); |
| 40 | |
| 41 | /* Enough zeroes for padding. */ |
| 42 | const char Zeroes[sizeof(uint64_t)] = {0}; |
| 43 | |
| 44 | /* Create the header. */ |
| 45 | __llvm_profile_header Header; |
| 46 | |
| 47 | if (!DataSize) |
| 48 | return 0; |
| 49 | |
| 50 | Header.Magic = __llvm_profile_get_magic(); |
| 51 | Header.Version = __llvm_profile_get_version(); |
| 52 | Header.DataSize = DataSize; |
| 53 | Header.CountersSize = CountersSize; |
| 54 | Header.NamesSize = NamesSize; |
| 55 | Header.CountersDelta = (uintptr_t)CountersBegin; |
| 56 | Header.NamesDelta = (uintptr_t)NamesBegin; |
Xinliang David Li | 78c4a44 | 2015-11-20 19:41:02 +0000 | [diff] [blame] | 57 | Header.ValueKindLast = IPVK_Last; |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 58 | Header.ValueDataSize = ValueDataSize; |
| 59 | Header.ValueDataDelta = (uintptr_t)ValueDataBegin; |
| 60 | |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 61 | /* Write the data. */ |
| 62 | ProfDataIOVec IOVec[] = { |
Xinliang David Li | f7185dc | 2015-11-21 07:26:46 +0000 | [diff] [blame^] | 63 | {&Header, sizeof(__llvm_profile_header), 1}, |
| 64 | {DataBegin, sizeof(__llvm_profile_data), DataSize}, |
| 65 | {CountersBegin, sizeof(uint64_t), CountersSize}, |
| 66 | {NamesBegin, sizeof(char), NamesSize}, |
| 67 | {Zeroes, sizeof(char), Padding}}; |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 68 | if (Writer(IOVec, sizeof(IOVec) / sizeof(ProfDataIOVec), &WriterCtx)) |
| 69 | return -1; |
| 70 | if (ValueDataBegin) { |
| 71 | ProfDataIOVec IOVec[1] = {{ValueDataBegin, sizeof(char), ValueDataSize}}; |
| 72 | if (Writer(IOVec, 1, &WriterCtx)) |
| 73 | return -1; |
| 74 | } |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |