xref: /llvm-project/llvm/lib/XRay/BlockIndexer.cpp (revision 985c2b9226dc4acbf816f8e416560f7c0d94c0e3)
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                           CurrentBlock.WallclockTime,
26                           std::move(CurrentBlock.Records)});
27     CurrentBlock.ProcessID = 0;
28     CurrentBlock.ThreadID = 0;
29     CurrentBlock.WallclockTime = nullptr;
30     CurrentBlock.Records = {};
31   }
32   CurrentState = State::ExtentsFound;
33   return Error::success();
34 }
35 
36 Error BlockIndexer::visit(WallclockRecord &R) {
37   CurrentBlock.Records.push_back(&R);
38   CurrentBlock.WallclockTime = &R;
39   return Error::success();
40 }
41 
42 Error BlockIndexer::visit(NewCPUIDRecord &R) {
43   CurrentBlock.Records.push_back(&R);
44   return Error::success();
45 }
46 
47 Error BlockIndexer::visit(TSCWrapRecord &R) {
48   CurrentBlock.Records.push_back(&R);
49   return Error::success();
50 }
51 
52 Error BlockIndexer::visit(CustomEventRecord &R) {
53   CurrentBlock.Records.push_back(&R);
54   return Error::success();
55 }
56 
57 Error BlockIndexer::visit(CallArgRecord &R) {
58   CurrentBlock.Records.push_back(&R);
59   return Error::success();
60 }
61 
62 Error BlockIndexer::visit(PIDRecord &R) {
63   CurrentBlock.ProcessID = R.pid();
64   CurrentBlock.Records.push_back(&R);
65   return Error::success();
66 }
67 
68 Error BlockIndexer::visit(NewBufferRecord &R) {
69   CurrentState = State::ThreadIDFound;
70   CurrentBlock.ThreadID = R.tid();
71   CurrentBlock.Records.push_back(&R);
72   return Error::success();
73 }
74 
75 Error BlockIndexer::visit(EndBufferRecord &R) {
76   CurrentState = State::SeekExtents;
77   CurrentBlock.Records.push_back(&R);
78   return Error::success();
79 }
80 
81 Error BlockIndexer::visit(FunctionRecord &R) {
82   CurrentBlock.Records.push_back(&R);
83   return Error::success();
84 }
85 
86 Error BlockIndexer::flush() {
87   CurrentState = State::SeekExtents;
88   Index::iterator It;
89   std::tie(It, std::ignore) =
90       Indices.insert({{CurrentBlock.ProcessID, CurrentBlock.ThreadID}, {}});
91   It->second.push_back({CurrentBlock.ProcessID, CurrentBlock.ThreadID,
92                         CurrentBlock.WallclockTime,
93                         std::move(CurrentBlock.Records)});
94   CurrentBlock.ProcessID = 0;
95   CurrentBlock.ThreadID = 0;
96   CurrentBlock.Records = {};
97   return Error::success();
98 }
99 
100 } // namespace xray
101 } // namespace llvm
102