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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 40 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 41 | #include "base/stringprintf.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 42 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 43 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 44 | #include "common_throws.h" |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 45 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 46 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 47 | #include "gc/accounting/heap_bitmap.h" |
| 48 | #include "gc/heap.h" |
| 49 | #include "gc/space/space.h" |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 50 | #include "globals.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 51 | #include "mirror/art_field-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "mirror/class.h" |
| 53 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | #include "mirror/object-inl.h" |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 55 | #include "os.h" |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 56 | #include "safe_map.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 57 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 58 | #include "thread_list.h" |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 59 | |
| 60 | namespace art { |
| 61 | |
| 62 | namespace hprof { |
| 63 | |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 64 | #define UNIQUE_ERROR -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff)) |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 65 | |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 66 | #define HPROF_TIME 0 |
| 67 | #define HPROF_NULL_STACK_TRACE 0 |
| 68 | #define HPROF_NULL_THREAD 0 |
| 69 | |
| 70 | #define U2_TO_BUF_BE(buf, offset, value) \ |
| 71 | do { \ |
| 72 | unsigned char* buf_ = (unsigned char*)(buf); \ |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 73 | int offset_ = static_cast<int>(offset); \ |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 74 | uint16_t value_ = (uint16_t)(value); \ |
| 75 | buf_[offset_ + 0] = (unsigned char)(value_ >> 8); \ |
| 76 | buf_[offset_ + 1] = (unsigned char)(value_ ); \ |
| 77 | } while (0) |
| 78 | |
| 79 | #define U4_TO_BUF_BE(buf, offset, value) \ |
| 80 | do { \ |
| 81 | unsigned char* buf_ = (unsigned char*)(buf); \ |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 82 | int offset_ = static_cast<int>(offset); \ |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 83 | uint32_t value_ = (uint32_t)(value); \ |
| 84 | buf_[offset_ + 0] = (unsigned char)(value_ >> 24); \ |
| 85 | buf_[offset_ + 1] = (unsigned char)(value_ >> 16); \ |
| 86 | buf_[offset_ + 2] = (unsigned char)(value_ >> 8); \ |
| 87 | buf_[offset_ + 3] = (unsigned char)(value_ ); \ |
| 88 | } while (0) |
| 89 | |
| 90 | #define U8_TO_BUF_BE(buf, offset, value) \ |
| 91 | do { \ |
| 92 | unsigned char* buf_ = (unsigned char*)(buf); \ |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 93 | int offset_ = static_cast<int>(offset); \ |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 94 | uint64_t value_ = (uint64_t)(value); \ |
| 95 | buf_[offset_ + 0] = (unsigned char)(value_ >> 56); \ |
| 96 | buf_[offset_ + 1] = (unsigned char)(value_ >> 48); \ |
| 97 | buf_[offset_ + 2] = (unsigned char)(value_ >> 40); \ |
| 98 | buf_[offset_ + 3] = (unsigned char)(value_ >> 32); \ |
| 99 | buf_[offset_ + 4] = (unsigned char)(value_ >> 24); \ |
| 100 | buf_[offset_ + 5] = (unsigned char)(value_ >> 16); \ |
| 101 | buf_[offset_ + 6] = (unsigned char)(value_ >> 8); \ |
| 102 | buf_[offset_ + 7] = (unsigned char)(value_ ); \ |
| 103 | } while (0) |
| 104 | |
| 105 | enum HprofTag { |
| 106 | HPROF_TAG_STRING = 0x01, |
| 107 | HPROF_TAG_LOAD_CLASS = 0x02, |
| 108 | HPROF_TAG_UNLOAD_CLASS = 0x03, |
| 109 | HPROF_TAG_STACK_FRAME = 0x04, |
| 110 | HPROF_TAG_STACK_TRACE = 0x05, |
| 111 | HPROF_TAG_ALLOC_SITES = 0x06, |
| 112 | HPROF_TAG_HEAP_SUMMARY = 0x07, |
| 113 | HPROF_TAG_START_THREAD = 0x0A, |
| 114 | HPROF_TAG_END_THREAD = 0x0B, |
| 115 | HPROF_TAG_HEAP_DUMP = 0x0C, |
| 116 | HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C, |
| 117 | HPROF_TAG_HEAP_DUMP_END = 0x2C, |
| 118 | HPROF_TAG_CPU_SAMPLES = 0x0D, |
| 119 | HPROF_TAG_CONTROL_SETTINGS = 0x0E, |
| 120 | }; |
| 121 | |
| 122 | // Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records: |
| 123 | enum HprofHeapTag { |
| 124 | // Traditional. |
| 125 | HPROF_ROOT_UNKNOWN = 0xFF, |
| 126 | HPROF_ROOT_JNI_GLOBAL = 0x01, |
| 127 | HPROF_ROOT_JNI_LOCAL = 0x02, |
| 128 | HPROF_ROOT_JAVA_FRAME = 0x03, |
| 129 | HPROF_ROOT_NATIVE_STACK = 0x04, |
| 130 | HPROF_ROOT_STICKY_CLASS = 0x05, |
| 131 | HPROF_ROOT_THREAD_BLOCK = 0x06, |
| 132 | HPROF_ROOT_MONITOR_USED = 0x07, |
| 133 | HPROF_ROOT_THREAD_OBJECT = 0x08, |
| 134 | HPROF_CLASS_DUMP = 0x20, |
| 135 | HPROF_INSTANCE_DUMP = 0x21, |
| 136 | HPROF_OBJECT_ARRAY_DUMP = 0x22, |
| 137 | HPROF_PRIMITIVE_ARRAY_DUMP = 0x23, |
| 138 | |
| 139 | // Android. |
| 140 | HPROF_HEAP_DUMP_INFO = 0xfe, |
| 141 | HPROF_ROOT_INTERNED_STRING = 0x89, |
| 142 | HPROF_ROOT_FINALIZING = 0x8a, // Obsolete. |
| 143 | HPROF_ROOT_DEBUGGER = 0x8b, |
| 144 | HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete. |
| 145 | HPROF_ROOT_VM_INTERNAL = 0x8d, |
| 146 | HPROF_ROOT_JNI_MONITOR = 0x8e, |
| 147 | HPROF_UNREACHABLE = 0x90, // Obsolete. |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 148 | HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete. |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | enum HprofHeapId { |
| 152 | HPROF_HEAP_DEFAULT = 0, |
| 153 | HPROF_HEAP_ZYGOTE = 'Z', |
| 154 | HPROF_HEAP_APP = 'A' |
| 155 | }; |
| 156 | |
| 157 | enum HprofBasicType { |
| 158 | hprof_basic_object = 2, |
| 159 | hprof_basic_boolean = 4, |
| 160 | hprof_basic_char = 5, |
| 161 | hprof_basic_float = 6, |
| 162 | hprof_basic_double = 7, |
| 163 | hprof_basic_byte = 8, |
| 164 | hprof_basic_short = 9, |
| 165 | hprof_basic_int = 10, |
| 166 | hprof_basic_long = 11, |
| 167 | }; |
| 168 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 169 | typedef uint32_t HprofStringId; |
| 170 | typedef uint32_t HprofClassObjectId; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 171 | |
| 172 | // Represents a top-level hprof record, whose serialized format is: |
| 173 | // U1 TAG: denoting the type of the record |
| 174 | // U4 TIME: number of microseconds since the time stamp in the header |
| 175 | // U4 LENGTH: number of bytes that follow this uint32_t field and belong to this record |
| 176 | // U1* BODY: as many bytes as specified in the above uint32_t field |
| 177 | class HprofRecord { |
| 178 | public: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 179 | HprofRecord() : alloc_length_(128), fp_(nullptr), tag_(0), time_(0), length_(0), dirty_(false) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 180 | body_ = reinterpret_cast<unsigned char*>(malloc(alloc_length_)); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | ~HprofRecord() { |
| 184 | free(body_); |
| 185 | } |
| 186 | |
| 187 | int StartNewRecord(FILE* fp, uint8_t tag, uint32_t time) { |
| 188 | int rc = Flush(); |
| 189 | if (rc != 0) { |
| 190 | return rc; |
| 191 | } |
| 192 | |
| 193 | fp_ = fp; |
| 194 | tag_ = tag; |
| 195 | time_ = time; |
| 196 | length_ = 0; |
| 197 | dirty_ = true; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | int Flush() { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 202 | if (dirty_) { |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 203 | unsigned char headBuf[sizeof(uint8_t) + 2 * sizeof(uint32_t)]; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 204 | |
| 205 | headBuf[0] = tag_; |
| 206 | U4_TO_BUF_BE(headBuf, 1, time_); |
| 207 | U4_TO_BUF_BE(headBuf, 5, length_); |
| 208 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 209 | int nb = fwrite(headBuf, 1, sizeof(headBuf), fp_); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 210 | if (nb != sizeof(headBuf)) { |
| 211 | return UNIQUE_ERROR; |
| 212 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 213 | nb = fwrite(body_, 1, length_, fp_); |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 214 | if (nb != static_cast<int>(length_)) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 215 | return UNIQUE_ERROR; |
| 216 | } |
| 217 | |
| 218 | dirty_ = false; |
| 219 | } |
| 220 | // TODO if we used less than half (or whatever) of allocLen, shrink the buffer. |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | int AddU1(uint8_t value) { |
| 225 | int err = GuaranteeRecordAppend(1); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 226 | if (UNLIKELY(err != 0)) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 227 | return err; |
| 228 | } |
| 229 | |
| 230 | body_[length_++] = value; |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int AddU2(uint16_t value) { |
| 235 | return AddU2List(&value, 1); |
| 236 | } |
| 237 | |
| 238 | int AddU4(uint32_t value) { |
| 239 | return AddU4List(&value, 1); |
| 240 | } |
| 241 | |
| 242 | int AddU8(uint64_t value) { |
| 243 | return AddU8List(&value, 1); |
| 244 | } |
| 245 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 246 | int AddObjectId(const mirror::Object* value) { |
| 247 | return AddU4(PointerToLowMemUInt32(value)); |
| 248 | } |
| 249 | |
| 250 | // The ID for the synthetic object generated to account for class static overhead. |
| 251 | int AddClassStaticsId(const mirror::Class* value) { |
| 252 | return AddU4(1 | PointerToLowMemUInt32(value)); |
| 253 | } |
| 254 | |
| 255 | int AddJniGlobalRefId(jobject value) { |
| 256 | return AddU4(PointerToLowMemUInt32(value)); |
| 257 | } |
| 258 | |
| 259 | int AddClassId(HprofClassObjectId value) { |
| 260 | return AddU4(value); |
| 261 | } |
| 262 | |
| 263 | int AddStringId(HprofStringId value) { |
| 264 | return AddU4(value); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 267 | int AddU1List(const uint8_t* values, size_t numValues) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 268 | int err = GuaranteeRecordAppend(numValues); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 269 | if (UNLIKELY(err != 0)) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 270 | return err; |
| 271 | } |
| 272 | |
| 273 | memcpy(body_ + length_, values, numValues); |
| 274 | length_ += numValues; |
| 275 | return 0; |
| 276 | } |
| 277 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 278 | int AddU2List(const uint16_t* values, size_t numValues) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 279 | int err = GuaranteeRecordAppend(numValues * 2); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 280 | if (UNLIKELY(err != 0)) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 281 | return err; |
| 282 | } |
| 283 | |
| 284 | unsigned char* insert = body_ + length_; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 285 | for (size_t i = 0; i < numValues; ++i) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 286 | U2_TO_BUF_BE(insert, 0, *values++); |
| 287 | insert += sizeof(*values); |
| 288 | } |
| 289 | length_ += numValues * 2; |
| 290 | return 0; |
| 291 | } |
| 292 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 293 | int AddU4List(const uint32_t* values, size_t numValues) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 294 | int err = GuaranteeRecordAppend(numValues * 4); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 295 | if (UNLIKELY(err != 0)) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 296 | return err; |
| 297 | } |
| 298 | |
| 299 | unsigned char* insert = body_ + length_; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 300 | for (size_t i = 0; i < numValues; ++i) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 301 | U4_TO_BUF_BE(insert, 0, *values++); |
| 302 | insert += sizeof(*values); |
| 303 | } |
| 304 | length_ += numValues * 4; |
| 305 | return 0; |
| 306 | } |
| 307 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 308 | void UpdateU4(size_t offset, uint32_t new_value) { |
| 309 | U4_TO_BUF_BE(body_, offset, new_value); |
| 310 | } |
| 311 | |
| 312 | int AddU8List(const uint64_t* values, size_t numValues) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 313 | int err = GuaranteeRecordAppend(numValues * 8); |
| 314 | if (err != 0) { |
| 315 | return err; |
| 316 | } |
| 317 | |
| 318 | unsigned char* insert = body_ + length_; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 319 | for (size_t i = 0; i < numValues; ++i) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 320 | U8_TO_BUF_BE(insert, 0, *values++); |
| 321 | insert += sizeof(*values); |
| 322 | } |
| 323 | length_ += numValues * 8; |
| 324 | return 0; |
| 325 | } |
| 326 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 327 | int AddIdList(mirror::ObjectArray<mirror::Object>* values) |
| 328 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 329 | int32_t length = values->GetLength(); |
| 330 | for (int32_t i = 0; i < length; ++i) { |
| 331 | int err = AddObjectId(values->GetWithoutChecks(i)); |
| 332 | if (UNLIKELY(err != 0)) { |
| 333 | return err; |
| 334 | } |
| 335 | } |
| 336 | return 0; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | int AddUtf8String(const char* str) { |
| 340 | // The terminating NUL character is NOT written. |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 341 | return AddU1List((const uint8_t*)str, strlen(str)); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 344 | size_t Size() const { |
| 345 | return length_; |
| 346 | } |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 347 | |
| 348 | private: |
| 349 | int GuaranteeRecordAppend(size_t nmore) { |
| 350 | size_t minSize = length_ + nmore; |
| 351 | if (minSize > alloc_length_) { |
| 352 | size_t newAllocLen = alloc_length_ * 2; |
| 353 | if (newAllocLen < minSize) { |
| 354 | newAllocLen = alloc_length_ + nmore + nmore/2; |
| 355 | } |
| 356 | unsigned char* newBody = (unsigned char*)realloc(body_, newAllocLen); |
| 357 | if (newBody != NULL) { |
| 358 | body_ = newBody; |
| 359 | alloc_length_ = newAllocLen; |
| 360 | } else { |
| 361 | // TODO: set an error flag so future ops will fail |
| 362 | return UNIQUE_ERROR; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | CHECK_LE(length_ + nmore, alloc_length_); |
| 367 | return 0; |
| 368 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 369 | |
| 370 | size_t alloc_length_; |
| 371 | unsigned char* body_; |
| 372 | |
| 373 | FILE* fp_; |
| 374 | uint8_t tag_; |
| 375 | uint32_t time_; |
| 376 | size_t length_; |
| 377 | bool dirty_; |
| 378 | |
| 379 | DISALLOW_COPY_AND_ASSIGN(HprofRecord); |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | class Hprof { |
| 383 | public: |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 384 | Hprof(const char* output_filename, int fd, bool direct_to_ddms) |
| 385 | : filename_(output_filename), |
| 386 | fd_(fd), |
| 387 | direct_to_ddms_(direct_to_ddms), |
| 388 | start_ns_(NanoTime()), |
| 389 | current_record_(), |
| 390 | gc_thread_serial_number_(0), |
| 391 | gc_scan_state_(0), |
| 392 | current_heap_(HPROF_HEAP_DEFAULT), |
| 393 | objects_in_segment_(0), |
| 394 | header_fp_(NULL), |
| 395 | header_data_ptr_(NULL), |
| 396 | header_data_size_(0), |
| 397 | body_fp_(NULL), |
| 398 | body_data_ptr_(NULL), |
| 399 | body_data_size_(0), |
| 400 | next_string_id_(0x400000) { |
| 401 | LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting..."; |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 402 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 403 | header_fp_ = open_memstream(&header_data_ptr_, &header_data_size_); |
| 404 | if (header_fp_ == NULL) { |
| 405 | PLOG(FATAL) << "header open_memstream failed"; |
| 406 | } |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 407 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 408 | body_fp_ = open_memstream(&body_data_ptr_, &body_data_size_); |
| 409 | if (body_fp_ == NULL) { |
| 410 | PLOG(FATAL) << "body open_memstream failed"; |
| 411 | } |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 412 | } |
| 413 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 414 | ~Hprof() { |
| 415 | if (header_fp_ != NULL) { |
| 416 | fclose(header_fp_); |
| 417 | } |
| 418 | if (body_fp_ != NULL) { |
| 419 | fclose(body_fp_); |
| 420 | } |
| 421 | free(header_data_ptr_); |
| 422 | free(body_data_ptr_); |
| 423 | } |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 424 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 425 | void Dump() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 426 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 427 | LOCKS_EXCLUDED(Locks::heap_bitmap_lock_) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 428 | // Walk the roots and the heap. |
| 429 | current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 430 | Runtime::Current()->VisitRoots(RootVisitor, this); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 431 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 432 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 433 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 434 | Runtime::Current()->GetHeap()->VisitObjects(VisitObjectCallback, this); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 435 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 436 | current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_END, HPROF_TIME); |
| 437 | current_record_.Flush(); |
| 438 | fflush(body_fp_); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 439 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 440 | // Write the header. |
| 441 | WriteFixedHeader(); |
| 442 | // Write the string and class tables, and any stack traces, to the header. |
| 443 | // (jhat requires that these appear before any of the data in the body that refers to them.) |
| 444 | WriteStringTable(); |
| 445 | WriteClassTable(); |
| 446 | WriteStackTraces(); |
| 447 | current_record_.Flush(); |
| 448 | fflush(header_fp_); |
| 449 | |
| 450 | bool okay = true; |
| 451 | if (direct_to_ddms_) { |
| 452 | // Send the data off to DDMS. |
| 453 | iovec iov[2]; |
| 454 | iov[0].iov_base = header_data_ptr_; |
| 455 | iov[0].iov_len = header_data_size_; |
| 456 | iov[1].iov_base = body_data_ptr_; |
| 457 | iov[1].iov_len = body_data_size_; |
| 458 | Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2); |
| 459 | } else { |
| 460 | // Where exactly are we writing to? |
| 461 | int out_fd; |
| 462 | if (fd_ >= 0) { |
| 463 | out_fd = dup(fd_); |
| 464 | if (out_fd < 0) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 465 | ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno)); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | } else { |
| 469 | out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 470 | if (out_fd < 0) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 471 | ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(), |
| 472 | strerror(errno)); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | } |
| 476 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 477 | std::unique_ptr<File> file(new File(out_fd, filename_)); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 478 | okay = file->WriteFully(header_data_ptr_, header_data_size_) && |
| 479 | file->WriteFully(body_data_ptr_, body_data_size_); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 480 | if (!okay) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 481 | std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s", |
| 482 | filename_.c_str(), strerror(errno))); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 483 | ThrowRuntimeException("%s", msg.c_str()); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 484 | LOG(ERROR) << msg; |
| 485 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // Throw out a log message for the benefit of "runhat". |
| 489 | if (okay) { |
| 490 | uint64_t duration = NanoTime() - start_ns_; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 491 | LOG(INFO) << "hprof: heap dump completed (" |
| 492 | << PrettySize(header_data_size_ + body_data_size_ + 1023) |
| 493 | << ") in " << PrettyDuration(duration); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | private: |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 498 | static void RootVisitor(mirror::Object** obj, void* arg, uint32_t thread_id, RootType root_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 499 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 500 | DCHECK(arg != nullptr); |
| 501 | DCHECK(obj != nullptr); |
| 502 | DCHECK(*obj != nullptr); |
| 503 | reinterpret_cast<Hprof*>(arg)->VisitRoot(*obj, thread_id, root_type); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 506 | static void VisitObjectCallback(mirror::Object* obj, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 507 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 508 | DCHECK(obj != NULL); |
| 509 | DCHECK(arg != NULL); |
| 510 | reinterpret_cast<Hprof*>(arg)->DumpHeapObject(obj); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 513 | void VisitRoot(const mirror::Object* obj, uint32_t thread_id, RootType type) |
| 514 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 515 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 516 | int DumpHeapObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 517 | |
| 518 | void Finish() { |
| 519 | } |
| 520 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 521 | int WriteClassTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 522 | HprofRecord* rec = ¤t_record_; |
| 523 | uint32_t nextSerialNumber = 1; |
| 524 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 525 | for (mirror::Class* c : classes_) { |
| 526 | CHECK(c != nullptr); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 527 | |
| 528 | int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_LOAD_CLASS, HPROF_TIME); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 529 | if (UNLIKELY(err != 0)) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 530 | return err; |
| 531 | } |
| 532 | |
| 533 | // LOAD CLASS format: |
| 534 | // U4: class serial number (always > 0) |
| 535 | // ID: class object ID. We use the address of the class object structure as its ID. |
| 536 | // U4: stack trace serial number |
| 537 | // ID: class name string ID |
| 538 | rec->AddU4(nextSerialNumber++); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 539 | rec->AddObjectId(c); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 540 | rec->AddU4(HPROF_NULL_STACK_TRACE); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 541 | rec->AddStringId(LookupClassNameId(c)); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | int WriteStringTable() { |
| 548 | HprofRecord* rec = ¤t_record_; |
| 549 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 550 | for (std::pair<std::string, HprofStringId> p : strings_) { |
| 551 | const std::string& string = p.first; |
| 552 | size_t id = p.second; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 553 | |
| 554 | int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_STRING, HPROF_TIME); |
| 555 | if (err != 0) { |
| 556 | return err; |
| 557 | } |
| 558 | |
| 559 | // STRING format: |
| 560 | // ID: ID for this string |
| 561 | // U1*: UTF8 characters for string (NOT NULL terminated) |
| 562 | // (the record format encodes the length) |
| 563 | err = rec->AddU4(id); |
| 564 | if (err != 0) { |
| 565 | return err; |
| 566 | } |
| 567 | err = rec->AddUtf8String(string.c_str()); |
| 568 | if (err != 0) { |
| 569 | return err; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | void StartNewHeapDumpSegment() { |
| 577 | // This flushes the old segment and starts a new one. |
| 578 | current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME); |
| 579 | objects_in_segment_ = 0; |
| 580 | |
| 581 | // Starting a new HEAP_DUMP resets the heap to default. |
| 582 | current_heap_ = HPROF_HEAP_DEFAULT; |
| 583 | } |
| 584 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 585 | int MarkRootObject(const mirror::Object* obj, jobject jniObj); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 586 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 587 | HprofClassObjectId LookupClassId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 588 | if (c == nullptr) { |
| 589 | // c is the superclass of java.lang.Object or a primitive. |
| 590 | return 0; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 593 | { |
| 594 | auto result = classes_.insert(c); |
| 595 | const mirror::Class* present = *result.first; |
| 596 | CHECK_EQ(present, c); |
| 597 | } |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 598 | |
| 599 | // Make sure that we've assigned a string ID for this class' name |
| 600 | LookupClassNameId(c); |
| 601 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 602 | HprofClassObjectId result = PointerToLowMemUInt32(c); |
| 603 | return result; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 606 | HprofStringId LookupStringId(mirror::String* string) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 607 | return LookupStringId(string->ToModifiedUtf8()); |
| 608 | } |
| 609 | |
| 610 | HprofStringId LookupStringId(const char* string) { |
| 611 | return LookupStringId(std::string(string)); |
| 612 | } |
| 613 | |
| 614 | HprofStringId LookupStringId(const std::string& string) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 615 | auto it = strings_.find(string); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 616 | if (it != strings_.end()) { |
| 617 | return it->second; |
| 618 | } |
| 619 | HprofStringId id = next_string_id_++; |
| 620 | strings_.Put(string, id); |
| 621 | return id; |
| 622 | } |
| 623 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 624 | HprofStringId LookupClassNameId(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 625 | return LookupStringId(PrettyDescriptor(c)); |
| 626 | } |
| 627 | |
| 628 | void WriteFixedHeader() { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 629 | char magic[] = "JAVA PROFILE 1.0.3"; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 630 | unsigned char buf[4]; |
| 631 | |
| 632 | // Write the file header. |
| 633 | // U1: NUL-terminated magic string. |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 634 | fwrite(magic, 1, sizeof(magic), header_fp_); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 635 | |
Calin Juravle | 3280517 | 2014-07-04 16:24:03 +0100 | [diff] [blame] | 636 | // U4: size of identifiers. We're using addresses as IDs and our heap references are stored |
| 637 | // as uint32_t. |
| 638 | // Note of warning: hprof-conv hard-codes the size of identifiers to 4. |
| 639 | COMPILE_ASSERT(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(uint32_t), |
| 640 | UnexpectedHeapReferenceSize); |
| 641 | U4_TO_BUF_BE(buf, 0, sizeof(uint32_t)); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 642 | fwrite(buf, 1, sizeof(uint32_t), header_fp_); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 643 | |
| 644 | // The current time, in milliseconds since 0:00 GMT, 1/1/70. |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 645 | timeval now; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 646 | uint64_t nowMs; |
| 647 | if (gettimeofday(&now, NULL) < 0) { |
| 648 | nowMs = 0; |
| 649 | } else { |
| 650 | nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000; |
| 651 | } |
| 652 | |
| 653 | // U4: high word of the 64-bit time. |
| 654 | U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32)); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 655 | fwrite(buf, 1, sizeof(uint32_t), header_fp_); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 656 | |
| 657 | // U4: low word of the 64-bit time. |
| 658 | U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL)); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 659 | fwrite(buf, 1, sizeof(uint32_t), header_fp_); // xxx fix the time |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 660 | } |
| 661 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 662 | void WriteStackTraces() { |
| 663 | // Write a dummy stack trace record so the analysis tools don't freak out. |
| 664 | current_record_.StartNewRecord(header_fp_, HPROF_TAG_STACK_TRACE, HPROF_TIME); |
| 665 | current_record_.AddU4(HPROF_NULL_STACK_TRACE); |
| 666 | current_record_.AddU4(HPROF_NULL_THREAD); |
| 667 | current_record_.AddU4(0); // no frames |
| 668 | } |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 669 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 670 | // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored. |
| 671 | // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will |
| 672 | // only be used for debug messages. |
| 673 | std::string filename_; |
| 674 | int fd_; |
| 675 | bool direct_to_ddms_; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 676 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 677 | uint64_t start_ns_; |
| 678 | |
| 679 | HprofRecord current_record_; |
| 680 | |
| 681 | uint32_t gc_thread_serial_number_; |
| 682 | uint8_t gc_scan_state_; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 683 | HprofHeapId current_heap_; // Which heap we're currently dumping. |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 684 | size_t objects_in_segment_; |
| 685 | |
| 686 | FILE* header_fp_; |
| 687 | char* header_data_ptr_; |
| 688 | size_t header_data_size_; |
| 689 | |
| 690 | FILE* body_fp_; |
| 691 | char* body_data_ptr_; |
| 692 | size_t body_data_size_; |
| 693 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 694 | std::set<mirror::Class*> classes_; |
| 695 | HprofStringId next_string_id_; |
| 696 | SafeMap<std::string, HprofStringId> strings_; |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 697 | |
| 698 | DISALLOW_COPY_AND_ASSIGN(Hprof); |
| 699 | }; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 700 | |
| 701 | #define OBJECTS_PER_SEGMENT ((size_t)128) |
| 702 | #define BYTES_PER_SEGMENT ((size_t)4096) |
| 703 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 704 | // The static field-name for the synthetic object generated to account for class static overhead. |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 705 | #define STATIC_OVERHEAD_NAME "$staticOverhead" |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 706 | |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 707 | static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 708 | char c = sig[0]; |
| 709 | HprofBasicType ret; |
| 710 | size_t size; |
| 711 | |
| 712 | switch (c) { |
| 713 | case '[': |
| 714 | case 'L': ret = hprof_basic_object; size = 4; break; |
| 715 | case 'Z': ret = hprof_basic_boolean; size = 1; break; |
| 716 | case 'C': ret = hprof_basic_char; size = 2; break; |
| 717 | case 'F': ret = hprof_basic_float; size = 4; break; |
| 718 | case 'D': ret = hprof_basic_double; size = 8; break; |
| 719 | case 'B': ret = hprof_basic_byte; size = 1; break; |
| 720 | case 'S': ret = hprof_basic_short; size = 2; break; |
| 721 | default: CHECK(false); |
| 722 | case 'I': ret = hprof_basic_int; size = 4; break; |
| 723 | case 'J': ret = hprof_basic_long; size = 8; break; |
| 724 | } |
| 725 | |
| 726 | if (sizeOut != NULL) { |
| 727 | *sizeOut = size; |
| 728 | } |
| 729 | |
| 730 | return ret; |
| 731 | } |
| 732 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 733 | static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 734 | HprofBasicType ret; |
| 735 | size_t size; |
| 736 | |
| 737 | switch (prim) { |
| 738 | case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break; |
| 739 | case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break; |
| 740 | case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break; |
| 741 | case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break; |
| 742 | case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break; |
| 743 | case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break; |
| 744 | default: CHECK(false); |
| 745 | case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break; |
| 746 | case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break; |
| 747 | } |
| 748 | |
| 749 | if (sizeOut != NULL) { |
| 750 | *sizeOut = size; |
| 751 | } |
| 752 | |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | // Always called when marking objects, but only does |
| 757 | // something when ctx->gc_scan_state_ is non-zero, which is usually |
| 758 | // only true when marking the root set or unreachable |
| 759 | // objects. Used to add rootset references to obj. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 760 | int Hprof::MarkRootObject(const mirror::Object* obj, jobject jniObj) { |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 761 | HprofRecord* rec = ¤t_record_; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 762 | HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_; |
| 763 | |
| 764 | if (heapTag == 0) { |
| 765 | return 0; |
| 766 | } |
| 767 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 768 | if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) { |
| 769 | StartNewHeapDumpSegment(); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | switch (heapTag) { |
| 773 | // ID: object ID |
| 774 | case HPROF_ROOT_UNKNOWN: |
| 775 | case HPROF_ROOT_STICKY_CLASS: |
| 776 | case HPROF_ROOT_MONITOR_USED: |
| 777 | case HPROF_ROOT_INTERNED_STRING: |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 778 | case HPROF_ROOT_DEBUGGER: |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 779 | case HPROF_ROOT_VM_INTERNAL: |
| 780 | rec->AddU1(heapTag); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 781 | rec->AddObjectId(obj); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 782 | break; |
| 783 | |
| 784 | // ID: object ID |
| 785 | // ID: JNI global ref ID |
| 786 | case HPROF_ROOT_JNI_GLOBAL: |
| 787 | rec->AddU1(heapTag); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 788 | rec->AddObjectId(obj); |
| 789 | rec->AddJniGlobalRefId(jniObj); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 790 | break; |
| 791 | |
| 792 | // ID: object ID |
| 793 | // U4: thread serial number |
| 794 | // U4: frame number in stack trace (-1 for empty) |
| 795 | case HPROF_ROOT_JNI_LOCAL: |
| 796 | case HPROF_ROOT_JNI_MONITOR: |
| 797 | case HPROF_ROOT_JAVA_FRAME: |
| 798 | rec->AddU1(heapTag); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 799 | rec->AddObjectId(obj); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 800 | rec->AddU4(gc_thread_serial_number_); |
| 801 | rec->AddU4((uint32_t)-1); |
| 802 | break; |
| 803 | |
| 804 | // ID: object ID |
| 805 | // U4: thread serial number |
| 806 | case HPROF_ROOT_NATIVE_STACK: |
| 807 | case HPROF_ROOT_THREAD_BLOCK: |
| 808 | rec->AddU1(heapTag); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 809 | rec->AddObjectId(obj); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 810 | rec->AddU4(gc_thread_serial_number_); |
| 811 | break; |
| 812 | |
| 813 | // ID: thread object ID |
| 814 | // U4: thread serial number |
| 815 | // U4: stack trace serial number |
| 816 | case HPROF_ROOT_THREAD_OBJECT: |
| 817 | rec->AddU1(heapTag); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 818 | rec->AddObjectId(obj); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 819 | rec->AddU4(gc_thread_serial_number_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 820 | rec->AddU4((uint32_t)-1); // xxx |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 821 | break; |
| 822 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 823 | case HPROF_CLASS_DUMP: |
| 824 | case HPROF_INSTANCE_DUMP: |
| 825 | case HPROF_OBJECT_ARRAY_DUMP: |
| 826 | case HPROF_PRIMITIVE_ARRAY_DUMP: |
| 827 | case HPROF_HEAP_DUMP_INFO: |
| 828 | case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP: |
| 829 | // Ignored. |
| 830 | break; |
| 831 | |
| 832 | case HPROF_ROOT_FINALIZING: |
| 833 | case HPROF_ROOT_REFERENCE_CLEANUP: |
| 834 | case HPROF_UNREACHABLE: |
| 835 | LOG(FATAL) << "obsolete tag " << static_cast<int>(heapTag); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 836 | break; |
| 837 | } |
| 838 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 839 | ++objects_in_segment_; |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 840 | return 0; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 841 | } |
| 842 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 843 | static int StackTraceSerialNumber(const mirror::Object* /*obj*/) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 844 | return HPROF_NULL_STACK_TRACE; |
| 845 | } |
| 846 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 847 | int Hprof::DumpHeapObject(mirror::Object* obj) { |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 848 | HprofRecord* rec = ¤t_record_; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 849 | HprofHeapId desiredHeap = false ? HPROF_HEAP_ZYGOTE : HPROF_HEAP_APP; // TODO: zygote objects? |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 850 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 851 | if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) { |
| 852 | StartNewHeapDumpSegment(); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | if (desiredHeap != current_heap_) { |
| 856 | HprofStringId nameId; |
| 857 | |
| 858 | // This object is in a different heap than the current one. |
| 859 | // Emit a HEAP_DUMP_INFO tag to change heaps. |
| 860 | rec->AddU1(HPROF_HEAP_DUMP_INFO); |
| 861 | rec->AddU4((uint32_t)desiredHeap); // uint32_t: heap id |
| 862 | switch (desiredHeap) { |
| 863 | case HPROF_HEAP_APP: |
| 864 | nameId = LookupStringId("app"); |
| 865 | break; |
| 866 | case HPROF_HEAP_ZYGOTE: |
| 867 | nameId = LookupStringId("zygote"); |
| 868 | break; |
| 869 | default: |
| 870 | // Internal error |
| 871 | LOG(ERROR) << "Unexpected desiredHeap"; |
| 872 | nameId = LookupStringId("<ILLEGAL>"); |
| 873 | break; |
| 874 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 875 | rec->AddStringId(nameId); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 876 | current_heap_ = desiredHeap; |
| 877 | } |
| 878 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 879 | mirror::Class* c = obj->GetClass(); |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 880 | if (c == NULL) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 881 | // This object will bother HprofReader, because it has a NULL |
| 882 | // class, so just don't dump it. It could be |
| 883 | // gDvm.unlinkedJavaLangClass or it could be an object just |
| 884 | // allocated which hasn't been initialized yet. |
| 885 | } else { |
| 886 | if (obj->IsClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 887 | mirror::Class* thisClass = obj->AsClass(); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 888 | // obj is a ClassObject. |
| 889 | size_t sFieldCount = thisClass->NumStaticFields(); |
| 890 | if (sFieldCount != 0) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 891 | int byteLength = sFieldCount * sizeof(JValue); // TODO bogus; fields are packed |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 892 | // Create a byte array to reflect the allocation of the |
| 893 | // StaticField array at the end of this class. |
| 894 | rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 895 | rec->AddClassStaticsId(thisClass); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 896 | rec->AddU4(StackTraceSerialNumber(obj)); |
| 897 | rec->AddU4(byteLength); |
| 898 | rec->AddU1(hprof_basic_byte); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 899 | for (int i = 0; i < byteLength; ++i) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 900 | rec->AddU1(0); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | rec->AddU1(HPROF_CLASS_DUMP); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 905 | rec->AddClassId(LookupClassId(thisClass)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 906 | rec->AddU4(StackTraceSerialNumber(thisClass)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 907 | rec->AddClassId(LookupClassId(thisClass->GetSuperClass())); |
| 908 | rec->AddObjectId(thisClass->GetClassLoader()); |
| 909 | rec->AddObjectId(nullptr); // no signer |
| 910 | rec->AddObjectId(nullptr); // no prot domain |
| 911 | rec->AddObjectId(nullptr); // reserved |
| 912 | rec->AddObjectId(nullptr); // reserved |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 913 | if (thisClass->IsClassClass()) { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 914 | // ClassObjects have their static fields appended, so aren't all the same size. |
| 915 | // But they're at least this size. |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 916 | rec->AddU4(sizeof(mirror::Class)); // instance size |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 917 | } else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) { |
| 918 | rec->AddU4(0); |
| 919 | } else { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 920 | rec->AddU4(thisClass->GetObjectSize()); // instance size |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 921 | } |
| 922 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 923 | rec->AddU2(0); // empty const pool |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 924 | |
| 925 | // Static fields |
| 926 | if (sFieldCount == 0) { |
| 927 | rec->AddU2((uint16_t)0); |
| 928 | } else { |
| 929 | rec->AddU2((uint16_t)(sFieldCount+1)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 930 | rec->AddStringId(LookupStringId(STATIC_OVERHEAD_NAME)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 931 | rec->AddU1(hprof_basic_object); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 932 | rec->AddClassStaticsId(thisClass); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 933 | |
| 934 | for (size_t i = 0; i < sFieldCount; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 935 | mirror::ArtField* f = thisClass->GetStaticField(i); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 936 | |
| 937 | size_t size; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 938 | HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size); |
| 939 | rec->AddStringId(LookupStringId(f->GetName())); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 940 | rec->AddU1(t); |
| 941 | if (size == 1) { |
Ian Rogers | a3b59cb | 2013-03-19 19:50:20 -0700 | [diff] [blame] | 942 | rec->AddU1(static_cast<uint8_t>(f->Get32(thisClass))); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 943 | } else if (size == 2) { |
Ian Rogers | a3b59cb | 2013-03-19 19:50:20 -0700 | [diff] [blame] | 944 | rec->AddU2(static_cast<uint16_t>(f->Get32(thisClass))); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 945 | } else if (size == 4) { |
Ian Rogers | a3b59cb | 2013-03-19 19:50:20 -0700 | [diff] [blame] | 946 | rec->AddU4(f->Get32(thisClass)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 947 | } else if (size == 8) { |
Ian Rogers | a3b59cb | 2013-03-19 19:50:20 -0700 | [diff] [blame] | 948 | rec->AddU8(f->Get64(thisClass)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 949 | } else { |
| 950 | CHECK(false); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | // Instance fields for this class (no superclass fields) |
| 956 | int iFieldCount = thisClass->IsObjectClass() ? 0 : thisClass->NumInstanceFields(); |
| 957 | rec->AddU2((uint16_t)iFieldCount); |
| 958 | for (int i = 0; i < iFieldCount; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 959 | mirror::ArtField* f = thisClass->GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 960 | HprofBasicType t = SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), NULL); |
| 961 | rec->AddStringId(LookupStringId(f->GetName())); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 962 | rec->AddU1(t); |
| 963 | } |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 964 | } else if (c->IsArrayClass()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 965 | mirror::Array* aobj = obj->AsArray(); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 966 | uint32_t length = aobj->GetLength(); |
| 967 | |
| 968 | if (obj->IsObjectArray()) { |
| 969 | // obj is an object array. |
| 970 | rec->AddU1(HPROF_OBJECT_ARRAY_DUMP); |
| 971 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 972 | rec->AddObjectId(obj); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 973 | rec->AddU4(StackTraceSerialNumber(obj)); |
| 974 | rec->AddU4(length); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 975 | rec->AddClassId(LookupClassId(c)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 976 | |
| 977 | // Dump the elements, which are always objects or NULL. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 978 | rec->AddIdList(aobj->AsObjectArray<mirror::Object>()); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 979 | } else { |
| 980 | size_t size; |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 981 | HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 982 | |
| 983 | // obj is a primitive array. |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 984 | rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 985 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 986 | rec->AddObjectId(obj); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 987 | rec->AddU4(StackTraceSerialNumber(obj)); |
| 988 | rec->AddU4(length); |
| 989 | rec->AddU1(t); |
| 990 | |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 991 | // Dump the raw, packed element values. |
| 992 | if (size == 1) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 993 | rec->AddU1List((const uint8_t*)aobj->GetRawData(sizeof(uint8_t), 0), length); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 994 | } else if (size == 2) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 995 | rec->AddU2List((const uint16_t*)aobj->GetRawData(sizeof(uint16_t), 0), length); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 996 | } else if (size == 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 997 | rec->AddU4List((const uint32_t*)aobj->GetRawData(sizeof(uint32_t), 0), length); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 998 | } else if (size == 8) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 999 | rec->AddU8List((const uint64_t*)aobj->GetRawData(sizeof(uint64_t), 0), length); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1000 | } |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1001 | } |
| 1002 | } else { |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1003 | // obj is an instance object. |
| 1004 | rec->AddU1(HPROF_INSTANCE_DUMP); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1005 | rec->AddObjectId(obj); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1006 | rec->AddU4(StackTraceSerialNumber(obj)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1007 | rec->AddClassId(LookupClassId(c)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1008 | |
| 1009 | // Reserve some space for the length of the instance data, which we won't |
| 1010 | // know until we're done writing it. |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1011 | size_t size_patch_offset = rec->Size(); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1012 | rec->AddU4(0x77777777); |
| 1013 | |
| 1014 | // Write the instance data; fields for this class, followed by super class fields, |
| 1015 | // and so on. Don't write the klass or monitor fields of Object.class. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1016 | mirror::Class* sclass = c; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1017 | while (!sclass->IsObjectClass()) { |
| 1018 | int ifieldCount = sclass->NumInstanceFields(); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1019 | for (int i = 0; i < ifieldCount; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1020 | mirror::ArtField* f = sclass->GetInstanceField(i); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1021 | size_t size; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1022 | SignatureToBasicTypeAndSize(f->GetTypeDescriptor(), &size); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1023 | if (size == 1) { |
| 1024 | rec->AddU1(f->Get32(obj)); |
| 1025 | } else if (size == 2) { |
| 1026 | rec->AddU2(f->Get32(obj)); |
| 1027 | } else if (size == 4) { |
| 1028 | rec->AddU4(f->Get32(obj)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1029 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1030 | CHECK_EQ(size, 8U); |
| 1031 | rec->AddU8(f->Get64(obj)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | sclass = sclass->GetSuperClass(); |
| 1036 | } |
| 1037 | |
| 1038 | // Patch the instance field length. |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1039 | rec->UpdateU4(size_patch_offset, rec->Size() - (size_patch_offset + 4)); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1043 | ++objects_in_segment_; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 1044 | return 0; |
| 1045 | } |
| 1046 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1047 | void Hprof::VisitRoot(const mirror::Object* obj, uint32_t thread_id, RootType type) { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 1048 | static const HprofHeapTag xlate[] = { |
| 1049 | HPROF_ROOT_UNKNOWN, |
| 1050 | HPROF_ROOT_JNI_GLOBAL, |
| 1051 | HPROF_ROOT_JNI_LOCAL, |
| 1052 | HPROF_ROOT_JAVA_FRAME, |
| 1053 | HPROF_ROOT_NATIVE_STACK, |
| 1054 | HPROF_ROOT_STICKY_CLASS, |
| 1055 | HPROF_ROOT_THREAD_BLOCK, |
| 1056 | HPROF_ROOT_MONITOR_USED, |
| 1057 | HPROF_ROOT_THREAD_OBJECT, |
| 1058 | HPROF_ROOT_INTERNED_STRING, |
| 1059 | HPROF_ROOT_FINALIZING, |
| 1060 | HPROF_ROOT_DEBUGGER, |
| 1061 | HPROF_ROOT_REFERENCE_CLEANUP, |
| 1062 | HPROF_ROOT_VM_INTERNAL, |
| 1063 | HPROF_ROOT_JNI_MONITOR, |
| 1064 | }; |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 1065 | CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag)); |
| 1066 | if (obj == NULL) { |
| 1067 | return; |
| 1068 | } |
| 1069 | gc_scan_state_ = xlate[type]; |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1070 | gc_thread_serial_number_ = thread_id; |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 1071 | MarkRootObject(obj, 0); |
| 1072 | gc_scan_state_ = 0; |
| 1073 | gc_thread_serial_number_ = 0; |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 1074 | } |
| 1075 | |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1076 | // If "direct_to_ddms" is true, the other arguments are ignored, and data is |
| 1077 | // sent directly to DDMS. |
| 1078 | // If "fd" is >= 0, the output will be written to that file descriptor. |
| 1079 | // Otherwise, "filename" is used to create an output file. |
| 1080 | void DumpHeap(const char* filename, int fd, bool direct_to_ddms) { |
| 1081 | CHECK(filename != NULL); |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 1082 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1083 | Runtime::Current()->GetThreadList()->SuspendAll(); |
Elliott Hughes | dcfdd2b | 2012-07-09 18:27:46 -0700 | [diff] [blame] | 1084 | Hprof hprof(filename, fd, direct_to_ddms); |
| 1085 | hprof.Dump(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1086 | Runtime::Current()->GetThreadList()->ResumeAll(); |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | } // namespace hprof |
| 1090 | |
| 1091 | } // namespace art |