< prev index next >

src/share/vm/services/heapDumper.cpp

Print this page
rev 9054 : 8144732: VM_HeapDumper hits assert with bad dump_len
Reviewed-by: dsamersoff


  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 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1154 void DumperSupport::dump_prim_array(DumpWriter* writer, typeArrayOop array) {
1155   BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1156 
1157   // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1158   short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1159 
1160   int length = calculate_array_max_length(writer, array, header_size);
1161   int type_size = type2aelembytes(type);
1162   u4 length_in_bytes = (u4)length * type_size;
1163 
1164   writer->write_u1(HPROF_GC_PRIM_ARRAY_DUMP);
1165   writer->write_objectID(array);
1166   writer->write_u4(STACK_TRACE_ID);
1167   writer->write_u4(length);
1168   writer->write_u1(type2tag(type));
1169 
1170   // nothing to copy
1171   if (length == 0) {
1172     return;
1173   }
1174 
1175   // If the byte ordering is big endian then we can copy most types directly

1176 
1177   switch (type) {
1178     case T_INT : {
1179       if (Bytes::is_Java_byte_ordering_different()) {
1180         WRITE_ARRAY(array, int, u4, length);
1181       } else {
1182         writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes);
1183       }
1184       break;
1185     }
1186     case T_BYTE : {
1187       writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes);
1188       break;
1189     }
1190     case T_CHAR : {
1191       if (Bytes::is_Java_byte_ordering_different()) {
1192         WRITE_ARRAY(array, char, u2, length);
1193       } else {
1194         writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes);
1195       }
1196       break;
1197     }
1198     case T_SHORT : {
1199       if (Bytes::is_Java_byte_ordering_different()) {
1200         WRITE_ARRAY(array, short, u2, length);
1201       } else {
1202         writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes);
1203       }
1204       break;
1205     }
1206     case T_BOOLEAN : {
1207       if (Bytes::is_Java_byte_ordering_different()) {
1208         WRITE_ARRAY(array, bool, u1, length);
1209       } else {
1210         writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes);
1211       }
1212       break;
1213     }
1214     case T_LONG : {
1215       if (Bytes::is_Java_byte_ordering_different()) {
1216         WRITE_ARRAY(array, long, u8, length);
1217       } else {
1218         writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes);
1219       }
1220       break;
1221     }
1222 
1223     // handle float/doubles in a special value to ensure than NaNs are
1224     // written correctly. TO DO: Check if we can avoid this on processors that
1225     // use IEEE 754.
1226 
1227     case T_FLOAT : {
1228       for (int i = 0; i < length; i++) {
1229         dump_float(writer, array->float_at(i));
1230       }
1231       break;
1232     }
1233     case T_DOUBLE : {
1234       for (int i = 0; i < length; i++) {
1235         dump_double(writer, array->double_at(i));
1236       }
1237       break;
1238     }
1239     default : ShouldNotReachHere();
1240   }
1241 }
1242 
1243 // create a HPROF_FRAME record of the given Method* and bci
1244 void DumperSupport::dump_stack_frame(DumpWriter* writer,
1245                                      int frame_serial_num,
1246                                      int class_serial_num,
1247                                      Method* m,
1248                                      int bci) {
1249   int line_number;
1250   if (m->is_native()) {
1251     line_number = -3;  // native frame
1252   } else {
1253     line_number = m->line_number_from_bci(bci);
1254   }
1255 


1432   } else if (o->is_objArray()) {
1433     // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1434     DumperSupport::dump_object_array(writer(), objArrayOop(o));
1435     mark_end_of_record();
1436   } else if (o->is_typeArray()) {
1437     // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1438     DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1439     mark_end_of_record();
1440   }
1441 }
1442 
1443 // The VM operation that performs the heap dump
1444 class VM_HeapDumper : public VM_GC_Operation {
1445  private:
1446   static VM_HeapDumper* _global_dumper;
1447   static DumpWriter*    _global_writer;
1448   DumpWriter*           _local_writer;
1449   JavaThread*           _oome_thread;
1450   Method*               _oome_constructor;
1451   bool _gc_before_heap_dump;


1452   GrowableArray<Klass*>* _klass_map;
1453   ThreadStackTrace** _stack_traces;
1454   int _num_threads;
1455 
1456   // accessors and setters
1457   static VM_HeapDumper* dumper()         {  assert(_global_dumper != NULL, "Error"); return _global_dumper; }
1458   static DumpWriter* writer()            {  assert(_global_writer != NULL, "Error"); return _global_writer; }
1459   void set_global_dumper() {
1460     assert(_global_dumper == NULL, "Error");
1461     _global_dumper = this;
1462   }
1463   void set_global_writer() {
1464     assert(_global_writer == NULL, "Error");
1465     _global_writer = _local_writer;
1466   }
1467   void clear_global_dumper() { _global_dumper = NULL; }
1468   void clear_global_writer() { _global_writer = NULL; }
1469 





1470   bool skip_operation() const;
1471 
1472   // writes a HPROF_LOAD_CLASS record
1473   static void do_load_class(Klass* k);
1474 
1475   // writes a HPROF_GC_CLASS_DUMP record for the given class
1476   // (and each array class too)
1477   static void do_class_dump(Klass* k);
1478 
1479   // writes a HPROF_GC_CLASS_DUMP records for a given basic type
1480   // array (and each multi-dimensional array too)
1481   static void do_basic_type_array_class_dump(Klass* k);
1482 
1483   // HPROF_GC_ROOT_THREAD_OBJ records
1484   int do_thread(JavaThread* thread, u4 thread_serial_num);
1485   void do_threads();
1486 
1487   void add_class_serial_number(Klass* k, int serial_num) {
1488     _klass_map->at_put_grow(serial_num, k);
1489   }
1490 
1491   // HPROF_TRACE and HPROF_FRAME records
1492   void dump_stack_traces();
1493 










1494  public:
1495   VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome) :
1496     VM_GC_Operation(0 /* total collections,      dummy, ignored */,
1497                     GCCause::_heap_dump /* GC Cause */,
1498                     0 /* total full collections, dummy, ignored */,
1499                     gc_before_heap_dump) {
1500     _local_writer = writer;
1501     _gc_before_heap_dump = gc_before_heap_dump;


1502     _klass_map = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, true);
1503     _stack_traces = NULL;
1504     _num_threads = 0;
1505     if (oome) {
1506       assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
1507       // get OutOfMemoryError zero-parameter constructor
1508       InstanceKlass* oome_ik = InstanceKlass::cast(SystemDictionary::OutOfMemoryError_klass());
1509       _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
1510                                                           vmSymbols::void_method_signature());
1511       // get thread throwing OOME when generating the heap dump at OOME
1512       _oome_thread = JavaThread::current();
1513     } else {
1514       _oome_thread = NULL;
1515       _oome_constructor = NULL;
1516     }
1517   }
1518   ~VM_HeapDumper() {
1519     if (_stack_traces != NULL) {
1520       for (int i=0; i < _num_threads; i++) {
1521         delete _stack_traces[i];
1522       }
1523       FREE_C_HEAP_ARRAY(ThreadStackTrace*, _stack_traces, mtInternal);
1524     }
1525     delete _klass_map;
1526   }
1527 
1528   VMOp_Type type() const { return VMOp_HeapDumper; }
1529   // used to mark sub-record boundary
1530   void check_segment_length();
1531   void doit();
1532 };
1533 
1534 VM_HeapDumper* VM_HeapDumper::_global_dumper = NULL;
1535 DumpWriter*    VM_HeapDumper::_global_writer = NULL;
1536 
1537 bool VM_HeapDumper::skip_operation() const {
1538   return false;
1539 }
1540 
1541  // writes a HPROF_HEAP_DUMP_SEGMENT record
1542 void DumperSupport::write_dump_header(DumpWriter* writer) {
1543   if (writer->is_open()) {
1544     writer->write_u1(HPROF_HEAP_DUMP_SEGMENT);
1545     writer->write_u4(0); // current ticks









1546 
1547     // record the starting position for the dump (its length will be fixed up later)
1548     writer->set_dump_start(writer->current_offset());
1549     writer->write_u4(0);
1550   }
1551 }
1552 
1553 // fixes up the length of the current dump record
1554 void DumperSupport::write_current_dump_record_length(DumpWriter* writer) {
1555   if (writer->is_open()) {
1556     julong dump_end = writer->bytes_written() + writer->bytes_unwritten();
1557     julong dump_len = writer->current_record_length();



1558 
1559     // record length must fit in a u4
1560     if (dump_len > max_juint) {
1561       warning("record is too large");
1562     }
1563 
1564     // seek to the dump start and fix-up the length
1565     assert(writer->dump_start() >= 0, "no dump start recorded");
1566     writer->seek_to_offset(writer->dump_start());
1567     writer->write_u4((u4)dump_len);
1568 
1569     // adjust the total size written to keep the bytes written correct.
1570     writer->adjust_bytes_written(-((jlong) sizeof(u4)));
1571 
1572     // seek to dump end so we can continue
1573     writer->seek_to_offset(dump_end);
1574 
1575     // no current dump record
1576     writer->set_dump_start((jlong)-1);
1577   }
1578 }
1579 
1580 // used on a sub-record boundary to check if we need to start a
1581 // new segment.
1582 void VM_HeapDumper::check_segment_length() {
1583   if (writer()->is_open()) {
1584     julong dump_len = writer()->current_record_length();





1585 
1586     if (dump_len > 2UL*G) {
1587       DumperSupport::write_current_dump_record_length(writer());
1588       DumperSupport::write_dump_header(writer());

1589     }
1590   }
1591 }
1592 
1593 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
1594 void DumperSupport::end_of_dump(DumpWriter* writer) {
1595   if (writer->is_open()) {
1596     write_current_dump_record_length(writer);

1597 
1598     writer->write_u1(HPROF_HEAP_DUMP_END);
1599     writer->write_u4(0);
1600     writer->write_u4(0);



1601   }
1602 }
1603 
1604 // marks sub-record boundary
1605 void HeapObjectDumper::mark_end_of_record() {
1606   dumper()->check_segment_length();
1607 }
1608 
1609 // writes a HPROF_LOAD_CLASS record for the class (and each of its
1610 // array classes)
1611 void VM_HeapDumper::do_load_class(Klass* k) {
1612   static u4 class_serial_num = 0;
1613 
1614   // len of HPROF_LOAD_CLASS record
1615   u4 remaining = 2*oopSize + 2*sizeof(u4);
1616 
1617   // write a HPROF_LOAD_CLASS for the class and each array class
1618   do {
1619     DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
1620 


1746     u4 stack_serial_num = thread_serial_num + STACK_TRACE_ID;
1747     writer()->write_u1(HPROF_GC_ROOT_THREAD_OBJ);
1748     writer()->write_objectID(threadObj);
1749     writer()->write_u4(thread_serial_num);  // thread number
1750     writer()->write_u4(stack_serial_num);   // stack trace serial number
1751     int num_frames = do_thread(thread, thread_serial_num);
1752     assert(num_frames == _stack_traces[i]->get_stack_depth(),
1753            "total number of Java frames not matched");
1754   }
1755 }
1756 
1757 
1758 // The VM operation that dumps the heap. The dump consists of the following
1759 // records:
1760 //
1761 //  HPROF_HEADER
1762 //  [HPROF_UTF8]*
1763 //  [HPROF_LOAD_CLASS]*
1764 //  [[HPROF_FRAME]*|HPROF_TRACE]*
1765 //  [HPROF_GC_CLASS_DUMP]*
1766 //  [HPROF_HEAP_DUMP_SEGMENT]*
1767 //  HPROF_HEAP_DUMP_END
1768 //
1769 // The HPROF_TRACE records represent the stack traces where the heap dump
1770 // is generated and a "dummy trace" record which does not include
1771 // any frames. The dummy trace record is used to be referenced as the
1772 // unknown object alloc site.
1773 //
1774 // Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records.
1775 // To allow the heap dump be generated in a single pass we remember the position
1776 // of the dump length and fix it up after all sub-records have been written.
1777 // To generate the sub-records we iterate over the heap, writing
1778 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
1779 // records as we go. Once that is done we write records for some of the GC
1780 // roots.
1781 
1782 void VM_HeapDumper::doit() {
1783 
1784   HandleMark hm;
1785   CollectedHeap* ch = Universe::heap();
1786 
1787   ch->ensure_parsability(false); // must happen, even if collection does
1788                                  // not happen (e.g. due to GC_locker)
1789 
1790   if (_gc_before_heap_dump) {
1791     if (GC_locker::is_active()) {
1792       warning("GC locker is held; pre-heapdump GC was skipped");
1793     } else {
1794       ch->collect_as_vm_thread(GCCause::_heap_dump);
1795     }
1796   }
1797 
1798   // At this point we should be the only dumper active, so
1799   // the following should be safe.
1800   set_global_dumper();
1801   set_global_writer();
1802 
1803   // Write the file header - we always use 1.0.2
1804   size_t used = ch->used();
1805   const char* header = "JAVA PROFILE 1.0.2";






1806 
1807   // header is few bytes long - no chance to overflow int
1808   writer()->write_raw((void*)header, (int)strlen(header));
1809   writer()->write_u1(0); // terminator
1810   writer()->write_u4(oopSize);
1811   writer()->write_u8(os::javaTimeMillis());
1812 
1813   // HPROF_UTF8 records
1814   SymbolTableDumper sym_dumper(writer());
1815   SymbolTable::symbols_do(&sym_dumper);
1816 
1817   // write HPROF_LOAD_CLASS records
1818   ClassLoaderDataGraph::classes_do(&do_load_class);
1819   Universe::basic_type_classes_do(&do_load_class);
1820 
1821   // write HPROF_FRAME and HPROF_TRACE records
1822   // this must be called after _klass_map is built when iterating the classes above.
1823   dump_stack_traces();
1824 
1825   // write HPROF_HEAP_DUMP_SEGMENT
1826   DumperSupport::write_dump_header(writer());
1827 
1828   // Writes HPROF_GC_CLASS_DUMP records
1829   ClassLoaderDataGraph::classes_do(&do_class_dump);
1830   Universe::basic_type_classes_do(&do_basic_type_array_class_dump);
1831   check_segment_length();
1832 
1833   // writes HPROF_GC_INSTANCE_DUMP records.
1834   // After each sub-record is written check_segment_length will be invoked
1835   // to check if the current segment exceeds a threshold. If so, a new
1836   // segment is started.
1837   // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
1838   // of the heap dump.
1839   HeapObjectDumper obj_dumper(this, writer());
1840   Universe::heap()->safe_object_iterate(&obj_dumper);
1841 
1842   // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
1843   do_threads();
1844   check_segment_length();
1845 
1846   // HPROF_GC_ROOT_MONITOR_USED
1847   MonitorUsedDumper mon_dumper(writer());
1848   ObjectSynchronizer::oops_do(&mon_dumper);
1849   check_segment_length();
1850 
1851   // HPROF_GC_ROOT_JNI_GLOBAL
1852   JNIGlobalsDumper jni_dumper(writer());
1853   JNIHandles::oops_do(&jni_dumper);
1854   Universe::oops_do(&jni_dumper);  // technically not jni roots, but global roots
1855                                    // for things like preallocated throwable backtraces
1856   check_segment_length();
1857 
1858   // HPROF_GC_ROOT_STICKY_CLASS
1859   StickyClassDumper class_dumper(writer());
1860   SystemDictionary::always_strong_classes_do(&class_dumper);
1861 
1862   // fixes up the length of the dump record and writes the HPROF_HEAP_DUMP_END record.
1863   DumperSupport::end_of_dump(writer());

1864 
1865   // Now we clear the global variables, so that a future dumper might run.
1866   clear_global_dumper();
1867   clear_global_writer();
1868 }
1869 
1870 void VM_HeapDumper::dump_stack_traces() {
1871   // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
1872   DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
1873   writer()->write_u4((u4) STACK_TRACE_ID);
1874   writer()->write_u4(0);                    // thread number
1875   writer()->write_u4(0);                    // frame count
1876 
1877   _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal);
1878   int frame_serial_num = 0;
1879   for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) {
1880     oop threadObj = thread->threadObj();
1881     if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
1882       // dump thread stack trace
1883       ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false);


< prev index next >