xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/track_event_parser.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 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_TRACK_EVENT_PARSER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_TRACK_EVENT_PARSER_H_
19 
20 #include <array>
21 #include <cstdint>
22 #include <optional>
23 #include <vector>
24 
25 #include "perfetto/protozero/field.h"
26 #include "src/trace_processor/importers/common/parser_types.h"
27 #include "src/trace_processor/importers/common/slice_tracker.h"
28 #include "src/trace_processor/importers/common/trace_parser.h"
29 #include "src/trace_processor/importers/proto/active_chrome_processes_tracker.h"
30 #include "src/trace_processor/importers/proto/chrome_string_lookup.h"
31 #include "src/trace_processor/storage/trace_storage.h"
32 #include "src/trace_processor/util/proto_to_args_parser.h"
33 
34 namespace Json {
35 class Value;
36 }
37 
38 namespace perfetto::trace_processor {
39 
40 // Field numbers to be added to args table automatically via reflection
41 //
42 // TODO(ddrone): replace with a predicate on field id to import new fields
43 // automatically
44 static constexpr uint16_t kReflectFields[] = {
45     24, 25, 26, 27, 28, 29, 32, 33, 34, 35, 38, 39, 40, 41, 43, 49, 50};
46 
47 class PacketSequenceStateGeneration;
48 class TraceProcessorContext;
49 class TrackEventTracker;
50 
51 class TrackEventParser {
52  public:
53   TrackEventParser(TraceProcessorContext*, TrackEventTracker*);
54 
55   void ParseTrackDescriptor(int64_t packet_timestamp,
56                             protozero::ConstBytes,
57                             uint32_t packet_sequence_id);
58   UniquePid ParseProcessDescriptor(int64_t packet_timestamp,
59                                    protozero::ConstBytes);
60   UniqueTid ParseThreadDescriptor(protozero::ConstBytes);
61 
62   void ParseTrackEvent(int64_t ts,
63                        const TrackEventData* event_data,
64                        protozero::ConstBytes,
65                        uint32_t packet_sequence_id);
66 
67   void NotifyEndOfFile();
68 
69  private:
70   class EventImporter;
71 
72   void ParseChromeProcessDescriptor(UniquePid, protozero::ConstBytes);
73   void ParseChromeThreadDescriptor(UniqueTid, protozero::ConstBytes);
74   void ParseCounterDescriptor(TrackId, protozero::ConstBytes);
75   void AddActiveProcess(int64_t packet_timestamp, int32_t pid);
76 
77   // Reflection-based proto TrackEvent field parser.
78   util::ProtoToArgsParser args_parser_;
79 
80   TraceProcessorContext* context_;
81   TrackEventTracker* track_event_tracker_;
82 
83   const StringId counter_name_thread_time_id_;
84   const StringId counter_name_thread_instruction_count_id_;
85   const StringId task_file_name_args_key_id_;
86   const StringId task_function_name_args_key_id_;
87   const StringId task_line_number_args_key_id_;
88   const StringId log_message_body_key_id_;
89   const StringId log_message_source_location_function_name_key_id_;
90   const StringId log_message_source_location_file_name_key_id_;
91   const StringId log_message_source_location_line_number_key_id_;
92   const StringId log_message_priority_id_;
93   const StringId source_location_function_name_key_id_;
94   const StringId source_location_file_name_key_id_;
95   const StringId source_location_line_number_key_id_;
96   const StringId raw_legacy_event_id_;
97   const StringId legacy_event_passthrough_utid_id_;
98   const StringId legacy_event_category_key_id_;
99   const StringId legacy_event_name_key_id_;
100   const StringId legacy_event_phase_key_id_;
101   const StringId legacy_event_duration_ns_key_id_;
102   const StringId legacy_event_thread_timestamp_ns_key_id_;
103   const StringId legacy_event_thread_duration_ns_key_id_;
104   const StringId legacy_event_thread_instruction_count_key_id_;
105   const StringId legacy_event_thread_instruction_delta_key_id_;
106   const StringId legacy_event_use_async_tts_key_id_;
107   const StringId legacy_event_unscoped_id_key_id_;
108   const StringId legacy_event_global_id_key_id_;
109   const StringId legacy_event_local_id_key_id_;
110   const StringId legacy_event_id_scope_key_id_;
111   const StringId legacy_event_bind_id_key_id_;
112   const StringId legacy_event_bind_to_enclosing_key_id_;
113   const StringId legacy_event_flow_direction_key_id_;
114   const StringId histogram_name_key_id_;
115   const StringId flow_direction_value_in_id_;
116   const StringId flow_direction_value_out_id_;
117   const StringId flow_direction_value_inout_id_;
118   const StringId chrome_legacy_ipc_class_args_key_id_;
119   const StringId chrome_legacy_ipc_line_args_key_id_;
120   const StringId chrome_host_app_package_name_id_;
121   const StringId chrome_crash_trace_id_name_id_;
122   const StringId chrome_process_label_flat_key_id_;
123   const StringId chrome_process_type_id_;
124   const StringId event_category_key_id_;
125   const StringId event_name_key_id_;
126 
127   ChromeStringLookup chrome_string_lookup_;
128   std::vector<uint32_t> reflect_fields_;
129   ActiveChromeProcessesTracker active_chrome_processes_tracker_;
130 };
131 
132 }  // namespace perfetto::trace_processor
133 
134 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_TRACK_EVENT_PARSER_H_
135