|
12 | 12 | // |
13 | 13 | //===----------------------------------------------------------------------===// |
14 | 14 |
|
| 15 | +#include "llvm/ADT/DenseMap.h" |
| 16 | +#include "llvm/ADT/MapVector.h" |
| 17 | +#include "llvm/ADT/StringRef.h" |
| 18 | +#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h" |
| 19 | +#include "llvm/DebugInfo/Symbolize/Symbolize.h" |
| 20 | +#include "llvm/Object/Binary.h" |
| 21 | +#include "llvm/Object/ObjectFile.h" |
| 22 | +#include "llvm/ProfileData/InstrProfReader.h" |
| 23 | +#include "llvm/ProfileData/MemProf.h" |
| 24 | +#include "llvm/ProfileData/MemProfData.inc" |
15 | 25 | #include "llvm/Support/Error.h" |
16 | 26 | #include "llvm/Support/MemoryBuffer.h" |
17 | 27 |
|
| 28 | +#include <cstddef> |
| 29 | + |
18 | 30 | namespace llvm { |
19 | 31 | namespace memprof { |
20 | 32 |
|
| 33 | +// Map from id (recorded from sanitizer stack depot) to virtual addresses for |
| 34 | +// each program counter address in the callstack. |
| 35 | +using CallStackMap = llvm::DenseMap<uint64_t, llvm::SmallVector<uint64_t, 32>>; |
| 36 | + |
21 | 37 | class RawMemProfReader { |
22 | 38 | public: |
23 | 39 | RawMemProfReader(std::unique_ptr<MemoryBuffer> DataBuffer) |
24 | 40 | : DataBuffer(std::move(DataBuffer)) {} |
| 41 | + RawMemProfReader(const RawMemProfReader &) = delete; |
| 42 | + RawMemProfReader &operator=(const RawMemProfReader &) = delete; |
| 43 | + |
25 | 44 | // Prints the contents of the profile in YAML format. |
26 | 45 | void printYAML(raw_ostream &OS); |
27 | 46 |
|
28 | 47 | // Return true if the \p DataBuffer starts with magic bytes indicating it is |
29 | 48 | // a raw binary memprof profile. |
30 | 49 | static bool hasFormat(const MemoryBuffer &DataBuffer); |
| 50 | + // Return true if the file at \p Path starts with magic bytes indicating it is |
| 51 | + // a raw binary memprof profile. |
| 52 | + static bool hasFormat(const StringRef Path); |
31 | 53 |
|
32 | 54 | // Create a RawMemProfReader after sanity checking the contents of the file at |
33 | | - // \p Path. |
34 | | - static Expected<std::unique_ptr<RawMemProfReader>> create(const Twine &Path); |
| 55 | + // \p Path. The binary from which the profile has been collected is specified |
| 56 | + // via a path in \p ProfiledBinary. |
| 57 | + static Expected<std::unique_ptr<RawMemProfReader>> |
| 58 | + create(const Twine &Path, const StringRef ProfiledBinary); |
| 59 | + |
| 60 | + Error readNextRecord(MemProfRecord &Record); |
| 61 | + |
| 62 | + using Iterator = InstrProfIterator<MemProfRecord, RawMemProfReader>; |
| 63 | + Iterator end() { return Iterator(); } |
| 64 | + Iterator begin() { |
| 65 | + Iter = ProfileData.begin(); |
| 66 | + return Iterator(this); |
| 67 | + } |
| 68 | + |
| 69 | + // Constructor for unittests only. |
| 70 | + RawMemProfReader(std::unique_ptr<llvm::symbolize::SymbolizableModule> Sym, |
| 71 | + llvm::SmallVectorImpl<SegmentEntry> &Seg, |
| 72 | + llvm::MapVector<uint64_t, MemInfoBlock> &Prof, |
| 73 | + CallStackMap &SM) |
| 74 | + : Symbolizer(std::move(Sym)), SegmentInfo(Seg.begin(), Seg.end()), |
| 75 | + ProfileData(Prof), StackMap(SM) {} |
35 | 76 |
|
36 | 77 | private: |
| 78 | + RawMemProfReader(std::unique_ptr<MemoryBuffer> DataBuffer, |
| 79 | + object::OwningBinary<object::Binary> &&Bin) |
| 80 | + : DataBuffer(std::move(DataBuffer)), Binary(std::move(Bin)) {} |
| 81 | + Error initialize(); |
| 82 | + Error readRawProfile(); |
| 83 | + |
| 84 | + object::SectionedAddress getModuleOffset(uint64_t VirtualAddress); |
| 85 | + Error fillRecord(const uint64_t Id, const MemInfoBlock &MIB, |
| 86 | + MemProfRecord &Record); |
37 | 87 | // Prints aggregate counts for each raw profile parsed from the DataBuffer in |
38 | 88 | // YAML format. |
39 | 89 | void printSummaries(raw_ostream &OS) const; |
40 | 90 |
|
41 | 91 | std::unique_ptr<MemoryBuffer> DataBuffer; |
| 92 | + object::OwningBinary<object::Binary> Binary; |
| 93 | + std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer; |
| 94 | + |
| 95 | + // The contents of the raw profile. |
| 96 | + llvm::SmallVector<SegmentEntry, 16> SegmentInfo; |
| 97 | + // A map from callstack id (same as key in CallStackMap below) to the heap |
| 98 | + // information recorded for that allocation context. |
| 99 | + llvm::MapVector<uint64_t, MemInfoBlock> ProfileData; |
| 100 | + CallStackMap StackMap; |
| 101 | + |
| 102 | + // Iterator to read from the ProfileData MapVector. |
| 103 | + llvm::MapVector<uint64_t, MemInfoBlock>::iterator Iter = ProfileData.end(); |
42 | 104 | }; |
43 | 105 |
|
44 | 106 | } // namespace memprof |
|
0 commit comments