blob: 48637686007a4876ce39bad9d4dd3742230d9c79 [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
14llvmWriteProfData(void *BufferOrFile, const uint8_t * ValueDataBegin, const uint64_t ValueDataSize, WriterCallback Writer) {
15 /* Match logic in __llvm_profile_write_buffer(). */
16 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
17 const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
18 const uint64_t *CountersBegin = __llvm_profile_begin_counters();
19 const uint64_t *CountersEnd = __llvm_profile_end_counters();
20 const char *NamesBegin = __llvm_profile_begin_names();
21 const char *NamesEnd = __llvm_profile_end_names();
22 return llvmWriteProfDataImpl(BufferOrFile, Writer, DataBegin, DataEnd,
23 CountersBegin, CountersEnd,
24 ValueDataBegin, ValueDataSize,
25 NamesBegin,
26 NamesEnd);
27}
28
29__attribute__((visibility("hidden"))) int llvmWriteProfDataImpl(
30 void *BufferOrFile, WriterCallback Writer,
31 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;
58 Header.ValueKindLast = VK_LAST;
59 Header.ValueDataSize = ValueDataSize;
60 Header.ValueDataDelta = (uintptr_t)ValueDataBegin;
61
62/* Write the data. */
63#define CHECK_write(Data, Size, Length, BuffOrFile) \
64 do { \
65 if (Writer(Data, Size, Length, &BuffOrFile) != Length) \
66 return -1; \
67 } while (0)
68 CHECK_write(&Header, sizeof(__llvm_profile_header), 1, BufferOrFile);
69 CHECK_write(DataBegin, sizeof(__llvm_profile_data), DataSize, BufferOrFile);
70 CHECK_write(CountersBegin, sizeof(uint64_t), CountersSize, BufferOrFile);
71 CHECK_write(NamesBegin, sizeof(char), NamesSize, BufferOrFile);
72 CHECK_write(Zeroes, sizeof(char), Padding, BufferOrFile);
73 if (ValueDataBegin)
74 CHECK_write(ValueDataBegin, sizeof(char), ValueDataSize, BufferOrFile);
75#undef CHECK_write
76 return 0;
77}