OSDN Git Service

ANRdaemon: move trace result from /sdcard to /data am: d93aa41807
[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 namespace simpleperf {
29
30 struct MapEntry {
31   uint64_t start_addr;
32   uint64_t len;
33   uint64_t pgoff;
34   uint64_t time;  // Map creation time.
35   Dso* dso;
36
37   MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time, Dso* dso)
38       : start_addr(start_addr), len(len), pgoff(pgoff), time(time), dso(dso) {
39   }
40   MapEntry() {
41   }
42
43   uint64_t get_end_addr() const {
44     return start_addr + len;
45   }
46 };
47
48 struct MapComparator {
49   bool operator()(const MapEntry* map1, const MapEntry* map2) const;
50 };
51
52 struct ThreadEntry {
53   int pid;
54   int tid;
55   const char* comm;  // It always refers to the latest comm.
56   std::set<MapEntry*, MapComparator> maps;
57 };
58
59 class ThreadTree {
60  public:
61   ThreadTree() : unknown_symbol_("unknown", 0, std::numeric_limits<unsigned long long>::max()) {
62     unknown_dso_ = Dso::CreateDso(DSO_ELF_FILE, "unknown");
63     unknown_map_ =
64         MapEntry(0, std::numeric_limits<unsigned long long>::max(), 0, 0, unknown_dso_.get());
65   }
66
67   void AddThread(int pid, int tid, const std::string& comm);
68   void ForkThread(int pid, int tid, int ppid, int ptid);
69   ThreadEntry* FindThreadOrNew(int pid, int tid);
70   void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time,
71                     const std::string& filename);
72   void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
73                     uint64_t time, const std::string& filename);
74   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel);
75   const Symbol* FindSymbol(const MapEntry* map, uint64_t ip);
76   const MapEntry* UnknownMap() const {
77     return &unknown_map_;
78   }
79
80   void Clear();
81
82  private:
83   Dso* FindKernelDsoOrNew(const std::string& filename);
84   Dso* FindUserDsoOrNew(const std::string& filename);
85   MapEntry* AllocateMap(const MapEntry& value);
86   void FixOverlappedMap(std::set<MapEntry*, MapComparator>* map_set, const MapEntry* map);
87
88   std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
89   std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
90
91   std::set<MapEntry*, MapComparator> kernel_map_tree_;
92   std::vector<std::unique_ptr<MapEntry>> map_storage_;
93   MapEntry unknown_map_;
94
95   std::unique_ptr<Dso> kernel_dso_;
96   std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_;
97   std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_;
98   std::unique_ptr<Dso> unknown_dso_;
99   Symbol unknown_symbol_;
100 };
101
102 }  // namespace simpleperf
103
104 using MapEntry = simpleperf::MapEntry;
105 using ThreadEntry = simpleperf::ThreadEntry;
106 using ThreadTree = simpleperf::ThreadTree;
107
108 struct Record;
109
110 void BuildThreadTree(const Record& record, ThreadTree* thread_tree);
111
112 #endif  // SIMPLE_PERF_THREAD_TREE_H_