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