blob: ec3bd1654a5c7397c750c5cef8315310f6076d01 [file] [log] [blame]
Xinliang David Li1d8d46a2015-11-18 21:08:03 +00001/*===- 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
Xinliang David Li6fe18f42015-11-23 04:38:17 +000013LLVM_LIBRARY_VISIBILITY int llvmWriteProfData(WriterCallback Writer,
14 void *WriterCtx,
15 const uint8_t *ValueDataBegin,
16 const uint64_t ValueDataSize) {
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000017 /* Match logic in __llvm_profile_write_buffer(). */
18 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
19 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
20 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
21 const uint64_t *CountersEnd = __llvm_profile_end_counters();
22 const char *NamesBegin = __llvm_profile_begin_names();
23 const char *NamesEnd = __llvm_profile_end_names();
Xinliang David Li2d5bf072015-11-21 04:16:42 +000024 return llvmWriteProfDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
Xinliang David Li6e557162015-11-18 21:11:46 +000025 CountersBegin, CountersEnd, ValueDataBegin,
26 ValueDataSize, NamesBegin, NamesEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000027}
28
Xinliang David Li6fe18f42015-11-23 04:38:17 +000029LLVM_LIBRARY_VISIBILITY int llvmWriteProfDataImpl(
Xinliang David Li2d5bf072015-11-21 04:16:42 +000030 WriterCallback Writer, void *WriterCtx,
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000031 const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
32 const uint64_t *CountersBegin, const uint64_t *CountersEnd,
33 const uint8_t *ValueDataBegin, const uint64_t ValueDataSize,
34 const char *NamesBegin, const char *NamesEnd) {
35
36 /* Calculate size of sections. */
37 const uint64_t DataSize = DataEnd - DataBegin;
38 const uint64_t CountersSize = CountersEnd - CountersBegin;
39 const uint64_t NamesSize = NamesEnd - NamesBegin;
40 const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
41
42 /* Enough zeroes for padding. */
43 const char Zeroes[sizeof(uint64_t)] = {0};
44
45 /* Create the header. */
46 __llvm_profile_header Header;
47
48 if (!DataSize)
49 return 0;
50
51 Header.Magic = __llvm_profile_get_magic();
52 Header.Version = __llvm_profile_get_version();
53 Header.DataSize = DataSize;
54 Header.CountersSize = CountersSize;
55 Header.NamesSize = NamesSize;
56 Header.CountersDelta = (uintptr_t)CountersBegin;
57 Header.NamesDelta = (uintptr_t)NamesBegin;
Xinliang David Li78c4a442015-11-20 19:41:02 +000058 Header.ValueKindLast = IPVK_Last;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000059 Header.ValueDataSize = ValueDataSize;
60 Header.ValueDataDelta = (uintptr_t)ValueDataBegin;
61
Xinliang David Li2d5bf072015-11-21 04:16:42 +000062 /* Write the data. */
63 ProfDataIOVec IOVec[] = {
Xinliang David Lif7185dc2015-11-21 07:26:46 +000064 {&Header, sizeof(__llvm_profile_header), 1},
65 {DataBegin, sizeof(__llvm_profile_data), DataSize},
66 {CountersBegin, sizeof(uint64_t), CountersSize},
67 {NamesBegin, sizeof(char), NamesSize},
68 {Zeroes, sizeof(char), Padding}};
Xinliang David Li2d5bf072015-11-21 04:16:42 +000069 if (Writer(IOVec, sizeof(IOVec) / sizeof(ProfDataIOVec), &WriterCtx))
70 return -1;
71 if (ValueDataBegin) {
72 ProfDataIOVec IOVec[1] = {{ValueDataBegin, sizeof(char), ValueDataSize}};
73 if (Writer(IOVec, 1, &WriterCtx))
74 return -1;
75 }
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000076 return 0;
77}