1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Preparation and completion of hprof data generation. The output is
* written into two files and then combined. This is necessary because
* we generate some of the data (strings and classes) while we dump the
* heap, and some analysis tools require that the class and string data
* appear first.
*/
#include "hprof.h"
#include "class_linker.h"
#include "debugger.h"
#include "heap.h"
#include "logging.h"
#include "object.h"
#include "object_utils.h"
#include "scoped_heap_lock.h"
#include "stringprintf.h"
#include <cutils/open_memstream.h>
#include <sys/uio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
namespace art {
namespace hprof {
#define HPROF_MAGIC_STRING "JAVA PROFILE 1.0.3"
/*
* Initialize an Hprof.
*/
Hprof::Hprof(const char* outputFileName, int fd, bool writeHeader, bool directToDdms)
: current_record_(),
gc_thread_serial_number_(0),
gc_scan_state_(0),
current_heap_(HPROF_HEAP_DEFAULT),
objects_in_segment_(0),
direct_to_ddms_(0),
file_name_(outputFileName),
file_data_ptr_(NULL),
file_data_size_(0),
mem_fp_(NULL),
fd_(0),
next_string_id_(0x400000) {
// Have to do this here, because it must happen after we
// memset the struct (want to treat file_data_ptr_/file_data_size_
// as read-only while the file is open).
FILE *fp = open_memstream(&file_data_ptr_, &file_data_size_);
if (fp == NULL) {
// not expected
LOG(ERROR) << StringPrintf("hprof: open_memstream failed: %s", strerror(errno));
CHECK(false);
}
direct_to_ddms_ = directToDdms;
mem_fp_ = fp;
fd_ = fd;
current_record_.alloc_length_ = 128;
current_record_.body_ = (unsigned char*)malloc(current_record_.alloc_length_);
// TODO check for/return an error
if (writeHeader) {
char magic[] = HPROF_MAGIC_STRING;
unsigned char buf[4];
// Write the file header.
// U1: NUL-terminated magic string.
fwrite(magic, 1, sizeof(magic), fp);
// U4: size of identifiers. We're using addresses as IDs, so make sure a pointer fits.
U4_TO_BUF_BE(buf, 0, sizeof(void *));
fwrite(buf, 1, sizeof(uint32_t), fp);
// The current time, in milliseconds since 0:00 GMT, 1/1/70.
struct timeval now;
uint64_t nowMs;
if (gettimeofday(&now, NULL) < 0) {
nowMs = 0;
} else {
nowMs = (uint64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
}
// U4: high word of the 64-bit time.
U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs >> 32));
fwrite(buf, 1, sizeof(uint32_t), fp);
// U4: low word of the 64-bit time.
U4_TO_BUF_BE(buf, 0, (uint32_t)(nowMs & 0xffffffffULL));
fwrite(buf, 1, sizeof(uint32_t), fp); //xxx fix the time
}
}
int Hprof::StartNewRecord(uint8_t tag, uint32_t time) {
HprofRecord *rec = ¤t_record_;
int err = rec->Flush(mem_fp_);
if (err != 0) {
return err;
} else if (rec->dirty_) {
return UNIQUE_ERROR();
}
rec->dirty_ = true;
rec->tag_ = tag;
rec->time_ = time;
rec->length_ = 0;
return 0;
}
int Hprof::FlushCurrentRecord() {
return current_record_.Flush(mem_fp_);
}
// Set DUMP_PRIM_DATA to 1 if you want to include the contents
// of primitive arrays (byte arrays, character arrays, etc.)
// in heap dumps. This can be a large amount of data.
#define DUMP_PRIM_DATA 1
#define OBJECTS_PER_SEGMENT ((size_t)128)
#define BYTES_PER_SEGMENT ((size_t)4096)
// The static field-name for the synthetic object generated to account
// for class static overhead.
#define STATIC_OVERHEAD_NAME "$staticOverhead"
// The ID for the synthetic object generated to account for class static overhead.
#define CLASS_STATICS_ID(c) ((HprofObjectId)(((uint32_t)(c)) | 1))
HprofBasicType Hprof::SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut) {
char c = sig[0];
HprofBasicType ret;
size_t size;
switch (c) {
case '[':
case 'L': ret = hprof_basic_object; size = 4; break;
case 'Z': ret = hprof_basic_boolean; size = 1; break;
case 'C': ret = hprof_basic_char; size = 2; break;
case 'F': ret = hprof_basic_float; size = 4; break;
case 'D': ret = hprof_basic_double; size = 8; break;
case 'B': ret = hprof_basic_byte; size = 1; break;
case 'S': ret = hprof_basic_short; size = 2; break;
default: CHECK(false);
case 'I': ret = hprof_basic_int; size = 4; break;
case 'J': ret = hprof_basic_long; size = 8; break;
}
if (sizeOut != NULL) {
*sizeOut = size;
}
return ret;
}
HprofBasicType Hprof::PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t *sizeOut) {
HprofBasicType ret;
size_t size;
switch (prim) {
case Primitive::kPrimBoolean: ret = hprof_basic_boolean; size = 1; break;
case Primitive::kPrimChar: ret = hprof_basic_char; size = 2; break;
case Primitive::kPrimFloat: ret = hprof_basic_float; size = 4; break;
case Primitive::kPrimDouble: ret = hprof_basic_double; size = 8; break;
case Primitive::kPrimByte: ret = hprof_basic_byte; size = 1; break;
case Primitive::kPrimShort: ret = hprof_basic_short; size = 2; break;
default: CHECK(false);
case Primitive::kPrimInt: ret = hprof_basic_int; size = 4; break;
case Primitive::kPrimLong: ret = hprof_basic_long; size = 8; break;
}
if (sizeOut != NULL) {
*sizeOut = size;
}
return ret;
}
// Always called when marking objects, but only does
// something when ctx->gc_scan_state_ is non-zero, which is usually
// only true when marking the root set or unreachable
// objects. Used to add rootset references to obj.
int Hprof::MarkRootObject(const Object *obj, jobject jniObj) {
HprofRecord *rec = ¤t_record_;
int err; // TODO: we may return this uninitialized
HprofHeapTag heapTag = (HprofHeapTag)gc_scan_state_;
if (heapTag == 0) {
return 0;
}
if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->length_ >= BYTES_PER_SEGMENT) {
// This flushes the old segment and starts a new one.
StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
objects_in_segment_ = 0;
}
switch (heapTag) {
// ID: object ID
case HPROF_ROOT_UNKNOWN:
case HPROF_ROOT_STICKY_CLASS:
case HPROF_ROOT_MONITOR_USED:
case HPROF_ROOT_INTERNED_STRING:
case HPROF_ROOT_FINALIZING:
case HPROF_ROOT_DEBUGGER:
case HPROF_ROOT_REFERENCE_CLEANUP:
case HPROF_ROOT_VM_INTERNAL:
rec->AddU1(heapTag);
rec->AddId((HprofObjectId)obj);
break;
// ID: object ID
// ID: JNI global ref ID
case HPROF_ROOT_JNI_GLOBAL:
rec->AddU1(heapTag);
rec->AddId((HprofObjectId)obj);
rec->AddId((HprofId)jniObj);
break;
// ID: object ID
// U4: thread serial number
// U4: frame number in stack trace (-1 for empty)
case HPROF_ROOT_JNI_LOCAL:
case HPROF_ROOT_JNI_MONITOR:
case HPROF_ROOT_JAVA_FRAME:
rec->AddU1(heapTag);
rec->AddId((HprofObjectId)obj);
rec->AddU4(gc_thread_serial_number_);
rec->AddU4((uint32_t)-1);
break;
// ID: object ID
// U4: thread serial number
case HPROF_ROOT_NATIVE_STACK:
case HPROF_ROOT_THREAD_BLOCK:
rec->AddU1(heapTag);
rec->AddId((HprofObjectId)obj);
rec->AddU4(gc_thread_serial_number_);
break;
// ID: thread object ID
// U4: thread serial number
// U4: stack trace serial number
case HPROF_ROOT_THREAD_OBJECT:
rec->AddU1(heapTag);
rec->AddId((HprofObjectId)obj);
rec->AddU4(gc_thread_serial_number_);
rec->AddU4((uint32_t)-1); //xxx
break;
default:
err = 0;
break;
}
objects_in_segment_++;
return err;
}
int Hprof::StackTraceSerialNumber(const void* /*obj*/) {
return HPROF_NULL_STACK_TRACE;
}
int Hprof::DumpHeapObject(const Object* obj) {
HprofRecord *rec = ¤t_record_;
HprofHeapId desiredHeap = false ? HPROF_HEAP_ZYGOTE : HPROF_HEAP_APP; // TODO: zygote objects?
if (objects_in_segment_ >= OBJECTS_PER_SEGMENT || rec->length_ >= BYTES_PER_SEGMENT) {
// This flushes the old segment and starts a new one.
StartNewRecord(HPROF_TAG_HEAP_DUMP_SEGMENT, HPROF_TIME);
objects_in_segment_ = 0;
// Starting a new HEAP_DUMP resets the heap to default.
current_heap_ = HPROF_HEAP_DEFAULT;
}
if (desiredHeap != current_heap_) {
HprofStringId nameId;
// This object is in a different heap than the current one.
// Emit a HEAP_DUMP_INFO tag to change heaps.
rec->AddU1(HPROF_HEAP_DUMP_INFO);
rec->AddU4((uint32_t)desiredHeap); // uint32_t: heap id
switch (desiredHeap) {
case HPROF_HEAP_APP:
nameId = LookupStringId("app");
break;
case HPROF_HEAP_ZYGOTE:
nameId = LookupStringId("zygote");
break;
default:
// Internal error
LOG(ERROR) << "Unexpected desiredHeap";
nameId = LookupStringId("<ILLEGAL>");
break;
}
rec->AddId(nameId);
current_heap_ = desiredHeap;
}
Class* c = obj->GetClass();
if (c == NULL) {
// This object will bother HprofReader, because it has a NULL
// class, so just don't dump it. It could be
// gDvm.unlinkedJavaLangClass or it could be an object just
// allocated which hasn't been initialized yet.
} else {
if (obj->IsClass()) {
Class* thisClass = (Class*)obj;
// obj is a ClassObject.
size_t sFieldCount = thisClass->NumStaticFields();
if (sFieldCount != 0) {
int byteLength = sFieldCount*sizeof(JValue); // TODO bogus; fields are packed
// Create a byte array to reflect the allocation of the
// StaticField array at the end of this class.
rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
rec->AddId(CLASS_STATICS_ID(obj));
rec->AddU4(StackTraceSerialNumber(obj));
rec->AddU4(byteLength);
rec->AddU1(hprof_basic_byte);
for (int i = 0; i < byteLength; i++) {
rec->AddU1(0);
}
}
rec->AddU1(HPROF_CLASS_DUMP);
rec->AddId(LookupClassId(thisClass));
rec->AddU4(StackTraceSerialNumber(thisClass));
rec->AddId(LookupClassId(thisClass->GetSuperClass()));
rec->AddId((HprofObjectId)thisClass->GetClassLoader());
rec->AddId((HprofObjectId)0); // no signer
rec->AddId((HprofObjectId)0); // no prot domain
rec->AddId((HprofId)0); // reserved
rec->AddId((HprofId)0); // reserved
if (thisClass->IsClassClass()) {
// ClassObjects have their static fields appended, so aren't all the same size.
// But they're at least this size.
rec->AddU4(sizeof(Class)); // instance size
} else if (thisClass->IsArrayClass() || thisClass->IsPrimitive()) {
rec->AddU4(0);
} else {
rec->AddU4(thisClass->GetObjectSize()); // instance size
}
rec->AddU2(0); // empty const pool
FieldHelper fh;
// Static fields
if (sFieldCount == 0) {
rec->AddU2((uint16_t)0);
} else {
rec->AddU2((uint16_t)(sFieldCount+1));
rec->AddId(LookupStringId(STATIC_OVERHEAD_NAME));
rec->AddU1(hprof_basic_object);
rec->AddId(CLASS_STATICS_ID(obj));
for (size_t i = 0; i < sFieldCount; ++i) {
Field* f = thisClass->GetStaticField(i);
fh.ChangeField(f);
size_t size;
HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
rec->AddId(LookupStringId(fh.GetName()));
rec->AddU1(t);
if (size == 1) {
rec->AddU1(static_cast<uint8_t>(f->Get32(NULL)));
} else if (size == 2) {
rec->AddU2(static_cast<uint16_t>(f->Get32(NULL)));
} else if (size == 4) {
rec->AddU4(f->Get32(NULL));
} else if (size == 8) {
rec->AddU8(f->Get64(NULL));
} else {
CHECK(false);
}
}
}
// Instance fields for this class (no superclass fields)
int iFieldCount = thisClass->IsObjectClass() ? 0 : thisClass->NumInstanceFields();
rec->AddU2((uint16_t)iFieldCount);
for (int i = 0; i < iFieldCount; ++i) {
Field* f = thisClass->GetInstanceField(i);
fh.ChangeField(f);
HprofBasicType t = SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), NULL);
rec->AddId(LookupStringId(fh.GetName()));
rec->AddU1(t);
}
} else if (c->IsArrayClass()) {
Array *aobj = (Array *)obj;
uint32_t length = aobj->GetLength();
if (obj->IsObjectArray()) {
// obj is an object array.
rec->AddU1(HPROF_OBJECT_ARRAY_DUMP);
rec->AddId((HprofObjectId)obj);
rec->AddU4(StackTraceSerialNumber(obj));
rec->AddU4(length);
rec->AddId(LookupClassId(c));
// Dump the elements, which are always objects or NULL.
rec->AddIdList((const HprofObjectId *)aobj->GetRawData(sizeof(Object*)), length);
} else {
size_t size;
HprofBasicType t = PrimitiveToBasicTypeAndSize(c->GetComponentType()->GetPrimitiveType(), &size);
// obj is a primitive array.
#if DUMP_PRIM_DATA
rec->AddU1(HPROF_PRIMITIVE_ARRAY_DUMP);
#else
rec->AddU1(HPROF_PRIMITIVE_ARRAY_NODATA_DUMP);
#endif
rec->AddId((HprofObjectId)obj);
rec->AddU4(StackTraceSerialNumber(obj));
rec->AddU4(length);
rec->AddU1(t);
#if DUMP_PRIM_DATA
// Dump the raw, packed element values.
if (size == 1) {
rec->AddU1List((const uint8_t *)aobj->GetRawData(sizeof(uint8_t)), length);
} else if (size == 2) {
rec->AddU2List((const uint16_t *)(void *)aobj->GetRawData(sizeof(uint16_t)), length);
} else if (size == 4) {
rec->AddU4List((const uint32_t *)(void *)aobj->GetRawData(sizeof(uint32_t)), length);
} else if (size == 8) {
rec->AddU8List((const uint64_t *)aobj->GetRawData(sizeof(uint64_t)), length);
}
#endif
}
} else {
// obj is an instance object.
rec->AddU1(HPROF_INSTANCE_DUMP);
rec->AddId((HprofObjectId)obj);
rec->AddU4(StackTraceSerialNumber(obj));
rec->AddId(LookupClassId(c));
// Reserve some space for the length of the instance data, which we won't
// know until we're done writing it.
size_t sizePatchOffset = rec->length_;
rec->AddU4(0x77777777);
// Write the instance data; fields for this class, followed by super class fields,
// and so on. Don't write the klass or monitor fields of Object.class.
const Class* sclass = c;
FieldHelper fh;
while (!sclass->IsObjectClass()) {
int ifieldCount = sclass->NumInstanceFields();
for (int i = 0; i < ifieldCount; i++) {
Field* f = sclass->GetInstanceField(i);
fh.ChangeField(f);
size_t size;
SignatureToBasicTypeAndSize(fh.GetTypeDescriptor(), &size);
if (size == 1) {
rec->AddU1(f->Get32(obj));
} else if (size == 2) {
rec->AddU2(f->Get32(obj));
} else if (size == 4) {
rec->AddU4(f->Get32(obj));
} else if (size == 8) {
rec->AddU8(f->Get64(obj));
} else {
CHECK(false);
}
}
sclass = sclass->GetSuperClass();
}
// Patch the instance field length.
size_t savedLen = rec->length_;
rec->length_ = sizePatchOffset;
rec->AddU4(savedLen - (sizePatchOffset + 4));
rec->length_ = savedLen;
}
}
objects_in_segment_++;
return 0;
}
#define kHeadSuffix "-hptemp"
// TODO: use File::WriteFully
int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg) {
while (count != 0) {
ssize_t actual = TEMP_FAILURE_RETRY(write(fd, buf, count));
if (actual < 0) {
int err = errno;
LOG(ERROR) << StringPrintf("%s: write failed: %s", logMsg, strerror(err));
return err;
} else if (actual != (ssize_t) count) {
LOG(DEBUG) << StringPrintf("%s: partial write (will retry): (%d of %zd)",
logMsg, (int) actual, count);
buf = (const void*) (((const uint8_t*) buf) + actual);
}
count -= actual;
}
return 0;
}
/*
* Finish up the hprof dump. Returns true on success.
*/
bool Hprof::Finish() {
// flush the "tail" portion of the output
StartNewRecord(HPROF_TAG_HEAP_DUMP_END, HPROF_TIME);
FlushCurrentRecord();
// create a new Hprof for the start of the file (as opposed to this, which is the tail)
Hprof headCtx(file_name_.c_str(), fd_, true, direct_to_ddms_);
headCtx.classes_ = classes_;
headCtx.strings_ = strings_;
LOG(INFO) << StringPrintf("hprof: dumping heap strings to \"%s\".", file_name_.c_str());
headCtx.DumpStrings();
headCtx.DumpClasses();
// write a dummy stack trace record so the analysis tools don't freak out
headCtx.StartNewRecord(HPROF_TAG_STACK_TRACE, HPROF_TIME);
headCtx.current_record_.AddU4(HPROF_NULL_STACK_TRACE);
headCtx.current_record_.AddU4(HPROF_NULL_THREAD);
headCtx.current_record_.AddU4(0); // no frames
headCtx.FlushCurrentRecord();
// flush to ensure memstream pointer and size are updated
fflush(headCtx.mem_fp_);
fflush(mem_fp_);
if (direct_to_ddms_) {
// send the data off to DDMS
struct iovec iov[2];
iov[0].iov_base = headCtx.file_data_ptr_;
iov[0].iov_len = headCtx.file_data_size_;
iov[1].iov_base = file_data_ptr_;
iov[1].iov_len = file_data_size_;
Dbg::DdmSendChunkV(CHUNK_TYPE("HPDS"), iov, 2);
} else {
// open the output file, and copy the head and tail to it.
CHECK_EQ(headCtx.fd_, fd_);
int outFd;
if (headCtx.fd_ >= 0) {
outFd = dup(headCtx.fd_);
if (outFd < 0) {
LOG(ERROR) << StringPrintf("dup(%d) failed: %s", headCtx.fd_, strerror(errno));
// continue to fail-handler below
}
} else {
outFd = open(file_name_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (outFd < 0) {
LOG(ERROR) << StringPrintf("can't open %s: %s", headCtx.file_name_.c_str(), strerror(errno));
// continue to fail-handler below
}
}
if (outFd < 0) {
return false;
}
int result = sysWriteFully(outFd, headCtx.file_data_ptr_,
headCtx.file_data_size_, "hprof-head");
result |= sysWriteFully(outFd, file_data_ptr_, file_data_size_, "hprof-tail");
close(outFd);
if (result != 0) {
return false;
}
}
// throw out a log message for the benefit of "runhat"
LOG(INFO) << "hprof: heap dump completed (" << PrettySize(headCtx.file_data_size_ + file_data_size_ + 1023) << ")";
return true;
}
Hprof::~Hprof() {
// we don't own ctx->fd_, do not close
if (mem_fp_ != NULL) {
fclose(mem_fp_);
}
free(current_record_.body_);
free(file_data_ptr_);
}
void Hprof::VisitRoot(const Object* obj) {
uint32_t threadId = 0; // TODO
/*RootType */ size_t type = 0; // TODO
static const HprofHeapTag xlate[] = {
HPROF_ROOT_UNKNOWN,
HPROF_ROOT_JNI_GLOBAL,
HPROF_ROOT_JNI_LOCAL,
HPROF_ROOT_JAVA_FRAME,
HPROF_ROOT_NATIVE_STACK,
HPROF_ROOT_STICKY_CLASS,
HPROF_ROOT_THREAD_BLOCK,
HPROF_ROOT_MONITOR_USED,
HPROF_ROOT_THREAD_OBJECT,
HPROF_ROOT_INTERNED_STRING,
HPROF_ROOT_FINALIZING,
HPROF_ROOT_DEBUGGER,
HPROF_ROOT_REFERENCE_CLEANUP,
HPROF_ROOT_VM_INTERNAL,
HPROF_ROOT_JNI_MONITOR,
};
CHECK_LT(type, sizeof(xlate) / sizeof(HprofHeapTag));
if (obj == NULL) {
return;
}
gc_scan_state_ = xlate[type];
gc_thread_serial_number_ = threadId;
MarkRootObject(obj, 0);
gc_scan_state_ = 0;
gc_thread_serial_number_ = 0;
}
HprofStringId Hprof::LookupStringId(String* string) {
return LookupStringId(string->ToModifiedUtf8());
}
HprofStringId Hprof::LookupStringId(const char* string) {
return LookupStringId(std::string(string));
}
HprofStringId Hprof::LookupStringId(std::string string) {
if (strings_.find(string) == strings_.end()) {
strings_[string] = next_string_id_++;
}
return strings_[string];
}
int Hprof::DumpStrings() {
HprofRecord *rec = ¤t_record_;
for (StringMapIterator it = strings_.begin(); it != strings_.end(); ++it) {
std::string string((*it).first);
size_t id = (*it).second;
int err = StartNewRecord(HPROF_TAG_STRING, HPROF_TIME);
if (err != 0) {
return err;
}
// STRING format:
// ID: ID for this string
// U1*: UTF8 characters for string (NOT NULL terminated)
// (the record format encodes the length)
err = rec->AddU4(id);
if (err != 0) {
return err;
}
err = rec->AddUtf8String(string.c_str());
if (err != 0) {
return err;
}
}
return 0;
}
HprofStringId Hprof::LookupClassNameId(Class* c) {
return LookupStringId(PrettyDescriptor(c));
}
HprofClassObjectId Hprof::LookupClassId(Class* c) {
if (c == NULL) {
// c is the superclass of java.lang.Object or a primitive
return (HprofClassObjectId)0;
}
std::pair<ClassSetIterator, bool> result = classes_.insert(c);
Class* present = *result.first;
// Make sure that we've assigned a string ID for this class' name
LookupClassNameId(c);
CHECK_EQ(present, c);
return (HprofStringId) present;
}
int Hprof::DumpClasses() {
HprofRecord *rec = ¤t_record_;
uint32_t nextSerialNumber = 1;
for (ClassSetIterator it = classes_.begin(); it != classes_.end(); ++it) {
Class* c = *it;
CHECK(c != NULL);
int err = StartNewRecord(HPROF_TAG_LOAD_CLASS, HPROF_TIME);
if (err != 0) {
return err;
}
// LOAD CLASS format:
// U4: class serial number (always > 0)
// ID: class object ID. We use the address of the class object structure as its ID.
// U4: stack trace serial number
// ID: class name string ID
rec->AddU4(nextSerialNumber++);
rec->AddId((HprofClassObjectId) c);
rec->AddU4(HPROF_NULL_STACK_TRACE);
rec->AddId(LookupClassNameId(c));
}
return 0;
}
void HprofRootVisitor(const Object* obj, void* arg) {
CHECK(arg != NULL);
Hprof* hprof = (Hprof*)arg;
hprof->VisitRoot(obj);
}
void HprofBitmapCallback(Object *obj, void *arg) {
CHECK(obj != NULL);
CHECK(arg != NULL);
Hprof *hprof = (Hprof*)arg;
hprof->DumpHeapObject(obj);
}
/*
* Walk the roots and heap writing heap information to the specified
* file.
*
* If "fd" is >= 0, the output will be written to that file descriptor.
* Otherwise, "file_name_" is used to create an output file.
*
* If "direct_to_ddms_" is set, the other arguments are ignored, and data is
* sent directly to DDMS.
*
* Returns 0 on success, or an error code on failure.
*/
int DumpHeap(const char* fileName, int fd, bool directToDdms) {
CHECK(fileName != NULL);
ScopedHeapLock heap_lock;
ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
ThreadList* thread_list = Runtime::Current()->GetThreadList();
thread_list->SuspendAll();
Runtime* runtime = Runtime::Current();
Hprof hprof(fileName, fd, false, directToDdms);
runtime->VisitRoots(HprofRootVisitor, &hprof);
runtime->GetHeap()->GetLiveBits()->Walk(HprofBitmapCallback, &hprof);
// TODO: write a HEAP_SUMMARY record
int success = hprof.Finish() ? 0 : -1;
thread_list->ResumeAll();
return success;
}
} // namespace hprof
} // namespace art
|