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 /*! 58e8d8bef9SDimitry Andric * \brief Set the page size. 59e8d8bef9SDimitry Andric * 60e8d8bef9SDimitry Andric * This is a pre-requisite for enabling continuous mode. The buffer size 61e8d8bef9SDimitry Andric * calculation code inside of libprofile cannot simply call getpagesize(), as 62e8d8bef9SDimitry Andric * it is not allowed to depend on libc. 63e8d8bef9SDimitry Andric */ 64e8d8bef9SDimitry Andric void __llvm_profile_set_page_size(unsigned PageSize); 65e8d8bef9SDimitry Andric 66e8d8bef9SDimitry Andric /*! 670b57cec5SDimitry Andric * \brief Get number of bytes necessary to pad the argument to eight 680b57cec5SDimitry Andric * byte boundary. 690b57cec5SDimitry Andric */ 700b57cec5SDimitry Andric uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes); 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric /*! 730b57cec5SDimitry Andric * \brief Get required size for profile buffer. 740b57cec5SDimitry Andric */ 750b57cec5SDimitry Andric uint64_t __llvm_profile_get_size_for_buffer(void); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric /*! 780b57cec5SDimitry Andric * \brief Write instrumentation data to the given buffer. 790b57cec5SDimitry Andric * 800b57cec5SDimitry Andric * \pre \c Buffer is the start of a buffer at least as big as \a 810b57cec5SDimitry Andric * __llvm_profile_get_size_for_buffer(). 820b57cec5SDimitry Andric */ 830b57cec5SDimitry Andric int __llvm_profile_write_buffer(char *Buffer); 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_begin_data(void); 860b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_end_data(void); 870b57cec5SDimitry Andric const char *__llvm_profile_begin_names(void); 880b57cec5SDimitry Andric const char *__llvm_profile_end_names(void); 890b57cec5SDimitry Andric uint64_t *__llvm_profile_begin_counters(void); 900b57cec5SDimitry Andric uint64_t *__llvm_profile_end_counters(void); 910b57cec5SDimitry Andric ValueProfNode *__llvm_profile_begin_vnodes(); 920b57cec5SDimitry Andric ValueProfNode *__llvm_profile_end_vnodes(); 930b57cec5SDimitry Andric uint32_t *__llvm_profile_begin_orderfile(); 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric /*! 960b57cec5SDimitry Andric * \brief Clear profile counters to zero. 970b57cec5SDimitry Andric * 980b57cec5SDimitry Andric */ 990b57cec5SDimitry Andric void __llvm_profile_reset_counters(void); 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric /*! 1020b57cec5SDimitry Andric * \brief Merge profile data from buffer. 1030b57cec5SDimitry Andric * 104*fe6060f1SDimitry Andric * Read profile data form buffer \p Profile and merge with in-process profile 105*fe6060f1SDimitry Andric * counters. The client is expected to have checked or already knows the profile 106*fe6060f1SDimitry Andric * data in the buffer matches the in-process counter structure before calling 107*fe6060f1SDimitry Andric * it. Returns 0 (success) if the profile data is valid. Upon reading 108*fe6060f1SDimitry Andric * invalid/corrupted profile data, returns 1 (failure). 1090b57cec5SDimitry Andric */ 110*fe6060f1SDimitry Andric int __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size); 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric /*! \brief Check if profile in buffer matches the current binary. 1130b57cec5SDimitry Andric * 1140b57cec5SDimitry Andric * Returns 0 (success) if the profile data in buffer \p Profile with size 1150b57cec5SDimitry Andric * \p Size was generated by the same binary and therefore matches 1160b57cec5SDimitry Andric * structurally the in-process counters. If the profile data in buffer is 1170b57cec5SDimitry Andric * not compatible, the interface returns 1 (failure). 1180b57cec5SDimitry Andric */ 1190b57cec5SDimitry Andric int __llvm_profile_check_compatibility(const char *Profile, 1200b57cec5SDimitry Andric uint64_t Size); 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric /*! 1230b57cec5SDimitry Andric * \brief Counts the number of times a target value is seen. 1240b57cec5SDimitry Andric * 1250b57cec5SDimitry Andric * Records the target value for the CounterIndex if not seen before. Otherwise, 1260b57cec5SDimitry Andric * increments the counter associated w/ the target value. 1270b57cec5SDimitry Andric * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data, 1280b57cec5SDimitry Andric * uint32_t CounterIndex); 1290b57cec5SDimitry Andric */ 1300b57cec5SDimitry Andric void INSTR_PROF_VALUE_PROF_FUNC( 1310b57cec5SDimitry Andric #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName 132480093f4SDimitry Andric #include "profile/InstrProfData.inc" 1330b57cec5SDimitry Andric ); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data, 1360b57cec5SDimitry Andric uint32_t CounterIndex, 1370b57cec5SDimitry Andric uint64_t CounterValue); 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric /*! 1400b57cec5SDimitry Andric * \brief Write instrumentation data to the current file. 1410b57cec5SDimitry Andric * 1420b57cec5SDimitry Andric * Writes to the file with the last name given to \a * 1430b57cec5SDimitry Andric * __llvm_profile_set_filename(), 1440b57cec5SDimitry Andric * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable, 1450b57cec5SDimitry Andric * or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR, 1460b57cec5SDimitry Andric * or if that's not set, \c "default.profraw". 1470b57cec5SDimitry Andric */ 1480b57cec5SDimitry Andric int __llvm_profile_write_file(void); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric int __llvm_orderfile_write_file(void); 1510b57cec5SDimitry Andric /*! 1520b57cec5SDimitry Andric * \brief this is a wrapper interface to \c __llvm_profile_write_file. 1530b57cec5SDimitry Andric * After this interface is invoked, a arleady dumped flag will be set 1540b57cec5SDimitry Andric * so that profile won't be dumped again during program exit. 1550b57cec5SDimitry Andric * Invocation of interface __llvm_profile_reset_counters will clear 1560b57cec5SDimitry Andric * the flag. This interface is designed to be used to collect profile 1570b57cec5SDimitry Andric * data from user selected hot regions. The use model is 1580b57cec5SDimitry Andric * __llvm_profile_reset_counters(); 1590b57cec5SDimitry Andric * ... hot region 1 1600b57cec5SDimitry Andric * __llvm_profile_dump(); 1610b57cec5SDimitry Andric * .. some other code 1620b57cec5SDimitry Andric * __llvm_profile_reset_counters(); 1630b57cec5SDimitry Andric * ... hot region 2 1640b57cec5SDimitry Andric * __llvm_profile_dump(); 1650b57cec5SDimitry Andric * 1660b57cec5SDimitry Andric * It is expected that on-line profile merging is on with \c %m specifier 1670b57cec5SDimitry Andric * used in profile filename . If merging is not turned on, user is expected 1680b57cec5SDimitry Andric * to invoke __llvm_profile_set_filename to specify different profile names 1690b57cec5SDimitry Andric * for different regions before dumping to avoid profile write clobbering. 1700b57cec5SDimitry Andric */ 1710b57cec5SDimitry Andric int __llvm_profile_dump(void); 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric int __llvm_orderfile_dump(void); 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric /*! 1760b57cec5SDimitry Andric * \brief Set the filename for writing instrumentation data. 1770b57cec5SDimitry Andric * 1780b57cec5SDimitry Andric * Sets the filename to be used for subsequent calls to 1790b57cec5SDimitry Andric * \a __llvm_profile_write_file(). 1800b57cec5SDimitry Andric * 1810b57cec5SDimitry Andric * \c Name is not copied, so it must remain valid. Passing NULL resets the 1820b57cec5SDimitry Andric * filename logic to the default behaviour. 18368d75effSDimitry Andric * 18468d75effSDimitry Andric * Note: There may be multiple copies of the profile runtime (one for each 18568d75effSDimitry Andric * instrumented image/DSO). This API only modifies the filename within the 18668d75effSDimitry Andric * copy of the runtime available to the calling image. 187480093f4SDimitry Andric * 188480093f4SDimitry Andric * Warning: This is a no-op if continuous mode (\ref 189480093f4SDimitry Andric * __llvm_profile_is_continuous_mode_enabled) is on. The reason for this is 190480093f4SDimitry Andric * that in continuous mode, profile counters are mmap()'d to the profile at 191480093f4SDimitry Andric * program initialization time. Support for transferring the mmap'd profile 192480093f4SDimitry Andric * counts to a new file has not been implemented. 1930b57cec5SDimitry Andric */ 1940b57cec5SDimitry Andric void __llvm_profile_set_filename(const char *Name); 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric /*! 1970b57cec5SDimitry Andric * \brief Set the FILE object for writing instrumentation data. 1980b57cec5SDimitry Andric * 1990b57cec5SDimitry Andric * Sets the FILE object to be used for subsequent calls to 2000b57cec5SDimitry Andric * \a __llvm_profile_write_file(). The profile file name set by environment 2010b57cec5SDimitry Andric * variable, command-line option, or calls to \a __llvm_profile_set_filename 2020b57cec5SDimitry Andric * will be ignored. 2030b57cec5SDimitry Andric * 2040b57cec5SDimitry Andric * \c File will not be closed after a call to \a __llvm_profile_write_file() but 2050b57cec5SDimitry Andric * it may be flushed. Passing NULL restores default behavior. 2060b57cec5SDimitry Andric * 2070b57cec5SDimitry Andric * If \c EnableMerge is nonzero, the runtime will always merge profiling data 2080b57cec5SDimitry Andric * with the contents of the profiling file. If EnableMerge is zero, the runtime 2090b57cec5SDimitry Andric * may still merge the data if it would have merged for another reason (for 2100b57cec5SDimitry Andric * example, because of a %m specifier in the file name). 21168d75effSDimitry Andric * 21268d75effSDimitry Andric * Note: There may be multiple copies of the profile runtime (one for each 21368d75effSDimitry Andric * instrumented image/DSO). This API only modifies the file object within the 21468d75effSDimitry Andric * copy of the runtime available to the calling image. 215480093f4SDimitry Andric * 216480093f4SDimitry Andric * Warning: This is a no-op if continuous mode (\ref 217480093f4SDimitry Andric * __llvm_profile_is_continuous_mode_enabled) is on. The reason for this is 218480093f4SDimitry Andric * that in continuous mode, profile counters are mmap()'d to the profile at 219480093f4SDimitry Andric * program initialization time. Support for transferring the mmap'd profile 220480093f4SDimitry Andric * counts to a new file has not been implemented. 2210b57cec5SDimitry Andric */ 2220b57cec5SDimitry Andric void __llvm_profile_set_file_object(FILE *File, int EnableMerge); 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric /*! \brief Register to write instrumentation data to file at exit. */ 2250b57cec5SDimitry Andric int __llvm_profile_register_write_file_atexit(void); 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric /*! \brief Initialize file handling. */ 2280b57cec5SDimitry Andric void __llvm_profile_initialize_file(void); 2290b57cec5SDimitry Andric 2305ffd83dbSDimitry Andric /*! \brief Initialize the profile runtime. */ 2315ffd83dbSDimitry Andric void __llvm_profile_initialize(void); 2325ffd83dbSDimitry Andric 2330b57cec5SDimitry Andric /*! 2340b57cec5SDimitry Andric * \brief Return path prefix (excluding the base filename) of the profile data. 2350b57cec5SDimitry Andric * This is useful for users using \c -fprofile-generate=./path_prefix who do 2360b57cec5SDimitry Andric * not care about the default raw profile name. It is also useful to collect 2370b57cec5SDimitry Andric * more than more profile data files dumped in the same directory (Online 2380b57cec5SDimitry Andric * merge mode is turned on for instrumented programs with shared libs). 2390b57cec5SDimitry Andric * Side-effect: this API call will invoke malloc with dynamic memory allocation. 2400b57cec5SDimitry Andric */ 2410b57cec5SDimitry Andric const char *__llvm_profile_get_path_prefix(); 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric /*! 2440b57cec5SDimitry Andric * \brief Return filename (including path) of the profile data. Note that if the 2450b57cec5SDimitry Andric * user calls __llvm_profile_set_filename later after invoking this interface, 2460b57cec5SDimitry Andric * the actual file name may differ from what is returned here. 24768d75effSDimitry Andric * Side-effect: this API call will invoke malloc with dynamic memory allocation 24868d75effSDimitry Andric * (the returned pointer must be passed to `free` to avoid a leak). 24968d75effSDimitry Andric * 25068d75effSDimitry Andric * Note: There may be multiple copies of the profile runtime (one for each 25168d75effSDimitry Andric * instrumented image/DSO). This API only retrieves the filename from the copy 25268d75effSDimitry Andric * of the runtime available to the calling image. 2530b57cec5SDimitry Andric */ 2540b57cec5SDimitry Andric const char *__llvm_profile_get_filename(); 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric /*! \brief Get the magic token for the file format. */ 2570b57cec5SDimitry Andric uint64_t __llvm_profile_get_magic(void); 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric /*! \brief Get the version of the file format. */ 2600b57cec5SDimitry Andric uint64_t __llvm_profile_get_version(void); 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric /*! \brief Get the number of entries in the profile data section. */ 2630b57cec5SDimitry Andric uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin, 2640b57cec5SDimitry Andric const __llvm_profile_data *End); 2650b57cec5SDimitry Andric 266480093f4SDimitry Andric /* ! \brief Given the sizes of the data and counter information, return the 267480093f4SDimitry Andric * number of padding bytes before and after the counters, and after the names, 268480093f4SDimitry Andric * in the raw profile. 269480093f4SDimitry Andric * 270480093f4SDimitry Andric * Note: In this context, "size" means "number of entries", i.e. the first two 271480093f4SDimitry Andric * arguments must be the result of __llvm_profile_get_data_size() and of 272480093f4SDimitry Andric * (__llvm_profile_end_counters() - __llvm_profile_begin_counters()) resp. 273480093f4SDimitry Andric * 274480093f4SDimitry Andric * Note: When mmap() mode is disabled, no padding bytes before/after counters 275480093f4SDimitry Andric * are needed. However, in mmap() mode, the counter section in the raw profile 276480093f4SDimitry Andric * must be page-aligned: this API computes the number of padding bytes 277480093f4SDimitry Andric * needed to achieve that. 278480093f4SDimitry Andric */ 279480093f4SDimitry Andric void __llvm_profile_get_padding_sizes_for_counters( 280480093f4SDimitry Andric uint64_t DataSize, uint64_t CountersSize, uint64_t NamesSize, 281480093f4SDimitry Andric uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters, 282480093f4SDimitry Andric uint64_t *PaddingBytesAfterNames); 283480093f4SDimitry Andric 2840b57cec5SDimitry Andric /*! 2850b57cec5SDimitry Andric * \brief Set the flag that profile data has been dumped to the file. 2860b57cec5SDimitry Andric * This is useful for users to disable dumping profile data to the file for 2870b57cec5SDimitry Andric * certain processes in case the processes don't have permission to write to 2880b57cec5SDimitry Andric * the disks, and trying to do so would result in side effects such as crashes. 2890b57cec5SDimitry Andric */ 2900b57cec5SDimitry Andric void __llvm_profile_set_dumped(); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric /*! 29368d75effSDimitry Andric * This variable is defined in InstrProfilingRuntime.cpp as a hidden 2940b57cec5SDimitry Andric * symbol. Its main purpose is to enable profile runtime user to 2950b57cec5SDimitry Andric * bypass runtime initialization code -- if the client code explicitly 2960b57cec5SDimitry Andric * define this variable, then InstProfileRuntime.o won't be linked in. 2970b57cec5SDimitry Andric * Note that this variable's visibility needs to be hidden so that the 2980b57cec5SDimitry Andric * definition of this variable in an instrumented shared library won't 2990b57cec5SDimitry Andric * affect runtime initialization decision of the main program. 3000b57cec5SDimitry Andric * __llvm_profile_profile_runtime. */ 3010b57cec5SDimitry Andric COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR; 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric /*! 3040b57cec5SDimitry Andric * This variable is defined in InstrProfiling.c. Its main purpose is to 3050b57cec5SDimitry Andric * encode the raw profile version value and other format related information 3060b57cec5SDimitry Andric * such as whether the profile is from IR based instrumentation. The variable 3070b57cec5SDimitry Andric * is defined as weak so that compiler can emit an overriding definition 3080b57cec5SDimitry Andric * depending on user option. Since we don't support mixing FE and IR based 3090b57cec5SDimitry Andric * data in the same raw profile data file (in other words, shared libs and 3100b57cec5SDimitry Andric * main program are expected to be instrumented in the same way), there is 3110b57cec5SDimitry Andric * no need for this variable to be hidden. 3120b57cec5SDimitry Andric */ 3130b57cec5SDimitry Andric extern uint64_t INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */ 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric /*! 3160b57cec5SDimitry Andric * This variable is a weak symbol defined in InstrProfiling.c. It allows 3170b57cec5SDimitry Andric * compiler instrumentation to provide overriding definition with value 3180b57cec5SDimitry Andric * from compiler command line. This variable has default visibility. 3190b57cec5SDimitry Andric */ 3200b57cec5SDimitry Andric extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */ 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric #endif /* PROFILE_INSTRPROFILING_H_ */ 323