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_CONTENT_ANALYZER_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_CONTENT_ANALYZER_H_ 19 20 #include <cstddef> 21 #include <utility> 22 23 #include "perfetto/ext/base/flat_hash_map.h" 24 #include "perfetto/ext/base/hash.h" 25 #include "perfetto/trace_processor/trace_blob_view.h" 26 #include "src/trace_processor/importers/proto/packet_analyzer.h" 27 #include "src/trace_processor/types/trace_processor_context.h" 28 #include "src/trace_processor/util/proto_profiler.h" 29 30 namespace perfetto::trace_processor { 31 32 // Interface for a module that processes track event information. 33 class ProtoContentAnalyzer : public PacketAnalyzer { 34 public: 35 struct Sample { 36 size_t size; 37 size_t count; 38 }; 39 using PathToSamplesMap = 40 base::FlatHashMap<util::SizeProfileComputer::FieldPath, 41 Sample, 42 util::SizeProfileComputer::FieldPathHasher>; 43 using SampleAnnotation = PacketAnalyzer::SampleAnnotation; 44 45 struct SampleAnnotationHasher { 46 using argument_type = SampleAnnotation; 47 using result_type = size_t; 48 operatorSampleAnnotationHasher49 result_type operator()(const argument_type& p) const { 50 base::Hasher hash; 51 for (auto v : p) { 52 hash.Update(v.first.raw_id()); 53 hash.Update(v.second.raw_id()); 54 } 55 return static_cast<size_t>(hash.digest()); 56 } 57 }; 58 using AnnotatedSamplesMap = base:: 59 FlatHashMap<SampleAnnotation, PathToSamplesMap, SampleAnnotationHasher>; 60 61 explicit ProtoContentAnalyzer(TraceProcessorContext* context); 62 ~ProtoContentAnalyzer() override; 63 64 void ProcessPacket(const TraceBlobView&, const SampleAnnotation&) override; 65 66 void NotifyEndOfFile() override; 67 68 private: 69 TraceProcessorContext* context_; 70 util::SizeProfileComputer computer_; 71 AnnotatedSamplesMap aggregated_samples_; 72 }; 73 74 } // namespace perfetto::trace_processor 75 76 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_CONTENT_ANALYZER_H_ 77