InstrProfilingInternal.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
  2. |*
  3. |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. |* See https://llvm.org/LICENSE.txt for license information.
  5. |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. |*
  7. \*===----------------------------------------------------------------------===*/
  8. #ifndef PROFILE_INSTRPROFILING_INTERNALH_
  9. #define PROFILE_INSTRPROFILING_INTERNALH_
  10. #include <stddef.h>
  11. #include "InstrProfiling.h"
  12. /*!
  13. * \brief Write instrumentation data to the given buffer, given explicit
  14. * pointers to the live data in memory. This function is probably not what you
  15. * want. Use __llvm_profile_get_size_for_buffer instead. Use this function if
  16. * your program has a custom memory layout.
  17. */
  18. uint64_t __llvm_profile_get_size_for_buffer_internal(
  19. const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
  20. const char *CountersBegin, const char *CountersEnd, const char *NamesBegin,
  21. const char *NamesEnd);
  22. /*!
  23. * \brief Write instrumentation data to the given buffer, given explicit
  24. * pointers to the live data in memory. This function is probably not what you
  25. * want. Use __llvm_profile_write_buffer instead. Use this function if your
  26. * program has a custom memory layout.
  27. *
  28. * \pre \c Buffer is the start of a buffer at least as big as \a
  29. * __llvm_profile_get_size_for_buffer_internal().
  30. */
  31. int __llvm_profile_write_buffer_internal(
  32. char *Buffer, const __llvm_profile_data *DataBegin,
  33. const __llvm_profile_data *DataEnd, const char *CountersBegin,
  34. const char *CountersEnd, const char *NamesBegin, const char *NamesEnd);
  35. /*!
  36. * The data structure describing the data to be written by the
  37. * low level writer callback function.
  38. *
  39. * If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
  40. * 0, the write is skipped (the writer simply advances ElmSize*NumElm bytes).
  41. *
  42. * If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is
  43. * nonzero, ElmSize*NumElm zero bytes are written.
  44. */
  45. typedef struct ProfDataIOVec {
  46. const void *Data;
  47. size_t ElmSize;
  48. size_t NumElm;
  49. int UseZeroPadding;
  50. } ProfDataIOVec;
  51. struct ProfDataWriter;
  52. typedef uint32_t (*WriterCallback)(struct ProfDataWriter *This, ProfDataIOVec *,
  53. uint32_t NumIOVecs);
  54. typedef struct ProfDataWriter {
  55. WriterCallback Write;
  56. void *WriterCtx;
  57. } ProfDataWriter;
  58. /*!
  59. * The data structure for buffered IO of profile data.
  60. */
  61. typedef struct ProfBufferIO {
  62. ProfDataWriter *FileWriter;
  63. uint32_t OwnFileWriter;
  64. /* The start of the buffer. */
  65. uint8_t *BufferStart;
  66. /* Total size of the buffer. */
  67. uint32_t BufferSz;
  68. /* Current byte offset from the start of the buffer. */
  69. uint32_t CurOffset;
  70. } ProfBufferIO;
  71. /* The creator interface used by testing. */
  72. ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);
  73. /*!
  74. * This is the interface to create a handle for buffered IO.
  75. */
  76. ProfBufferIO *lprofCreateBufferIO(ProfDataWriter *FileWriter);
  77. /*!
  78. * The interface to destroy the bufferIO handle and reclaim
  79. * the memory.
  80. */
  81. void lprofDeleteBufferIO(ProfBufferIO *BufferIO);
  82. /*!
  83. * This is the interface to write \c Data of \c Size bytes through
  84. * \c BufferIO. Returns 0 if successful, otherwise return -1.
  85. */
  86. int lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,
  87. uint32_t Size);
  88. /*!
  89. * The interface to flush the remaining data in the buffer.
  90. * through the low level writer callback.
  91. */
  92. int lprofBufferIOFlush(ProfBufferIO *BufferIO);
  93. /* The low level interface to write data into a buffer. It is used as the
  94. * callback by other high level writer methods such as buffered IO writer
  95. * and profile data writer. */
  96. uint32_t lprofBufferWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
  97. uint32_t NumIOVecs);
  98. void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer);
  99. struct ValueProfData;
  100. struct ValueProfRecord;
  101. struct InstrProfValueData;
  102. struct ValueProfNode;
  103. /*!
  104. * The class that defines a set of methods to read value profile
  105. * data for streaming/serialization from the instrumentation runtime.
  106. */
  107. typedef struct VPDataReaderType {
  108. uint32_t (*InitRTRecord)(const __llvm_profile_data *Data,
  109. uint8_t *SiteCountArray[]);
  110. /* Function pointer to getValueProfRecordHeader method. */
  111. uint32_t (*GetValueProfRecordHeaderSize)(uint32_t NumSites);
  112. /* Function pointer to getFristValueProfRecord method. */
  113. struct ValueProfRecord *(*GetFirstValueProfRecord)(struct ValueProfData *);
  114. /* Return the number of value data for site \p Site. */
  115. uint32_t (*GetNumValueDataForSite)(uint32_t VK, uint32_t Site);
  116. /* Return the total size of the value profile data of the
  117. * current function. */
  118. uint32_t (*GetValueProfDataSize)(void);
  119. /*!
  120. * Read the next \p N value data for site \p Site and store the data
  121. * in \p Dst. \p StartNode is the first value node to start with if
  122. * it is not null. The function returns the pointer to the value
  123. * node pointer to be used as the \p StartNode of the next batch reading.
  124. * If there is nothing left, it returns NULL.
  125. */
  126. struct ValueProfNode *(*GetValueData)(uint32_t ValueKind, uint32_t Site,
  127. struct InstrProfValueData *Dst,
  128. struct ValueProfNode *StartNode,
  129. uint32_t N);
  130. } VPDataReaderType;
  131. /* Write profile data to destination. If SkipNameDataWrite is set to 1,
  132. the name data is already in destination, we just skip over it. */
  133. int lprofWriteData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader,
  134. int SkipNameDataWrite);
  135. int lprofWriteDataImpl(ProfDataWriter *Writer,
  136. const __llvm_profile_data *DataBegin,
  137. const __llvm_profile_data *DataEnd,
  138. const char *CountersBegin, const char *CountersEnd,
  139. VPDataReaderType *VPDataReader, const char *NamesBegin,
  140. const char *NamesEnd, int SkipNameDataWrite);
  141. /* Merge value profile data pointed to by SrcValueProfData into
  142. * in-memory profile counters pointed by to DstData. */
  143. void lprofMergeValueProfData(struct ValueProfData *SrcValueProfData,
  144. __llvm_profile_data *DstData);
  145. VPDataReaderType *lprofGetVPDataReader();
  146. /* Internal interface used by test to reset the max number of
  147. * tracked values per value site to be \p MaxVals.
  148. */
  149. void lprofSetMaxValsPerSite(uint32_t MaxVals);
  150. void lprofSetupValueProfiler();
  151. /* Return the profile header 'signature' value associated with the current
  152. * executable or shared library. The signature value can be used to for
  153. * a profile name that is unique to this load module so that it does not
  154. * collide with profiles from other binaries. It also allows shared libraries
  155. * to dump merged profile data into its own profile file. */
  156. uint64_t lprofGetLoadModuleSignature();
  157. /*
  158. * Return non zero value if the profile data has already been
  159. * dumped to the file.
  160. */
  161. unsigned lprofProfileDumped(void);
  162. void lprofSetProfileDumped(unsigned);
  163. COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
  164. COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
  165. COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;
  166. COMPILER_RT_VISIBILITY extern uint32_t VPMaxNumValsPerSite;
  167. /* Pointer to the start of static value counters to be allocted. */
  168. COMPILER_RT_VISIBILITY extern ValueProfNode *CurrentVNode;
  169. COMPILER_RT_VISIBILITY extern ValueProfNode *EndVNode;
  170. extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);
  171. /*
  172. * Write binary ids into profiles if writer is given.
  173. * Return -1 if an error occurs, otherwise, return total size of binary ids.
  174. */
  175. int __llvm_write_binary_ids(ProfDataWriter *Writer);
  176. #endif