InstrProfiling.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_H_
  9. #define PROFILE_INSTRPROFILING_H_
  10. #include "InstrProfilingPort.h"
  11. #include <stdio.h>
  12. // Make sure __LLVM_INSTR_PROFILE_GENERATE is always defined before
  13. // including instr_prof_interface.h so the interface functions are
  14. // declared correctly for the runtime.
  15. // __LLVM_INSTR_PROFILE_GENERATE is always `#undef`ed after the header,
  16. // because compiler-rt does not support profiling the profiling runtime itself.
  17. #ifndef __LLVM_INSTR_PROFILE_GENERATE
  18. #define __LLVM_INSTR_PROFILE_GENERATE
  19. #endif
  20. #include "profile/instr_prof_interface.h"
  21. #undef __LLVM_INSTR_PROFILE_GENERATE
  22. #define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY
  23. #include "profile/InstrProfData.inc"
  24. enum ValueKind {
  25. #define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
  26. #include "profile/InstrProfData.inc"
  27. };
  28. typedef void *IntPtrT;
  29. typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
  30. __llvm_profile_data {
  31. #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;
  32. #include "profile/InstrProfData.inc"
  33. } __llvm_profile_data;
  34. typedef struct __llvm_profile_header {
  35. #define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;
  36. #include "profile/InstrProfData.inc"
  37. } __llvm_profile_header;
  38. typedef struct ValueProfNode * PtrToNodeT;
  39. typedef struct ValueProfNode {
  40. #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name;
  41. #include "profile/InstrProfData.inc"
  42. } ValueProfNode;
  43. /*!
  44. * \brief Return 1 if profile counters are continuously synced to the raw
  45. * profile via an mmap(). This is in contrast to the default mode, in which
  46. * the raw profile is written out at program exit time.
  47. */
  48. int __llvm_profile_is_continuous_mode_enabled(void);
  49. /*!
  50. * \brief Enable continuous mode.
  51. *
  52. * See \ref __llvm_profile_is_continuous_mode_enabled. The behavior is undefined
  53. * if continuous mode is already enabled, or if it cannot be enable due to
  54. * conflicting options.
  55. */
  56. void __llvm_profile_enable_continuous_mode(void);
  57. /*!
  58. * \brief Disable continuous mode.
  59. *
  60. */
  61. void __llvm_profile_disable_continuous_mode(void);
  62. /*!
  63. * \brief Set the page size.
  64. *
  65. * This is a pre-requisite for enabling continuous mode. The buffer size
  66. * calculation code inside of libprofile cannot simply call getpagesize(), as
  67. * it is not allowed to depend on libc.
  68. */
  69. void __llvm_profile_set_page_size(unsigned PageSize);
  70. /*!
  71. * \brief Get number of bytes necessary to pad the argument to eight
  72. * byte boundary.
  73. */
  74. uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
  75. /*!
  76. * \brief Get required size for profile buffer.
  77. */
  78. uint64_t __llvm_profile_get_size_for_buffer(void);
  79. /*!
  80. * \brief Write instrumentation data to the given buffer.
  81. *
  82. * \pre \c Buffer is the start of a buffer at least as big as \a
  83. * __llvm_profile_get_size_for_buffer().
  84. */
  85. int __llvm_profile_write_buffer(char *Buffer);
  86. const __llvm_profile_data *__llvm_profile_begin_data(void);
  87. const __llvm_profile_data *__llvm_profile_end_data(void);
  88. const char *__llvm_profile_begin_names(void);
  89. const char *__llvm_profile_end_names(void);
  90. char *__llvm_profile_begin_counters(void);
  91. char *__llvm_profile_end_counters(void);
  92. char *__llvm_profile_begin_bitmap(void);
  93. char *__llvm_profile_end_bitmap(void);
  94. ValueProfNode *__llvm_profile_begin_vnodes();
  95. ValueProfNode *__llvm_profile_end_vnodes();
  96. uint32_t *__llvm_profile_begin_orderfile();
  97. /*!
  98. * \brief Merge profile data from buffer.
  99. *
  100. * Read profile data from buffer \p Profile and merge with in-process profile
  101. * counters and bitmaps. The client is expected to have checked or already
  102. * know the profile data in the buffer matches the in-process counter
  103. * structure before calling it. Returns 0 (success) if the profile data is
  104. * valid. Upon reading invalid/corrupted profile data, returns 1 (failure).
  105. */
  106. int __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size);
  107. /*! \brief Check if profile in buffer matches the current binary.
  108. *
  109. * Returns 0 (success) if the profile data in buffer \p Profile with size
  110. * \p Size was generated by the same binary and therefore matches
  111. * structurally the in-process counters and bitmaps. If the profile data in
  112. * buffer is not compatible, the interface returns 1 (failure).
  113. */
  114. int __llvm_profile_check_compatibility(const char *Profile,
  115. uint64_t Size);
  116. /*!
  117. * \brief Counts the number of times a target value is seen.
  118. *
  119. * Records the target value for the CounterIndex if not seen before. Otherwise,
  120. * increments the counter associated w/ the target value.
  121. * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
  122. * uint32_t CounterIndex);
  123. */
  124. void INSTR_PROF_VALUE_PROF_FUNC(
  125. #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName
  126. #include "profile/InstrProfData.inc"
  127. );
  128. void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,
  129. uint32_t CounterIndex,
  130. uint64_t CounterValue);
  131. /*!
  132. * \brief Write instrumentation data to the current file.
  133. *
  134. * Writes to the file with the last name given to \a *
  135. * __llvm_profile_set_filename(),
  136. * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
  137. * or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR,
  138. * or if that's not set, \c "default.profraw".
  139. */
  140. int __llvm_profile_write_file(void);
  141. int __llvm_orderfile_write_file(void);
  142. /*!
  143. * \brief Set the FILE object for writing instrumentation data. Return 0 if set
  144. * successfully or return 1 if failed.
  145. *
  146. * Sets the FILE object to be used for subsequent calls to
  147. * \a __llvm_profile_write_file(). The profile file name set by environment
  148. * variable, command-line option, or calls to \a __llvm_profile_set_filename
  149. * will be ignored.
  150. *
  151. * \c File will not be closed after a call to \a __llvm_profile_write_file() but
  152. * it may be flushed. Passing NULL restores default behavior.
  153. *
  154. * If \c EnableMerge is nonzero, the runtime will always merge profiling data
  155. * with the contents of the profiling file. If EnableMerge is zero, the runtime
  156. * may still merge the data if it would have merged for another reason (for
  157. * example, because of a %m specifier in the file name).
  158. *
  159. * Note: There may be multiple copies of the profile runtime (one for each
  160. * instrumented image/DSO). This API only modifies the file object within the
  161. * copy of the runtime available to the calling image.
  162. *
  163. * Warning: This is a no-op if EnableMerge is 0 in continuous mode (\ref
  164. * __llvm_profile_is_continuous_mode_enabled), because disable merging requires
  165. * copying the old profile file to new profile file and this function is usually
  166. * used when the proess doesn't have permission to open file.
  167. */
  168. int __llvm_profile_set_file_object(FILE *File, int EnableMerge);
  169. /*! \brief Register to write instrumentation data to file at exit. */
  170. int __llvm_profile_register_write_file_atexit(void);
  171. /*! \brief Initialize file handling. */
  172. void __llvm_profile_initialize_file(void);
  173. /*! \brief Initialize the profile runtime. */
  174. void __llvm_profile_initialize(void);
  175. /*!
  176. * \brief Return path prefix (excluding the base filename) of the profile data.
  177. * This is useful for users using \c -fprofile-generate=./path_prefix who do
  178. * not care about the default raw profile name. It is also useful to collect
  179. * more than more profile data files dumped in the same directory (Online
  180. * merge mode is turned on for instrumented programs with shared libs).
  181. * Side-effect: this API call will invoke malloc with dynamic memory allocation.
  182. */
  183. const char *__llvm_profile_get_path_prefix();
  184. /*!
  185. * \brief Return filename (including path) of the profile data. Note that if the
  186. * user calls __llvm_profile_set_filename later after invoking this interface,
  187. * the actual file name may differ from what is returned here.
  188. * Side-effect: this API call will invoke malloc with dynamic memory allocation
  189. * (the returned pointer must be passed to `free` to avoid a leak).
  190. *
  191. * Note: There may be multiple copies of the profile runtime (one for each
  192. * instrumented image/DSO). This API only retrieves the filename from the copy
  193. * of the runtime available to the calling image.
  194. */
  195. const char *__llvm_profile_get_filename();
  196. /*! \brief Get the magic token for the file format. */
  197. uint64_t __llvm_profile_get_magic(void);
  198. /*! \brief Get the version of the file format. */
  199. uint64_t __llvm_profile_get_version(void);
  200. /*! \brief Get the number of entries in the profile data section. */
  201. uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
  202. const __llvm_profile_data *End);
  203. /*! \brief Get the size of the profile data section in bytes. */
  204. uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
  205. const __llvm_profile_data *End);
  206. /*! \brief Get the size in bytes of a single counter entry. */
  207. size_t __llvm_profile_counter_entry_size(void);
  208. /*! \brief Get the number of entries in the profile counters section. */
  209. uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End);
  210. /*! \brief Get the size of the profile counters section in bytes. */
  211. uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End);
  212. /*! \brief Get the number of bytes in the profile bitmap section. */
  213. uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,
  214. const char *End);
  215. /*! \brief Get the size of the profile name section in bytes. */
  216. uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End);
  217. /* ! \brief Given the sizes of the data and counter information, return the
  218. * number of padding bytes before and after the counters, and after the names,
  219. * in the raw profile.
  220. *
  221. * Note: When mmap() mode is disabled, no padding bytes before/after counters
  222. * are needed. However, in mmap() mode, the counter section in the raw profile
  223. * must be page-aligned: this API computes the number of padding bytes
  224. * needed to achieve that.
  225. */
  226. void __llvm_profile_get_padding_sizes_for_counters(
  227. uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes,
  228. uint64_t NamesSize, uint64_t *PaddingBytesBeforeCounters,
  229. uint64_t *PaddingBytesAfterCounters, uint64_t *PaddingBytesAfterBitmap,
  230. uint64_t *PaddingBytesAfterNames);
  231. /*!
  232. * \brief Set the flag that profile data has been dumped to the file.
  233. * This is useful for users to disable dumping profile data to the file for
  234. * certain processes in case the processes don't have permission to write to
  235. * the disks, and trying to do so would result in side effects such as crashes.
  236. */
  237. void __llvm_profile_set_dumped();
  238. /*!
  239. * This variable is defined in InstrProfilingRuntime.cpp as a hidden
  240. * symbol. Its main purpose is to enable profile runtime user to
  241. * bypass runtime initialization code -- if the client code explicitly
  242. * define this variable, then InstProfileRuntime.o won't be linked in.
  243. * Note that this variable's visibility needs to be hidden so that the
  244. * definition of this variable in an instrumented shared library won't
  245. * affect runtime initialization decision of the main program.
  246. * __llvm_profile_profile_runtime. */
  247. COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR;
  248. /*!
  249. * This variable is defined in InstrProfilingVersionVar.c as a hidden symbol
  250. * (except on Apple platforms where this symbol is checked by TAPI). Its main
  251. * purpose is to encode the raw profile version value and other format related
  252. * information such as whether the profile is from IR based instrumentation. The
  253. * variable is defined as weak so that compiler can emit an overriding
  254. * definition depending on user option.
  255. */
  256. extern uint64_t INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */
  257. /*!
  258. * This variable is a weak symbol defined in InstrProfiling.c. It allows
  259. * compiler instrumentation to provide overriding definition with value
  260. * from compiler command line. This variable has default visibility.
  261. */
  262. extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */
  263. #endif /* PROFILE_INSTRPROFILING_H_ */