OSDN Git Service

Simpleperf: fix the process of parsing records.
[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 <limits.h>
21 #include <stdint.h>
22 #include <set>
23 #include "dso.h"
24
25 struct MapEntry {
26   uint64_t start_addr;
27   uint64_t len;
28   uint64_t pgoff;
29   uint64_t time;  // Map creation time.
30   DsoEntry* dso;
31 };
32
33 struct MapComparator {
34   bool operator()(const MapEntry* map1, const MapEntry* map2) const;
35 };
36
37 struct ThreadEntry {
38   int pid;
39   int tid;
40   const char* comm;  // It always refers to the latest comm.
41   std::set<MapEntry*, MapComparator> maps;
42 };
43
44 class ThreadTree {
45  public:
46   ThreadTree() : unknown_dso_(DSO_ELF_FILE, "unknown") {
47     unknown_map_ = MapEntry{
48         0,              // start_addr
49         ULLONG_MAX,     // len
50         0,              // pgoff
51         0,              // time
52         &unknown_dso_,  // dso
53     };
54     unknown_symbol_ = SymbolEntry{
55         "unknown",   // name
56         0,           // addr
57         ULLONG_MAX,  // len
58     };
59   }
60
61   void AddThread(int pid, int tid, const std::string& comm);
62   void ForkThread(int pid, int tid, int ppid, int ptid);
63   ThreadEntry* FindThreadOrNew(int pid, int tid);
64   void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time,
65                     const std::string& filename);
66   void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
67                     uint64_t time, const std::string& filename);
68   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel);
69   const SymbolEntry* FindSymbol(const MapEntry* map, uint64_t ip);
70   const MapEntry* UnknownMap() const {
71     return &unknown_map_;
72   }
73
74  private:
75   DsoEntry* FindKernelDsoOrNew(const std::string& filename);
76   DsoEntry* FindUserDsoOrNew(const std::string& filename);
77
78   std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
79   std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
80
81   std::set<MapEntry*, MapComparator> kernel_map_tree_;
82   std::vector<std::unique_ptr<MapEntry>> map_storage_;
83   MapEntry unknown_map_;
84
85   std::unique_ptr<DsoEntry> kernel_dso_;
86   std::unordered_map<std::string, std::unique_ptr<DsoEntry>> module_dso_tree_;
87   std::unordered_map<std::string, std::unique_ptr<DsoEntry>> user_dso_tree_;
88   DsoEntry unknown_dso_;
89   SymbolEntry unknown_symbol_;
90 };
91
92 struct Record;
93
94 void BuildThreadTree(const Record& record, ThreadTree* thread_tree);
95
96 #endif  // SIMPLE_PERF_THREAD_TREE_H_