xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/multi_machine_trace_manager.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_MULTI_MACHINE_TRACE_MANAGER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_MULTI_MACHINE_TRACE_MANAGER_H_
19 
20 #include <memory>
21 #include <vector>
22 
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "src/trace_processor/storage/trace_storage.h"
25 #include "src/trace_processor/types/trace_processor_context.h"
26 
27 namespace perfetto {
28 namespace trace_processor {
29 
30 class TraceProcessorContext;
31 class ProtoTraceReader;
32 
33 // This class provides the get-or-create function for ProtoTraceReader to
34 // support multi-machine tracing. When the default ProtoTraceReader instance
35 // decodes a trace packet with a non-default machine ID:
36 //
37 // packet {
38 //    ftrace_events {
39 //    }
40 //    machine_id: 1001
41 // }
42 //
43 // An object graph rooted from a new ProtoTraceReader is created for the
44 // machine:
45 //
46 // ProtoTraceReader -> TraceProcessorContext (with a non-null machine_id).
47 //                     +--> TraceProcessorStorage (shared with the default
48 //                     instance)
49 //                     |--> TraceSorter (shared with the default instance)
50 //                     |--> TrackTracker (created for machine 1001)
51 //                     |--> ProcessTracker (created for machine 1001)
52 //                     |--> ... other data members rooted from
53 //                     TraceProcessorContext
54 //
55 // and the new ProtoTraceReader is used to parse all trace packet with the same
56 // machine ID. The context is used to insert the machine ID into the sqlite
57 // tables. for query in the trace processor or from the UI frontend.
58 class MultiMachineTraceManager {
59  public:
60   // RawMachineId is the value of 'machine_id' in trace packets.
61   using RawMachineId = uint32_t;
62 
63   explicit MultiMachineTraceManager(TraceProcessorContext* default_context);
64   ~MultiMachineTraceManager();
65 
66   // Get or create an instance of ProtoTraceReader for parsing the trace packets
67   // with the RawMachineId from the trace packet.
68   ProtoTraceReader* GetOrCreateReader(RawMachineId);
69 
70   using ProtoImporterModuleFactory = void (*)(TraceProcessorContext*);
71   void EnableAdditionalModules(ProtoImporterModuleFactory);
72 
73  private:
74   struct RemoteMachineContext {
75     std::unique_ptr<TraceProcessorContext> context;
76     std::unique_ptr<ProtoTraceReader> reader;
77   };
78 
79   std::unique_ptr<TraceProcessorContext> CreateContext(RawMachineId);
80 
81   // The default TraceProcessorContext instance.
82   TraceProcessorContext* default_context_;
83   // Owns contexts for remote machines.
84   base::FlatHashMap<RawMachineId, RemoteMachineContext>
85       remote_machine_contexts_;
86 
87   ProtoImporterModuleFactory additional_modules_factory_ = nullptr;
88 };
89 
90 }  // namespace trace_processor
91 }  // namespace perfetto
92 
93 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_MULTI_MACHINE_TRACE_MANAGER_H_
94