102f097e1SDean Michael Berris //===- BlockIndexer.cpp - FDR Block Indexing VIsitor ----------------------===//
202f097e1SDean Michael Berris //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
602f097e1SDean Michael Berris //
702f097e1SDean Michael Berris //===----------------------------------------------------------------------===//
802f097e1SDean Michael Berris //
902f097e1SDean Michael Berris // An implementation of the RecordVisitor which generates a mapping between a
1002f097e1SDean Michael Berris // thread and a range of records representing a block.
1102f097e1SDean Michael Berris //
1202f097e1SDean Michael Berris //===----------------------------------------------------------------------===//
1302f097e1SDean Michael Berris #include "llvm/XRay/BlockIndexer.h"
1402f097e1SDean Michael Berris 
1502f097e1SDean Michael Berris namespace llvm {
1602f097e1SDean Michael Berris namespace xray {
1702f097e1SDean Michael Berris 
visit(BufferExtents &)1890a46bdeSDean Michael Berris Error BlockIndexer::visit(BufferExtents &) { return Error::success(); }
1902f097e1SDean Michael Berris 
visit(WallclockRecord & R)2002f097e1SDean Michael Berris Error BlockIndexer::visit(WallclockRecord &R) {
2102f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
22985c2b92SDean Michael Berris   CurrentBlock.WallclockTime = &R;
2302f097e1SDean Michael Berris   return Error::success();
2402f097e1SDean Michael Berris }
2502f097e1SDean Michael Berris 
visit(NewCPUIDRecord & R)2602f097e1SDean Michael Berris Error BlockIndexer::visit(NewCPUIDRecord &R) {
2702f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
2802f097e1SDean Michael Berris   return Error::success();
2902f097e1SDean Michael Berris }
3002f097e1SDean Michael Berris 
visit(TSCWrapRecord & R)3102f097e1SDean Michael Berris Error BlockIndexer::visit(TSCWrapRecord &R) {
3202f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
3302f097e1SDean Michael Berris   return Error::success();
3402f097e1SDean Michael Berris }
3502f097e1SDean Michael Berris 
visit(CustomEventRecord & R)3602f097e1SDean Michael Berris Error BlockIndexer::visit(CustomEventRecord &R) {
3702f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
3802f097e1SDean Michael Berris   return Error::success();
3902f097e1SDean Michael Berris }
4002f097e1SDean Michael Berris 
visit(CustomEventRecordV5 & R)4159439dd0SDean Michael Berris Error BlockIndexer::visit(CustomEventRecordV5 &R) {
4259439dd0SDean Michael Berris   CurrentBlock.Records.push_back(&R);
4359439dd0SDean Michael Berris   return Error::success();
4459439dd0SDean Michael Berris }
4559439dd0SDean Michael Berris 
visit(TypedEventRecord & R)4659439dd0SDean Michael Berris Error BlockIndexer::visit(TypedEventRecord &R) {
4759439dd0SDean Michael Berris   CurrentBlock.Records.push_back(&R);
4859439dd0SDean Michael Berris   return Error::success();
4959439dd0SDean Michael Berris }
5059439dd0SDean Michael Berris 
visit(CallArgRecord & R)5102f097e1SDean Michael Berris Error BlockIndexer::visit(CallArgRecord &R) {
5202f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
5302f097e1SDean Michael Berris   return Error::success();
546088e851SSimon Pilgrim }
5502f097e1SDean Michael Berris 
visit(PIDRecord & R)5602f097e1SDean Michael Berris Error BlockIndexer::visit(PIDRecord &R) {
5702f097e1SDean Michael Berris   CurrentBlock.ProcessID = R.pid();
5802f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
5902f097e1SDean Michael Berris   return Error::success();
6002f097e1SDean Michael Berris }
6102f097e1SDean Michael Berris 
visit(NewBufferRecord & R)6202f097e1SDean Michael Berris Error BlockIndexer::visit(NewBufferRecord &R) {
6390a46bdeSDean Michael Berris   if (!CurrentBlock.Records.empty())
6490a46bdeSDean Michael Berris     if (auto E = flush())
6590a46bdeSDean Michael Berris       return E;
6690a46bdeSDean Michael Berris 
6702f097e1SDean Michael Berris   CurrentBlock.ThreadID = R.tid();
6802f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
6902f097e1SDean Michael Berris   return Error::success();
7002f097e1SDean Michael Berris }
7102f097e1SDean Michael Berris 
visit(EndBufferRecord & R)7202f097e1SDean Michael Berris Error BlockIndexer::visit(EndBufferRecord &R) {
7302f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
7402f097e1SDean Michael Berris   return Error::success();
7502f097e1SDean Michael Berris }
7602f097e1SDean Michael Berris 
visit(FunctionRecord & R)7702f097e1SDean Michael Berris Error BlockIndexer::visit(FunctionRecord &R) {
7802f097e1SDean Michael Berris   CurrentBlock.Records.push_back(&R);
7902f097e1SDean Michael Berris   return Error::success();
8002f097e1SDean Michael Berris }
8102f097e1SDean Michael Berris 
flush()8202f097e1SDean Michael Berris Error BlockIndexer::flush() {
8302f097e1SDean Michael Berris   Index::iterator It;
8402f097e1SDean Michael Berris   std::tie(It, std::ignore) =
8502f097e1SDean Michael Berris       Indices.insert({{CurrentBlock.ProcessID, CurrentBlock.ThreadID}, {}});
8602f097e1SDean Michael Berris   It->second.push_back({CurrentBlock.ProcessID, CurrentBlock.ThreadID,
87985c2b92SDean Michael Berris                         CurrentBlock.WallclockTime,
8802f097e1SDean Michael Berris                         std::move(CurrentBlock.Records)});
8902f097e1SDean Michael Berris   CurrentBlock.ProcessID = 0;
9002f097e1SDean Michael Berris   CurrentBlock.ThreadID = 0;
9102f097e1SDean Michael Berris   CurrentBlock.Records = {};
9290a46bdeSDean Michael Berris   CurrentBlock.WallclockTime = nullptr;
9302f097e1SDean Michael Berris   return Error::success();
9402f097e1SDean Michael Berris }
9502f097e1SDean Michael Berris 
9602f097e1SDean Michael Berris } // namespace xray
9702f097e1SDean Michael Berris } // namespace llvm
98