blob: e0a4c05402a8c4ebaf302dd638e070af1bcc5190 [file] [log] [blame]
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001/*
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 Rogers6d4d9fc2011-11-30 16:24:48 -080026
Elliott Hughes622a6982012-06-08 17:58:54 -070027#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 Hughes07ed66b2012-12-12 18:34:25 -080040#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080041#include "base/stringprintf.h"
Elliott Hughes76160052012-12-12 16:31:20 -080042#include "base/unix_file/fd_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080043#include "class_linker.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040044#include "debugger.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070045#include "globals.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080046#include "heap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/class.h"
48#include "mirror/class-inl.h"
49#include "mirror/field.h"
50#include "mirror/field-inl.h"
51#include "mirror/object-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080052#include "object_utils.h"
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -070053#include "os.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070054#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055#include "scoped_thread_state_change.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070056#include "gc/space.h"
Elliott Hughes622a6982012-06-08 17:58:54 -070057#include "thread_list.h"
Jesse Wilsonc4824e62011-11-01 14:39:04 -040058
59namespace art {
60
61namespace hprof {
62
Elliott Hughes622a6982012-06-08 17:58:54 -070063#define UNIQUE_ERROR -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff))
Jesse Wilson0c54ac12011-11-09 15:14:05 -050064
Elliott Hughes622a6982012-06-08 17:58:54 -070065#define HPROF_TIME 0
66#define HPROF_NULL_STACK_TRACE 0
67#define HPROF_NULL_THREAD 0
68
69#define U2_TO_BUF_BE(buf, offset, value) \
70 do { \
71 unsigned char* buf_ = (unsigned char*)(buf); \
72 int offset_ = (int)(offset); \
73 uint16_t value_ = (uint16_t)(value); \
74 buf_[offset_ + 0] = (unsigned char)(value_ >> 8); \
75 buf_[offset_ + 1] = (unsigned char)(value_ ); \
76 } while (0)
77
78#define U4_TO_BUF_BE(buf, offset, value) \
79 do { \
80 unsigned char* buf_ = (unsigned char*)(buf); \
81 int offset_ = (int)(offset); \
82 uint32_t value_ = (uint32_t)(value); \
83 buf_[offset_ + 0] = (unsigned char)(value_ >> 24); \
84 buf_[offset_ + 1] = (unsigned char)(value_ >> 16); \
85 buf_[offset_ + 2] = (unsigned char)(value_ >> 8); \
86 buf_[offset_ + 3] = (unsigned char)(value_ ); \
87 } while (0)
88
89#define U8_TO_BUF_BE(buf, offset, value) \
90 do { \
91 unsigned char* buf_ = (unsigned char*)(buf); \
92 int offset_ = (int)(offset); \
93 uint64_t value_ = (uint64_t)(value); \
94 buf_[offset_ + 0] = (unsigned char)(value_ >> 56); \
95 buf_[offset_ + 1] = (unsigned char)(value_ >> 48); \
96 buf_[offset_ + 2] = (unsigned char)(value_ >> 40); \
97 buf_[offset_ + 3] = (unsigned char)(value_ >> 32); \
98 buf_[offset_ + 4] = (unsigned char)(value_ >> 24); \
99 buf_[offset_ + 5] = (unsigned char)(value_ >> 16); \
100 buf_[offset_ + 6] = (unsigned char)(value_ >> 8); \
101 buf_[offset_ + 7] = (unsigned char)(value_ ); \
102 } while (0)
103
104enum HprofTag {
105 HPROF_TAG_STRING = 0x01,
106 HPROF_TAG_LOAD_CLASS = 0x02,
107 HPROF_TAG_UNLOAD_CLASS = 0x03,
108 HPROF_TAG_STACK_FRAME = 0x04,
109 HPROF_TAG_STACK_TRACE = 0x05,
110 HPROF_TAG_ALLOC_SITES = 0x06,
111 HPROF_TAG_HEAP_SUMMARY = 0x07,
112 HPROF_TAG_START_THREAD = 0x0A,
113 HPROF_TAG_END_THREAD = 0x0B,
114 HPROF_TAG_HEAP_DUMP = 0x0C,
115 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
116 HPROF_TAG_HEAP_DUMP_END = 0x2C,
117 HPROF_TAG_CPU_SAMPLES = 0x0D,
118 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
119};
120
121// Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records:
122enum HprofHeapTag {
123 // Traditional.
124 HPROF_ROOT_UNKNOWN = 0xFF,
125 HPROF_ROOT_JNI_GLOBAL = 0x01,
126 HPROF_ROOT_JNI_LOCAL = 0x02,
127 HPROF_ROOT_JAVA_FRAME = 0x03,
128 HPROF_ROOT_NATIVE_STACK = 0x04,
129 HPROF_ROOT_STICKY_CLASS = 0x05,
130 HPROF_ROOT_THREAD_BLOCK = 0x06,
131 HPROF_ROOT_MONITOR_USED = 0x07,
132 HPROF_ROOT_THREAD_OBJECT = 0x08,
133 HPROF_CLASS_DUMP = 0x20,
134 HPROF_INSTANCE_DUMP = 0x21,
135 HPROF_OBJECT_ARRAY_DUMP = 0x22,
136 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
137
138 // Android.
139 HPROF_HEAP_DUMP_INFO = 0xfe,
140 HPROF_ROOT_INTERNED_STRING = 0x89,
141 HPROF_ROOT_FINALIZING = 0x8a, // Obsolete.
142 HPROF_ROOT_DEBUGGER = 0x8b,
143 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, // Obsolete.
144 HPROF_ROOT_VM_INTERNAL = 0x8d,
145 HPROF_ROOT_JNI_MONITOR = 0x8e,
146 HPROF_UNREACHABLE = 0x90, // Obsolete.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700147 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, // Obsolete.
Elliott Hughes622a6982012-06-08 17:58:54 -0700148};
149
150enum HprofHeapId {
151 HPROF_HEAP_DEFAULT = 0,
152 HPROF_HEAP_ZYGOTE = 'Z',
153 HPROF_HEAP_APP = 'A'
154};
155
156enum HprofBasicType {
157 hprof_basic_object = 2,
158 hprof_basic_boolean = 4,
159 hprof_basic_char = 5,
160 hprof_basic_float = 6,
161 hprof_basic_double = 7,
162 hprof_basic_byte = 8,
163 hprof_basic_short = 9,
164 hprof_basic_int = 10,
165 hprof_basic_long = 11,
166};
167
168typedef uint32_t HprofId;
169typedef HprofId HprofStringId;
170typedef HprofId HprofObjectId;
171typedef HprofId HprofClassObjectId;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172typedef std::set<mirror::Class*> ClassSet;
173typedef std::set<mirror::Class*>::iterator ClassSetIterator;
Elliott Hughes622a6982012-06-08 17:58:54 -0700174typedef SafeMap<std::string, size_t> StringMap;
175typedef SafeMap<std::string, size_t>::iterator StringMapIterator;
176
177// Represents a top-level hprof record, whose serialized format is:
178// U1 TAG: denoting the type of the record
179// U4 TIME: number of microseconds since the time stamp in the header
180// U4 LENGTH: number of bytes that follow this uint32_t field and belong to this record
181// U1* BODY: as many bytes as specified in the above uint32_t field
182class HprofRecord {
183 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700184 HprofRecord() {
185 dirty_ = false;
186 alloc_length_ = 128;
187 body_ = reinterpret_cast<unsigned char*>(malloc(alloc_length_));
188 fp_ = NULL;
189 }
190
191 ~HprofRecord() {
192 free(body_);
193 }
194
195 int StartNewRecord(FILE* fp, uint8_t tag, uint32_t time) {
196 int rc = Flush();
197 if (rc != 0) {
198 return rc;
199 }
200
201 fp_ = fp;
202 tag_ = tag;
203 time_ = time;
204 length_ = 0;
205 dirty_ = true;
206 return 0;
207 }
208
209 int Flush() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700210 if (dirty_) {
Elliott Hughesa21039c2012-06-21 12:09:25 -0700211 unsigned char headBuf[sizeof(uint8_t) + 2 * sizeof(uint32_t)];
Elliott Hughes622a6982012-06-08 17:58:54 -0700212
213 headBuf[0] = tag_;
214 U4_TO_BUF_BE(headBuf, 1, time_);
215 U4_TO_BUF_BE(headBuf, 5, length_);
216
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700217 int nb = fwrite(headBuf, 1, sizeof(headBuf), fp_);
Elliott Hughes622a6982012-06-08 17:58:54 -0700218 if (nb != sizeof(headBuf)) {
219 return UNIQUE_ERROR;
220 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700221 nb = fwrite(body_, 1, length_, fp_);
Elliott Hughes622a6982012-06-08 17:58:54 -0700222 if (nb != (int)length_) {
223 return UNIQUE_ERROR;
224 }
225
226 dirty_ = false;
227 }
228 // TODO if we used less than half (or whatever) of allocLen, shrink the buffer.
229 return 0;
230 }
231
232 int AddU1(uint8_t value) {
233 int err = GuaranteeRecordAppend(1);
234 if (err != 0) {
235 return err;
236 }
237
238 body_[length_++] = value;
239 return 0;
240 }
241
242 int AddU2(uint16_t value) {
243 return AddU2List(&value, 1);
244 }
245
246 int AddU4(uint32_t value) {
247 return AddU4List(&value, 1);
248 }
249
250 int AddU8(uint64_t value) {
251 return AddU8List(&value, 1);
252 }
253
254 int AddId(HprofObjectId value) {
255 return AddU4((uint32_t) value);
256 }
257
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700258 int AddU1List(const uint8_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700259 int err = GuaranteeRecordAppend(numValues);
260 if (err != 0) {
261 return err;
262 }
263
264 memcpy(body_ + length_, values, numValues);
265 length_ += numValues;
266 return 0;
267 }
268
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700269 int AddU2List(const uint16_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700270 int err = GuaranteeRecordAppend(numValues * 2);
271 if (err != 0) {
272 return err;
273 }
274
275 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700276 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700277 U2_TO_BUF_BE(insert, 0, *values++);
278 insert += sizeof(*values);
279 }
280 length_ += numValues * 2;
281 return 0;
282 }
283
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700284 int AddU4List(const uint32_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700285 int err = GuaranteeRecordAppend(numValues * 4);
286 if (err != 0) {
287 return err;
288 }
289
290 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700291 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700292 U4_TO_BUF_BE(insert, 0, *values++);
293 insert += sizeof(*values);
294 }
295 length_ += numValues * 4;
296 return 0;
297 }
298
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700299 void UpdateU4(size_t offset, uint32_t new_value) {
300 U4_TO_BUF_BE(body_, offset, new_value);
301 }
302
303 int AddU8List(const uint64_t* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700304 int err = GuaranteeRecordAppend(numValues * 8);
305 if (err != 0) {
306 return err;
307 }
308
309 unsigned char* insert = body_ + length_;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700310 for (size_t i = 0; i < numValues; ++i) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700311 U8_TO_BUF_BE(insert, 0, *values++);
312 insert += sizeof(*values);
313 }
314 length_ += numValues * 8;
315 return 0;
316 }
317
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700318 int AddIdList(const HprofObjectId* values, size_t numValues) {
Elliott Hughes622a6982012-06-08 17:58:54 -0700319 return AddU4List((const uint32_t*) values, numValues);
320 }
321
322 int AddUtf8String(const char* str) {
323 // The terminating NUL character is NOT written.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700324 return AddU1List((const uint8_t*)str, strlen(str));
Elliott Hughes622a6982012-06-08 17:58:54 -0700325 }
326
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700327 size_t Size() const {
328 return length_;
329 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700330
331 private:
332 int GuaranteeRecordAppend(size_t nmore) {
333 size_t minSize = length_ + nmore;
334 if (minSize > alloc_length_) {
335 size_t newAllocLen = alloc_length_ * 2;
336 if (newAllocLen < minSize) {
337 newAllocLen = alloc_length_ + nmore + nmore/2;
338 }
339 unsigned char* newBody = (unsigned char*)realloc(body_, newAllocLen);
340 if (newBody != NULL) {
341 body_ = newBody;
342 alloc_length_ = newAllocLen;
343 } else {
344 // TODO: set an error flag so future ops will fail
345 return UNIQUE_ERROR;
346 }
347 }
348
349 CHECK_LE(length_ + nmore, alloc_length_);
350 return 0;
351 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700352
353 size_t alloc_length_;
354 unsigned char* body_;
355
356 FILE* fp_;
357 uint8_t tag_;
358 uint32_t time_;
359 size_t length_;
360 bool dirty_;
361
362 DISALLOW_COPY_AND_ASSIGN(HprofRecord);
Elliott Hughes622a6982012-06-08 17:58:54 -0700363};
364
365class Hprof {
366 public:
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700367 Hprof(const char* output_filename, int fd, bool direct_to_ddms)
368 : filename_(output_filename),
369 fd_(fd),
370 direct_to_ddms_(direct_to_ddms),
371 start_ns_(NanoTime()),
372 current_record_(),
373 gc_thread_serial_number_(0),
374 gc_scan_state_(0),
375 current_heap_(HPROF_HEAP_DEFAULT),
376 objects_in_segment_(0),
377 header_fp_(NULL),
378 header_data_ptr_(NULL),
379 header_data_size_(0),
380 body_fp_(NULL),
381 body_data_ptr_(NULL),
382 body_data_size_(0),
383 next_string_id_(0x400000) {
384 LOG(INFO) << "hprof: heap dump \"" << filename_ << "\" starting...";
Elliott Hughes622a6982012-06-08 17:58:54 -0700385
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700386 header_fp_ = open_memstream(&header_data_ptr_, &header_data_size_);
387 if (header_fp_ == NULL) {
388 PLOG(FATAL) << "header open_memstream failed";
389 }
Elliott Hughes622a6982012-06-08 17:58:54 -0700390
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700391 body_fp_ = open_memstream(&body_data_ptr_, &body_data_size_);
392 if (body_fp_ == NULL) {
393 PLOG(FATAL) << "body open_memstream failed";
394 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500395 }
396
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700397 ~Hprof() {
398 if (header_fp_ != NULL) {
399 fclose(header_fp_);
400 }
401 if (body_fp_ != NULL) {
402 fclose(body_fp_);
403 }
404 free(header_data_ptr_);
405 free(body_data_ptr_);
406 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500407
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 void Dump()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700409 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
410 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700411 // Walk the roots and the heap.
412 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
413 Runtime::Current()->VisitRoots(RootVisitor, this);
Ian Rogers50b35e22012-10-04 10:09:15 -0700414 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700415 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700416 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700417 Runtime::Current()->GetHeap()->FlushAllocStack();
418 }
419 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700420 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700421 Runtime::Current()->GetHeap()->GetLiveBitmap()->Walk(HeapBitmapCallback, this);
422 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700423 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_END, HPROF_TIME);
424 current_record_.Flush();
425 fflush(body_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500426
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700427 // Write the header.
428 WriteFixedHeader();
429 // Write the string and class tables, and any stack traces, to the header.
430 // (jhat requires that these appear before any of the data in the body that refers to them.)
431 WriteStringTable();
432 WriteClassTable();
433 WriteStackTraces();
434 current_record_.Flush();
435 fflush(header_fp_);
436
437 bool okay = true;
438 if (direct_to_ddms_) {
439 // Send the data off to DDMS.
440 iovec iov[2];
441 iov[0].iov_base = header_data_ptr_;
442 iov[0].iov_len = header_data_size_;
443 iov[1].iov_base = body_data_ptr_;
444 iov[1].iov_len = body_data_size_;
445 Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
446 } else {
447 // Where exactly are we writing to?
448 int out_fd;
449 if (fd_ >= 0) {
450 out_fd = dup(fd_);
451 if (out_fd < 0) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700452 self->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
453 "Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700454 return;
455 }
456 } else {
457 out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
458 if (out_fd < 0) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700459 self->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
460 "Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
461 strerror(errno));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700462 return;
463 }
464 }
465
Elliott Hughes76160052012-12-12 16:31:20 -0800466 UniquePtr<File> file(new File(out_fd, filename_));
Ian Rogers50b35e22012-10-04 10:09:15 -0700467 okay = file->WriteFully(header_data_ptr_, header_data_size_) &&
468 file->WriteFully(body_data_ptr_, body_data_size_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700469 if (!okay) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700470 std::string msg(StringPrintf("Couldn't dump heap; writing \"%s\" failed: %s",
471 filename_.c_str(), strerror(errno)));
472 self->ThrowNewException("Ljava/lang/RuntimeException;", msg.c_str());
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700473 LOG(ERROR) << msg;
474 }
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700475 }
476
477 // Throw out a log message for the benefit of "runhat".
478 if (okay) {
479 uint64_t duration = NanoTime() - start_ns_;
Ian Rogers50b35e22012-10-04 10:09:15 -0700480 LOG(INFO) << "hprof: heap dump completed ("
481 << PrettySize(header_data_size_ + body_data_size_ + 1023)
482 << ") in " << PrettyDuration(duration);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700483 }
484 }
485
486 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487 static void RootVisitor(const mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700488 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700489 CHECK(arg != NULL);
490 Hprof* hprof = reinterpret_cast<Hprof*>(arg);
491 hprof->VisitRoot(obj);
492 }
493
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800494 static void HeapBitmapCallback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700495 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700496 CHECK(obj != NULL);
497 CHECK(arg != NULL);
498 Hprof* hprof = reinterpret_cast<Hprof*>(arg);
499 hprof->DumpHeapObject(obj);
500 }
501
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502 void VisitRoot(const mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700503
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800504 int DumpHeapObject(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700505
506 void Finish() {
507 }
508
Ian Rogersb726dcb2012-09-05 08:57:23 -0700509 int WriteClassTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700510 HprofRecord* rec = &current_record_;
511 uint32_t nextSerialNumber = 1;
512
513 for (ClassSetIterator it = classes_.begin(); it != classes_.end(); ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514 const mirror::Class* c = *it;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700515 CHECK(c != NULL);
516
517 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_LOAD_CLASS, HPROF_TIME);
518 if (err != 0) {
519 return err;
520 }
521
522 // LOAD CLASS format:
523 // U4: class serial number (always > 0)
524 // ID: class object ID. We use the address of the class object structure as its ID.
525 // U4: stack trace serial number
526 // ID: class name string ID
527 rec->AddU4(nextSerialNumber++);
528 rec->AddId((HprofClassObjectId) c);
529 rec->AddU4(HPROF_NULL_STACK_TRACE);
530 rec->AddId(LookupClassNameId(c));
531 }
532
533 return 0;
534 }
535
536 int WriteStringTable() {
537 HprofRecord* rec = &current_record_;
538
539 for (StringMapIterator it = strings_.begin(); it != strings_.end(); ++it) {
540 std::string string((*it).first);
541 size_t id = (*it).second;
542
543 int err = current_record_.StartNewRecord(header_fp_, HPROF_TAG_STRING, HPROF_TIME);
544 if (err != 0) {
545 return err;
546 }
547
548 // STRING format:
549 // ID: ID for this string
550 // U1*: UTF8 characters for string (NOT NULL terminated)
551 // (the record format encodes the length)
552 err = rec->AddU4(id);
553 if (err != 0) {
554 return err;
555 }
556 err = rec->AddUtf8String(string.c_str());
557 if (err != 0) {
558 return err;
559 }
560 }
561
562 return 0;
563 }
564
565 void StartNewHeapDumpSegment() {
566 // This flushes the old segment and starts a new one.
567 current_record_.StartNewRecord(body_fp_, HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
568 objects_in_segment_ = 0;
569
570 // Starting a new HEAP_DUMP resets the heap to default.
571 current_heap_ = HPROF_HEAP_DEFAULT;
572 }
573
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 int MarkRootObject(const mirror::Object* obj, jobject jniObj);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700575
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576 HprofClassObjectId LookupClassId(mirror::Class* c)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700577 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700578 if (c == NULL) {
579 // c is the superclass of java.lang.Object or a primitive
580 return (HprofClassObjectId)0;
581 }
582
583 std::pair<ClassSetIterator, bool> result = classes_.insert(c);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800584 const mirror::Class* present = *result.first;
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700585
586 // Make sure that we've assigned a string ID for this class' name
587 LookupClassNameId(c);
588
589 CHECK_EQ(present, c);
590 return (HprofStringId) present;
591 }
592
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800593 HprofStringId LookupStringId(mirror::String* string) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700594 return LookupStringId(string->ToModifiedUtf8());
595 }
596
597 HprofStringId LookupStringId(const char* string) {
598 return LookupStringId(std::string(string));
599 }
600
601 HprofStringId LookupStringId(const std::string& string) {
602 StringMapIterator it = strings_.find(string);
603 if (it != strings_.end()) {
604 return it->second;
605 }
606 HprofStringId id = next_string_id_++;
607 strings_.Put(string, id);
608 return id;
609 }
610
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 HprofStringId LookupClassNameId(const mirror::Class* c)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700613 return LookupStringId(PrettyDescriptor(c));
614 }
615
616 void WriteFixedHeader() {
Elliott Hughes622a6982012-06-08 17:58:54 -0700617 char magic[] = "JAVA PROFILE 1.0.3";
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500618 unsigned char buf[4];
619
620 // Write the file header.
621 // U1: NUL-terminated magic string.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700622 fwrite(magic, 1, sizeof(magic), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500623
624 // U4: size of identifiers. We're using addresses as IDs, so make sure a pointer fits.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700625 U4_TO_BUF_BE(buf, 0, sizeof(void*));
626 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500627
628 // The current time, in milliseconds since 0:00 GMT, 1/1/70.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700629 timeval now;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500630 uint64_t nowMs;
631 if (gettimeofday(&now, NULL) < 0) {
632 nowMs = 0;
633 } else {
634 nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
635 }
636
637 // U4: high word of the 64-bit time.
638 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700639 fwrite(buf, 1, sizeof(uint32_t), header_fp_);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500640
641 // U4: low word of the 64-bit time.
642 U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL));
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700643 fwrite(buf, 1, sizeof(uint32_t), header_fp_); //xxx fix the time
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500644 }
645
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700646 void WriteStackTraces() {
647 // Write a dummy stack trace record so the analysis tools don't freak out.
648 current_record_.StartNewRecord(header_fp_, HPROF_TAG_STACK_TRACE, HPROF_TIME);
649 current_record_.AddU4(HPROF_NULL_STACK_TRACE);
650 current_record_.AddU4(HPROF_NULL_THREAD);
651 current_record_.AddU4(0); // no frames
652 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500653
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700654 // If direct_to_ddms_ is set, "filename_" and "fd" will be ignored.
655 // Otherwise, "filename_" must be valid, though if "fd" >= 0 it will
656 // only be used for debug messages.
657 std::string filename_;
658 int fd_;
659 bool direct_to_ddms_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500660
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700661 uint64_t start_ns_;
662
663 HprofRecord current_record_;
664
665 uint32_t gc_thread_serial_number_;
666 uint8_t gc_scan_state_;
667 HprofHeapId current_heap_; // Which heap we're currently dumping.
668 size_t objects_in_segment_;
669
670 FILE* header_fp_;
671 char* header_data_ptr_;
672 size_t header_data_size_;
673
674 FILE* body_fp_;
675 char* body_data_ptr_;
676 size_t body_data_size_;
677
678 ClassSet classes_;
679 size_t next_string_id_;
680 StringMap strings_;
681
682 DISALLOW_COPY_AND_ASSIGN(Hprof);
683};
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500684
685#define OBJECTS_PER_SEGMENT ((size_t)128)
686#define BYTES_PER_SEGMENT ((size_t)4096)
687
688// The static field-name for the synthetic object generated to account
689// for class static overhead.
690#define STATIC_OVERHEAD_NAME "$staticOverhead"
691// The ID for the synthetic object generated to account for class static overhead.
Elliott Hughese84278b2012-03-22 10:06:53 -0700692#define CLASS_STATICS_ID(c) ((HprofObjectId)(((uint32_t)(c)) | 1))
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500693
Elliott Hughes622a6982012-06-08 17:58:54 -0700694static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500695 char c = sig[0];
696 HprofBasicType ret;
697 size_t size;
698
699 switch (c) {
700 case '[':
701 case 'L': ret = hprof_basic_object; size = 4; break;
702 case 'Z': ret = hprof_basic_boolean; size = 1; break;
703 case 'C': ret = hprof_basic_char; size = 2; break;
704 case 'F': ret = hprof_basic_float; size = 4; break;
705 case 'D': ret = hprof_basic_double; size = 8; break;
706 case 'B': ret = hprof_basic_byte; size = 1; break;
707 case 'S': ret = hprof_basic_short; size = 2; break;
708 default: CHECK(false);
709 case 'I': ret = hprof_basic_int; size = 4; break;
710 case 'J': ret = hprof_basic_long; size = 8; break;
711 }
712
713 if (sizeOut != NULL) {
714 *sizeOut = size;
715 }
716
717 return ret;
718}
719
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700720static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500721 HprofBasicType ret;
722 size_t size;
723
724 switch (prim) {
725 case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break;
726 case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break;
727 case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break;
728 case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break;
729 case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break;
730 case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break;
731 default: CHECK(false);
732 case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break;
733 case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break;
734 }
735
736 if (sizeOut != NULL) {
737 *sizeOut = size;
738 }
739
740 return ret;
741}
742
743// Always called when marking objects, but only does
744// something when ctx->gc_scan_state_ is non-zero, which is usually
745// only true when marking the root set or unreachable
746// objects. Used to add rootset references to obj.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800747int Hprof::MarkRootObject(const mirror::Object* obj, jobject jniObj) {
Elliott Hughes73e66f72012-05-09 09:34:45 -0700748 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500749 HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_;
750
751 if (heapTag == 0) {
752 return 0;
753 }
754
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700755 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
756 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500757 }
758
759 switch (heapTag) {
760 // ID: object ID
761 case HPROF_ROOT_UNKNOWN:
762 case HPROF_ROOT_STICKY_CLASS:
763 case HPROF_ROOT_MONITOR_USED:
764 case HPROF_ROOT_INTERNED_STRING:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500765 case HPROF_ROOT_DEBUGGER:
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500766 case HPROF_ROOT_VM_INTERNAL:
767 rec->AddU1(heapTag);
768 rec->AddId((HprofObjectId)obj);
769 break;
770
771 // ID: object ID
772 // ID: JNI global ref ID
773 case HPROF_ROOT_JNI_GLOBAL:
774 rec->AddU1(heapTag);
775 rec->AddId((HprofObjectId)obj);
776 rec->AddId((HprofId)jniObj);
777 break;
778
779 // ID: object ID
780 // U4: thread serial number
781 // U4: frame number in stack trace (-1 for empty)
782 case HPROF_ROOT_JNI_LOCAL:
783 case HPROF_ROOT_JNI_MONITOR:
784 case HPROF_ROOT_JAVA_FRAME:
785 rec->AddU1(heapTag);
786 rec->AddId((HprofObjectId)obj);
787 rec->AddU4(gc_thread_serial_number_);
788 rec->AddU4((uint32_t)-1);
789 break;
790
791 // ID: object ID
792 // U4: thread serial number
793 case HPROF_ROOT_NATIVE_STACK:
794 case HPROF_ROOT_THREAD_BLOCK:
795 rec->AddU1(heapTag);
796 rec->AddId((HprofObjectId)obj);
797 rec->AddU4(gc_thread_serial_number_);
798 break;
799
800 // ID: thread object ID
801 // U4: thread serial number
802 // U4: stack trace serial number
803 case HPROF_ROOT_THREAD_OBJECT:
804 rec->AddU1(heapTag);
805 rec->AddId((HprofObjectId)obj);
806 rec->AddU4(gc_thread_serial_number_);
807 rec->AddU4((uint32_t)-1); //xxx
808 break;
809
Elliott Hughes73e66f72012-05-09 09:34:45 -0700810 case HPROF_CLASS_DUMP:
811 case HPROF_INSTANCE_DUMP:
812 case HPROF_OBJECT_ARRAY_DUMP:
813 case HPROF_PRIMITIVE_ARRAY_DUMP:
814 case HPROF_HEAP_DUMP_INFO:
815 case HPROF_PRIMITIVE_ARRAY_NODATA_DUMP:
816 // Ignored.
817 break;
818
819 case HPROF_ROOT_FINALIZING:
820 case HPROF_ROOT_REFERENCE_CLEANUP:
821 case HPROF_UNREACHABLE:
822 LOG(FATAL) << "obsolete tag " << static_cast<int>(heapTag);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500823 break;
824 }
825
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700826 ++objects_in_segment_;
Elliott Hughes73e66f72012-05-09 09:34:45 -0700827 return 0;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500828}
829
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800830static int StackTraceSerialNumber(const mirror::Object* /*obj*/) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500831 return HPROF_NULL_STACK_TRACE;
832}
833
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800834int Hprof::DumpHeapObject(mirror::Object* obj) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700835 HprofRecord* rec = &current_record_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500836 HprofHeapId desiredHeap = false ? HPROF_HEAP_ZYGOTE : HPROF_HEAP_APP; // TODO: zygote objects?
837
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700838 if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->Size() >= BYTES_PER_SEGMENT) {
839 StartNewHeapDumpSegment();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500840 }
841
842 if (desiredHeap != current_heap_) {
843 HprofStringId nameId;
844
845 // This object is in a different heap than the current one.
846 // Emit a HEAP_DUMP_INFO tag to change heaps.
847 rec->AddU1(HPROF_HEAP_DUMP_INFO);
848 rec->AddU4((uint32_t)desiredHeap); // uint32_t: heap id
849 switch (desiredHeap) {
850 case HPROF_HEAP_APP:
851 nameId = LookupStringId("app");
852 break;
853 case HPROF_HEAP_ZYGOTE:
854 nameId = LookupStringId("zygote");
855 break;
856 default:
857 // Internal error
858 LOG(ERROR) << "Unexpected desiredHeap";
859 nameId = LookupStringId("<ILLEGAL>");
860 break;
861 }
862 rec->AddId(nameId);
863 current_heap_ = desiredHeap;
864 }
865
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800866 mirror::Class* c = obj->GetClass();
Elliott Hughese84278b2012-03-22 10:06:53 -0700867 if (c == NULL) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500868 // This object will bother HprofReader, because it has a NULL
869 // class, so just don't dump it. It could be
870 // gDvm.unlinkedJavaLangClass or it could be an object just
871 // allocated which hasn't been initialized yet.
872 } else {
873 if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800874 mirror::Class* thisClass = obj->AsClass();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500875 // obj is a ClassObject.
876 size_t sFieldCount = thisClass->NumStaticFields();
877 if (sFieldCount != 0) {
878 int byteLength = sFieldCount*sizeof(JValue); // TODO bogus; fields are packed
879 // Create a byte array to reflect the allocation of the
880 // StaticField array at the end of this class.
881 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
882 rec->AddId(CLASS_STATICS_ID(obj));
883 rec->AddU4(StackTraceSerialNumber(obj));
884 rec->AddU4(byteLength);
885 rec->AddU1(hprof_basic_byte);
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700886 for (int i = 0; i < byteLength; ++i) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500887 rec->AddU1(0);
888 }
889 }
890
891 rec->AddU1(HPROF_CLASS_DUMP);
892 rec->AddId(LookupClassId(thisClass));
893 rec->AddU4(StackTraceSerialNumber(thisClass));
894 rec->AddId(LookupClassId(thisClass->GetSuperClass()));
895 rec->AddId((HprofObjectId)thisClass->GetClassLoader());
896 rec->AddId((HprofObjectId)0); // no signer
897 rec->AddId((HprofObjectId)0); // no prot domain
898 rec->AddId((HprofId)0); // reserved
899 rec->AddId((HprofId)0); // reserved
Elliott Hughesdbb40792011-11-18 17:05:22 -0800900 if (thisClass->IsClassClass()) {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500901 // ClassObjects have their static fields appended, so aren't all the same size.
902 // But they're at least this size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800903 rec->AddU4(sizeof(mirror::Class)); // instance size
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500904 } else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) {
905 rec->AddU4(0);
906 } else {
907 rec->AddU4(thisClass->GetObjectSize()); // instance size
908 }
909
910 rec->AddU2(0); // empty const pool
911
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800912 FieldHelper fh;
913
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500914 // Static fields
915 if (sFieldCount == 0) {
916 rec->AddU2((uint16_t)0);
917 } else {
918 rec->AddU2((uint16_t)(sFieldCount+1));
919 rec->AddId(LookupStringId(STATIC_OVERHEAD_NAME));
920 rec->AddU1(hprof_basic_object);
921 rec->AddId(CLASS_STATICS_ID(obj));
922
923 for (size_t i = 0; i < sFieldCount; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800924 mirror::Field* f = thisClass->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800925 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500926
927 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800928 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
929 rec->AddId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500930 rec->AddU1(t);
931 if (size == 1) {
932 rec->AddU1(static_cast<uint8_t>(f->Get32(NULL)));
933 } else if (size == 2) {
934 rec->AddU2(static_cast<uint16_t>(f->Get32(NULL)));
935 } else if (size == 4) {
936 rec->AddU4(f->Get32(NULL));
937 } else if (size == 8) {
938 rec->AddU8(f->Get64(NULL));
939 } else {
940 CHECK(false);
941 }
942 }
943 }
944
945 // Instance fields for this class (no superclass fields)
946 int iFieldCount = thisClass->IsObjectClass() ? 0 : thisClass->NumInstanceFields();
947 rec->AddU2((uint16_t)iFieldCount);
948 for (int i = 0; i < iFieldCount; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800949 mirror::Field* f = thisClass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800950 fh.ChangeField(f);
951 HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), NULL);
952 rec->AddId(LookupStringId(fh.GetName()));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500953 rec->AddU1(t);
954 }
Elliott Hughese84278b2012-03-22 10:06:53 -0700955 } else if (c->IsArrayClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800956 const mirror::Array* aobj = obj->AsArray();
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500957 uint32_t length = aobj->GetLength();
958
959 if (obj->IsObjectArray()) {
960 // obj is an object array.
961 rec->AddU1(HPROF_OBJECT_ARRAY_DUMP);
962
963 rec->AddId((HprofObjectId)obj);
964 rec->AddU4(StackTraceSerialNumber(obj));
965 rec->AddU4(length);
Elliott Hughese84278b2012-03-22 10:06:53 -0700966 rec->AddId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500967
968 // Dump the elements, which are always objects or NULL.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800969 rec->AddIdList((const HprofObjectId*)aobj->GetRawData(sizeof(mirror::Object*)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500970 } else {
971 size_t size;
Elliott Hughese84278b2012-03-22 10:06:53 -0700972 HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500973
974 // obj is a primitive array.
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500975 rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500976
977 rec->AddId((HprofObjectId)obj);
978 rec->AddU4(StackTraceSerialNumber(obj));
979 rec->AddU4(length);
980 rec->AddU1(t);
981
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500982 // Dump the raw, packed element values.
983 if (size == 1) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700984 rec->AddU1List((const uint8_t*)aobj->GetRawData(sizeof(uint8_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500985 } else if (size == 2) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700986 rec->AddU2List((const uint16_t*)(void*)aobj->GetRawData(sizeof(uint16_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500987 } else if (size == 4) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700988 rec->AddU4List((const uint32_t*)(void*)aobj->GetRawData(sizeof(uint32_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500989 } else if (size == 8) {
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -0700990 rec->AddU8List((const uint64_t*)aobj->GetRawData(sizeof(uint64_t)), length);
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500991 }
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500992 }
993 } else {
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500994 // obj is an instance object.
995 rec->AddU1(HPROF_INSTANCE_DUMP);
996 rec->AddId((HprofObjectId)obj);
997 rec->AddU4(StackTraceSerialNumber(obj));
Elliott Hughese84278b2012-03-22 10:06:53 -0700998 rec->AddId(LookupClassId(c));
Jesse Wilson0c54ac12011-11-09 15:14:05 -0500999
1000 // Reserve some space for the length of the instance data, which we won't
1001 // know until we're done writing it.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001002 size_t size_patch_offset = rec->Size();
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001003 rec->AddU4(0x77777777);
1004
1005 // Write the instance data; fields for this class, followed by super class fields,
1006 // and so on. Don't write the klass or monitor fields of Object.class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001007 const mirror::Class* sclass = c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001008 FieldHelper fh;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001009 while (!sclass->IsObjectClass()) {
1010 int ifieldCount = sclass->NumInstanceFields();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001011 for (int i = 0; i < ifieldCount; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001012 mirror::Field* f = sclass->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001013 fh.ChangeField(f);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001014 size_t size;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001015 SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001016 if (size == 1) {
1017 rec->AddU1(f->Get32(obj));
1018 } else if (size == 2) {
1019 rec->AddU2(f->Get32(obj));
1020 } else if (size == 4) {
1021 rec->AddU4(f->Get32(obj));
1022 } else if (size == 8) {
1023 rec->AddU8(f->Get64(obj));
1024 } else {
1025 CHECK(false);
1026 }
1027 }
1028
1029 sclass = sclass->GetSuperClass();
1030 }
1031
1032 // Patch the instance field length.
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001033 rec->UpdateU4(size_patch_offset, rec->Size() - (size_patch_offset + 4));
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001034 }
1035 }
1036
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001037 ++objects_in_segment_;
Jesse Wilson0c54ac12011-11-09 15:14:05 -05001038 return 0;
1039}
1040
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001041void Hprof::VisitRoot(const mirror::Object* obj) {
Jesse Wilson0b075f12011-11-09 10:57:41 -05001042 uint32_t threadId = 0; // TODO
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001043 /*RootType*/ size_t type = 0; // TODO
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001044
Jesse Wilson0b075f12011-11-09 10:57:41 -05001045 static const HprofHeapTag xlate[] = {
1046 HPROF_ROOT_UNKNOWN,
1047 HPROF_ROOT_JNI_GLOBAL,
1048 HPROF_ROOT_JNI_LOCAL,
1049 HPROF_ROOT_JAVA_FRAME,
1050 HPROF_ROOT_NATIVE_STACK,
1051 HPROF_ROOT_STICKY_CLASS,
1052 HPROF_ROOT_THREAD_BLOCK,
1053 HPROF_ROOT_MONITOR_USED,
1054 HPROF_ROOT_THREAD_OBJECT,
1055 HPROF_ROOT_INTERNED_STRING,
1056 HPROF_ROOT_FINALIZING,
1057 HPROF_ROOT_DEBUGGER,
1058 HPROF_ROOT_REFERENCE_CLEANUP,
1059 HPROF_ROOT_VM_INTERNAL,
1060 HPROF_ROOT_JNI_MONITOR,
1061 };
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001062
Jesse Wilson0b075f12011-11-09 10:57:41 -05001063 CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag));
1064 if (obj == NULL) {
1065 return;
1066 }
1067 gc_scan_state_ = xlate[type];
1068 gc_thread_serial_number_ = threadId;
1069 MarkRootObject(obj, 0);
1070 gc_scan_state_ = 0;
1071 gc_thread_serial_number_ = 0;
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001072}
1073
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001074// If "direct_to_ddms" is true, the other arguments are ignored, and data is
1075// sent directly to DDMS.
1076// If "fd" is >= 0, the output will be written to that file descriptor.
1077// Otherwise, "filename" is used to create an output file.
1078void DumpHeap(const char* filename, int fd, bool direct_to_ddms) {
1079 CHECK(filename != NULL);
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001080
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 Runtime::Current()->GetThreadList()->SuspendAll();
Elliott Hughesdcfdd2b2012-07-09 18:27:46 -07001082 Hprof hprof(filename, fd, direct_to_ddms);
1083 hprof.Dump();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001084 Runtime::Current()->GetThreadList()->ResumeAll();
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001085}
1086
1087} // namespace hprof
1088
1089} // namespace art