OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // --- |
| 6 // Author: Sainbayar Sukhbaatar |
| 7 // Dai Mikurube |
| 8 // |
| 9 // This file contains a class DeepHeapProfile and its public function |
| 10 // DeepHeapProfile::FillOrderedProfile() which works as an alternative of |
| 11 // HeapProfileTable::FillOrderedProfile(). |
| 12 // |
| 13 // DeepHeapProfile::FillOrderedProfile() dumps more detailed information about |
| 14 // heap usage, which includes OS-level information such as whether the memory |
| 15 // block is actually in memory, or not. DeepHeapProfile::FillOrderedProfile() |
| 16 // uses logged data in HeapProfileTable as one of its data sources. |
| 17 // DeepHeapProfile works only when its FillOrderedProfile() is called. It has |
| 18 // overhead when dumping, but no overhead when logging. |
| 19 // |
| 20 // It currently works only on Linux. It just delegates to HeapProfileTable in |
| 21 // non-Linux environments. |
| 22 |
| 23 |
| 24 #ifndef BASE_DEEP_HEAP_PROFILE_H_ |
| 25 #define BASE_DEEP_HEAP_PROFILE_H_ |
| 26 |
| 27 #include "config.h" |
| 28 |
| 29 #if defined(__linux__) |
| 30 #define DEEP_HEAP_PROFILE 1 |
| 31 #endif |
| 32 |
| 33 #include "addressmap-inl.h" |
| 34 #include "heap-profile-table.h" |
| 35 |
| 36 class DeepHeapProfile { |
| 37 public: |
| 38 typedef HeapProfileTable::Bucket Bucket; |
| 39 typedef HeapProfileTable::AllocationMap AllocationMap; |
| 40 typedef HeapProfileTable::AllocValue AllocValue; |
| 41 typedef HeapProfileTable::Stats Stats; |
| 42 |
| 43 // Construct a DeepHeapProfile instance. It works as a wrapper of |
| 44 // HeapProfileTable. |
| 45 // |
| 46 // 'heap_profile' is a pointer to HeapProfileTable. DeepHeapProfile reads |
| 47 // data in 'heap_profile' and forwards operations to 'heap_profile' if |
| 48 // DeepHeapProfile is not available (non-Linux). |
| 49 // 'prefix' is a prefix of dumped file names. |
| 50 DeepHeapProfile(HeapProfileTable* heap_profile, const char* prefix); |
| 51 ~DeepHeapProfile(); |
| 52 |
| 53 // Fill deep profile data into buffer 'buffer' of size 'size', and return the |
| 54 // actual size occupied by the dump in 'buffer'. It works as an alternative |
| 55 // of HeapProfileTable::FillOrderedProfile. |
| 56 // |
| 57 // The profile buckets are dumped in the decreasing order of currently |
| 58 // allocated bytes. We do not provision for 0-terminating 'buffer'. |
| 59 int FillOrderedProfile(char buffer[], int buffer_size); |
| 60 |
| 61 private: |
| 62 #ifdef DEEP_HEAP_PROFILE |
| 63 struct DeepBucket { |
| 64 Bucket* bucket; |
| 65 size_t committed_size; |
| 66 int id; // Unique ID of the bucket. |
| 67 bool is_logged; // True if the stracktrace is logged to a file. |
| 68 }; |
| 69 |
| 70 typedef AddressMap<DeepBucket> DeepBucketMap; |
| 71 |
| 72 struct PageState { |
| 73 bool is_committed; // Currently, we use only this |
| 74 bool is_present; |
| 75 bool is_swapped; |
| 76 bool is_shared; |
| 77 bool is_mmap; |
| 78 }; |
| 79 |
| 80 struct RegionStats { |
| 81 size_t virtual_bytes; |
| 82 size_t committed_bytes; |
| 83 |
| 84 // Initialize 'virtual_bytes' and 'committed_bytes'. |
| 85 void Initialize(); |
| 86 |
| 87 // Update the RegionStats to include the tallies of virtual_bytes and |
| 88 // committed_bytes in the region from 'first_adress' to 'last_address' |
| 89 // inclusive. |
| 90 void Record(int pagemap_fd, uint64 first_address, uint64 last_address); |
| 91 }; |
| 92 |
| 93 struct GlobalStats { |
| 94 // All RegionStats members in this class contain the bytes of virtual |
| 95 // memory and committed memory. |
| 96 // TODO(dmikurube): These regions should be classified more precisely later |
| 97 // for more detailed analysis. |
| 98 |
| 99 // Total bytes of the process memory. |
| 100 RegionStats total; |
| 101 |
| 102 // Total bytes of memory which is mapped to a file. |
| 103 // Regions which contain file paths in the last column of /proc/<pid>/maps. |
| 104 RegionStats file_mapped; |
| 105 |
| 106 // Total bytes of memory which is mapped anonymously. |
| 107 // Regions which contain nothing in the last column of /proc/<pid>/maps. |
| 108 RegionStats anonymous; |
| 109 |
| 110 // Total bytes of memory which is labeled, but not mapped to any file. |
| 111 // Regions which contain non-path strings in the last column of |
| 112 // /proc/<pid>/maps. |
| 113 RegionStats other; |
| 114 |
| 115 // Total bytes of mmap'ed regions. |
| 116 RegionStats record_mmap; |
| 117 |
| 118 // Total bytes of malloc'ed regions. |
| 119 RegionStats record_malloc; |
| 120 }; |
| 121 |
| 122 // Open /proc/pid/pagemap and return its file descriptor. |
| 123 // File descriptors need to be refreshed after each fork. |
| 124 static int OpenProcPagemap(); |
| 125 |
| 126 // Seek to the offset of the open pagemap file pagemap_fd. |
| 127 // It returns true if succeeded. Otherwise, it returns false. |
| 128 static bool SeekProcPagemap(int pagemap_fd, uint64 address); |
| 129 |
| 130 // Read a pagemap state from the current pagemap_fd offset. |
| 131 // It returns true if succeeded. Otherwise, it returns false. |
| 132 static bool ReadProcPagemap(int pagemap_fd, PageState* state); |
| 133 |
| 134 // Returns the number of resident (including swapped) bytes of the memory |
| 135 // region starting at 'first_address' and ending at 'last_address' inclusive. |
| 136 static size_t GetCommittedSize(int pagemap_fd, |
| 137 uint64 first_address, |
| 138 uint64 last_address); |
| 139 |
| 140 // Write re-formatted /proc/self/maps into a file which has 'filename_prefix' |
| 141 // with using 'buffer' of size 'buffer_size'. |
| 142 static void WriteMapsToFile(char buffer[], int buffer_size, |
| 143 char* filename_prefix); |
| 144 |
| 145 // Compute the global statistics from /proc/self/maps and 'pagemap_fd', and |
| 146 // store the statistics in 'stats'. |
| 147 static void GetGlobalStats(int pagemap_fd, GlobalStats* stats); |
| 148 |
| 149 // Get the DeepBucket object corresponding to the given 'bucket'. |
| 150 // DeepBucket is an extension to Bucket which is declared above. |
| 151 DeepBucket* GetDeepBucket(Bucket* bucket); |
| 152 |
| 153 // Reset committed_size member variables in DeepBucket objects to 0. |
| 154 void ResetCommittedSize(Bucket** bucket_table); |
| 155 |
| 156 // Fill bucket data in 'table' into buffer 'buffer' of size 'size', and return |
| 157 // the size occupied by the bucket data in 'buffer'. 'bucket_length' is the |
| 158 // offset for 'buffer' to start filling. Statistics in 'stats' is updated if |
| 159 // it's not NULL. |
| 160 int FillBucketTable(Bucket** bucket_table, char buffer[], int buffer_size, |
| 161 int bucket_length, HeapProfileTable::Stats* stats); |
| 162 |
| 163 // Record both virtual and committed byte counts of malloc and mmap regions |
| 164 // as callback functions for AllocationMap::Iterate(). |
| 165 static void RecordAlloc(const void* pointer, AllocValue* alloc_value, |
| 166 DeepHeapProfile* deep_profile); |
| 167 static void RecordMMap(const void* pointer, AllocValue* alloc_value, |
| 168 DeepHeapProfile* deep_profile); |
| 169 void RecordAllAllocs(); |
| 170 |
| 171 // Fill a bucket (a bucket id and its corresponding calling stack) into |
| 172 // 'buffer' of size 'buffer_size'. |
| 173 int FillBucketForBucketFile(const DeepBucket* deep_bucket, |
| 174 char buffer[], |
| 175 int buffer_size); |
| 176 |
| 177 // Write a bucket table 'table' into a file of 'bucket_fd'. |
| 178 void WriteBucketsTableToBucketFile(Bucket** table, RawFD bucket_fd); |
| 179 |
| 180 // Write both malloc and mmap bucket tables into "a bucket file". |
| 181 void WriteBucketsToBucketFile(); |
| 182 |
| 183 // Fill a (deep) bucket into 'buffer' from the offset 'used_in_buffer'. |
| 184 // Add the sizes to 'profile_stats' if it's not NULL. |
| 185 static int UnparseBucket(const DeepBucket& b, |
| 186 char* buffer, int used_in_buffer, int buffer_size, |
| 187 const char* extra, |
| 188 Stats* profile_stats); |
| 189 |
| 190 // Fill statistics of a region into 'buffer'. |
| 191 static int UnparseRegionStats(const RegionStats* stats, |
| 192 const char* name, |
| 193 char* buffer, |
| 194 int used_in_buffer, |
| 195 int buffer_size); |
| 196 |
| 197 // Fill global statistics into 'buffer'. |
| 198 int UnparseGlobalStats(char* buffer, int used_in_buffer, int buffer_size); |
| 199 |
| 200 #endif // DEEP_HEAP_PROFILE |
| 201 HeapProfileTable* heap_profile_; |
| 202 #ifdef DEEP_HEAP_PROFILE |
| 203 |
| 204 int pagemap_fd_; // File descriptor of /proc/self/pagemap. |
| 205 pid_t most_recent_pid_; // Process ID of the last dump. This could change. |
| 206 GlobalStats stats_; // Stats about total memory. |
| 207 int dump_count_; // The number of dumps. |
| 208 char* filename_prefix_; // Output file prefix. |
| 209 char* profiler_buffer_; // Buffer we use many times. |
| 210 |
| 211 int bucket_id_; |
| 212 DeepBucketMap* deep_bucket_map_; |
| 213 #endif // DEEP_HEAP_PROFILE |
| 214 |
| 215 DISALLOW_COPY_AND_ASSIGN(DeepHeapProfile); |
| 216 }; |
| 217 |
| 218 #endif // BASE_DEEP_HEAP_PROFILE_H_ |
OLD | NEW |