xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/statsd_module.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2022 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_STATSD_MODULE_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_STATSD_MODULE_H_
19 
20 #include <cstdint>
21 #include <optional>
22 
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "protos/perfetto/trace/trace_packet.pbzero.h"
25 #include "src/trace_processor/importers/common/async_track_set_tracker.h"
26 #include "src/trace_processor/importers/common/trace_parser.h"
27 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
28 #include "src/trace_processor/importers/proto/proto_importer_module.h"
29 #include "src/trace_processor/storage/trace_storage.h"
30 #include "src/trace_processor/tables/sched_tables_py.h"
31 #include "src/trace_processor/tables/slice_tables_py.h"
32 #include "src/trace_processor/tables/track_tables_py.h"
33 #include "src/trace_processor/types/trace_processor_context.h"
34 #include "src/trace_processor/util/descriptors.h"
35 #include "src/trace_processor/util/proto_to_args_parser.h"
36 
37 namespace perfetto {
38 namespace trace_processor {
39 
40 // Wraps a DescriptorPool and a pointer into that pool. This prevents
41 // common bugs where moving/changing the pool invalidates the pointer.
42 class PoolAndDescriptor {
43  public:
44   PoolAndDescriptor(const uint8_t* data, size_t size, const char* name);
45   virtual ~PoolAndDescriptor();
46 
pool()47   const DescriptorPool* pool() const { return &pool_; }
48 
descriptor()49   const ProtoDescriptor* descriptor() const { return descriptor_; }
50 
51  private:
52   PoolAndDescriptor(const PoolAndDescriptor&) = delete;
53   PoolAndDescriptor& operator=(const PoolAndDescriptor&) = delete;
54   PoolAndDescriptor(PoolAndDescriptor&&) = delete;
55   PoolAndDescriptor& operator=(PoolAndDescriptor&&) = delete;
56 
57   DescriptorPool pool_;
58   const ProtoDescriptor* descriptor_{};
59 };
60 
61 class StatsdModule : public ProtoImporterModule {
62  public:
63   explicit StatsdModule(TraceProcessorContext* context);
64 
65   ~StatsdModule() override;
66 
67   ModuleResult TokenizePacket(const protos::pbzero::TracePacket::Decoder&,
68                               TraceBlobView* packet,
69                               int64_t packet_timestamp,
70                               RefPtr<PacketSequenceStateGeneration> state,
71                               uint32_t field_id) override;
72 
73   void ParseTracePacketData(const protos::pbzero::TracePacket::Decoder& decoder,
74                             int64_t ts,
75                             const TracePacketData&,
76                             uint32_t field_id) override;
77 
78  private:
79   void ParseAtom(int64_t ts, protozero::ConstBytes bytes);
80   StringId GetAtomName(uint32_t atom_field_id);
81   AsyncTrackSetTracker::TrackSetId InternAsyncTrackSetId();
82 
83   TraceProcessorContext* context_;
84   base::FlatHashMap<uint32_t, StringId> atom_names_;
85   PoolAndDescriptor pool_;
86   util::ProtoToArgsParser args_parser_;
87   std::optional<AsyncTrackSetTracker::TrackSetId> track_set_id_;
88 };
89 
90 }  // namespace trace_processor
91 }  // namespace perfetto
92 
93 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_STATSD_MODULE_H_
94