xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/etm/target_memory.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #include "src/trace_processor/importers/etm/target_memory.h"
18 
19 #include <memory>
20 #include <optional>
21 
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "perfetto/public/compiler.h"
25 #include "src/trace_processor/importers/common/address_range.h"
26 #include "src/trace_processor/importers/etm/virtual_address_space.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 #include "src/trace_processor/types/destructible.h"
29 #include "src/trace_processor/types/trace_processor_context.h"
30 
31 namespace perfetto::trace_processor::etm {
32 
33 // static
InitStorage(TraceProcessorContext * context)34 void TargetMemory::InitStorage(TraceProcessorContext* context) {
35   PERFETTO_CHECK(context->storage->etm_target_memory() == nullptr);
36   context->storage->set_etm_target_memory(
37       std::unique_ptr<Destructible>(new TargetMemory(context)));
38 }
39 
TargetMemory(TraceProcessorContext * context)40 TargetMemory::TargetMemory(TraceProcessorContext* context)
41     : storage_(context->storage.get()) {
42   auto kernel = VirtualAddressSpace::Builder(context);
43   base::FlatHashMap<UniquePid, VirtualAddressSpace::Builder> user;
44 
45   for (auto mmap = context->storage->mmap_record_table().IterateRows(); mmap;
46        ++mmap) {
47     std::optional<UniquePid> upid = mmap.upid();
48     if (!upid) {
49       kernel.AddMapping(mmap.row_reference());
50       continue;
51     }
52     auto it = user.Find(*upid);
53     if (!it) {
54       it = user.Insert(*upid, VirtualAddressSpace::Builder(context)).first;
55     }
56     it->AddMapping(mmap.row_reference());
57   }
58 
59   kernel_memory_ = std::move(kernel).Build();
60   for (auto it = user.GetIterator(); it; ++it) {
61     user_memory_.Insert(it.key(), std::move(it.value()).Build());
62   }
63 }
64 TargetMemory::~TargetMemory() = default;
65 
FindUserSpaceForTid(uint32_t tid) const66 VirtualAddressSpace* TargetMemory::FindUserSpaceForTid(uint32_t tid) const {
67   auto user_mem = tid_to_space_.Find(tid);
68   if (PERFETTO_UNLIKELY(!user_mem)) {
69     std::optional<UniquePid> upid = FindUpidForTid(tid);
70     user_mem =
71         tid_to_space_.Insert(tid, upid ? user_memory_.Find(*upid) : nullptr)
72             .first;
73   }
74 
75   return *user_mem;
76 }
77 
FindUpidForTid(uint32_t tid) const78 std::optional<UniquePid> TargetMemory::FindUpidForTid(uint32_t tid) const {
79   const auto& tread_table = storage_->thread_table();
80   Query q;
81   q.constraints = {tread_table.tid().eq(tid)};
82   auto it = tread_table.FilterToIterator(q);
83   if (!it) {
84     return std::nullopt;
85   }
86   return it.upid();
87 }
88 
FindMapping(int64_t ts,uint32_t tid,uint64_t address) const89 const MappingVersion* TargetMemory::FindMapping(int64_t ts,
90                                                 uint32_t tid,
91                                                 uint64_t address) const {
92   if (IsKernelAddress(address)) {
93     return kernel_memory_.FindMapping(ts, address);
94   }
95   auto* vas = FindUserSpaceForTid(tid);
96   if (!vas) {
97     return nullptr;
98   }
99   return vas->FindMapping(ts, address);
100 }
101 
FindMapping(int64_t ts,uint32_t tid,const AddressRange & range) const102 const MappingVersion* TargetMemory::FindMapping(
103     int64_t ts,
104     uint32_t tid,
105     const AddressRange& range) const {
106   const MappingVersion* m = FindMapping(ts, tid, range.start());
107   if (!m || range.end() > m->end()) {
108     return nullptr;
109   }
110   return m;
111 }
112 
113 }  // namespace perfetto::trace_processor::etm
114