1276789Sdim/*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
2276789Sdim|*
3276789Sdim|*                     The LLVM Compiler Infrastructure
4276789Sdim|*
5276789Sdim|* This file is distributed under the University of Illinois Open Source
6276789Sdim|* License. See LICENSE.TXT for details.
7276789Sdim|*
8276789Sdim\*===----------------------------------------------------------------------===*/
9276789Sdim
10276789Sdim#ifndef PROFILE_INSTRPROFILING_INTERNALH_
11276789Sdim#define PROFILE_INSTRPROFILING_INTERNALH_
12276789Sdim
13276789Sdim#include "InstrProfiling.h"
14296417Sdim#include "stddef.h"
15276789Sdim
16276789Sdim/*!
17276789Sdim * \brief Write instrumentation data to the given buffer, given explicit
18276789Sdim * pointers to the live data in memory.  This function is probably not what you
19276789Sdim * want.  Use __llvm_profile_get_size_for_buffer instead.  Use this function if
20276789Sdim * your program has a custom memory layout.
21276789Sdim */
22276789Sdimuint64_t __llvm_profile_get_size_for_buffer_internal(
23276789Sdim    const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
24276789Sdim    const uint64_t *CountersBegin, const uint64_t *CountersEnd,
25276789Sdim    const char *NamesBegin, const char *NamesEnd);
26276789Sdim
27276789Sdim/*!
28276789Sdim * \brief Write instrumentation data to the given buffer, given explicit
29276789Sdim * pointers to the live data in memory.  This function is probably not what you
30276789Sdim * want.  Use __llvm_profile_write_buffer instead.  Use this function if your
31276789Sdim * program has a custom memory layout.
32276789Sdim *
33276789Sdim * \pre \c Buffer is the start of a buffer at least as big as \a
34276789Sdim * __llvm_profile_get_size_for_buffer_internal().
35276789Sdim */
36276789Sdimint __llvm_profile_write_buffer_internal(
37276789Sdim    char *Buffer, const __llvm_profile_data *DataBegin,
38276789Sdim    const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
39276789Sdim    const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd);
40276789Sdim
41296417Sdim/*!
42296417Sdim * The data structure describing the data to be written by the
43296417Sdim * low level writer callback function.
44296417Sdim */
45296417Sdimtypedef struct ProfDataIOVec {
46296417Sdim  const void *Data;
47296417Sdim  size_t ElmSize;
48296417Sdim  size_t NumElm;
49296417Sdim} ProfDataIOVec;
50296417Sdim
51296417Sdimtypedef uint32_t (*WriterCallback)(ProfDataIOVec *, uint32_t NumIOVecs,
52296417Sdim                                   void **WriterCtx);
53296417Sdim
54296417Sdim/*!
55296417Sdim * The data structure for buffered IO of profile data.
56296417Sdim */
57296417Sdimtypedef struct ProfBufferIO {
58296417Sdim  /* File handle.  */
59296417Sdim  void *File;
60296417Sdim  /* Low level IO callback. */
61296417Sdim  WriterCallback FileWriter;
62296417Sdim  /* The start of the buffer. */
63296417Sdim  uint8_t *BufferStart;
64296417Sdim  /* Total size of the buffer. */
65296417Sdim  uint32_t BufferSz;
66296417Sdim  /* Current byte offset from the start of the buffer. */
67296417Sdim  uint32_t CurOffset;
68296417Sdim} ProfBufferIO;
69296417Sdim
70296417Sdim/* The creator interface used by testing.  */
71296417SdimProfBufferIO *llvmCreateBufferIOInternal(void *File, uint32_t DefaultBufferSz);
72296417Sdim/*!
73296417Sdim * This is the interface to create a handle for buffered IO.
74296417Sdim */
75296417SdimProfBufferIO *llvmCreateBufferIO(WriterCallback FileWriter, void *File,
76296417Sdim                                 uint32_t DefaultBufferSz);
77296417Sdim/*!
78296417Sdim * The interface to destroy the bufferIO handle and reclaim
79296417Sdim * the memory.
80296417Sdim */
81296417Sdimvoid llvmDeleteBufferIO(ProfBufferIO *BufferIO);
82296417Sdim
83296417Sdim/*!
84296417Sdim * This is the interface to write \c Data of \c Size bytes through
85296417Sdim * \c BufferIO. Returns 0 if successful, otherwise return -1.
86296417Sdim */
87296417Sdimint llvmBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,
88296417Sdim                      uint32_t Size);
89296417Sdim/*!
90296417Sdim * The interface to flush the remaining data in the buffer.
91296417Sdim * through the low level writer callback.
92296417Sdim */
93296417Sdimint llvmBufferIOFlush(ProfBufferIO *BufferIO);
94296417Sdim
95296417Sdim/* The low level interface to write data into a buffer. It is used as the
96296417Sdim * callback by other high level writer methods such as buffered IO writer
97296417Sdim * and profile data writer.  */
98296417Sdimuint32_t llvmBufferWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
99296417Sdim                          void **WriterCtx);
100296417Sdim
101296417Sdimint llvmWriteProfData(WriterCallback Writer, void *WriterCtx,
102296417Sdim                      struct ValueProfData **ValueDataArray,
103296417Sdim                      const uint64_t ValueDataSize);
104296417Sdimint llvmWriteProfDataImpl(WriterCallback Writer, void *WriterCtx,
105296417Sdim                          const __llvm_profile_data *DataBegin,
106296417Sdim                          const __llvm_profile_data *DataEnd,
107296417Sdim                          const uint64_t *CountersBegin,
108296417Sdim                          const uint64_t *CountersEnd,
109296417Sdim                          struct ValueProfData **ValueDataBeginArray,
110296417Sdim                          const uint64_t ValueDataSize, const char *NamesBegin,
111296417Sdim                          const char *NamesEnd);
112296417Sdim
113296417Sdimextern char *(*GetEnvHook)(const char *);
114296417Sdimextern void (*FreeHook)(void *);
115296417Sdimextern void* (*CallocHook)(size_t, size_t);
116296417Sdimextern uint32_t VPBufferSize;
117296417Sdim
118276789Sdim#endif
119