blob: a931d45623d982ef749d429572e39fd9dd45d24a [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
Xinliang David Li6376db52015-11-23 18:36:40 +000051 /* Initialize header struture. */
52#define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
53#include "InstrProfData.inc"
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000054
Xinliang David Li2d5bf072015-11-21 04:16:42 +000055 /* Write the data. */
56 ProfDataIOVec IOVec[] = {
Xinliang David Lif7185dc2015-11-21 07:26:46 +000057 {&Header, sizeof(__llvm_profile_header), 1},
58 {DataBegin, sizeof(__llvm_profile_data), DataSize},
59 {CountersBegin, sizeof(uint64_t), CountersSize},
60 {NamesBegin, sizeof(char), NamesSize},
61 {Zeroes, sizeof(char), Padding}};
Xinliang David Li2d5bf072015-11-21 04:16:42 +000062 if (Writer(IOVec, sizeof(IOVec) / sizeof(ProfDataIOVec), &WriterCtx))
63 return -1;
64 if (ValueDataBegin) {
65 ProfDataIOVec IOVec[1] = {{ValueDataBegin, sizeof(char), ValueDataSize}};
66 if (Writer(IOVec, 1, &WriterCtx))
67 return -1;
68 }
Xinliang David Li1d8d46a2015-11-18 21:08:03 +000069 return 0;
70}