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