• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DSO_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PERF_DSO_TRACKER_H_
19 
20 #include <cstdint>
21 #include <string>
22 
23 #include "perfetto/base/status.h"
24 #include "perfetto/ext/base/flat_hash_map.h"
25 #include "protos/third_party/simpleperf/record_file.pbzero.h"
26 #include "src/trace_processor/importers/common/address_range.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 #include "src/trace_processor/tables/profiler_tables_py.h"
29 #include "src/trace_processor/types/destructible.h"
30 #include "src/trace_processor/types/trace_processor_context.h"
31 
32 namespace perfetto::trace_processor::perf_importer {
33 
34 // Keeps track of DSO symbols to symbolize frames at the end of the trace
35 // parsing.
36 // TODO(b/334978369): We could potentially use this class (or a similar one) to
37 // process the ModuleSymbols proto packets and consolidate all symbolization in
38 // one place.
39 class DsoTracker : public Destructible {
40  public:
GetOrCreate(TraceProcessorContext * context)41   static DsoTracker& GetOrCreate(TraceProcessorContext* context) {
42     if (!context->perf_dso_tracker) {
43       context->perf_dso_tracker.reset(new DsoTracker(context));
44     }
45     return static_cast<DsoTracker&>(*context->perf_dso_tracker);
46   }
47   ~DsoTracker() override;
48 
49   // Add symbol data contained in a `FileFeature` proto.
50   void AddSimpleperfFile2(
51       const third_party::simpleperf::proto::pbzero::FileFeature::Decoder& file);
52 
53   // Tries to symbolize any `STACK_PROFILE_FRAME` frame missing the `name`
54   // attribute. This should be called at the end of parsing when all packets
55   // have been processed and all tables updated.
56   void SymbolizeFrames();
57 
58  private:
59   struct Dso {
60     uint64_t load_bias;
61     AddressRangeMap<std::string> symbols;
62   };
63 
64   explicit DsoTracker(TraceProcessorContext* context);
65 
66   void SymbolizeKernelFrame(tables::StackProfileFrameTable::RowReference frame);
67   // Returns true it the frame was symbolized.
68   bool TrySymbolizeFrame(tables::StackProfileFrameTable::RowReference frame);
69 
70   TraceProcessorContext* const context_;
71   const tables::StackProfileMappingTable& mapping_table_;
72   base::FlatHashMap<StringId, Dso> files_;
73   AddressRangeMap<std::string> kernel_symbols_;
74 };
75 
76 }  // namespace perfetto::trace_processor::perf_importer
77 
78 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PERF_DSO_TRACKER_H_
79