32#define DEBUG_TYPE "mcpseudoprobe"
35using namespace support;
57 assert((LastProbe || IsSentinel) &&
58 "Last probe should not be null for non-sentinel probes");
67 assert(
Type <= 0xF &&
"Probe type too big to encode, exceeding 15");
71 assert(NewAttributes <= 0x7 &&
72 "Probe attributes too big to encode, exceeding 7");
121 if (InlineStack.
empty()) {
131 if (!InlineStack.
empty()) {
132 auto Iter = InlineStack.
begin();
133 auto Index = std::get<1>(*Iter);
135 for (; Iter != InlineStack.
end(); Iter++) {
137 Cur = Cur->getOrAddNode(
InlineSite(std::get<0>(*Iter), Index));
138 Index = std::get<1>(*Iter);
143 Cur->Probes.push_back(Probe);
150 dbgs() <<
"Group [\n";
153 assert(!
isRoot() &&
"Root should be handled separately");
164 bool NeedSentinel =
false;
167 "Starting probe of a top-level function should be a sentinel probe");
178 LastProbe->
emit(MCOS,
nullptr);
181 for (
const auto &Probe :
Probes) {
182 Probe.emit(MCOS, LastProbe);
188 using InlineeType = std::pair<InlineSite, MCPseudoProbeInlineTree *>;
189 std::vector<InlineeType> Inlinees;
191 Inlinees.emplace_back(Child.first, Child.second.get());
194 for (
const auto &Inlinee : Inlinees) {
199 dbgs() <<
"InlineSite: " << std::get<1>(Inlinee.first) <<
"\n";
202 Inlinee.second->emit(MCOS, LastProbe);
215 Vec.
reserve(MCProbeDivisions.size());
216 for (
auto &ProbeSec : MCProbeDivisions)
219 I.value().setOrdinal(
I.index());
221 return A.first->getSection().getOrdinal() <
222 B.first->getSection().getOrdinal();
224 for (
auto [FuncSym, RootPtr] : Vec) {
225 const auto &Root = *RootPtr;
227 FuncSym->getSection())) {
233 using InlineeType = std::pair<InlineSite, MCPseudoProbeInlineTree *>;
234 std::vector<InlineeType> Inlinees;
235 for (
const auto &Child : Root.getChildren())
236 Inlinees.emplace_back(Child.first, Child.second.get());
239 for (
const auto &Inlinee : Inlinees) {
247 Inlinee.second->emit(MCOS, Probe);
263 if (ProbeSections.empty())
269 ProbeSections.emit(MCOS);
274 auto It = GUID2FuncMAP.
find(GUID);
275 assert(It != GUID2FuncMAP.end() &&
276 "Probe function must exist for a valid GUID");
299 std::reverse(ContextStack.
begin() + Begin, ContextStack.
end());
304 std::ostringstream OContextStr;
307 for (
auto &Cxt : ContextStack) {
308 if (OContextStr.str().size())
309 OContextStr <<
" @ ";
310 OContextStr << Cxt.first.str() <<
":" << Cxt.second;
312 return OContextStr.str();
320 bool ShowName)
const {
324 OS << FuncName.
str() <<
" ";
328 OS <<
"Index: " <<
Index <<
" ";
333 if (InlineContextStr.size()) {
335 OS << InlineContextStr;
340template <
typename T>
ErrorOr<T> MCPseudoProbeDecoder::readUnencodedNumber() {
341 if (Data +
sizeof(
T) > End) {
342 return std::error_code();
344 T Val = endian::readNext<T, llvm::endianness::little>(Data);
348template <
typename T>
ErrorOr<T> MCPseudoProbeDecoder::readUnsignedNumber() {
349 unsigned NumBytesRead = 0;
351 if (Val > std::numeric_limits<T>::max() || (Data + NumBytesRead > End)) {
352 return std::error_code();
354 Data += NumBytesRead;
358template <
typename T>
ErrorOr<T> MCPseudoProbeDecoder::readSignedNumber() {
359 unsigned NumBytesRead = 0;
361 if (Val > std::numeric_limits<T>::max() || (Data + NumBytesRead > End)) {
362 return std::error_code();
364 Data += NumBytesRead;
370 if (Data +
Size > End) {
371 return std::error_code();
397 if (!readUnencodedNumber<uint64_t>())
400 if (!readUnencodedNumber<uint64_t>())
403 auto ErrorOrNameSize = readUnsignedNumber<uint32_t>();
404 if (!ErrorOrNameSize)
407 if (!readString(*ErrorOrNameSize))
411 assert(Data == End &&
"Have unprocessed data in pseudo_probe_desc section");
412 GUID2FuncDescMap.reserve(FuncDescCount);
426 GUID2FuncDescMap.emplace_back(
427 GUID, Hash, IsMMapped ?
Name :
Name.copy(FuncNameAllocator));
429 assert(Data == End &&
"Have unprocessed data in pseudo_probe_desc section");
430 assert(GUID2FuncDescMap.size() == FuncDescCount &&
431 "Mismatching function description count pre- and post-parsing");
433 return LHS.FuncGUID <
RHS.FuncGUID;
438template <
bool IsTopLevelFunc>
441 const Uint64Set &GuidFilter,
const Uint64Map &FuncStartAddrs,
447 if (IsTopLevelFunc) {
449 Index = CurChildIndex;
459 if (IsTopLevelFunc && !GuidFilter.empty() && !GuidFilter.count(
Guid))
468 if (IsTopLevelFunc && !EncodingIsAddrBased) {
469 if (
auto V = FuncStartAddrs.lookup(
Guid))
482 for (std::size_t
I = 0;
I < NodeCount;
I++) {
500 if (
auto V = FuncStartAddrs.lookup(
Addr))
507 EncodingIsAddrBased =
true;
528 InlineTreeVec.resize(InlineTreeVec.size() + ChildrenToProcess);
533 buildAddress2ProbeMap<false>(Cur, LastAddr, GuidFilter, FuncStartAddrs,
I);
538template <
bool IsTopLevelFunc>
544 if (!readUnsignedNumber<uint32_t>())
548 auto ErrorOrCurGuid = readUnencodedNumber<uint64_t>();
554 if (IsTopLevelFunc) {
562 auto ErrorOrNodeCount = readUnsignedNumber<uint32_t>();
563 if (!ErrorOrNodeCount)
565 uint32_t NodeCount = std::move(*ErrorOrNodeCount);
569 auto ErrorOrCurChildrenToProcess = readUnsignedNumber<uint32_t>();
570 if (!ErrorOrCurChildrenToProcess)
572 uint32_t ChildrenToProcess = std::move(*ErrorOrCurChildrenToProcess);
575 for (std::size_t
I = 0;
I < NodeCount;
I++) {
577 if (!readUnsignedNumber<uint32_t>())
581 auto ErrorOrValue = readUnencodedNumber<uint8_t>();
589 if (!readSignedNumber<int64_t>())
593 if (!readUnencodedNumber<int64_t>())
599 if (!readUnsignedNumber<uint32_t>())
607 ProbeCount += CurrentProbeCount;
608 InlinedCount += ChildrenToProcess;
612 if (!countRecords<false>(Discard, ProbeCount, InlinedCount, GuidFilter))
627 bool Discard =
false;
629 if (!countRecords<true>(Discard, ProbeCount, InlinedCount, GuidFilter))
631 TopLevelFuncs += !Discard;
633 assert(Data == End &&
"Have unprocessed data in pseudo_probe section");
634 PseudoProbeVec.reserve(ProbeCount);
635 InlineTreeVec.reserve(InlinedCount);
638 InlineTreeVec.resize(TopLevelFuncs);
646 CurChildIndex += buildAddress2ProbeMap<true>(
647 &DummyInlineRoot, LastAddr, GuidFilter, FuncStartAddrs, CurChildIndex);
648 assert(Data == End &&
"Have unprocessed data in pseudo_probe section");
649 assert(PseudoProbeVec.size() == ProbeCount &&
650 "Mismatching probe count pre- and post-parsing");
651 assert(InlineTreeVec.size() == InlinedCount &&
652 "Mismatching function records count pre- and post-parsing");
654 std::vector<std::pair<uint64_t, uint32_t>> SortedA2P(ProbeCount);
656 SortedA2P[
I] = {Probe.getAddress(),
I};
658 Address2ProbesMap.reserve(ProbeCount);
660 Address2ProbesMap.emplace_back(PseudoProbeVec[
I]);
666 OS <<
"Pseudo Probe Desc:\n";
667 for (
auto &
I : GUID2FuncDescMap)
675 Probe.print(
OS, GUID2FuncDescMap,
true);
688 Probe.print(
OS, GUID2FuncDescMap,
true);
696 if (Probe.isCall()) {
718 auto It = GUID2FuncDescMap.
find(GUID);
719 assert(It != GUID2FuncDescMap.end() &&
"Function descriptor doesn't exist");
726 bool IncludeLeaf)
const {
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static const MCExpr * buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, const MCSymbol *B, SMLoc Loc)
static const char * PseudoProbeTypeStr[3]
static StringRef getProbeFNameForGUID(const GUIDProbeFunctionMap &GUID2FuncMAP, uint64_t GUID)
static const MCExpr * buildSymbolDiff(MCObjectStreamer *MCOS, const MCSymbol *A, const MCSymbol *B)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
auto find(uint64_t From, uint64_t To) const
Represents either an error or a value T.
auto find(uint64_t GUID) const
static const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Context object for machine code objects.
MCPseudoProbeTable & getMCPseudoProbeTable()
const MCObjectFileInfo * getObjectFileInfo() const
F * allocFragment(Args &&...args)
InlineSite getInlineSite() const
bool hasInlineSite() const
void setProbes(MutableArrayRef< MCDecodedPseudoProbe > ProbesRef)
void print(raw_ostream &OS, const GUIDProbeFunctionMap &GUID2FuncMAP, bool ShowName) const
std::string getInlineContextStr(const GUIDProbeFunctionMap &GUID2FuncMAP) const
MCDecodedPseudoProbeInlineTree * getInlineTreeNode() const
void getInlineContext(SmallVectorImpl< MCPseudoProbeFrameLocation > &ContextStack, const GUIDProbeFunctionMap &GUID2FuncMAP) const
Base class for the full range of assembler expressions which are needed for parsing.
MCSection * getPseudoProbeSection(const MCSection &TextSec) const
Streaming object file generation interface.
MCAssembler & getAssembler()
MCAssembler * getAssemblerPtr() override
void insert(MCFragment *F)
uint32_t getIndex() const
uint8_t getAttributes() const
bool buildAddress2ProbeMap(const uint8_t *Start, std::size_t Size, const Uint64Set &GuildFilter, const Uint64Map &FuncStartAddrs)
void printProbesForAllAddresses(raw_ostream &OS)
void printGUID2FuncDescMap(raw_ostream &OS)
bool buildGUID2FuncDescMap(const uint8_t *Start, std::size_t Size, bool IsMMapped=false)
void printProbeForAddress(raw_ostream &OS, uint64_t Address)
void getInlineContextForProbe(const MCDecodedPseudoProbe *Probe, SmallVectorImpl< MCPseudoProbeFrameLocation > &InlineContextStack, bool IncludeLeaf) const
const MCPseudoProbeFuncDesc * getInlinerDescForProbe(const MCDecodedPseudoProbe *Probe) const
bool countRecords(bool &Discard, uint32_t &ProbeCount, uint32_t &InlinedCount, const Uint64Set &GuidFilter)
const MCDecodedPseudoProbe * getCallProbeForAddr(uint64_t Address) const
const MCPseudoProbeFuncDesc * getFuncDescForGUID(uint64_t GUID) const
std::unordered_map< InlineSite, std::unique_ptr< MCPseudoProbeInlineTree >, InlineSiteHash > Children
MCPseudoProbeInlineTree * getOrAddNode(const InlineSite &Site)
std::vector< MCPseudoProbe > Probes
MCPseudoProbeInlineTreeBase< std::vector< MCPseudoProbe >, MCPseudoProbeInlineTree, std::unordered_map< InlineSite, std::unique_ptr< MCPseudoProbeInlineTree >, InlineSiteHash > > * Parent
InlinedProbeTreeMap & getChildren()
void emit(MCObjectStreamer *MCOS, const MCPseudoProbe *&LastProbe)
void addPseudoProbe(const MCPseudoProbe &Probe, const MCPseudoProbeInlineStack &InlineStack)
void emit(MCObjectStreamer *MCOS)
MCPseudoProbeSections & getProbeSections()
static int DdgPrintIndent
static void emit(MCObjectStreamer *MCOS)
Instances of this class represent a pseudo probe instance for a pseudo probe table entry,...
MCSymbol * getLabel() const
void emit(MCObjectStreamer *MCOS, const MCPseudoProbe *LastProbe) const
MCContext & getContext() const
unsigned emitULEB128IntValue(uint64_t Value, unsigned PadTo=0)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
void emitInt64(uint64_t Value)
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
unsigned emitSLEB128IntValue(int64_t Value)
Special case of EmitSLEB128Value that avoids the client having to pass in a MCExpr for constant integ...
void emitInt8(uint64_t Value)
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
MutableArrayRef< T > take_back(size_t N=1) const
Return a copy of *this with only the last N elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
constexpr size_t NameSize
uint64_t MD5Hash(const FunctionId &Obj)
This is an optimization pass for GlobalISel generic memory operations.
static bool isSentinelProbe(uint32_t Flags)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
std::tuple< uint64_t, uint32_t > InlineSite
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool hasDiscriminator(uint32_t Flags)
std::pair< StringRef, uint32_t > MCPseudoProbeFrameLocation
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
void print(raw_ostream &OS)
Function object to check whether the first component of a container supported by std::get (like std::...