blob: e837a351151da80df41566c8426b1adfc15caa2a [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 Lie8efaea2015-11-19 07:21:47 +000014llvmWriteProfData(void *WriterCtx, WriterCallback Writer,
15 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 Lie8efaea2015-11-19 07:21:47 +000023 return llvmWriteProfDataImpl(WriterCtx, Writer, 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 Lie8efaea2015-11-19 07:21:47 +000029 void *WriterCtx, WriterCallback Writer,
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;
57 Header.ValueKindLast = VK_LAST;
58 Header.ValueDataSize = ValueDataSize;
59 Header.ValueDataDelta = (uintptr_t)ValueDataBegin;
60
61/* Write the data. */
Xinliang David Li6e557162015-11-18 21:11:46 +000062#define CHECK_write(Data, Size, Length, BuffOrFile) \
63 do { \
64 if (Writer(Data, Size, Length, &BuffOrFile) != Length) \
65 return -1; \
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000066 } while (0)
Xinliang David Lie8efaea2015-11-19 07:21:47 +000067 CHECK_write(&Header, sizeof(__llvm_profile_header), 1, WriterCtx);
68 CHECK_write(DataBegin, sizeof(__llvm_profile_data), DataSize, WriterCtx);
69 CHECK_write(CountersBegin, sizeof(uint64_t), CountersSize, WriterCtx);
70 CHECK_write(NamesBegin, sizeof(char), NamesSize, WriterCtx);
71 CHECK_write(Zeroes, sizeof(char), Padding, WriterCtx);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000072 if (ValueDataBegin)
Xinliang David Lie8efaea2015-11-19 07:21:47 +000073 CHECK_write(ValueDataBegin, sizeof(char), ValueDataSize, WriterCtx);
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000074#undef CHECK_write
75 return 0;
76}