xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/stack_profile_tracker.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 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_COMMON_STACK_PROFILE_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_STACK_PROFILE_TRACKER_H_
19 
20 #include <cstdint>
21 #include <optional>
22 #include <tuple>
23 #include <vector>
24 
25 #include "perfetto/ext/base/flat_hash_map.h"
26 #include "perfetto/ext/base/hash.h"
27 
28 #include "src/trace_processor/storage/trace_storage.h"
29 #include "src/trace_processor/tables/profiler_tables_py.h"
30 
31 namespace perfetto {
32 namespace trace_processor {
33 
34 struct NameInPackage {
35   StringId name;
36   StringId package;
37 
38   bool operator==(const NameInPackage& b) const {
39     return std::tie(name, package) == std::tie(b.name, b.package);
40   }
41 
42   struct Hasher {
operatorNameInPackage::Hasher43     size_t operator()(const NameInPackage& o) const {
44       return static_cast<size_t>(
45           base::Hasher::Combine(o.name.raw_id(), o.package.raw_id()));
46     }
47   };
48 };
49 
50 class TraceProcessorContext;
51 
52 class StackProfileTracker {
53  public:
StackProfileTracker(TraceProcessorContext * context)54   explicit StackProfileTracker(TraceProcessorContext* context)
55       : context_(context) {}
56 
57   std::vector<FrameId> JavaFramesForName(NameInPackage name) const;
58 
59   CallsiteId InternCallsite(std::optional<CallsiteId> parent_callsite_id,
60                             FrameId frame_id,
61                             uint32_t depth);
62 
63   void OnFrameCreated(FrameId frame_id);
64 
65  private:
66   TraceProcessorContext* const context_;
67   base::FlatHashMap<tables::StackProfileCallsiteTable::Row, CallsiteId>
68       callsite_unique_row_index_;
69 
70   struct FrameKey {
71     MappingId mapping_id;
72     uint64_t rel_pc;
73 
74     bool operator==(const FrameKey& o) const {
75       return mapping_id == o.mapping_id && rel_pc == o.rel_pc;
76     }
77 
78     bool operator!=(const FrameKey& o) const { return !(*this == o); }
79 
80     struct Hasher {
operatorFrameKey::Hasher81       size_t operator()(const FrameKey& o) const {
82         return static_cast<size_t>(
83             base::Hasher::Combine(o.mapping_id.value, o.rel_pc));
84       }
85     };
86   };
87 
88   base::FlatHashMap<NameInPackage, std::vector<FrameId>, NameInPackage::Hasher>
89       java_frames_for_name_;
90 };
91 
92 }  // namespace trace_processor
93 }  // namespace perfetto
94 
95 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_STACK_PROFILE_TRACKER_H_
96