LLVM  16.0.0git
RawMemProfReader.h
Go to the documentation of this file.
1 #ifndef LLVM_PROFILEDATA_RAWMEMPROFREADER_H_
2 #define LLVM_PROFILEDATA_RAWMEMPROFREADER_H_
3 //===- MemProfReader.h - Instrumented memory profiling reader ---*- C++ -*-===//
4 //
5 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6 // See https://llvm.org/LICENSE.txt for license information.
7 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file contains support for reading MemProf profiling data.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/StringRef.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/Object/Binary.h"
23 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Support/Error.h"
29 
30 #include <cstddef>
31 
32 namespace llvm {
33 namespace memprof {
34 
35 // Map from id (recorded from sanitizer stack depot) to virtual addresses for
36 // each program counter address in the callstack.
38 
40 public:
41  RawMemProfReader(const RawMemProfReader &) = delete;
42  RawMemProfReader &operator=(const RawMemProfReader &) = delete;
43 
44  // Prints the contents of the profile in YAML format.
45  void printYAML(raw_ostream &OS);
46 
47  // Return true if the \p DataBuffer starts with magic bytes indicating it is
48  // a raw binary memprof profile.
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);
53 
54  // Create a RawMemProfReader after sanity checking the contents of the file at
55  // \p Path. The binary from which the profile has been collected is specified
56  // via a path in \p ProfiledBinary.
58  create(const Twine &Path, const StringRef ProfiledBinary,
59  bool KeepName = false);
60 
61  using GuidMemProfRecordPair = std::pair<GlobalValue::GUID, MemProfRecord>;
63  Iterator end() { return Iterator(); }
65  Iter = FunctionProfileData.begin();
66  return Iterator(this);
67  }
68 
70 
71  // The RawMemProfReader only holds memory profile information.
73 
74  // Constructor for unittests only.
75  RawMemProfReader(std::unique_ptr<llvm::symbolize::SymbolizableModule> Sym,
78  CallStackMap &SM, bool KeepName = false)
79  : Symbolizer(std::move(Sym)), SegmentInfo(Seg.begin(), Seg.end()),
80  CallstackProfileData(Prof), StackMap(SM), KeepSymbolName(KeepName) {
81  // We don't call initialize here since there is no raw profile to read. The
82  // test should pass in the raw profile as structured data.
83 
84  // If there is an error here then the mock symbolizer has not been
85  // initialized properly.
86  if (Error E = symbolizeAndFilterStackFrames())
88  if (Error E = mapRawProfileToRecords())
90  }
91 
92  // Return a const reference to the internal Id to Frame mappings.
94  return IdToFrame;
95  }
96 
97  // Return a const reference to the internal function profile data.
99  getProfileData() const {
100  return FunctionProfileData;
101  }
102 
103 private:
105  : Binary(std::move(Bin)), KeepSymbolName(KeepName) {}
106  // Initializes the RawMemProfReader with the contents in `DataBuffer`.
107  Error initialize(std::unique_ptr<MemoryBuffer> DataBuffer);
108  // Read and parse the contents of the `DataBuffer` as a binary format profile.
109  Error readRawProfile(std::unique_ptr<MemoryBuffer> DataBuffer);
110  // Symbolize and cache all the virtual addresses we encounter in the
111  // callstacks from the raw profile. Also prune callstack frames which we can't
112  // symbolize or those that belong to the runtime. For profile entries where
113  // the entire callstack is pruned, we drop the entry from the profile.
114  Error symbolizeAndFilterStackFrames();
115  // Construct memprof records for each function and store it in the
116  // `FunctionProfileData` map. A function may have allocation profile data or
117  // callsite data or both.
118  Error mapRawProfileToRecords();
119 
120  // A helper method to extract the frame from the IdToFrame map.
121  const Frame &idToFrame(const FrameId Id) const {
122  auto It = IdToFrame.find(Id);
123  assert(It != IdToFrame.end() && "Id not found in map.");
124  return It->getSecond();
125  }
126 
127  object::SectionedAddress getModuleOffset(uint64_t VirtualAddress);
128 
129  object::OwningBinary<object::Binary> Binary;
130  std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer;
131 
132  // The contents of the raw profile.
134  // A map from callstack id (same as key in CallStackMap below) to the heap
135  // information recorded for that allocation context.
136  llvm::MapVector<uint64_t, MemInfoBlock> CallstackProfileData;
137  CallStackMap StackMap;
138 
139  // Cached symbolization from PC to Frame.
142 
145 
146  // Whether to keep the symbol name for each frame after hashing.
147  bool KeepSymbolName = false;
148  // A mapping of the hash to symbol name, only used if KeepSymbolName is true.
149  llvm::DenseMap<uint64_t, std::string> GuidToSymbolName;
150 };
151 } // namespace memprof
152 } // namespace llvm
153 
154 #endif // LLVM_PROFILEDATA_RAWMEMPROFREADER_H_
llvm::MapVector::iterator
typename VectorType::iterator iterator
Definition: MapVector.h:50
MemoryBuffer.h
llvm::memprof::RawMemProfReader::operator=
RawMemProfReader & operator=(const RawMemProfReader &)=delete
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
StringRef.h
llvm::SmallVector< SegmentEntry, 16 >
MapVector.h
llvm::memprof::RawMemProfReader::begin
Iterator begin()
Definition: RawMemProfReader.h:64
Error.h
DenseMap.h
llvm::memprof::RawMemProfReader::create
static Expected< std::unique_ptr< RawMemProfReader > > create(const Twine &Path, const StringRef ProfiledBinary, bool KeepName=false)
Definition: RawMemProfReader.cpp:172
llvm::MapVector< uint64_t, MemInfoBlock >
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::SubDirectoryType::Bin
@ Bin
llvm::memprof::RawMemProfReader::RawMemProfReader
RawMemProfReader(std::unique_ptr< llvm::symbolize::SymbolizableModule > Sym, llvm::SmallVectorImpl< SegmentEntry > &Seg, llvm::MapVector< uint64_t, MemInfoBlock > &Prof, CallStackMap &SM, bool KeepName=false)
Definition: RawMemProfReader.h:75
GlobalValue.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::memprof::RawMemProfReader::readNextRecord
Error readNextRecord(GuidMemProfRecordPair &GuidRecord)
Definition: RawMemProfReader.cpp:535
llvm::memprof::RawMemProfReader::RawMemProfReader
RawMemProfReader(const RawMemProfReader &)=delete
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::memprof::RawMemProfReader::getProfileKind
InstrProfKind getProfileKind() const
Definition: RawMemProfReader.h:72
InstrProfReader.h
uint64_t
SymbolizableModule.h
llvm::memprof::RawMemProfReader
Definition: RawMemProfReader.h:39
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap< uint64_t, llvm::SmallVector< uint64_t > >
llvm::memprof::RawMemProfReader::printYAML
void printYAML(raw_ostream &OS)
Definition: RawMemProfReader.cpp:219
llvm::memprof::RawMemProfReader::getFrameMapping
const llvm::DenseMap< FrameId, Frame > & getFrameMapping() const
Definition: RawMemProfReader.h:93
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1676
ObjectFile.h
llvm::InstrProfKind::MemProf
@ MemProf
llvm::memprof::RawMemProfReader::GuidMemProfRecordPair
std::pair< GlobalValue::GUID, MemProfRecord > GuidMemProfRecordPair
Definition: RawMemProfReader.h:61
Symbolize.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::memprof::RawMemProfReader::Iterator
InstrProfIterator< GuidMemProfRecordPair, RawMemProfReader > Iterator
Definition: RawMemProfReader.h:62
MemProfData.inc
MemProf.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std
Definition: BitVector.h:851
llvm::memprof::RawMemProfReader::hasFormat
static bool hasFormat(const MemoryBuffer &DataBuffer)
Definition: RawMemProfReader.cpp:210
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::memprof::RawMemProfReader::end
Iterator end()
Definition: RawMemProfReader.h:63
memprof
memprof
Definition: MemProfiler.cpp:281
Binary.h
llvm::memprof::CallStackMap
llvm::DenseMap< uint64_t, llvm::SmallVector< uint64_t > > CallStackMap
Definition: RawMemProfReader.h:37
llvm::InstrProfKind
InstrProfKind
An enum describing the attributes of an instrumented profile.
Definition: InstrProf.h:282
llvm::SmallVectorImpl< SegmentEntry >
llvm::memprof::RawMemProfReader::getProfileData
const llvm::MapVector< GlobalValue::GUID, IndexedMemProfRecord > & getProfileData() const
Definition: RawMemProfReader.h:99
llvm::InstrProfIterator
A file format agnostic iterator over profiling data.
Definition: InstrProfReader.h:45
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:241
llvm::memprof::FrameId
uint64_t FrameId
Definition: MemProf.h:137
SetVector.h