blob: 8bade76cfe0a3886bc4af7e60b33d7f26d7a4fc2 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001/*===- 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 Hines86277eb2015-03-23 12:06:32 -070011#include "InstrProfilingInternal.h"
12
Stephen Hines2d1fdb22014-05-28 23:58:16 -070013#include <string.h>
14
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080015COMPILER_RT_VISIBILITY
Stephen Hines2d1fdb22014-05-28 23:58:16 -070016uint64_t __llvm_profile_get_size_for_buffer(void) {
Stephen Hines86277eb2015-03-23 12:06:32 -070017 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 Nainar799172d2016-03-03 15:50:30 -080030COMPILER_RT_VISIBILITY
Stephen Hines86277eb2015-03-23 12:06:32 -070031uint64_t __llvm_profile_get_size_for_buffer_internal(
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080032 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 Hines2d1fdb22014-05-28 23:58:16 -070035 /* Match logic in __llvm_profile_write_buffer(). */
Stephen Hines86277eb2015-03-23 12:06:32 -070036 const uint64_t NamesSize = PROFILE_RANGE_SIZE(Names) * sizeof(char);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080037 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 Hines2d1fdb22014-05-28 23:58:16 -070041}
42
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080043/* The buffer writer is reponsponsible in keeping writer state
44 * across the call.
45 */
46static 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 Hines86277eb2015-03-23 12:06:32 -070056}
57
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080058COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
59 return llvmWriteProfData(bufferWriter, Buffer, 0, 0);
60}
61
62COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
Stephen Hines86277eb2015-03-23 12:06:32 -070063 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 Nainar799172d2016-03-03 15:50:30 -080066 return llvmWriteProfDataImpl(bufferWriter, Buffer, DataBegin, DataEnd,
67 CountersBegin, CountersEnd, 0, 0, NamesBegin,
68 NamesEnd);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070069}