Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | /* |
| 18 | * Preparation and completion of hprof data generation. The output is |
| 19 | * written into two files and then combined. This is necessary because |
| 20 | * we generate some of the data (strings and classes) while we dump the |
| 21 | * heap, and some analysis tools require that the class and string data |
| 22 | * appear first. |
| 23 | */ |
| 24 | |
| 25 | #include "hprof.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 26 | |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 27 | #include <cutils/open_memstream.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <sys/time.h> |
| 33 | #include <sys/uio.h> |
| 34 | #include <time.h> |
| 35 | #include <time.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include <set> |
| 39 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 40 | #include "art_field-inl.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 41 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 42 | #include "base/stringprintf.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 43 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 44 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 45 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 46 | #include "common_throws.h" |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 47 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 48 | #include "dex_file-inl.h" |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 49 | #include "gc_root.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 50 | #include "gc/accounting/heap_bitmap.h" |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 51 | #include "gc/allocation_record.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 52 | #include "gc/heap.h" |
| 53 | #include "gc/space/space.h" |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 54 | #include "globals.h" |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 55 | #include "jdwp/jdwp.h" |
| 56 | #include "jdwp/jdwp_priv.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 57 | #include "mirror/class.h" |
| 58 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | #include "mirror/object-inl.h" |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 60 | #include "os.h" |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 61 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 62 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 63 | #include "thread_list.h" |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 64 | |
| 65 | namespace art { |
| 66 | |
| 67 | namespace hprof { |
| 68 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 69 | static constexpr bool kDirectStream = true; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 70 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 71 | static constexpr uint32_t kHprofTime = 0; |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 72 | static constexpr uint32_t kHprofNullThread = 0; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 73 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 74 | static constexpr size_t kMaxObjectsPerSegment = 128; |
| 75 | static constexpr size_t kMaxBytesPerSegment = 4096; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 76 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 77 | // The static field-name for the synthetic object generated to account for class static overhead. |
Mathieu Chartier | a6d3a7e | 2015-06-03 16:51:09 -0700 | [diff] [blame] | 78 | static constexpr const char* kClassOverheadName = "$classOverhead"; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 79 | |
| 80 | enum HprofTag { |
| 81 | HPROF_TAG_STRING = 0x01, |
| 82 | HPROF_TAG_LOAD_CLASS = 0x02, |
| 83 | HPROF_TAG_UNLOAD_CLASS = 0x03, |
| 84 | HPROF_TAG_STACK_FRAME = 0x04, |
| 85 | HPROF_TAG_STACK_TRACE = 0x05, |
| 86 | HPROF_TAG_ALLOC_SITES = 0x06, |
| 87 | HPROF_TAG_HEAP_SUMMARY = 0x07, |
| 88 | HPROF_TAG_START_THREAD = 0x0A, |
| 89 | HPROF_TAG_END_THREAD = 0x0B, |
| 90 | HPROF_TAG_HEAP_DUMP = 0x0C, |
| 91 | HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C, |
| 92 | HPROF_TAG_HEAP_DUMP_END = 0x2C, |
| 93 | HPROF_TAG_CPU_SAMPLES = 0x0D, |
| 94 | HPROF_TAG_CONTROL_SETTINGS = 0x0E, |
| 95 | }; |
| 96 | |
| 97 | // Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records: |
| 98 | enum HprofHeapTag { |
| 99 | // Traditional. |
| 100 | HPROF_ROOT_UNKNOWN = 0xFF, |
| 101 | HPROF_ROOT_JNI_GLOBAL = 0x01, |
| 102 | HPROF_ROOT_JNI_LOCAL = 0x02, |
| 103 | HPROF_ROOT_JAVA_FRAME = 0x03, |
| 104 | HPROF_ROOT_NATIVE_STACK = 0x04, |
| 105 | HPROF_ROOT_STICKY_CLASS = 0x05, |
| 106 | HPROF_ROOT_THREAD_BLOCK = 0x06, |
| 107 | HPROF_ROOT_MONITOR_USED = 0x07, |
| 108 | HPROF_ROOT_THREAD_OBJECT = 0x08, |
| 109 | HPROF_CLASS_DUMP = 0x20, |
| 110 | HPROF_INSTANCE_DUMP = 0x21, |
| 111 | HPROF_OBJECT_ARRAY_DUMP = 0x22, |
| 112 | HPROF_PRIMITIVE_ARRAY_DUMP = 0x23, |
| 113 | |
| 114 | // Android. |
| 115 | HPROF_HEAP_DUMP_INFO = 0xfe, |
| 116 | HPROF_ROOT_INTERNED_STRING = 0x89, |
| 117 | HPROF_ROOT_FINALIZING = 0x8a, // Obsolete. |
| 118 | HPROF_ROOT_DEBUGGER = 0x8b, |
| 119 | HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete. |
| 120 | HPROF_ROOT_VM_INTERNAL = 0x8d, |
| 121 | HPROF_ROOT_JNI_MONITOR = 0x8e, |
| 122 | HPROF_UNREACHABLE = 0x90, // Obsolete. |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 123 | HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete. |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | enum HprofHeapId { |
| 127 | HPROF_HEAP_DEFAULT = 0, |
| 128 | HPROF_HEAP_ZYGOTE = 'Z', |
Mathieu Chartier | ae1ad00 | 2014-07-18 17:58:22 -0700 | [diff] [blame] | 129 | HPROF_HEAP_APP = 'A', |
| 130 | HPROF_HEAP_IMAGE = 'I', |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | enum HprofBasicType { |
| 134 | hprof_basic_object = 2, |
| 135 | hprof_basic_boolean = 4, |
| 136 | hprof_basic_char = 5, |
| 137 | hprof_basic_float = 6, |
| 138 | hprof_basic_double = 7, |
| 139 | hprof_basic_byte = 8, |
| 140 | hprof_basic_short = 9, |
| 141 | hprof_basic_int = 10, |
| 142 | hprof_basic_long = 11, |
| 143 | }; |
| 144 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 145 | typedef uint32_t HprofStringId; |
| 146 | typedef uint32_t HprofClassObjectId; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 147 | typedef uint32_t HprofClassSerialNumber; |
| 148 | typedef uint32_t HprofStackTraceSerialNumber; |
| 149 | typedef uint32_t HprofStackFrameId; |
| 150 | static constexpr HprofStackTraceSerialNumber kHprofNullStackTrace = 0; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 151 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 152 | class EndianOutput { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 153 | public: |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 154 | EndianOutput() : length_(0), sum_length_(0), max_length_(0), started_(false) {} |
| 155 | virtual ~EndianOutput() {} |
| 156 | |
| 157 | void StartNewRecord(uint8_t tag, uint32_t time) { |
| 158 | if (length_ > 0) { |
| 159 | EndRecord(); |
| 160 | } |
| 161 | DCHECK_EQ(length_, 0U); |
| 162 | AddU1(tag); |
| 163 | AddU4(time); |
| 164 | AddU4(0xdeaddead); // Length, replaced on flush. |
| 165 | started_ = true; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 168 | void EndRecord() { |
| 169 | // Replace length in header. |
| 170 | if (started_) { |
| 171 | UpdateU4(sizeof(uint8_t) + sizeof(uint32_t), |
| 172 | length_ - sizeof(uint8_t) - 2 * sizeof(uint32_t)); |
| 173 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 174 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 175 | HandleEndRecord(); |
| 176 | |
| 177 | sum_length_ += length_; |
| 178 | max_length_ = std::max(max_length_, length_); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 179 | length_ = 0; |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 180 | started_ = false; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 183 | void AddU1(uint8_t value) { |
| 184 | AddU1List(&value, 1); |
| 185 | } |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 186 | void AddU2(uint16_t value) { |
| 187 | AddU2List(&value, 1); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 188 | } |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 189 | void AddU4(uint32_t value) { |
| 190 | AddU4List(&value, 1); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 193 | void AddU8(uint64_t value) { |
| 194 | AddU8List(&value, 1); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 197 | void AddObjectId(const mirror::Object* value) { |
| 198 | AddU4(PointerToLowMemUInt32(value)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 201 | void AddStackTraceSerialNumber(HprofStackTraceSerialNumber value) { |
| 202 | AddU4(value); |
| 203 | } |
| 204 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 205 | // The ID for the synthetic object generated to account for class static overhead. |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 206 | void AddClassStaticsId(const mirror::Class* value) { |
| 207 | AddU4(1 | PointerToLowMemUInt32(value)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 210 | void AddJniGlobalRefId(jobject value) { |
| 211 | AddU4(PointerToLowMemUInt32(value)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 214 | void AddClassId(HprofClassObjectId value) { |
| 215 | AddU4(value); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 218 | void AddStringId(HprofStringId value) { |
| 219 | AddU4(value); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 222 | void AddU1List(const uint8_t* values, size_t count) { |
| 223 | HandleU1List(values, count); |
| 224 | length_ += count; |
| 225 | } |
| 226 | void AddU2List(const uint16_t* values, size_t count) { |
| 227 | HandleU2List(values, count); |
| 228 | length_ += count * sizeof(uint16_t); |
| 229 | } |
| 230 | void AddU4List(const uint32_t* values, size_t count) { |
| 231 | HandleU4List(values, count); |
| 232 | length_ += count * sizeof(uint32_t); |
| 233 | } |
Andreas Gampe | ca71458 | 2015-04-03 19:41:34 -0700 | [diff] [blame] | 234 | virtual void UpdateU4(size_t offset, uint32_t new_value ATTRIBUTE_UNUSED) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 235 | DCHECK_LE(offset, length_ - 4); |
| 236 | } |
| 237 | void AddU8List(const uint64_t* values, size_t count) { |
| 238 | HandleU8List(values, count); |
| 239 | length_ += count * sizeof(uint64_t); |
| 240 | } |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 241 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 242 | void AddIdList(mirror::ObjectArray<mirror::Object>* values) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 243 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 244 | const int32_t length = values->GetLength(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 245 | for (int32_t i = 0; i < length; ++i) { |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 246 | AddObjectId(values->GetWithoutChecks(i)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 247 | } |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 250 | void AddUtf8String(const char* str) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 251 | // The terminating NUL character is NOT written. |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 252 | AddU1List((const uint8_t*)str, strlen(str)); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 255 | size_t Length() const { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 256 | return length_; |
| 257 | } |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 258 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 259 | size_t SumLength() const { |
| 260 | return sum_length_; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 261 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 262 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 263 | size_t MaxLength() const { |
| 264 | return max_length_; |
| 265 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 266 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 267 | protected: |
| 268 | virtual void HandleU1List(const uint8_t* values ATTRIBUTE_UNUSED, |
| 269 | size_t count ATTRIBUTE_UNUSED) { |
| 270 | } |
| 271 | virtual void HandleU2List(const uint16_t* values ATTRIBUTE_UNUSED, |
| 272 | size_t count ATTRIBUTE_UNUSED) { |
| 273 | } |
| 274 | virtual void HandleU4List(const uint32_t* values ATTRIBUTE_UNUSED, |
| 275 | size_t count ATTRIBUTE_UNUSED) { |
| 276 | } |
| 277 | virtual void HandleU8List(const uint64_t* values ATTRIBUTE_UNUSED, |
| 278 | size_t count ATTRIBUTE_UNUSED) { |
| 279 | } |
| 280 | virtual void HandleEndRecord() { |
| 281 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 282 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 283 | size_t length_; // Current record size. |
| 284 | size_t sum_length_; // Size of all data. |
| 285 | size_t max_length_; // Maximum seen length. |
| 286 | bool started_; // Was StartRecord called? |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 289 | // This keeps things buffered until flushed. |
| 290 | class EndianOutputBuffered : public EndianOutput { |
| 291 | public: |
| 292 | explicit EndianOutputBuffered(size_t reserve_size) { |
| 293 | buffer_.reserve(reserve_size); |
| 294 | } |
| 295 | virtual ~EndianOutputBuffered() {} |
| 296 | |
| 297 | void UpdateU4(size_t offset, uint32_t new_value) OVERRIDE { |
| 298 | DCHECK_LE(offset, length_ - 4); |
| 299 | buffer_[offset + 0] = static_cast<uint8_t>((new_value >> 24) & 0xFF); |
| 300 | buffer_[offset + 1] = static_cast<uint8_t>((new_value >> 16) & 0xFF); |
| 301 | buffer_[offset + 2] = static_cast<uint8_t>((new_value >> 8) & 0xFF); |
| 302 | buffer_[offset + 3] = static_cast<uint8_t>((new_value >> 0) & 0xFF); |
| 303 | } |
| 304 | |
| 305 | protected: |
| 306 | void HandleU1List(const uint8_t* values, size_t count) OVERRIDE { |
| 307 | DCHECK_EQ(length_, buffer_.size()); |
| 308 | buffer_.insert(buffer_.end(), values, values + count); |
| 309 | } |
| 310 | |
| 311 | void HandleU2List(const uint16_t* values, size_t count) OVERRIDE { |
| 312 | DCHECK_EQ(length_, buffer_.size()); |
| 313 | for (size_t i = 0; i < count; ++i) { |
| 314 | uint16_t value = *values; |
| 315 | buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF)); |
| 316 | buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF)); |
| 317 | values++; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void HandleU4List(const uint32_t* values, size_t count) OVERRIDE { |
| 322 | DCHECK_EQ(length_, buffer_.size()); |
| 323 | for (size_t i = 0; i < count; ++i) { |
| 324 | uint32_t value = *values; |
| 325 | buffer_.push_back(static_cast<uint8_t>((value >> 24) & 0xFF)); |
| 326 | buffer_.push_back(static_cast<uint8_t>((value >> 16) & 0xFF)); |
| 327 | buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF)); |
| 328 | buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF)); |
| 329 | values++; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void HandleU8List(const uint64_t* values, size_t count) OVERRIDE { |
| 334 | DCHECK_EQ(length_, buffer_.size()); |
| 335 | for (size_t i = 0; i < count; ++i) { |
| 336 | uint64_t value = *values; |
| 337 | buffer_.push_back(static_cast<uint8_t>((value >> 56) & 0xFF)); |
| 338 | buffer_.push_back(static_cast<uint8_t>((value >> 48) & 0xFF)); |
| 339 | buffer_.push_back(static_cast<uint8_t>((value >> 40) & 0xFF)); |
| 340 | buffer_.push_back(static_cast<uint8_t>((value >> 32) & 0xFF)); |
| 341 | buffer_.push_back(static_cast<uint8_t>((value >> 24) & 0xFF)); |
| 342 | buffer_.push_back(static_cast<uint8_t>((value >> 16) & 0xFF)); |
| 343 | buffer_.push_back(static_cast<uint8_t>((value >> 8) & 0xFF)); |
| 344 | buffer_.push_back(static_cast<uint8_t>((value >> 0) & 0xFF)); |
| 345 | values++; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void HandleEndRecord() OVERRIDE { |
| 350 | DCHECK_EQ(buffer_.size(), length_); |
| 351 | if (kIsDebugBuild && started_) { |
| 352 | uint32_t stored_length = |
| 353 | static_cast<uint32_t>(buffer_[5]) << 24 | |
| 354 | static_cast<uint32_t>(buffer_[6]) << 16 | |
| 355 | static_cast<uint32_t>(buffer_[7]) << 8 | |
| 356 | static_cast<uint32_t>(buffer_[8]); |
| 357 | DCHECK_EQ(stored_length, length_ - sizeof(uint8_t) - 2 * sizeof(uint32_t)); |
| 358 | } |
| 359 | HandleFlush(buffer_.data(), length_); |
| 360 | buffer_.clear(); |
| 361 | } |
| 362 | |
| 363 | virtual void HandleFlush(const uint8_t* buffer ATTRIBUTE_UNUSED, size_t length ATTRIBUTE_UNUSED) { |
| 364 | } |
| 365 | |
| 366 | std::vector<uint8_t> buffer_; |
| 367 | }; |
| 368 | |
| 369 | class FileEndianOutput FINAL : public EndianOutputBuffered { |
| 370 | public: |
| 371 | FileEndianOutput(File* fp, size_t reserved_size) |
| 372 | : EndianOutputBuffered(reserved_size), fp_(fp), errors_(false) { |
| 373 | DCHECK(fp != nullptr); |
| 374 | } |
| 375 | ~FileEndianOutput() { |
| 376 | } |
| 377 | |
| 378 | bool Errors() { |
| 379 | return errors_; |
| 380 | } |
| 381 | |
| 382 | protected: |
| 383 | void HandleFlush(const uint8_t* buffer, size_t length) OVERRIDE { |
| 384 | if (!errors_) { |
| 385 | errors_ = !fp_->WriteFully(buffer, length); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | private: |
| 390 | File* fp_; |
| 391 | bool errors_; |
| 392 | }; |
| 393 | |
| 394 | class NetStateEndianOutput FINAL : public EndianOutputBuffered { |
| 395 | public: |
| 396 | NetStateEndianOutput(JDWP::JdwpNetStateBase* net_state, size_t reserved_size) |
| 397 | : EndianOutputBuffered(reserved_size), net_state_(net_state) { |
| 398 | DCHECK(net_state != nullptr); |
| 399 | } |
| 400 | ~NetStateEndianOutput() {} |
| 401 | |
| 402 | protected: |
| 403 | void HandleFlush(const uint8_t* buffer, size_t length) OVERRIDE { |
| 404 | std::vector<iovec> iov; |
| 405 | iov.push_back(iovec()); |
| 406 | iov[0].iov_base = const_cast<void*>(reinterpret_cast<const void*>(buffer)); |
| 407 | iov[0].iov_len = length; |
| 408 | net_state_->WriteBufferedPacketLocked(iov); |
| 409 | } |
| 410 | |
| 411 | private: |
| 412 | JDWP::JdwpNetStateBase* net_state_; |
| 413 | }; |
| 414 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 415 | #define __ output_-> |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 416 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 417 | class Hprof : public SingleRootVisitor { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 418 | public: |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 419 | Hprof(const char* output_filename, int fd, bool direct_to_ddms) |
| 420 | : filename_(output_filename), |
| 421 | fd_(fd), |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 422 | direct_to_ddms_(direct_to_ddms) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 423 | LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting..."; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 424 | } |
| 425 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 426 | void Dump() |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 427 | REQUIRES(Locks::mutator_lock_) |
| 428 | REQUIRES(!Locks::heap_bitmap_lock_, !Locks::alloc_tracker_lock_) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 429 | { |
| 430 | MutexLock mu(Thread::Current(), *Locks::alloc_tracker_lock_); |
| 431 | if (Runtime::Current()->GetHeap()->IsAllocTrackingEnabled()) { |
| 432 | PopulateAllocationTrackingTraces(); |
| 433 | } |
| 434 | } |
| 435 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 436 | // First pass to measure the size of the dump. |
| 437 | size_t overall_size; |
| 438 | size_t max_length; |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 439 | { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 440 | EndianOutput count_output; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 441 | output_ = &count_output; |
| 442 | ProcessHeap(false); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 443 | overall_size = count_output.SumLength(); |
| 444 | max_length = count_output.MaxLength(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 445 | output_ = nullptr; |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 446 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 447 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 448 | bool okay; |
| 449 | if (direct_to_ddms_) { |
| 450 | if (kDirectStream) { |
| 451 | okay = DumpToDdmsDirect(overall_size, max_length, CHUNK_TYPE("HPDS")); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 452 | } else { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 453 | okay = DumpToDdmsBuffered(overall_size, max_length); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 454 | } |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 455 | } else { |
| 456 | okay = DumpToFile(overall_size, max_length); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 459 | if (okay) { |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 460 | const uint64_t duration = NanoTime() - start_ns_; |
| 461 | LOG(INFO) << "hprof: heap dump completed (" << PrettySize(RoundUp(overall_size, KB)) |
| 462 | << ") in " << PrettyDuration(duration) |
| 463 | << " objects " << total_objects_ |
| 464 | << " objects with stack traces " << total_objects_with_stack_trace_; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | private: |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 469 | static void VisitObjectCallback(mirror::Object* obj, void* arg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 470 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 471 | DCHECK(obj != nullptr); |
| 472 | DCHECK(arg != nullptr); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 473 | reinterpret_cast<Hprof*>(arg)->DumpHeapObject(obj); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 476 | void DumpHeapObject(mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 477 | SHARED_REQUIRES(Locks::mutator_lock_); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 478 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 479 | void DumpHeapClass(mirror::Class* klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 480 | SHARED_REQUIRES(Locks::mutator_lock_); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 481 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 482 | void DumpHeapArray(mirror::Array* obj, mirror::Class* klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 483 | SHARED_REQUIRES(Locks::mutator_lock_); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 484 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 485 | void DumpHeapInstanceObject(mirror::Object* obj, mirror::Class* klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 486 | SHARED_REQUIRES(Locks::mutator_lock_); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 487 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 488 | void ProcessHeap(bool header_first) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 489 | REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 490 | // Reset current heap and object count. |
| 491 | current_heap_ = HPROF_HEAP_DEFAULT; |
| 492 | objects_in_segment_ = 0; |
| 493 | |
| 494 | if (header_first) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 495 | ProcessHeader(true); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 496 | ProcessBody(); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 497 | } else { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 498 | ProcessBody(); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 499 | ProcessHeader(false); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 503 | void ProcessBody() REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 504 | Runtime* const runtime = Runtime::Current(); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 505 | // Walk the roots and the heap. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 506 | output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 507 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 508 | runtime->VisitRoots(this); |
| 509 | runtime->VisitImageRoots(this); |
| 510 | runtime->GetHeap()->VisitObjectsPaused(VisitObjectCallback, this); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 511 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 512 | output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_END, kHprofTime); |
| 513 | output_->EndRecord(); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 516 | void ProcessHeader(bool string_first) REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 517 | // Write the header. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 518 | WriteFixedHeader(); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 519 | // Write the string and class tables, and any stack traces, to the header. |
| 520 | // (jhat requires that these appear before any of the data in the body that refers to them.) |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 521 | // jhat also requires the string table appear before class table and stack traces. |
| 522 | // However, WriteStackTraces() can modify the string table, so it's necessary to call |
| 523 | // WriteStringTable() last in the first pass, to compute the correct length of the output. |
| 524 | if (string_first) { |
| 525 | WriteStringTable(); |
| 526 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 527 | WriteClassTable(); |
| 528 | WriteStackTraces(); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 529 | if (!string_first) { |
| 530 | WriteStringTable(); |
| 531 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 532 | output_->EndRecord(); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 535 | void WriteClassTable() SHARED_REQUIRES(Locks::mutator_lock_) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 536 | for (const auto& p : classes_) { |
| 537 | mirror::Class* c = p.first; |
| 538 | HprofClassSerialNumber sn = p.second; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 539 | CHECK(c != nullptr); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 540 | output_->StartNewRecord(HPROF_TAG_LOAD_CLASS, kHprofTime); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 541 | // LOAD CLASS format: |
| 542 | // U4: class serial number (always > 0) |
| 543 | // ID: class object ID. We use the address of the class object structure as its ID. |
| 544 | // U4: stack trace serial number |
| 545 | // ID: class name string ID |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 546 | __ AddU4(sn); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 547 | __ AddObjectId(c); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 548 | __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(c)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 549 | __ AddStringId(LookupClassNameId(c)); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 550 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 553 | void WriteStringTable() { |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 554 | for (const std::pair<std::string, HprofStringId>& p : strings_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 555 | const std::string& string = p.first; |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 556 | const size_t id = p.second; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 557 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 558 | output_->StartNewRecord(HPROF_TAG_STRING, kHprofTime); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 559 | |
| 560 | // STRING format: |
| 561 | // ID: ID for this string |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 562 | // U1*: UTF8 characters for string (NOT null terminated) |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 563 | // (the record format encodes the length) |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 564 | __ AddU4(id); |
| 565 | __ AddUtf8String(string.c_str()); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 566 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 569 | void StartNewHeapDumpSegment() { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 570 | // This flushes the old segment and starts a new one. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 571 | output_->StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, kHprofTime); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 572 | objects_in_segment_ = 0; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 573 | // Starting a new HEAP_DUMP resets the heap to default. |
| 574 | current_heap_ = HPROF_HEAP_DEFAULT; |
| 575 | } |
| 576 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 577 | void CheckHeapSegmentConstraints() { |
| 578 | if (objects_in_segment_ >= kMaxObjectsPerSegment || output_->Length() >= kMaxBytesPerSegment) { |
| 579 | StartNewHeapDumpSegment(); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 583 | void VisitRoot(mirror::Object* obj, const RootInfo& root_info) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 584 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 585 | void MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag, |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 586 | uint32_t thread_serial); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 587 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 588 | HprofClassObjectId LookupClassId(mirror::Class* c) SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 589 | if (c != nullptr) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 590 | auto it = classes_.find(c); |
| 591 | if (it == classes_.end()) { |
| 592 | // first time to see this class |
| 593 | HprofClassSerialNumber sn = next_class_serial_number_++; |
| 594 | classes_.Put(c, sn); |
| 595 | // Make sure that we've assigned a string ID for this class' name |
| 596 | LookupClassNameId(c); |
| 597 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 598 | } |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 599 | return PointerToLowMemUInt32(c); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 602 | HprofStackTraceSerialNumber LookupStackTraceSerialNumber(const mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 603 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 604 | auto r = allocation_records_.find(obj); |
| 605 | if (r == allocation_records_.end()) { |
| 606 | return kHprofNullStackTrace; |
| 607 | } else { |
| 608 | const gc::AllocRecordStackTrace* trace = r->second; |
| 609 | auto result = traces_.find(trace); |
| 610 | CHECK(result != traces_.end()); |
| 611 | return result->second; |
| 612 | } |
| 613 | } |
| 614 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 615 | HprofStringId LookupStringId(mirror::String* string) SHARED_REQUIRES(Locks::mutator_lock_) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 616 | return LookupStringId(string->ToModifiedUtf8()); |
| 617 | } |
| 618 | |
| 619 | HprofStringId LookupStringId(const char* string) { |
| 620 | return LookupStringId(std::string(string)); |
| 621 | } |
| 622 | |
| 623 | HprofStringId LookupStringId(const std::string& string) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 624 | auto it = strings_.find(string); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 625 | if (it != strings_.end()) { |
| 626 | return it->second; |
| 627 | } |
| 628 | HprofStringId id = next_string_id_++; |
| 629 | strings_.Put(string, id); |
| 630 | return id; |
| 631 | } |
| 632 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 633 | HprofStringId LookupClassNameId(mirror::Class* c) SHARED_REQUIRES(Locks::mutator_lock_) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 634 | return LookupStringId(PrettyDescriptor(c)); |
| 635 | } |
| 636 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 637 | void WriteFixedHeader() { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 638 | // Write the file header. |
| 639 | // U1: NUL-terminated magic string. |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 640 | const char magic[] = "JAVA PROFILE 1.0.3"; |
| 641 | __ AddU1List(reinterpret_cast<const uint8_t*>(magic), sizeof(magic)); |
| 642 | |
Calin Juravle | 3280517 | 2014-07-04 16:24:03 +0100 | [diff] [blame] | 643 | // U4: size of identifiers. We're using addresses as IDs and our heap references are stored |
| 644 | // as uint32_t. |
| 645 | // Note of warning: hprof-conv hard-codes the size of identifiers to 4. |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 646 | static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(uint32_t), |
| 647 | "Unexpected HeapReference size"); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 648 | __ AddU4(sizeof(uint32_t)); |
| 649 | |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 650 | // The current time, in milliseconds since 0:00 GMT, 1/1/70. |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 651 | timeval now; |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 652 | const uint64_t nowMs = (gettimeofday(&now, nullptr) < 0) ? 0 : |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 653 | (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000; |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 654 | // TODO: It seems it would be correct to use U8. |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 655 | // U4: high word of the 64-bit time. |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 656 | __ AddU4(static_cast<uint32_t>(nowMs >> 32)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 657 | // U4: low word of the 64-bit time. |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 658 | __ AddU4(static_cast<uint32_t>(nowMs & 0xFFFFFFFF)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 659 | } |
| 660 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 661 | void WriteStackTraces() SHARED_REQUIRES(Locks::mutator_lock_) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 662 | // Write a dummy stack trace record so the analysis tools don't freak out. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 663 | output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 664 | __ AddStackTraceSerialNumber(kHprofNullStackTrace); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 665 | __ AddU4(kHprofNullThread); |
| 666 | __ AddU4(0); // no frames |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 667 | |
| 668 | // TODO: jhat complains "WARNING: Stack trace not found for serial # -1", but no trace should |
| 669 | // have -1 as its serial number (as long as HprofStackTraceSerialNumber doesn't overflow). |
| 670 | for (const auto& it : traces_) { |
| 671 | const gc::AllocRecordStackTrace* trace = it.first; |
| 672 | HprofStackTraceSerialNumber trace_sn = it.second; |
| 673 | size_t depth = trace->GetDepth(); |
| 674 | |
| 675 | // First write stack frames of the trace |
| 676 | for (size_t i = 0; i < depth; ++i) { |
| 677 | const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i); |
| 678 | ArtMethod* method = frame->GetMethod(); |
| 679 | CHECK(method != nullptr); |
| 680 | output_->StartNewRecord(HPROF_TAG_STACK_FRAME, kHprofTime); |
| 681 | // STACK FRAME format: |
| 682 | // ID: stack frame ID. We use the address of the AllocRecordStackTraceElement object as its ID. |
| 683 | // ID: method name string ID |
| 684 | // ID: method signature string ID |
| 685 | // ID: source file name string ID |
| 686 | // U4: class serial number |
| 687 | // U4: >0, line number; 0, no line information available; -1, unknown location |
| 688 | auto frame_result = frames_.find(frame); |
| 689 | CHECK(frame_result != frames_.end()); |
| 690 | __ AddU4(frame_result->second); |
| 691 | __ AddStringId(LookupStringId(method->GetName())); |
| 692 | __ AddStringId(LookupStringId(method->GetSignature().ToString())); |
| 693 | const char* source_file = method->GetDeclaringClassSourceFile(); |
| 694 | if (source_file == nullptr) { |
| 695 | source_file = ""; |
| 696 | } |
| 697 | __ AddStringId(LookupStringId(source_file)); |
| 698 | auto class_result = classes_.find(method->GetDeclaringClass()); |
| 699 | CHECK(class_result != classes_.end()); |
| 700 | __ AddU4(class_result->second); |
| 701 | __ AddU4(frame->ComputeLineNumber()); |
| 702 | } |
| 703 | |
| 704 | // Then write the trace itself |
| 705 | output_->StartNewRecord(HPROF_TAG_STACK_TRACE, kHprofTime); |
| 706 | // STACK TRACE format: |
| 707 | // U4: stack trace serial number. We use the address of the AllocRecordStackTrace object as its serial number. |
| 708 | // U4: thread serial number. We use Thread::GetTid(). |
| 709 | // U4: number of frames |
| 710 | // [ID]*: series of stack frame ID's |
| 711 | __ AddStackTraceSerialNumber(trace_sn); |
| 712 | __ AddU4(trace->GetTid()); |
| 713 | __ AddU4(depth); |
| 714 | for (size_t i = 0; i < depth; ++i) { |
| 715 | const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i); |
| 716 | auto frame_result = frames_.find(frame); |
| 717 | CHECK(frame_result != frames_.end()); |
| 718 | __ AddU4(frame_result->second); |
| 719 | } |
| 720 | } |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | bool DumpToDdmsBuffered(size_t overall_size ATTRIBUTE_UNUSED, size_t max_length ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 724 | REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 725 | LOG(FATAL) << "Unimplemented"; |
| 726 | UNREACHABLE(); |
| 727 | // // Send the data off to DDMS. |
| 728 | // iovec iov[2]; |
| 729 | // iov[0].iov_base = header_data_ptr_; |
| 730 | // iov[0].iov_len = header_data_size_; |
| 731 | // iov[1].iov_base = body_data_ptr_; |
| 732 | // iov[1].iov_len = body_data_size_; |
| 733 | // Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2); |
| 734 | } |
| 735 | |
| 736 | bool DumpToFile(size_t overall_size, size_t max_length) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 737 | REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 738 | // Where exactly are we writing to? |
| 739 | int out_fd; |
| 740 | if (fd_ >= 0) { |
| 741 | out_fd = dup(fd_); |
| 742 | if (out_fd < 0) { |
| 743 | ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno)); |
| 744 | return false; |
| 745 | } |
| 746 | } else { |
| 747 | out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 748 | if (out_fd < 0) { |
| 749 | ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(), |
| 750 | strerror(errno)); |
| 751 | return false; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | std::unique_ptr<File> file(new File(out_fd, filename_, true)); |
| 756 | bool okay; |
| 757 | { |
| 758 | FileEndianOutput file_output(file.get(), max_length); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 759 | output_ = &file_output; |
| 760 | ProcessHeap(true); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 761 | okay = !file_output.Errors(); |
| 762 | |
| 763 | if (okay) { |
Andreas Gampe | c515f21 | 2015-08-28 18:15:27 -0700 | [diff] [blame] | 764 | // Check for expected size. Output is expected to be less-or-equal than first phase, see |
| 765 | // b/23521263. |
| 766 | DCHECK_LE(file_output.SumLength(), overall_size); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 767 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 768 | output_ = nullptr; |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | if (okay) { |
| 772 | okay = file->FlushCloseOrErase() == 0; |
| 773 | } else { |
| 774 | file->Erase(); |
| 775 | } |
| 776 | if (!okay) { |
| 777 | std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s", |
| 778 | filename_.c_str(), strerror(errno))); |
| 779 | ThrowRuntimeException("%s", msg.c_str()); |
| 780 | LOG(ERROR) << msg; |
| 781 | } |
| 782 | |
| 783 | return okay; |
| 784 | } |
| 785 | |
| 786 | bool DumpToDdmsDirect(size_t overall_size, size_t max_length, uint32_t chunk_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 787 | REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 788 | CHECK(direct_to_ddms_); |
| 789 | JDWP::JdwpState* state = Dbg::GetJdwpState(); |
| 790 | CHECK(state != nullptr); |
| 791 | JDWP::JdwpNetStateBase* net_state = state->netState; |
| 792 | CHECK(net_state != nullptr); |
| 793 | |
| 794 | // Hold the socket lock for the whole time since we want this to be atomic. |
| 795 | MutexLock mu(Thread::Current(), *net_state->GetSocketLock()); |
| 796 | |
| 797 | // Prepare the Ddms chunk. |
| 798 | constexpr size_t kChunkHeaderSize = kJDWPHeaderLen + 8; |
| 799 | uint8_t chunk_header[kChunkHeaderSize] = { 0 }; |
| 800 | state->SetupChunkHeader(chunk_type, overall_size, kChunkHeaderSize, chunk_header); |
| 801 | |
| 802 | // Prepare the output and send the chunk header. |
| 803 | NetStateEndianOutput net_output(net_state, max_length); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 804 | output_ = &net_output; |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 805 | net_output.AddU1List(chunk_header, kChunkHeaderSize); |
| 806 | |
| 807 | // Write the dump. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 808 | ProcessHeap(true); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 809 | |
Andreas Gampe | c515f21 | 2015-08-28 18:15:27 -0700 | [diff] [blame] | 810 | // Check for expected size. See DumpToFile for comment. |
| 811 | DCHECK_LE(net_output.SumLength(), overall_size + kChunkHeaderSize); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 812 | output_ = nullptr; |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 813 | |
| 814 | return true; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 815 | } |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 816 | |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 817 | void PopulateAllocationTrackingTraces() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 818 | REQUIRES(Locks::mutator_lock_, Locks::alloc_tracker_lock_) { |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 819 | gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords(); |
| 820 | CHECK(records != nullptr); |
| 821 | HprofStackTraceSerialNumber next_trace_sn = kHprofNullStackTrace + 1; |
| 822 | HprofStackFrameId next_frame_id = 0; |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 823 | size_t count = 0; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 824 | |
| 825 | for (auto it = records->Begin(), end = records->End(); it != end; ++it) { |
| 826 | const mirror::Object* obj = it->first.Read(); |
Man Cao | 42c3c33 | 2015-06-23 16:38:25 -0700 | [diff] [blame] | 827 | if (obj == nullptr) { |
| 828 | continue; |
| 829 | } |
| 830 | ++count; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 831 | const gc::AllocRecordStackTrace* trace = it->second->GetStackTrace(); |
| 832 | |
| 833 | // Copy the pair into a real hash map to speed up look up. |
| 834 | auto records_result = allocation_records_.emplace(obj, trace); |
| 835 | // The insertion should always succeed, i.e. no duplicate object pointers in "records" |
| 836 | CHECK(records_result.second); |
| 837 | |
| 838 | // Generate serial numbers for traces, and IDs for frames. |
| 839 | auto traces_result = traces_.find(trace); |
| 840 | if (traces_result == traces_.end()) { |
| 841 | traces_.emplace(trace, next_trace_sn++); |
| 842 | // only check frames if the trace is newly discovered |
| 843 | for (size_t i = 0, depth = trace->GetDepth(); i < depth; ++i) { |
| 844 | const gc::AllocRecordStackTraceElement* frame = &trace->GetStackElement(i); |
| 845 | auto frames_result = frames_.find(frame); |
| 846 | if (frames_result == frames_.end()) { |
| 847 | frames_.emplace(frame, next_frame_id++); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | CHECK_EQ(traces_.size(), next_trace_sn - kHprofNullStackTrace - 1); |
| 853 | CHECK_EQ(frames_.size(), next_frame_id); |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 854 | total_objects_with_stack_trace_ = count; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 857 | // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored. |
| 858 | // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will |
| 859 | // only be used for debug messages. |
| 860 | std::string filename_; |
| 861 | int fd_; |
| 862 | bool direct_to_ddms_; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 863 | |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 864 | uint64_t start_ns_ = NanoTime(); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 865 | |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 866 | EndianOutput* output_ = nullptr; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 867 | |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 868 | HprofHeapId current_heap_ = HPROF_HEAP_DEFAULT; // Which heap we're currently dumping. |
| 869 | size_t objects_in_segment_ = 0; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 870 | |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 871 | size_t total_objects_ = 0u; |
| 872 | size_t total_objects_with_stack_trace_ = 0u; |
| 873 | |
| 874 | HprofStringId next_string_id_ = 0x400000; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 875 | SafeMap<std::string, HprofStringId> strings_; |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 876 | HprofClassSerialNumber next_class_serial_number_ = 1; |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 877 | SafeMap<mirror::Class*, HprofClassSerialNumber> classes_; |
| 878 | |
| 879 | std::unordered_map<const gc::AllocRecordStackTrace*, HprofStackTraceSerialNumber, |
| 880 | gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTrace>, |
| 881 | gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTrace>> traces_; |
| 882 | std::unordered_map<const gc::AllocRecordStackTraceElement*, HprofStackFrameId, |
| 883 | gc::HashAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>, |
| 884 | gc::EqAllocRecordTypesPtr<gc::AllocRecordStackTraceElement>> frames_; |
| 885 | std::unordered_map<const mirror::Object*, const gc::AllocRecordStackTrace*> allocation_records_; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 886 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 887 | friend class GcRootVisitor; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 888 | DISALLOW_COPY_AND_ASSIGN(Hprof); |
| 889 | }; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 890 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 891 | static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* size_out) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 892 | char c = sig[0]; |
| 893 | HprofBasicType ret; |
| 894 | size_t size; |
| 895 | |
| 896 | switch (c) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 897 | case '[': |
| 898 | case 'L': |
| 899 | ret = hprof_basic_object; |
| 900 | size = 4; |
| 901 | break; |
| 902 | case 'Z': |
| 903 | ret = hprof_basic_boolean; |
| 904 | size = 1; |
| 905 | break; |
| 906 | case 'C': |
| 907 | ret = hprof_basic_char; |
| 908 | size = 2; |
| 909 | break; |
| 910 | case 'F': |
| 911 | ret = hprof_basic_float; |
| 912 | size = 4; |
| 913 | break; |
| 914 | case 'D': |
| 915 | ret = hprof_basic_double; |
| 916 | size = 8; |
| 917 | break; |
| 918 | case 'B': |
| 919 | ret = hprof_basic_byte; |
| 920 | size = 1; |
| 921 | break; |
| 922 | case 'S': |
| 923 | ret = hprof_basic_short; |
| 924 | size = 2; |
| 925 | break; |
| 926 | case 'I': |
| 927 | ret = hprof_basic_int; |
| 928 | size = 4; |
| 929 | break; |
| 930 | case 'J': |
| 931 | ret = hprof_basic_long; |
| 932 | size = 8; |
| 933 | break; |
| 934 | default: |
| 935 | LOG(FATAL) << "UNREACHABLE"; |
| 936 | UNREACHABLE(); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 937 | } |
| 938 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 939 | if (size_out != nullptr) { |
| 940 | *size_out = size; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | // Always called when marking objects, but only does |
| 947 | // something when ctx->gc_scan_state_ is non-zero, which is usually |
| 948 | // only true when marking the root set or unreachable |
| 949 | // objects. Used to add rootset references to obj. |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 950 | void Hprof::MarkRootObject(const mirror::Object* obj, jobject jni_obj, HprofHeapTag heap_tag, |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 951 | uint32_t thread_serial) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 952 | if (heap_tag == 0) { |
| 953 | return; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 954 | } |
| 955 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 956 | CheckHeapSegmentConstraints(); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 957 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 958 | switch (heap_tag) { |
| 959 | // ID: object ID |
| 960 | case HPROF_ROOT_UNKNOWN: |
| 961 | case HPROF_ROOT_STICKY_CLASS: |
| 962 | case HPROF_ROOT_MONITOR_USED: |
| 963 | case HPROF_ROOT_INTERNED_STRING: |
| 964 | case HPROF_ROOT_DEBUGGER: |
| 965 | case HPROF_ROOT_VM_INTERNAL: |
| 966 | __ AddU1(heap_tag); |
| 967 | __ AddObjectId(obj); |
| 968 | break; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 969 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 970 | // ID: object ID |
| 971 | // ID: JNI global ref ID |
| 972 | case HPROF_ROOT_JNI_GLOBAL: |
| 973 | __ AddU1(heap_tag); |
| 974 | __ AddObjectId(obj); |
| 975 | __ AddJniGlobalRefId(jni_obj); |
| 976 | break; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 977 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 978 | // ID: object ID |
| 979 | // U4: thread serial number |
| 980 | // U4: frame number in stack trace (-1 for empty) |
| 981 | case HPROF_ROOT_JNI_LOCAL: |
| 982 | case HPROF_ROOT_JNI_MONITOR: |
| 983 | case HPROF_ROOT_JAVA_FRAME: |
| 984 | __ AddU1(heap_tag); |
| 985 | __ AddObjectId(obj); |
| 986 | __ AddU4(thread_serial); |
| 987 | __ AddU4((uint32_t)-1); |
| 988 | break; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 989 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 990 | // ID: object ID |
| 991 | // U4: thread serial number |
| 992 | case HPROF_ROOT_NATIVE_STACK: |
| 993 | case HPROF_ROOT_THREAD_BLOCK: |
| 994 | __ AddU1(heap_tag); |
| 995 | __ AddObjectId(obj); |
| 996 | __ AddU4(thread_serial); |
| 997 | break; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 998 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 999 | // ID: thread object ID |
| 1000 | // U4: thread serial number |
| 1001 | // U4: stack trace serial number |
| 1002 | case HPROF_ROOT_THREAD_OBJECT: |
| 1003 | __ AddU1(heap_tag); |
| 1004 | __ AddObjectId(obj); |
| 1005 | __ AddU4(thread_serial); |
| 1006 | __ AddU4((uint32_t)-1); // xxx |
| 1007 | break; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1008 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1009 | case HPROF_CLASS_DUMP: |
| 1010 | case HPROF_INSTANCE_DUMP: |
| 1011 | case HPROF_OBJECT_ARRAY_DUMP: |
| 1012 | case HPROF_PRIMITIVE_ARRAY_DUMP: |
| 1013 | case HPROF_HEAP_DUMP_INFO: |
| 1014 | case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP: |
| 1015 | // Ignored. |
| 1016 | break; |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 1017 | |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1018 | case HPROF_ROOT_FINALIZING: |
| 1019 | case HPROF_ROOT_REFERENCE_CLEANUP: |
| 1020 | case HPROF_UNREACHABLE: |
| 1021 | LOG(FATAL) << "obsolete tag " << static_cast<int>(heap_tag); |
| 1022 | break; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1023 | } |
| 1024 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1025 | ++objects_in_segment_; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 1028 | // Use for visiting the GcRoots held live by ArtFields, ArtMethods, and ClassLoaders. |
| 1029 | class GcRootVisitor { |
| 1030 | public: |
| 1031 | explicit GcRootVisitor(Hprof* hprof) : hprof_(hprof) {} |
| 1032 | |
| 1033 | void operator()(mirror::Object* obj ATTRIBUTE_UNUSED, |
| 1034 | MemberOffset offset ATTRIBUTE_UNUSED, |
| 1035 | bool is_static ATTRIBUTE_UNUSED) const {} |
| 1036 | |
| 1037 | // Note that these don't have read barriers. Its OK however since the GC is guaranteed to not be |
| 1038 | // running during the hprof dumping process. |
| 1039 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 1040 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1041 | if (!root->IsNull()) { |
| 1042 | VisitRoot(root); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 1047 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1048 | mirror::Object* obj = root->AsMirrorPtr(); |
| 1049 | // The two cases are either classes or dex cache arrays. If it is a dex cache array, then use |
| 1050 | // VM internal. Otherwise the object is a declaring class of an ArtField or ArtMethod or a |
| 1051 | // class from a ClassLoader. |
| 1052 | hprof_->VisitRoot(obj, RootInfo(obj->IsClass() ? kRootStickyClass : kRootVMInternal)); |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | private: |
| 1057 | Hprof* const hprof_; |
| 1058 | }; |
| 1059 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1060 | void Hprof::DumpHeapObject(mirror::Object* obj) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1061 | // Ignore classes that are retired. |
| 1062 | if (obj->IsClass() && obj->AsClass()->IsRetired()) { |
| 1063 | return; |
| 1064 | } |
| 1065 | |
Mathieu Chartier | c9cd7ac | 2016-01-20 14:48:36 -0800 | [diff] [blame^] | 1066 | ++total_objects_; |
| 1067 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 1068 | GcRootVisitor visitor(this); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1069 | obj->VisitReferences(visitor, VoidFunctor()); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 1070 | |
Mathieu Chartier | e715811 | 2015-06-03 13:32:15 -0700 | [diff] [blame] | 1071 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1072 | const gc::space::ContinuousSpace* const space = heap->FindContinuousSpaceFromObject(obj, true); |
Mathieu Chartier | ae1ad00 | 2014-07-18 17:58:22 -0700 | [diff] [blame] | 1073 | HprofHeapId heap_type = HPROF_HEAP_APP; |
| 1074 | if (space != nullptr) { |
| 1075 | if (space->IsZygoteSpace()) { |
| 1076 | heap_type = HPROF_HEAP_ZYGOTE; |
| 1077 | } else if (space->IsImageSpace()) { |
| 1078 | heap_type = HPROF_HEAP_IMAGE; |
| 1079 | } |
Mathieu Chartier | e715811 | 2015-06-03 13:32:15 -0700 | [diff] [blame] | 1080 | } else { |
| 1081 | const auto* los = heap->GetLargeObjectsSpace(); |
| 1082 | if (los->Contains(obj) && los->IsZygoteLargeObject(Thread::Current(), obj)) { |
| 1083 | heap_type = HPROF_HEAP_ZYGOTE; |
| 1084 | } |
Mathieu Chartier | ae1ad00 | 2014-07-18 17:58:22 -0700 | [diff] [blame] | 1085 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1086 | CheckHeapSegmentConstraints(); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1087 | |
Mathieu Chartier | ae1ad00 | 2014-07-18 17:58:22 -0700 | [diff] [blame] | 1088 | if (heap_type != current_heap_) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1089 | HprofStringId nameId; |
| 1090 | |
| 1091 | // This object is in a different heap than the current one. |
| 1092 | // Emit a HEAP_DUMP_INFO tag to change heaps. |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1093 | __ AddU1(HPROF_HEAP_DUMP_INFO); |
| 1094 | __ AddU4(static_cast<uint32_t>(heap_type)); // uint32_t: heap type |
Mathieu Chartier | ae1ad00 | 2014-07-18 17:58:22 -0700 | [diff] [blame] | 1095 | switch (heap_type) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1096 | case HPROF_HEAP_APP: |
| 1097 | nameId = LookupStringId("app"); |
| 1098 | break; |
| 1099 | case HPROF_HEAP_ZYGOTE: |
| 1100 | nameId = LookupStringId("zygote"); |
| 1101 | break; |
Mathieu Chartier | ae1ad00 | 2014-07-18 17:58:22 -0700 | [diff] [blame] | 1102 | case HPROF_HEAP_IMAGE: |
| 1103 | nameId = LookupStringId("image"); |
| 1104 | break; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1105 | default: |
| 1106 | // Internal error |
| 1107 | LOG(ERROR) << "Unexpected desiredHeap"; |
| 1108 | nameId = LookupStringId("<ILLEGAL>"); |
| 1109 | break; |
| 1110 | } |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1111 | __ AddStringId(nameId); |
Mathieu Chartier | ae1ad00 | 2014-07-18 17:58:22 -0700 | [diff] [blame] | 1112 | current_heap_ = heap_type; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1113 | } |
| 1114 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1115 | mirror::Class* c = obj->GetClass(); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1116 | if (c == nullptr) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1117 | // This object will bother HprofReader, because it has a null |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1118 | // class, so just don't dump it. It could be |
| 1119 | // gDvm.unlinkedJavaLangClass or it could be an object just |
| 1120 | // allocated which hasn't been initialized yet. |
| 1121 | } else { |
| 1122 | if (obj->IsClass()) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1123 | DumpHeapClass(obj->AsClass()); |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 1124 | } else if (c->IsArrayClass()) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1125 | DumpHeapArray(obj->AsArray(), c); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1126 | } else { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1127 | DumpHeapInstanceObject(obj, c); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1131 | ++objects_in_segment_; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1132 | } |
| 1133 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1134 | void Hprof::DumpHeapClass(mirror::Class* klass) { |
Sebastien Hertz | ca068b2 | 2015-04-07 10:28:53 +0200 | [diff] [blame] | 1135 | if (!klass->IsLoaded() && !klass->IsErroneous()) { |
| 1136 | // Class is allocated but not yet loaded: we cannot access its fields or super class. |
| 1137 | return; |
| 1138 | } |
Mathieu Chartier | a6d3a7e | 2015-06-03 16:51:09 -0700 | [diff] [blame] | 1139 | const size_t num_static_fields = klass->NumStaticFields(); |
| 1140 | // Total class size including embedded IMT, embedded vtable, and static fields. |
| 1141 | const size_t class_size = klass->GetClassSize(); |
| 1142 | // Class size excluding static fields (relies on reference fields being the first static fields). |
| 1143 | const size_t class_size_without_overhead = sizeof(mirror::Class); |
| 1144 | CHECK_LE(class_size_without_overhead, class_size); |
| 1145 | const size_t overhead_size = class_size - class_size_without_overhead; |
| 1146 | |
| 1147 | if (overhead_size != 0) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1148 | // Create a byte array to reflect the allocation of the |
| 1149 | // StaticField array at the end of this class. |
| 1150 | __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP); |
| 1151 | __ AddClassStaticsId(klass); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1152 | __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass)); |
Mathieu Chartier | a6d3a7e | 2015-06-03 16:51:09 -0700 | [diff] [blame] | 1153 | __ AddU4(overhead_size); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1154 | __ AddU1(hprof_basic_byte); |
Mathieu Chartier | a6d3a7e | 2015-06-03 16:51:09 -0700 | [diff] [blame] | 1155 | for (size_t i = 0; i < overhead_size; ++i) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1156 | __ AddU1(0); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | __ AddU1(HPROF_CLASS_DUMP); |
| 1161 | __ AddClassId(LookupClassId(klass)); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1162 | __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(klass)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1163 | __ AddClassId(LookupClassId(klass->GetSuperClass())); |
| 1164 | __ AddObjectId(klass->GetClassLoader()); |
| 1165 | __ AddObjectId(nullptr); // no signer |
| 1166 | __ AddObjectId(nullptr); // no prot domain |
| 1167 | __ AddObjectId(nullptr); // reserved |
| 1168 | __ AddObjectId(nullptr); // reserved |
| 1169 | if (klass->IsClassClass()) { |
| 1170 | // ClassObjects have their static fields appended, so aren't all the same size. |
| 1171 | // But they're at least this size. |
Mathieu Chartier | a6d3a7e | 2015-06-03 16:51:09 -0700 | [diff] [blame] | 1172 | __ AddU4(class_size_without_overhead); // instance size |
Jeff Hao | 0ce4353 | 2015-05-12 18:58:32 -0700 | [diff] [blame] | 1173 | } else if (klass->IsStringClass()) { |
| 1174 | // Strings are variable length with character data at the end like arrays. |
| 1175 | // This outputs the size of an empty string. |
| 1176 | __ AddU4(sizeof(mirror::String)); |
| 1177 | } else if (klass->IsArrayClass() || klass->IsPrimitive()) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1178 | __ AddU4(0); |
| 1179 | } else { |
| 1180 | __ AddU4(klass->GetObjectSize()); // instance size |
| 1181 | } |
| 1182 | |
| 1183 | __ AddU2(0); // empty const pool |
| 1184 | |
| 1185 | // Static fields |
Mathieu Chartier | a6d3a7e | 2015-06-03 16:51:09 -0700 | [diff] [blame] | 1186 | if (overhead_size == 0) { |
| 1187 | __ AddU2(static_cast<uint16_t>(0)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1188 | } else { |
Mathieu Chartier | a6d3a7e | 2015-06-03 16:51:09 -0700 | [diff] [blame] | 1189 | __ AddU2(static_cast<uint16_t>(num_static_fields + 1)); |
| 1190 | __ AddStringId(LookupStringId(kClassOverheadName)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1191 | __ AddU1(hprof_basic_object); |
| 1192 | __ AddClassStaticsId(klass); |
| 1193 | |
Mathieu Chartier | a6d3a7e | 2015-06-03 16:51:09 -0700 | [diff] [blame] | 1194 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1195 | ArtField* f = klass->GetStaticField(i); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1196 | |
| 1197 | size_t size; |
| 1198 | HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size); |
| 1199 | __ AddStringId(LookupStringId(f->GetName())); |
| 1200 | __ AddU1(t); |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1201 | switch (t) { |
| 1202 | case hprof_basic_byte: |
Mathieu Chartier | ff38c04 | 2015-03-06 11:33:36 -0800 | [diff] [blame] | 1203 | __ AddU1(f->GetByte(klass)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1204 | break; |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1205 | case hprof_basic_boolean: |
| 1206 | __ AddU1(f->GetBoolean(klass)); |
| 1207 | break; |
| 1208 | case hprof_basic_char: |
Mathieu Chartier | ff38c04 | 2015-03-06 11:33:36 -0800 | [diff] [blame] | 1209 | __ AddU2(f->GetChar(klass)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1210 | break; |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1211 | case hprof_basic_short: |
| 1212 | __ AddU2(f->GetShort(klass)); |
| 1213 | break; |
| 1214 | case hprof_basic_float: |
| 1215 | case hprof_basic_int: |
| 1216 | case hprof_basic_object: |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1217 | __ AddU4(f->Get32(klass)); |
| 1218 | break; |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1219 | case hprof_basic_double: |
| 1220 | case hprof_basic_long: |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1221 | __ AddU8(f->Get64(klass)); |
| 1222 | break; |
| 1223 | default: |
| 1224 | LOG(FATAL) << "Unexpected size " << size; |
| 1225 | UNREACHABLE(); |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | // Instance fields for this class (no superclass fields) |
Mathieu Chartier | 7c1f53e | 2015-06-03 10:51:13 -0700 | [diff] [blame] | 1231 | int iFieldCount = klass->NumInstanceFields(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1232 | if (klass->IsStringClass()) { |
| 1233 | __ AddU2((uint16_t)iFieldCount + 1); |
| 1234 | } else { |
| 1235 | __ AddU2((uint16_t)iFieldCount); |
| 1236 | } |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1237 | for (int i = 0; i < iFieldCount; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1238 | ArtField* f = klass->GetInstanceField(i); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1239 | __ AddStringId(LookupStringId(f->GetName())); |
| 1240 | HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), nullptr); |
| 1241 | __ AddU1(t); |
| 1242 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1243 | // Add native value character array for strings. |
| 1244 | if (klass->IsStringClass()) { |
| 1245 | __ AddStringId(LookupStringId("value")); |
| 1246 | __ AddU1(hprof_basic_object); |
| 1247 | } |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1250 | void Hprof::DumpHeapArray(mirror::Array* obj, mirror::Class* klass) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1251 | uint32_t length = obj->GetLength(); |
| 1252 | |
| 1253 | if (obj->IsObjectArray()) { |
| 1254 | // obj is an object array. |
| 1255 | __ AddU1(HPROF_OBJECT_ARRAY_DUMP); |
| 1256 | |
| 1257 | __ AddObjectId(obj); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1258 | __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1259 | __ AddU4(length); |
| 1260 | __ AddClassId(LookupClassId(klass)); |
| 1261 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1262 | // Dump the elements, which are always objects or null. |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1263 | __ AddIdList(obj->AsObjectArray<mirror::Object>()); |
| 1264 | } else { |
| 1265 | size_t size; |
| 1266 | HprofBasicType t = SignatureToBasicTypeAndSize( |
| 1267 | Primitive::Descriptor(klass->GetComponentType()->GetPrimitiveType()), &size); |
| 1268 | |
| 1269 | // obj is a primitive array. |
| 1270 | __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP); |
| 1271 | |
| 1272 | __ AddObjectId(obj); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1273 | __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1274 | __ AddU4(length); |
| 1275 | __ AddU1(t); |
| 1276 | |
| 1277 | // Dump the raw, packed element values. |
| 1278 | if (size == 1) { |
| 1279 | __ AddU1List(reinterpret_cast<const uint8_t*>(obj->GetRawData(sizeof(uint8_t), 0)), length); |
| 1280 | } else if (size == 2) { |
| 1281 | __ AddU2List(reinterpret_cast<const uint16_t*>(obj->GetRawData(sizeof(uint16_t), 0)), length); |
| 1282 | } else if (size == 4) { |
| 1283 | __ AddU4List(reinterpret_cast<const uint32_t*>(obj->GetRawData(sizeof(uint32_t), 0)), length); |
| 1284 | } else if (size == 8) { |
| 1285 | __ AddU8List(reinterpret_cast<const uint64_t*>(obj->GetRawData(sizeof(uint64_t), 0)), length); |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1290 | void Hprof::DumpHeapInstanceObject(mirror::Object* obj, mirror::Class* klass) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1291 | // obj is an instance object. |
| 1292 | __ AddU1(HPROF_INSTANCE_DUMP); |
| 1293 | __ AddObjectId(obj); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1294 | __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1295 | __ AddClassId(LookupClassId(klass)); |
| 1296 | |
| 1297 | // Reserve some space for the length of the instance data, which we won't |
| 1298 | // know until we're done writing it. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1299 | size_t size_patch_offset = output_->Length(); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1300 | __ AddU4(0x77777777); |
| 1301 | |
Mathieu Chartier | 07d7eab | 2015-06-23 15:45:15 -0700 | [diff] [blame] | 1302 | // What we will use for the string value if the object is a string. |
| 1303 | mirror::Object* string_value = nullptr; |
| 1304 | |
| 1305 | // Write the instance data; fields for this class, followed by super class fields, and so on. |
Mathieu Chartier | 7c1f53e | 2015-06-03 10:51:13 -0700 | [diff] [blame] | 1306 | do { |
Mathieu Chartier | 07d7eab | 2015-06-23 15:45:15 -0700 | [diff] [blame] | 1307 | const size_t instance_fields = klass->NumInstanceFields(); |
| 1308 | for (size_t i = 0; i < instance_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1309 | ArtField* f = klass->GetInstanceField(i); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1310 | size_t size; |
Mathieu Chartier | 07d7eab | 2015-06-23 15:45:15 -0700 | [diff] [blame] | 1311 | HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size); |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1312 | switch (t) { |
| 1313 | case hprof_basic_byte: |
Mathieu Chartier | ff38c04 | 2015-03-06 11:33:36 -0800 | [diff] [blame] | 1314 | __ AddU1(f->GetByte(obj)); |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1315 | break; |
| 1316 | case hprof_basic_boolean: |
| 1317 | __ AddU1(f->GetBoolean(obj)); |
| 1318 | break; |
| 1319 | case hprof_basic_char: |
Mathieu Chartier | ff38c04 | 2015-03-06 11:33:36 -0800 | [diff] [blame] | 1320 | __ AddU2(f->GetChar(obj)); |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1321 | break; |
| 1322 | case hprof_basic_short: |
| 1323 | __ AddU2(f->GetShort(obj)); |
| 1324 | break; |
| 1325 | case hprof_basic_float: |
| 1326 | case hprof_basic_int: |
| 1327 | case hprof_basic_object: |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1328 | __ AddU4(f->Get32(obj)); |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1329 | break; |
| 1330 | case hprof_basic_double: |
| 1331 | case hprof_basic_long: |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1332 | __ AddU8(f->Get64(obj)); |
Mathieu Chartier | 15f345c | 2015-03-06 12:45:44 -0800 | [diff] [blame] | 1333 | break; |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1334 | } |
| 1335 | } |
Mathieu Chartier | 07d7eab | 2015-06-23 15:45:15 -0700 | [diff] [blame] | 1336 | // Add value field for String if necessary. |
| 1337 | if (klass->IsStringClass()) { |
| 1338 | mirror::String* s = obj->AsString(); |
| 1339 | if (s->GetLength() == 0) { |
| 1340 | // If string is empty, use an object-aligned address within the string for the value. |
| 1341 | string_value = reinterpret_cast<mirror::Object*>( |
| 1342 | reinterpret_cast<uintptr_t>(s) + kObjectAlignment); |
| 1343 | } else { |
| 1344 | string_value = reinterpret_cast<mirror::Object*>(s->GetValue()); |
| 1345 | } |
| 1346 | __ AddObjectId(string_value); |
| 1347 | } |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1348 | |
| 1349 | klass = klass->GetSuperClass(); |
Mathieu Chartier | 7c1f53e | 2015-06-03 10:51:13 -0700 | [diff] [blame] | 1350 | } while (klass != nullptr); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1351 | |
Mathieu Chartier | 07d7eab | 2015-06-23 15:45:15 -0700 | [diff] [blame] | 1352 | // Patch the instance field length. |
| 1353 | __ UpdateU4(size_patch_offset, output_->Length() - (size_patch_offset + 4)); |
| 1354 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1355 | // Output native value character array for strings. |
Mathieu Chartier | 07d7eab | 2015-06-23 15:45:15 -0700 | [diff] [blame] | 1356 | CHECK_EQ(obj->IsString(), string_value != nullptr); |
| 1357 | if (string_value != nullptr) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1358 | mirror::String* s = obj->AsString(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1359 | __ AddU1(HPROF_PRIMITIVE_ARRAY_DUMP); |
Mathieu Chartier | 07d7eab | 2015-06-23 15:45:15 -0700 | [diff] [blame] | 1360 | __ AddObjectId(string_value); |
Man Cao | 8c2ff64 | 2015-05-27 17:25:30 -0700 | [diff] [blame] | 1361 | __ AddStackTraceSerialNumber(LookupStackTraceSerialNumber(obj)); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1362 | __ AddU4(s->GetLength()); |
| 1363 | __ AddU1(hprof_basic_char); |
| 1364 | __ AddU2List(s->GetValue(), s->GetLength()); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1365 | } |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1368 | void Hprof::VisitRoot(mirror::Object* obj, const RootInfo& info) { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 1369 | static const HprofHeapTag xlate[] = { |
| 1370 | HPROF_ROOT_UNKNOWN, |
| 1371 | HPROF_ROOT_JNI_GLOBAL, |
| 1372 | HPROF_ROOT_JNI_LOCAL, |
| 1373 | HPROF_ROOT_JAVA_FRAME, |
| 1374 | HPROF_ROOT_NATIVE_STACK, |
| 1375 | HPROF_ROOT_STICKY_CLASS, |
| 1376 | HPROF_ROOT_THREAD_BLOCK, |
| 1377 | HPROF_ROOT_MONITOR_USED, |
| 1378 | HPROF_ROOT_THREAD_OBJECT, |
| 1379 | HPROF_ROOT_INTERNED_STRING, |
| 1380 | HPROF_ROOT_FINALIZING, |
| 1381 | HPROF_ROOT_DEBUGGER, |
| 1382 | HPROF_ROOT_REFERENCE_CLEANUP, |
| 1383 | HPROF_ROOT_VM_INTERNAL, |
| 1384 | HPROF_ROOT_JNI_MONITOR, |
| 1385 | }; |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 1386 | CHECK_LT(info.GetType(), sizeof(xlate) / sizeof(HprofHeapTag)); |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1387 | if (obj == nullptr) { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 1388 | return; |
| 1389 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1390 | MarkRootObject(obj, 0, xlate[info.GetType()], info.GetThreadId()); |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 1391 | } |
| 1392 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1393 | // If "direct_to_ddms" is true, the other arguments are ignored, and data is |
| 1394 | // sent directly to DDMS. |
| 1395 | // If "fd" is >= 0, the output will be written to that file descriptor. |
| 1396 | // Otherwise, "filename" is used to create an output file. |
| 1397 | void DumpHeap(const char* filename, int fd, bool direct_to_ddms) { |
Andreas Gampe | 3a91309 | 2015-01-10 00:26:17 -0800 | [diff] [blame] | 1398 | CHECK(filename != nullptr); |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 1399 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1400 | Thread* self = Thread::Current(); |
| 1401 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1402 | if (heap->IsGcConcurrentAndMoving()) { |
| 1403 | // Need to take a heap dump while GC isn't running. See the |
| 1404 | // comment in Heap::VisitObjects(). |
| 1405 | heap->IncrementDisableMovingGC(self); |
| 1406 | } |
Mathieu Chartier | 4f55e22 | 2015-09-04 13:26:21 -0700 | [diff] [blame] | 1407 | { |
| 1408 | ScopedSuspendAll ssa(__FUNCTION__, true /* long suspend */); |
| 1409 | Hprof hprof(filename, fd, direct_to_ddms); |
| 1410 | hprof.Dump(); |
| 1411 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1412 | if (heap->IsGcConcurrentAndMoving()) { |
| 1413 | heap->DecrementDisableMovingGC(self); |
| 1414 | } |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 1415 | } |
| 1416 | |
Mathieu Chartier | ad466ad | 2015-01-08 16:28:08 -0800 | [diff] [blame] | 1417 | } // namespace hprof |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 1418 | } // namespace art |