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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PERF_PERF_TRACKER_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_PERF_TRACKER_H_ 19 20 #include <cstdint> 21 #include <functional> 22 #include <optional> 23 #include <utility> 24 25 #include "perfetto/ext/base/flat_hash_map.h" 26 #include "perfetto/ext/base/status_or.h" 27 #include "src/trace_processor/importers/common/create_mapping_params.h" 28 #include "src/trace_processor/importers/common/virtual_memory_mapping.h" 29 #include "src/trace_processor/importers/perf/auxtrace_info_record.h" 30 #include "src/trace_processor/storage/trace_storage.h" 31 #include "src/trace_processor/tables/perf_tables_py.h" 32 #include "src/trace_processor/types/destructible.h" 33 #include "src/trace_processor/types/trace_processor_context.h" 34 35 #include "protos/third_party/simpleperf/record_file.pbzero.h" 36 37 namespace perfetto::trace_processor::perf_importer { 38 39 class AuxDataTokenizer; 40 41 class PerfTracker : public Destructible { 42 public: GetOrCreate(TraceProcessorContext * context)43 static PerfTracker* GetOrCreate(TraceProcessorContext* context) { 44 if (!context->perf_tracker) { 45 context->perf_tracker.reset(new PerfTracker(context)); 46 } 47 return static_cast<PerfTracker*>(context->perf_tracker.get()); 48 } 49 ~PerfTracker() override; 50 51 using AuxDataTokenizerFactory = 52 std::function<base::StatusOr<std::unique_ptr<AuxDataTokenizer>>( 53 TraceProcessorContext*, 54 AuxtraceInfoRecord)>; 55 void RegisterAuxTokenizer(uint32_t type, AuxDataTokenizerFactory factory); 56 57 base::StatusOr<std::unique_ptr<AuxDataTokenizer>> CreateAuxDataTokenizer( 58 AuxtraceInfoRecord info); 59 60 // Add symbol data contained in a `FileFeature` proto. 61 void AddSimpleperfFile2( 62 const third_party::simpleperf::proto::pbzero::FileFeature::Decoder& file); 63 64 void CreateKernelMemoryMapping(int64_t trace_ts, CreateMappingParams params); 65 void CreateUserMemoryMapping(int64_t trace_ts, 66 UniquePid upid, 67 CreateMappingParams params); 68 69 void NotifyEndOfFile(); 70 71 private: 72 struct Dso { 73 uint64_t load_bias; 74 AddressRangeMap<std::string> symbols; 75 }; 76 explicit PerfTracker(TraceProcessorContext* context); 77 78 // Tries to symbolize any `STACK_PROFILE_FRAME` frame missing the `name` 79 // attribute. This should be called at the end of parsing when all packets 80 // have been processed and all tables updated. 81 void SymbolizeFrames(); 82 83 void SymbolizeKernelFrame(tables::StackProfileFrameTable::RowReference frame); 84 // Returns true it the frame was symbolized. 85 bool TrySymbolizeFrame(tables::StackProfileFrameTable::RowReference frame); 86 87 void AddMapping(int64_t trace_ts, 88 std::optional<UniquePid> upid, 89 const VirtualMemoryMapping& mapping); 90 91 TraceProcessorContext* const context_; 92 const tables::StackProfileMappingTable& mapping_table_; 93 base::FlatHashMap<uint32_t, AuxDataTokenizerFactory> factories_; 94 95 base::FlatHashMap<StringId, Dso> files_; 96 AddressRangeMap<std::string> kernel_symbols_; 97 }; 98 99 } // namespace perfetto::trace_processor::perf_importer 100 101 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_PERF_TRACKER_H_ 102