xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfiling.h (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
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 
15*7a6dacacSDimitry Andric // Make sure __LLVM_INSTR_PROFILE_GENERATE is always defined before
16*7a6dacacSDimitry Andric // including instr_prof_interface.h so the interface functions are
17*7a6dacacSDimitry Andric // declared correctly for the runtime.
18*7a6dacacSDimitry Andric // __LLVM_INSTR_PROFILE_GENERATE is always `#undef`ed after the header,
19*7a6dacacSDimitry Andric // because compiler-rt does not support profiling the profiling runtime itself.
20*7a6dacacSDimitry Andric #ifndef __LLVM_INSTR_PROFILE_GENERATE
21*7a6dacacSDimitry Andric #define __LLVM_INSTR_PROFILE_GENERATE
22*7a6dacacSDimitry Andric #endif
23*7a6dacacSDimitry Andric #include "profile/instr_prof_interface.h"
24*7a6dacacSDimitry Andric #undef __LLVM_INSTR_PROFILE_GENERATE
25*7a6dacacSDimitry Andric 
260b57cec5SDimitry Andric #define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY
27480093f4SDimitry Andric #include "profile/InstrProfData.inc"
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric enum ValueKind {
300b57cec5SDimitry Andric #define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
31480093f4SDimitry Andric #include "profile/InstrProfData.inc"
320b57cec5SDimitry Andric };
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric typedef void *IntPtrT;
350b57cec5SDimitry Andric typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
360b57cec5SDimitry Andric     __llvm_profile_data {
370b57cec5SDimitry Andric #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;
38480093f4SDimitry Andric #include "profile/InstrProfData.inc"
390b57cec5SDimitry Andric } __llvm_profile_data;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric typedef struct __llvm_profile_header {
420b57cec5SDimitry Andric #define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;
43480093f4SDimitry Andric #include "profile/InstrProfData.inc"
440b57cec5SDimitry Andric } __llvm_profile_header;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric typedef struct ValueProfNode * PtrToNodeT;
470b57cec5SDimitry Andric typedef struct ValueProfNode {
480b57cec5SDimitry Andric #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name;
49480093f4SDimitry Andric #include "profile/InstrProfData.inc"
500b57cec5SDimitry Andric } ValueProfNode;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric /*!
53480093f4SDimitry Andric  * \brief Return 1 if profile counters are continuously synced to the raw
54480093f4SDimitry Andric  * profile via an mmap(). This is in contrast to the default mode, in which
55480093f4SDimitry Andric  * the raw profile is written out at program exit time.
56480093f4SDimitry Andric  */
57480093f4SDimitry Andric int __llvm_profile_is_continuous_mode_enabled(void);
58480093f4SDimitry Andric 
59480093f4SDimitry Andric /*!
60480093f4SDimitry Andric  * \brief Enable continuous mode.
61480093f4SDimitry Andric  *
62480093f4SDimitry Andric  * See \ref __llvm_profile_is_continuous_mode_enabled. The behavior is undefined
63480093f4SDimitry Andric  * if continuous mode is already enabled, or if it cannot be enable due to
64480093f4SDimitry Andric  * conflicting options.
65480093f4SDimitry Andric  */
66480093f4SDimitry Andric void __llvm_profile_enable_continuous_mode(void);
67480093f4SDimitry Andric 
68480093f4SDimitry Andric /*!
695f757f3fSDimitry Andric  * \brief Disable continuous mode.
705f757f3fSDimitry Andric  *
715f757f3fSDimitry Andric  */
725f757f3fSDimitry Andric void __llvm_profile_disable_continuous_mode(void);
735f757f3fSDimitry Andric 
745f757f3fSDimitry Andric /*!
75e8d8bef9SDimitry Andric  * \brief Set the page size.
76e8d8bef9SDimitry Andric  *
77e8d8bef9SDimitry Andric  * This is a pre-requisite for enabling continuous mode. The buffer size
78e8d8bef9SDimitry Andric  * calculation code inside of libprofile cannot simply call getpagesize(), as
79e8d8bef9SDimitry Andric  * it is not allowed to depend on libc.
80e8d8bef9SDimitry Andric  */
81e8d8bef9SDimitry Andric void __llvm_profile_set_page_size(unsigned PageSize);
82e8d8bef9SDimitry Andric 
83e8d8bef9SDimitry Andric /*!
840b57cec5SDimitry Andric  * \brief Get number of bytes necessary to pad the argument to eight
850b57cec5SDimitry Andric  * byte boundary.
860b57cec5SDimitry Andric  */
870b57cec5SDimitry Andric uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric /*!
900b57cec5SDimitry Andric  * \brief Get required size for profile buffer.
910b57cec5SDimitry Andric  */
920b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer(void);
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric /*!
950b57cec5SDimitry Andric  * \brief Write instrumentation data to the given buffer.
960b57cec5SDimitry Andric  *
970b57cec5SDimitry Andric  * \pre \c Buffer is the start of a buffer at least as big as \a
980b57cec5SDimitry Andric  * __llvm_profile_get_size_for_buffer().
990b57cec5SDimitry Andric  */
1000b57cec5SDimitry Andric int __llvm_profile_write_buffer(char *Buffer);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_begin_data(void);
1030b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_end_data(void);
1040b57cec5SDimitry Andric const char *__llvm_profile_begin_names(void);
1050b57cec5SDimitry Andric const char *__llvm_profile_end_names(void);
10604eeddc0SDimitry Andric char *__llvm_profile_begin_counters(void);
10704eeddc0SDimitry Andric char *__llvm_profile_end_counters(void);
1085f757f3fSDimitry Andric char *__llvm_profile_begin_bitmap(void);
1095f757f3fSDimitry Andric char *__llvm_profile_end_bitmap(void);
1100b57cec5SDimitry Andric ValueProfNode *__llvm_profile_begin_vnodes();
1110b57cec5SDimitry Andric ValueProfNode *__llvm_profile_end_vnodes();
1120b57cec5SDimitry Andric uint32_t *__llvm_profile_begin_orderfile();
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric /*!
1150b57cec5SDimitry Andric  * \brief Merge profile data from buffer.
1160b57cec5SDimitry Andric  *
1175f757f3fSDimitry Andric  * Read profile data from buffer \p Profile and merge with in-process profile
1185f757f3fSDimitry Andric  * counters and bitmaps. The client is expected to have checked or already
1195f757f3fSDimitry Andric  * know the profile data in the buffer matches the in-process counter
1205f757f3fSDimitry Andric  * structure before calling it. Returns 0 (success) if the profile data is
1215f757f3fSDimitry Andric  * valid. Upon reading invalid/corrupted profile data, returns 1 (failure).
1220b57cec5SDimitry Andric  */
123fe6060f1SDimitry Andric int __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size);
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric /*! \brief Check if profile in buffer matches the current binary.
1260b57cec5SDimitry Andric  *
1270b57cec5SDimitry Andric  *  Returns 0 (success) if the profile data in buffer \p Profile with size
1280b57cec5SDimitry Andric  *  \p Size was generated by the same binary and therefore matches
1295f757f3fSDimitry Andric  *  structurally the in-process counters and bitmaps. If the profile data in
1305f757f3fSDimitry Andric  *  buffer is not compatible, the interface returns 1 (failure).
1310b57cec5SDimitry Andric  */
1320b57cec5SDimitry Andric int __llvm_profile_check_compatibility(const char *Profile,
1330b57cec5SDimitry Andric                                        uint64_t Size);
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric /*!
1360b57cec5SDimitry Andric  * \brief Counts the number of times a target value is seen.
1370b57cec5SDimitry Andric  *
1380b57cec5SDimitry Andric  * Records the target value for the CounterIndex if not seen before. Otherwise,
1390b57cec5SDimitry Andric  * increments the counter associated w/ the target value.
1400b57cec5SDimitry Andric  * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
1410b57cec5SDimitry Andric  *                                       uint32_t CounterIndex);
1420b57cec5SDimitry Andric  */
1430b57cec5SDimitry Andric void INSTR_PROF_VALUE_PROF_FUNC(
1440b57cec5SDimitry Andric #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName
145480093f4SDimitry Andric #include "profile/InstrProfData.inc"
1460b57cec5SDimitry Andric     );
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,
1490b57cec5SDimitry Andric                                             uint32_t CounterIndex,
1500b57cec5SDimitry Andric                                             uint64_t CounterValue);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric /*!
1530b57cec5SDimitry Andric  * \brief Write instrumentation data to the current file.
1540b57cec5SDimitry Andric  *
1550b57cec5SDimitry Andric  * Writes to the file with the last name given to \a *
1560b57cec5SDimitry Andric  * __llvm_profile_set_filename(),
1570b57cec5SDimitry Andric  * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
1580b57cec5SDimitry Andric  * or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR,
1590b57cec5SDimitry Andric  * or if that's not set,  \c "default.profraw".
1600b57cec5SDimitry Andric  */
1610b57cec5SDimitry Andric int __llvm_profile_write_file(void);
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric int __llvm_orderfile_write_file(void);
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric /*!
166349cc55cSDimitry Andric  * \brief Set the FILE object for writing instrumentation data. Return 0 if set
167349cc55cSDimitry Andric  * successfully or return 1 if failed.
1680b57cec5SDimitry Andric  *
1690b57cec5SDimitry Andric  * Sets the FILE object to be used for subsequent calls to
1700b57cec5SDimitry Andric  * \a __llvm_profile_write_file(). The profile file name set by environment
1710b57cec5SDimitry Andric  * variable, command-line option, or calls to \a  __llvm_profile_set_filename
1720b57cec5SDimitry Andric  * will be ignored.
1730b57cec5SDimitry Andric  *
1740b57cec5SDimitry Andric  * \c File will not be closed after a call to \a __llvm_profile_write_file() but
1750b57cec5SDimitry Andric  * it may be flushed. Passing NULL restores default behavior.
1760b57cec5SDimitry Andric  *
1770b57cec5SDimitry Andric  * If \c EnableMerge is nonzero, the runtime will always merge profiling data
1780b57cec5SDimitry Andric  * with the contents of the profiling file. If EnableMerge is zero, the runtime
1790b57cec5SDimitry Andric  * may still merge the data if it would have merged for another reason (for
1800b57cec5SDimitry Andric  * example, because of a %m specifier in the file name).
18168d75effSDimitry Andric  *
18268d75effSDimitry Andric  * Note: There may be multiple copies of the profile runtime (one for each
18368d75effSDimitry Andric  * instrumented image/DSO). This API only modifies the file object within the
18468d75effSDimitry Andric  * copy of the runtime available to the calling image.
185480093f4SDimitry Andric  *
186349cc55cSDimitry Andric  * Warning: This is a no-op if EnableMerge is 0 in continuous mode (\ref
187349cc55cSDimitry Andric  * __llvm_profile_is_continuous_mode_enabled), because disable merging requires
188349cc55cSDimitry Andric  * copying the old profile file to new profile file and this function is usually
189349cc55cSDimitry Andric  * used when the proess doesn't have permission to open file.
1900b57cec5SDimitry Andric  */
191349cc55cSDimitry Andric int __llvm_profile_set_file_object(FILE *File, int EnableMerge);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric /*! \brief Register to write instrumentation data to file at exit. */
1940b57cec5SDimitry Andric int __llvm_profile_register_write_file_atexit(void);
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric /*! \brief Initialize file handling. */
1970b57cec5SDimitry Andric void __llvm_profile_initialize_file(void);
1980b57cec5SDimitry Andric 
1995ffd83dbSDimitry Andric /*! \brief Initialize the profile runtime. */
2005ffd83dbSDimitry Andric void __llvm_profile_initialize(void);
2015ffd83dbSDimitry Andric 
2020b57cec5SDimitry Andric /*!
2030b57cec5SDimitry Andric  * \brief Return path prefix (excluding the base filename) of the profile data.
2040b57cec5SDimitry Andric  * This is useful for users using \c -fprofile-generate=./path_prefix who do
2050b57cec5SDimitry Andric  * not care about the default raw profile name. It is also useful to collect
2060b57cec5SDimitry Andric  * more than more profile data files dumped in the same directory (Online
2070b57cec5SDimitry Andric  * merge mode is turned on for instrumented programs with shared libs).
2080b57cec5SDimitry Andric  * Side-effect: this API call will invoke malloc with dynamic memory allocation.
2090b57cec5SDimitry Andric  */
2100b57cec5SDimitry Andric const char *__llvm_profile_get_path_prefix();
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric /*!
2130b57cec5SDimitry Andric  * \brief Return filename (including path) of the profile data. Note that if the
2140b57cec5SDimitry Andric  * user calls __llvm_profile_set_filename later after invoking this interface,
2150b57cec5SDimitry Andric  * the actual file name may differ from what is returned here.
21668d75effSDimitry Andric  * Side-effect: this API call will invoke malloc with dynamic memory allocation
21768d75effSDimitry Andric  * (the returned pointer must be passed to `free` to avoid a leak).
21868d75effSDimitry Andric  *
21968d75effSDimitry Andric  * Note: There may be multiple copies of the profile runtime (one for each
22068d75effSDimitry Andric  * instrumented image/DSO). This API only retrieves the filename from the copy
22168d75effSDimitry Andric  * of the runtime available to the calling image.
2220b57cec5SDimitry Andric  */
2230b57cec5SDimitry Andric const char *__llvm_profile_get_filename();
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric /*! \brief Get the magic token for the file format. */
2260b57cec5SDimitry Andric uint64_t __llvm_profile_get_magic(void);
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric /*! \brief Get the version of the file format. */
2290b57cec5SDimitry Andric uint64_t __llvm_profile_get_version(void);
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric /*! \brief Get the number of entries in the profile data section. */
23204eeddc0SDimitry Andric uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
23304eeddc0SDimitry Andric                                      const __llvm_profile_data *End);
23404eeddc0SDimitry Andric 
23504eeddc0SDimitry Andric /*! \brief Get the size of the profile data section in bytes. */
2360b57cec5SDimitry Andric uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
2370b57cec5SDimitry Andric                                       const __llvm_profile_data *End);
2380b57cec5SDimitry Andric 
23904eeddc0SDimitry Andric /*! \brief Get the size in bytes of a single counter entry. */
24004eeddc0SDimitry Andric size_t __llvm_profile_counter_entry_size(void);
24104eeddc0SDimitry Andric 
24204eeddc0SDimitry Andric /*! \brief Get the number of entries in the profile counters section. */
24304eeddc0SDimitry Andric uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End);
24404eeddc0SDimitry Andric 
24504eeddc0SDimitry Andric /*! \brief Get the size of the profile counters section in bytes. */
24604eeddc0SDimitry Andric uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End);
24704eeddc0SDimitry Andric 
2485f757f3fSDimitry Andric /*! \brief Get the number of bytes in the profile bitmap section. */
2495f757f3fSDimitry Andric uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,
2505f757f3fSDimitry Andric                                              const char *End);
2515f757f3fSDimitry Andric 
2525f757f3fSDimitry Andric /*! \brief Get the size of the profile name section in bytes. */
2535f757f3fSDimitry Andric uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End);
2545f757f3fSDimitry Andric 
255480093f4SDimitry Andric /* ! \brief Given the sizes of the data and counter information, return the
256480093f4SDimitry Andric  * number of padding bytes before and after the counters, and after the names,
257480093f4SDimitry Andric  * in the raw profile.
258480093f4SDimitry Andric  *
259480093f4SDimitry Andric  * Note: When mmap() mode is disabled, no padding bytes before/after counters
260480093f4SDimitry Andric  * are needed. However, in mmap() mode, the counter section in the raw profile
261480093f4SDimitry Andric  * must be page-aligned: this API computes the number of padding bytes
262480093f4SDimitry Andric  * needed to achieve that.
263480093f4SDimitry Andric  */
264480093f4SDimitry Andric void __llvm_profile_get_padding_sizes_for_counters(
2655f757f3fSDimitry Andric     uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes,
2665f757f3fSDimitry Andric     uint64_t NamesSize, uint64_t *PaddingBytesBeforeCounters,
2675f757f3fSDimitry Andric     uint64_t *PaddingBytesAfterCounters, uint64_t *PaddingBytesAfterBitmap,
268480093f4SDimitry Andric     uint64_t *PaddingBytesAfterNames);
269480093f4SDimitry Andric 
2700b57cec5SDimitry Andric /*!
2710b57cec5SDimitry Andric  * \brief Set the flag that profile data has been dumped to the file.
2720b57cec5SDimitry Andric  * This is useful for users to disable dumping profile data to the file for
2730b57cec5SDimitry Andric  * certain processes in case the processes don't have permission to write to
2740b57cec5SDimitry Andric  * the disks, and trying to do so would result in side effects such as crashes.
2750b57cec5SDimitry Andric  */
2760b57cec5SDimitry Andric void __llvm_profile_set_dumped();
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric /*!
27968d75effSDimitry Andric  * This variable is defined in InstrProfilingRuntime.cpp as a hidden
2800b57cec5SDimitry Andric  * symbol. Its main purpose is to enable profile runtime user to
2810b57cec5SDimitry Andric  * bypass runtime initialization code -- if the client code explicitly
2820b57cec5SDimitry Andric  * define this variable, then InstProfileRuntime.o won't be linked in.
2830b57cec5SDimitry Andric  * Note that this variable's visibility needs to be hidden so that the
2840b57cec5SDimitry Andric  * definition of this variable in an instrumented shared library won't
2850b57cec5SDimitry Andric  * affect runtime initialization decision of the main program.
2860b57cec5SDimitry Andric  *  __llvm_profile_profile_runtime. */
2870b57cec5SDimitry Andric COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR;
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric /*!
290349cc55cSDimitry Andric  * This variable is defined in InstrProfilingVersionVar.c as a hidden symbol
291349cc55cSDimitry Andric  * (except on Apple platforms where this symbol is checked by TAPI).  Its main
292349cc55cSDimitry Andric  * purpose is to encode the raw profile version value and other format related
293349cc55cSDimitry Andric  * information such as whether the profile is from IR based instrumentation. The
294349cc55cSDimitry Andric  * variable is defined as weak so that compiler can emit an overriding
295349cc55cSDimitry Andric  * definition depending on user option.
2960b57cec5SDimitry Andric  */
2970b57cec5SDimitry Andric extern uint64_t INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric /*!
3000b57cec5SDimitry Andric  * This variable is a weak symbol defined in InstrProfiling.c. It allows
3010b57cec5SDimitry Andric  * compiler instrumentation to provide overriding definition with value
3020b57cec5SDimitry Andric  * from compiler command line. This variable has default visibility.
3030b57cec5SDimitry Andric  */
3040b57cec5SDimitry Andric extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric #endif /* PROFILE_INSTRPROFILING_H_ */
307