Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | /*===- InstrProfilingBuffer.c - Write instrumentation to a memory 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" |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 11 | #include "InstrProfilingInternal.h" |
| 12 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 15 | COMPILER_RT_VISIBILITY |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 16 | uint64_t __llvm_profile_get_size_for_buffer(void) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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(); |
| 23 | |
| 24 | return __llvm_profile_get_size_for_buffer_internal( |
| 25 | DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd); |
| 26 | } |
| 27 | |
| 28 | #define PROFILE_RANGE_SIZE(Range) (Range##End - Range##Begin) |
| 29 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 30 | COMPILER_RT_VISIBILITY |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 31 | uint64_t __llvm_profile_get_size_for_buffer_internal( |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 32 | const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd, |
| 33 | const uint64_t *CountersBegin, const uint64_t *CountersEnd, |
| 34 | const char *NamesBegin, const char *NamesEnd) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 35 | /* Match logic in __llvm_profile_write_buffer(). */ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 36 | const uint64_t NamesSize = PROFILE_RANGE_SIZE(Names) * sizeof(char); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 37 | const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize); |
| 38 | return sizeof(__llvm_profile_header) + |
| 39 | PROFILE_RANGE_SIZE(Data) * sizeof(__llvm_profile_data) + |
| 40 | PROFILE_RANGE_SIZE(Counters) * sizeof(uint64_t) + NamesSize + Padding; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 43 | /* The buffer writer is reponsponsible in keeping writer state |
| 44 | * across the call. |
| 45 | */ |
| 46 | static uint32_t bufferWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs, |
| 47 | void **WriterCtx) { |
| 48 | uint32_t I; |
| 49 | char **Buffer = (char **)WriterCtx; |
| 50 | for (I = 0; I < NumIOVecs; I++) { |
| 51 | size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; |
| 52 | memcpy(*Buffer, IOVecs[I].Data, Length); |
| 53 | *Buffer += Length; |
| 54 | } |
| 55 | return 0; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 58 | COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) { |
| 59 | return llvmWriteProfData(bufferWriter, Buffer, 0, 0); |
| 60 | } |
| 61 | |
| 62 | COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal( |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 63 | char *Buffer, const __llvm_profile_data *DataBegin, |
| 64 | const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin, |
| 65 | const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 66 | return llvmWriteProfDataImpl(bufferWriter, Buffer, DataBegin, DataEnd, |
| 67 | CountersBegin, CountersEnd, 0, 0, NamesBegin, |
| 68 | NamesEnd); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 69 | } |