LLVM 17.0.0git
Go to the documentation of this file.
3//===- MemProfReader.h - Instrumented memory profiling reader ---*- C++ -*-===//
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
11// This file contains support for reading MemProf profiling data.
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"
26#include "llvm/Support/Error.h"
29#include <cstddef>
31namespace llvm {
32namespace memprof {
34// Map from id (recorded from sanitizer stack depot) to virtual addresses for
35// each program counter address in the callstack.
43 // Prints the contents of the profile in YAML format.
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);
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);
60 using GuidMemProfRecordPair = std::pair<GlobalValue::GUID, MemProfRecord>;
62 Iterator end() { return Iterator(); }
64 Iter = FunctionProfileData.begin();
65 return Iterator(this);
66 }
70 // The RawMemProfReader only holds memory profile information.
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.
83 // If there is an error here then the mock symbolizer has not been
84 // initialized properly.
85 if (Error E = symbolizeAndFilterStackFrames())
86 report_fatal_error(std::move(E));
87 if (Error E = mapRawProfileToRecords())
88 report_fatal_error(std::move(E));
89 }
91 // Return a const reference to the internal Id to Frame mappings.
93 return IdToFrame;
94 }
96 // Return a const reference to the internal function profile data.
99 return FunctionProfileData;
100 }
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();
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 }
126 object::SectionedAddress getModuleOffset(uint64_t VirtualAddress);
128 object::OwningBinary<object::Binary> Binary;
129 std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer;
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;
138 // Cached symbolization from PC to Frame.
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.
150} // namespace memprof
151} // namespace llvm
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
This file implements a map that provides insertion order iteration.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Tagged union holding either a T or a Error.
Definition: Error.h:470
A file format agnostic iterator over profiling data.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
typename VectorType::iterator iterator
Definition: MapVector.h:50
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
InstrProfIterator< GuidMemProfRecordPair, RawMemProfReader > Iterator
RawMemProfReader & operator=(const RawMemProfReader &)=delete
const llvm::MapVector< GlobalValue::GUID, IndexedMemProfRecord > & getProfileData() const
RawMemProfReader(const RawMemProfReader &)=delete
const llvm::DenseMap< FrameId, Frame > & getFrameMapping() const
std::pair< GlobalValue::GUID, MemProfRecord > GuidMemProfRecordPair
static Expected< std::unique_ptr< RawMemProfReader > > create(const Twine &Path, const StringRef ProfiledBinary, bool KeepName=false)
Error readNextRecord(GuidMemProfRecordPair &GuidRecord)
RawMemProfReader(std::unique_ptr< llvm::symbolize::SymbolizableModule > Sym, llvm::SmallVectorImpl< SegmentEntry > &Seg, llvm::MapVector< uint64_t, MemInfoBlock > &Prof, CallStackMap &SM, bool KeepName=false)
static bool hasFormat(const MemoryBuffer &DataBuffer)
InstrProfKind getProfileKind() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
uint64_t FrameId
Definition: MemProf.h:138
llvm::DenseMap< uint64_t, llvm::SmallVector< uint64_t > > CallStackMap
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
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:1909
An enum describing the attributes of an instrumented profile.
Definition: InstrProf.h:287
Definition: BitVector.h:851