LLVM 22.0.0git
Profile.h
Go to the documentation of this file.
1//===- Profile.h - XRay Profile Abstraction -------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines the XRay Profile class representing the latency profile generated by
10// XRay's profiling mode.
11//
12//===----------------------------------------------------------------------===//
13#ifndef LLVM_XRAY_PROFILE_H
14#define LLVM_XRAY_PROFILE_H
15
16#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Error.h"
21#include <list>
22#include <utility>
23#include <vector>
24
25namespace llvm {
26namespace xray {
27
28class Profile;
29
30// We forward declare the Trace type for turning a Trace into a Profile.
31class Trace;
32
33/// This function will attempt to load an XRay Profiling Mode profile from the
34/// provided |Filename|.
35///
36/// For any errors encountered in the loading of the profile data from
37/// |Filename|, this function will return an Error condition appropriately.
38LLVM_ABI Expected<Profile> loadProfile(StringRef Filename);
39
40/// This algorithm will merge two Profile instances into a single Profile
41/// instance, aggregating blocks by Thread ID.
43
44/// This algorithm will merge two Profile instances into a single Profile
45/// instance, aggregating blocks by function call stack.
47
48/// This function takes a Trace and creates a Profile instance from it.
49LLVM_ABI Expected<Profile> profileFromTrace(const Trace &T);
50
51/// Profile instances are thread-compatible.
52class Profile {
53public:
56 using FuncID = int32_t;
57
58 struct Data {
61 };
62
63 struct Block {
65 std::vector<std::pair<PathID, Data>> PathData;
66 };
67
68 /// Provides a sequence of function IDs from a previously interned PathID.
69 ///
70 /// Returns an error if |P| had not been interned before into the Profile.
71 ///
73
74 /// The stack represented in |P| must be in stack order (leaf to root). This
75 /// will always return the same PathID for |P| that has the same sequence.
77
78 /// Appends a fully-formed Block instance into the Profile.
79 ///
80 /// Returns an error condition in the following cases:
81 ///
82 /// - The PathData component of the Block is empty
83 ///
85
86 Profile() = default;
87 ~Profile() = default;
88
89 Profile(Profile &&O) noexcept
90 : Blocks(std::move(O.Blocks)), NodeStorage(std::move(O.NodeStorage)),
91 Roots(std::move(O.Roots)), PathIDMap(std::move(O.PathIDMap)),
92 NextID(O.NextID) {}
93
94 Profile &operator=(Profile &&O) noexcept {
95 Blocks = std::move(O.Blocks);
96 NodeStorage = std::move(O.NodeStorage);
97 Roots = std::move(O.Roots);
98 PathIDMap = std::move(O.PathIDMap);
99 NextID = O.NextID;
100 return *this;
101 }
102
103 LLVM_ABI Profile(const Profile &);
105
106 friend void swap(Profile &L, Profile &R) {
107 using std::swap;
108 swap(L.Blocks, R.Blocks);
109 swap(L.NodeStorage, R.NodeStorage);
110 swap(L.Roots, R.Roots);
111 swap(L.PathIDMap, R.PathIDMap);
112 swap(L.NextID, R.NextID);
113 }
114
115private:
116 using BlockList = std::list<Block>;
117
118 struct TrieNode {
119 FuncID Func = 0;
120 std::vector<TrieNode *> Callees{};
121 TrieNode *Caller = nullptr;
122 PathID ID = 0;
123 };
124
125 // List of blocks associated with a Profile.
126 BlockList Blocks;
127
128 // List of TrieNode elements we've seen.
129 std::list<TrieNode> NodeStorage;
130
131 // List of call stack roots.
132 SmallVector<TrieNode *, 4> Roots;
133
134 // Reverse mapping between a PathID to a TrieNode*.
135 DenseMap<PathID, TrieNode *> PathIDMap;
136
137 // Used to identify paths.
138 PathID NextID = 1;
139
140public:
141 using const_iterator = BlockList::const_iterator;
142 const_iterator begin() const { return Blocks.begin(); }
143 const_iterator end() const { return Blocks.end(); }
144 bool empty() const { return Blocks.empty(); }
145};
146
147} // namespace xray
148} // namespace llvm
149
150#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:507
Load MIR Sample Profile
#define P(N)
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
Profile instances are thread-compatible.
Definition: Profile.h:52
Profile & operator=(Profile &&O) noexcept
Definition: Profile.h:94
LLVM_ABI PathID internPath(ArrayRef< FuncID > P)
The stack represented in |P| must be in stack order (leaf to root).
Definition: Profile.cpp:142
BlockList::const_iterator const_iterator
Definition: Profile.h:141
LLVM_ABI Expected< std::vector< FuncID > > expandPath(PathID P) const
Provides a sequence of function IDs from a previously interned PathID.
Definition: Profile.cpp:130
const_iterator end() const
Definition: Profile.h:143
unsigned PathID
Definition: Profile.h:55
LLVM_ABI Error addBlock(Block &&B)
Appends a fully-formed Block instance into the Profile.
Definition: Profile.cpp:120
friend void swap(Profile &L, Profile &R)
Definition: Profile.h:106
Profile(Profile &&O) noexcept
Definition: Profile.h:89
int32_t FuncID
Definition: Profile.h:56
const_iterator begin() const
Definition: Profile.h:142
bool empty() const
Definition: Profile.h:144
LLVM_ABI Profile mergeProfilesByStack(const Profile &L, const Profile &R)
This algorithm will merge two Profile instances into a single Profile instance, aggregating blocks by...
Definition: Profile.cpp:231
LLVM_ABI Expected< Profile > profileFromTrace(const Trace &T)
This function takes a Trace and creates a Profile instance from it.
Definition: Profile.cpp:325
LLVM_ABI Expected< Profile > loadProfile(StringRef Filename)
This function will attempt to load an XRay Profiling Mode profile from the provided |Filename|.
Definition: Profile.cpp:261
LLVM_ABI Profile mergeProfilesByThread(const Profile &L, const Profile &R)
This algorithm will merge two Profile instances into a single Profile instance, aggregating blocks by...
Definition: Profile.cpp:192
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:858
std::vector< std::pair< PathID, Data > > PathData
Definition: Profile.h:65
uint64_t CumulativeLocalTime
Definition: Profile.h:60