1 //===- BlockIndexer.h - 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 #ifndef LLVM_LIB_XRAY_BLOCKINDEXER_H_
15 #define LLVM_LIB_XRAY_BLOCKINDEXER_H_
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/XRay/FDRRecords.h"
19 #include <cstdint>
20 #include <vector>
21 
22 namespace llvm {
23 namespace xray {
24 
25 // The BlockIndexer will gather all related records associated with a
26 // process+thread and group them by 'Block'.
27 class BlockIndexer : public RecordVisitor {
28 public:
29   struct Block {
30     uint64_t ProcessID;
31     int32_t ThreadID;
32     WallclockRecord *WallclockTime;
33     std::vector<Record *> Records;
34   };
35 
36   // This maps the process + thread combination to a sequence of blocks.
37   using Index = DenseMap<std::pair<uint64_t, int32_t>, std::vector<Block>>;
38 
39 private:
40   Index &Indices;
41 
42   Block CurrentBlock{0, 0, nullptr, {}};
43 
44 public:
BlockIndexer(Index & I)45   explicit BlockIndexer(Index &I) : RecordVisitor(), Indices(I) {}
46 
47   Error visit(BufferExtents &) override;
48   Error visit(WallclockRecord &) override;
49   Error visit(NewCPUIDRecord &) override;
50   Error visit(TSCWrapRecord &) override;
51   Error visit(CustomEventRecord &) override;
52   Error visit(CallArgRecord &) override;
53   Error visit(PIDRecord &) override;
54   Error visit(NewBufferRecord &) override;
55   Error visit(EndBufferRecord &) override;
56   Error visit(FunctionRecord &) override;
57   Error visit(CustomEventRecordV5 &) override;
58   Error visit(TypedEventRecord &) override;
59 
60   /// The flush() function will clear out the current state of the visitor, to
61   /// allow for explicitly flushing a block's records to the currently
62   /// recognized thread and process combination.
63   Error flush();
64 };
65 
66 } // namespace xray
67 } // namespace llvm
68 
69 #endif // LLVM_LIB_XRAY_BLOCKINDEXER_H_
70