LLVM  14.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"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Error.h"
20 #include <list>
21 #include <utility>
22 #include <vector>
23 
24 namespace llvm {
25 namespace xray {
26 
27 class Profile;
28 
29 // We forward declare the Trace type for turning a Trace into a Profile.
30 class Trace;
31 
32 /// This function will attempt to load an XRay Profiling Mode profile from the
33 /// provided |Filename|.
34 ///
35 /// For any errors encountered in the loading of the profile data from
36 /// |Filename|, this function will return an Error condition appropriately.
37 Expected<Profile> loadProfile(StringRef Filename);
38 
39 /// This algorithm will merge two Profile instances into a single Profile
40 /// instance, aggregating blocks by Thread ID.
42 
43 /// This algorithm will merge two Profile instances into a single Profile
44 /// instance, aggregating blocks by function call stack.
45 Profile mergeProfilesByStack(const Profile &L, const Profile &R);
46 
47 /// This function takes a Trace and creates a Profile instance from it.
48 Expected<Profile> profileFromTrace(const Trace &T);
49 
50 /// Profile instances are thread-compatible.
51 class Profile {
52 public:
53  using ThreadID = uint64_t;
54  using PathID = unsigned;
55  using FuncID = int32_t;
56 
57  struct Data {
60  };
61 
62  struct Block {
64  std::vector<std::pair<PathID, Data>> PathData;
65  };
66 
67  /// Provides a sequence of function IDs from a previously interned PathID.
68  ///
69  /// Returns an error if |P| had not been interned before into the Profile.
70  ///
72 
73  /// The stack represented in |P| must be in stack order (leaf to root). This
74  /// will always return the same PathID for |P| that has the same sequence.
76 
77  /// Appends a fully-formed Block instance into the Profile.
78  ///
79  /// Returns an error condition in the following cases:
80  ///
81  /// - The PathData component of the Block is empty
82  ///
83  Error addBlock(Block &&B);
84 
85  Profile() = default;
86  ~Profile() = default;
87 
88  Profile(Profile &&O) noexcept
89  : Blocks(std::move(O.Blocks)), NodeStorage(std::move(O.NodeStorage)),
90  Roots(std::move(O.Roots)), PathIDMap(std::move(O.PathIDMap)),
91  NextID(O.NextID) {}
92 
93  Profile &operator=(Profile &&O) noexcept {
94  Blocks = std::move(O.Blocks);
95  NodeStorage = std::move(O.NodeStorage);
96  Roots = std::move(O.Roots);
97  PathIDMap = std::move(O.PathIDMap);
98  NextID = O.NextID;
99  return *this;
100  }
101 
102  Profile(const Profile &);
103  Profile &operator=(const Profile &);
104 
105  friend void swap(Profile &L, Profile &R) {
106  using std::swap;
107  swap(L.Blocks, R.Blocks);
108  swap(L.NodeStorage, R.NodeStorage);
109  swap(L.Roots, R.Roots);
110  swap(L.PathIDMap, R.PathIDMap);
111  swap(L.NextID, R.NextID);
112  }
113 
114 private:
115  using BlockList = std::list<Block>;
116 
117  struct TrieNode {
118  FuncID Func = 0;
119  std::vector<TrieNode *> Callees{};
120  TrieNode *Caller = nullptr;
121  PathID ID = 0;
122  };
123 
124  // List of blocks associated with a Profile.
125  BlockList Blocks;
126 
127  // List of TrieNode elements we've seen.
128  std::list<TrieNode> NodeStorage;
129 
130  // List of call stack roots.
131  SmallVector<TrieNode *, 4> Roots;
132 
133  // Reverse mapping between a PathID to a TrieNode*.
134  DenseMap<PathID, TrieNode *> PathIDMap;
135 
136  // Used to identify paths.
137  PathID NextID = 1;
138 
139 public:
140  using const_iterator = BlockList::const_iterator;
141  const_iterator begin() const { return Blocks.begin(); }
142  const_iterator end() const { return Blocks.end(); }
143  bool empty() const { return Blocks.empty(); }
144 };
145 
146 } // namespace xray
147 } // namespace llvm
148 
149 #endif
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::xray::loadProfile
Expected< Profile > loadProfile(StringRef Filename)
This function will attempt to load an XRay Profiling Mode profile from the provided |Filename|.
Definition: Profile.cpp:262
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::xray::Profile::~Profile
~Profile()=default
llvm::xray::Profile::operator=
Profile & operator=(Profile &&O) noexcept
Definition: Profile.h:93
Error.h
DenseMap.h
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:193
llvm::xray::profileFromTrace
Expected< Profile > profileFromTrace(const Trace &T)
This function takes a Trace and creates a Profile instance from it.
Definition: Profile.cpp:326
llvm::xray::Profile::Data::CumulativeLocalTime
uint64_t CumulativeLocalTime
Definition: Profile.h:59
llvm::xray::Profile::begin
const_iterator begin() const
Definition: Profile.h:141
llvm::xray::Profile::Profile
Profile()=default
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::xray::Profile::Data
Definition: Profile.h:57
llvm::xray::mergeProfilesByStack
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:232
llvm::xray::Profile::Block
Definition: Profile.h:62
llvm::xray::Profile::addBlock
Error addBlock(Block &&B)
Appends a fully-formed Block instance into the Profile.
Definition: Profile.cpp:121
llvm::xray::Profile::Data::CallCount
uint64_t CallCount
Definition: Profile.h:58
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:192
uint64_t
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::xray::Profile::swap
friend void swap(Profile &L, Profile &R)
Definition: Profile.h:105
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:840
llvm::xray::Profile::internPath
PathID internPath(ArrayRef< FuncID > P)
The stack represented in |P| must be in stack order (leaf to root).
Definition: Profile.cpp:143
llvm::xray::Profile::FuncID
int32_t FuncID
Definition: Profile.h:55
llvm::xray::Profile
Profile instances are thread-compatible.
Definition: Profile.h:51
llvm::xray::Profile::PathID
unsigned PathID
Definition: Profile.h:54
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
Profile
Load MIR Sample Profile
Definition: MIRSampleProfile.cpp:62
llvm::xray::Profile::Block::PathData
std::vector< std::pair< PathID, Data > > PathData
Definition: Profile.h:64
llvm::xray::Profile::Block::Thread
ThreadID Thread
Definition: Profile.h:63
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::xray::mergeProfilesByThread
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:193
llvm::xray::Profile::Profile
Profile(Profile &&O) noexcept
Definition: Profile.h:88
SmallVector.h
llvm::xray::Profile::end
const_iterator end() const
Definition: Profile.h:142
llvm::xray::Profile::empty
bool empty() const
Definition: Profile.h:143
llvm::xray::Profile::expandPath
Expected< std::vector< FuncID > > expandPath(PathID P) const
Provides a sequence of function IDs from a previously interned PathID.
Definition: Profile.cpp:131
llvm::xray::Profile::const_iterator
BlockList::const_iterator const_iterator
Definition: Profile.h:140
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37