LLVM  15.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(std::unique_ptr<MemoryBuffer> DataBuffer)
42  : DataBuffer(std::move(DataBuffer)) {}
43  RawMemProfReader(const RawMemProfReader &) = delete;
44  RawMemProfReader &operator=(const RawMemProfReader &) = delete;
45 
46  // Prints the contents of the profile in YAML format.
47  void printYAML(raw_ostream &OS);
48 
49  // Return true if the \p DataBuffer starts with magic bytes indicating it is
50  // a raw binary memprof profile.
51  static bool hasFormat(const MemoryBuffer &DataBuffer);
52  // Return true if the file at \p Path starts with magic bytes indicating it is
53  // a raw binary memprof profile.
54  static bool hasFormat(const StringRef Path);
55 
56  // Create a RawMemProfReader after sanity checking the contents of the file at
57  // \p Path. The binary from which the profile has been collected is specified
58  // via a path in \p ProfiledBinary.
60  create(const Twine &Path, const StringRef ProfiledBinary);
61 
62  using GuidMemProfRecordPair = std::pair<GlobalValue::GUID, MemProfRecord>;
64  Iterator end() { return Iterator(); }
66  Iter = FunctionProfileData.begin();
67  return Iterator(this);
68  }
69 
71 
72  // The RawMemProfReader only holds memory profile information.
74 
75  // Constructor for unittests only.
76  RawMemProfReader(std::unique_ptr<llvm::symbolize::SymbolizableModule> Sym,
79  CallStackMap &SM)
80  : Symbolizer(std::move(Sym)), SegmentInfo(Seg.begin(), Seg.end()),
81  CallstackProfileData(Prof), StackMap(SM) {
82  // We don't call initialize here since there is no raw profile to read. The
83  // test should pass in the raw profile as structured data.
84 
85  // If there is an error here then the mock symbolizer has not been
86  // initialized properly.
87  if (Error E = symbolizeAndFilterStackFrames())
89  if (Error E = mapRawProfileToRecords())
91  }
92 
93  // Return a const reference to the internal Id to Frame mappings.
95  return IdToFrame;
96  }
97 
98  // Return a const reference to the internal function profile data.
100  getProfileData() const {
101  return FunctionProfileData;
102  }
103 
104 private:
105  RawMemProfReader(std::unique_ptr<MemoryBuffer> DataBuffer,
107  : DataBuffer(std::move(DataBuffer)), Binary(std::move(Bin)) {}
108  Error initialize();
109  Error readRawProfile();
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  // Prints aggregate counts for each raw profile parsed from the DataBuffer in
129  // YAML format.
130  void printSummaries(raw_ostream &OS) const;
131 
132  std::unique_ptr<MemoryBuffer> DataBuffer;
133  object::OwningBinary<object::Binary> Binary;
134  std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer;
135 
136  // The contents of the raw profile.
138  // A map from callstack id (same as key in CallStackMap below) to the heap
139  // information recorded for that allocation context.
140  llvm::MapVector<uint64_t, MemInfoBlock> CallstackProfileData;
141  CallStackMap StackMap;
142 
143  // Cached symbolization from PC to Frame.
146 
149 };
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:17
StringRef.h
llvm::SmallVector< SegmentEntry, 16 >
MapVector.h
llvm::memprof::RawMemProfReader::begin
Iterator begin()
Definition: RawMemProfReader.h:65
Error.h
DenseMap.h
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::memprof::RawMemProfReader::RawMemProfReader
RawMemProfReader(std::unique_ptr< MemoryBuffer > DataBuffer)
Definition: RawMemProfReader.h:41
llvm::SubDirectoryType::Bin
@ Bin
GlobalValue.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::memprof::RawMemProfReader::create
static Expected< std::unique_ptr< RawMemProfReader > > create(const Twine &Path, const StringRef ProfiledBinary)
Definition: RawMemProfReader.cpp:177
llvm::memprof::RawMemProfReader::readNextRecord
Error readNextRecord(GuidMemProfRecordPair &GuidRecord)
Definition: RawMemProfReader.cpp:520
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:143
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::memprof::RawMemProfReader::getProfileKind
InstrProfKind getProfileKind() const
Definition: RawMemProfReader.h:73
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:222
llvm::memprof::RawMemProfReader::getFrameMapping
const llvm::DenseMap< FrameId, Frame > & getFrameMapping() const
Definition: RawMemProfReader.h:94
llvm::memprof::RawMemProfReader::RawMemProfReader
RawMemProfReader(std::unique_ptr< llvm::symbolize::SymbolizableModule > Sym, llvm::SmallVectorImpl< SegmentEntry > &Seg, llvm::MapVector< uint64_t, MemInfoBlock > &Prof, CallStackMap &SM)
Definition: RawMemProfReader.h:76
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:1665
ObjectFile.h
llvm::InstrProfKind::MemProf
@ MemProf
llvm::memprof::RawMemProfReader::GuidMemProfRecordPair
std::pair< GlobalValue::GUID, MemProfRecord > GuidMemProfRecordPair
Definition: RawMemProfReader.h:62
Symbolize.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::memprof::RawMemProfReader::Iterator
InstrProfIterator< GuidMemProfRecordPair, RawMemProfReader > Iterator
Definition: RawMemProfReader.h:63
MemProfData.inc
MemProf.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
std
Definition: BitVector.h:851
llvm::memprof::RawMemProfReader::hasFormat
static bool hasFormat(const MemoryBuffer &DataBuffer)
Definition: RawMemProfReader.cpp:213
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:64
memprof
memprof
Definition: MemProfiler.cpp:283
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:100
llvm::InstrProfIterator
A file format agnostic iterator over profiling data.
Definition: InstrProfReader.h:45
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:235
llvm::memprof::FrameId
uint64_t FrameId
Definition: MemProf.h:137
SetVector.h