xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfiling.h (revision 68d75eff68281c1b445e3010bb975eae07aac225)
10b57cec5SDimitry Andric /*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef PROFILE_INSTRPROFILING_H_
100b57cec5SDimitry Andric #define PROFILE_INSTRPROFILING_H_
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "InstrProfilingPort.h"
130b57cec5SDimitry Andric #include <stdio.h>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY
160b57cec5SDimitry Andric #include "InstrProfData.inc"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric enum ValueKind {
190b57cec5SDimitry Andric #define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
200b57cec5SDimitry Andric #include "InstrProfData.inc"
210b57cec5SDimitry Andric };
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric typedef void *IntPtrT;
240b57cec5SDimitry Andric typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
250b57cec5SDimitry Andric     __llvm_profile_data {
260b57cec5SDimitry Andric #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;
270b57cec5SDimitry Andric #include "InstrProfData.inc"
280b57cec5SDimitry Andric } __llvm_profile_data;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric typedef struct __llvm_profile_header {
310b57cec5SDimitry Andric #define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;
320b57cec5SDimitry Andric #include "InstrProfData.inc"
330b57cec5SDimitry Andric } __llvm_profile_header;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric typedef struct ValueProfNode * PtrToNodeT;
360b57cec5SDimitry Andric typedef struct ValueProfNode {
370b57cec5SDimitry Andric #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name;
380b57cec5SDimitry Andric #include "InstrProfData.inc"
390b57cec5SDimitry Andric } ValueProfNode;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric /*!
420b57cec5SDimitry Andric  * \brief Get number of bytes necessary to pad the argument to eight
430b57cec5SDimitry Andric  * byte boundary.
440b57cec5SDimitry Andric  */
450b57cec5SDimitry Andric uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric /*!
480b57cec5SDimitry Andric  * \brief Get required size for profile buffer.
490b57cec5SDimitry Andric  */
500b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer(void);
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric /*!
530b57cec5SDimitry Andric  * \brief Write instrumentation data to the given buffer.
540b57cec5SDimitry Andric  *
550b57cec5SDimitry Andric  * \pre \c Buffer is the start of a buffer at least as big as \a
560b57cec5SDimitry Andric  * __llvm_profile_get_size_for_buffer().
570b57cec5SDimitry Andric  */
580b57cec5SDimitry Andric int __llvm_profile_write_buffer(char *Buffer);
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_begin_data(void);
610b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_end_data(void);
620b57cec5SDimitry Andric const char *__llvm_profile_begin_names(void);
630b57cec5SDimitry Andric const char *__llvm_profile_end_names(void);
640b57cec5SDimitry Andric uint64_t *__llvm_profile_begin_counters(void);
650b57cec5SDimitry Andric uint64_t *__llvm_profile_end_counters(void);
660b57cec5SDimitry Andric ValueProfNode *__llvm_profile_begin_vnodes();
670b57cec5SDimitry Andric ValueProfNode *__llvm_profile_end_vnodes();
680b57cec5SDimitry Andric uint32_t *__llvm_profile_begin_orderfile();
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric /*!
710b57cec5SDimitry Andric  * \brief Clear profile counters to zero.
720b57cec5SDimitry Andric  *
730b57cec5SDimitry Andric  */
740b57cec5SDimitry Andric void __llvm_profile_reset_counters(void);
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric /*!
770b57cec5SDimitry Andric  * \brief Merge profile data from buffer.
780b57cec5SDimitry Andric  *
790b57cec5SDimitry Andric  * Read profile data form buffer \p Profile  and merge with
800b57cec5SDimitry Andric  * in-process profile counters. The client is expected to
810b57cec5SDimitry Andric  * have checked or already knows the profile data in the
820b57cec5SDimitry Andric  * buffer matches the in-process counter structure before
830b57cec5SDimitry Andric  * calling it.
840b57cec5SDimitry Andric  */
850b57cec5SDimitry Andric void __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size);
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric /*! \brief Check if profile in buffer matches the current binary.
880b57cec5SDimitry Andric  *
890b57cec5SDimitry Andric  *  Returns 0 (success) if the profile data in buffer \p Profile with size
900b57cec5SDimitry Andric  *  \p Size was generated by the same binary and therefore matches
910b57cec5SDimitry Andric  *  structurally the in-process counters. If the profile data in buffer is
920b57cec5SDimitry Andric  *  not compatible, the interface returns 1 (failure).
930b57cec5SDimitry Andric  */
940b57cec5SDimitry Andric int __llvm_profile_check_compatibility(const char *Profile,
950b57cec5SDimitry Andric                                        uint64_t Size);
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric /*!
980b57cec5SDimitry Andric  * \brief Counts the number of times a target value is seen.
990b57cec5SDimitry Andric  *
1000b57cec5SDimitry Andric  * Records the target value for the CounterIndex if not seen before. Otherwise,
1010b57cec5SDimitry Andric  * increments the counter associated w/ the target value.
1020b57cec5SDimitry Andric  * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
1030b57cec5SDimitry Andric  *                                       uint32_t CounterIndex);
1040b57cec5SDimitry Andric  */
1050b57cec5SDimitry Andric void INSTR_PROF_VALUE_PROF_FUNC(
1060b57cec5SDimitry Andric #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName
1070b57cec5SDimitry Andric #include "InstrProfData.inc"
1080b57cec5SDimitry Andric     );
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,
1110b57cec5SDimitry Andric                                             uint32_t CounterIndex,
1120b57cec5SDimitry Andric                                             uint64_t CounterValue);
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric /*!
1150b57cec5SDimitry Andric  * \brief Write instrumentation data to the current file.
1160b57cec5SDimitry Andric  *
1170b57cec5SDimitry Andric  * Writes to the file with the last name given to \a *
1180b57cec5SDimitry Andric  * __llvm_profile_set_filename(),
1190b57cec5SDimitry Andric  * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
1200b57cec5SDimitry Andric  * or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR,
1210b57cec5SDimitry Andric  * or if that's not set,  \c "default.profraw".
1220b57cec5SDimitry Andric  */
1230b57cec5SDimitry Andric int __llvm_profile_write_file(void);
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric int __llvm_orderfile_write_file(void);
1260b57cec5SDimitry Andric /*!
1270b57cec5SDimitry Andric  * \brief this is a wrapper interface to \c __llvm_profile_write_file.
1280b57cec5SDimitry Andric  * After this interface is invoked, a arleady dumped flag will be set
1290b57cec5SDimitry Andric  * so that profile won't be dumped again during program exit.
1300b57cec5SDimitry Andric  * Invocation of interface __llvm_profile_reset_counters will clear
1310b57cec5SDimitry Andric  * the flag. This interface is designed to be used to collect profile
1320b57cec5SDimitry Andric  * data from user selected hot regions. The use model is
1330b57cec5SDimitry Andric  *      __llvm_profile_reset_counters();
1340b57cec5SDimitry Andric  *      ... hot region 1
1350b57cec5SDimitry Andric  *      __llvm_profile_dump();
1360b57cec5SDimitry Andric  *      .. some other code
1370b57cec5SDimitry Andric  *      __llvm_profile_reset_counters();
1380b57cec5SDimitry Andric  *       ... hot region 2
1390b57cec5SDimitry Andric  *      __llvm_profile_dump();
1400b57cec5SDimitry Andric  *
1410b57cec5SDimitry Andric  *  It is expected that on-line profile merging is on with \c %m specifier
1420b57cec5SDimitry Andric  *  used in profile filename . If merging is  not turned on, user is expected
1430b57cec5SDimitry Andric  *  to invoke __llvm_profile_set_filename  to specify different profile names
1440b57cec5SDimitry Andric  *  for different regions before dumping to avoid profile write clobbering.
1450b57cec5SDimitry Andric  */
1460b57cec5SDimitry Andric int __llvm_profile_dump(void);
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric int __llvm_orderfile_dump(void);
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric /*!
1510b57cec5SDimitry Andric  * \brief Set the filename for writing instrumentation data.
1520b57cec5SDimitry Andric  *
1530b57cec5SDimitry Andric  * Sets the filename to be used for subsequent calls to
1540b57cec5SDimitry Andric  * \a __llvm_profile_write_file().
1550b57cec5SDimitry Andric  *
1560b57cec5SDimitry Andric  * \c Name is not copied, so it must remain valid.  Passing NULL resets the
1570b57cec5SDimitry Andric  * filename logic to the default behaviour.
158*68d75effSDimitry Andric  *
159*68d75effSDimitry Andric  * Note: There may be multiple copies of the profile runtime (one for each
160*68d75effSDimitry Andric  * instrumented image/DSO). This API only modifies the filename within the
161*68d75effSDimitry Andric  * copy of the runtime available to the calling image.
1620b57cec5SDimitry Andric  */
1630b57cec5SDimitry Andric void __llvm_profile_set_filename(const char *Name);
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric /*!
1660b57cec5SDimitry Andric  * \brief Set the FILE object for writing instrumentation data.
1670b57cec5SDimitry Andric  *
1680b57cec5SDimitry Andric  * Sets the FILE object to be used for subsequent calls to
1690b57cec5SDimitry Andric  * \a __llvm_profile_write_file(). The profile file name set by environment
1700b57cec5SDimitry Andric  * variable, command-line option, or calls to \a  __llvm_profile_set_filename
1710b57cec5SDimitry Andric  * will be ignored.
1720b57cec5SDimitry Andric  *
1730b57cec5SDimitry Andric  * \c File will not be closed after a call to \a __llvm_profile_write_file() but
1740b57cec5SDimitry Andric  * it may be flushed. Passing NULL restores default behavior.
1750b57cec5SDimitry Andric  *
1760b57cec5SDimitry Andric  * If \c EnableMerge is nonzero, the runtime will always merge profiling data
1770b57cec5SDimitry Andric  * with the contents of the profiling file. If EnableMerge is zero, the runtime
1780b57cec5SDimitry Andric  * may still merge the data if it would have merged for another reason (for
1790b57cec5SDimitry Andric  * example, because of a %m specifier in the file name).
180*68d75effSDimitry Andric  *
181*68d75effSDimitry Andric  * Note: There may be multiple copies of the profile runtime (one for each
182*68d75effSDimitry Andric  * instrumented image/DSO). This API only modifies the file object within the
183*68d75effSDimitry Andric  * copy of the runtime available to the calling image.
1840b57cec5SDimitry Andric  */
1850b57cec5SDimitry Andric void __llvm_profile_set_file_object(FILE *File, int EnableMerge);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric /*! \brief Register to write instrumentation data to file at exit. */
1880b57cec5SDimitry Andric int __llvm_profile_register_write_file_atexit(void);
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric /*! \brief Initialize file handling. */
1910b57cec5SDimitry Andric void __llvm_profile_initialize_file(void);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric /*!
1940b57cec5SDimitry Andric  * \brief Return path prefix (excluding the base filename) of the profile data.
1950b57cec5SDimitry Andric  * This is useful for users using \c -fprofile-generate=./path_prefix who do
1960b57cec5SDimitry Andric  * not care about the default raw profile name. It is also useful to collect
1970b57cec5SDimitry Andric  * more than more profile data files dumped in the same directory (Online
1980b57cec5SDimitry Andric  * merge mode is turned on for instrumented programs with shared libs).
1990b57cec5SDimitry Andric  * Side-effect: this API call will invoke malloc with dynamic memory allocation.
2000b57cec5SDimitry Andric  */
2010b57cec5SDimitry Andric const char *__llvm_profile_get_path_prefix();
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric /*!
2040b57cec5SDimitry Andric  * \brief Return filename (including path) of the profile data. Note that if the
2050b57cec5SDimitry Andric  * user calls __llvm_profile_set_filename later after invoking this interface,
2060b57cec5SDimitry Andric  * the actual file name may differ from what is returned here.
207*68d75effSDimitry Andric  * Side-effect: this API call will invoke malloc with dynamic memory allocation
208*68d75effSDimitry Andric  * (the returned pointer must be passed to `free` to avoid a leak).
209*68d75effSDimitry Andric  *
210*68d75effSDimitry Andric  * Note: There may be multiple copies of the profile runtime (one for each
211*68d75effSDimitry Andric  * instrumented image/DSO). This API only retrieves the filename from the copy
212*68d75effSDimitry Andric  * of the runtime available to the calling image.
2130b57cec5SDimitry Andric  */
2140b57cec5SDimitry Andric const char *__llvm_profile_get_filename();
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric /*! \brief Get the magic token for the file format. */
2170b57cec5SDimitry Andric uint64_t __llvm_profile_get_magic(void);
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric /*! \brief Get the version of the file format. */
2200b57cec5SDimitry Andric uint64_t __llvm_profile_get_version(void);
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric /*! \brief Get the number of entries in the profile data section. */
2230b57cec5SDimitry Andric uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
2240b57cec5SDimitry Andric                                       const __llvm_profile_data *End);
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric /*!
2270b57cec5SDimitry Andric  * \brief Set the flag that profile data has been dumped to the file.
2280b57cec5SDimitry Andric  * This is useful for users to disable dumping profile data to the file for
2290b57cec5SDimitry Andric  * certain processes in case the processes don't have permission to write to
2300b57cec5SDimitry Andric  * the disks, and trying to do so would result in side effects such as crashes.
2310b57cec5SDimitry Andric  */
2320b57cec5SDimitry Andric void __llvm_profile_set_dumped();
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric /*!
235*68d75effSDimitry Andric  * This variable is defined in InstrProfilingRuntime.cpp as a hidden
2360b57cec5SDimitry Andric  * symbol. Its main purpose is to enable profile runtime user to
2370b57cec5SDimitry Andric  * bypass runtime initialization code -- if the client code explicitly
2380b57cec5SDimitry Andric  * define this variable, then InstProfileRuntime.o won't be linked in.
2390b57cec5SDimitry Andric  * Note that this variable's visibility needs to be hidden so that the
2400b57cec5SDimitry Andric  * definition of this variable in an instrumented shared library won't
2410b57cec5SDimitry Andric  * affect runtime initialization decision of the main program.
2420b57cec5SDimitry Andric  *  __llvm_profile_profile_runtime. */
2430b57cec5SDimitry Andric COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric /*!
2460b57cec5SDimitry Andric  * This variable is defined in InstrProfiling.c. Its main purpose is to
2470b57cec5SDimitry Andric  * encode the raw profile version value and other format related information
2480b57cec5SDimitry Andric  * such as whether the profile is from IR based instrumentation. The variable
2490b57cec5SDimitry Andric  * is defined as weak so that compiler can emit an overriding definition
2500b57cec5SDimitry Andric  * depending on user option.  Since we don't support mixing FE and IR based
2510b57cec5SDimitry Andric  * data in the same raw profile data file (in other words, shared libs and
2520b57cec5SDimitry Andric  * main program are expected to be instrumented in the same way), there is
2530b57cec5SDimitry Andric  * no need for this variable to be hidden.
2540b57cec5SDimitry Andric  */
2550b57cec5SDimitry Andric extern uint64_t INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric /*!
2580b57cec5SDimitry Andric  * This variable is a weak symbol defined in InstrProfiling.c. It allows
2590b57cec5SDimitry Andric  * compiler instrumentation to provide overriding definition with value
2600b57cec5SDimitry Andric  * from compiler command line. This variable has default visibility.
2610b57cec5SDimitry Andric  */
2620b57cec5SDimitry Andric extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric #endif /* PROFILE_INSTRPROFILING_H_ */
265