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_module.h"
18
19 #include "perfetto/base/build_config.h"
20 #include "perfetto/ext/base/string_writer.h"
21 #include "perfetto/protozero/scattered_heap_buffer.h"
22 #include "src/trace_processor/importers/proto/android_probes_parser.h"
23 #include "src/trace_processor/importers/proto/android_probes_tracker.h"
24 #include "src/trace_processor/timestamped_trace_piece.h"
25 #include "src/trace_processor/trace_sorter.h"
26
27 #include "protos/perfetto/config/trace_config.pbzero.h"
28 #include "protos/perfetto/trace/power/power_rails.pbzero.h"
29 #include "protos/perfetto/trace/trace_packet.pbzero.h"
30
31 namespace perfetto {
32 namespace trace_processor {
33 namespace {
34
MapToFriendlyPowerRailName(base::StringView raw)35 const char* MapToFriendlyPowerRailName(base::StringView raw) {
36 if (raw == "S4M_VDD_CPUCL0") {
37 return "cpu.little";
38 } else if (raw == "S3M_VDD_CPUCL1") {
39 return "cpu.mid";
40 } else if (raw == "S2M_VDD_CPUCL2") {
41 return "cpu.big";
42 } else if (raw == "S5M_VDD_INT") {
43 return "system.fabric";
44 } else if (raw == "PPVAR_VSYS_PWR_DISP") {
45 return "display";
46 } else if (raw == "VSYS_PWR_MODEM") {
47 return "modem";
48 } else if (raw == "S1M_VDD_MIF") {
49 return "memory.interface";
50 } else if (raw == "VSYS_PWR_WLAN_BT") {
51 return "wifi.bt";
52 } else if (raw == "L2S_VDD_AOC_RET") {
53 return "aoc.memory";
54 } else if (raw == "S9S_VDD_AOC") {
55 return "aoc.logic";
56 } else if (raw == "S5S_VDDQ_MEM") {
57 return "ddr.a";
58 } else if (raw == "S10S_VDD2L") {
59 return "ddr.b";
60 } else if (raw == "S4S_VDD2H_MEM") {
61 return "ddr.c";
62 } else if (raw == "S2S_VDD_G3D") {
63 return "gpu";
64 } else if (raw == "L9S_GNSS_CORE") {
65 return "gps";
66 } else if (raw == "VSYS_PWR_RFFE") {
67 return "radio.frontend";
68 }
69 return nullptr;
70 }
71
72 } // namespace
73
74 using perfetto::protos::pbzero::TracePacket;
75
AndroidProbesModule(TraceProcessorContext * context)76 AndroidProbesModule::AndroidProbesModule(TraceProcessorContext* context)
77 : parser_(context), context_(context) {
78 RegisterForField(TracePacket::kBatteryFieldNumber, context);
79 RegisterForField(TracePacket::kPowerRailsFieldNumber, context);
80 RegisterForField(TracePacket::kAndroidLogFieldNumber, context);
81 RegisterForField(TracePacket::kPackagesListFieldNumber, context);
82 RegisterForField(TracePacket::kInitialDisplayStateFieldNumber, context);
83 }
84
TokenizePacket(const protos::pbzero::TracePacket_Decoder &,TraceBlobView * packet,int64_t packet_timestamp,PacketSequenceState * state,uint32_t field_id)85 ModuleResult AndroidProbesModule::TokenizePacket(
86 const protos::pbzero::TracePacket_Decoder&,
87 TraceBlobView* packet,
88 int64_t packet_timestamp,
89 PacketSequenceState* state,
90 uint32_t field_id) {
91 if (field_id != TracePacket::kPowerRailsFieldNumber)
92 return ModuleResult::Ignored();
93
94 // Power rails are similar to ftrace in that they have many events, each with
95 // their own timestamp, packed inside a single TracePacket. This means that,
96 // similar to ftrace, we need to unpack them and individually sort them.
97
98 // However, as these events are not perf sensitive, it's not worth adding
99 // a lot of machinery to shepherd these events through the sorting queues
100 // in a special way. Therefore, we just forge new packets and sort them as if
101 // they came from the underlying trace.
102
103 protos::pbzero::TracePacket::Decoder decoder(packet->data(),
104 packet->length());
105 auto power_rails = decoder.power_rails();
106 protos::pbzero::PowerRails::Decoder evt(power_rails.data, power_rails.size);
107
108 for (auto it = evt.rail_descriptor(); it; ++it) {
109 protos::pbzero::PowerRails::RailDescriptor::Decoder desc(*it);
110 uint32_t idx = desc.index();
111 if (PERFETTO_UNLIKELY(idx > 256)) {
112 PERFETTO_DLOG("Skipping excessively large power_rail index %" PRIu32,
113 idx);
114 continue;
115 }
116 char counter_name[255];
117 base::StringWriter writer(counter_name, base::ArraySize(counter_name));
118 const char* friendly_name = MapToFriendlyPowerRailName(desc.rail_name());
119 if (friendly_name) {
120 writer.AppendStringView("power.rails.");
121 writer.AppendStringView(friendly_name);
122 } else {
123 writer.AppendStringView("power.");
124 writer.AppendStringView(desc.rail_name());
125 writer.AppendStringView("_uws");
126 }
127 AndroidProbesTracker::GetOrCreate(context_)->SetPowerRailName(
128 desc.index(), context_->storage->InternString(writer.GetStringView()));
129 }
130
131 // For each energy data message, turn it into its own trace packet
132 // making sure its timestamp is consistent between the packet level and
133 // the EnergyData level.
134 for (auto it = evt.energy_data(); it; ++it) {
135 protozero::ConstBytes bytes = *it;
136 protos::pbzero::PowerRails_EnergyData_Decoder data(bytes.data, bytes.size);
137 int64_t actual_ts =
138 data.has_timestamp_ms()
139 ? static_cast<int64_t>(data.timestamp_ms()) * 1000000
140 : packet_timestamp;
141
142 protozero::HeapBuffered<protos::pbzero::TracePacket> data_packet;
143 data_packet->set_timestamp(static_cast<uint64_t>(actual_ts));
144
145 auto* energy = data_packet->set_power_rails()->add_energy_data();
146 energy->set_energy(data.energy());
147 energy->set_index(data.index());
148 energy->set_timestamp_ms(static_cast<uint64_t>(actual_ts / 1000000));
149
150 std::vector<uint8_t> vec = data_packet.SerializeAsArray();
151 std::unique_ptr<uint8_t[]> buffer(new uint8_t[vec.size()]);
152 memcpy(buffer.get(), vec.data(), vec.size());
153 context_->sorter->PushTracePacket(
154 actual_ts, state, TraceBlobView(std::move(buffer), 0, vec.size()));
155 }
156
157 return ModuleResult::Handled();
158 }
159
ParsePacket(const TracePacket::Decoder & decoder,const TimestampedTracePiece & ttp,uint32_t field_id)160 void AndroidProbesModule::ParsePacket(const TracePacket::Decoder& decoder,
161 const TimestampedTracePiece& ttp,
162 uint32_t field_id) {
163 switch (field_id) {
164 case TracePacket::kBatteryFieldNumber:
165 parser_.ParseBatteryCounters(ttp.timestamp, decoder.battery());
166 return;
167 case TracePacket::kPowerRailsFieldNumber:
168 parser_.ParsePowerRails(ttp.timestamp, decoder.power_rails());
169 return;
170 case TracePacket::kAndroidLogFieldNumber:
171 parser_.ParseAndroidLogPacket(decoder.android_log());
172 return;
173 case TracePacket::kPackagesListFieldNumber:
174 parser_.ParseAndroidPackagesList(decoder.packages_list());
175 return;
176 case TracePacket::kInitialDisplayStateFieldNumber:
177 parser_.ParseInitialDisplayState(ttp.timestamp,
178 decoder.initial_display_state());
179 return;
180 }
181 }
182
ParseTraceConfig(const protos::pbzero::TraceConfig::Decoder & decoder)183 void AndroidProbesModule::ParseTraceConfig(
184 const protos::pbzero::TraceConfig::Decoder& decoder) {
185 if (decoder.has_statsd_metadata()) {
186 parser_.ParseStatsdMetadata(decoder.statsd_metadata());
187 }
188 }
189
190 } // namespace trace_processor
191 } // namespace perfetto
192