xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfiling.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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
16480093f4SDimitry Andric #include "profile/InstrProfData.inc"
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric enum ValueKind {
190b57cec5SDimitry Andric #define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
20480093f4SDimitry Andric #include "profile/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;
27480093f4SDimitry Andric #include "profile/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;
32480093f4SDimitry Andric #include "profile/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;
38480093f4SDimitry Andric #include "profile/InstrProfData.inc"
390b57cec5SDimitry Andric } ValueProfNode;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric /*!
42480093f4SDimitry Andric  * \brief Return 1 if profile counters are continuously synced to the raw
43480093f4SDimitry Andric  * profile via an mmap(). This is in contrast to the default mode, in which
44480093f4SDimitry Andric  * the raw profile is written out at program exit time.
45480093f4SDimitry Andric  */
46480093f4SDimitry Andric int __llvm_profile_is_continuous_mode_enabled(void);
47480093f4SDimitry Andric 
48480093f4SDimitry Andric /*!
49480093f4SDimitry Andric  * \brief Enable continuous mode.
50480093f4SDimitry Andric  *
51480093f4SDimitry Andric  * See \ref __llvm_profile_is_continuous_mode_enabled. The behavior is undefined
52480093f4SDimitry Andric  * if continuous mode is already enabled, or if it cannot be enable due to
53480093f4SDimitry Andric  * conflicting options.
54480093f4SDimitry Andric  */
55480093f4SDimitry Andric void __llvm_profile_enable_continuous_mode(void);
56480093f4SDimitry Andric 
57480093f4SDimitry Andric /*!
580b57cec5SDimitry Andric  * \brief Get number of bytes necessary to pad the argument to eight
590b57cec5SDimitry Andric  * byte boundary.
600b57cec5SDimitry Andric  */
610b57cec5SDimitry Andric uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric /*!
640b57cec5SDimitry Andric  * \brief Get required size for profile buffer.
650b57cec5SDimitry Andric  */
660b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer(void);
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric /*!
690b57cec5SDimitry Andric  * \brief Write instrumentation data to the given buffer.
700b57cec5SDimitry Andric  *
710b57cec5SDimitry Andric  * \pre \c Buffer is the start of a buffer at least as big as \a
720b57cec5SDimitry Andric  * __llvm_profile_get_size_for_buffer().
730b57cec5SDimitry Andric  */
740b57cec5SDimitry Andric int __llvm_profile_write_buffer(char *Buffer);
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_begin_data(void);
770b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_end_data(void);
780b57cec5SDimitry Andric const char *__llvm_profile_begin_names(void);
790b57cec5SDimitry Andric const char *__llvm_profile_end_names(void);
800b57cec5SDimitry Andric uint64_t *__llvm_profile_begin_counters(void);
810b57cec5SDimitry Andric uint64_t *__llvm_profile_end_counters(void);
820b57cec5SDimitry Andric ValueProfNode *__llvm_profile_begin_vnodes();
830b57cec5SDimitry Andric ValueProfNode *__llvm_profile_end_vnodes();
840b57cec5SDimitry Andric uint32_t *__llvm_profile_begin_orderfile();
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric /*!
870b57cec5SDimitry Andric  * \brief Clear profile counters to zero.
880b57cec5SDimitry Andric  *
890b57cec5SDimitry Andric  */
900b57cec5SDimitry Andric void __llvm_profile_reset_counters(void);
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric /*!
930b57cec5SDimitry Andric  * \brief Merge profile data from buffer.
940b57cec5SDimitry Andric  *
950b57cec5SDimitry Andric  * Read profile data form buffer \p Profile  and merge with
960b57cec5SDimitry Andric  * in-process profile counters. The client is expected to
970b57cec5SDimitry Andric  * have checked or already knows the profile data in the
980b57cec5SDimitry Andric  * buffer matches the in-process counter structure before
990b57cec5SDimitry Andric  * calling it.
1000b57cec5SDimitry Andric  */
1010b57cec5SDimitry Andric void __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size);
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric /*! \brief Check if profile in buffer matches the current binary.
1040b57cec5SDimitry Andric  *
1050b57cec5SDimitry Andric  *  Returns 0 (success) if the profile data in buffer \p Profile with size
1060b57cec5SDimitry Andric  *  \p Size was generated by the same binary and therefore matches
1070b57cec5SDimitry Andric  *  structurally the in-process counters. If the profile data in buffer is
1080b57cec5SDimitry Andric  *  not compatible, the interface returns 1 (failure).
1090b57cec5SDimitry Andric  */
1100b57cec5SDimitry Andric int __llvm_profile_check_compatibility(const char *Profile,
1110b57cec5SDimitry Andric                                        uint64_t Size);
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric /*!
1140b57cec5SDimitry Andric  * \brief Counts the number of times a target value is seen.
1150b57cec5SDimitry Andric  *
1160b57cec5SDimitry Andric  * Records the target value for the CounterIndex if not seen before. Otherwise,
1170b57cec5SDimitry Andric  * increments the counter associated w/ the target value.
1180b57cec5SDimitry Andric  * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
1190b57cec5SDimitry Andric  *                                       uint32_t CounterIndex);
1200b57cec5SDimitry Andric  */
1210b57cec5SDimitry Andric void INSTR_PROF_VALUE_PROF_FUNC(
1220b57cec5SDimitry Andric #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName
123480093f4SDimitry Andric #include "profile/InstrProfData.inc"
1240b57cec5SDimitry Andric     );
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,
1270b57cec5SDimitry Andric                                             uint32_t CounterIndex,
1280b57cec5SDimitry Andric                                             uint64_t CounterValue);
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric /*!
1310b57cec5SDimitry Andric  * \brief Write instrumentation data to the current file.
1320b57cec5SDimitry Andric  *
1330b57cec5SDimitry Andric  * Writes to the file with the last name given to \a *
1340b57cec5SDimitry Andric  * __llvm_profile_set_filename(),
1350b57cec5SDimitry Andric  * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
1360b57cec5SDimitry Andric  * or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR,
1370b57cec5SDimitry Andric  * or if that's not set,  \c "default.profraw".
1380b57cec5SDimitry Andric  */
1390b57cec5SDimitry Andric int __llvm_profile_write_file(void);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric int __llvm_orderfile_write_file(void);
1420b57cec5SDimitry Andric /*!
1430b57cec5SDimitry Andric  * \brief this is a wrapper interface to \c __llvm_profile_write_file.
1440b57cec5SDimitry Andric  * After this interface is invoked, a arleady dumped flag will be set
1450b57cec5SDimitry Andric  * so that profile won't be dumped again during program exit.
1460b57cec5SDimitry Andric  * Invocation of interface __llvm_profile_reset_counters will clear
1470b57cec5SDimitry Andric  * the flag. This interface is designed to be used to collect profile
1480b57cec5SDimitry Andric  * data from user selected hot regions. The use model is
1490b57cec5SDimitry Andric  *      __llvm_profile_reset_counters();
1500b57cec5SDimitry Andric  *      ... hot region 1
1510b57cec5SDimitry Andric  *      __llvm_profile_dump();
1520b57cec5SDimitry Andric  *      .. some other code
1530b57cec5SDimitry Andric  *      __llvm_profile_reset_counters();
1540b57cec5SDimitry Andric  *       ... hot region 2
1550b57cec5SDimitry Andric  *      __llvm_profile_dump();
1560b57cec5SDimitry Andric  *
1570b57cec5SDimitry Andric  *  It is expected that on-line profile merging is on with \c %m specifier
1580b57cec5SDimitry Andric  *  used in profile filename . If merging is  not turned on, user is expected
1590b57cec5SDimitry Andric  *  to invoke __llvm_profile_set_filename  to specify different profile names
1600b57cec5SDimitry Andric  *  for different regions before dumping to avoid profile write clobbering.
1610b57cec5SDimitry Andric  */
1620b57cec5SDimitry Andric int __llvm_profile_dump(void);
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric int __llvm_orderfile_dump(void);
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric /*!
1670b57cec5SDimitry Andric  * \brief Set the filename for writing instrumentation data.
1680b57cec5SDimitry Andric  *
1690b57cec5SDimitry Andric  * Sets the filename to be used for subsequent calls to
1700b57cec5SDimitry Andric  * \a __llvm_profile_write_file().
1710b57cec5SDimitry Andric  *
1720b57cec5SDimitry Andric  * \c Name is not copied, so it must remain valid.  Passing NULL resets the
1730b57cec5SDimitry Andric  * filename logic to the default behaviour.
17468d75effSDimitry Andric  *
17568d75effSDimitry Andric  * Note: There may be multiple copies of the profile runtime (one for each
17668d75effSDimitry Andric  * instrumented image/DSO). This API only modifies the filename within the
17768d75effSDimitry Andric  * copy of the runtime available to the calling image.
178480093f4SDimitry Andric  *
179480093f4SDimitry Andric  * Warning: This is a no-op if continuous mode (\ref
180480093f4SDimitry Andric  * __llvm_profile_is_continuous_mode_enabled) is on. The reason for this is
181480093f4SDimitry Andric  * that in continuous mode, profile counters are mmap()'d to the profile at
182480093f4SDimitry Andric  * program initialization time. Support for transferring the mmap'd profile
183480093f4SDimitry Andric  * counts to a new file has not been implemented.
1840b57cec5SDimitry Andric  */
1850b57cec5SDimitry Andric void __llvm_profile_set_filename(const char *Name);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric /*!
1880b57cec5SDimitry Andric  * \brief Set the FILE object for writing instrumentation data.
1890b57cec5SDimitry Andric  *
1900b57cec5SDimitry Andric  * Sets the FILE object to be used for subsequent calls to
1910b57cec5SDimitry Andric  * \a __llvm_profile_write_file(). The profile file name set by environment
1920b57cec5SDimitry Andric  * variable, command-line option, or calls to \a  __llvm_profile_set_filename
1930b57cec5SDimitry Andric  * will be ignored.
1940b57cec5SDimitry Andric  *
1950b57cec5SDimitry Andric  * \c File will not be closed after a call to \a __llvm_profile_write_file() but
1960b57cec5SDimitry Andric  * it may be flushed. Passing NULL restores default behavior.
1970b57cec5SDimitry Andric  *
1980b57cec5SDimitry Andric  * If \c EnableMerge is nonzero, the runtime will always merge profiling data
1990b57cec5SDimitry Andric  * with the contents of the profiling file. If EnableMerge is zero, the runtime
2000b57cec5SDimitry Andric  * may still merge the data if it would have merged for another reason (for
2010b57cec5SDimitry Andric  * example, because of a %m specifier in the file name).
20268d75effSDimitry Andric  *
20368d75effSDimitry Andric  * Note: There may be multiple copies of the profile runtime (one for each
20468d75effSDimitry Andric  * instrumented image/DSO). This API only modifies the file object within the
20568d75effSDimitry Andric  * copy of the runtime available to the calling image.
206480093f4SDimitry Andric  *
207480093f4SDimitry Andric  * Warning: This is a no-op if continuous mode (\ref
208480093f4SDimitry Andric  * __llvm_profile_is_continuous_mode_enabled) is on. The reason for this is
209480093f4SDimitry Andric  * that in continuous mode, profile counters are mmap()'d to the profile at
210480093f4SDimitry Andric  * program initialization time. Support for transferring the mmap'd profile
211480093f4SDimitry Andric  * counts to a new file has not been implemented.
2120b57cec5SDimitry Andric  */
2130b57cec5SDimitry Andric void __llvm_profile_set_file_object(FILE *File, int EnableMerge);
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric /*! \brief Register to write instrumentation data to file at exit. */
2160b57cec5SDimitry Andric int __llvm_profile_register_write_file_atexit(void);
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric /*! \brief Initialize file handling. */
2190b57cec5SDimitry Andric void __llvm_profile_initialize_file(void);
2200b57cec5SDimitry Andric 
221*5ffd83dbSDimitry Andric /*! \brief Initialize the profile runtime. */
222*5ffd83dbSDimitry Andric void __llvm_profile_initialize(void);
223*5ffd83dbSDimitry Andric 
2240b57cec5SDimitry Andric /*!
2250b57cec5SDimitry Andric  * \brief Return path prefix (excluding the base filename) of the profile data.
2260b57cec5SDimitry Andric  * This is useful for users using \c -fprofile-generate=./path_prefix who do
2270b57cec5SDimitry Andric  * not care about the default raw profile name. It is also useful to collect
2280b57cec5SDimitry Andric  * more than more profile data files dumped in the same directory (Online
2290b57cec5SDimitry Andric  * merge mode is turned on for instrumented programs with shared libs).
2300b57cec5SDimitry Andric  * Side-effect: this API call will invoke malloc with dynamic memory allocation.
2310b57cec5SDimitry Andric  */
2320b57cec5SDimitry Andric const char *__llvm_profile_get_path_prefix();
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric /*!
2350b57cec5SDimitry Andric  * \brief Return filename (including path) of the profile data. Note that if the
2360b57cec5SDimitry Andric  * user calls __llvm_profile_set_filename later after invoking this interface,
2370b57cec5SDimitry Andric  * the actual file name may differ from what is returned here.
23868d75effSDimitry Andric  * Side-effect: this API call will invoke malloc with dynamic memory allocation
23968d75effSDimitry Andric  * (the returned pointer must be passed to `free` to avoid a leak).
24068d75effSDimitry Andric  *
24168d75effSDimitry Andric  * Note: There may be multiple copies of the profile runtime (one for each
24268d75effSDimitry Andric  * instrumented image/DSO). This API only retrieves the filename from the copy
24368d75effSDimitry Andric  * of the runtime available to the calling image.
2440b57cec5SDimitry Andric  */
2450b57cec5SDimitry Andric const char *__llvm_profile_get_filename();
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric /*! \brief Get the magic token for the file format. */
2480b57cec5SDimitry Andric uint64_t __llvm_profile_get_magic(void);
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric /*! \brief Get the version of the file format. */
2510b57cec5SDimitry Andric uint64_t __llvm_profile_get_version(void);
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric /*! \brief Get the number of entries in the profile data section. */
2540b57cec5SDimitry Andric uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
2550b57cec5SDimitry Andric                                       const __llvm_profile_data *End);
2560b57cec5SDimitry Andric 
257480093f4SDimitry Andric /* ! \brief Given the sizes of the data and counter information, return the
258480093f4SDimitry Andric  * number of padding bytes before and after the counters, and after the names,
259480093f4SDimitry Andric  * in the raw profile.
260480093f4SDimitry Andric  *
261480093f4SDimitry Andric  * Note: In this context, "size" means "number of entries", i.e. the first two
262480093f4SDimitry Andric  * arguments must be the result of __llvm_profile_get_data_size() and of
263480093f4SDimitry Andric  * (__llvm_profile_end_counters() - __llvm_profile_begin_counters()) resp.
264480093f4SDimitry Andric  *
265480093f4SDimitry Andric  * Note: When mmap() mode is disabled, no padding bytes before/after counters
266480093f4SDimitry Andric  * are needed. However, in mmap() mode, the counter section in the raw profile
267480093f4SDimitry Andric  * must be page-aligned: this API computes the number of padding bytes
268480093f4SDimitry Andric  * needed to achieve that.
269480093f4SDimitry Andric  */
270480093f4SDimitry Andric void __llvm_profile_get_padding_sizes_for_counters(
271480093f4SDimitry Andric     uint64_t DataSize, uint64_t CountersSize, uint64_t NamesSize,
272480093f4SDimitry Andric     uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
273480093f4SDimitry Andric     uint64_t *PaddingBytesAfterNames);
274480093f4SDimitry Andric 
2750b57cec5SDimitry Andric /*!
2760b57cec5SDimitry Andric  * \brief Set the flag that profile data has been dumped to the file.
2770b57cec5SDimitry Andric  * This is useful for users to disable dumping profile data to the file for
2780b57cec5SDimitry Andric  * certain processes in case the processes don't have permission to write to
2790b57cec5SDimitry Andric  * the disks, and trying to do so would result in side effects such as crashes.
2800b57cec5SDimitry Andric  */
2810b57cec5SDimitry Andric void __llvm_profile_set_dumped();
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric /*!
28468d75effSDimitry Andric  * This variable is defined in InstrProfilingRuntime.cpp as a hidden
2850b57cec5SDimitry Andric  * symbol. Its main purpose is to enable profile runtime user to
2860b57cec5SDimitry Andric  * bypass runtime initialization code -- if the client code explicitly
2870b57cec5SDimitry Andric  * define this variable, then InstProfileRuntime.o won't be linked in.
2880b57cec5SDimitry Andric  * Note that this variable's visibility needs to be hidden so that the
2890b57cec5SDimitry Andric  * definition of this variable in an instrumented shared library won't
2900b57cec5SDimitry Andric  * affect runtime initialization decision of the main program.
2910b57cec5SDimitry Andric  *  __llvm_profile_profile_runtime. */
2920b57cec5SDimitry Andric COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR;
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric /*!
2950b57cec5SDimitry Andric  * This variable is defined in InstrProfiling.c. Its main purpose is to
2960b57cec5SDimitry Andric  * encode the raw profile version value and other format related information
2970b57cec5SDimitry Andric  * such as whether the profile is from IR based instrumentation. The variable
2980b57cec5SDimitry Andric  * is defined as weak so that compiler can emit an overriding definition
2990b57cec5SDimitry Andric  * depending on user option.  Since we don't support mixing FE and IR based
3000b57cec5SDimitry Andric  * data in the same raw profile data file (in other words, shared libs and
3010b57cec5SDimitry Andric  * main program are expected to be instrumented in the same way), there is
3020b57cec5SDimitry Andric  * no need for this variable to be hidden.
3030b57cec5SDimitry Andric  */
3040b57cec5SDimitry Andric extern uint64_t INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric /*!
3070b57cec5SDimitry Andric  * This variable is a weak symbol defined in InstrProfiling.c. It allows
3080b57cec5SDimitry Andric  * compiler instrumentation to provide overriding definition with value
3090b57cec5SDimitry Andric  * from compiler command line. This variable has default visibility.
3100b57cec5SDimitry Andric  */
3110b57cec5SDimitry Andric extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */
3120b57cec5SDimitry Andric 
313*5ffd83dbSDimitry Andric /*!
314*5ffd83dbSDimitry Andric  * This variable is a weak symbol defined in InstrProfilingBiasVar.c. It
315*5ffd83dbSDimitry Andric  * allows compiler instrumentation to provide overriding definition with
316*5ffd83dbSDimitry Andric  * value from compiler command line. This variable has hidden visibility.
317*5ffd83dbSDimitry Andric  */
318*5ffd83dbSDimitry Andric COMPILER_RT_VISIBILITY extern intptr_t __llvm_profile_counter_bias;
319*5ffd83dbSDimitry Andric 
3200b57cec5SDimitry Andric #endif /* PROFILE_INSTRPROFILING_H_ */
321