OSDN Git Service

Merge "ext4_utils: copy mke2fs.conf to /etc" into oc-dr1-dev am: 7f60c1a8af
[android-x86/system-extras.git] / simpleperf / thread_tree.h
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
20 #include <stdint.h>
21
22 #include <limits>
23 #include <memory>
24 #include <set>
25
26 #include "dso.h"
27
28 struct Record;
29
30 constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]";
31 // Seen in perf.data file generated by perf.
32 constexpr char DEFAULT_KERNEL_MMAP_NAME_PERF[] = "[kernel.kallsyms]_text";
33 constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon";
34
35 namespace simpleperf {
36
37 struct MapEntry {
38   uint64_t start_addr;
39   uint64_t len;
40   uint64_t pgoff;
41   uint64_t time;  // Map creation time.
42   Dso* dso;
43   bool in_kernel;
44
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() {}
54
55   uint64_t get_end_addr() const { return start_addr + len; }
56 };
57
58 struct MapComparator {
59   bool operator()(const MapEntry* map1, const MapEntry* map2) const;
60 };
61
62 using MapSet = std::set<MapEntry*, MapComparator>;
63
64 struct ThreadEntry {
65   int pid;
66   int tid;
67   const char* comm;  // It always refers to the latest comm.
68   MapSet* maps;
69 };
70
71 // ThreadTree contains thread information (in ThreadEntry) and mmap information
72 // (in MapEntry) of the monitored threads. It also has interface to access
73 // symbols in executable binaries mapped in the monitored threads.
74 class ThreadTree {
75  public:
76   ThreadTree()
77       : show_ip_for_unknown_symbol_(false),
78         show_mark_for_unknown_symbol_(false),
79         unknown_symbol_("unknown", 0,
80                         std::numeric_limits<unsigned long long>::max()) {
81     unknown_dso_ = Dso::CreateDso(DSO_ELF_FILE, "unknown");
82     unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(),
83                             0, 0, unknown_dso_.get(), false);
84     kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
85     // We can't dump comm for pid 0 from /proc, so add it's name here.
86     SetThreadName(0, 0, "swapper");
87   }
88
89   void SetThreadName(int pid, int tid, const std::string& comm);
90   void ForkThread(int pid, int tid, int ppid, int ptid);
91   ThreadEntry* FindThreadOrNew(int pid, int tid);
92   void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
93                     uint64_t time, const std::string& filename);
94   void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len,
95                     uint64_t pgoff, uint64_t time, const std::string& filename);
96   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip,
97                           bool in_kernel);
98   // Find map for an ip address when we don't know whether it is in kernel.
99   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip);
100   const Symbol* FindSymbol(const MapEntry* map, uint64_t ip,
101                            uint64_t* pvaddr_in_file, Dso** pdso = nullptr);
102   const Symbol* FindKernelSymbol(uint64_t ip);
103   bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); }
104   const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
105
106   void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
107   void ShowMarkForUnknownSymbol() {
108     show_mark_for_unknown_symbol_ = true;
109     unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
110   }
111   // Clear thread and map information, but keep loaded dso information. It saves
112   // the time to reload dso information.
113   void ClearThreadAndMap();
114
115   void AddDsoInfo(const std::string& file_path, uint32_t file_type,
116                   uint64_t min_vaddr, std::vector<Symbol>* symbols);
117
118   // Update thread tree with information provided by record.
119   void Update(const Record& record);
120
121   std::vector<Dso*> GetAllDsos() const;
122   std::vector<const ThreadEntry*> GetAllThreads() const;
123
124  private:
125   ThreadEntry* CreateThread(int pid, int tid);
126   Dso* FindKernelDsoOrNew(const std::string& filename);
127   Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0);
128   MapEntry* AllocateMap(const MapEntry& value);
129   void FixOverlappedMap(MapSet* maps, const MapEntry* map);
130
131   std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
132   std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
133
134   std::vector<std::unique_ptr<MapSet>> map_set_storage_;
135   MapSet kernel_maps_;
136   std::vector<std::unique_ptr<MapEntry>> map_storage_;
137   MapEntry unknown_map_;
138
139   std::unique_ptr<Dso> kernel_dso_;
140   std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_;
141   std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_;
142   std::unique_ptr<Dso> unknown_dso_;
143   bool show_ip_for_unknown_symbol_;
144   bool show_mark_for_unknown_symbol_;
145   Symbol unknown_symbol_;
146 };
147
148 }  // namespace simpleperf
149
150 using MapEntry = simpleperf::MapEntry;
151 using ThreadEntry = simpleperf::ThreadEntry;
152 using ThreadTree = simpleperf::ThreadTree;
153
154 #endif  // SIMPLE_PERF_THREAD_TREE_H_