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 <map>
22 
23 #include "perfetto/base/build_config.h"
24 #include "perfetto/protozero/field.h"
25 #include "src/trace_processor/importers/common/args_tracker.h"
26 #include "src/trace_processor/importers/common/slice_tracker.h"
27 #include "src/trace_processor/importers/proto/chrome_string_lookup.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 #include "src/trace_processor/timestamped_trace_piece.h"
30 #include "src/trace_processor/util/proto_to_args_parser.h"
31 
32 #include "protos/perfetto/trace/track_event/track_event.pbzero.h"
33 
34 namespace Json {
35 class Value;
36 }
37 
38 namespace perfetto {
39 namespace trace_processor {
40 
41 // Field numbers to be added to args table automatically via reflection
42 //
43 // TODO(ddrone): replace with a predicate on field id to import new fields
44 // automatically
45 static constexpr uint16_t kReflectFields[] = {24, 25, 26, 27, 28, 29, 32, 33,
46                                               34, 35, 38, 39, 40, 41, 42, 43};
47 
48 class PacketSequenceStateGeneration;
49 class TraceProcessorContext;
50 class TrackEventTracker;
51 
52 class TrackEventParser {
53  public:
54   TrackEventParser(TraceProcessorContext*, TrackEventTracker*);
55 
56   void ParseTrackDescriptor(protozero::ConstBytes);
57   UniquePid ParseProcessDescriptor(protozero::ConstBytes);
58   UniqueTid ParseThreadDescriptor(protozero::ConstBytes);
59 
60   void ParseTrackEvent(int64_t ts,
61                        TrackEventData* event_data,
62                        protozero::ConstBytes);
63 
64  private:
65   class EventImporter;
66 
67   void ParseChromeProcessDescriptor(UniquePid, protozero::ConstBytes);
68   void ParseChromeThreadDescriptor(UniqueTid, protozero::ConstBytes);
69   void ParseCounterDescriptor(TrackId, protozero::ConstBytes);
70 
71   // Reflection-based proto TrackEvent field parser.
72   util::ProtoToArgsParser args_parser_;
73 
74   TraceProcessorContext* context_;
75   TrackEventTracker* track_event_tracker_;
76 
77   const StringId counter_name_thread_time_id_;
78   const StringId counter_name_thread_instruction_count_id_;
79   const StringId task_file_name_args_key_id_;
80   const StringId task_function_name_args_key_id_;
81   const StringId task_line_number_args_key_id_;
82   const StringId log_message_body_key_id_;
83   const StringId source_location_function_name_key_id_;
84   const StringId source_location_file_name_key_id_;
85   const StringId source_location_line_number_key_id_;
86   const StringId raw_legacy_event_id_;
87   const StringId legacy_event_passthrough_utid_id_;
88   const StringId legacy_event_category_key_id_;
89   const StringId legacy_event_name_key_id_;
90   const StringId legacy_event_phase_key_id_;
91   const StringId legacy_event_duration_ns_key_id_;
92   const StringId legacy_event_thread_timestamp_ns_key_id_;
93   const StringId legacy_event_thread_duration_ns_key_id_;
94   const StringId legacy_event_thread_instruction_count_key_id_;
95   const StringId legacy_event_thread_instruction_delta_key_id_;
96   const StringId legacy_event_use_async_tts_key_id_;
97   const StringId legacy_event_unscoped_id_key_id_;
98   const StringId legacy_event_global_id_key_id_;
99   const StringId legacy_event_local_id_key_id_;
100   const StringId legacy_event_id_scope_key_id_;
101   const StringId legacy_event_bind_id_key_id_;
102   const StringId legacy_event_bind_to_enclosing_key_id_;
103   const StringId legacy_event_flow_direction_key_id_;
104   const StringId histogram_name_key_id_;
105   const StringId flow_direction_value_in_id_;
106   const StringId flow_direction_value_out_id_;
107   const StringId flow_direction_value_inout_id_;
108   const StringId chrome_legacy_ipc_class_args_key_id_;
109   const StringId chrome_legacy_ipc_line_args_key_id_;
110   const StringId chrome_host_app_package_name_id_;
111   const StringId chrome_crash_trace_id_name_id_;
112 
113   ChromeStringLookup chrome_string_lookup_;
114   std::array<StringId, 4> counter_unit_ids_;
115 
116   std::vector<uint16_t> reflect_fields_;
117 };
118 
119 }  // namespace trace_processor
120 }  // namespace perfetto
121 
122 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_TRACK_EVENT_PARSER_H_
123