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 | |
| 14 | /* The buffer writer is reponsponsible in keeping writer state |
| 15 | * across the call. |
| 16 | */ |
| 17 | COMPILER_RT_VISIBILITY uint32_t llvmBufferWriter(ProfDataIOVec *IOVecs, |
| 18 | uint32_t NumIOVecs, |
| 19 | void **WriterCtx) { |
| 20 | uint32_t I; |
| 21 | char **Buffer = (char **)WriterCtx; |
| 22 | for (I = 0; I < NumIOVecs; I++) { |
| 23 | size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; |
| 24 | memcpy(*Buffer, IOVecs[I].Data, Length); |
| 25 | *Buffer += Length; |
| 26 | } |
| 27 | return 0; |
| 28 | } |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 29 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 30 | COMPILER_RT_VISIBILITY int llvmWriteProfData(WriterCallback Writer, |
| 31 | void *WriterCtx, |
| 32 | const uint8_t *ValueDataBegin, |
| 33 | const uint64_t ValueDataSize) { |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 34 | /* Match logic in __llvm_profile_write_buffer(). */ |
| 35 | const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); |
| 36 | const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); |
| 37 | const uint64_t *CountersBegin = __llvm_profile_begin_counters(); |
| 38 | const uint64_t *CountersEnd = __llvm_profile_end_counters(); |
| 39 | const char *NamesBegin = __llvm_profile_begin_names(); |
| 40 | const char *NamesEnd = __llvm_profile_end_names(); |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 41 | return llvmWriteProfDataImpl(Writer, WriterCtx, DataBegin, DataEnd, |
Xinliang David Li | 6e55716 | 2015-11-18 21:11:46 +0000 | [diff] [blame] | 42 | CountersBegin, CountersEnd, ValueDataBegin, |
| 43 | ValueDataSize, NamesBegin, NamesEnd); |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Xinliang David Li | abfd553 | 2015-12-16 03:29:15 +0000 | [diff] [blame] | 46 | COMPILER_RT_VISIBILITY int llvmWriteProfDataImpl( |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 47 | WriterCallback Writer, void *WriterCtx, |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 48 | const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd, |
| 49 | const uint64_t *CountersBegin, const uint64_t *CountersEnd, |
| 50 | const uint8_t *ValueDataBegin, const uint64_t ValueDataSize, |
| 51 | const char *NamesBegin, const char *NamesEnd) { |
| 52 | |
| 53 | /* Calculate size of sections. */ |
| 54 | const uint64_t DataSize = DataEnd - DataBegin; |
| 55 | const uint64_t CountersSize = CountersEnd - CountersBegin; |
| 56 | const uint64_t NamesSize = NamesEnd - NamesBegin; |
| 57 | const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize); |
| 58 | |
| 59 | /* Enough zeroes for padding. */ |
| 60 | const char Zeroes[sizeof(uint64_t)] = {0}; |
| 61 | |
| 62 | /* Create the header. */ |
| 63 | __llvm_profile_header Header; |
| 64 | |
| 65 | if (!DataSize) |
| 66 | return 0; |
| 67 | |
Xinliang David Li | 6376db5 | 2015-11-23 18:36:40 +0000 | [diff] [blame] | 68 | /* Initialize header struture. */ |
| 69 | #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init; |
| 70 | #include "InstrProfData.inc" |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 71 | |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 72 | /* Write the data. */ |
| 73 | ProfDataIOVec IOVec[] = { |
Xinliang David Li | f7185dc | 2015-11-21 07:26:46 +0000 | [diff] [blame] | 74 | {&Header, sizeof(__llvm_profile_header), 1}, |
| 75 | {DataBegin, sizeof(__llvm_profile_data), DataSize}, |
| 76 | {CountersBegin, sizeof(uint64_t), CountersSize}, |
| 77 | {NamesBegin, sizeof(char), NamesSize}, |
| 78 | {Zeroes, sizeof(char), Padding}}; |
Joerg Sonnenberger | d6487b2 | 2015-12-04 00:40:07 +0000 | [diff] [blame] | 79 | if (Writer(IOVec, sizeof(IOVec) / sizeof(*IOVec), &WriterCtx)) |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 80 | return -1; |
| 81 | if (ValueDataBegin) { |
Joerg Sonnenberger | d6487b2 | 2015-12-04 00:40:07 +0000 | [diff] [blame] | 82 | ProfDataIOVec IOVec2[] = {{ValueDataBegin, sizeof(char), ValueDataSize}}; |
| 83 | if (Writer(IOVec2, sizeof(IOVec2) / sizeof(*IOVec2), &WriterCtx)) |
Xinliang David Li | 2d5bf07 | 2015-11-21 04:16:42 +0000 | [diff] [blame] | 84 | return -1; |
| 85 | } |
Xinliang David Li | 1d8d46a | 2015-11-18 21:08:03 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | } |