Path: blob/main/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingInternal.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_INTERNALH_9#define PROFILE_INSTRPROFILING_INTERNALH_1011#include <stddef.h>1213#include "InstrProfiling.h"1415/*!16* \brief Write instrumentation data to the given buffer, given explicit17* pointers to the live data in memory. This function is probably not what you18* want. Use __llvm_profile_get_size_for_buffer instead. Use this function if19* your program has a custom memory layout.20*/21uint64_t __llvm_profile_get_size_for_buffer_internal(22const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,23const char *CountersBegin, const char *CountersEnd, const char *BitmapBegin,24const char *BitmapEnd, const char *NamesBegin, const char *NamesEnd,25const VTableProfData *VTableBegin, const VTableProfData *VTableEnd,26const char *VNamesBegin, const char *VNamesEnd);2728/*!29* \brief Write instrumentation data to the given buffer, given explicit30* pointers to the live data in memory. This function is probably not what you31* want. Use __llvm_profile_write_buffer instead. Use this function if your32* program has a custom memory layout.33*34* \pre \c Buffer is the start of a buffer at least as big as \a35* __llvm_profile_get_size_for_buffer_internal().36*/37int __llvm_profile_write_buffer_internal(38char *Buffer, const __llvm_profile_data *DataBegin,39const __llvm_profile_data *DataEnd, const char *CountersBegin,40const char *CountersEnd, const char *BitmapBegin, const char *BitmapEnd,41const char *NamesBegin, const char *NamesEnd);4243/*!44* The data structure describing the data to be written by the45* low level writer callback function.46*47* If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is48* 0, the write is skipped (the writer simply advances ElmSize*NumElm bytes).49*50* If \ref ProfDataIOVec.Data is null, and \ref ProfDataIOVec.UseZeroPadding is51* nonzero, ElmSize*NumElm zero bytes are written.52*/53typedef struct ProfDataIOVec {54const void *Data;55size_t ElmSize;56size_t NumElm;57int UseZeroPadding;58} ProfDataIOVec;5960struct ProfDataWriter;61typedef uint32_t (*WriterCallback)(struct ProfDataWriter *This, ProfDataIOVec *,62uint32_t NumIOVecs);6364typedef struct ProfDataWriter {65WriterCallback Write;66void *WriterCtx;67} ProfDataWriter;6869/*!70* The data structure for buffered IO of profile data.71*/72typedef struct ProfBufferIO {73ProfDataWriter *FileWriter;74uint32_t OwnFileWriter;75/* The start of the buffer. */76uint8_t *BufferStart;77/* Total size of the buffer. */78uint32_t BufferSz;79/* Current byte offset from the start of the buffer. */80uint32_t CurOffset;81} ProfBufferIO;8283/* The creator interface used by testing. */84ProfBufferIO *lprofCreateBufferIOInternal(void *File, uint32_t BufferSz);8586/*!87* This is the interface to create a handle for buffered IO.88*/89ProfBufferIO *lprofCreateBufferIO(ProfDataWriter *FileWriter);9091/*!92* The interface to destroy the bufferIO handle and reclaim93* the memory.94*/95void lprofDeleteBufferIO(ProfBufferIO *BufferIO);9697/*!98* This is the interface to write \c Data of \c Size bytes through99* \c BufferIO. Returns 0 if successful, otherwise return -1.100*/101int lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data,102uint32_t Size);103/*!104* The interface to flush the remaining data in the buffer.105* through the low level writer callback.106*/107int lprofBufferIOFlush(ProfBufferIO *BufferIO);108109/* The low level interface to write data into a buffer. It is used as the110* callback by other high level writer methods such as buffered IO writer111* and profile data writer. */112uint32_t lprofBufferWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,113uint32_t NumIOVecs);114void initBufferWriter(ProfDataWriter *BufferWriter, char *Buffer);115116struct ValueProfData;117struct ValueProfRecord;118struct InstrProfValueData;119struct ValueProfNode;120121/*!122* The class that defines a set of methods to read value profile123* data for streaming/serialization from the instrumentation runtime.124*/125typedef struct VPDataReaderType {126uint32_t (*InitRTRecord)(const __llvm_profile_data *Data,127uint8_t *SiteCountArray[]);128/* Function pointer to getValueProfRecordHeader method. */129uint32_t (*GetValueProfRecordHeaderSize)(uint32_t NumSites);130/* Function pointer to getFristValueProfRecord method. */131struct ValueProfRecord *(*GetFirstValueProfRecord)(struct ValueProfData *);132/* Return the number of value data for site \p Site. */133uint32_t (*GetNumValueDataForSite)(uint32_t VK, uint32_t Site);134/* Return the total size of the value profile data of the135* current function. */136uint32_t (*GetValueProfDataSize)(void);137/*!138* Read the next \p N value data for site \p Site and store the data139* in \p Dst. \p StartNode is the first value node to start with if140* it is not null. The function returns the pointer to the value141* node pointer to be used as the \p StartNode of the next batch reading.142* If there is nothing left, it returns NULL.143*/144struct ValueProfNode *(*GetValueData)(uint32_t ValueKind, uint32_t Site,145struct InstrProfValueData *Dst,146struct ValueProfNode *StartNode,147uint32_t N);148} VPDataReaderType;149150/* Write profile data to destination. If SkipNameDataWrite is set to 1,151the name data is already in destination, we just skip over it. */152int lprofWriteData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader,153int SkipNameDataWrite);154int lprofWriteDataImpl(ProfDataWriter *Writer,155const __llvm_profile_data *DataBegin,156const __llvm_profile_data *DataEnd,157const char *CountersBegin, const char *CountersEnd,158const char *BitmapBegin, const char *BitmapEnd,159VPDataReaderType *VPDataReader, const char *NamesBegin,160const char *NamesEnd, const VTableProfData *VTableBegin,161const VTableProfData *VTableEnd, const char *VNamesBegin,162const char *VNamesEnd, int SkipNameDataWrite);163164/* Merge value profile data pointed to by SrcValueProfData into165* in-memory profile counters pointed by to DstData. */166void lprofMergeValueProfData(struct ValueProfData *SrcValueProfData,167__llvm_profile_data *DstData);168169VPDataReaderType *lprofGetVPDataReader();170171/* Internal interface used by test to reset the max number of172* tracked values per value site to be \p MaxVals.173*/174void lprofSetMaxValsPerSite(uint32_t MaxVals);175void lprofSetupValueProfiler();176177/* Return the profile header 'signature' value associated with the current178* executable or shared library. The signature value can be used to for179* a profile name that is unique to this load module so that it does not180* collide with profiles from other binaries. It also allows shared libraries181* to dump merged profile data into its own profile file. */182uint64_t lprofGetLoadModuleSignature();183184/*185* Return non zero value if the profile data has already been186* dumped to the file.187*/188unsigned lprofProfileDumped(void);189void lprofSetProfileDumped(unsigned);190191COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);192COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;193COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;194COMPILER_RT_VISIBILITY extern uint32_t VPMaxNumValsPerSite;195/* Pointer to the start of static value counters to be allocted. */196COMPILER_RT_VISIBILITY extern ValueProfNode *CurrentVNode;197COMPILER_RT_VISIBILITY extern ValueProfNode *EndVNode;198extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);199200/*201* Write binary ids into profiles if writer is given.202* Return -1 if an error occurs, otherwise, return total size of binary ids.203*/204int __llvm_write_binary_ids(ProfDataWriter *Writer);205206/*207* Write binary id length and then its data, because binary id does not208* have a fixed length.209*/210int lprofWriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,211const uint8_t *BinaryIdData,212uint64_t BinaryIdPadding);213214#endif215216217