1 /*
2 * Copyright (C) 2024 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/pixel_modem_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/common/machine_tracker.h"
23 #include "src/trace_processor/importers/common/track_tracker.h"
24
25 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
26 #include "src/trace_processor/importers/proto/pixel_modem_parser.h"
27 #include "src/trace_processor/sorter/trace_sorter.h"
28
29 #include "protos/perfetto/common/android_energy_consumer_descriptor.pbzero.h"
30 #include "protos/perfetto/trace/android/pixel_modem_events.pbzero.h"
31 #include "protos/perfetto/trace/trace_packet.pbzero.h"
32
33 namespace perfetto {
34 namespace trace_processor {
35
36 using perfetto::protos::pbzero::TracePacket;
37
PixelModemModule(TraceProcessorContext * context)38 PixelModemModule::PixelModemModule(TraceProcessorContext* context)
39 : context_(context), parser_(context) {
40 RegisterForField(TracePacket::kPixelModemEventsFieldNumber, context);
41 RegisterForField(TracePacket::kPixelModemTokenDatabaseFieldNumber, context);
42 }
43
TokenizePacket(const protos::pbzero::TracePacket_Decoder & decoder,TraceBlobView *,int64_t packet_timestamp,RefPtr<PacketSequenceStateGeneration> state,uint32_t field_id)44 ModuleResult PixelModemModule::TokenizePacket(
45 const protos::pbzero::TracePacket_Decoder& decoder,
46 TraceBlobView* /* packet */,
47 int64_t packet_timestamp,
48 RefPtr<PacketSequenceStateGeneration> state,
49 uint32_t field_id) {
50 // The database packet does not have a timestamp so needs to be handled at
51 // the tokenization phase.
52 if (field_id == TracePacket::kPixelModemTokenDatabaseFieldNumber) {
53 auto db = decoder.pixel_modem_token_database();
54 protos::pbzero::PixelModemTokenDatabase::Decoder database(db);
55
56 base::Status status = parser_.SetDatabase(database.database());
57 if (status.ok()) {
58 return ModuleResult::Handled();
59 } else {
60 return ModuleResult::Error(status.message());
61 }
62 }
63
64 if (field_id != TracePacket::kPixelModemEventsFieldNumber) {
65 return ModuleResult::Ignored();
66 }
67
68 // Pigweed events are similar to ftrace in that they have many events, each
69 // with their own timestamp, packed inside a single TracePacket. This means
70 // that, similar to ftrace, we need to unpack them and individually sort them.
71
72 // However, as these events are not perf sensitive, it's not worth adding
73 // a lot of machinery to shepherd these events through the sorting queues
74 // in a special way. Therefore, we just forge new packets and sort them as if
75 // they came from the underlying trace.
76 auto events = decoder.pixel_modem_events();
77 protos::pbzero::PixelModemEvents::Decoder evt(events.data, events.size);
78
79 // To reduce overhead we store events and timestamps in parallel lists.
80 // We also store timestamps within a packet as deltas.
81 auto ts_it = evt.event_time_nanos();
82 int64_t ts = 0;
83 for (auto it = evt.events(); it && ts_it; ++it, ++ts_it) {
84 protozero::ConstBytes event_bytes = *it;
85 ts += *ts_it;
86 if (ts < 0) {
87 context_->storage->IncrementStats(stats::pixel_modem_negative_timestamp);
88 continue;
89 }
90
91 protozero::HeapBuffered<protos::pbzero::TracePacket> data_packet;
92 // Keep the original timestamp to later extract as an arg; the sorter does
93 // not read this.
94 data_packet->set_timestamp(static_cast<uint64_t>(packet_timestamp));
95 data_packet->set_pixel_modem_events()->add_events(event_bytes);
96 std::vector<uint8_t> vec = data_packet.SerializeAsArray();
97 TraceBlob blob = TraceBlob::CopyFrom(vec.data(), vec.size());
98 context_->sorter->PushTracePacket(ts, state, TraceBlobView(std::move(blob)),
99 context_->machine_id());
100 }
101
102 return ModuleResult::Handled();
103 }
104
ParseTracePacketData(const TracePacket::Decoder & decoder,int64_t ts,const TracePacketData &,uint32_t field_id)105 void PixelModemModule::ParseTracePacketData(const TracePacket::Decoder& decoder,
106 int64_t ts,
107 const TracePacketData&,
108 uint32_t field_id) {
109 if (field_id != TracePacket::kPixelModemEventsFieldNumber) {
110 return;
111 }
112
113 auto events = decoder.pixel_modem_events();
114 protos::pbzero::PixelModemEvents::Decoder evt(events.data, events.size);
115 auto it = evt.events();
116
117 // We guarantee above there will be exactly one event.
118 parser_.ParseEvent(ts, decoder.timestamp(), *it);
119 }
120
121 } // namespace trace_processor
122 } // namespace perfetto
123