blob: af0afc85affb524eb1e40aa1b0fdbb00fa30be48 [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
13__attribute__((visibility("hidden"))) int
Xinliang David Li2d5bf072015-11-21 04:16:42 +000014llvmWriteProfData(WriterCallback Writer, void *WriterCtx,
Xinliang David Lie8efaea2015-11-19 07:21:47 +000015 const uint8_t *ValueDataBegin, const uint64_t ValueDataSize) {
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000016 /* 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 Li2d5bf072015-11-21 04:16:42 +000023 return llvmWriteProfDataImpl(Writer, WriterCtx, DataBegin, DataEnd,
Xinliang David Li6e557162015-11-18 21:11:46 +000024 CountersBegin, CountersEnd, ValueDataBegin,
25 ValueDataSize, NamesBegin, NamesEnd);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000026}
27
28__attribute__((visibility("hidden"))) int llvmWriteProfDataImpl(
Xinliang David Li2d5bf072015-11-21 04:16:42 +000029 WriterCallback Writer, void *WriterCtx,
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000030 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 Li78c4a442015-11-20 19:41:02 +000057 Header.ValueKindLast = IPVK_Last;
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000058 Header.ValueDataSize = ValueDataSize;
59 Header.ValueDataDelta = (uintptr_t)ValueDataBegin;
60
Xinliang David Li2d5bf072015-11-21 04:16:42 +000061 /* Write the data. */
62 ProfDataIOVec IOVec[] = {
63 {(const char *)&Header, sizeof(__llvm_profile_header), 1},
64 {(const char *)DataBegin, sizeof(__llvm_profile_data), DataSize},
65 {(const char *)CountersBegin, sizeof(uint64_t), CountersSize},
66 {(const char *)NamesBegin, sizeof(char), NamesSize},
67 {(const char *)Zeroes, sizeof(char), Padding}};
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 Li1d8d46a2015-11-18 21:08:03 +000075 return 0;
76}