OSDN Git Service

am ae5cab41: Merge "Simpleperf: work around unexpected (pid, tid) pair."
[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 <base/logging.h>
20 #include "environment.h"
21 #include "perf_event.h"
22 #include "record.h"
23
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;
27   }
28   if (map1->len != map2->len) {
29     return map1->len < map2->len;
30   }
31   if (map1->time != map2->time) {
32     return map1->time < map2->time;
33   }
34   return false;
35 }
36
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{
41         pid, tid,
42         "unknown",                             // comm
43         std::set<MapEntry*, MapComparator>(),  // maps
44     };
45     auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
46     CHECK(pair.second);
47     it = pair.first;
48   }
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();
51 }
52
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;
58 }
59
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);
65   } else {
66     if (pid != it->second.get()->pid) {
67       // TODO: b/22185053.
68       LOG(DEBUG) << "unexpected (pid, tid) pair: expected (" << it->second.get()->pid << ", " << tid
69                  << "), actual (" << pid << ", " << tid << ")";
70     }
71   }
72   return it->second.get();
73 }
74
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) {
78       break;
79     }
80     if ((*it)->start_addr + (*it)->len <= map->start_addr) {
81       ++it;
82     } else {
83       it = map_set->erase(it);
84     }
85   }
86 }
87
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.
91   if (len == 0) {
92     return;
93   }
94   DsoEntry* dso = FindKernelDsoOrNew(filename);
95   MapEntry* map = new MapEntry{
96       start_addr, len, pgoff, time, dso,
97   };
98   map_storage_.push_back(std::unique_ptr<MapEntry>(map));
99   RemoveOverlappedMap(&kernel_map_tree_, map);
100   auto pair = kernel_map_tree_.insert(map);
101   CHECK(pair.second);
102 }
103
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);
108     }
109     return kernel_dso_.get();
110   }
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);
115   }
116   return it->second.get();
117 }
118
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,
125   };
126   map_storage_.push_back(std::unique_ptr<MapEntry>(map));
127   RemoveOverlappedMap(&thread->maps, map);
128   auto pair = thread->maps.insert(map);
129   CHECK(pair.second);
130 }
131
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);
137   }
138   return it->second.get();
139 }
140
141 static bool IsAddrInMap(uint64_t addr, const MapEntry* map) {
142   return (addr >= map->start_addr && addr < map->start_addr + map->len);
143 }
144
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 = {
148       addr,        // start_addr
149       ULLONG_MAX,  // len
150       0,           // pgoff
151       ULLONG_MAX,  // time
152       nullptr,     // dso
153   };
154   auto it = maps.upper_bound(&find_map);
155   if (it != maps.begin() && IsAddrInMap(addr, *--it)) {
156     return *it;
157   }
158   return nullptr;
159 }
160
161 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
162   MapEntry* result = nullptr;
163   if (!in_kernel) {
164     result = FindMapByAddr(thread->maps, ip);
165   } else {
166     result = FindMapByAddr(kernel_map_tree_, ip);
167   }
168   return result != nullptr ? result : &unknown_map_;
169 }
170
171 const SymbolEntry* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip) {
172   uint64_t offset_in_file;
173   if (map->dso == kernel_dso_.get()) {
174     offset_in_file = ip;
175   } else {
176     offset_in_file = ip - map->start_addr + map->pgoff;
177   }
178   const SymbolEntry* symbol = map->dso->FindSymbol(offset_in_file);
179   if (symbol == nullptr) {
180     symbol = &unknown_symbol_;
181   }
182   return symbol;
183 }
184
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,
191                                   r.filename);
192       } else {
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);
195       }
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,
200                                   r.filename);
201       } else {
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);
206       }
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);
213     }
214   }
215 }