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