|
| 1 | +//=-- ProfileDataReader.h - Instrumented profiling reader ---------*- C++ -*-=// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This file contains support for reading profiling data for instrumentation |
| 11 | +// based PGO and coverage. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#ifndef LLVM_PROFILE_PROFILEDATA_READER_H__ |
| 16 | +#define LLVM_PROFILE_PROFILEDATA_READER_H__ |
| 17 | + |
| 18 | +#include "llvm/ADT/StringMap.h" |
| 19 | +#include "llvm/Support/Compiler.h" |
| 20 | +#include "llvm/Support/DataTypes.h" |
| 21 | +#include "llvm/Support/ErrorOr.h" |
| 22 | +#include "llvm/Support/MemoryBuffer.h" |
| 23 | +#include "llvm/Support/raw_ostream.h" |
| 24 | + |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +namespace llvm { |
| 28 | + |
| 29 | +class ProfileDataCursor; |
| 30 | + |
| 31 | +/// Reader for the profile data that is used for instrumentation based PGO. |
| 32 | +class ProfileDataReader { |
| 33 | +private: |
| 34 | + /// The profile data file contents. |
| 35 | + std::unique_ptr<MemoryBuffer> DataBuffer; |
| 36 | + /// Offsets into DataBuffer for each function's counters. |
| 37 | + StringMap<uint32_t> DataOffsets; |
| 38 | + /// The maximal execution count among all functions. |
| 39 | + uint64_t MaxFunctionCount; |
| 40 | + |
| 41 | + ProfileDataReader(const ProfileDataReader &) LLVM_DELETED_FUNCTION; |
| 42 | + ProfileDataReader &operator=(const ProfileDataReader &) LLVM_DELETED_FUNCTION; |
| 43 | +protected: |
| 44 | + ProfileDataReader(std::unique_ptr<MemoryBuffer> &DataBuffer) |
| 45 | + : DataBuffer(DataBuffer.release()) {} |
| 46 | + |
| 47 | + /// Populate internal state using the profile data's index |
| 48 | + error_code readIndex(); |
| 49 | +public: |
| 50 | + |
| 51 | + class name_iterator { |
| 52 | + typedef StringMap<unsigned>::const_iterator IterTy; |
| 53 | + IterTy Ix; |
| 54 | + public: |
| 55 | + explicit name_iterator(const IterTy &Ix) : Ix(Ix) {} |
| 56 | + |
| 57 | + StringRef operator*() const { return Ix->getKey(); } |
| 58 | + |
| 59 | + bool operator==(const name_iterator &RHS) const { return Ix == RHS.Ix; } |
| 60 | + bool operator!=(const name_iterator &RHS) const { return Ix != RHS.Ix; } |
| 61 | + |
| 62 | + inline name_iterator& operator++() { ++Ix; return *this; } |
| 63 | + }; |
| 64 | + |
| 65 | + /// Iterators over the names of indexed items |
| 66 | + name_iterator begin() const { |
| 67 | + return name_iterator(DataOffsets.begin()); |
| 68 | + } |
| 69 | + name_iterator end() const { |
| 70 | + return name_iterator(DataOffsets.end()); |
| 71 | + } |
| 72 | + |
| 73 | +private: |
| 74 | + error_code findFunctionCounts(StringRef FuncName, uint64_t &FunctionHash, |
| 75 | + ProfileDataCursor &Cursor); |
| 76 | +public: |
| 77 | + /// The number of profiled functions |
| 78 | + size_t numProfiledFunctions() { return DataOffsets.size(); } |
| 79 | + /// Fill Counts with the profile data for the given function name. |
| 80 | + error_code getFunctionCounts(StringRef FuncName, uint64_t &FunctionHash, |
| 81 | + std::vector<uint64_t> &Counts); |
| 82 | + /// Get the frequency with which a function is called relative to the function |
| 83 | + /// that is called most often in the program. |
| 84 | + error_code getCallFrequency(StringRef FuncName, uint64_t &FunctionHash, |
| 85 | + double &F); |
| 86 | + |
| 87 | + static error_code create(std::string Path, |
| 88 | + std::unique_ptr<ProfileDataReader> &Result); |
| 89 | +}; |
| 90 | + |
| 91 | +} // end namespace llvm |
| 92 | + |
| 93 | +#endif // LLVM_PROFILE_PROFILEDATA_READER_H__ |
0 commit comments