LLVM 22.0.0git
Profile.cpp
Go to the documentation of this file.
1//===- Profile.cpp - 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#include "llvm/XRay/Profile.h"
14
16#include "llvm/Support/Error.h"
18#include "llvm/XRay/Trace.h"
19#include <memory>
20
21namespace llvm {
22namespace xray {
23
25 // We need to re-create all the tries from the original (O), into the current
26 // Profile being initialized, through the Block instances we see.
27 for (const auto &Block : O) {
28 Blocks.push_back({Block.Thread, {}});
29 auto &B = Blocks.back();
30 for (const auto &PathData : Block.PathData)
31 B.PathData.push_back({internPath(cantFail(O.expandPath(PathData.first))),
32 PathData.second});
33 }
34}
35
37 Profile P = O;
38 *this = std::move(P);
39 return *this;
40}
41
42namespace {
43
44struct BlockHeader {
47 uint64_t Thread;
48};
49
50static Expected<BlockHeader> readBlockHeader(DataExtractor &Extractor,
52 BlockHeader H;
53 uint64_t CurrentOffset = Offset;
54 H.Size = Extractor.getU32(&Offset);
55 if (Offset == CurrentOffset)
56 return make_error<StringError>(
57 Twine("Error parsing block header size at offset '") +
58 Twine(CurrentOffset) + "'",
59 std::make_error_code(std::errc::invalid_argument));
60 CurrentOffset = Offset;
61 H.Number = Extractor.getU32(&Offset);
62 if (Offset == CurrentOffset)
63 return make_error<StringError>(
64 Twine("Error parsing block header number at offset '") +
65 Twine(CurrentOffset) + "'",
66 std::make_error_code(std::errc::invalid_argument));
67 CurrentOffset = Offset;
68 H.Thread = Extractor.getU64(&Offset);
69 if (Offset == CurrentOffset)
70 return make_error<StringError>(
71 Twine("Error parsing block header thread id at offset '") +
72 Twine(CurrentOffset) + "'",
73 std::make_error_code(std::errc::invalid_argument));
74 return H;
75}
76
77static Expected<std::vector<Profile::FuncID>> readPath(DataExtractor &Extractor,
79 // We're reading a sequence of int32_t's until we find a 0.
80 std::vector<Profile::FuncID> Path;
81 auto CurrentOffset = Offset;
82 int32_t FuncId;
83 do {
84 FuncId = Extractor.getSigned(&Offset, 4);
85 if (CurrentOffset == Offset)
86 return make_error<StringError>(
87 Twine("Error parsing path at offset '") + Twine(CurrentOffset) + "'",
88 std::make_error_code(std::errc::invalid_argument));
89 CurrentOffset = Offset;
90 Path.push_back(FuncId);
91 } while (FuncId != 0);
92 return std::move(Path);
93}
94
95static Expected<Profile::Data> readData(DataExtractor &Extractor,
97 // We expect a certain number of elements for Data:
98 // - A 64-bit CallCount
99 // - A 64-bit CumulativeLocalTime counter
100 Profile::Data D;
101 auto CurrentOffset = Offset;
102 D.CallCount = Extractor.getU64(&Offset);
103 if (CurrentOffset == Offset)
104 return make_error<StringError>(
105 Twine("Error parsing call counts at offset '") + Twine(CurrentOffset) +
106 "'",
107 std::make_error_code(std::errc::invalid_argument));
108 CurrentOffset = Offset;
109 D.CumulativeLocalTime = Extractor.getU64(&Offset);
110 if (CurrentOffset == Offset)
111 return make_error<StringError>(
112 Twine("Error parsing cumulative local time at offset '") +
113 Twine(CurrentOffset) + "'",
114 std::make_error_code(std::errc::invalid_argument));
115 return D;
116}
117
118} // namespace
119
121 if (B.PathData.empty())
122 return make_error<StringError>(
123 "Block may not have empty path data.",
124 std::make_error_code(std::errc::invalid_argument));
125
126 Blocks.emplace_back(std::move(B));
127 return Error::success();
128}
129
131 auto It = PathIDMap.find(P);
132 if (It == PathIDMap.end())
133 return make_error<StringError>(
134 Twine("PathID not found: ") + Twine(P),
135 std::make_error_code(std::errc::invalid_argument));
136 std::vector<Profile::FuncID> Path;
137 for (auto Node = It->second; Node; Node = Node->Caller)
138 Path.push_back(Node->Func);
139 return std::move(Path);
140}
141
143 if (P.empty())
144 return 0;
145
146 auto RootToLeafPath = reverse(P);
147
148 // Find the root.
149 auto It = RootToLeafPath.begin();
150 auto PathRoot = *It++;
151 auto RootIt =
152 find_if(Roots, [PathRoot](TrieNode *N) { return N->Func == PathRoot; });
153
154 // If we've not seen this root before, remember it.
155 TrieNode *Node = nullptr;
156 if (RootIt == Roots.end()) {
157 NodeStorage.emplace_back();
158 Node = &NodeStorage.back();
159 Node->Func = PathRoot;
160 Roots.push_back(Node);
161 } else {
162 Node = *RootIt;
163 }
164
165 // Now traverse the path, re-creating if necessary.
166 while (It != RootToLeafPath.end()) {
167 auto NodeFuncID = *It++;
168 auto CalleeIt = find_if(Node->Callees, [NodeFuncID](TrieNode *N) {
169 return N->Func == NodeFuncID;
170 });
171 if (CalleeIt == Node->Callees.end()) {
172 NodeStorage.emplace_back();
173 auto NewNode = &NodeStorage.back();
174 NewNode->Func = NodeFuncID;
175 NewNode->Caller = Node;
176 Node->Callees.push_back(NewNode);
177 Node = NewNode;
178 } else {
179 Node = *CalleeIt;
180 }
181 }
182
183 // At this point, Node *must* be pointing at the leaf.
184 assert(Node->Func == P.front());
185 if (Node->ID == 0) {
186 Node->ID = NextID++;
187 PathIDMap.insert({Node->ID, Node});
188 }
189 return Node->ID;
190}
191
193 Profile Merged;
195 using PathDataMapPtr = std::unique_ptr<PathDataMap>;
196 using PathDataVector = decltype(Profile::Block::PathData);
197 using ThreadProfileIndexMap = DenseMap<Profile::ThreadID, PathDataMapPtr>;
198 ThreadProfileIndexMap ThreadProfileIndex;
199
200 for (const auto &P : {std::ref(L), std::ref(R)})
201 for (const auto &Block : P.get()) {
202 ThreadProfileIndexMap::iterator It;
203 std::tie(It, std::ignore) = ThreadProfileIndex.insert(
204 {Block.Thread, std::make_unique<PathDataMap>()});
205 for (const auto &PathAndData : Block.PathData) {
206 auto &PathID = PathAndData.first;
207 auto &Data = PathAndData.second;
208 auto NewPathID =
209 Merged.internPath(cantFail(P.get().expandPath(PathID)));
210 PathDataMap::iterator PathDataIt;
211 bool Inserted;
212 std::tie(PathDataIt, Inserted) = It->second->insert({NewPathID, Data});
213 if (!Inserted) {
214 auto &ExistingData = PathDataIt->second;
215 ExistingData.CallCount += Data.CallCount;
216 ExistingData.CumulativeLocalTime += Data.CumulativeLocalTime;
217 }
218 }
219 }
220
221 for (const auto &IndexedThreadBlock : ThreadProfileIndex) {
222 PathDataVector PathAndData;
223 PathAndData.reserve(IndexedThreadBlock.second->size());
224 copy(*IndexedThreadBlock.second, std::back_inserter(PathAndData));
225 cantFail(
226 Merged.addBlock({IndexedThreadBlock.first, std::move(PathAndData)}));
227 }
228 return Merged;
229}
230
232 Profile Merged;
234 PathDataMap PathData;
235 using PathDataVector = decltype(Profile::Block::PathData);
236 for (const auto &P : {std::ref(L), std::ref(R)})
237 for (const auto &Block : P.get())
238 for (const auto &PathAndData : Block.PathData) {
239 auto &PathId = PathAndData.first;
240 auto &Data = PathAndData.second;
241 auto NewPathID =
242 Merged.internPath(cantFail(P.get().expandPath(PathId)));
243 PathDataMap::iterator PathDataIt;
244 bool Inserted;
245 std::tie(PathDataIt, Inserted) = PathData.insert({NewPathID, Data});
246 if (!Inserted) {
247 auto &ExistingData = PathDataIt->second;
248 ExistingData.CallCount += Data.CallCount;
249 ExistingData.CumulativeLocalTime += Data.CumulativeLocalTime;
250 }
251 }
252
253 // In the end there's a single Block, for thread 0.
254 PathDataVector Block;
255 Block.reserve(PathData.size());
256 copy(PathData, std::back_inserter(Block));
257 cantFail(Merged.addBlock({0, std::move(Block)}));
258 return Merged;
259}
260
263 if (!FdOrErr)
264 return FdOrErr.takeError();
265
266 uint64_t FileSize;
267 if (auto EC = sys::fs::file_size(Filename, FileSize))
268 return make_error<StringError>(
269 Twine("Cannot get filesize of '") + Filename + "'", EC);
270
271 std::error_code EC;
274 EC);
275 sys::fs::closeFile(*FdOrErr);
276 if (EC)
277 return make_error<StringError>(
278 Twine("Cannot mmap profile '") + Filename + "'", EC);
279 StringRef Data(MappedFile.data(), MappedFile.size());
280
281 Profile P;
282 uint64_t Offset = 0;
283 DataExtractor Extractor(Data, true, 8);
284
285 // For each block we get from the file:
286 while (Offset != MappedFile.size()) {
287 auto HeaderOrError = readBlockHeader(Extractor, Offset);
288 if (!HeaderOrError)
289 return HeaderOrError.takeError();
290
291 // TODO: Maybe store this header information for each block, even just for
292 // debugging?
293 const auto &Header = HeaderOrError.get();
294
295 // Read in the path data.
296 auto PathOrError = readPath(Extractor, Offset);
297 if (!PathOrError)
298 return PathOrError.takeError();
299 const auto &Path = PathOrError.get();
300
301 // For each path we encounter, we should intern it to get a PathID.
302 auto DataOrError = readData(Extractor, Offset);
303 if (!DataOrError)
304 return DataOrError.takeError();
305 auto &Data = DataOrError.get();
306
307 if (auto E =
308 P.addBlock(Profile::Block{Profile::ThreadID{Header.Thread},
309 {{P.internPath(Path), std::move(Data)}}}))
310 return std::move(E);
311 }
312
313 return P;
314}
315
316namespace {
317
318struct StackEntry {
320 Profile::FuncID FuncId;
321};
322
323} // namespace
324
326 Profile P;
327
328 // The implementation of the algorithm re-creates the execution of
329 // the functions based on the trace data. To do this, we set up a number of
330 // data structures to track the execution context of every thread in the
331 // Trace.
334 ThreadPathData;
335
336 // We then do a pass through the Trace to account data on a per-thread-basis.
337 for (const auto &E : T) {
338 auto &TSD = ThreadStacks[E.TId];
339 switch (E.Type) {
340 case RecordTypes::ENTER:
341 case RecordTypes::ENTER_ARG:
342
343 // Push entries into the function call stack.
344 TSD.push_back({E.TSC, E.FuncId});
345 break;
346
347 case RecordTypes::EXIT:
348 case RecordTypes::TAIL_EXIT:
349
350 // Exits cause some accounting to happen, based on the state of the stack.
351 // For each function we pop off the stack, we take note of the path and
352 // record the cumulative state for this path. As we're doing this, we
353 // intern the path into the Profile.
354 while (!TSD.empty()) {
355 auto Top = TSD.back();
356 auto FunctionLocalTime = AbsoluteDifference(Top.Timestamp, E.TSC);
358 transform(reverse(TSD), std::back_inserter(Path),
359 std::mem_fn(&StackEntry::FuncId));
360 auto InternedPath = P.internPath(Path);
361 auto &TPD = ThreadPathData[E.TId][InternedPath];
362 ++TPD.CallCount;
363 TPD.CumulativeLocalTime += FunctionLocalTime;
364 TSD.pop_back();
365
366 // If we've matched the corresponding entry event for this function,
367 // then we exit the loop.
368 if (Top.FuncId == E.FuncId)
369 break;
370
371 // FIXME: Consider the intermediate times and the cumulative tree time
372 // as well.
373 }
374
375 break;
376
377 case RecordTypes::CUSTOM_EVENT:
378 case RecordTypes::TYPED_EVENT:
379 // TODO: Support an extension point to allow handling of custom and typed
380 // events in profiles.
381 break;
382 }
383 }
384
385 // Once we've gone through the Trace, we now create one Block per thread in
386 // the Profile.
387 for (const auto &ThreadPaths : ThreadPathData) {
388 const auto &TID = ThreadPaths.first;
389 const auto &PathsData = ThreadPaths.second;
390 if (auto E = P.addBlock({
391 TID,
392 std::vector<std::pair<Profile::PathID, Profile::Data>>(
393 PathsData.begin(), PathsData.end()),
394 }))
395 return std::move(E);
396 }
397
398 return P;
399}
400
401} // namespace xray
402} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define H(x, y, z)
Definition: MD5.cpp:57
#define P(N)
uint32_t Number
Definition: Profile.cpp:46
Profile::FuncID FuncId
Definition: Profile.cpp:320
uint64_t Timestamp
Definition: Profile.cpp:319
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:230
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
Tagged union holding either a T or a Error.
Definition: Error.h:485
Error takeError()
Take ownership of the stored error.
Definition: Error.h:612
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
This class represents a memory mapped file.
Definition: FileSystem.h:1287
LLVM_ABI size_t size() const
Definition: Path.cpp:1159
@ readonly
May only access map via const_data as read only.
Definition: FileSystem.h:1290
LLVM_ABI char * data() const
Definition: Path.cpp:1164
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
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
LLVM_ABI Error addBlock(Block &&B)
Appends a fully-formed Block instance into the Profile.
Definition: Profile.cpp:120
A Trace object represents the records that have been loaded from XRay log files generated by instrume...
Definition: Trace.h:47
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
std::error_code file_size(const Twine &Path, uint64_t &Result)
Get file size.
Definition: FileSystem.h:691
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
@ Offset
Definition: DWP.cpp:477
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition: STLExtras.h:1987
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:428
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:769
constexpr T AbsoluteDifference(U X, V Y)
Subtract two unsigned integers, X and Y, of type T and return the absolute value of the result.
Definition: MathExtras.h:605
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1854
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1777
#define N
std::vector< std::pair< PathID, Data > > PathData
Definition: Profile.h:65