OSDN Git Service

am f0737a5a: (-s ours) am ae5cab41: Merge "Simpleperf: work around unexpected (pid...
[android-x86/system-extras.git] / simpleperf / thread_tree.cpp
1 /*
2  * Copyright (C) 2015 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 "thread_tree.h"
18
19 #include <limits>
20
21 #include <base/logging.h>
22
23 #include "environment.h"
24 #include "perf_event.h"
25 #include "record.h"
26
27 bool MapComparator::operator()(const MapEntry* map1, const MapEntry* map2) const {
28   if (map1->start_addr != map2->start_addr) {
29     return map1->start_addr < map2->start_addr;
30   }
31   if (map1->len != map2->len) {
32     return map1->len < map2->len;
33   }
34   if (map1->time != map2->time) {
35     return map1->time < map2->time;
36   }
37   return false;
38 }
39
40 void ThreadTree::AddThread(int pid, int tid, const std::string& comm) {
41   auto it = thread_tree_.find(tid);
42   if (it == thread_tree_.end()) {
43     ThreadEntry* thread = new ThreadEntry{
44         pid, tid,
45         "unknown",                             // comm
46         std::set<MapEntry*, MapComparator>(),  // maps
47     };
48     auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
49     CHECK(pair.second);
50     it = pair.first;
51   }
52   thread_comm_storage_.push_back(std::unique_ptr<std::string>(new std::string(comm)));
53   it->second->comm = thread_comm_storage_.back()->c_str();
54 }
55
56 void ThreadTree::ForkThread(int pid, int tid, int ppid, int ptid) {
57   ThreadEntry* parent = FindThreadOrNew(ppid, ptid);
58   ThreadEntry* child = FindThreadOrNew(pid, tid);
59   child->comm = parent->comm;
60   child->maps = parent->maps;
61 }
62
63 ThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) {
64   auto it = thread_tree_.find(tid);
65   if (it == thread_tree_.end()) {
66     AddThread(pid, tid, "unknown");
67     it = thread_tree_.find(tid);
68   } else {
69     if (pid != it->second.get()->pid) {
70       // TODO: b/22185053.
71       LOG(DEBUG) << "unexpected (pid, tid) pair: expected (" << it->second.get()->pid << ", " << tid
72                  << "), actual (" << pid << ", " << tid << ")";
73     }
74   }
75   return it->second.get();
76 }
77
78 static void RemoveOverlappedMap(std::set<MapEntry*, MapComparator>* map_set, const MapEntry* map) {
79   for (auto it = map_set->begin(); it != map_set->end();) {
80     if ((*it)->start_addr >= map->start_addr + map->len) {
81       break;
82     }
83     if ((*it)->start_addr + (*it)->len <= map->start_addr) {
84       ++it;
85     } else {
86       it = map_set->erase(it);
87     }
88   }
89 }
90
91 void ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time,
92                               const std::string& filename) {
93   // kernel map len can be 0 when record command is not run in supervisor mode.
94   if (len == 0) {
95     return;
96   }
97   Dso* dso = FindKernelDsoOrNew(filename);
98   MapEntry* map = new MapEntry{
99       start_addr, len, pgoff, time, dso,
100   };
101   map_storage_.push_back(std::unique_ptr<MapEntry>(map));
102   RemoveOverlappedMap(&kernel_map_tree_, map);
103   auto pair = kernel_map_tree_.insert(map);
104   CHECK(pair.second);
105 }
106
107 Dso* ThreadTree::FindKernelDsoOrNew(const std::string& filename) {
108   if (filename == DEFAULT_KERNEL_MMAP_NAME) {
109     if (kernel_dso_ == nullptr) {
110       kernel_dso_ = Dso::CreateDso(DSO_KERNEL);
111     }
112     return kernel_dso_.get();
113   }
114   auto it = module_dso_tree_.find(filename);
115   if (it == module_dso_tree_.end()) {
116     module_dso_tree_[filename] = Dso::CreateDso(DSO_KERNEL_MODULE, filename);
117     it = module_dso_tree_.find(filename);
118   }
119   return it->second.get();
120 }
121
122 void ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
123                               uint64_t time, const std::string& filename) {
124   ThreadEntry* thread = FindThreadOrNew(pid, tid);
125   Dso* dso = FindUserDsoOrNew(filename);
126   MapEntry* map = new MapEntry{
127       start_addr, len, pgoff, time, dso,
128   };
129   map_storage_.push_back(std::unique_ptr<MapEntry>(map));
130   RemoveOverlappedMap(&thread->maps, map);
131   auto pair = thread->maps.insert(map);
132   CHECK(pair.second);
133 }
134
135 Dso* ThreadTree::FindUserDsoOrNew(const std::string& filename) {
136   auto it = user_dso_tree_.find(filename);
137   if (it == user_dso_tree_.end()) {
138     user_dso_tree_[filename] = Dso::CreateDso(DSO_ELF_FILE, filename);
139     it = user_dso_tree_.find(filename);
140   }
141   return it->second.get();
142 }
143
144 static bool IsAddrInMap(uint64_t addr, const MapEntry* map) {
145   return (addr >= map->start_addr && addr < map->start_addr + map->len);
146 }
147
148 static MapEntry* FindMapByAddr(const std::set<MapEntry*, MapComparator>& maps, uint64_t addr) {
149   // Construct a map_entry which is strictly after the searched map_entry, based on MapComparator.
150   MapEntry find_map = {
151       addr,                                            // start_addr
152       std::numeric_limits<unsigned long long>::max(),  // len
153       0,                                               // pgoff
154       std::numeric_limits<unsigned long long>::max(),  // time
155       nullptr,                                         // dso
156   };
157   auto it = maps.upper_bound(&find_map);
158   if (it != maps.begin() && IsAddrInMap(addr, *--it)) {
159     return *it;
160   }
161   return nullptr;
162 }
163
164 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
165   MapEntry* result = nullptr;
166   if (!in_kernel) {
167     result = FindMapByAddr(thread->maps, ip);
168   } else {
169     result = FindMapByAddr(kernel_map_tree_, ip);
170   }
171   return result != nullptr ? result : &unknown_map_;
172 }
173
174 const Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip) {
175   uint64_t offset_in_file;
176   if (map->dso == kernel_dso_.get()) {
177     offset_in_file = ip;
178   } else {
179     offset_in_file = ip - map->start_addr + map->pgoff;
180   }
181   const Symbol* symbol = map->dso->FindSymbol(offset_in_file);
182   if (symbol == nullptr) {
183     symbol = &unknown_symbol_;
184   }
185   return symbol;
186 }
187
188 void BuildThreadTree(const Record& record, ThreadTree* thread_tree) {
189   if (record.header.type == PERF_RECORD_MMAP) {
190     const MmapRecord& r = *static_cast<const MmapRecord*>(&record);
191     if ((r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL) {
192       thread_tree->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time,
193                                 r.filename);
194     } else {
195       thread_tree->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff,
196                                 r.sample_id.time_data.time, r.filename);
197     }
198   } else if (record.header.type == PERF_RECORD_MMAP2) {
199     const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record);
200     if ((r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL) {
201       thread_tree->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time,
202                                 r.filename);
203     } else {
204       std::string filename =
205           (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename;
206       thread_tree->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff,
207                                 r.sample_id.time_data.time, filename);
208     }
209   } else if (record.header.type == PERF_RECORD_COMM) {
210     const CommRecord& r = *static_cast<const CommRecord*>(&record);
211     thread_tree->AddThread(r.data.pid, r.data.tid, r.comm);
212   } else if (record.header.type == PERF_RECORD_FORK) {
213     const ForkRecord& r = *static_cast<const ForkRecord*>(&record);
214     thread_tree->ForkThread(r.data.pid, r.data.tid, r.data.ppid, r.data.ptid);
215   }
216 }