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