blob: 5498df536db841699bfddefa9e338a31dacf9b52 [file] [log] [blame]
Yabin Cui60a0ea92015-07-22 20:30:43 -07001/*
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 Cui60a0ea92015-07-22 20:30:43 -070020#include <stdint.h>
Yabin Cuic8485602015-08-20 15:04:39 -070021
22#include <limits>
23#include <memory>
Yabin Cui60a0ea92015-07-22 20:30:43 -070024#include <set>
Yabin Cuic8485602015-08-20 15:04:39 -070025
Yabin Cui60a0ea92015-07-22 20:30:43 -070026#include "dso.h"
Yabin Cui767dd172016-06-02 21:02:43 -070027#include "environment.h"
28
29struct Record;
Yabin Cui60a0ea92015-07-22 20:30:43 -070030
Yabin Cui040f7b42016-04-13 21:28:54 -070031namespace simpleperf {
32
Yabin Cui60a0ea92015-07-22 20:30:43 -070033struct MapEntry {
34 uint64_t start_addr;
35 uint64_t len;
36 uint64_t pgoff;
37 uint64_t time; // Map creation time.
Yabin Cuic8485602015-08-20 15:04:39 -070038 Dso* dso;
Yabin Cuib4212972016-05-25 14:08:05 -070039 bool in_kernel;
Yabin Cui547c60e2015-10-12 16:56:05 -070040
Yabin Cui9970a232016-06-29 12:18:11 -070041 MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time,
42 Dso* dso, bool in_kernel)
43 : start_addr(start_addr),
44 len(len),
45 pgoff(pgoff),
46 time(time),
47 dso(dso),
48 in_kernel(in_kernel) {}
49 MapEntry() {}
Yabin Cui547c60e2015-10-12 16:56:05 -070050
Yabin Cui9970a232016-06-29 12:18:11 -070051 uint64_t get_end_addr() const { return start_addr + len; }
Yabin Cui60a0ea92015-07-22 20:30:43 -070052};
53
54struct MapComparator {
55 bool operator()(const MapEntry* map1, const MapEntry* map2) const;
56};
57
58struct ThreadEntry {
59 int pid;
60 int tid;
61 const char* comm; // It always refers to the latest comm.
62 std::set<MapEntry*, MapComparator> maps;
63};
64
Yabin Cui767dd172016-06-02 21:02:43 -070065// ThreadTree contains thread information (in ThreadEntry) and mmap information
66// (in MapEntry) of the monitored threads. It also has interface to access
67// symbols in executable binaries mapped in the monitored threads.
Yabin Cui60a0ea92015-07-22 20:30:43 -070068class ThreadTree {
69 public:
Yabin Cui9970a232016-06-29 12:18:11 -070070 ThreadTree()
Yabin Cui15475e62016-07-14 13:26:19 -070071 : show_ip_for_unknown_symbol_(false),
Yabin Cui71b533b2016-07-21 12:29:47 -070072 show_mark_for_unknown_symbol_(false),
Yabin Cui15475e62016-07-14 13:26:19 -070073 unknown_symbol_("unknown", 0,
Yabin Cui9970a232016-06-29 12:18:11 -070074 std::numeric_limits<unsigned long long>::max()) {
Yabin Cuic8485602015-08-20 15:04:39 -070075 unknown_dso_ = Dso::CreateDso(DSO_ELF_FILE, "unknown");
Yabin Cui9970a232016-06-29 12:18:11 -070076 unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(),
77 0, 0, unknown_dso_.get(), false);
Yabin Cui767dd172016-06-02 21:02:43 -070078 kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
Yabin Cui60a0ea92015-07-22 20:30:43 -070079 }
80
81 void AddThread(int pid, int tid, const std::string& comm);
82 void ForkThread(int pid, int tid, int ppid, int ptid);
83 ThreadEntry* FindThreadOrNew(int pid, int tid);
Yabin Cui9970a232016-06-29 12:18:11 -070084 void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
Yabin Cui60a0ea92015-07-22 20:30:43 -070085 uint64_t time, const std::string& filename);
Yabin Cui9970a232016-06-29 12:18:11 -070086 void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len,
87 uint64_t pgoff, uint64_t time, const std::string& filename);
88 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip,
89 bool in_kernel);
Yabin Cuib64a8632016-05-24 18:23:33 -070090 // Find map for an ip address when we don't know whether it is in kernel.
91 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip);
Yabin Cui9970a232016-06-29 12:18:11 -070092 const Symbol* FindSymbol(const MapEntry* map, uint64_t ip,
93 uint64_t* pvaddr_in_file);
Yabin Cui6965d422016-06-15 11:41:42 -070094 const Symbol* FindKernelSymbol(uint64_t ip);
Yabin Cuidd9c9482016-07-11 16:27:52 -070095 const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
Yabin Cui60a0ea92015-07-22 20:30:43 -070096
Yabin Cui15475e62016-07-14 13:26:19 -070097 void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
Yabin Cui71b533b2016-07-21 12:29:47 -070098 void ShowMarkForUnknownSymbol() {
99 show_mark_for_unknown_symbol_ = true;
100 unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
101 }
Yabin Cui767dd172016-06-02 21:02:43 -0700102 // Clear thread and map information, but keep loaded dso information. It saves
103 // the time to reload dso information.
104 void ClearThreadAndMap();
105
106 // Update thread tree with information provided by record.
107 void Update(const Record& record);
Yabin Cuib7f481f2015-10-23 19:48:42 -0700108
Yabin Cui60a0ea92015-07-22 20:30:43 -0700109 private:
Yabin Cuic8485602015-08-20 15:04:39 -0700110 Dso* FindKernelDsoOrNew(const std::string& filename);
111 Dso* FindUserDsoOrNew(const std::string& filename);
Yabin Cui547c60e2015-10-12 16:56:05 -0700112 MapEntry* AllocateMap(const MapEntry& value);
Yabin Cui9970a232016-06-29 12:18:11 -0700113 void FixOverlappedMap(std::set<MapEntry*, MapComparator>* map_set,
114 const MapEntry* map);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700115
116 std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
117 std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
118
119 std::set<MapEntry*, MapComparator> kernel_map_tree_;
120 std::vector<std::unique_ptr<MapEntry>> map_storage_;
121 MapEntry unknown_map_;
122
Yabin Cuic8485602015-08-20 15:04:39 -0700123 std::unique_ptr<Dso> kernel_dso_;
124 std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_;
125 std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_;
126 std::unique_ptr<Dso> unknown_dso_;
Yabin Cui15475e62016-07-14 13:26:19 -0700127 bool show_ip_for_unknown_symbol_;
Yabin Cui71b533b2016-07-21 12:29:47 -0700128 bool show_mark_for_unknown_symbol_;
Yabin Cuic8485602015-08-20 15:04:39 -0700129 Symbol unknown_symbol_;
Yabin Cui767dd172016-06-02 21:02:43 -0700130 std::unordered_map<uint64_t, Dso*> dso_id_to_dso_map_;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700131};
132
Yabin Cui040f7b42016-04-13 21:28:54 -0700133} // namespace simpleperf
134
135using MapEntry = simpleperf::MapEntry;
136using ThreadEntry = simpleperf::ThreadEntry;
137using ThreadTree = simpleperf::ThreadTree;
138
Yabin Cui60a0ea92015-07-22 20:30:43 -0700139#endif // SIMPLE_PERF_THREAD_TREE_H_