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_PROTO_TRACE_PARSER_H_
18 #define SRC_TRACE_PROCESSOR_PROTO_TRACE_PARSER_H_
19 
20 #include <stdint.h>
21 
22 #include <array>
23 #include <memory>
24 
25 #include "perfetto/base/string_view.h"
26 #include "perfetto/protozero/field.h"
27 #include "src/trace_processor/ftrace_descriptors.h"
28 #include "src/trace_processor/proto_incremental_state.h"
29 #include "src/trace_processor/trace_blob_view.h"
30 #include "src/trace_processor/trace_parser.h"
31 #include "src/trace_processor/trace_storage.h"
32 
33 namespace perfetto {
34 namespace trace_processor {
35 
36 class TraceProcessorContext;
37 
38 struct SystraceTracePoint {
39   char phase;
40   uint32_t tgid;
41 
42   // For phase = 'B' and phase = 'C' only.
43   base::StringView name;
44 
45   // For phase = 'C' only.
46   double value;
47 };
48 
49 inline bool operator==(const SystraceTracePoint& x,
50                        const SystraceTracePoint& y) {
51   return std::tie(x.phase, x.tgid, x.name, x.value) ==
52          std::tie(y.phase, y.tgid, y.name, y.value);
53 }
54 
55 enum class SystraceParseResult { kFailure = 0, kUnsupported, kSuccess };
56 
57 SystraceParseResult ParseSystraceTracePoint(base::StringView,
58                                             SystraceTracePoint* out);
59 
60 class ProtoTraceParser : public TraceParser {
61  public:
62   using ConstBytes = protozero::ConstBytes;
63   explicit ProtoTraceParser(TraceProcessorContext*);
64   ~ProtoTraceParser() override;
65 
66   // TraceParser implementation.
67   void ParseTracePacket(int64_t timestamp,
68                         TraceSorter::TimestampedTracePiece) override;
69   void ParseFtracePacket(uint32_t cpu,
70                          int64_t timestamp,
71                          TraceSorter::TimestampedTracePiece) override;
72 
73   void ParseProcessTree(ConstBytes);
74   void ParseProcessStats(int64_t timestamp, ConstBytes);
75   void ParseSchedSwitch(uint32_t cpu, int64_t timestamp, ConstBytes);
76   void ParseSchedWakeup(int64_t timestamp, ConstBytes);
77   void ParseTaskNewTask(int64_t timestamp, uint32_t source_tid, ConstBytes);
78   void ParseTaskRename(ConstBytes);
79   void ParseCpuFreq(int64_t timestamp, ConstBytes);
80   void ParseCpuIdle(int64_t timestamp, ConstBytes);
81   void ParsePrint(uint32_t cpu, int64_t timestamp, uint32_t pid, ConstBytes);
82   void ParseSysStats(int64_t ts, ConstBytes);
83   void ParseRssStat(int64_t ts, uint32_t pid, ConstBytes);
84   void ParseIonHeapGrowOrShrink(int64_t ts,
85                                 uint32_t pid,
86                                 ConstBytes,
87                                 bool grow);
88   void ParseSignalDeliver(int64_t ts, uint32_t pid, ConstBytes);
89   void ParseSignalGenerate(int64_t ts, ConstBytes);
90   void ParseLowmemoryKill(int64_t ts, ConstBytes);
91   void ParseBatteryCounters(int64_t ts, ConstBytes);
92   void ParsePowerRails(ConstBytes);
93   void ParseOOMScoreAdjUpdate(int64_t ts, ConstBytes);
94   void ParseMmEventRecord(int64_t ts, uint32_t pid, ConstBytes);
95   void ParseSysEvent(int64_t ts, uint32_t pid, bool is_enter, ConstBytes);
96   void ParseClockSnapshot(ConstBytes);
97   void ParseAndroidLogPacket(ConstBytes);
98   void ParseAndroidLogEvent(ConstBytes);
99   void ParseAndroidLogStats(ConstBytes);
100   void ParseGenericFtrace(int64_t timestamp,
101                           uint32_t cpu,
102                           uint32_t pid,
103                           ConstBytes view);
104   void ParseTypedFtraceToRaw(uint32_t ftrace_id,
105                              int64_t timestamp,
106                              uint32_t cpu,
107                              uint32_t pid,
108                              ConstBytes view);
109   void ParseTraceStats(ConstBytes);
110   void ParseFtraceStats(ConstBytes);
111   void ParseProfilePacket(ConstBytes);
112   void ParseSystemInfo(ConstBytes);
113   void ParseTrackEvent(int64_t ts,
114                        int64_t tts,
115                        ProtoIncrementalState::PacketSequenceState*,
116                        ConstBytes);
117 
118  private:
119   TraceProcessorContext* context_;
120   const StringId utid_name_id_;
121   const StringId sched_wakeup_name_id_;
122   const StringId cpu_freq_name_id_;
123   const StringId cpu_idle_name_id_;
124   const StringId comm_name_id_;
125   const StringId num_forks_name_id_;
126   const StringId num_irq_total_name_id_;
127   const StringId num_softirq_total_name_id_;
128   const StringId num_irq_name_id_;
129   const StringId num_softirq_name_id_;
130   const StringId cpu_times_user_ns_id_;
131   const StringId cpu_times_user_nice_ns_id_;
132   const StringId cpu_times_system_mode_ns_id_;
133   const StringId cpu_times_idle_ns_id_;
134   const StringId cpu_times_io_wait_ns_id_;
135   const StringId cpu_times_irq_ns_id_;
136   const StringId cpu_times_softirq_ns_id_;
137   const StringId signal_deliver_id_;
138   const StringId signal_generate_id_;
139   const StringId batt_charge_id_;
140   const StringId batt_capacity_id_;
141   const StringId batt_current_id_;
142   const StringId batt_current_avg_id_;
143   const StringId lmk_id_;
144   const StringId oom_score_adj_id_;
145   const StringId ion_total_unknown_id_;
146   const StringId ion_change_unknown_id_;
147   std::vector<StringId> meminfo_strs_id_;
148   std::vector<StringId> vmstat_strs_id_;
149   std::vector<StringId> rss_members_;
150   std::vector<StringId> power_rails_strs_id_;
151 
152   struct FtraceMessageStrings {
153     // The string id of name of the event field (e.g. sched_switch's id).
154     StringId message_name_id = 0;
155     std::array<StringId, kMaxFtraceEventFields> field_name_ids;
156   };
157   std::vector<FtraceMessageStrings> ftrace_message_strings_;
158 
159   // Maps a proto field number for memcounters in ProcessStats::Process to
160   // their StringId. Keep kProcStatsProcessSize equal to 1 + max proto field
161   // id of ProcessStats::Process.
162   static constexpr size_t kProcStatsProcessSize = 11;
163   std::array<StringId, kProcStatsProcessSize> proc_stats_process_names_{};
164 
165   struct MmEventCounterNames {
166     MmEventCounterNames() = default;
MmEventCounterNamesMmEventCounterNames167     MmEventCounterNames(StringId _count, StringId _max_lat, StringId _avg_lat)
168         : count(_count), max_lat(_max_lat), avg_lat(_avg_lat) {}
169 
170     StringId count = 0;
171     StringId max_lat = 0;
172     StringId avg_lat = 0;
173   };
174 
175   // Keep kMmEventCounterSize equal to mm_event_type::MM_TYPE_NUM in the kernel.
176   static constexpr size_t kMmEventCounterSize = 7;
177   std::array<MmEventCounterNames, kMmEventCounterSize> mm_event_counter_names_;
178 
179 };
180 
181 }  // namespace trace_processor
182 }  // namespace perfetto
183 
184 #endif  // SRC_TRACE_PROCESSOR_PROTO_TRACE_PARSER_H_
185