• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/trace_processor/importers/proto/multi_machine_trace_manager.h"
18 
19 #include <memory>
20 #include <utility>
21 
22 #include "perfetto/base/logging.h"
23 #include "src/trace_processor/importers/common/process_tracker.h"
24 #include "src/trace_processor/importers/proto/default_modules.h"
25 #include "src/trace_processor/importers/proto/proto_trace_parser_impl.h"
26 #include "src/trace_processor/importers/proto/proto_trace_reader.h"
27 #include "src/trace_processor/sorter/trace_sorter.h"
28 #include "src/trace_processor/types/trace_processor_context.h"
29 
30 namespace perfetto::trace_processor {
31 
MultiMachineTraceManager(TraceProcessorContext * default_context)32 MultiMachineTraceManager::MultiMachineTraceManager(
33     TraceProcessorContext* default_context)
34     : default_context_(default_context) {
35   PERFETTO_DCHECK(default_context && !default_context_->machine_id());
36 }
37 MultiMachineTraceManager::~MultiMachineTraceManager() = default;
38 
CreateContext(RawMachineId raw_machine_id)39 std::unique_ptr<TraceProcessorContext> MultiMachineTraceManager::CreateContext(
40     RawMachineId raw_machine_id) {
41   TraceProcessorContext::InitArgs args{
42       default_context_->config, default_context_->storage, raw_machine_id};
43   auto new_context = std::make_unique<TraceProcessorContext>(args);
44 
45   // Register default and additional modules (if enabled).
46   RegisterDefaultModules(new_context.get());
47   // Register addtional modules through the registered function pointer.
48   if (additional_modules_factory_)
49     additional_modules_factory_(new_context.get());
50 
51   // Set up shared member fields:
52   // arg_set_id is a monotonically increasing ID.
53   // Share |global_args_tracker| between contexts.
54   new_context->global_args_tracker = default_context_->global_args_tracker;
55   // Share the sorter, but enable for the parser.
56   new_context->sorter = default_context_->sorter;
57   new_context->sorter->AddMachineContext(new_context.get());
58   new_context->process_tracker->SetPidZeroIsUpidZeroIdleProcess();
59   new_context->proto_trace_parser.reset(
60       new ProtoTraceParserImpl(new_context.get()));
61 
62   return new_context;
63 }
64 
EnableAdditionalModules(ProtoImporterModuleFactory factory)65 void MultiMachineTraceManager::EnableAdditionalModules(
66     ProtoImporterModuleFactory factory) {
67   additional_modules_factory_ = factory;
68 }
69 
GetOrCreateReader(RawMachineId raw_machine_id)70 ProtoTraceReader* MultiMachineTraceManager::GetOrCreateReader(
71     RawMachineId raw_machine_id) {
72   auto* remote_ctx = remote_machine_contexts_.Find(raw_machine_id);
73   if (remote_ctx)
74     return remote_ctx->reader.get();
75 
76   auto new_context = CreateContext(raw_machine_id);
77 
78   auto new_reader = std::make_unique<ProtoTraceReader>(new_context.get());
79   remote_machine_contexts_[raw_machine_id] =
80       RemoteMachineContext{std::move(new_context), std::move(new_reader)};
81   return remote_machine_contexts_[raw_machine_id].reader.get();
82 }
83 
84 }  // namespace perfetto::trace_processor
85