xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/network_trace_module.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2023 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_NETWORK_TRACE_MODULE_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_NETWORK_TRACE_MODULE_H_
19 
20 #include <cstdint>
21 
22 #include "perfetto/ext/base/flat_hash_map.h"
23 #include "perfetto/protozero/scattered_heap_buffer.h"
24 #include "src/trace_processor/importers/common/args_tracker.h"
25 #include "src/trace_processor/importers/common/parser_types.h"
26 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
27 #include "src/trace_processor/importers/proto/proto_importer_module.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 #include "src/trace_processor/types/trace_processor_context.h"
30 
31 #include "protos/perfetto/trace/android/network_trace.pbzero.h"
32 #include "protos/perfetto/trace/trace_packet.pbzero.h"
33 
34 namespace perfetto {
35 namespace trace_processor {
36 
37 class NetworkTraceModule : public ProtoImporterModule {
38  public:
39   explicit NetworkTraceModule(TraceProcessorContext* context);
40   ~NetworkTraceModule() override = default;
41 
42   // Tokenize and de-intern NetworkPacketBundles so that bundles of multiple
43   // packets are sorted appropriately. This splits bundles with per-packet
44   // details (packet_timestamps and packet_lengths) into one NetworkTraceEvent
45   // per packet. Bundles with aggregates (i.e. total_packets) are forwarded
46   // after de-interning the packet context.
47   ModuleResult TokenizePacket(
48       const protos::pbzero::TracePacket::Decoder& decoder,
49       TraceBlobView* packet,
50       int64_t ts,
51       RefPtr<PacketSequenceStateGeneration> state,
52       uint32_t field_id) override;
53 
54   void ParseTracePacketData(const protos::pbzero::TracePacket::Decoder& decoder,
55                             int64_t ts,
56                             const TracePacketData&,
57                             uint32_t field_id) override;
58 
59  private:
60   void ParseGenericEvent(int64_t ts,
61                          int64_t dur,
62                          int64_t length,
63                          int64_t count,
64                          protos::pbzero::NetworkPacketEvent::Decoder& evt);
65 
66   void ParseNetworkPacketEvent(int64_t ts, protozero::ConstBytes blob);
67   void ParseNetworkPacketBundle(int64_t ts, protozero::ConstBytes blob);
68 
69   // Helper to simplify pushing a TracePacket to the sorter. The caller fills in
70   // the packet buffer and uses this to push for sorting and reset the buffer.
71   void PushPacketBufferForSort(int64_t timestamp,
72                                RefPtr<PacketSequenceStateGeneration> state);
73 
74   StringId GetIpProto(protos::pbzero::NetworkPacketEvent::Decoder& evt);
75 
76   TraceProcessorContext* context_;
77   protozero::HeapBuffered<protos::pbzero::TracePacket> packet_buffer_;
78 
79   bool loaded_package_names_ = false;
80   base::FlatHashMap<int64_t, StringId> package_names_;
81 
82   const StringId net_arg_length_;
83   const StringId net_arg_ip_proto_;
84   const StringId net_arg_tcp_flags_;
85   const StringId net_arg_tag_;
86   const StringId net_arg_uid_;
87   const StringId net_arg_local_port_;
88   const StringId net_arg_remote_port_;
89   const StringId net_arg_icmp_type_;
90   const StringId net_arg_icmp_code_;
91   const StringId net_ipproto_tcp_;
92   const StringId net_ipproto_udp_;
93   const StringId net_ipproto_icmp_;
94   const StringId net_ipproto_icmpv6_;
95   const StringId packet_count_;
96 };
97 
98 }  // namespace trace_processor
99 }  // namespace perfetto
100 
101 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_NETWORK_TRACE_MODULE_H_
102