11#pragma once
22
3- #include < stdint.h>
43#include < fcntl.h>
4+ #include < stdint.h>
55
66#include < cstdint>
77#include < cstdio>
88#include < cstdlib>
99#include < memory>
10- #include < vector>
11- #include < unordered_map>
1210#include < mutex>
11+ #include < unordered_map>
12+ #include < vector>
1313
1414#include < bionic/macros.h>
1515#include < unwindstack/Unwinder.h>
1616
1717#include " Config.h"
1818
19- enum MemType{ HOST, MMAP, DMA };
19+ enum MemType { HOST, MMAP, DMA };
2020
2121struct FrameKeyType {
22- size_t num_frames;
23- uintptr_t * frames;
24-
25- bool operator ==(const FrameKeyType& comp) const {
26- if (num_frames != comp.num_frames ) return false ;
27- for (size_t i = 0 ; i < num_frames; i++) {
28- if (frames[i] != comp.frames [i]) {
29- return false ;
30- }
22+ size_t num_frames;
23+ uintptr_t * frames;
24+
25+ bool operator ==(const FrameKeyType& comp) const {
26+ if (num_frames != comp.num_frames )
27+ return false ;
28+ for (size_t i = 0 ; i < num_frames; i++) {
29+ if (frames[i] != comp.frames [i]) {
30+ return false ;
31+ }
32+ }
33+ return true ;
3134 }
32- return true ;
33- }
3435};
3536
3637// 新增 hash 算法
3738namespace std {
38- template <> struct hash <FrameKeyType> {
39- std::size_t operator ()(const FrameKeyType& key) const {
40- std::size_t cur_hash = key.frames [0 ];
41- // Limit the number of frames to speed up hashing.
42- size_t max_frames = (key.num_frames > 5 ) ? 5 : key.num_frames ;
43- for (size_t i = 1 ; i < max_frames; i++) {
44- cur_hash ^= key.frames [i];
39+ template <>
40+ struct hash <FrameKeyType> {
41+ std::size_t operator ()(const FrameKeyType& key) const {
42+ std::size_t cur_hash = key.frames [0 ];
43+ // Limit the number of frames to speed up hashing.
44+ size_t max_frames = (key.num_frames > 5 ) ? 5 : key.num_frames ;
45+ for (size_t i = 1 ; i < max_frames; i++) {
46+ cur_hash ^= key.frames [i];
47+ }
48+ return cur_hash;
4549 }
46- return cur_hash;
47- }
4850};
4951}; // namespace std
5052
5153struct FrameInfoType {
52- size_t references = 0 ;
53- std::vector<uintptr_t > frames;
54+ size_t references = 0 ;
55+ std::vector<uintptr_t > frames;
5456};
5557
5658// 新增 timeval 比较函数
@@ -62,64 +64,67 @@ inline bool operator<(const timeval& lhs, const timeval& rhs) {
6264}
6365
6466struct PointerInfoType {
65- size_t size;
66- size_t hash_index;
67- MemType mem_type;
68- timeval alloc_time;
69- size_t RealSize () const { return size & ~(1U << 31 ); }
70- static size_t MaxSize () { return (1U << 31 ) - 1 ; }
67+ size_t size;
68+ size_t hash_index;
69+ MemType mem_type;
70+ timeval alloc_time;
71+ size_t RealSize () const { return size & ~(1U << 31 ); }
72+ static size_t MaxSize () { return (1U << 31 ) - 1 ; }
7173};
7274
7375struct ListInfoType {
74- uintptr_t pointer;
75- size_t num_allocations;
76- size_t size;
77- MemType mem_type;
78- FrameInfoType* frame_info;
79- std::shared_ptr<std::vector<unwindstack::FrameData>> backtrace_info;
80- timeval alloc_time;
76+ uintptr_t pointer;
77+ size_t num_allocations;
78+ size_t size;
79+ MemType mem_type;
80+ FrameInfoType* frame_info;
81+ std::shared_ptr<std::vector<unwindstack::FrameData>> backtrace_info;
82+ timeval alloc_time;
8183};
8284using Pred = std::function<bool (const ListInfoType&, const ListInfoType&)>;
8385
8486class PointerData {
8587public:
86- PointerData () = default ;
87- virtual ~PointerData () = default ;
88+ PointerData () = default ;
89+ virtual ~PointerData () = default ;
8890
89- bool Initialize (const Config& config);
91+ bool Initialize (const Config& config);
9092
91- size_t AddBacktrace (size_t num_frames, size_t size_bytes);
92- void RemoveBacktrace (size_t hash_index);
93+ size_t AddBacktrace (size_t num_frames, size_t size_bytes);
94+ void RemoveBacktrace (size_t hash_index);
9395
94- void Add (uintptr_t pointer, size_t size, MemType type);
95- void AddHost (const void * ptr, size_t pointer_size, MemType type = HOST);
96- void AddDMA (const uint32_t ptr, size_t pointer_size, MemType type = DMA);
97- void Remove (uintptr_t pointer, bool is_dma);
98- void RemoveHost (const void * ptr);
99- void RemoveDMA (const uint32_t ptr);
96+ void Add (uintptr_t pointer, size_t size, MemType type);
97+ void AddHost (const void * ptr, size_t pointer_size, MemType type = HOST);
98+ void AddDMA (const uint32_t ptr, size_t pointer_size, MemType type = DMA);
99+ void Remove (uintptr_t pointer, bool is_dma);
100+ void RemoveHost (const void * ptr);
101+ void RemoveDMA (const uint32_t ptr);
100102
101- void DumpLiveToFile (int fd);
102- void DumpPeakInfo ();
103+ void DumpLiveToFile (int fd);
104+ void DumpPeakInfo ();
103105
104106private:
105- inline uintptr_t ManglePointer (uintptr_t pointer) { return pointer ^ UINTPTR_MAX; }
106- inline uintptr_t DemanglePointer (uintptr_t pointer) { return pointer ^ UINTPTR_MAX; }
107+ inline uintptr_t ManglePointer (uintptr_t pointer) { return pointer ^ UINTPTR_MAX; }
108+ inline uintptr_t DemanglePointer (uintptr_t pointer) {
109+ return pointer ^ UINTPTR_MAX;
110+ }
107111
108- void GetList (std::vector<ListInfoType>* list, bool only_with_backtrace, Pred pred);
109- void GetUniqueList (std::vector<ListInfoType>* list, bool only_with_backtrace);
112+ void GetList (std::vector<ListInfoType>* list, bool only_with_backtrace, Pred pred);
113+ void GetUniqueList (std::vector<ListInfoType>* list, bool only_with_backtrace);
110114
111- std::mutex pointer_mutex_;
112- std::unordered_map<uintptr_t , PointerInfoType> pointers_;
115+ std::mutex pointer_mutex_;
116+ std::unordered_map<uintptr_t , PointerInfoType> pointers_;
113117
114- std::mutex frame_mutex_;
115- std::unordered_map<FrameKeyType, size_t > key_to_index_;
116- std::unordered_map<size_t , FrameInfoType> frames_;
117- std::unordered_map<size_t , std::shared_ptr<std::vector<unwindstack::FrameData>>> backtraces_info_;
118- size_t cur_hash_index_;
118+ std::mutex frame_mutex_;
119+ std::unordered_map<FrameKeyType, size_t > key_to_index_;
120+ std::unordered_map<size_t , FrameInfoType> frames_;
121+ std::unordered_map<size_t , std::shared_ptr<std::vector<unwindstack::FrameData>>>
122+ backtraces_info_;
123+ size_t cur_hash_index_;
119124
120- size_t current_used, current_host, current_dma;
121- size_t peak_tot, peak_host, peak_dma;
122- std::vector<ListInfoType> peak_list;
125+ size_t current_used, current_host, current_dma;
126+ size_t peak_tot, peak_host, peak_dma;
127+ std::vector<ListInfoType> peak_list;
123128
124- BIONIC_DISALLOW_COPY_AND_ASSIGN (PointerData);
129+ BIONIC_DISALLOW_COPY_AND_ASSIGN (PointerData);
125130};
0 commit comments