33 #include "oops/objArrayKlass.hpp"
34 #include "runtime/javaCalls.hpp"
35 #include "runtime/jniHandles.hpp"
36 #include "runtime/reflectionUtils.hpp"
37 #include "runtime/vframe.hpp"
38 #include "runtime/vmThread.hpp"
39 #include "runtime/vm_operations.hpp"
40 #include "services/heapDumper.hpp"
41 #include "services/threadService.hpp"
42 #include "utilities/ostream.hpp"
43 #include "utilities/macros.hpp"
44 #if INCLUDE_ALL_GCS
45 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
46 #endif // INCLUDE_ALL_GCS
47
48 /*
49 * HPROF binary format - description copied from:
50 * src/share/demo/jvmti/hprof/hprof_io.c
51 *
52 *
53 * header "JAVA PROFILE 1.0.1" or "JAVA PROFILE 1.0.2"
54 * (0-terminated)
55 *
56 * u4 size of identifiers. Identifiers are used to represent
57 * UTF8 strings, objects, stack traces, etc. They usually
58 * have the same size as host pointers. For example, on
59 * Solaris and Win32, the size is 4.
60 * u4 high word
61 * u4 low word number of milliseconds since 0:00 GMT, 1/1/70
62 * [record]* a sequence of records.
63 *
64 *
65 * Record format:
66 *
67 * u1 a TAG denoting the type of the record
68 * u4 number of *microseconds* since the time stamp in the
69 * header. (wraps around in a little more than an hour)
70 * u4 number of bytes *remaining* in the record. Note that
71 * this number excludes the tag and the length field itself.
72 * [u1]* BODY of the record (a sequence of bytes)
73 *
74 *
365 enum {
366 STACK_TRACE_ID = 1,
367 INITIAL_CLASS_COUNT = 200
368 };
369
370 // Supports I/O operations on a dump file
371
372 class DumpWriter : public StackObj {
373 private:
374 enum {
375 io_buffer_size = 8*M
376 };
377
378 int _fd; // file descriptor (-1 if dump file not open)
379 julong _bytes_written; // number of byte written to dump file
380
381 char* _buffer; // internal buffer
382 size_t _size;
383 size_t _pos;
384
385 char* _error; // error message when I/O fails
386
387 void set_file_descriptor(int fd) { _fd = fd; }
388 int file_descriptor() const { return _fd; }
389
390 char* buffer() const { return _buffer; }
391 size_t buffer_size() const { return _size; }
392 size_t position() const { return _pos; }
393 void set_position(size_t pos) { _pos = pos; }
394
395 void set_error(const char* error) { _error = (char*)os::strdup(error); }
396
397 // all I/O go through this function
398 void write_internal(void* s, size_t len);
399
400 public:
401 DumpWriter(const char* path);
402 ~DumpWriter();
403
404 void close();
405 bool is_open() const { return file_descriptor() >= 0; }
406 void flush();
407
408 // total number of bytes written to the disk
409 julong bytes_written() const { return _bytes_written; }
410
411 // adjust the number of bytes written to disk (used to keep the count
412 // of the number of bytes written in case of rewrites)
413 void adjust_bytes_written(jlong n) { _bytes_written += n; }
414
415 // number of (buffered) bytes as yet unwritten to the dump file
416 size_t bytes_unwritten() const { return position(); }
417
418 char* error() const { return _error; }
419
420 jlong current_offset();
421 void seek_to_offset(jlong pos);
422
423 // writer functions
424 void write_raw(void* s, size_t len);
425 void write_u1(u1 x) { write_raw((void*)&x, 1); }
426 void write_u2(u2 x);
427 void write_u4(u4 x);
429 void write_objectID(oop o);
430 void write_symbolID(Symbol* o);
431 void write_classID(Klass* k);
432 void write_id(u4 x);
433 };
434
435 DumpWriter::DumpWriter(const char* path) {
436 // try to allocate an I/O buffer of io_buffer_size. If there isn't
437 // sufficient memory then reduce size until we can allocate something.
438 _size = io_buffer_size;
439 do {
440 _buffer = (char*)os::malloc(_size, mtInternal);
441 if (_buffer == NULL) {
442 _size = _size >> 1;
443 }
444 } while (_buffer == NULL && _size > 0);
445 assert((_size > 0 && _buffer != NULL) || (_size == 0 && _buffer == NULL), "sanity check");
446 _pos = 0;
447 _error = NULL;
448 _bytes_written = 0L;
449 _fd = os::create_binary_file(path, false); // don't replace existing file
450
451 // if the open failed we record the error
452 if (_fd < 0) {
453 _error = (char*)os::strdup(strerror(errno));
454 }
455 }
456
457 DumpWriter::~DumpWriter() {
458 // flush and close dump file
459 if (is_open()) {
460 close();
461 }
462 if (_buffer != NULL) os::free(_buffer);
463 if (_error != NULL) os::free(_error);
464 }
465
466 // closes dump file (if open)
467 void DumpWriter::close() {
468 // flush and close dump file
469 if (is_open()) {
470 flush();
471 ::close(file_descriptor());
472 set_file_descriptor(-1);
473 }
474 }
475
476 // write directly to the file
477 void DumpWriter::write_internal(void* s, size_t len) {
478 if (is_open()) {
479 const char* pos = (char*)s;
480 ssize_t n = 0;
481 while (len > 0) {
482 uint tmp = (uint)MIN2(len, (size_t)UINT_MAX);
483 n = ::write(file_descriptor(), pos, tmp);
484
485 if (n < 0) {
486 set_error(strerror(errno));
487 ::close(file_descriptor());
488 set_file_descriptor(-1);
489 return;
490 }
491
492 _bytes_written += n;
493 pos += n;
494 len -= n;
495 }
624 static void dump_static_fields(DumpWriter* writer, Klass* k);
625 // dump the raw values of the instance fields of the given object
626 static void dump_instance_fields(DumpWriter* writer, oop o);
627 // dumps the definition of the instance fields for a given class
628 static void dump_instance_field_descriptors(DumpWriter* writer, Klass* k);
629 // creates HPROF_GC_INSTANCE_DUMP record for the given object
630 static void dump_instance(DumpWriter* writer, oop o);
631 // creates HPROF_GC_CLASS_DUMP record for the given class and each of its
632 // array classes
633 static void dump_class_and_array_classes(DumpWriter* writer, Klass* k);
634 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
635 // class (and each multi-dimensional array class too)
636 static void dump_basic_type_array_class(DumpWriter* writer, Klass* k);
637
638 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
639 static void dump_object_array(DumpWriter* writer, objArrayOop array);
640 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
641 static void dump_prim_array(DumpWriter* writer, typeArrayOop array);
642 // create HPROF_FRAME record for the given method and bci
643 static void dump_stack_frame(DumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
644 };
645
646 // write a header of the given type
647 void DumperSupport:: write_header(DumpWriter* writer, hprofTag tag, u4 len) {
648 writer->write_u1((u1)tag);
649 writer->write_u4(0); // current ticks
650 writer->write_u4(len);
651 }
652
653 // returns hprof tag for the given type signature
654 hprofTag DumperSupport::sig2tag(Symbol* sig) {
655 switch (sig->byte_at(0)) {
656 case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
657 case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
658 case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
659 case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
660 case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
661 case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
662 case JVM_SIGNATURE_INT : return HPROF_INT;
663 case JVM_SIGNATURE_LONG : return HPROF_LONG;
1030 Klass* java_super = klass->java_super();
1031 assert(java_super != NULL, "checking");
1032 writer->write_classID(java_super);
1033
1034 writer->write_objectID(oop(NULL)); // loader
1035 writer->write_objectID(oop(NULL)); // signers
1036 writer->write_objectID(oop(NULL)); // protection domain
1037
1038 writer->write_objectID(oop(NULL)); // reserved
1039 writer->write_objectID(oop(NULL));
1040 writer->write_u4(0); // instance size
1041 writer->write_u2(0); // constant pool
1042 writer->write_u2(0); // static fields
1043 writer->write_u2(0); // instance fields
1044
1045 // get the array class for the next rank
1046 k = klass->array_klass_or_null();
1047 }
1048 }
1049
1050 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1051 void DumperSupport::dump_object_array(DumpWriter* writer, objArrayOop array) {
1052
1053 writer->write_u1(HPROF_GC_OBJ_ARRAY_DUMP);
1054 writer->write_objectID(array);
1055 writer->write_u4(STACK_TRACE_ID);
1056 writer->write_u4((u4)array->length());
1057
1058 // array class ID
1059 writer->write_classID(array->klass());
1060
1061 // [id]* elements
1062 for (int index=0; index<array->length(); index++) {
1063 oop o = array->obj_at(index);
1064 writer->write_objectID(o);
1065 }
1066 }
1067
1068 #define WRITE_ARRAY(Array, Type, Size) \
1069 for (int i=0; i<Array->length(); i++) { writer->write_##Size((Size)array->Type##_at(i)); }
1070
1071
1072 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1073 void DumperSupport::dump_prim_array(DumpWriter* writer, typeArrayOop array) {
1074 BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1075
1076 writer->write_u1(HPROF_GC_PRIM_ARRAY_DUMP);
1077 writer->write_objectID(array);
1078 writer->write_u4(STACK_TRACE_ID);
1079 writer->write_u4((u4)array->length());
1080 writer->write_u1(type2tag(type));
1081
1082 // nothing to copy
1083 if (array->length() == 0) {
1084 return;
1085 }
1086
1087 // If the byte ordering is big endian then we can copy most types directly
1088 u4 length_in_bytes = (u4)array->length() * type2aelembytes(type);
1089
1090 switch (type) {
1091 case T_INT : {
1092 if (Bytes::is_Java_byte_ordering_different()) {
1093 WRITE_ARRAY(array, int, u4);
1094 } else {
1095 writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes);
1096 }
1097 break;
1098 }
1099 case T_BYTE : {
1100 writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes);
1101 break;
1102 }
1103 case T_CHAR : {
1104 if (Bytes::is_Java_byte_ordering_different()) {
1105 WRITE_ARRAY(array, char, u2);
1106 } else {
1107 writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes);
1108 }
1109 break;
1110 }
1111 case T_SHORT : {
1112 if (Bytes::is_Java_byte_ordering_different()) {
1113 WRITE_ARRAY(array, short, u2);
1114 } else {
1115 writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes);
1116 }
1117 break;
1118 }
1119 case T_BOOLEAN : {
1120 if (Bytes::is_Java_byte_ordering_different()) {
1121 WRITE_ARRAY(array, bool, u1);
1122 } else {
1123 writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes);
1124 }
1125 break;
1126 }
1127 case T_LONG : {
1128 if (Bytes::is_Java_byte_ordering_different()) {
1129 WRITE_ARRAY(array, long, u8);
1130 } else {
1131 writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes);
1132 }
1133 break;
1134 }
1135
1136 // handle float/doubles in a special value to ensure than NaNs are
1137 // written correctly. TO DO: Check if we can avoid this on processors that
1138 // use IEEE 754.
1139
1140 case T_FLOAT : {
1141 for (int i=0; i<array->length(); i++) {
1142 dump_float( writer, array->float_at(i) );
1143 }
1144 break;
1145 }
1146 case T_DOUBLE : {
1147 for (int i=0; i<array->length(); i++) {
1148 dump_double( writer, array->double_at(i) );
1149 }
1150 break;
1151 }
1152 default : ShouldNotReachHere();
1153 }
1154 }
1155
1156 // create a HPROF_FRAME record of the given Method* and bci
1157 void DumperSupport::dump_stack_frame(DumpWriter* writer,
1158 int frame_serial_num,
1159 int class_serial_num,
1160 Method* m,
1161 int bci) {
1162 int line_number;
1163 if (m->is_native()) {
1164 line_number = -3; // native frame
1165 } else {
1166 line_number = m->line_number_from_bci(bci);
1167 }
1168
1345 } else if (o->is_objArray()) {
1346 // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1347 DumperSupport::dump_object_array(writer(), objArrayOop(o));
1348 mark_end_of_record();
1349 } else if (o->is_typeArray()) {
1350 // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1351 DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1352 mark_end_of_record();
1353 }
1354 }
1355
1356 // The VM operation that performs the heap dump
1357 class VM_HeapDumper : public VM_GC_Operation {
1358 private:
1359 static VM_HeapDumper* _global_dumper;
1360 static DumpWriter* _global_writer;
1361 DumpWriter* _local_writer;
1362 JavaThread* _oome_thread;
1363 Method* _oome_constructor;
1364 bool _gc_before_heap_dump;
1365 bool _is_segmented_dump;
1366 jlong _dump_start;
1367 GrowableArray<Klass*>* _klass_map;
1368 ThreadStackTrace** _stack_traces;
1369 int _num_threads;
1370
1371 // accessors and setters
1372 static VM_HeapDumper* dumper() { assert(_global_dumper != NULL, "Error"); return _global_dumper; }
1373 static DumpWriter* writer() { assert(_global_writer != NULL, "Error"); return _global_writer; }
1374 void set_global_dumper() {
1375 assert(_global_dumper == NULL, "Error");
1376 _global_dumper = this;
1377 }
1378 void set_global_writer() {
1379 assert(_global_writer == NULL, "Error");
1380 _global_writer = _local_writer;
1381 }
1382 void clear_global_dumper() { _global_dumper = NULL; }
1383 void clear_global_writer() { _global_writer = NULL; }
1384
1385 bool is_segmented_dump() const { return _is_segmented_dump; }
1386 void set_segmented_dump() { _is_segmented_dump = true; }
1387 jlong dump_start() const { return _dump_start; }
1388 void set_dump_start(jlong pos);
1389
1390 bool skip_operation() const;
1391
1392 // writes a HPROF_LOAD_CLASS record
1393 static void do_load_class(Klass* k);
1394
1395 // writes a HPROF_GC_CLASS_DUMP record for the given class
1396 // (and each array class too)
1397 static void do_class_dump(Klass* k);
1398
1399 // writes a HPROF_GC_CLASS_DUMP records for a given basic type
1400 // array (and each multi-dimensional array too)
1401 static void do_basic_type_array_class_dump(Klass* k);
1402
1403 // HPROF_GC_ROOT_THREAD_OBJ records
1404 int do_thread(JavaThread* thread, u4 thread_serial_num);
1405 void do_threads();
1406
1407 void add_class_serial_number(Klass* k, int serial_num) {
1408 _klass_map->at_put_grow(serial_num, k);
1409 }
1410
1411 // HPROF_TRACE and HPROF_FRAME records
1412 void dump_stack_traces();
1413
1414 // writes a HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT record
1415 void write_dump_header();
1416
1417 // fixes up the length of the current dump record
1418 void write_current_dump_record_length();
1419
1420 // fixes up the current dump record )and writes HPROF_HEAP_DUMP_END
1421 // record in the case of a segmented heap dump)
1422 void end_of_dump();
1423
1424 public:
1425 VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome) :
1426 VM_GC_Operation(0 /* total collections, dummy, ignored */,
1427 GCCause::_heap_dump /* GC Cause */,
1428 0 /* total full collections, dummy, ignored */,
1429 gc_before_heap_dump) {
1430 _local_writer = writer;
1431 _gc_before_heap_dump = gc_before_heap_dump;
1432 _is_segmented_dump = false;
1433 _dump_start = (jlong)-1;
1434 _klass_map = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, true);
1435 _stack_traces = NULL;
1436 _num_threads = 0;
1437 if (oome) {
1438 assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
1439 // get OutOfMemoryError zero-parameter constructor
1440 InstanceKlass* oome_ik = InstanceKlass::cast(SystemDictionary::OutOfMemoryError_klass());
1441 _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
1442 vmSymbols::void_method_signature());
1443 // get thread throwing OOME when generating the heap dump at OOME
1444 _oome_thread = JavaThread::current();
1445 } else {
1446 _oome_thread = NULL;
1447 _oome_constructor = NULL;
1448 }
1449 }
1450 ~VM_HeapDumper() {
1451 if (_stack_traces != NULL) {
1452 for (int i=0; i < _num_threads; i++) {
1453 delete _stack_traces[i];
1454 }
1455 FREE_C_HEAP_ARRAY(ThreadStackTrace*, _stack_traces, mtInternal);
1456 }
1457 delete _klass_map;
1458 }
1459
1460 VMOp_Type type() const { return VMOp_HeapDumper; }
1461 // used to mark sub-record boundary
1462 void check_segment_length();
1463 void doit();
1464 };
1465
1466 VM_HeapDumper* VM_HeapDumper::_global_dumper = NULL;
1467 DumpWriter* VM_HeapDumper::_global_writer = NULL;
1468
1469 bool VM_HeapDumper::skip_operation() const {
1470 return false;
1471 }
1472
1473 // sets the dump starting position
1474 void VM_HeapDumper::set_dump_start(jlong pos) {
1475 _dump_start = pos;
1476 }
1477
1478 // writes a HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT record
1479 void VM_HeapDumper::write_dump_header() {
1480 if (writer()->is_open()) {
1481 if (is_segmented_dump()) {
1482 writer()->write_u1(HPROF_HEAP_DUMP_SEGMENT);
1483 } else {
1484 writer()->write_u1(HPROF_HEAP_DUMP);
1485 }
1486 writer()->write_u4(0); // current ticks
1487
1488 // record the starting position for the dump (its length will be fixed up later)
1489 set_dump_start(writer()->current_offset());
1490 writer()->write_u4(0);
1491 }
1492 }
1493
1494 // fixes up the length of the current dump record
1495 void VM_HeapDumper::write_current_dump_record_length() {
1496 if (writer()->is_open()) {
1497 assert(dump_start() >= 0, "no dump start recorded");
1498
1499 // calculate the size of the dump record
1500 julong dump_end = writer()->current_offset();
1501 julong dump_len = (dump_end - dump_start() - 4);
1502
1503 // record length must fit in a u4
1504 if (dump_len > max_juint) {
1505 warning("record is too large");
1506 }
1507
1508 // seek to the dump start and fix-up the length
1509 writer()->seek_to_offset(dump_start());
1510 writer()->write_u4((u4)dump_len);
1511
1512 // adjust the total size written to keep the bytes written correct.
1513 writer()->adjust_bytes_written(-((jlong) sizeof(u4)));
1514
1515 // seek to dump end so we can continue
1516 writer()->seek_to_offset(dump_end);
1517
1518 // no current dump record
1519 set_dump_start((jlong)-1);
1520 }
1521 }
1522
1523 // used on a sub-record boundary to check if we need to start a
1524 // new segment.
1525 void VM_HeapDumper::check_segment_length() {
1526 if (writer()->is_open()) {
1527 if (is_segmented_dump()) {
1528 // don't use current_offset that would be too expensive on a per record basis
1529 julong dump_end = writer()->bytes_written() + writer()->bytes_unwritten();
1530 assert(dump_end == (julong)writer()->current_offset(), "checking");
1531 julong dump_len = (dump_end - dump_start() - 4);
1532 assert(dump_len <= max_juint, "bad dump length");
1533
1534 if (dump_len > HeapDumpSegmentSize) {
1535 write_current_dump_record_length();
1536 write_dump_header();
1537 }
1538 }
1539 }
1540 }
1541
1542 // fixes up the current dump record )and writes HPROF_HEAP_DUMP_END
1543 // record in the case of a segmented heap dump)
1544 void VM_HeapDumper::end_of_dump() {
1545 if (writer()->is_open()) {
1546 write_current_dump_record_length();
1547
1548 // for segmented dump we write the end record
1549 if (is_segmented_dump()) {
1550 writer()->write_u1(HPROF_HEAP_DUMP_END);
1551 writer()->write_u4(0);
1552 writer()->write_u4(0);
1553 }
1554 }
1555 }
1556
1557 // marks sub-record boundary
1558 void HeapObjectDumper::mark_end_of_record() {
1559 dumper()->check_segment_length();
1560 }
1561
1562 // writes a HPROF_LOAD_CLASS record for the class (and each of its
1563 // array classes)
1564 void VM_HeapDumper::do_load_class(Klass* k) {
1565 static u4 class_serial_num = 0;
1566
1567 // len of HPROF_LOAD_CLASS record
1568 u4 remaining = 2*oopSize + 2*sizeof(u4);
1569
1570 // write a HPROF_LOAD_CLASS for the class and each array class
1571 do {
1572 DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
1573
1699 u4 stack_serial_num = thread_serial_num + STACK_TRACE_ID;
1700 writer()->write_u1(HPROF_GC_ROOT_THREAD_OBJ);
1701 writer()->write_objectID(threadObj);
1702 writer()->write_u4(thread_serial_num); // thread number
1703 writer()->write_u4(stack_serial_num); // stack trace serial number
1704 int num_frames = do_thread(thread, thread_serial_num);
1705 assert(num_frames == _stack_traces[i]->get_stack_depth(),
1706 "total number of Java frames not matched");
1707 }
1708 }
1709
1710
1711 // The VM operation that dumps the heap. The dump consists of the following
1712 // records:
1713 //
1714 // HPROF_HEADER
1715 // [HPROF_UTF8]*
1716 // [HPROF_LOAD_CLASS]*
1717 // [[HPROF_FRAME]*|HPROF_TRACE]*
1718 // [HPROF_GC_CLASS_DUMP]*
1719 // HPROF_HEAP_DUMP
1720 //
1721 // The HPROF_TRACE records represent the stack traces where the heap dump
1722 // is generated and a "dummy trace" record which does not include
1723 // any frames. The dummy trace record is used to be referenced as the
1724 // unknown object alloc site.
1725 //
1726 // The HPROF_HEAP_DUMP record has a length following by sub-records. To allow
1727 // the heap dump be generated in a single pass we remember the position of
1728 // the dump length and fix it up after all sub-records have been written.
1729 // To generate the sub-records we iterate over the heap, writing
1730 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
1731 // records as we go. Once that is done we write records for some of the GC
1732 // roots.
1733
1734 void VM_HeapDumper::doit() {
1735
1736 HandleMark hm;
1737 CollectedHeap* ch = Universe::heap();
1738
1739 ch->ensure_parsability(false); // must happen, even if collection does
1740 // not happen (e.g. due to GC_locker)
1741
1742 if (_gc_before_heap_dump) {
1743 if (GC_locker::is_active()) {
1744 warning("GC locker is held; pre-heapdump GC was skipped");
1745 } else {
1746 ch->collect_as_vm_thread(GCCause::_heap_dump);
1747 }
1748 }
1749
1750 // At this point we should be the only dumper active, so
1751 // the following should be safe.
1752 set_global_dumper();
1753 set_global_writer();
1754
1755 // Write the file header - use 1.0.2 for large heaps, otherwise 1.0.1
1756 size_t used = ch->used();
1757 const char* header;
1758 if (used > (size_t)SegmentedHeapDumpThreshold) {
1759 set_segmented_dump();
1760 header = "JAVA PROFILE 1.0.2";
1761 } else {
1762 header = "JAVA PROFILE 1.0.1";
1763 }
1764
1765 // header is few bytes long - no chance to overflow int
1766 writer()->write_raw((void*)header, (int)strlen(header));
1767 writer()->write_u1(0); // terminator
1768 writer()->write_u4(oopSize);
1769 writer()->write_u8(os::javaTimeMillis());
1770
1771 // HPROF_UTF8 records
1772 SymbolTableDumper sym_dumper(writer());
1773 SymbolTable::symbols_do(&sym_dumper);
1774
1775 // write HPROF_LOAD_CLASS records
1776 ClassLoaderDataGraph::classes_do(&do_load_class);
1777 Universe::basic_type_classes_do(&do_load_class);
1778
1779 // write HPROF_FRAME and HPROF_TRACE records
1780 // this must be called after _klass_map is built when iterating the classes above.
1781 dump_stack_traces();
1782
1783 // write HPROF_HEAP_DUMP or HPROF_HEAP_DUMP_SEGMENT
1784 write_dump_header();
1785
1786 // Writes HPROF_GC_CLASS_DUMP records
1787 ClassLoaderDataGraph::classes_do(&do_class_dump);
1788 Universe::basic_type_classes_do(&do_basic_type_array_class_dump);
1789 check_segment_length();
1790
1791 // writes HPROF_GC_INSTANCE_DUMP records.
1792 // After each sub-record is written check_segment_length will be invoked. When
1793 // generated a segmented heap dump this allows us to check if the current
1794 // segment exceeds a threshold and if so, then a new segment is started.
1795 // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
1796 // of the heap dump.
1797 HeapObjectDumper obj_dumper(this, writer());
1798 Universe::heap()->safe_object_iterate(&obj_dumper);
1799
1800 // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
1801 do_threads();
1802 check_segment_length();
1803
1804 // HPROF_GC_ROOT_MONITOR_USED
1805 MonitorUsedDumper mon_dumper(writer());
1806 ObjectSynchronizer::oops_do(&mon_dumper);
1807 check_segment_length();
1808
1809 // HPROF_GC_ROOT_JNI_GLOBAL
1810 JNIGlobalsDumper jni_dumper(writer());
1811 JNIHandles::oops_do(&jni_dumper);
1812 Universe::oops_do(&jni_dumper); // technically not jni roots, but global roots
1813 // for things like preallocated throwable backtraces
1814 check_segment_length();
1815
1816 // HPROF_GC_ROOT_STICKY_CLASS
1817 StickyClassDumper class_dumper(writer());
1818 SystemDictionary::always_strong_classes_do(&class_dumper);
1819
1820 // fixes up the length of the dump record. In the case of a segmented
1821 // heap then the HPROF_HEAP_DUMP_END record is also written.
1822 end_of_dump();
1823
1824 // Now we clear the global variables, so that a future dumper might run.
1825 clear_global_dumper();
1826 clear_global_writer();
1827 }
1828
1829 void VM_HeapDumper::dump_stack_traces() {
1830 // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
1831 DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
1832 writer()->write_u4((u4) STACK_TRACE_ID);
1833 writer()->write_u4(0); // thread number
1834 writer()->write_u4(0); // frame count
1835
1836 _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal);
1837 int frame_serial_num = 0;
1838 for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) {
1839 oop threadObj = thread->threadObj();
1840 if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
1841 // dump thread stack trace
1842 ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false);
|
33 #include "oops/objArrayKlass.hpp"
34 #include "runtime/javaCalls.hpp"
35 #include "runtime/jniHandles.hpp"
36 #include "runtime/reflectionUtils.hpp"
37 #include "runtime/vframe.hpp"
38 #include "runtime/vmThread.hpp"
39 #include "runtime/vm_operations.hpp"
40 #include "services/heapDumper.hpp"
41 #include "services/threadService.hpp"
42 #include "utilities/ostream.hpp"
43 #include "utilities/macros.hpp"
44 #if INCLUDE_ALL_GCS
45 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
46 #endif // INCLUDE_ALL_GCS
47
48 /*
49 * HPROF binary format - description copied from:
50 * src/share/demo/jvmti/hprof/hprof_io.c
51 *
52 *
53 * header "JAVA PROFILE 1.0.2" (0-terminated)
54 *
55 * u4 size of identifiers. Identifiers are used to represent
56 * UTF8 strings, objects, stack traces, etc. They usually
57 * have the same size as host pointers. For example, on
58 * Solaris and Win32, the size is 4.
59 * u4 high word
60 * u4 low word number of milliseconds since 0:00 GMT, 1/1/70
61 * [record]* a sequence of records.
62 *
63 *
64 * Record format:
65 *
66 * u1 a TAG denoting the type of the record
67 * u4 number of *microseconds* since the time stamp in the
68 * header. (wraps around in a little more than an hour)
69 * u4 number of bytes *remaining* in the record. Note that
70 * this number excludes the tag and the length field itself.
71 * [u1]* BODY of the record (a sequence of bytes)
72 *
73 *
364 enum {
365 STACK_TRACE_ID = 1,
366 INITIAL_CLASS_COUNT = 200
367 };
368
369 // Supports I/O operations on a dump file
370
371 class DumpWriter : public StackObj {
372 private:
373 enum {
374 io_buffer_size = 8*M
375 };
376
377 int _fd; // file descriptor (-1 if dump file not open)
378 julong _bytes_written; // number of byte written to dump file
379
380 char* _buffer; // internal buffer
381 size_t _size;
382 size_t _pos;
383
384 jlong _dump_start;
385
386 char* _error; // error message when I/O fails
387
388 void set_file_descriptor(int fd) { _fd = fd; }
389 int file_descriptor() const { return _fd; }
390
391 char* buffer() const { return _buffer; }
392 size_t buffer_size() const { return _size; }
393 size_t position() const { return _pos; }
394 void set_position(size_t pos) { _pos = pos; }
395
396 void set_error(const char* error) { _error = (char*)os::strdup(error); }
397
398 // all I/O go through this function
399 void write_internal(void* s, size_t len);
400
401 public:
402 DumpWriter(const char* path);
403 ~DumpWriter();
404
405 void close();
406 bool is_open() const { return file_descriptor() >= 0; }
407 void flush();
408
409 jlong dump_start() const { return _dump_start; }
410 void set_dump_start(jlong pos);
411 julong current_record_length();
412
413 // total number of bytes written to the disk
414 julong bytes_written() const { return _bytes_written; }
415
416 // adjust the number of bytes written to disk (used to keep the count
417 // of the number of bytes written in case of rewrites)
418 void adjust_bytes_written(jlong n) { _bytes_written += n; }
419
420 // number of (buffered) bytes as yet unwritten to the dump file
421 size_t bytes_unwritten() const { return position(); }
422
423 char* error() const { return _error; }
424
425 jlong current_offset();
426 void seek_to_offset(jlong pos);
427
428 // writer functions
429 void write_raw(void* s, size_t len);
430 void write_u1(u1 x) { write_raw((void*)&x, 1); }
431 void write_u2(u2 x);
432 void write_u4(u4 x);
434 void write_objectID(oop o);
435 void write_symbolID(Symbol* o);
436 void write_classID(Klass* k);
437 void write_id(u4 x);
438 };
439
440 DumpWriter::DumpWriter(const char* path) {
441 // try to allocate an I/O buffer of io_buffer_size. If there isn't
442 // sufficient memory then reduce size until we can allocate something.
443 _size = io_buffer_size;
444 do {
445 _buffer = (char*)os::malloc(_size, mtInternal);
446 if (_buffer == NULL) {
447 _size = _size >> 1;
448 }
449 } while (_buffer == NULL && _size > 0);
450 assert((_size > 0 && _buffer != NULL) || (_size == 0 && _buffer == NULL), "sanity check");
451 _pos = 0;
452 _error = NULL;
453 _bytes_written = 0L;
454 _dump_start = (jlong)-1;
455 _fd = os::create_binary_file(path, false); // don't replace existing file
456
457 // if the open failed we record the error
458 if (_fd < 0) {
459 _error = (char*)os::strdup(strerror(errno));
460 }
461 }
462
463 DumpWriter::~DumpWriter() {
464 // flush and close dump file
465 if (is_open()) {
466 close();
467 }
468 if (_buffer != NULL) os::free(_buffer);
469 if (_error != NULL) os::free(_error);
470 }
471
472 // closes dump file (if open)
473 void DumpWriter::close() {
474 // flush and close dump file
475 if (is_open()) {
476 flush();
477 ::close(file_descriptor());
478 set_file_descriptor(-1);
479 }
480 }
481
482 // sets the dump starting position
483 void DumpWriter::set_dump_start(jlong pos) {
484 _dump_start = pos;
485 }
486
487 julong DumpWriter::current_record_length() {
488 if (is_open()) {
489 // calculate the size of the dump record
490 julong dump_end = bytes_written() + bytes_unwritten();
491 assert(dump_end == (size_t)current_offset(), "checking");
492 julong dump_len = dump_end - dump_start() - 4;
493 return dump_len;
494 }
495 return 0;
496 }
497
498 // write directly to the file
499 void DumpWriter::write_internal(void* s, size_t len) {
500 if (is_open()) {
501 const char* pos = (char*)s;
502 ssize_t n = 0;
503 while (len > 0) {
504 uint tmp = (uint)MIN2(len, (size_t)UINT_MAX);
505 n = ::write(file_descriptor(), pos, tmp);
506
507 if (n < 0) {
508 set_error(strerror(errno));
509 ::close(file_descriptor());
510 set_file_descriptor(-1);
511 return;
512 }
513
514 _bytes_written += n;
515 pos += n;
516 len -= n;
517 }
646 static void dump_static_fields(DumpWriter* writer, Klass* k);
647 // dump the raw values of the instance fields of the given object
648 static void dump_instance_fields(DumpWriter* writer, oop o);
649 // dumps the definition of the instance fields for a given class
650 static void dump_instance_field_descriptors(DumpWriter* writer, Klass* k);
651 // creates HPROF_GC_INSTANCE_DUMP record for the given object
652 static void dump_instance(DumpWriter* writer, oop o);
653 // creates HPROF_GC_CLASS_DUMP record for the given class and each of its
654 // array classes
655 static void dump_class_and_array_classes(DumpWriter* writer, Klass* k);
656 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
657 // class (and each multi-dimensional array class too)
658 static void dump_basic_type_array_class(DumpWriter* writer, Klass* k);
659
660 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
661 static void dump_object_array(DumpWriter* writer, objArrayOop array);
662 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
663 static void dump_prim_array(DumpWriter* writer, typeArrayOop array);
664 // create HPROF_FRAME record for the given method and bci
665 static void dump_stack_frame(DumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
666
667 // check if we need to truncate an array
668 static int calculate_array_max_length(DumpWriter* writer, arrayOop array, short header_size);
669
670 // writes a HPROF_HEAP_DUMP_SEGMENT record
671 static void write_dump_header(DumpWriter* writer);
672
673 // fixes up the length of the current dump record
674 static void write_current_dump_record_length(DumpWriter* writer);
675
676 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
677 static void end_of_dump(DumpWriter* writer);
678 };
679
680 // write a header of the given type
681 void DumperSupport:: write_header(DumpWriter* writer, hprofTag tag, u4 len) {
682 writer->write_u1((u1)tag);
683 writer->write_u4(0); // current ticks
684 writer->write_u4(len);
685 }
686
687 // returns hprof tag for the given type signature
688 hprofTag DumperSupport::sig2tag(Symbol* sig) {
689 switch (sig->byte_at(0)) {
690 case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
691 case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
692 case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
693 case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
694 case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
695 case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
696 case JVM_SIGNATURE_INT : return HPROF_INT;
697 case JVM_SIGNATURE_LONG : return HPROF_LONG;
1064 Klass* java_super = klass->java_super();
1065 assert(java_super != NULL, "checking");
1066 writer->write_classID(java_super);
1067
1068 writer->write_objectID(oop(NULL)); // loader
1069 writer->write_objectID(oop(NULL)); // signers
1070 writer->write_objectID(oop(NULL)); // protection domain
1071
1072 writer->write_objectID(oop(NULL)); // reserved
1073 writer->write_objectID(oop(NULL));
1074 writer->write_u4(0); // instance size
1075 writer->write_u2(0); // constant pool
1076 writer->write_u2(0); // static fields
1077 writer->write_u2(0); // instance fields
1078
1079 // get the array class for the next rank
1080 k = klass->array_klass_or_null();
1081 }
1082 }
1083
1084 // Hprof uses an u4 as record length field,
1085 // which means we need to truncate arrays that are too long.
1086 int DumperSupport::calculate_array_max_length(DumpWriter* writer, arrayOop array, short header_size) {
1087 BasicType type = ArrayKlass::cast(array->klass())->element_type();
1088 assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1089
1090 int length = array->length();
1091
1092 int type_size;
1093 if (type == T_OBJECT) {
1094 type_size = sizeof(address);
1095 } else {
1096 type_size = type2aelembytes(type);
1097 }
1098
1099 size_t length_in_bytes = (size_t)length * type_size;
1100
1101 // Create a new record if the current record is non-empty and the array can't fit.
1102 julong current_record_length = writer->current_record_length();
1103 if (current_record_length > 0 &&
1104 (current_record_length + header_size + length_in_bytes) > max_juint) {
1105 write_current_dump_record_length(writer);
1106 write_dump_header(writer);
1107
1108 // We now have an empty record.
1109 current_record_length = 0;
1110 }
1111
1112 // Calculate max bytes we can use.
1113 uint max_bytes = max_juint - (header_size + current_record_length);
1114
1115 // Array too long for the record?
1116 // Calculate max length and return it.
1117 if (length_in_bytes > max_bytes) {
1118 length = max_bytes / type_size;
1119 length_in_bytes = (size_t)length * type_size;
1120
1121 warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1122 type2name_tab[type], array->length(), length);
1123 }
1124 return length;
1125 }
1126
1127 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1128 void DumperSupport::dump_object_array(DumpWriter* writer, objArrayOop array) {
1129 // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1130 short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1131
1132 int length = calculate_array_max_length(writer, array, header_size);
1133
1134 writer->write_u1(HPROF_GC_OBJ_ARRAY_DUMP);
1135 writer->write_objectID(array);
1136 writer->write_u4(STACK_TRACE_ID);
1137 writer->write_u4(length);
1138
1139
1140 // array class ID
1141 writer->write_classID(array->klass());
1142
1143 // [id]* elements
1144 for (int index = 0; index < length; index++) {
1145 oop o = array->obj_at(index);
1146 writer->write_objectID(o);
1147 }
1148 }
1149
1150 #define WRITE_ARRAY(Array, Type, Size, Length) \
1151 for (int i = 0; i < Length; i++) { writer->write_##Size((Size)array->Type##_at(i)); }
1152
1153
1154 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1155 void DumperSupport::dump_prim_array(DumpWriter* writer, typeArrayOop array) {
1156 BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1157
1158 // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1159 short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1160
1161 int length = calculate_array_max_length(writer, array, header_size);
1162 int type_size = type2aelembytes(type);
1163 u4 length_in_bytes = (u4)length * type_size;
1164
1165 writer->write_u1(HPROF_GC_PRIM_ARRAY_DUMP);
1166 writer->write_objectID(array);
1167 writer->write_u4(STACK_TRACE_ID);
1168 writer->write_u4(length);
1169 writer->write_u1(type2tag(type));
1170
1171 // nothing to copy
1172 if (length == 0) {
1173 return;
1174 }
1175
1176 // If the byte ordering is big endian then we can copy most types directly
1177
1178 switch (type) {
1179 case T_INT : {
1180 if (Bytes::is_Java_byte_ordering_different()) {
1181 WRITE_ARRAY(array, int, u4, length);
1182 } else {
1183 writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes);
1184 }
1185 break;
1186 }
1187 case T_BYTE : {
1188 writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes);
1189 break;
1190 }
1191 case T_CHAR : {
1192 if (Bytes::is_Java_byte_ordering_different()) {
1193 WRITE_ARRAY(array, char, u2, length);
1194 } else {
1195 writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes);
1196 }
1197 break;
1198 }
1199 case T_SHORT : {
1200 if (Bytes::is_Java_byte_ordering_different()) {
1201 WRITE_ARRAY(array, short, u2, length);
1202 } else {
1203 writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes);
1204 }
1205 break;
1206 }
1207 case T_BOOLEAN : {
1208 if (Bytes::is_Java_byte_ordering_different()) {
1209 WRITE_ARRAY(array, bool, u1, length);
1210 } else {
1211 writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes);
1212 }
1213 break;
1214 }
1215 case T_LONG : {
1216 if (Bytes::is_Java_byte_ordering_different()) {
1217 WRITE_ARRAY(array, long, u8, length);
1218 } else {
1219 writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes);
1220 }
1221 break;
1222 }
1223
1224 // handle float/doubles in a special value to ensure than NaNs are
1225 // written correctly. TO DO: Check if we can avoid this on processors that
1226 // use IEEE 754.
1227
1228 case T_FLOAT : {
1229 for (int i = 0; i < length; i++) {
1230 dump_float(writer, array->float_at(i));
1231 }
1232 break;
1233 }
1234 case T_DOUBLE : {
1235 for (int i = 0; i < length; i++) {
1236 dump_double(writer, array->double_at(i));
1237 }
1238 break;
1239 }
1240 default : ShouldNotReachHere();
1241 }
1242 }
1243
1244 // create a HPROF_FRAME record of the given Method* and bci
1245 void DumperSupport::dump_stack_frame(DumpWriter* writer,
1246 int frame_serial_num,
1247 int class_serial_num,
1248 Method* m,
1249 int bci) {
1250 int line_number;
1251 if (m->is_native()) {
1252 line_number = -3; // native frame
1253 } else {
1254 line_number = m->line_number_from_bci(bci);
1255 }
1256
1433 } else if (o->is_objArray()) {
1434 // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1435 DumperSupport::dump_object_array(writer(), objArrayOop(o));
1436 mark_end_of_record();
1437 } else if (o->is_typeArray()) {
1438 // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1439 DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1440 mark_end_of_record();
1441 }
1442 }
1443
1444 // The VM operation that performs the heap dump
1445 class VM_HeapDumper : public VM_GC_Operation {
1446 private:
1447 static VM_HeapDumper* _global_dumper;
1448 static DumpWriter* _global_writer;
1449 DumpWriter* _local_writer;
1450 JavaThread* _oome_thread;
1451 Method* _oome_constructor;
1452 bool _gc_before_heap_dump;
1453 GrowableArray<Klass*>* _klass_map;
1454 ThreadStackTrace** _stack_traces;
1455 int _num_threads;
1456
1457 // accessors and setters
1458 static VM_HeapDumper* dumper() { assert(_global_dumper != NULL, "Error"); return _global_dumper; }
1459 static DumpWriter* writer() { assert(_global_writer != NULL, "Error"); return _global_writer; }
1460 void set_global_dumper() {
1461 assert(_global_dumper == NULL, "Error");
1462 _global_dumper = this;
1463 }
1464 void set_global_writer() {
1465 assert(_global_writer == NULL, "Error");
1466 _global_writer = _local_writer;
1467 }
1468 void clear_global_dumper() { _global_dumper = NULL; }
1469 void clear_global_writer() { _global_writer = NULL; }
1470
1471 bool skip_operation() const;
1472
1473 // writes a HPROF_LOAD_CLASS record
1474 static void do_load_class(Klass* k);
1475
1476 // writes a HPROF_GC_CLASS_DUMP record for the given class
1477 // (and each array class too)
1478 static void do_class_dump(Klass* k);
1479
1480 // writes a HPROF_GC_CLASS_DUMP records for a given basic type
1481 // array (and each multi-dimensional array too)
1482 static void do_basic_type_array_class_dump(Klass* k);
1483
1484 // HPROF_GC_ROOT_THREAD_OBJ records
1485 int do_thread(JavaThread* thread, u4 thread_serial_num);
1486 void do_threads();
1487
1488 void add_class_serial_number(Klass* k, int serial_num) {
1489 _klass_map->at_put_grow(serial_num, k);
1490 }
1491
1492 // HPROF_TRACE and HPROF_FRAME records
1493 void dump_stack_traces();
1494
1495 public:
1496 VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome) :
1497 VM_GC_Operation(0 /* total collections, dummy, ignored */,
1498 GCCause::_heap_dump /* GC Cause */,
1499 0 /* total full collections, dummy, ignored */,
1500 gc_before_heap_dump) {
1501 _local_writer = writer;
1502 _gc_before_heap_dump = gc_before_heap_dump;
1503 _klass_map = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, true);
1504 _stack_traces = NULL;
1505 _num_threads = 0;
1506 if (oome) {
1507 assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
1508 // get OutOfMemoryError zero-parameter constructor
1509 InstanceKlass* oome_ik = InstanceKlass::cast(SystemDictionary::OutOfMemoryError_klass());
1510 _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
1511 vmSymbols::void_method_signature());
1512 // get thread throwing OOME when generating the heap dump at OOME
1513 _oome_thread = JavaThread::current();
1514 } else {
1515 _oome_thread = NULL;
1516 _oome_constructor = NULL;
1517 }
1518 }
1519 ~VM_HeapDumper() {
1520 if (_stack_traces != NULL) {
1521 for (int i=0; i < _num_threads; i++) {
1522 delete _stack_traces[i];
1523 }
1524 FREE_C_HEAP_ARRAY(ThreadStackTrace*, _stack_traces, mtInternal);
1525 }
1526 delete _klass_map;
1527 }
1528
1529 VMOp_Type type() const { return VMOp_HeapDumper; }
1530 // used to mark sub-record boundary
1531 void check_segment_length();
1532 void doit();
1533 };
1534
1535 VM_HeapDumper* VM_HeapDumper::_global_dumper = NULL;
1536 DumpWriter* VM_HeapDumper::_global_writer = NULL;
1537
1538 bool VM_HeapDumper::skip_operation() const {
1539 return false;
1540 }
1541
1542 // writes a HPROF_HEAP_DUMP_SEGMENT record
1543 void DumperSupport::write_dump_header(DumpWriter* writer) {
1544 if (writer->is_open()) {
1545 writer->write_u1(HPROF_HEAP_DUMP_SEGMENT);
1546 writer->write_u4(0); // current ticks
1547
1548 // record the starting position for the dump (its length will be fixed up later)
1549 writer->set_dump_start(writer->current_offset());
1550 writer->write_u4(0);
1551 }
1552 }
1553
1554 // fixes up the length of the current dump record
1555 void DumperSupport::write_current_dump_record_length(DumpWriter* writer) {
1556 if (writer->is_open()) {
1557 julong dump_end = writer->bytes_written() + writer->bytes_unwritten();
1558 julong dump_len = writer->current_record_length();
1559
1560 // record length must fit in a u4
1561 if (dump_len > max_juint) {
1562 warning("record is too large");
1563 }
1564
1565 // seek to the dump start and fix-up the length
1566 assert(writer->dump_start() >= 0, "no dump start recorded");
1567 writer->seek_to_offset(writer->dump_start());
1568 writer->write_u4((u4)dump_len);
1569
1570 // adjust the total size written to keep the bytes written correct.
1571 writer->adjust_bytes_written(-((jlong) sizeof(u4)));
1572
1573 // seek to dump end so we can continue
1574 writer->seek_to_offset(dump_end);
1575
1576 // no current dump record
1577 writer->set_dump_start((jlong)-1);
1578 }
1579 }
1580
1581 // used on a sub-record boundary to check if we need to start a
1582 // new segment.
1583 void VM_HeapDumper::check_segment_length() {
1584 if (writer()->is_open()) {
1585 julong dump_len = writer()->current_record_length();
1586
1587 if (dump_len > 2UL*G) {
1588 DumperSupport::write_current_dump_record_length(writer());
1589 DumperSupport::write_dump_header(writer());
1590 }
1591 }
1592 }
1593
1594 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
1595 void DumperSupport::end_of_dump(DumpWriter* writer) {
1596 if (writer->is_open()) {
1597 write_current_dump_record_length(writer);
1598
1599 writer->write_u1(HPROF_HEAP_DUMP_END);
1600 writer->write_u4(0);
1601 writer->write_u4(0);
1602 }
1603 }
1604
1605 // marks sub-record boundary
1606 void HeapObjectDumper::mark_end_of_record() {
1607 dumper()->check_segment_length();
1608 }
1609
1610 // writes a HPROF_LOAD_CLASS record for the class (and each of its
1611 // array classes)
1612 void VM_HeapDumper::do_load_class(Klass* k) {
1613 static u4 class_serial_num = 0;
1614
1615 // len of HPROF_LOAD_CLASS record
1616 u4 remaining = 2*oopSize + 2*sizeof(u4);
1617
1618 // write a HPROF_LOAD_CLASS for the class and each array class
1619 do {
1620 DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
1621
1747 u4 stack_serial_num = thread_serial_num + STACK_TRACE_ID;
1748 writer()->write_u1(HPROF_GC_ROOT_THREAD_OBJ);
1749 writer()->write_objectID(threadObj);
1750 writer()->write_u4(thread_serial_num); // thread number
1751 writer()->write_u4(stack_serial_num); // stack trace serial number
1752 int num_frames = do_thread(thread, thread_serial_num);
1753 assert(num_frames == _stack_traces[i]->get_stack_depth(),
1754 "total number of Java frames not matched");
1755 }
1756 }
1757
1758
1759 // The VM operation that dumps the heap. The dump consists of the following
1760 // records:
1761 //
1762 // HPROF_HEADER
1763 // [HPROF_UTF8]*
1764 // [HPROF_LOAD_CLASS]*
1765 // [[HPROF_FRAME]*|HPROF_TRACE]*
1766 // [HPROF_GC_CLASS_DUMP]*
1767 // [HPROF_HEAP_DUMP_SEGMENT]*
1768 // HPROF_HEAP_DUMP_END
1769 //
1770 // The HPROF_TRACE records represent the stack traces where the heap dump
1771 // is generated and a "dummy trace" record which does not include
1772 // any frames. The dummy trace record is used to be referenced as the
1773 // unknown object alloc site.
1774 //
1775 // Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records.
1776 // To allow the heap dump be generated in a single pass we remember the position
1777 // of the dump length and fix it up after all sub-records have been written.
1778 // To generate the sub-records we iterate over the heap, writing
1779 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
1780 // records as we go. Once that is done we write records for some of the GC
1781 // roots.
1782
1783 void VM_HeapDumper::doit() {
1784
1785 HandleMark hm;
1786 CollectedHeap* ch = Universe::heap();
1787
1788 ch->ensure_parsability(false); // must happen, even if collection does
1789 // not happen (e.g. due to GC_locker)
1790
1791 if (_gc_before_heap_dump) {
1792 if (GC_locker::is_active()) {
1793 warning("GC locker is held; pre-heapdump GC was skipped");
1794 } else {
1795 ch->collect_as_vm_thread(GCCause::_heap_dump);
1796 }
1797 }
1798
1799 // At this point we should be the only dumper active, so
1800 // the following should be safe.
1801 set_global_dumper();
1802 set_global_writer();
1803
1804 // Write the file header - we always use 1.0.2
1805 size_t used = ch->used();
1806 const char* header = "JAVA PROFILE 1.0.2";
1807
1808 // header is few bytes long - no chance to overflow int
1809 writer()->write_raw((void*)header, (int)strlen(header));
1810 writer()->write_u1(0); // terminator
1811 writer()->write_u4(oopSize);
1812 writer()->write_u8(os::javaTimeMillis());
1813
1814 // HPROF_UTF8 records
1815 SymbolTableDumper sym_dumper(writer());
1816 SymbolTable::symbols_do(&sym_dumper);
1817
1818 // write HPROF_LOAD_CLASS records
1819 ClassLoaderDataGraph::classes_do(&do_load_class);
1820 Universe::basic_type_classes_do(&do_load_class);
1821
1822 // write HPROF_FRAME and HPROF_TRACE records
1823 // this must be called after _klass_map is built when iterating the classes above.
1824 dump_stack_traces();
1825
1826 // write HPROF_HEAP_DUMP_SEGMENT
1827 DumperSupport::write_dump_header(writer());
1828
1829 // Writes HPROF_GC_CLASS_DUMP records
1830 ClassLoaderDataGraph::classes_do(&do_class_dump);
1831 Universe::basic_type_classes_do(&do_basic_type_array_class_dump);
1832 check_segment_length();
1833
1834 // writes HPROF_GC_INSTANCE_DUMP records.
1835 // After each sub-record is written check_segment_length will be invoked
1836 // to check if the current segment exceeds a threshold. If so, a new
1837 // segment is started.
1838 // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
1839 // of the heap dump.
1840 HeapObjectDumper obj_dumper(this, writer());
1841 Universe::heap()->safe_object_iterate(&obj_dumper);
1842
1843 // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
1844 do_threads();
1845 check_segment_length();
1846
1847 // HPROF_GC_ROOT_MONITOR_USED
1848 MonitorUsedDumper mon_dumper(writer());
1849 ObjectSynchronizer::oops_do(&mon_dumper);
1850 check_segment_length();
1851
1852 // HPROF_GC_ROOT_JNI_GLOBAL
1853 JNIGlobalsDumper jni_dumper(writer());
1854 JNIHandles::oops_do(&jni_dumper);
1855 Universe::oops_do(&jni_dumper); // technically not jni roots, but global roots
1856 // for things like preallocated throwable backtraces
1857 check_segment_length();
1858
1859 // HPROF_GC_ROOT_STICKY_CLASS
1860 StickyClassDumper class_dumper(writer());
1861 SystemDictionary::always_strong_classes_do(&class_dumper);
1862
1863 // fixes up the length of the dump record and writes the HPROF_HEAP_DUMP_END record.
1864 DumperSupport::end_of_dump(writer());
1865
1866 // Now we clear the global variables, so that a future dumper might run.
1867 clear_global_dumper();
1868 clear_global_writer();
1869 }
1870
1871 void VM_HeapDumper::dump_stack_traces() {
1872 // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
1873 DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
1874 writer()->write_u4((u4) STACK_TRACE_ID);
1875 writer()->write_u4(0); // thread number
1876 writer()->write_u4(0); // frame count
1877
1878 _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal);
1879 int frame_serial_num = 0;
1880 for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) {
1881 oop threadObj = thread->threadObj();
1882 if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
1883 // dump thread stack trace
1884 ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false);
|