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/StringRef.h"
20 #include "llvm/IR/GlobalValue.h"
21 #include "llvm/Object/Binary.h"
22 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Support/Error.h"
28 
29 #include <cstddef>
30 
31 namespace llvm {
32 namespace memprof {
33 
34 // Map from id (recorded from sanitizer stack depot) to virtual addresses for
35 // each program counter address in the callstack.
37 
39 public:
40  RawMemProfReader(const RawMemProfReader &) = delete;
41  RawMemProfReader &operator=(const RawMemProfReader &) = delete;
42 
43  // Prints the contents of the profile in YAML format.
44  void printYAML(raw_ostream &OS);
45 
46  // Return true if the \p DataBuffer starts with magic bytes indicating it is
47  // a raw binary memprof profile.
48  static bool hasFormat(const MemoryBuffer &DataBuffer);
49  // Return true if the file at \p Path starts with magic bytes indicating it is
50  // a raw binary memprof profile.
51  static bool hasFormat(const StringRef Path);
52 
53  // Create a RawMemProfReader after sanity checking the contents of the file at
54  // \p Path. The binary from which the profile has been collected is specified
55  // via a path in \p ProfiledBinary.
57  create(const Twine &Path, const StringRef ProfiledBinary,
58  bool KeepName = false);
59 
60  using GuidMemProfRecordPair = std::pair<GlobalValue::GUID, MemProfRecord>;
62  Iterator end() { return Iterator(); }
64  Iter = FunctionProfileData.begin();
65  return Iterator(this);
66  }
67 
69 
70  // The RawMemProfReader only holds memory profile information.
72 
73  // Constructor for unittests only.
74  RawMemProfReader(std::unique_ptr<llvm::symbolize::SymbolizableModule> Sym,
77  CallStackMap &SM, bool KeepName = false)
78  : Symbolizer(std::move(Sym)), SegmentInfo(Seg.begin(), Seg.end()),
79  CallstackProfileData(Prof), StackMap(SM), KeepSymbolName(KeepName) {
80  // We don't call initialize here since there is no raw profile to read. The
81  // test should pass in the raw profile as structured data.
82 
83  // If there is an error here then the mock symbolizer has not been
84  // initialized properly.
85  if (Error E = symbolizeAndFilterStackFrames())
87  if (Error E = mapRawProfileToRecords())
89  }
90 
91  // Return a const reference to the internal Id to Frame mappings.
93  return IdToFrame;
94  }
95 
96  // Return a const reference to the internal function profile data.
98  getProfileData() const {
99  return FunctionProfileData;
100  }
101 
102 private:
104  : Binary(std::move(Bin)), KeepSymbolName(KeepName) {}
105  // Initializes the RawMemProfReader with the contents in `DataBuffer`.
106  Error initialize(std::unique_ptr<MemoryBuffer> DataBuffer);
107  // Read and parse the contents of the `DataBuffer` as a binary format profile.
108  Error readRawProfile(std::unique_ptr<MemoryBuffer> DataBuffer);
109  // Symbolize and cache all the virtual addresses we encounter in the
110  // callstacks from the raw profile. Also prune callstack frames which we can't
111  // symbolize or those that belong to the runtime. For profile entries where
112  // the entire callstack is pruned, we drop the entry from the profile.
113  Error symbolizeAndFilterStackFrames();
114  // Construct memprof records for each function and store it in the
115  // `FunctionProfileData` map. A function may have allocation profile data or
116  // callsite data or both.
117  Error mapRawProfileToRecords();
118 
119  // A helper method to extract the frame from the IdToFrame map.
120  const Frame &idToFrame(const FrameId Id) const {
121  auto It = IdToFrame.find(Id);
122  assert(It != IdToFrame.end() && "Id not found in map.");
123  return It->getSecond();
124  }
125 
126  object::SectionedAddress getModuleOffset(uint64_t VirtualAddress);
127 
128  object::OwningBinary<object::Binary> Binary;
129  std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer;
130 
131  // The contents of the raw profile.
133  // A map from callstack id (same as key in CallStackMap below) to the heap
134  // information recorded for that allocation context.
135  llvm::MapVector<uint64_t, MemInfoBlock> CallstackProfileData;
136  CallStackMap StackMap;
137 
138  // Cached symbolization from PC to Frame.
141 
144 
145  // Whether to keep the symbol name for each frame after hashing.
146  bool KeepSymbolName = false;
147  // A mapping of the hash to symbol name, only used if KeepSymbolName is true.
148  llvm::DenseMap<uint64_t, std::string> GuidToSymbolName;
149 };
150 } // namespace memprof
151 } // namespace llvm
152 
153 #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:63
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:173
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:51
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:74
GlobalValue.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::memprof::RawMemProfReader::readNextRecord
Error readNextRecord(GuidMemProfRecordPair &GuidRecord)
Definition: RawMemProfReader.cpp:534
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:53
llvm::memprof::RawMemProfReader::getProfileKind
InstrProfKind getProfileKind() const
Definition: RawMemProfReader.h:71
InstrProfReader.h
uint64_t
SymbolizableModule.h
llvm::memprof::RawMemProfReader
Definition: RawMemProfReader.h:38
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:220
llvm::memprof::RawMemProfReader::getFrameMapping
const llvm::DenseMap< FrameId, Frame > & getFrameMapping() const
Definition: RawMemProfReader.h:92
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:1861
ObjectFile.h
llvm::InstrProfKind::MemProf
@ MemProf
llvm::memprof::RawMemProfReader::GuidMemProfRecordPair
std::pair< GlobalValue::GUID, MemProfRecord > GuidMemProfRecordPair
Definition: RawMemProfReader.h:60
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:61
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:211
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:62
Binary.h
llvm::memprof::CallStackMap
llvm::DenseMap< uint64_t, llvm::SmallVector< uint64_t > > CallStackMap
Definition: RawMemProfReader.h:36
llvm::InstrProfKind
InstrProfKind
An enum describing the attributes of an instrumented profile.
Definition: InstrProf.h:287
llvm::SmallVectorImpl< SegmentEntry >
llvm::memprof::RawMemProfReader::getProfileData
const llvm::MapVector< GlobalValue::GUID, IndexedMemProfRecord > & getProfileData() const
Definition: RawMemProfReader.h:98
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:138