Path: blob/main/contrib/llvm-project/compiler-rt/lib/profile/InstrProfiling.h
35233 views
/*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\1|*2|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3|* See https://llvm.org/LICENSE.txt for license information.4|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5|*6\*===----------------------------------------------------------------------===*/78#ifndef PROFILE_INSTRPROFILING_H_9#define PROFILE_INSTRPROFILING_H_1011#include "InstrProfilingPort.h"12#include <stdio.h>1314// Make sure __LLVM_INSTR_PROFILE_GENERATE is always defined before15// including instr_prof_interface.h so the interface functions are16// declared correctly for the runtime.17// __LLVM_INSTR_PROFILE_GENERATE is always `#undef`ed after the header,18// because compiler-rt does not support profiling the profiling runtime itself.19#ifndef __LLVM_INSTR_PROFILE_GENERATE20#define __LLVM_INSTR_PROFILE_GENERATE21#endif22#include "profile/instr_prof_interface.h"23#undef __LLVM_INSTR_PROFILE_GENERATE2425#define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY26#include "profile/InstrProfData.inc"2728enum ValueKind {29#define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,30#include "profile/InstrProfData.inc"31};3233typedef void *IntPtrT;34typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)35__llvm_profile_data {36#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;37#include "profile/InstrProfData.inc"38} __llvm_profile_data;3940typedef struct __llvm_profile_header {41#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;42#include "profile/InstrProfData.inc"43} __llvm_profile_header;4445typedef struct ValueProfNode * PtrToNodeT;46typedef struct ValueProfNode {47#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name;48#include "profile/InstrProfData.inc"49} ValueProfNode;5051typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT) VTableProfData {52#define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Initializer) Type Name;53#include "profile/InstrProfData.inc"54} VTableProfData;5556/*!57* \brief Return 1 if profile counters are continuously synced to the raw58* profile via an mmap(). This is in contrast to the default mode, in which59* the raw profile is written out at program exit time.60*/61int __llvm_profile_is_continuous_mode_enabled(void);6263/*!64* \brief Enable continuous mode.65*66* See \ref __llvm_profile_is_continuous_mode_enabled. The behavior is undefined67* if continuous mode is already enabled, or if it cannot be enable due to68* conflicting options.69*/70void __llvm_profile_enable_continuous_mode(void);7172/*!73* \brief Disable continuous mode.74*75*/76void __llvm_profile_disable_continuous_mode(void);7778/*!79* \brief Set the page size.80*81* This is a pre-requisite for enabling continuous mode. The buffer size82* calculation code inside of libprofile cannot simply call getpagesize(), as83* it is not allowed to depend on libc.84*/85void __llvm_profile_set_page_size(unsigned PageSize);8687/*!88* \brief Get number of bytes necessary to pad the argument to eight89* byte boundary.90*/91uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);9293/*!94* \brief Get required size for profile buffer.95*/96uint64_t __llvm_profile_get_size_for_buffer(void);9798/*!99* \brief Write instrumentation data to the given buffer.100*101* \pre \c Buffer is the start of a buffer at least as big as \a102* __llvm_profile_get_size_for_buffer().103*/104int __llvm_profile_write_buffer(char *Buffer);105106const __llvm_profile_data *__llvm_profile_begin_data(void);107const __llvm_profile_data *__llvm_profile_end_data(void);108const char *__llvm_profile_begin_names(void);109const char *__llvm_profile_end_names(void);110const char *__llvm_profile_begin_vtabnames(void);111const char *__llvm_profile_end_vtabnames(void);112char *__llvm_profile_begin_counters(void);113char *__llvm_profile_end_counters(void);114char *__llvm_profile_begin_bitmap(void);115char *__llvm_profile_end_bitmap(void);116ValueProfNode *__llvm_profile_begin_vnodes();117ValueProfNode *__llvm_profile_end_vnodes();118const VTableProfData *__llvm_profile_begin_vtables();119const VTableProfData *__llvm_profile_end_vtables();120uint32_t *__llvm_profile_begin_orderfile();121122/*!123* \brief Merge profile data from buffer.124*125* Read profile data from buffer \p Profile and merge with in-process profile126* counters and bitmaps. The client is expected to have checked or already127* know the profile data in the buffer matches the in-process counter128* structure before calling it. Returns 0 (success) if the profile data is129* valid. Upon reading invalid/corrupted profile data, returns 1 (failure).130*/131int __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size);132133/*! \brief Check if profile in buffer matches the current binary.134*135* Returns 0 (success) if the profile data in buffer \p Profile with size136* \p Size was generated by the same binary and therefore matches137* structurally the in-process counters and bitmaps. If the profile data in138* buffer is not compatible, the interface returns 1 (failure).139*/140int __llvm_profile_check_compatibility(const char *Profile,141uint64_t Size);142143/*!144* \brief Counts the number of times a target value is seen.145*146* Records the target value for the CounterIndex if not seen before. Otherwise,147* increments the counter associated w/ the target value.148* void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,149* uint32_t CounterIndex);150*/151void INSTR_PROF_VALUE_PROF_FUNC(152#define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName153#include "profile/InstrProfData.inc"154);155156void __llvm_profile_instrument_target_value(uint64_t TargetValue, void *Data,157uint32_t CounterIndex,158uint64_t CounterValue);159160/*!161* \brief Write instrumentation data to the current file.162*163* Writes to the file with the last name given to \a *164* __llvm_profile_set_filename(),165* or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,166* or if that's not set, the last name set to INSTR_PROF_PROFILE_NAME_VAR,167* or if that's not set, \c "default.profraw".168*/169int __llvm_profile_write_file(void);170171int __llvm_orderfile_write_file(void);172173/*!174* \brief Set the FILE object for writing instrumentation data. Return 0 if set175* successfully or return 1 if failed.176*177* Sets the FILE object to be used for subsequent calls to178* \a __llvm_profile_write_file(). The profile file name set by environment179* variable, command-line option, or calls to \a __llvm_profile_set_filename180* will be ignored.181*182* \c File will not be closed after a call to \a __llvm_profile_write_file() but183* it may be flushed. Passing NULL restores default behavior.184*185* If \c EnableMerge is nonzero, the runtime will always merge profiling data186* with the contents of the profiling file. If EnableMerge is zero, the runtime187* may still merge the data if it would have merged for another reason (for188* example, because of a %m specifier in the file name).189*190* Note: There may be multiple copies of the profile runtime (one for each191* instrumented image/DSO). This API only modifies the file object within the192* copy of the runtime available to the calling image.193*194* Warning: This is a no-op if EnableMerge is 0 in continuous mode (\ref195* __llvm_profile_is_continuous_mode_enabled), because disable merging requires196* copying the old profile file to new profile file and this function is usually197* used when the proess doesn't have permission to open file.198*/199int __llvm_profile_set_file_object(FILE *File, int EnableMerge);200201/*! \brief Register to write instrumentation data to file at exit. */202int __llvm_profile_register_write_file_atexit(void);203204/*! \brief Initialize file handling. */205void __llvm_profile_initialize_file(void);206207/*! \brief Initialize the profile runtime. */208void __llvm_profile_initialize(void);209210/*!211* \brief Return path prefix (excluding the base filename) of the profile data.212* This is useful for users using \c -fprofile-generate=./path_prefix who do213* not care about the default raw profile name. It is also useful to collect214* more than more profile data files dumped in the same directory (Online215* merge mode is turned on for instrumented programs with shared libs).216* Side-effect: this API call will invoke malloc with dynamic memory allocation.217*/218const char *__llvm_profile_get_path_prefix();219220/*!221* \brief Return filename (including path) of the profile data. Note that if the222* user calls __llvm_profile_set_filename later after invoking this interface,223* the actual file name may differ from what is returned here.224* Side-effect: this API call will invoke malloc with dynamic memory allocation225* (the returned pointer must be passed to `free` to avoid a leak).226*227* Note: There may be multiple copies of the profile runtime (one for each228* instrumented image/DSO). This API only retrieves the filename from the copy229* of the runtime available to the calling image.230*/231const char *__llvm_profile_get_filename();232233/*! \brief Get the magic token for the file format. */234uint64_t __llvm_profile_get_magic(void);235236/*! \brief Get the version of the file format. */237uint64_t __llvm_profile_get_version(void);238239/*! \brief Get the number of entries in the profile data section. */240uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,241const __llvm_profile_data *End);242243/*! \brief Get the size of the profile data section in bytes. */244uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,245const __llvm_profile_data *End);246247/*! \brief Get the size in bytes of a single counter entry. */248size_t __llvm_profile_counter_entry_size(void);249250/*! \brief Get the number of entries in the profile counters section. */251uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End);252253/*! \brief Get the size of the profile counters section in bytes. */254uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End);255256/*! \brief Get the number of bytes in the profile bitmap section. */257uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,258const char *End);259260/*! \brief Get the size of the profile name section in bytes. */261uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End);262263/*! \brief Get the number of virtual table profile data entries */264uint64_t __llvm_profile_get_num_vtable(const VTableProfData *Begin,265const VTableProfData *End);266267/*! \brief Get the size of virtual table profile data in bytes. */268uint64_t __llvm_profile_get_vtable_section_size(const VTableProfData *Begin,269const VTableProfData *End);270271/* ! \brief Given the sizes of the data and counter information, computes the272* number of padding bytes before and after the counter section, as well as the273* number of padding bytes after other setions in the raw profile.274* Returns -1 upon errors and 0 upon success. Output parameters should be used275* iff return value is 0.276*277* Note: When mmap() mode is disabled, no padding bytes before/after counters278* are needed. However, in mmap() mode, the counter section in the raw profile279* must be page-aligned: this API computes the number of padding bytes280* needed to achieve that.281*/282int __llvm_profile_get_padding_sizes_for_counters(283uint64_t DataSize, uint64_t CountersSize, uint64_t NumBitmapBytes,284uint64_t NamesSize, uint64_t VTableSize, uint64_t VNameSize,285uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,286uint64_t *PaddingBytesAfterBitmap, uint64_t *PaddingBytesAfterNames,287uint64_t *PaddingBytesAfterVTable, uint64_t *PaddingBytesAfterVNames);288289/*!290* \brief Set the flag that profile data has been dumped to the file.291* This is useful for users to disable dumping profile data to the file for292* certain processes in case the processes don't have permission to write to293* the disks, and trying to do so would result in side effects such as crashes.294*/295void __llvm_profile_set_dumped();296297/*!298* This variable is defined in InstrProfilingRuntime.cpp as a hidden299* symbol. Its main purpose is to enable profile runtime user to300* bypass runtime initialization code -- if the client code explicitly301* define this variable, then InstProfileRuntime.o won't be linked in.302* Note that this variable's visibility needs to be hidden so that the303* definition of this variable in an instrumented shared library won't304* affect runtime initialization decision of the main program.305* __llvm_profile_profile_runtime. */306COMPILER_RT_VISIBILITY extern int INSTR_PROF_PROFILE_RUNTIME_VAR;307308/*!309* This variable is defined in InstrProfilingVersionVar.c as a hidden symbol310* (except on Apple platforms where this symbol is checked by TAPI). Its main311* purpose is to encode the raw profile version value and other format related312* information such as whether the profile is from IR based instrumentation. The313* variable is defined as weak so that compiler can emit an overriding314* definition depending on user option.315*/316COMPILER_RT_VISIBILITY extern uint64_t317INSTR_PROF_RAW_VERSION_VAR; /* __llvm_profile_raw_version */318319/*!320* This variable is a weak symbol defined in InstrProfiling.c. It allows321* compiler instrumentation to provide overriding definition with value322* from compiler command line. This variable has default visibility.323*/324extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */325326#endif /* PROFILE_INSTRPROFILING_H_ */327328329