2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "thread_tree.h"
19 #include <base/logging.h>
20 #include "environment.h"
21 #include "perf_event.h"
24 bool MapComparator::operator()(const MapEntry* map1, const MapEntry* map2) const {
25 if (map1->start_addr != map2->start_addr) {
26 return map1->start_addr < map2->start_addr;
28 if (map1->len != map2->len) {
29 return map1->len < map2->len;
31 if (map1->time != map2->time) {
32 return map1->time < map2->time;
37 void ThreadTree::AddThread(int pid, int tid, const std::string& comm) {
38 auto it = thread_tree_.find(tid);
39 if (it == thread_tree_.end()) {
40 ThreadEntry* thread = new ThreadEntry{
43 std::set<MapEntry*, MapComparator>(), // maps
45 auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
49 thread_comm_storage_.push_back(std::unique_ptr<std::string>(new std::string(comm)));
50 it->second->comm = thread_comm_storage_.back()->c_str();
53 void ThreadTree::ForkThread(int pid, int tid, int ppid, int ptid) {
54 ThreadEntry* parent = FindThreadOrNew(ppid, ptid);
55 ThreadEntry* child = FindThreadOrNew(pid, tid);
56 child->comm = parent->comm;
57 child->maps = parent->maps;
60 ThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) {
61 auto it = thread_tree_.find(tid);
62 if (it == thread_tree_.end()) {
63 AddThread(pid, tid, "unknown");
64 it = thread_tree_.find(tid);
66 if (pid != it->second.get()->pid) {
68 LOG(DEBUG) << "unexpected (pid, tid) pair: expected (" << it->second.get()->pid << ", " << tid
69 << "), actual (" << pid << ", " << tid << ")";
72 return it->second.get();
75 static void RemoveOverlappedMap(std::set<MapEntry*, MapComparator>* map_set, const MapEntry* map) {
76 for (auto it = map_set->begin(); it != map_set->end();) {
77 if ((*it)->start_addr >= map->start_addr + map->len) {
80 if ((*it)->start_addr + (*it)->len <= map->start_addr) {
83 it = map_set->erase(it);
88 void ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time,
89 const std::string& filename) {
90 // kernel map len can be 0 when record command is not run in supervisor mode.
94 DsoEntry* dso = FindKernelDsoOrNew(filename);
95 MapEntry* map = new MapEntry{
96 start_addr, len, pgoff, time, dso,
98 map_storage_.push_back(std::unique_ptr<MapEntry>(map));
99 RemoveOverlappedMap(&kernel_map_tree_, map);
100 auto pair = kernel_map_tree_.insert(map);
104 DsoEntry* ThreadTree::FindKernelDsoOrNew(const std::string& filename) {
105 if (filename == DEFAULT_KERNEL_MMAP_NAME) {
106 if (kernel_dso_ == nullptr) {
107 kernel_dso_ = DsoFactory::GetInstance()->CreateDso(DSO_KERNEL);
109 return kernel_dso_.get();
111 auto it = module_dso_tree_.find(filename);
112 if (it == module_dso_tree_.end()) {
113 module_dso_tree_[filename] = DsoFactory::GetInstance()->CreateDso(DSO_KERNEL_MODULE, filename);
114 it = module_dso_tree_.find(filename);
116 return it->second.get();
119 void ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
120 uint64_t time, const std::string& filename) {
121 ThreadEntry* thread = FindThreadOrNew(pid, tid);
122 DsoEntry* dso = FindUserDsoOrNew(filename);
123 MapEntry* map = new MapEntry{
124 start_addr, len, pgoff, time, dso,
126 map_storage_.push_back(std::unique_ptr<MapEntry>(map));
127 RemoveOverlappedMap(&thread->maps, map);
128 auto pair = thread->maps.insert(map);
132 DsoEntry* ThreadTree::FindUserDsoOrNew(const std::string& filename) {
133 auto it = user_dso_tree_.find(filename);
134 if (it == user_dso_tree_.end()) {
135 user_dso_tree_[filename] = DsoFactory::GetInstance()->CreateDso(DSO_ELF_FILE, filename);
136 it = user_dso_tree_.find(filename);
138 return it->second.get();
141 static bool IsAddrInMap(uint64_t addr, const MapEntry* map) {
142 return (addr >= map->start_addr && addr < map->start_addr + map->len);
145 static MapEntry* FindMapByAddr(const std::set<MapEntry*, MapComparator>& maps, uint64_t addr) {
146 // Construct a map_entry which is strictly after the searched map_entry, based on MapComparator.
147 MapEntry find_map = {
154 auto it = maps.upper_bound(&find_map);
155 if (it != maps.begin() && IsAddrInMap(addr, *--it)) {
161 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
162 MapEntry* result = nullptr;
164 result = FindMapByAddr(thread->maps, ip);
166 result = FindMapByAddr(kernel_map_tree_, ip);
168 return result != nullptr ? result : &unknown_map_;
171 const SymbolEntry* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip) {
172 uint64_t offset_in_file;
173 if (map->dso == kernel_dso_.get()) {
176 offset_in_file = ip - map->start_addr + map->pgoff;
178 const SymbolEntry* symbol = map->dso->FindSymbol(offset_in_file);
179 if (symbol == nullptr) {
180 symbol = &unknown_symbol_;
185 void BuildThreadTree(const std::vector<std::unique_ptr<Record>>& records, ThreadTree* thread_tree) {
186 for (auto& record : records) {
187 if (record->header.type == PERF_RECORD_MMAP) {
188 const MmapRecord& r = *static_cast<const MmapRecord*>(record.get());
189 if ((r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL) {
190 thread_tree->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time,
193 thread_tree->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff,
194 r.sample_id.time_data.time, r.filename);
196 } else if (record->header.type == PERF_RECORD_MMAP2) {
197 const Mmap2Record& r = *static_cast<const Mmap2Record*>(record.get());
198 if ((r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL) {
199 thread_tree->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time,
202 std::string filename =
203 (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename;
204 thread_tree->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff,
205 r.sample_id.time_data.time, filename);
207 } else if (record->header.type == PERF_RECORD_COMM) {
208 const CommRecord& r = *static_cast<const CommRecord*>(record.get());
209 thread_tree->AddThread(r.data.pid, r.data.tid, r.comm);
210 } else if (record->header.type == PERF_RECORD_FORK) {
211 const ForkRecord& r = *static_cast<const ForkRecord*>(record.get());
212 thread_tree->ForkThread(r.data.pid, r.data.tid, r.data.ppid, r.data.ptid);