13cab2bb3Spatrick /*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\ 23cab2bb3Spatrick |* 33cab2bb3Spatrick |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 43cab2bb3Spatrick |* See https://llvm.org/LICENSE.txt for license information. 53cab2bb3Spatrick |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 63cab2bb3Spatrick |* 73cab2bb3Spatrick \*===----------------------------------------------------------------------===*/ 83cab2bb3Spatrick 93cab2bb3Spatrick #ifndef PROFILE_INSTRPROFILING_H_ 103cab2bb3Spatrick #define PROFILE_INSTRPROFILING_H_ 113cab2bb3Spatrick 123cab2bb3Spatrick #include "InstrProfilingPort.h" 133cab2bb3Spatrick #include <stdio.h> 143cab2bb3Spatrick 153cab2bb3Spatrick #define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY 163cab2bb3Spatrick #include "profile/InstrProfData.inc" 173cab2bb3Spatrick 183cab2bb3Spatrick enum ValueKind { 193cab2bb3Spatrick #define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value, 203cab2bb3Spatrick #include "profile/InstrProfData.inc" 213cab2bb3Spatrick }; 223cab2bb3Spatrick 233cab2bb3Spatrick typedef void *IntPtrT; 243cab2bb3Spatrick typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT) 253cab2bb3Spatrick __llvm_profile_data { 263cab2bb3Spatrick #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name; 273cab2bb3Spatrick #include "profile/InstrProfData.inc" 283cab2bb3Spatrick } __llvm_profile_data; 293cab2bb3Spatrick 303cab2bb3Spatrick typedef struct __llvm_profile_header { 313cab2bb3Spatrick #define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name; 323cab2bb3Spatrick #include "profile/InstrProfData.inc" 333cab2bb3Spatrick } __llvm_profile_header; 343cab2bb3Spatrick 353cab2bb3Spatrick typedef struct ValueProfNode * PtrToNodeT; 363cab2bb3Spatrick typedef struct ValueProfNode { 373cab2bb3Spatrick #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name; 383cab2bb3Spatrick #include "profile/InstrProfData.inc" 393cab2bb3Spatrick } ValueProfNode; 403cab2bb3Spatrick 413cab2bb3Spatrick /*! 423cab2bb3Spatrick * \brief Return 1 if profile counters are continuously synced to the raw 433cab2bb3Spatrick * profile via an mmap(). This is in contrast to the default mode, in which 443cab2bb3Spatrick * the raw profile is written out at program exit time. 453cab2bb3Spatrick */ 463cab2bb3Spatrick int __llvm_profile_is_continuous_mode_enabled(void); 473cab2bb3Spatrick 483cab2bb3Spatrick /*! 493cab2bb3Spatrick * \brief Enable continuous mode. 503cab2bb3Spatrick * 513cab2bb3Spatrick * See \ref __llvm_profile_is_continuous_mode_enabled. The behavior is undefined 523cab2bb3Spatrick * if continuous mode is already enabled, or if it cannot be enable due to 533cab2bb3Spatrick * conflicting options. 543cab2bb3Spatrick */ 553cab2bb3Spatrick void __llvm_profile_enable_continuous_mode(void); 563cab2bb3Spatrick 573cab2bb3Spatrick /*! 58d89ec533Spatrick * \brief Set the page size. 59d89ec533Spatrick * 60d89ec533Spatrick * This is a pre-requisite for enabling continuous mode. The buffer size 61d89ec533Spatrick * calculation code inside of libprofile cannot simply call getpagesize(), as 62d89ec533Spatrick * it is not allowed to depend on libc. 63d89ec533Spatrick */ 64d89ec533Spatrick void __llvm_profile_set_page_size(unsigned PageSize); 65d89ec533Spatrick 66d89ec533Spatrick /*! 673cab2bb3Spatrick * \brief Get number of bytes necessary to pad the argument to eight 683cab2bb3Spatrick * byte boundary. 693cab2bb3Spatrick */ 703cab2bb3Spatrick uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes); 713cab2bb3Spatrick 723cab2bb3Spatrick /*! 733cab2bb3Spatrick * \brief Get required size for profile buffer. 743cab2bb3Spatrick */ 753cab2bb3Spatrick uint64_t __llvm_profile_get_size_for_buffer(void); 763cab2bb3Spatrick 773cab2bb3Spatrick /*! 783cab2bb3Spatrick * \brief Write instrumentation data to the given buffer. 793cab2bb3Spatrick * 803cab2bb3Spatrick * \pre \c Buffer is the start of a buffer at least as big as \a 813cab2bb3Spatrick * __llvm_profile_get_size_for_buffer(). 823cab2bb3Spatrick */ 833cab2bb3Spatrick int __llvm_profile_write_buffer(char *Buffer); 843cab2bb3Spatrick 853cab2bb3Spatrick const __llvm_profile_data *__llvm_profile_begin_data(void); 863cab2bb3Spatrick const __llvm_profile_data *__llvm_profile_end_data(void); 873cab2bb3Spatrick const char *__llvm_profile_begin_names(void); 883cab2bb3Spatrick const char *__llvm_profile_end_names(void); 89*810390e3Srobert char *__llvm_profile_begin_counters(void); 90*810390e3Srobert char *__llvm_profile_end_counters(void); 913cab2bb3Spatrick ValueProfNode *__llvm_profile_begin_vnodes(); 923cab2bb3Spatrick ValueProfNode *__llvm_profile_end_vnodes(); 933cab2bb3Spatrick uint32_t *__llvm_profile_begin_orderfile(); 943cab2bb3Spatrick 953cab2bb3Spatrick /*! 963cab2bb3Spatrick * \brief Clear profile counters to zero. 973cab2bb3Spatrick * 983cab2bb3Spatrick */ 993cab2bb3Spatrick void __llvm_profile_reset_counters(void); 1003cab2bb3Spatrick 1013cab2bb3Spatrick /*! 1023cab2bb3Spatrick * \brief Merge profile data from buffer. 1033cab2bb3Spatrick * 104d89ec533Spatrick * Read profile data form buffer \p Profile and merge with in-process profile 105d89ec533Spatrick * counters. The client is expected to have checked or already knows the profile 106d89ec533Spatrick * data in the buffer matches the in-process counter structure before calling 107d89ec533Spatrick * it. Returns 0 (success) if the profile data is valid. Upon reading 108d89ec533Spatrick * invalid/corrupted profile data, returns 1 (failure). 1093cab2bb3Spatrick */ 110d89ec533Spatrick int __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size); 1113cab2bb3Spatrick 1123cab2bb3Spatrick /*! \brief Check if profile in buffer matches the current binary. 1133cab2bb3Spatrick * 1143cab2bb3Spatrick * Returns 0 (success) if the profile data in buffer \p Profile with size 1153cab2bb3Spatrick * \p Size was generated by the same binary and therefore matches 1163cab2bb3Spatrick * structurally the in-process counters. If the profile data in buffer is 1173cab2bb3Spatrick * not compatible, the interface returns 1 (failure). 1183cab2bb3Spatrick */ 1193cab2bb3Spatrick int __llvm_profile_check_compatibility(const char *Profile, 1203cab2bb3Spatrick uint64_t Size); 1213cab2bb3Spatrick 1223cab2bb3Spatrick /*! 1233cab2bb3Spatrick * \brief Counts the number of times a target value is seen. 1243cab2bb3Spatrick * 1253cab2bb3Spatrick * Records the target value for the CounterIndex if not seen before. Otherwise, 1263cab2bb3Spatrick * increments the counter associated w/ the target value. 1273cab2bb3Spatrick * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data, 1283cab2bb3Spatrick * uint32_t CounterIndex); 1293cab2bb3Spatrick */ 1303cab2bb3Spatrick void INSTR_PROF_VALUE_PROF_FUNC( 1313cab2bb3Spatrick #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName 1323cab2bb3Spatrick #include "profile/InstrProfData.inc" 1333cab2bb3Spatrick ); 1343cab2bb3Spatrick 1353cab2bb3Spatrick void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data, 1363cab2bb3Spatrick uint32_t CounterIndex, 1373cab2bb3Spatrick uint64_t CounterValue); 1383cab2bb3Spatrick 1393cab2bb3Spatrick /*! 1403cab2bb3Spatrick * \brief Write instrumentation data to the current file. 1413cab2bb3Spatrick * 1423cab2bb3Spatrick * Writes to the file with the last name given to \a * 1433cab2bb3Spatrick * __llvm_profile_set_filename(), 1443cab2bb3Spatrick * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable, 1453cab2bb3Spatrick * or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR, 1463cab2bb3Spatrick * or if that's not set, \c "default.profraw". 1473cab2bb3Spatrick */ 1483cab2bb3Spatrick int __llvm_profile_write_file(void); 1493cab2bb3Spatrick 1503cab2bb3Spatrick int __llvm_orderfile_write_file(void); 1513cab2bb3Spatrick /*! 1523cab2bb3Spatrick * \brief this is a wrapper interface to \c __llvm_profile_write_file. 153*810390e3Srobert * After this interface is invoked, an already dumped flag will be set 1543cab2bb3Spatrick * so that profile won't be dumped again during program exit. 1553cab2bb3Spatrick * Invocation of interface __llvm_profile_reset_counters will clear 1563cab2bb3Spatrick * the flag. This interface is designed to be used to collect profile 1573cab2bb3Spatrick * data from user selected hot regions. The use model is 1583cab2bb3Spatrick * __llvm_profile_reset_counters(); 1593cab2bb3Spatrick * ... hot region 1 1603cab2bb3Spatrick * __llvm_profile_dump(); 1613cab2bb3Spatrick * .. some other code 1623cab2bb3Spatrick * __llvm_profile_reset_counters(); 1633cab2bb3Spatrick * ... hot region 2 1643cab2bb3Spatrick * __llvm_profile_dump(); 1653cab2bb3Spatrick * 1663cab2bb3Spatrick * It is expected that on-line profile merging is on with \c %m specifier 1673cab2bb3Spatrick * used in profile filename . If merging is not turned on, user is expected 1683cab2bb3Spatrick * to invoke __llvm_profile_set_filename to specify different profile names 1693cab2bb3Spatrick * for different regions before dumping to avoid profile write clobbering. 1703cab2bb3Spatrick */ 1713cab2bb3Spatrick int __llvm_profile_dump(void); 1723cab2bb3Spatrick 1733cab2bb3Spatrick int __llvm_orderfile_dump(void); 1743cab2bb3Spatrick 1753cab2bb3Spatrick /*! 1763cab2bb3Spatrick * \brief Set the filename for writing instrumentation data. 1773cab2bb3Spatrick * 1783cab2bb3Spatrick * Sets the filename to be used for subsequent calls to 1793cab2bb3Spatrick * \a __llvm_profile_write_file(). 1803cab2bb3Spatrick * 1813cab2bb3Spatrick * \c Name is not copied, so it must remain valid. Passing NULL resets the 1823cab2bb3Spatrick * filename logic to the default behaviour. 1833cab2bb3Spatrick * 1843cab2bb3Spatrick * Note: There may be multiple copies of the profile runtime (one for each 1853cab2bb3Spatrick * instrumented image/DSO). This API only modifies the filename within the 1863cab2bb3Spatrick * copy of the runtime available to the calling image. 1873cab2bb3Spatrick * 1883cab2bb3Spatrick * Warning: This is a no-op if continuous mode (\ref 1893cab2bb3Spatrick * __llvm_profile_is_continuous_mode_enabled) is on. The reason for this is 1903cab2bb3Spatrick * that in continuous mode, profile counters are mmap()'d to the profile at 1913cab2bb3Spatrick * program initialization time. Support for transferring the mmap'd profile 1923cab2bb3Spatrick * counts to a new file has not been implemented. 1933cab2bb3Spatrick */ 1943cab2bb3Spatrick void __llvm_profile_set_filename(const char *Name); 1953cab2bb3Spatrick 1963cab2bb3Spatrick /*! 197*810390e3Srobert * \brief Set the FILE object for writing instrumentation data. Return 0 if set 198*810390e3Srobert * successfully or return 1 if failed. 1993cab2bb3Spatrick * 2003cab2bb3Spatrick * Sets the FILE object to be used for subsequent calls to 2013cab2bb3Spatrick * \a __llvm_profile_write_file(). The profile file name set by environment 2023cab2bb3Spatrick * variable, command-line option, or calls to \a __llvm_profile_set_filename 2033cab2bb3Spatrick * will be ignored. 2043cab2bb3Spatrick * 2053cab2bb3Spatrick * \c File will not be closed after a call to \a __llvm_profile_write_file() but 2063cab2bb3Spatrick * it may be flushed. Passing NULL restores default behavior. 2073cab2bb3Spatrick * 2083cab2bb3Spatrick * If \c EnableMerge is nonzero, the runtime will always merge profiling data 2093cab2bb3Spatrick * with the contents of the profiling file. If EnableMerge is zero, the runtime 2103cab2bb3Spatrick * may still merge the data if it would have merged for another reason (for 2113cab2bb3Spatrick * example, because of a %m specifier in the file name). 2123cab2bb3Spatrick * 2133cab2bb3Spatrick * Note: There may be multiple copies of the profile runtime (one for each 2143cab2bb3Spatrick * instrumented image/DSO). This API only modifies the file object within the 2153cab2bb3Spatrick * copy of the runtime available to the calling image. 2163cab2bb3Spatrick * 217*810390e3Srobert * Warning: This is a no-op if EnableMerge is 0 in continuous mode (\ref 218*810390e3Srobert * __llvm_profile_is_continuous_mode_enabled), because disable merging requires 219*810390e3Srobert * copying the old profile file to new profile file and this function is usually 220*810390e3Srobert * used when the proess doesn't have permission to open file. 2213cab2bb3Spatrick */ 222*810390e3Srobert int __llvm_profile_set_file_object(FILE *File, int EnableMerge); 2233cab2bb3Spatrick 2243cab2bb3Spatrick /*! \brief Register to write instrumentation data to file at exit. */ 2253cab2bb3Spatrick int __llvm_profile_register_write_file_atexit(void); 2263cab2bb3Spatrick 2273cab2bb3Spatrick /*! \brief Initialize file handling. */ 2283cab2bb3Spatrick void __llvm_profile_initialize_file(void); 2293cab2bb3Spatrick 2301f9cb04fSpatrick /*! \brief Initialize the profile runtime. */ 2311f9cb04fSpatrick void __llvm_profile_initialize(void); 2321f9cb04fSpatrick 2333cab2bb3Spatrick /*! 2343cab2bb3Spatrick * \brief Return path prefix (excluding the base filename) of the profile data. 2353cab2bb3Spatrick * This is useful for users using \c -fprofile-generate=./path_prefix who do 2363cab2bb3Spatrick * not care about the default raw profile name. It is also useful to collect 2373cab2bb3Spatrick * more than more profile data files dumped in the same directory (Online 2383cab2bb3Spatrick * merge mode is turned on for instrumented programs with shared libs). 2393cab2bb3Spatrick * Side-effect: this API call will invoke malloc with dynamic memory allocation. 2403cab2bb3Spatrick */ 2413cab2bb3Spatrick const char *__llvm_profile_get_path_prefix(); 2423cab2bb3Spatrick 2433cab2bb3Spatrick /*! 2443cab2bb3Spatrick * \brief Return filename (including path) of the profile data. Note that if the 2453cab2bb3Spatrick * user calls __llvm_profile_set_filename later after invoking this interface, 2463cab2bb3Spatrick * the actual file name may differ from what is returned here. 2473cab2bb3Spatrick * Side-effect: this API call will invoke malloc with dynamic memory allocation 2483cab2bb3Spatrick * (the returned pointer must be passed to `free` to avoid a leak). 2493cab2bb3Spatrick * 2503cab2bb3Spatrick * Note: There may be multiple copies of the profile runtime (one for each 2513cab2bb3Spatrick * instrumented image/DSO). This API only retrieves the filename from the copy 2523cab2bb3Spatrick * of the runtime available to the calling image. 2533cab2bb3Spatrick */ 2543cab2bb3Spatrick const char *__llvm_profile_get_filename(); 2553cab2bb3Spatrick 2563cab2bb3Spatrick /*! \brief Get the magic token for the file format. */ 2573cab2bb3Spatrick uint64_t __llvm_profile_get_magic(void); 2583cab2bb3Spatrick 2593cab2bb3Spatrick /*! \brief Get the version of the file format. */ 2603cab2bb3Spatrick uint64_t __llvm_profile_get_version(void); 2613cab2bb3Spatrick 2623cab2bb3Spatrick /*! \brief Get the number of entries in the profile data section. */ 263*810390e3Srobert uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin, 264*810390e3Srobert const __llvm_profile_data *End); 265*810390e3Srobert 266*810390e3Srobert /*! \brief Get the size of the profile data section in bytes. */ 2673cab2bb3Spatrick uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin, 2683cab2bb3Spatrick const __llvm_profile_data *End); 2693cab2bb3Spatrick 270*810390e3Srobert /*! \brief Get the size in bytes of a single counter entry. */ 271*810390e3Srobert size_t __llvm_profile_counter_entry_size(void); 272*810390e3Srobert 273*810390e3Srobert /*! \brief Get the number of entries in the profile counters section. */ 274*810390e3Srobert uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End); 275*810390e3Srobert 276*810390e3Srobert /*! \brief Get the size of the profile counters section in bytes. */ 277*810390e3Srobert uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End); 278*810390e3Srobert 2793cab2bb3Spatrick /* ! \brief Given the sizes of the data and counter information, return the 2803cab2bb3Spatrick * number of padding bytes before and after the counters, and after the names, 2813cab2bb3Spatrick * in the raw profile. 2823cab2bb3Spatrick * 2833cab2bb3Spatrick * Note: When mmap() mode is disabled, no padding bytes before/after counters 2843cab2bb3Spatrick * are needed. However, in mmap() mode, the counter section in the raw profile 2853cab2bb3Spatrick * must be page-aligned: this API computes the number of padding bytes 2863cab2bb3Spatrick * needed to achieve that. 2873cab2bb3Spatrick */ 2883cab2bb3Spatrick void __llvm_profile_get_padding_sizes_for_counters( 2893cab2bb3Spatrick uint64_t DataSize, uint64_t CountersSize, uint64_t NamesSize, 2903cab2bb3Spatrick uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters, 2913cab2bb3Spatrick uint64_t *PaddingBytesAfterNames); 2923cab2bb3Spatrick 2933cab2bb3Spatrick /*! 2943cab2bb3Spatrick * \brief Set the flag that profile data has been dumped to the file. 2953cab2bb3Spatrick * This is useful for users to disable dumping profile data to the file for 2963cab2bb3Spatrick * certain processes in case the processes don't have permission to write to 2973cab2bb3Spatrick * the disks, and trying to do so would result in side effects such as crashes. 2983cab2bb3Spatrick */ 2993cab2bb3Spatrick void __llvm_profile_set_dumped(); 3003cab2bb3Spatrick 3013cab2bb3Spatrick /*! 3023cab2bb3Spatrick * This variable is defined in InstrProfilingRuntime.cpp as a hidden 3033cab2bb3Spatrick * symbol. Its main purpose is to enable profile runtime user to 3043cab2bb3Spatrick * bypass runtime initialization code -- if the client code explicitly 3053cab2bb3Spatrick * define this variable, then InstProfileRuntime.o won't be linked in. 3063cab2bb3Spatrick * Note that this variable's visibility needs to be hidden so that the 3073cab2bb3Spatrick * definition of this variable in an instrumented shared library won't 3083cab2bb3Spatrick * affect runtime initialization decision of the main program. 3093cab2bb3Spatrick * __llvm_profile_profile_runtime. */ 3103cab2bb3Spatrick COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR; 3113cab2bb3Spatrick 3123cab2bb3Spatrick /*! 313*810390e3Srobert * This variable is defined in InstrProfilingVersionVar.c as a hidden symbol 314*810390e3Srobert * (except on Apple platforms where this symbol is checked by TAPI). Its main 315*810390e3Srobert * purpose is to encode the raw profile version value and other format related 316*810390e3Srobert * information such as whether the profile is from IR based instrumentation. The 317*810390e3Srobert * variable is defined as weak so that compiler can emit an overriding 318*810390e3Srobert * definition depending on user option. 3193cab2bb3Spatrick */ 3203cab2bb3Spatrick extern uint64_t INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */ 3213cab2bb3Spatrick 3223cab2bb3Spatrick /*! 3233cab2bb3Spatrick * This variable is a weak symbol defined in InstrProfiling.c. It allows 3243cab2bb3Spatrick * compiler instrumentation to provide overriding definition with value 3253cab2bb3Spatrick * from compiler command line. This variable has default visibility. 3263cab2bb3Spatrick */ 3273cab2bb3Spatrick extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */ 3283cab2bb3Spatrick 3293cab2bb3Spatrick #endif /* PROFILE_INSTRPROFILING_H_ */ 330