xref: /aosp_15_r20/external/perfetto/src/trace_processor/types/trace_processor_context.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2018 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_TYPES_TRACE_PROCESSOR_CONTEXT_H_
18 #define SRC_TRACE_PROCESSOR_TYPES_TRACE_PROCESSOR_CONTEXT_H_
19 
20 #include <cstdint>
21 #include <memory>
22 #include <optional>
23 #include <vector>
24 
25 #include "perfetto/trace_processor/basic_types.h"
26 #include "src/trace_processor/tables/metadata_tables_py.h"
27 #include "src/trace_processor/types/destructible.h"
28 
29 namespace perfetto::trace_processor {
30 
31 class AndroidDumpstateEventParser;
32 class AndroidLogEventParser;
33 class ArgsTracker;
34 class ArgsTranslationTable;
35 class ArtMethodParser;
36 class AsyncTrackSetTracker;
37 class ChunkedTraceReader;
38 class ClockConverter;
39 class ClockTracker;
40 class CpuTracker;
41 class DeobfuscationMappingTable;
42 class DescriptorPool;
43 class EtwModule;
44 class EventTracker;
45 class FlowTracker;
46 class ForwardingTraceParser;
47 class FtraceModule;
48 class FuchsiaRecordParser;
49 class GeckoTraceParser;
50 class GlobalArgsTracker;
51 class HeapGraphTracker;
52 class InstrumentsRowParser;
53 class JsonTraceParser;
54 class LegacyV8CpuProfileTracker;
55 class MachineTracker;
56 class MappingTracker;
57 class MetadataTracker;
58 class MultiMachineTraceManager;
59 class PacketAnalyzer;
60 class PerfRecordParser;
61 class PerfSampleTracker;
62 class PerfTextTraceParser;
63 class ProcessTracker;
64 class ProcessTrackTranslationTable;
65 class ProtoImporterModule;
66 class ProtoTraceParser;
67 class SchedEventTracker;
68 class SliceTracker;
69 class SliceTranslationTable;
70 class SpeRecordParser;
71 class StackProfileTracker;
72 class TraceFileTracker;
73 class TraceReaderRegistry;
74 class TraceSorter;
75 class TraceStorage;
76 class TrackEventModule;
77 class TrackTracker;
78 
79 using MachineId = tables::MachineTable::Id;
80 
81 class TraceProcessorContext {
82  public:
83   struct InitArgs {
84     Config config;
85     std::shared_ptr<TraceStorage> storage;
86     uint32_t raw_machine_id = 0;
87   };
88 
89   explicit TraceProcessorContext(const InitArgs&);
90 
91   // The default constructor is used in testing.
92   TraceProcessorContext();
93   ~TraceProcessorContext();
94 
95   TraceProcessorContext(TraceProcessorContext&&);
96   TraceProcessorContext& operator=(TraceProcessorContext&&);
97 
98   Config config;
99 
100   // |storage| is shared among multiple contexts in multi-machine tracing.
101   std::shared_ptr<TraceStorage> storage;
102 
103   std::unique_ptr<TraceReaderRegistry> reader_registry;
104 
105   // We might create multiple `ChunkedTraceReader` instances (e.g. one for each
106   // file in a ZIP ). The instances are kept around here as some tokenizers
107   // might keep state that is later needed after sorting.
108   std::vector<std::unique_ptr<ChunkedTraceReader>> chunk_readers;
109 
110   // The sorter is used to sort trace data by timestamp and is shared among
111   // multiple machines.
112   std::shared_ptr<TraceSorter> sorter;
113 
114   // Keep the global tracker before the args tracker as we access the global
115   // tracker in the destructor of the args tracker. Also keep it before other
116   // trackers, as they may own ArgsTrackers themselves.
117   std::unique_ptr<GlobalArgsTracker> global_args_tracker;
118   std::unique_ptr<ArgsTracker> args_tracker;
119   std::unique_ptr<ArgsTranslationTable> args_translation_table;
120 
121   std::unique_ptr<TrackTracker> track_tracker;
122   std::unique_ptr<AsyncTrackSetTracker> async_track_set_tracker;
123   std::unique_ptr<SliceTracker> slice_tracker;
124   std::unique_ptr<SliceTranslationTable> slice_translation_table;
125   std::unique_ptr<FlowTracker> flow_tracker;
126   std::unique_ptr<ProcessTracker> process_tracker;
127   std::unique_ptr<ProcessTrackTranslationTable> process_track_translation_table;
128   std::unique_ptr<EventTracker> event_tracker;
129   std::unique_ptr<SchedEventTracker> sched_event_tracker;
130   std::unique_ptr<ClockTracker> clock_tracker;
131   std::unique_ptr<ClockConverter> clock_converter;
132   std::unique_ptr<MappingTracker> mapping_tracker;
133   std::unique_ptr<MachineTracker> machine_tracker;
134   std::unique_ptr<PerfSampleTracker> perf_sample_tracker;
135   std::unique_ptr<StackProfileTracker> stack_profile_tracker;
136   std::unique_ptr<MetadataTracker> metadata_tracker;
137   std::unique_ptr<CpuTracker> cpu_tracker;
138   std::unique_ptr<TraceFileTracker> trace_file_tracker;
139   std::unique_ptr<LegacyV8CpuProfileTracker> legacy_v8_cpu_profile_tracker;
140 
141   // These fields are stored as pointers to Destructible objects rather than
142   // their actual type (a subclass of Destructible), as the concrete subclass
143   // type is only available in storage_full target. To access these fields use
144   // the GetOrCreate() method on their subclass type, e.g.
145   // SyscallTracker::GetOrCreate(context)
146   // clang-format off
147   std::unique_ptr<Destructible> android_probes_tracker;                 // AndroidProbesTracker
148   std::unique_ptr<Destructible> android_battery_stats_history_tracker;  // AndroidBatteryStatsHistoryStringTracker
149   std::unique_ptr<Destructible> binder_tracker;                         // BinderTracker
150   std::unique_ptr<Destructible> heap_graph_tracker;                     // HeapGraphTracker
151   std::unique_ptr<Destructible> syscall_tracker;                        // SyscallTracker
152   std::unique_ptr<Destructible> system_info_tracker;                    // SystemInfoTracker
153   std::unique_ptr<Destructible> v4l2_tracker;                           // V4l2Tracker
154   std::unique_ptr<Destructible> virtio_video_tracker;                   // VirtioVideoTracker
155   std::unique_ptr<Destructible> systrace_parser;                        // SystraceParser
156   std::unique_ptr<Destructible> thread_state_tracker;                   // ThreadStateTracker
157   std::unique_ptr<Destructible> i2c_tracker;                            // I2CTracker
158   std::unique_ptr<Destructible> perf_data_tracker;                      // PerfDataTracker
159   std::unique_ptr<Destructible> content_analyzer;                       // ProtoContentAnalyzer
160   std::unique_ptr<Destructible> shell_transitions_tracker;              // ShellTransitionsTracker
161   std::unique_ptr<Destructible> protolog_messages_tracker;              // ProtoLogMessagesTracker
162   std::unique_ptr<Destructible> ftrace_sched_tracker;                   // FtraceSchedEventTracker
163   std::unique_ptr<Destructible> v8_tracker;                             // V8Tracker
164   std::unique_ptr<Destructible> jit_tracker;                            // JitTracker
165   std::unique_ptr<Destructible> protolog_message_decoder;               // ProtoLogMessageDecoder
166   std::unique_ptr<Destructible> instruments_row_data_tracker;           // RowDataTracker
167   std::unique_ptr<Destructible> perf_tracker;                           // PerfTracker
168   std::unique_ptr<Destructible> etm_tracker;                            // EtmTracker
169   // clang-format on
170 
171   std::unique_ptr<ProtoTraceParser> proto_trace_parser;
172 
173   // These fields are trace parsers which will be called by |forwarding_parser|
174   // once the format of the trace is discovered. They are placed here as they
175   // are only available in the lib target.
176   std::unique_ptr<JsonTraceParser> json_trace_parser;
177   std::unique_ptr<FuchsiaRecordParser> fuchsia_record_parser;
178   std::unique_ptr<PerfRecordParser> perf_record_parser;
179   std::unique_ptr<SpeRecordParser> spe_record_parser;
180   std::unique_ptr<InstrumentsRowParser> instruments_row_parser;
181   std::unique_ptr<AndroidDumpstateEventParser> android_dumpstate_event_parser;
182   std::unique_ptr<AndroidLogEventParser> android_log_event_parser;
183   std::unique_ptr<GeckoTraceParser> gecko_trace_parser;
184   std::unique_ptr<ArtMethodParser> art_method_parser;
185   std::unique_ptr<PerfTextTraceParser> perf_text_parser;
186 
187   // This field contains the list of proto descriptors that can be used by
188   // reflection-based parsers.
189   std::unique_ptr<DescriptorPool> descriptor_pool_;
190 
191   // The module at the index N is registered to handle field id N in
192   // TracePacket.
193   std::vector<std::vector<ProtoImporterModule*>> modules_by_field;
194   std::vector<std::unique_ptr<ProtoImporterModule>> modules;
195   // Pointers to modules from the modules vector that need to be called for
196   // all fields.
197   std::vector<ProtoImporterModule*> modules_for_all_fields;
198   FtraceModule* ftrace_module = nullptr;
199   EtwModule* etw_module = nullptr;
200   TrackEventModule* track_module = nullptr;
201 
202   // Marks whether the uuid was read from the trace.
203   // If the uuid was NOT read, the uuid will be made from the hash of the first
204   // 4KB of the trace.
205   bool uuid_found_in_trace = false;
206 
207   std::optional<MachineId> machine_id() const;
208 
209   // Manages the contexts for reading trace data emitted from remote machines.
210   std::unique_ptr<MultiMachineTraceManager> multi_machine_trace_manager;
211 };
212 
213 }  // namespace perfetto::trace_processor
214 
215 #endif  // SRC_TRACE_PROCESSOR_TYPES_TRACE_PROCESSOR_CONTEXT_H_
216