xref: /llvm-project/llvm/lib/XRay/BlockIndexer.cpp (revision 02f097e122cd7d83753e27c5ce0e6cd3c610eef4)
1 //===- BlockIndexer.cpp - FDR Block Indexing VIsitor ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // An implementation of the RecordVisitor which generates a mapping between a
11 // thread and a range of records representing a block.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "llvm/XRay/BlockIndexer.h"
15 
16 namespace llvm {
17 namespace xray {
18 
19 Error BlockIndexer::visit(BufferExtents &) {
20   if (CurrentState == State::ThreadIDFound) {
21     Index::iterator It;
22     std::tie(It, std::ignore) =
23         Indices.insert({{CurrentBlock.ProcessID, CurrentBlock.ThreadID}, {}});
24     It->second.push_back({CurrentBlock.ProcessID, CurrentBlock.ThreadID,
25                           std::move(CurrentBlock.Records)});
26     CurrentBlock.ProcessID = 0;
27     CurrentBlock.ThreadID = 0;
28     CurrentBlock.Records = {};
29   }
30   CurrentState = State::ExtentsFound;
31   return Error::success();
32 }
33 
34 Error BlockIndexer::visit(WallclockRecord &R) {
35   CurrentBlock.Records.push_back(&R);
36   return Error::success();
37 }
38 
39 Error BlockIndexer::visit(NewCPUIDRecord &R) {
40   CurrentBlock.Records.push_back(&R);
41   return Error::success();
42 }
43 
44 Error BlockIndexer::visit(TSCWrapRecord &R) {
45   CurrentBlock.Records.push_back(&R);
46   return Error::success();
47 }
48 
49 Error BlockIndexer::visit(CustomEventRecord &R) {
50   CurrentBlock.Records.push_back(&R);
51   return Error::success();
52 }
53 
54 Error BlockIndexer::visit(CallArgRecord &R) {
55   CurrentBlock.Records.push_back(&R);
56   return Error::success();
57 };
58 
59 Error BlockIndexer::visit(PIDRecord &R) {
60   CurrentBlock.ProcessID = R.pid();
61   CurrentBlock.Records.push_back(&R);
62   return Error::success();
63 }
64 
65 Error BlockIndexer::visit(NewBufferRecord &R) {
66   CurrentState = State::ThreadIDFound;
67   CurrentBlock.ThreadID = R.tid();
68   CurrentBlock.Records.push_back(&R);
69   return Error::success();
70 }
71 
72 Error BlockIndexer::visit(EndBufferRecord &R) {
73   CurrentState = State::SeekExtents;
74   CurrentBlock.Records.push_back(&R);
75   return Error::success();
76 }
77 
78 Error BlockIndexer::visit(FunctionRecord &R) {
79   CurrentBlock.Records.push_back(&R);
80   return Error::success();
81 }
82 
83 Error BlockIndexer::flush() {
84   CurrentState = State::SeekExtents;
85   Index::iterator It;
86   std::tie(It, std::ignore) =
87       Indices.insert({{CurrentBlock.ProcessID, CurrentBlock.ThreadID}, {}});
88   It->second.push_back({CurrentBlock.ProcessID, CurrentBlock.ThreadID,
89                         std::move(CurrentBlock.Records)});
90   CurrentBlock.ProcessID = 0;
91   CurrentBlock.ThreadID = 0;
92   CurrentBlock.Records = {};
93   return Error::success();
94 }
95 
96 } // namespace xray
97 } // namespace llvm
98