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/proto_importer_module.h" 28 #include "src/trace_processor/storage/trace_storage.h" 29 #include "src/trace_processor/tables/slice_tables_py.h" 30 #include "src/trace_processor/tables/track_tables_py.h" 31 #include "src/trace_processor/types/trace_processor_context.h" 32 #include "src/trace_processor/util/descriptors.h" 33 #include "src/trace_processor/util/proto_to_args_parser.h" 34 35 namespace perfetto { 36 namespace trace_processor { 37 38 // Wraps a DescriptorPool and a pointer into that pool. This prevents 39 // common bugs where moving/changing the pool invalidates the pointer. 40 class PoolAndDescriptor { 41 public: 42 PoolAndDescriptor(const uint8_t* data, size_t size, const char* name); 43 virtual ~PoolAndDescriptor(); 44 pool()45 const DescriptorPool* pool() const { return &pool_; } 46 descriptor()47 const ProtoDescriptor* descriptor() const { return descriptor_; } 48 49 private: 50 PoolAndDescriptor(const PoolAndDescriptor&) = delete; 51 PoolAndDescriptor& operator=(const PoolAndDescriptor&) = delete; 52 PoolAndDescriptor(PoolAndDescriptor&&) = delete; 53 PoolAndDescriptor& operator=(PoolAndDescriptor&&) = delete; 54 55 DescriptorPool pool_; 56 const ProtoDescriptor* descriptor_{}; 57 }; 58 59 class StatsdModule : public ProtoImporterModule { 60 public: 61 explicit StatsdModule(TraceProcessorContext* context); 62 63 ~StatsdModule() override; 64 65 void ParseTracePacketData(const protos::pbzero::TracePacket_Decoder& decoder, 66 int64_t ts, 67 const TracePacketData&, 68 uint32_t field_id) override; 69 70 private: 71 void ParseAtom(int64_t ts, protozero::ConstBytes bytes); 72 StringId GetAtomName(uint32_t atom_field_id); 73 AsyncTrackSetTracker::TrackSetId InternAsyncTrackSetId(); 74 75 TraceProcessorContext* context_; 76 base::FlatHashMap<uint32_t, StringId> atom_names_; 77 PoolAndDescriptor pool_; 78 util::ProtoToArgsParser args_parser_; 79 std::optional<AsyncTrackSetTracker::TrackSetId> track_set_id_; 80 }; 81 82 } // namespace trace_processor 83 } // namespace perfetto 84 85 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_STATSD_MODULE_H_ 86