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 #include "src/trace_processor/importers/proto/android_probes_parser.h"
18
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/string_utils.h"
21 #include "perfetto/ext/traced/sys_stats_counters.h"
22 #include "src/trace_processor/importers/common/args_tracker.h"
23 #include "src/trace_processor/importers/common/clock_tracker.h"
24 #include "src/trace_processor/importers/common/event_tracker.h"
25 #include "src/trace_processor/importers/common/process_tracker.h"
26 #include "src/trace_processor/importers/proto/metadata_tracker.h"
27 #include "src/trace_processor/importers/syscalls/syscall_tracker.h"
28 #include "src/trace_processor/types/trace_processor_context.h"
29
30 #include "protos/perfetto/common/android_log_constants.pbzero.h"
31 #include "protos/perfetto/common/builtin_clock.pbzero.h"
32 #include "protos/perfetto/config/trace_config.pbzero.h"
33 #include "protos/perfetto/trace/android/android_log.pbzero.h"
34 #include "protos/perfetto/trace/android/initial_display_state.pbzero.h"
35 #include "protos/perfetto/trace/android/packages_list.pbzero.h"
36 #include "protos/perfetto/trace/power/battery_counters.pbzero.h"
37 #include "protos/perfetto/trace/power/power_rails.pbzero.h"
38 #include "protos/perfetto/trace/ps/process_stats.pbzero.h"
39 #include "protos/perfetto/trace/ps/process_tree.pbzero.h"
40 #include "protos/perfetto/trace/sys_stats/sys_stats.pbzero.h"
41 #include "protos/perfetto/trace/system_info.pbzero.h"
42
43 #include "src/trace_processor/importers/proto/android_probes_tracker.h"
44
45 namespace perfetto {
46 namespace trace_processor {
47
AndroidProbesParser(TraceProcessorContext * context)48 AndroidProbesParser::AndroidProbesParser(TraceProcessorContext* context)
49 : context_(context),
50 batt_charge_id_(context->storage->InternString("batt.charge_uah")),
51 batt_capacity_id_(context->storage->InternString("batt.capacity_pct")),
52 batt_current_id_(context->storage->InternString("batt.current_ua")),
53 batt_current_avg_id_(
54 context->storage->InternString("batt.current.avg_ua")),
55 screen_state_id_(context->storage->InternString("ScreenState")) {}
56
ParseBatteryCounters(int64_t ts,ConstBytes blob)57 void AndroidProbesParser::ParseBatteryCounters(int64_t ts, ConstBytes blob) {
58 protos::pbzero::BatteryCounters::Decoder evt(blob.data, blob.size);
59 if (evt.has_charge_counter_uah()) {
60 TrackId track =
61 context_->track_tracker->InternGlobalCounterTrack(batt_charge_id_);
62 context_->event_tracker->PushCounter(
63 ts, static_cast<double>(evt.charge_counter_uah()), track);
64 }
65 if (evt.has_capacity_percent()) {
66 TrackId track =
67 context_->track_tracker->InternGlobalCounterTrack(batt_capacity_id_);
68 context_->event_tracker->PushCounter(
69 ts, static_cast<double>(evt.capacity_percent()), track);
70 }
71 if (evt.has_current_ua()) {
72 TrackId track =
73 context_->track_tracker->InternGlobalCounterTrack(batt_current_id_);
74 context_->event_tracker->PushCounter(
75 ts, static_cast<double>(evt.current_ua()), track);
76 }
77 if (evt.has_current_avg_ua()) {
78 TrackId track =
79 context_->track_tracker->InternGlobalCounterTrack(batt_current_avg_id_);
80 context_->event_tracker->PushCounter(
81 ts, static_cast<double>(evt.current_avg_ua()), track);
82 }
83 }
84
ParsePowerRails(int64_t ts,ConstBytes blob)85 void AndroidProbesParser::ParsePowerRails(int64_t ts, ConstBytes blob) {
86 protos::pbzero::PowerRails::Decoder evt(blob.data, blob.size);
87
88 // Descriptors should have been processed at tokenization time.
89 PERFETTO_DCHECK(evt.has_energy_data());
90
91 // Because we have some special code in the tokenization phase, we
92 // will only every get one EnergyData message per packet. Therefore,
93 // we can just read the data directly.
94 auto it = evt.energy_data();
95 protos::pbzero::PowerRails::EnergyData::Decoder desc(*it);
96
97 auto opt_rail_name =
98 AndroidProbesTracker::GetOrCreate(context_)->GetPowerRailName(
99 desc.index());
100 if (opt_rail_name) {
101 // The tokenization makes sure that this field is always present and
102 // is equal to the packet's timestamp (as the packet was forged in
103 // the tokenizer).
104 PERFETTO_DCHECK(desc.has_timestamp_ms());
105 PERFETTO_DCHECK(ts / 1000000 == static_cast<int64_t>(desc.timestamp_ms()));
106
107 TrackId track =
108 context_->track_tracker->InternGlobalCounterTrack(*opt_rail_name);
109 context_->event_tracker->PushCounter(ts, static_cast<double>(desc.energy()),
110 track);
111 } else {
112 context_->storage->IncrementStats(stats::power_rail_unknown_index);
113 }
114
115 // DCHECK that we only got one message.
116 PERFETTO_DCHECK(!++it);
117 }
118
ParseAndroidLogPacket(ConstBytes blob)119 void AndroidProbesParser::ParseAndroidLogPacket(ConstBytes blob) {
120 protos::pbzero::AndroidLogPacket::Decoder packet(blob.data, blob.size);
121 for (auto it = packet.events(); it; ++it)
122 ParseAndroidLogEvent(*it);
123
124 if (packet.has_stats())
125 ParseAndroidLogStats(packet.stats());
126 }
127
ParseAndroidLogEvent(ConstBytes blob)128 void AndroidProbesParser::ParseAndroidLogEvent(ConstBytes blob) {
129 // TODO(primiano): Add events and non-stringified fields to the "raw" table.
130 protos::pbzero::AndroidLogPacket::LogEvent::Decoder evt(blob.data, blob.size);
131 int64_t ts = static_cast<int64_t>(evt.timestamp());
132 uint32_t pid = static_cast<uint32_t>(evt.pid());
133 uint32_t tid = static_cast<uint32_t>(evt.tid());
134 uint8_t prio = static_cast<uint8_t>(evt.prio());
135 StringId tag_id = context_->storage->InternString(
136 evt.has_tag() ? evt.tag() : base::StringView());
137 StringId msg_id = context_->storage->InternString(
138 evt.has_message() ? evt.message() : base::StringView());
139
140 char arg_msg[4096];
141 char* arg_str = &arg_msg[0];
142 *arg_str = '\0';
143 auto arg_avail = [&arg_msg, &arg_str]() {
144 size_t used = static_cast<size_t>(arg_str - arg_msg);
145 PERFETTO_CHECK(used <= sizeof(arg_msg));
146 return sizeof(arg_msg) - used;
147 };
148 for (auto it = evt.args(); it; ++it) {
149 protos::pbzero::AndroidLogPacket::LogEvent::Arg::Decoder arg(*it);
150 if (!arg.has_name())
151 continue;
152 arg_str += base::SprintfTrunc(arg_str, arg_avail(),
153 " %.*s=", static_cast<int>(arg.name().size),
154 arg.name().data);
155 if (arg.has_string_value()) {
156 arg_str += base::SprintfTrunc(arg_str, arg_avail(), "\"%.*s\"",
157 static_cast<int>(arg.string_value().size),
158 arg.string_value().data);
159 } else if (arg.has_int_value()) {
160 arg_str +=
161 base::SprintfTrunc(arg_str, arg_avail(), "%" PRId64, arg.int_value());
162 } else if (arg.has_float_value()) {
163 arg_str += base::SprintfTrunc(arg_str, arg_avail(), "%f",
164 static_cast<double>(arg.float_value()));
165 }
166 }
167
168 if (prio == 0)
169 prio = protos::pbzero::AndroidLogPriority::PRIO_INFO;
170
171 if (arg_str != &arg_msg[0]) {
172 PERFETTO_DCHECK(msg_id.is_null());
173 // Skip the first space char (" foo=1 bar=2" -> "foo=1 bar=2").
174 msg_id = context_->storage->InternString(&arg_msg[1]);
175 }
176 UniquePid utid = tid ? context_->process_tracker->UpdateThread(tid, pid) : 0;
177 base::Optional<int64_t> opt_trace_time = context_->clock_tracker->ToTraceTime(
178 protos::pbzero::BUILTIN_CLOCK_REALTIME, ts);
179 if (!opt_trace_time)
180 return;
181
182 // Log events are NOT required to be sorted by trace_time. The virtual table
183 // will take care of sorting on-demand.
184 context_->storage->mutable_android_log_table()->Insert(
185 {opt_trace_time.value(), utid, prio, tag_id, msg_id});
186 }
187
ParseAndroidLogStats(ConstBytes blob)188 void AndroidProbesParser::ParseAndroidLogStats(ConstBytes blob) {
189 protos::pbzero::AndroidLogPacket::Stats::Decoder evt(blob.data, blob.size);
190 if (evt.has_num_failed()) {
191 context_->storage->SetStats(stats::android_log_num_failed,
192 static_cast<int64_t>(evt.num_failed()));
193 }
194
195 if (evt.has_num_skipped()) {
196 context_->storage->SetStats(stats::android_log_num_skipped,
197 static_cast<int64_t>(evt.num_skipped()));
198 }
199
200 if (evt.has_num_total()) {
201 context_->storage->SetStats(stats::android_log_num_total,
202 static_cast<int64_t>(evt.num_total()));
203 }
204 }
205
ParseStatsdMetadata(ConstBytes blob)206 void AndroidProbesParser::ParseStatsdMetadata(ConstBytes blob) {
207 protos::pbzero::TraceConfig::StatsdMetadata::Decoder metadata(blob.data,
208 blob.size);
209 if (metadata.has_triggering_subscription_id()) {
210 context_->metadata_tracker->SetMetadata(
211 metadata::statsd_triggering_subscription_id,
212 Variadic::Integer(metadata.triggering_subscription_id()));
213 }
214 }
215
ParseAndroidPackagesList(ConstBytes blob)216 void AndroidProbesParser::ParseAndroidPackagesList(ConstBytes blob) {
217 protos::pbzero::PackagesList::Decoder pkg_list(blob.data, blob.size);
218 context_->storage->SetStats(stats::packages_list_has_read_errors,
219 pkg_list.read_error());
220 context_->storage->SetStats(stats::packages_list_has_parse_errors,
221 pkg_list.parse_error());
222
223 AndroidProbesTracker* tracker = AndroidProbesTracker::GetOrCreate(context_);
224 for (auto it = pkg_list.packages(); it; ++it) {
225 protos::pbzero::PackagesList_PackageInfo::Decoder pkg(*it);
226 std::string pkg_name = pkg.name().ToStdString();
227 if (!tracker->ShouldInsertPackage(pkg_name)) {
228 continue;
229 }
230 context_->storage->mutable_package_list_table()->Insert(
231 {context_->storage->InternString(pkg.name()),
232 static_cast<int64_t>(pkg.uid()), pkg.debuggable(),
233 pkg.profileable_from_shell(),
234 static_cast<int64_t>(pkg.version_code())});
235 tracker->InsertedPackage(std::move(pkg_name));
236 }
237 }
238
ParseInitialDisplayState(int64_t ts,ConstBytes blob)239 void AndroidProbesParser::ParseInitialDisplayState(int64_t ts,
240 ConstBytes blob) {
241 protos::pbzero::InitialDisplayState::Decoder state(blob.data, blob.size);
242
243 TrackId track =
244 context_->track_tracker->InternGlobalCounterTrack(screen_state_id_);
245 context_->event_tracker->PushCounter(ts, state.display_state(), track);
246 }
247
248 } // namespace trace_processor
249 } // namespace perfetto
250