| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 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 | #ifndef SIMPLE_PERF_THREAD_TREE_H_ |
| 18 | #define SIMPLE_PERF_THREAD_TREE_H_ |
| 19 | |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 21 | |
| 22 | #include <limits> |
| 23 | #include <memory> |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 24 | #include <set> |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 25 | |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 26 | #include "dso.h" |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 27 | |
| 28 | struct Record; |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 29 | |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 30 | constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]"; |
| Daniel Friederich | b2465ad | 2016-10-17 12:28:03 -0500 | [diff] [blame] | 31 | // Seen in perf.data file generated by perf. |
| 32 | constexpr char DEFAULT_KERNEL_MMAP_NAME_PERF[] = "[kernel.kallsyms]_text"; |
| Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 33 | constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon"; |
| 34 | |
| Yabin Cui | 040f7b4 | 2016-04-13 21:28:54 -0700 | [diff] [blame] | 35 | namespace simpleperf { |
| 36 | |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 37 | struct MapEntry { |
| 38 | uint64_t start_addr; |
| 39 | uint64_t len; |
| 40 | uint64_t pgoff; |
| 41 | uint64_t time; // Map creation time. |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 42 | Dso* dso; |
| Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 43 | bool in_kernel; |
| Yabin Cui | 547c60e | 2015-10-12 16:56:05 -0700 | [diff] [blame] | 44 | |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 45 | MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time, |
| 46 | Dso* dso, bool in_kernel) |
| 47 | : start_addr(start_addr), |
| 48 | len(len), |
| 49 | pgoff(pgoff), |
| 50 | time(time), |
| 51 | dso(dso), |
| 52 | in_kernel(in_kernel) {} |
| 53 | MapEntry() {} |
| Yabin Cui | 547c60e | 2015-10-12 16:56:05 -0700 | [diff] [blame] | 54 | |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 55 | uint64_t get_end_addr() const { return start_addr + len; } |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct MapComparator { |
| 59 | bool operator()(const MapEntry* map1, const MapEntry* map2) const; |
| 60 | }; |
| 61 | |
| Christopher Ferris | 15933b6 | 2018-02-22 19:06:42 -0800 | [diff] [blame^] | 62 | struct MapSet { |
| 63 | std::set<MapEntry*, MapComparator> maps; |
| 64 | uint64_t version = 0u; // incremented each time changing maps |
| 65 | }; |
| Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 66 | |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 67 | struct ThreadEntry { |
| 68 | int pid; |
| 69 | int tid; |
| 70 | const char* comm; // It always refers to the latest comm. |
| Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 71 | MapSet* maps; |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 74 | // ThreadTree contains thread information (in ThreadEntry) and mmap information |
| 75 | // (in MapEntry) of the monitored threads. It also has interface to access |
| 76 | // symbols in executable binaries mapped in the monitored threads. |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 77 | class ThreadTree { |
| 78 | public: |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 79 | ThreadTree() |
| Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 80 | : show_ip_for_unknown_symbol_(false), |
| Yabin Cui | 71b533b | 2016-07-21 12:29:47 -0700 | [diff] [blame] | 81 | show_mark_for_unknown_symbol_(false), |
| Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 82 | unknown_symbol_("unknown", 0, |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 83 | std::numeric_limits<unsigned long long>::max()) { |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 84 | unknown_dso_ = Dso::CreateDso(DSO_ELF_FILE, "unknown"); |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 85 | unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(), |
| 86 | 0, 0, unknown_dso_.get(), false); |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 87 | kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME); |
| Yabin Cui | a447c0d | 2016-08-26 11:35:28 -0700 | [diff] [blame] | 88 | // We can't dump comm for pid 0 from /proc, so add it's name here. |
| Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 89 | SetThreadName(0, 0, "swapper"); |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 92 | void SetThreadName(int pid, int tid, const std::string& comm); |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 93 | void ForkThread(int pid, int tid, int ppid, int ptid); |
| 94 | ThreadEntry* FindThreadOrNew(int pid, int tid); |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 95 | void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 96 | uint64_t time, const std::string& filename); |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 97 | void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, |
| 98 | uint64_t pgoff, uint64_t time, const std::string& filename); |
| 99 | const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip, |
| 100 | bool in_kernel); |
| Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 101 | // Find map for an ip address when we don't know whether it is in kernel. |
| 102 | const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip); |
| Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 103 | const Symbol* FindSymbol(const MapEntry* map, uint64_t ip, |
| Yabin Cui | 16501ff | 2016-10-19 15:06:29 -0700 | [diff] [blame] | 104 | uint64_t* pvaddr_in_file, Dso** pdso = nullptr); |
| Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 105 | const Symbol* FindKernelSymbol(uint64_t ip); |
| Yabin Cui | 98c7584 | 2017-04-28 13:43:08 -0700 | [diff] [blame] | 106 | bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); } |
| Yabin Cui | dd9c948 | 2016-07-11 16:27:52 -0700 | [diff] [blame] | 107 | const Symbol* UnknownSymbol() const { return &unknown_symbol_; } |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 108 | |
| Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 109 | void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; } |
| Yabin Cui | 71b533b | 2016-07-21 12:29:47 -0700 | [diff] [blame] | 110 | void ShowMarkForUnknownSymbol() { |
| 111 | show_mark_for_unknown_symbol_ = true; |
| 112 | unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX); |
| 113 | } |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 114 | // Clear thread and map information, but keep loaded dso information. It saves |
| 115 | // the time to reload dso information. |
| 116 | void ClearThreadAndMap(); |
| 117 | |
| Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 118 | void AddDsoInfo(const std::string& file_path, uint32_t file_type, |
| 119 | uint64_t min_vaddr, std::vector<Symbol>* symbols); |
| 120 | |
| Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 121 | // Update thread tree with information provided by record. |
| 122 | void Update(const Record& record); |
| Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 123 | |
| Yabin Cui | 78fddd1 | 2016-10-24 14:09:26 -0700 | [diff] [blame] | 124 | std::vector<Dso*> GetAllDsos() const; |
| Yabin Cui | 52190a3 | 2017-06-02 14:47:33 -0700 | [diff] [blame] | 125 | std::vector<const ThreadEntry*> GetAllThreads() const; |
| Yabin Cui | 78fddd1 | 2016-10-24 14:09:26 -0700 | [diff] [blame] | 126 | |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 127 | private: |
| Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 128 | ThreadEntry* CreateThread(int pid, int tid); |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 129 | Dso* FindKernelDsoOrNew(const std::string& filename); |
| Yabin Cui | 63a1c3d | 2017-05-19 12:57:44 -0700 | [diff] [blame] | 130 | Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0); |
| Yabin Cui | 547c60e | 2015-10-12 16:56:05 -0700 | [diff] [blame] | 131 | MapEntry* AllocateMap(const MapEntry& value); |
| Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 132 | void FixOverlappedMap(MapSet* maps, const MapEntry* map); |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 133 | |
| 134 | std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_; |
| 135 | std::vector<std::unique_ptr<std::string>> thread_comm_storage_; |
| 136 | |
| Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 137 | std::vector<std::unique_ptr<MapSet>> map_set_storage_; |
| 138 | MapSet kernel_maps_; |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 139 | std::vector<std::unique_ptr<MapEntry>> map_storage_; |
| 140 | MapEntry unknown_map_; |
| 141 | |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 142 | std::unique_ptr<Dso> kernel_dso_; |
| 143 | std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_; |
| 144 | std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_; |
| 145 | std::unique_ptr<Dso> unknown_dso_; |
| Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 146 | bool show_ip_for_unknown_symbol_; |
| Yabin Cui | 71b533b | 2016-07-21 12:29:47 -0700 | [diff] [blame] | 147 | bool show_mark_for_unknown_symbol_; |
| Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 148 | Symbol unknown_symbol_; |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| Yabin Cui | 040f7b4 | 2016-04-13 21:28:54 -0700 | [diff] [blame] | 151 | } // namespace simpleperf |
| 152 | |
| 153 | using MapEntry = simpleperf::MapEntry; |
| 154 | using ThreadEntry = simpleperf::ThreadEntry; |
| 155 | using ThreadTree = simpleperf::ThreadTree; |
| 156 | |
| Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 157 | #endif // SIMPLE_PERF_THREAD_TREE_H_ |