1 /*
   2  * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "classfile/classLoaderData.inline.hpp"
  28 #include "classfile/classLoaderDataGraph.hpp"
  29 #include "classfile/javaClasses.inline.hpp"
  30 #include "classfile/symbolTable.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "gc/shared/gcLocker.hpp"
  34 #include "gc/shared/gcVMOperations.hpp"
  35 #include "gc/shared/workgroup.hpp"
  36 #include "jfr/jfrEvents.hpp"
  37 #include "memory/allocation.inline.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "memory/universe.hpp"
  40 #include "oops/klass.inline.hpp"
  41 #include "oops/objArrayKlass.hpp"
  42 #include "oops/objArrayOop.inline.hpp"
  43 #include "oops/flatArrayKlass.hpp"
  44 #include "oops/flatArrayOop.inline.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "oops/typeArrayOop.inline.hpp"
  47 #include "runtime/fieldDescriptor.inline.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/javaCalls.hpp"
  51 #include "runtime/jniHandles.hpp"
  52 #include "runtime/os.hpp"
  53 #include "runtime/reflectionUtils.hpp"
  54 #include "runtime/thread.inline.hpp"
  55 #include "runtime/threadSMR.hpp"
  56 #include "runtime/vframe.hpp"
  57 #include "runtime/vmThread.hpp"
  58 #include "runtime/vmOperations.hpp"
  59 #include "services/heapDumper.hpp"
  60 #include "services/heapDumperCompression.hpp"
  61 #include "services/threadService.hpp"
  62 #include "utilities/macros.hpp"
  63 #include "utilities/ostream.hpp"
  64 
  65 /*
  66  * HPROF binary format - description copied from:
  67  *   src/share/demo/jvmti/hprof/hprof_io.c
  68  *
  69  *
  70  *  header    "JAVA PROFILE 1.0.2" (0-terminated)
  71  *
  72  *  u4        size of identifiers. Identifiers are used to represent
  73  *            UTF8 strings, objects, stack traces, etc. They usually
  74  *            have the same size as host pointers. For example, on
  75  *            Solaris and Win32, the size is 4.
  76  * u4         high word
  77  * u4         low word    number of milliseconds since 0:00 GMT, 1/1/70
  78  * [record]*  a sequence of records.
  79  *
  80  *
  81  * Record format:
  82  *
  83  * u1         a TAG denoting the type of the record
  84  * u4         number of *microseconds* since the time stamp in the
  85  *            header. (wraps around in a little more than an hour)
  86  * u4         number of bytes *remaining* in the record. Note that
  87  *            this number excludes the tag and the length field itself.
  88  * [u1]*      BODY of the record (a sequence of bytes)
  89  *
  90  *
  91  * The following TAGs are supported:
  92  *
  93  * TAG           BODY       notes
  94  *----------------------------------------------------------
  95  * HPROF_UTF8               a UTF8-encoded name
  96  *
  97  *               id         name ID
  98  *               [u1]*      UTF8 characters (no trailing zero)
  99  *
 100  * HPROF_LOAD_CLASS         a newly loaded class
 101  *
 102  *                u4        class serial number (> 0)
 103  *                id        class object ID
 104  *                u4        stack trace serial number
 105  *                id        class name ID
 106  *
 107  * HPROF_UNLOAD_CLASS       an unloading class
 108  *
 109  *                u4        class serial_number
 110  *
 111  * HPROF_FRAME              a Java stack frame
 112  *
 113  *                id        stack frame ID
 114  *                id        method name ID
 115  *                id        method signature ID
 116  *                id        source file name ID
 117  *                u4        class serial number
 118  *                i4        line number. >0: normal
 119  *                                       -1: unknown
 120  *                                       -2: compiled method
 121  *                                       -3: native method
 122  *
 123  * HPROF_TRACE              a Java stack trace
 124  *
 125  *               u4         stack trace serial number
 126  *               u4         thread serial number
 127  *               u4         number of frames
 128  *               [id]*      stack frame IDs
 129  *
 130  *
 131  * HPROF_ALLOC_SITES        a set of heap allocation sites, obtained after GC
 132  *
 133  *               u2         flags 0x0001: incremental vs. complete
 134  *                                0x0002: sorted by allocation vs. live
 135  *                                0x0004: whether to force a GC
 136  *               u4         cutoff ratio
 137  *               u4         total live bytes
 138  *               u4         total live instances
 139  *               u8         total bytes allocated
 140  *               u8         total instances allocated
 141  *               u4         number of sites that follow
 142  *               [u1        is_array: 0:  normal object
 143  *                                    2:  object array
 144  *                                    4:  boolean array
 145  *                                    5:  char array
 146  *                                    6:  float array
 147  *                                    7:  double array
 148  *                                    8:  byte array
 149  *                                    9:  short array
 150  *                                    10: int array
 151  *                                    11: long array
 152  *                u4        class serial number (may be zero during startup)
 153  *                u4        stack trace serial number
 154  *                u4        number of bytes alive
 155  *                u4        number of instances alive
 156  *                u4        number of bytes allocated
 157  *                u4]*      number of instance allocated
 158  *
 159  * HPROF_START_THREAD       a newly started thread.
 160  *
 161  *               u4         thread serial number (> 0)
 162  *               id         thread object ID
 163  *               u4         stack trace serial number
 164  *               id         thread name ID
 165  *               id         thread group name ID
 166  *               id         thread group parent name ID
 167  *
 168  * HPROF_END_THREAD         a terminating thread.
 169  *
 170  *               u4         thread serial number
 171  *
 172  * HPROF_HEAP_SUMMARY       heap summary
 173  *
 174  *               u4         total live bytes
 175  *               u4         total live instances
 176  *               u8         total bytes allocated
 177  *               u8         total instances allocated
 178  *
 179  * HPROF_HEAP_DUMP          denote a heap dump
 180  *
 181  *               [heap dump sub-records]*
 182  *
 183  *                          There are four kinds of heap dump sub-records:
 184  *
 185  *               u1         sub-record type
 186  *
 187  *               HPROF_GC_ROOT_UNKNOWN         unknown root
 188  *
 189  *                          id         object ID
 190  *
 191  *               HPROF_GC_ROOT_THREAD_OBJ      thread object
 192  *
 193  *                          id         thread object ID  (may be 0 for a
 194  *                                     thread newly attached through JNI)
 195  *                          u4         thread sequence number
 196  *                          u4         stack trace sequence number
 197  *
 198  *               HPROF_GC_ROOT_JNI_GLOBAL      JNI global ref root
 199  *
 200  *                          id         object ID
 201  *                          id         JNI global ref ID
 202  *
 203  *               HPROF_GC_ROOT_JNI_LOCAL       JNI local ref
 204  *
 205  *                          id         object ID
 206  *                          u4         thread serial number
 207  *                          u4         frame # in stack trace (-1 for empty)
 208  *
 209  *               HPROF_GC_ROOT_JAVA_FRAME      Java stack frame
 210  *
 211  *                          id         object ID
 212  *                          u4         thread serial number
 213  *                          u4         frame # in stack trace (-1 for empty)
 214  *
 215  *               HPROF_GC_ROOT_NATIVE_STACK    Native stack
 216  *
 217  *                          id         object ID
 218  *                          u4         thread serial number
 219  *
 220  *               HPROF_GC_ROOT_STICKY_CLASS    System class
 221  *
 222  *                          id         object ID
 223  *
 224  *               HPROF_GC_ROOT_THREAD_BLOCK    Reference from thread block
 225  *
 226  *                          id         object ID
 227  *                          u4         thread serial number
 228  *
 229  *               HPROF_GC_ROOT_MONITOR_USED    Busy monitor
 230  *
 231  *                          id         object ID
 232  *
 233  *               HPROF_GC_CLASS_DUMP           dump of a class object
 234  *
 235  *                          id         class object ID
 236  *                          u4         stack trace serial number
 237  *                          id         super class object ID
 238  *                          id         class loader object ID
 239  *                          id         signers object ID
 240  *                          id         protection domain object ID
 241  *                          id         reserved
 242  *                          id         reserved
 243  *
 244  *                          u4         instance size (in bytes)
 245  *
 246  *                          u2         size of constant pool
 247  *                          [u2,       constant pool index,
 248  *                           ty,       type
 249  *                                     2:  object
 250  *                                     4:  boolean
 251  *                                     5:  char
 252  *                                     6:  float
 253  *                                     7:  double
 254  *                                     8:  byte
 255  *                                     9:  short
 256  *                                     10: int
 257  *                                     11: long
 258  *                           vl]*      and value
 259  *
 260  *                          u2         number of static fields
 261  *                          [id,       static field name,
 262  *                           ty,       type,
 263  *                           vl]*      and value
 264  *
 265  *                          u2         number of inst. fields (not inc. super)
 266  *                          [id,       instance field name,
 267  *                           ty]*      type
 268  *
 269  *               HPROF_GC_INSTANCE_DUMP        dump of a normal object
 270  *
 271  *                          id         object ID
 272  *                          u4         stack trace serial number
 273  *                          id         class object ID
 274  *                          u4         number of bytes that follow
 275  *                          [vl]*      instance field values (class, followed
 276  *                                     by super, super's super ...)
 277  *
 278  *               HPROF_GC_OBJ_ARRAY_DUMP       dump of an object array
 279  *
 280  *                          id         array object ID
 281  *                          u4         stack trace serial number
 282  *                          u4         number of elements
 283  *                          id         array class ID
 284  *                          [id]*      elements
 285  *
 286  *               HPROF_GC_PRIM_ARRAY_DUMP      dump of a primitive array
 287  *
 288  *                          id         array object ID
 289  *                          u4         stack trace serial number
 290  *                          u4         number of elements
 291  *                          u1         element type
 292  *                                     4:  boolean array
 293  *                                     5:  char array
 294  *                                     6:  float array
 295  *                                     7:  double array
 296  *                                     8:  byte array
 297  *                                     9:  short array
 298  *                                     10: int array
 299  *                                     11: long array
 300  *                          [u1]*      elements
 301  *
 302  * HPROF_CPU_SAMPLES        a set of sample traces of running threads
 303  *
 304  *                u4        total number of samples
 305  *                u4        # of traces
 306  *               [u4        # of samples
 307  *                u4]*      stack trace serial number
 308  *
 309  * HPROF_CONTROL_SETTINGS   the settings of on/off switches
 310  *
 311  *                u4        0x00000001: alloc traces on/off
 312  *                          0x00000002: cpu sampling on/off
 313  *                u2        stack trace depth
 314  *
 315  *
 316  * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
 317  * be generated as a sequence of heap dump segments. This sequence is
 318  * terminated by an end record. The additional tags allowed by format
 319  * "JAVA PROFILE 1.0.2" are:
 320  *
 321  * HPROF_HEAP_DUMP_SEGMENT  denote a heap dump segment
 322  *
 323  *               [heap dump sub-records]*
 324  *               The same sub-record types allowed by HPROF_HEAP_DUMP
 325  *
 326  * HPROF_HEAP_DUMP_END      denotes the end of a heap dump
 327  *
 328  */
 329 
 330 
 331 // HPROF tags
 332 
 333 typedef enum {
 334   // top-level records
 335   HPROF_UTF8                    = 0x01,
 336   HPROF_LOAD_CLASS              = 0x02,
 337   HPROF_UNLOAD_CLASS            = 0x03,
 338   HPROF_FRAME                   = 0x04,
 339   HPROF_TRACE                   = 0x05,
 340   HPROF_ALLOC_SITES             = 0x06,
 341   HPROF_HEAP_SUMMARY            = 0x07,
 342   HPROF_START_THREAD            = 0x0A,
 343   HPROF_END_THREAD              = 0x0B,
 344   HPROF_HEAP_DUMP               = 0x0C,
 345   HPROF_CPU_SAMPLES             = 0x0D,
 346   HPROF_CONTROL_SETTINGS        = 0x0E,
 347 
 348   // 1.0.2 record types
 349   HPROF_HEAP_DUMP_SEGMENT       = 0x1C,
 350   HPROF_HEAP_DUMP_END           = 0x2C,
 351 
 352   // field types
 353   HPROF_ARRAY_OBJECT            = 0x01,
 354   HPROF_NORMAL_OBJECT           = 0x02,
 355   HPROF_BOOLEAN                 = 0x04,
 356   HPROF_CHAR                    = 0x05,
 357   HPROF_FLOAT                   = 0x06,
 358   HPROF_DOUBLE                  = 0x07,
 359   HPROF_BYTE                    = 0x08,
 360   HPROF_SHORT                   = 0x09,
 361   HPROF_INT                     = 0x0A,
 362   HPROF_LONG                    = 0x0B,
 363 
 364   // data-dump sub-records
 365   HPROF_GC_ROOT_UNKNOWN         = 0xFF,
 366   HPROF_GC_ROOT_JNI_GLOBAL      = 0x01,
 367   HPROF_GC_ROOT_JNI_LOCAL       = 0x02,
 368   HPROF_GC_ROOT_JAVA_FRAME      = 0x03,
 369   HPROF_GC_ROOT_NATIVE_STACK    = 0x04,
 370   HPROF_GC_ROOT_STICKY_CLASS    = 0x05,
 371   HPROF_GC_ROOT_THREAD_BLOCK    = 0x06,
 372   HPROF_GC_ROOT_MONITOR_USED    = 0x07,
 373   HPROF_GC_ROOT_THREAD_OBJ      = 0x08,
 374   HPROF_GC_CLASS_DUMP           = 0x20,
 375   HPROF_GC_INSTANCE_DUMP        = 0x21,
 376   HPROF_GC_OBJ_ARRAY_DUMP       = 0x22,
 377   HPROF_GC_PRIM_ARRAY_DUMP      = 0x23
 378 } hprofTag;
 379 
 380 // Default stack trace ID (used for dummy HPROF_TRACE record)
 381 enum {
 382   STACK_TRACE_ID = 1,
 383   INITIAL_CLASS_COUNT = 200
 384 };
 385 
 386 
 387 // Inlined fields of primitive classes are dumped as identity objects and require unique object ids.
 388 // We cannot use address of the object in container oop (as we do for identity objects)
 389 // because the address can be the same for inlined object which contains inlined field with offset 0.
 390 class InlinedObjectSupport : public StackObj {
 391     friend class AbstractDumpWriter;
 392     InlinedObjectSupport(int initial_value = 1) : _counter(initial_value) {}
 393 
 394     int _counter;
 395     int getId() { return _counter++; }
 396 public:
 397     InlinedObjectSupport save() const {
 398         return InlinedObjectSupport(_counter);
 399     }
 400 };
 401 
 402 // Supports I/O operations for a dump
 403 // Base class for dump and parallel dump
 404 class AbstractDumpWriter : public StackObj {
 405  protected:
 406   enum {
 407     io_buffer_max_size = 1*M,
 408     io_buffer_max_waste = 10*K,
 409     dump_segment_header_size = 9
 410   };
 411 
 412   char* _buffer;    // internal buffer
 413   size_t _size;
 414   size_t _pos;
 415 
 416   bool _in_dump_segment; // Are we currently in a dump segment?
 417   bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
 418   DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
 419   DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
 420 
 421   virtual void flush(bool force = false) = 0;
 422 
 423   char* buffer() const                          { return _buffer; }
 424   size_t buffer_size() const                    { return _size; }
 425   void set_position(size_t pos)                 { _pos = pos; }
 426 
 427   // Can be called if we have enough room in the buffer.
 428   void write_fast(void* s, size_t len);
 429 
 430   // Returns true if we have enough room in the buffer for 'len' bytes.
 431   bool can_write_fast(size_t len);
 432 
 433   InlinedObjectSupport _inlined_object_support;
 434 
 435  public:
 436   AbstractDumpWriter() :
 437     _buffer(NULL),
 438     _size(io_buffer_max_size),
 439     _pos(0),
 440     _in_dump_segment(false) { }
 441 
 442   // total number of bytes written to the disk
 443   virtual julong bytes_written() const = 0;
 444   virtual char const* error() const = 0;
 445 
 446   InlinedObjectSupport& inlined_object_support() { return _inlined_object_support; }
 447 
 448   size_t position() const                       { return _pos; }
 449   // writer functions
 450   virtual void write_raw(void* s, size_t len);
 451   void write_u1(u1 x);
 452   void write_u2(u2 x);
 453   void write_u4(u4 x);
 454   void write_u8(u8 x);
 455   void write_objectID(oop o);
 456   void write_inlinedObjectID(InlinedObjectSupport &inlinedObjectSupport);
 457   void write_symbolID(Symbol* o);
 458   void write_classID(Klass* k);
 459   void write_id(u4 x);
 460 
 461   // Start a new sub-record. Starts a new heap dump segment if needed.
 462   void start_sub_record(u1 tag, u4 len);
 463   // Ends the current sub-record.
 464   void end_sub_record();
 465   // Finishes the current dump segment if not already finished.
 466   void finish_dump_segment(bool force_flush = false);
 467   // Refresh to get new buffer
 468   void refresh() {
 469     assert (_in_dump_segment ==false, "Sanity check");
 470     _buffer = NULL;
 471     _size = io_buffer_max_size;
 472     _pos = 0;
 473     // Force flush to guarantee data from parallel dumper are written.
 474     flush(true);
 475   }
 476   // Called when finished to release the threads.
 477   virtual void deactivate() = 0;
 478 };
 479 
 480 void AbstractDumpWriter::write_fast(void* s, size_t len) {
 481   assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
 482   assert(buffer_size() - position() >= len, "Must fit");
 483   debug_only(_sub_record_left -= len);
 484   memcpy(buffer() + position(), s, len);
 485   set_position(position() + len);
 486 }
 487 
 488 bool AbstractDumpWriter::can_write_fast(size_t len) {
 489   return buffer_size() - position() >= len;
 490 }
 491 
 492 // write raw bytes
 493 void AbstractDumpWriter::write_raw(void* s, size_t len) {
 494   assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
 495   debug_only(_sub_record_left -= len);
 496 
 497   // flush buffer to make room.
 498   while (len > buffer_size() - position()) {
 499     assert(!_in_dump_segment || _is_huge_sub_record,
 500            "Cannot overflow in non-huge sub-record.");
 501     size_t to_write = buffer_size() - position();
 502     memcpy(buffer() + position(), s, to_write);
 503     s = (void*) ((char*) s + to_write);
 504     len -= to_write;
 505     set_position(position() + to_write);
 506     flush();
 507   }
 508 
 509   memcpy(buffer() + position(), s, len);
 510   set_position(position() + len);
 511 }
 512 
 513 // Makes sure we inline the fast write into the write_u* functions. This is a big speedup.
 514 #define WRITE_KNOWN_TYPE(p, len) do { if (can_write_fast((len))) write_fast((p), (len)); \
 515                                       else write_raw((p), (len)); } while (0)
 516 
 517 void AbstractDumpWriter::write_u1(u1 x) {
 518   WRITE_KNOWN_TYPE((void*) &x, 1);
 519 }
 520 
 521 void AbstractDumpWriter::write_u2(u2 x) {
 522   u2 v;
 523   Bytes::put_Java_u2((address)&v, x);
 524   WRITE_KNOWN_TYPE((void*)&v, 2);
 525 }
 526 
 527 void AbstractDumpWriter::write_u4(u4 x) {
 528   u4 v;
 529   Bytes::put_Java_u4((address)&v, x);
 530   WRITE_KNOWN_TYPE((void*)&v, 4);
 531 }
 532 
 533 void AbstractDumpWriter::write_u8(u8 x) {
 534   u8 v;
 535   Bytes::put_Java_u8((address)&v, x);
 536   WRITE_KNOWN_TYPE((void*)&v, 8);
 537 }
 538 
 539 void AbstractDumpWriter::write_objectID(oop o) {
 540   address a = cast_from_oop<address>(o);
 541 #ifdef _LP64
 542   write_u8((u8)a);
 543 #else
 544   write_u4((u4)a);
 545 #endif
 546 }
 547 
 548 void AbstractDumpWriter::write_inlinedObjectID(InlinedObjectSupport& inlinedObjectSupport) {
 549 #ifdef _LP64
 550   write_u8(inlinedObjectSupport.getId());
 551 #else
 552   write_u4(inlinedObjectSupport.getId());
 553 #endif
 554 }
 555 
 556 void AbstractDumpWriter::write_symbolID(Symbol* s) {
 557   address a = (address)((uintptr_t)s);
 558 #ifdef _LP64
 559   write_u8((u8)a);
 560 #else
 561   write_u4((u4)a);
 562 #endif
 563 }
 564 
 565 void AbstractDumpWriter::write_id(u4 x) {
 566 #ifdef _LP64
 567   write_u8((u8) x);
 568 #else
 569   write_u4(x);
 570 #endif
 571 }
 572 
 573 // We use java mirror as the class ID
 574 void AbstractDumpWriter::write_classID(Klass* k) {
 575   write_objectID(k->java_mirror());
 576 }
 577 
 578 void AbstractDumpWriter::finish_dump_segment(bool force_flush) {
 579   if (_in_dump_segment) {
 580     assert(_sub_record_left == 0, "Last sub-record not written completely");
 581     assert(_sub_record_ended, "sub-record must have ended");
 582 
 583     // Fix up the dump segment length if we haven't written a huge sub-record last
 584     // (in which case the segment length was already set to the correct value initially).
 585     if (!_is_huge_sub_record) {
 586       assert(position() > dump_segment_header_size, "Dump segment should have some content");
 587       Bytes::put_Java_u4((address) (buffer() + 5),
 588                          (u4) (position() - dump_segment_header_size));
 589     } else {
 590       // Finish process huge sub record
 591       // Set _is_huge_sub_record to false so the parallel dump writer can flush data to file.
 592       _is_huge_sub_record = false;
 593     }
 594 
 595     _in_dump_segment = false;
 596     flush(force_flush);
 597   }
 598 }
 599 
 600 void AbstractDumpWriter::start_sub_record(u1 tag, u4 len) {
 601   if (!_in_dump_segment) {
 602     if (position() > 0) {
 603       flush();
 604     }
 605 
 606     assert(position() == 0 && buffer_size() > dump_segment_header_size, "Must be at the start");
 607 
 608     write_u1(HPROF_HEAP_DUMP_SEGMENT);
 609     write_u4(0); // timestamp
 610     // Will be fixed up later if we add more sub-records.  If this is a huge sub-record,
 611     // this is already the correct length, since we don't add more sub-records.
 612     write_u4(len);
 613     assert(Bytes::get_Java_u4((address)(buffer() + 5)) == len, "Inconsitent size!");
 614     _in_dump_segment = true;
 615     _is_huge_sub_record = len > buffer_size() - dump_segment_header_size;
 616   } else if (_is_huge_sub_record || (len > buffer_size() - position())) {
 617     // This object will not fit in completely or the last sub-record was huge.
 618     // Finish the current segement and try again.
 619     finish_dump_segment();
 620     start_sub_record(tag, len);
 621 
 622     return;
 623   }
 624 
 625   debug_only(_sub_record_left = len);
 626   debug_only(_sub_record_ended = false);
 627 
 628   write_u1(tag);
 629 }
 630 
 631 void AbstractDumpWriter::end_sub_record() {
 632   assert(_in_dump_segment, "must be in dump segment");
 633   assert(_sub_record_left == 0, "sub-record not written completely");
 634   assert(!_sub_record_ended, "Must not have ended yet");
 635   debug_only(_sub_record_ended = true);
 636 }
 637 
 638 // Supports I/O operations for a dump
 639 
 640 class DumpWriter : public AbstractDumpWriter {
 641  private:
 642   CompressionBackend _backend; // Does the actual writing.
 643  protected:
 644   virtual void flush(bool force = false);
 645 
 646  public:
 647   // Takes ownership of the writer and compressor.
 648   DumpWriter(AbstractWriter* writer, AbstractCompressor* compressor);
 649 
 650   // total number of bytes written to the disk
 651   virtual julong bytes_written() const          { return (julong) _backend.get_written(); }
 652 
 653   virtual char const* error() const             { return _backend.error(); }
 654 
 655   // Called by threads used for parallel writing.
 656   void writer_loop()                    { _backend.thread_loop(); }
 657   // Called when finish to release the threads.
 658   virtual void deactivate()             { flush(); _backend.deactivate(); }
 659   // Get the backend pointer, used by parallel dump writer.
 660   CompressionBackend* backend_ptr() { return &_backend; }
 661 
 662 };
 663 
 664 // Check for error after constructing the object and destroy it in case of an error.
 665 DumpWriter::DumpWriter(AbstractWriter* writer, AbstractCompressor* compressor) :
 666   AbstractDumpWriter(),
 667   _backend(writer, compressor, io_buffer_max_size, io_buffer_max_waste) {
 668   flush();
 669 }
 670 
 671 // flush any buffered bytes to the file
 672 void DumpWriter::flush(bool force) {
 673   _backend.get_new_buffer(&_buffer, &_pos, &_size, force);
 674 }
 675 
 676 // Buffer queue used for parallel dump.
 677 struct ParWriterBufferQueueElem {
 678   char* _buffer;
 679   size_t _used;
 680   ParWriterBufferQueueElem* _next;
 681 };
 682 
 683 class ParWriterBufferQueue : public CHeapObj<mtInternal> {
 684  private:
 685   ParWriterBufferQueueElem* _head;
 686   ParWriterBufferQueueElem* _tail;
 687   uint _length;
 688  public:
 689   ParWriterBufferQueue() : _head(NULL), _tail(NULL), _length(0) { }
 690 
 691   void enqueue(ParWriterBufferQueueElem* entry) {
 692     if (_head == NULL) {
 693       assert(is_empty() && _tail == NULL, "Sanity check");
 694       _head = _tail = entry;
 695     } else {
 696       assert ((_tail->_next == NULL && _tail->_buffer != NULL), "Buffer queue is polluted");
 697       _tail->_next = entry;
 698       _tail = entry;
 699     }
 700     _length++;
 701     assert(_tail->_next == NULL, "Bufer queue is polluted");
 702   }
 703 
 704   ParWriterBufferQueueElem* dequeue() {
 705     if (_head == NULL)  return NULL;
 706     ParWriterBufferQueueElem* entry = _head;
 707     assert (entry->_buffer != NULL, "polluted buffer in writer list");
 708     _head = entry->_next;
 709     if (_head == NULL) {
 710       _tail = NULL;
 711     }
 712     entry->_next = NULL;
 713     _length--;
 714     return entry;
 715   }
 716 
 717   bool is_empty() {
 718     return _length == 0;
 719   }
 720 
 721   uint length() { return _length; }
 722 };
 723 
 724 // Support parallel heap dump.
 725 class ParDumpWriter : public AbstractDumpWriter {
 726  private:
 727   // Lock used to guarantee the integrity of multiple buffers writing.
 728   static Monitor* _lock;
 729   // Pointer of backend from global DumpWriter.
 730   CompressionBackend* _backend_ptr;
 731   char const * _err;
 732   ParWriterBufferQueue* _buffer_queue;
 733   size_t _internal_buffer_used;
 734   char* _buffer_base;
 735   bool _split_data;
 736   static const uint BackendFlushThreshold = 2;
 737  protected:
 738   virtual void flush(bool force = false) {
 739     assert(_pos != 0, "must not be zero");
 740     if (_pos != 0) {
 741       refresh_buffer();
 742     }
 743 
 744     if (_split_data || _is_huge_sub_record) {
 745       return;
 746     }
 747 
 748     if (should_flush_buf_list(force)) {
 749       assert(!_in_dump_segment && !_split_data && !_is_huge_sub_record, "incomplete data send to backend!\n");
 750       flush_to_backend(force);
 751     }
 752   }
 753 
 754  public:
 755   // Check for error after constructing the object and destroy it in case of an error.
 756   ParDumpWriter(DumpWriter* dw) :
 757     AbstractDumpWriter(),
 758     _backend_ptr(dw->backend_ptr()),
 759     _buffer_queue((new (std::nothrow) ParWriterBufferQueue())),
 760     _buffer_base(NULL),
 761     _split_data(false) {
 762     // prepare internal buffer
 763     allocate_internal_buffer();
 764   }
 765 
 766   ~ParDumpWriter() {
 767      assert(_buffer_queue != NULL, "Sanity check");
 768      assert((_internal_buffer_used == 0) && (_buffer_queue->is_empty()),
 769             "All data must be send to backend");
 770      if (_buffer_base != NULL) {
 771        os::free(_buffer_base);
 772        _buffer_base = NULL;
 773      }
 774      delete _buffer_queue;
 775      _buffer_queue = NULL;
 776   }
 777 
 778   // total number of bytes written to the disk
 779   virtual julong bytes_written() const          { return (julong) _backend_ptr->get_written(); }
 780   virtual char const* error() const { return _err == NULL ? _backend_ptr->error() : _err; }
 781 
 782   static void before_work() {
 783     assert(_lock == NULL, "ParDumpWriter lock must be initialized only once");
 784     _lock = new (std::nothrow) PaddedMonitor(Mutex::nonleaf, "ParallelHProfWriter_lock", Mutex::_safepoint_check_always);
 785   }
 786 
 787   static void after_work() {
 788     assert(_lock != NULL, "ParDumpWriter lock is not initialized");
 789     delete _lock;
 790     _lock = NULL;
 791   }
 792 
 793   // write raw bytes
 794   virtual void write_raw(void* s, size_t len) {
 795     assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
 796     debug_only(_sub_record_left -= len);
 797     assert(!_split_data, "Invalid split data");
 798     _split_data = true;
 799     // flush buffer to make room.
 800     while (len > buffer_size() - position()) {
 801       assert(!_in_dump_segment || _is_huge_sub_record,
 802              "Cannot overflow in non-huge sub-record.");
 803       size_t to_write = buffer_size() - position();
 804       memcpy(buffer() + position(), s, to_write);
 805       s = (void*) ((char*) s + to_write);
 806       len -= to_write;
 807       set_position(position() + to_write);
 808       flush();
 809     }
 810     _split_data = false;
 811     memcpy(buffer() + position(), s, len);
 812     set_position(position() + len);
 813   }
 814 
 815   virtual void deactivate()             { flush(true); _backend_ptr->deactivate(); }
 816 
 817  private:
 818   void allocate_internal_buffer() {
 819     assert(_buffer_queue != NULL, "Internal buffer queue is not ready when allocate internal buffer");
 820     assert(_buffer == NULL && _buffer_base == NULL, "current buffer must be NULL before allocate");
 821     _buffer_base = _buffer = (char*)os::malloc(io_buffer_max_size, mtInternal);
 822     if (_buffer == NULL) {
 823       set_error("Could not allocate buffer for writer");
 824       return;
 825     }
 826     _pos = 0;
 827     _internal_buffer_used = 0;
 828     _size = io_buffer_max_size;
 829   }
 830 
 831   void set_error(char const* new_error) {
 832     if ((new_error != NULL) && (_err == NULL)) {
 833       _err = new_error;
 834     }
 835   }
 836 
 837   // Add buffer to internal list
 838   void refresh_buffer() {
 839     size_t expected_total = _internal_buffer_used + _pos;
 840     if (expected_total < io_buffer_max_size - io_buffer_max_waste) {
 841       // reuse current buffer.
 842       _internal_buffer_used = expected_total;
 843       assert(_size - _pos == io_buffer_max_size - expected_total, "illegal resize of buffer");
 844       _size -= _pos;
 845       _buffer += _pos;
 846       _pos = 0;
 847 
 848       return;
 849     }
 850     // It is not possible here that expected_total is larger than io_buffer_max_size because
 851     // of limitation in write_xxx().
 852     assert(expected_total <= io_buffer_max_size, "buffer overflow");
 853     assert(_buffer - _buffer_base <= io_buffer_max_size, "internal buffer overflow");
 854     ParWriterBufferQueueElem* entry =
 855         (ParWriterBufferQueueElem*)os::malloc(sizeof(ParWriterBufferQueueElem), mtInternal);
 856     if (entry == NULL) {
 857       set_error("Heap dumper can allocate memory");
 858       return;
 859     }
 860     entry->_buffer = _buffer_base;
 861     entry->_used = expected_total;
 862     entry->_next = NULL;
 863     // add to internal buffer queue
 864     _buffer_queue->enqueue(entry);
 865     _buffer_base =_buffer = NULL;
 866     allocate_internal_buffer();
 867   }
 868 
 869   void reclaim_entry(ParWriterBufferQueueElem* entry) {
 870     assert(entry != NULL && entry->_buffer != NULL, "Invalid entry to reclaim");
 871     os::free(entry->_buffer);
 872     entry->_buffer = NULL;
 873     os::free(entry);
 874   }
 875 
 876   void flush_buffer(char* buffer, size_t used) {
 877     assert(_lock->owner() == Thread::current(), "flush buffer must hold lock");
 878     size_t max = io_buffer_max_size;
 879     // get_new_buffer
 880     _backend_ptr->flush_external_buffer(buffer, used, max);
 881   }
 882 
 883   bool should_flush_buf_list(bool force) {
 884     return force || _buffer_queue->length() > BackendFlushThreshold;
 885   }
 886 
 887   void flush_to_backend(bool force) {
 888     // Guarantee there is only one writer updating the backend buffers.
 889     MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
 890     while (!_buffer_queue->is_empty()) {
 891       ParWriterBufferQueueElem* entry = _buffer_queue->dequeue();
 892       flush_buffer(entry->_buffer, entry->_used);
 893       // Delete buffer and entry.
 894       reclaim_entry(entry);
 895       entry = NULL;
 896     }
 897     assert(_pos == 0, "available buffer must be empty before flush");
 898     // Flush internal buffer.
 899     if (_internal_buffer_used > 0) {
 900       flush_buffer(_buffer_base, _internal_buffer_used);
 901       os::free(_buffer_base);
 902       _pos = 0;
 903       _internal_buffer_used = 0;
 904       _buffer_base = _buffer = NULL;
 905       // Allocate internal buffer for future use.
 906       allocate_internal_buffer();
 907     }
 908   }
 909 };
 910 
 911 Monitor* ParDumpWriter::_lock = NULL;
 912 
 913 // Support class with a collection of functions used when dumping the heap
 914 
 915 class DumperSupport : AllStatic {
 916  public:
 917 
 918   // write a header of the given type
 919   static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
 920 
 921   // returns hprof tag for the given type signature
 922   static hprofTag sig2tag(Symbol* sig);
 923   // returns hprof tag for the given basic type
 924   static hprofTag type2tag(BasicType type);
 925   // Returns the size of the data to write.
 926   static u4 sig2size(Symbol* sig);
 927 
 928   // returns the size of the instance of the given class
 929   static u4 instance_size(Klass* k);
 930 
 931   // dump a jfloat
 932   static void dump_float(AbstractDumpWriter* writer, jfloat f);
 933   // dump a jdouble
 934   static void dump_double(AbstractDumpWriter* writer, jdouble d);
 935   // dumps the raw value of the given field; obj and offset specify the object (offset is 0 for identity objects)
 936   // for inlined fields writed object id generated by writer->inlined_object_support()
 937   static void dump_field_value(AbstractDumpWriter* writer, const FieldStream& fld, oop obj, int offset);
 938   // returns the size of the static fields; also counts the static fields
 939   static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
 940   // dumps static fields of the given class
 941   static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
 942   // dump the raw values of the instance fields of the given identity or inlined object;
 943   // for identity objects offset is 0 and 'klass' is o->klass(),
 944   // for inlined objects offset is the offset in the holder object, 'klass' is inlined object class
 945   static void dump_instance_fields(AbstractDumpWriter* writer, oop o, int offset, Klass* klass);
 946   // dump inlined instance fields of the given object (identity or inlined);
 947   // o is the holder object, offset and klass specify flattened field (or field of flattened field, etc.);
 948   // for identity object offset is 0 and klass is o->klass()
 949   static void dump_inlined_instance_fields(AbstractDumpWriter* writer, oop o, int offset, Klass* klass, InlinedObjectSupport &ios);
 950 
 951   // get the count of the instance fields for a given class
 952   static u2 get_instance_fields_count(InstanceKlass* ik);
 953   // dumps the definition of the instance fields for a given class
 954   static void dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k);
 955   // creates HPROF_GC_INSTANCE_DUMP record for the given inlined object
 956   static void dump_inlined_object(AbstractDumpWriter* writer, oop holder, int offset, InlineKlass* klass, InlinedObjectSupport& ios);
 957   // creates HPROF_GC_INSTANCE_DUMP record for the given object
 958   static void dump_instance(AbstractDumpWriter* writer, oop o);
 959   // creates HPROF_GC_CLASS_DUMP record for the given class and each of its
 960   // array classes
 961   static void dump_class_and_array_classes(AbstractDumpWriter* writer, Klass* k);
 962   // creates HPROF_GC_CLASS_DUMP record for a given primitive array
 963   // class (and each multi-dimensional array class too)
 964   static void dump_basic_type_array_class(AbstractDumpWriter* writer, Klass* k);
 965 
 966   // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
 967   static void dump_object_array(AbstractDumpWriter* writer, arrayOop array);
 968   // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
 969   static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
 970   // create HPROF_FRAME record for the given method and bci
 971   static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
 972 
 973   // check if we need to truncate an array
 974   static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
 975 
 976   // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
 977   static void end_of_dump(AbstractDumpWriter* writer);
 978 
 979   static oop mask_dormant_archived_object(oop o) {
 980     if (o != NULL && o->klass()->java_mirror() == NULL) {
 981       // Ignore this object since the corresponding java mirror is not loaded.
 982       // Might be a dormant archive object.
 983       return NULL;
 984     } else {
 985       return o;
 986     }
 987   }
 988 };
 989 
 990 // write a header of the given type
 991 void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
 992   writer->write_u1((u1)tag);
 993   writer->write_u4(0);                  // current ticks
 994   writer->write_u4(len);
 995 }
 996 
 997 // returns hprof tag for the given type signature
 998 hprofTag DumperSupport::sig2tag(Symbol* sig) {
 999   switch (sig->char_at(0)) {
1000     case JVM_SIGNATURE_CLASS    : return HPROF_NORMAL_OBJECT;
1001     case JVM_SIGNATURE_INLINE_TYPE: return HPROF_NORMAL_OBJECT;
1002     case JVM_SIGNATURE_ARRAY    : return HPROF_NORMAL_OBJECT;
1003     case JVM_SIGNATURE_BYTE     : return HPROF_BYTE;
1004     case JVM_SIGNATURE_CHAR     : return HPROF_CHAR;
1005     case JVM_SIGNATURE_FLOAT    : return HPROF_FLOAT;
1006     case JVM_SIGNATURE_DOUBLE   : return HPROF_DOUBLE;
1007     case JVM_SIGNATURE_INT      : return HPROF_INT;
1008     case JVM_SIGNATURE_LONG     : return HPROF_LONG;
1009     case JVM_SIGNATURE_SHORT    : return HPROF_SHORT;
1010     case JVM_SIGNATURE_BOOLEAN  : return HPROF_BOOLEAN;
1011     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
1012   }
1013 }
1014 
1015 hprofTag DumperSupport::type2tag(BasicType type) {
1016   switch (type) {
1017     case T_BYTE     : return HPROF_BYTE;
1018     case T_CHAR     : return HPROF_CHAR;
1019     case T_FLOAT    : return HPROF_FLOAT;
1020     case T_DOUBLE   : return HPROF_DOUBLE;
1021     case T_INT      : return HPROF_INT;
1022     case T_LONG     : return HPROF_LONG;
1023     case T_SHORT    : return HPROF_SHORT;
1024     case T_BOOLEAN  : return HPROF_BOOLEAN;
1025     default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
1026   }
1027 }
1028 
1029 u4 DumperSupport::sig2size(Symbol* sig) {
1030   switch (sig->char_at(0)) {
1031     case JVM_SIGNATURE_CLASS:
1032     case JVM_SIGNATURE_INLINE_TYPE:
1033     case JVM_SIGNATURE_ARRAY: return sizeof(address);
1034     case JVM_SIGNATURE_BOOLEAN:
1035     case JVM_SIGNATURE_BYTE: return 1;
1036     case JVM_SIGNATURE_SHORT:
1037     case JVM_SIGNATURE_CHAR: return 2;
1038     case JVM_SIGNATURE_INT:
1039     case JVM_SIGNATURE_FLOAT: return 4;
1040     case JVM_SIGNATURE_LONG:
1041     case JVM_SIGNATURE_DOUBLE: return 8;
1042     default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
1043   }
1044 }
1045 
1046 // dump a jfloat
1047 void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
1048   if (g_isnan(f)) {
1049     writer->write_u4(0x7fc00000);    // collapsing NaNs
1050   } else {
1051     union {
1052       int i;
1053       float f;
1054     } u;
1055     u.f = (float)f;
1056     writer->write_u4((u4)u.i);
1057   }
1058 }
1059 
1060 // dump a jdouble
1061 void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
1062   union {
1063     jlong l;
1064     double d;
1065   } u;
1066   if (g_isnan(d)) {                 // collapsing NaNs
1067     u.l = (jlong)(0x7ff80000);
1068     u.l = (u.l << 32);
1069   } else {
1070     u.d = (double)d;
1071   }
1072   writer->write_u8((u8)u.l);
1073 }
1074 
1075 // dumps the raw value of the given field; obj and offset specify the object (offset is 0 for identity objects)
1076 // for inlined fields writed object id generated by writer->inlined_object_support()
1077 void DumperSupport::dump_field_value(AbstractDumpWriter* writer, const FieldStream& fld, oop obj, int offset) {
1078   char type = fld.signature()->char_at(0);
1079   offset += fld.offset();
1080   switch (type) {
1081     case JVM_SIGNATURE_INLINE_TYPE: {
1082       if (fld.field_descriptor().is_inlined()) {
1083         writer->write_inlinedObjectID(writer->inlined_object_support());
1084         break;
1085       }
1086     }
1087     // pass through
1088     case JVM_SIGNATURE_CLASS :
1089     case JVM_SIGNATURE_ARRAY : {
1090       oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
1091       if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
1092         ResourceMark rm;
1093         log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1094                              p2i(o), o->klass()->external_name(),
1095                              p2i(obj), obj->klass()->external_name());
1096       }
1097       o = mask_dormant_archived_object(o);
1098       assert(oopDesc::is_oop_or_null(o), "Expected an oop or NULL at " PTR_FORMAT, p2i(o));
1099       writer->write_objectID(o);
1100       break;
1101     }
1102     case JVM_SIGNATURE_BYTE : {
1103       jbyte b = obj->byte_field(offset);
1104       writer->write_u1((u1)b);
1105       break;
1106     }
1107     case JVM_SIGNATURE_CHAR : {
1108       jchar c = obj->char_field(offset);
1109       writer->write_u2((u2)c);
1110       break;
1111     }
1112     case JVM_SIGNATURE_SHORT : {
1113       jshort s = obj->short_field(offset);
1114       writer->write_u2((u2)s);
1115       break;
1116     }
1117     case JVM_SIGNATURE_FLOAT : {
1118       jfloat f = obj->float_field(offset);
1119       dump_float(writer, f);
1120       break;
1121     }
1122     case JVM_SIGNATURE_DOUBLE : {
1123       jdouble d = obj->double_field(offset);
1124       dump_double(writer, d);
1125       break;
1126     }
1127     case JVM_SIGNATURE_INT : {
1128       jint i = obj->int_field(offset);
1129       writer->write_u4((u4)i);
1130       break;
1131     }
1132     case JVM_SIGNATURE_LONG : {
1133       jlong l = obj->long_field(offset);
1134       writer->write_u8((u8)l);
1135       break;
1136     }
1137     case JVM_SIGNATURE_BOOLEAN : {
1138       jboolean b = obj->bool_field(offset);
1139       writer->write_u1((u1)b);
1140       break;
1141     }
1142     default : {
1143       ShouldNotReachHere();
1144       break;
1145     }
1146   }
1147 }
1148 
1149 // returns the size of the instance of the given class
1150 u4 DumperSupport::instance_size(Klass* k) {
1151   InstanceKlass* ik = InstanceKlass::cast(k);
1152   u4 size = 0;
1153 
1154   for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1155     if (!fld.access_flags().is_static()) {
1156       size += sig2size(fld.signature());
1157     }
1158   }
1159   return size;
1160 }
1161 
1162 u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) {
1163   field_count = 0;
1164   u4 size = 0;
1165 
1166   for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
1167     if (fldc.access_flags().is_static()) {
1168       field_count++;
1169       size += sig2size(fldc.signature());
1170     }
1171   }
1172 
1173   // Add in resolved_references which is referenced by the cpCache
1174   // The resolved_references is an array per InstanceKlass holding the
1175   // strings and other oops resolved from the constant pool.
1176   oop resolved_references = ik->constants()->resolved_references_or_null();
1177   if (resolved_references != NULL) {
1178     field_count++;
1179     size += sizeof(address);
1180 
1181     // Add in the resolved_references of the used previous versions of the class
1182     // in the case of RedefineClasses
1183     InstanceKlass* prev = ik->previous_versions();
1184     while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) {
1185       field_count++;
1186       size += sizeof(address);
1187       prev = prev->previous_versions();
1188     }
1189   }
1190 
1191   // Also provide a pointer to the init_lock if present, so there aren't unreferenced int[0]
1192   // arrays.
1193   oop init_lock = ik->init_lock();
1194   if (init_lock != NULL) {
1195     field_count++;
1196     size += sizeof(address);
1197   }
1198 
1199   // We write the value itself plus a name and a one byte type tag per field.
1200   return size + field_count * (sizeof(address) + 1);
1201 }
1202 
1203 // dumps static fields of the given class
1204 void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1205   InstanceKlass* ik = InstanceKlass::cast(k);
1206 
1207   // dump the field descriptors and raw values
1208   for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1209     if (fld.access_flags().is_static()) {
1210       Symbol* sig = fld.signature();
1211 
1212       writer->write_symbolID(fld.name());   // name
1213       writer->write_u1(sig2tag(sig));       // type
1214 
1215       // if this changes, need to handle this properly (dump inlined objects after dump_static_fields)
1216       assert(!fld.field_descriptor().is_inlined(), "static fields cannot be inlined");
1217 
1218       // value
1219       dump_field_value(writer, fld, ik->java_mirror(), 0);
1220     }
1221   }
1222 
1223   // Add resolved_references for each class that has them
1224   oop resolved_references = ik->constants()->resolved_references_or_null();
1225   if (resolved_references != NULL) {
1226     writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1227     writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1228     writer->write_objectID(resolved_references);
1229 
1230     // Also write any previous versions
1231     InstanceKlass* prev = ik->previous_versions();
1232     while (prev != NULL && prev->constants()->resolved_references_or_null() != NULL) {
1233       writer->write_symbolID(vmSymbols::resolved_references_name());  // name
1234       writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1235       writer->write_objectID(prev->constants()->resolved_references());
1236       prev = prev->previous_versions();
1237     }
1238   }
1239 
1240   // Add init lock to the end if the class is not yet initialized
1241   oop init_lock = ik->init_lock();
1242   if (init_lock != NULL) {
1243     writer->write_symbolID(vmSymbols::init_lock_name());         // name
1244     writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type
1245     writer->write_objectID(init_lock);
1246   }
1247 }
1248 
1249 
1250 // dump the raw values of the instance fields of the given object
1251 void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o, int offset, Klass *klass) {
1252   InstanceKlass* ik = InstanceKlass::cast(klass);
1253 
1254   for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1255     if (!fld.access_flags().is_static()) {
1256       dump_field_value(writer, fld, o, offset);
1257     }
1258   }
1259 }
1260 
1261 void DumperSupport::dump_inlined_instance_fields(AbstractDumpWriter *writer, oop o, int offset, Klass *klass, InlinedObjectSupport &ios) {
1262   InstanceKlass* ik = InstanceKlass::cast(klass);
1263 
1264   assert(&ios != &writer->inlined_object_support(), "must be saved copy");
1265 
1266   for (FieldStream fld(ik, false, false); !fld.eos(); fld.next()) {
1267     if (!fld.access_flags().is_static()) {
1268       if (fld.field_descriptor().is_inlined()) {
1269         InstanceKlass* holder_klass = fld.field_descriptor().field_holder();
1270         InlineKlass* field_klass = InlineKlass::cast(holder_klass->get_inline_type_field_klass(fld.index()));
1271         dump_inlined_object(writer, o, offset + fld.offset(), field_klass, ios);
1272       }
1273     }
1274   }
1275 }
1276 
1277 // gets the count of the instance fields for a given class
1278 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1279   u2 field_count = 0;
1280 
1281   for (FieldStream fldc(ik, true, true); !fldc.eos(); fldc.next()) {
1282     if (!fldc.access_flags().is_static()) field_count++;
1283   }
1284 
1285   return field_count;
1286 }
1287 
1288 // dumps the definition of the instance fields for a given class
1289 void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k) {
1290   InstanceKlass* ik = InstanceKlass::cast(k);
1291 
1292   // dump the field descriptors
1293   for (FieldStream fld(ik, true, true); !fld.eos(); fld.next()) {
1294     if (!fld.access_flags().is_static()) {
1295       Symbol* sig = fld.signature();
1296 
1297       writer->write_symbolID(fld.name());   // name
1298       writer->write_u1(sig2tag(sig));       // type
1299     }
1300   }
1301 }
1302 
1303 // creates HPROF_GC_INSTANCE_DUMP record for the given inlined object
1304 void DumperSupport::dump_inlined_object(AbstractDumpWriter* writer, oop holder, int offset, InlineKlass* klass, InlinedObjectSupport& ios) {
1305   u4 is = instance_size(klass);
1306   u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1307 
1308   writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1309   writer->write_inlinedObjectID(ios);
1310 
1311   writer->write_u4(STACK_TRACE_ID);
1312 
1313   // class ID
1314   writer->write_classID(klass);
1315 
1316   // number of bytes that follow
1317   writer->write_u4(is);
1318 
1319   // the object if flattened, so all fields are stored without headers
1320   // update offset here instead of handling it in both dump_instance_fields and dump_inlined_instance_fields
1321   offset -= klass->first_field_offset();
1322 
1323   InlinedObjectSupport saved_ios = writer->inlined_object_support().save();
1324 
1325   // field values
1326   dump_instance_fields(writer, holder, offset, klass);
1327 
1328   writer->end_sub_record();
1329 
1330   // dump flattened fields
1331   dump_inlined_instance_fields(writer, holder, offset, klass, saved_ios);
1332 }
1333 
1334 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1335 void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o) {
1336   InstanceKlass* ik = InstanceKlass::cast(o->klass());
1337   u4 is = instance_size(ik);
1338   u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1339 
1340   writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1341   writer->write_objectID(o);
1342   writer->write_u4(STACK_TRACE_ID);
1343 
1344   // class ID
1345   writer->write_classID(ik);
1346 
1347   // number of bytes that follow
1348   writer->write_u4(is);
1349 
1350   InlinedObjectSupport saved_ios = writer->inlined_object_support().save();
1351 
1352   // field values
1353   dump_instance_fields(writer, o, 0, o->klass());
1354 
1355   writer->end_sub_record();
1356 
1357   // dump inlined fields
1358   dump_inlined_instance_fields(writer, o, 0, o->klass(), saved_ios);
1359 }
1360 
1361 // creates HPROF_GC_CLASS_DUMP record for the given class and each of
1362 // its array classes
1363 void DumperSupport::dump_class_and_array_classes(AbstractDumpWriter* writer, Klass* k) {
1364   InstanceKlass* ik = InstanceKlass::cast(k);
1365 
1366   // We can safepoint and do a heap dump at a point where we have a Klass,
1367   // but no java mirror class has been setup for it. So we need to check
1368   // that the class is at least loaded, to avoid crash from a null mirror.
1369   if (!ik->is_loaded()) {
1370     return;
1371   }
1372 
1373   u2 static_fields_count = 0;
1374   u4 static_size = get_static_fields_size(ik, static_fields_count);
1375   u2 instance_fields_count = get_instance_fields_count(ik);
1376   u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1377   u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size;
1378 
1379   writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1380 
1381   // class ID
1382   writer->write_classID(ik);
1383   writer->write_u4(STACK_TRACE_ID);
1384 
1385   // super class ID
1386   InstanceKlass* java_super = ik->java_super();
1387   if (java_super == NULL) {
1388     writer->write_objectID(oop(NULL));
1389   } else {
1390     writer->write_classID(java_super);
1391   }
1392 
1393   writer->write_objectID(ik->class_loader());
1394   writer->write_objectID(ik->signers());
1395   writer->write_objectID(ik->protection_domain());
1396 
1397   // reserved
1398   writer->write_objectID(oop(NULL));
1399   writer->write_objectID(oop(NULL));
1400 
1401   // instance size
1402   writer->write_u4(DumperSupport::instance_size(ik));
1403 
1404   // size of constant pool - ignored by HAT 1.1
1405   writer->write_u2(0);
1406 
1407   // static fields
1408   writer->write_u2(static_fields_count);
1409   dump_static_fields(writer, ik);
1410 
1411   // description of instance fields
1412   writer->write_u2(instance_fields_count);
1413   dump_instance_field_descriptors(writer, ik);
1414 
1415   writer->end_sub_record();
1416 
1417   // array classes
1418   k = k->array_klass_or_null();
1419   while (k != NULL) {
1420     assert(k->is_objArray_klass(), "not an ObjArrayKlass");
1421 
1422     u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1423     writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1424     writer->write_classID(k);
1425     writer->write_u4(STACK_TRACE_ID);
1426 
1427     // super class of array classes is java.lang.Object
1428     java_super = k->java_super();
1429     assert(java_super != NULL, "checking");
1430     writer->write_classID(java_super);
1431 
1432     writer->write_objectID(ik->class_loader());
1433     writer->write_objectID(ik->signers());
1434     writer->write_objectID(ik->protection_domain());
1435 
1436     writer->write_objectID(oop(NULL));    // reserved
1437     writer->write_objectID(oop(NULL));
1438     writer->write_u4(0);             // instance size
1439     writer->write_u2(0);             // constant pool
1440     writer->write_u2(0);             // static fields
1441     writer->write_u2(0);             // instance fields
1442 
1443     writer->end_sub_record();
1444 
1445     // get the array class for the next rank
1446     k = k->array_klass_or_null();
1447   }
1448 }
1449 
1450 // creates HPROF_GC_CLASS_DUMP record for a given primitive array
1451 // class (and each multi-dimensional array class too)
1452 void DumperSupport::dump_basic_type_array_class(AbstractDumpWriter* writer, Klass* k) {
1453   // array classes
1454   while (k != NULL) {
1455     Klass* klass = k;
1456 
1457     u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1458     writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1459     writer->write_classID(klass);
1460     writer->write_u4(STACK_TRACE_ID);
1461 
1462     // super class of array classes is java.lang.Object
1463     InstanceKlass* java_super = klass->java_super();
1464     assert(java_super != NULL, "checking");
1465     writer->write_classID(java_super);
1466 
1467     writer->write_objectID(oop(NULL));    // loader
1468     writer->write_objectID(oop(NULL));    // signers
1469     writer->write_objectID(oop(NULL));    // protection domain
1470 
1471     writer->write_objectID(oop(NULL));    // reserved
1472     writer->write_objectID(oop(NULL));
1473     writer->write_u4(0);             // instance size
1474     writer->write_u2(0);             // constant pool
1475     writer->write_u2(0);             // static fields
1476     writer->write_u2(0);             // instance fields
1477 
1478     writer->end_sub_record();
1479 
1480     // get the array class for the next rank
1481     k = klass->array_klass_or_null();
1482   }
1483 }
1484 
1485 // Hprof uses an u4 as record length field,
1486 // which means we need to truncate arrays that are too long.
1487 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1488   BasicType type = ArrayKlass::cast(array->klass())->element_type();
1489   assert((type >= T_BOOLEAN && type <= T_OBJECT) || type == T_INLINE_TYPE, "invalid array element type");
1490 
1491   int length = array->length();
1492 
1493   int type_size;
1494   if (type == T_OBJECT || type == T_INLINE_TYPE) {
1495     type_size = sizeof(address);
1496   } else {
1497     type_size = type2aelembytes(type);
1498   }
1499 
1500   size_t length_in_bytes = (size_t)length * type_size;
1501   uint max_bytes = max_juint - header_size;
1502 
1503   if (length_in_bytes > max_bytes) {
1504     length = max_bytes / type_size;
1505     length_in_bytes = (size_t)length * type_size;
1506 
1507     warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1508             type2name_tab[type], array->length(), length);
1509   }
1510   return length;
1511 }
1512 
1513 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1514 void DumperSupport::dump_object_array(AbstractDumpWriter* writer, arrayOop array) {
1515   // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1516   short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1517   int length = calculate_array_max_length(writer, array, header_size);
1518   u4 size = header_size + length * sizeof(address);
1519 
1520   writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1521   writer->write_objectID(array);
1522   writer->write_u4(STACK_TRACE_ID);
1523   writer->write_u4(length);
1524 
1525   // array class ID
1526   writer->write_classID(array->klass());
1527 
1528   InlinedObjectSupport ios = writer->inlined_object_support().save();
1529   if (array->is_objArray()) {
1530     // [id]* elements
1531     objArrayOop objArray = objArrayOop(array);
1532     for (int index = 0; index < length; index++) {
1533       oop o = objArray->obj_at(index);
1534       if (o != NULL && log_is_enabled(Debug, cds, heap) && mask_dormant_archived_object(o) == NULL) {
1535         ResourceMark rm;
1536         log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
1537                              p2i(o), o->klass()->external_name(),
1538                              p2i(array), array->klass()->external_name());
1539       }
1540       o = mask_dormant_archived_object(o);
1541       writer->write_objectID(o);
1542     }
1543   } else { // flatArray
1544     // [id]* elements
1545     flatArrayOop flatArray = flatArrayOop(array);
1546     for (int index = 0; index < length; index++) {
1547       writer->write_inlinedObjectID(writer->inlined_object_support());
1548     }
1549   }
1550 
1551   writer->end_sub_record();
1552 
1553   if (array->is_flatArray()) {
1554     flatArrayOop flatArray = flatArrayOop(array);
1555     FlatArrayKlass* vaklass = FlatArrayKlass::cast(flatArray->klass());
1556     InlineKlass* vklass = vaklass->element_klass();
1557     for (int index = 0; index < length; index++) {
1558       // need offset in the holder to read inlined object. calculate it from flatArrayOop::value_at_addr()
1559       int offset = (int)((address)flatArray->value_at_addr(index, vaklass->layout_helper())
1560                         - cast_from_oop<address>(flatArray));
1561 
1562       dump_inlined_object(writer, flatArray, offset, vklass, ios);
1563     }
1564   }
1565 }
1566 
1567 #define WRITE_ARRAY(Array, Type, Size, Length) \
1568   for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1569 
1570 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1571 void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1572   BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1573   // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1574   short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1575 
1576   int length = calculate_array_max_length(writer, array, header_size);
1577   int type_size = type2aelembytes(type);
1578   u4 length_in_bytes = (u4)length * type_size;
1579   u4 size = header_size + length_in_bytes;
1580 
1581   writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1582   writer->write_objectID(array);
1583   writer->write_u4(STACK_TRACE_ID);
1584   writer->write_u4(length);
1585   writer->write_u1(type2tag(type));
1586 
1587   // nothing to copy
1588   if (length == 0) {
1589     writer->end_sub_record();
1590     return;
1591   }
1592 
1593   // If the byte ordering is big endian then we can copy most types directly
1594 
1595   switch (type) {
1596     case T_INT : {
1597       if (Endian::is_Java_byte_ordering_different()) {
1598         WRITE_ARRAY(array, int, u4, length);
1599       } else {
1600         writer->write_raw((void*)(array->int_at_addr(0)), length_in_bytes);
1601       }
1602       break;
1603     }
1604     case T_BYTE : {
1605       writer->write_raw((void*)(array->byte_at_addr(0)), length_in_bytes);
1606       break;
1607     }
1608     case T_CHAR : {
1609       if (Endian::is_Java_byte_ordering_different()) {
1610         WRITE_ARRAY(array, char, u2, length);
1611       } else {
1612         writer->write_raw((void*)(array->char_at_addr(0)), length_in_bytes);
1613       }
1614       break;
1615     }
1616     case T_SHORT : {
1617       if (Endian::is_Java_byte_ordering_different()) {
1618         WRITE_ARRAY(array, short, u2, length);
1619       } else {
1620         writer->write_raw((void*)(array->short_at_addr(0)), length_in_bytes);
1621       }
1622       break;
1623     }
1624     case T_BOOLEAN : {
1625       if (Endian::is_Java_byte_ordering_different()) {
1626         WRITE_ARRAY(array, bool, u1, length);
1627       } else {
1628         writer->write_raw((void*)(array->bool_at_addr(0)), length_in_bytes);
1629       }
1630       break;
1631     }
1632     case T_LONG : {
1633       if (Endian::is_Java_byte_ordering_different()) {
1634         WRITE_ARRAY(array, long, u8, length);
1635       } else {
1636         writer->write_raw((void*)(array->long_at_addr(0)), length_in_bytes);
1637       }
1638       break;
1639     }
1640 
1641     // handle float/doubles in a special value to ensure than NaNs are
1642     // written correctly. TO DO: Check if we can avoid this on processors that
1643     // use IEEE 754.
1644 
1645     case T_FLOAT : {
1646       for (int i = 0; i < length; i++) {
1647         dump_float(writer, array->float_at(i));
1648       }
1649       break;
1650     }
1651     case T_DOUBLE : {
1652       for (int i = 0; i < length; i++) {
1653         dump_double(writer, array->double_at(i));
1654       }
1655       break;
1656     }
1657     default : ShouldNotReachHere();
1658   }
1659 
1660   writer->end_sub_record();
1661 }
1662 
1663 // create a HPROF_FRAME record of the given Method* and bci
1664 void DumperSupport::dump_stack_frame(AbstractDumpWriter* writer,
1665                                      int frame_serial_num,
1666                                      int class_serial_num,
1667                                      Method* m,
1668                                      int bci) {
1669   int line_number;
1670   if (m->is_native()) {
1671     line_number = -3;  // native frame
1672   } else {
1673     line_number = m->line_number_from_bci(bci);
1674   }
1675 
1676   write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
1677   writer->write_id(frame_serial_num);               // frame serial number
1678   writer->write_symbolID(m->name());                // method's name
1679   writer->write_symbolID(m->signature());           // method's signature
1680 
1681   assert(m->method_holder()->is_instance_klass(), "not InstanceKlass");
1682   writer->write_symbolID(m->method_holder()->source_file_name());  // source file name
1683   writer->write_u4(class_serial_num);               // class serial number
1684   writer->write_u4((u4) line_number);               // line number
1685 }
1686 
1687 
1688 // Support class used to generate HPROF_UTF8 records from the entries in the
1689 // SymbolTable.
1690 
1691 class SymbolTableDumper : public SymbolClosure {
1692  private:
1693   AbstractDumpWriter* _writer;
1694   AbstractDumpWriter* writer() const                { return _writer; }
1695  public:
1696   SymbolTableDumper(AbstractDumpWriter* writer)     { _writer = writer; }
1697   void do_symbol(Symbol** p);
1698 };
1699 
1700 void SymbolTableDumper::do_symbol(Symbol** p) {
1701   ResourceMark rm;
1702   Symbol* sym = *p;
1703   int len = sym->utf8_length();
1704   if (len > 0) {
1705     char* s = sym->as_utf8();
1706     DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1707     writer()->write_symbolID(sym);
1708     writer()->write_raw(s, len);
1709   }
1710 }
1711 
1712 // Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
1713 
1714 class JNILocalsDumper : public OopClosure {
1715  private:
1716   AbstractDumpWriter* _writer;
1717   u4 _thread_serial_num;
1718   int _frame_num;
1719   AbstractDumpWriter* writer() const                { return _writer; }
1720  public:
1721   JNILocalsDumper(AbstractDumpWriter* writer, u4 thread_serial_num) {
1722     _writer = writer;
1723     _thread_serial_num = thread_serial_num;
1724     _frame_num = -1;  // default - empty stack
1725   }
1726   void set_frame_number(int n) { _frame_num = n; }
1727   void do_oop(oop* obj_p);
1728   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1729 };
1730 
1731 
1732 void JNILocalsDumper::do_oop(oop* obj_p) {
1733   // ignore null handles
1734   oop o = *obj_p;
1735   if (o != NULL) {
1736     u4 size = 1 + sizeof(address) + 4 + 4;
1737     writer()->start_sub_record(HPROF_GC_ROOT_JNI_LOCAL, size);
1738     writer()->write_objectID(o);
1739     writer()->write_u4(_thread_serial_num);
1740     writer()->write_u4((u4)_frame_num);
1741     writer()->end_sub_record();
1742   }
1743 }
1744 
1745 
1746 // Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
1747 
1748 class JNIGlobalsDumper : public OopClosure {
1749  private:
1750   AbstractDumpWriter* _writer;
1751   AbstractDumpWriter* writer() const                { return _writer; }
1752 
1753  public:
1754   JNIGlobalsDumper(AbstractDumpWriter* writer) {
1755     _writer = writer;
1756   }
1757   void do_oop(oop* obj_p);
1758   void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1759 };
1760 
1761 void JNIGlobalsDumper::do_oop(oop* obj_p) {
1762   oop o = NativeAccess<AS_NO_KEEPALIVE>::oop_load(obj_p);
1763 
1764   // ignore these
1765   if (o == NULL) return;
1766   // we ignore global ref to symbols and other internal objects
1767   if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
1768     u4 size = 1 + 2 * sizeof(address);
1769     writer()->start_sub_record(HPROF_GC_ROOT_JNI_GLOBAL, size);
1770     writer()->write_objectID(o);
1771     writer()->write_objectID((oopDesc*)obj_p);      // global ref ID
1772     writer()->end_sub_record();
1773   }
1774 };
1775 
1776 // Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
1777 
1778 class StickyClassDumper : public KlassClosure {
1779  private:
1780   AbstractDumpWriter* _writer;
1781   AbstractDumpWriter* writer() const                { return _writer; }
1782  public:
1783   StickyClassDumper(AbstractDumpWriter* writer) {
1784     _writer = writer;
1785   }
1786   void do_klass(Klass* k) {
1787     if (k->is_instance_klass()) {
1788       InstanceKlass* ik = InstanceKlass::cast(k);
1789       u4 size = 1 + sizeof(address);
1790       writer()->start_sub_record(HPROF_GC_ROOT_STICKY_CLASS, size);
1791       writer()->write_classID(ik);
1792       writer()->end_sub_record();
1793     }
1794   }
1795 };
1796 
1797 // Large object heap dump support.
1798 // To avoid memory consumption, when dumping large objects such as huge array and
1799 // large objects whose size are larger than LARGE_OBJECT_DUMP_THRESHOLD, the scanned
1800 // partial object/array data will be sent to the backend directly instead of caching
1801 // the whole object/array in the internal buffer.
1802 // The HeapDumpLargeObjectList is used to save the large object when dumper scans
1803 // the heap. The large objects could be added (push) parallelly by multiple dumpers,
1804 // But they will be removed (popped) serially only by the VM thread.
1805 class HeapDumpLargeObjectList : public CHeapObj<mtInternal> {
1806  private:
1807   class HeapDumpLargeObjectListElem : public CHeapObj<mtInternal> {
1808    public:
1809     HeapDumpLargeObjectListElem(oop obj) : _obj(obj), _next(NULL) { }
1810     oop _obj;
1811     HeapDumpLargeObjectListElem* _next;
1812   };
1813 
1814   volatile HeapDumpLargeObjectListElem* _head;
1815 
1816  public:
1817   HeapDumpLargeObjectList() : _head(NULL) { }
1818 
1819   void atomic_push(oop obj) {
1820     assert (obj != NULL, "sanity check");
1821     HeapDumpLargeObjectListElem* entry = new HeapDumpLargeObjectListElem(obj);
1822     if (entry == NULL) {
1823       warning("failed to allocate element for large object list");
1824       return;
1825     }
1826     assert (entry->_obj != NULL, "sanity check");
1827     while (true) {
1828       volatile HeapDumpLargeObjectListElem* old_head = Atomic::load_acquire(&_head);
1829       HeapDumpLargeObjectListElem* new_head = entry;
1830       if (Atomic::cmpxchg(&_head, old_head, new_head) == old_head) {
1831         // successfully push
1832         new_head->_next = (HeapDumpLargeObjectListElem*)old_head;
1833         return;
1834       }
1835     }
1836   }
1837 
1838   oop pop() {
1839     if (_head == NULL) {
1840       return NULL;
1841     }
1842     HeapDumpLargeObjectListElem* entry = (HeapDumpLargeObjectListElem*)_head;
1843     _head = _head->_next;
1844     assert (entry != NULL, "illegal larger object list entry");
1845     oop ret = entry->_obj;
1846     delete entry;
1847     assert (ret != NULL, "illegal oop pointer");
1848     return ret;
1849   }
1850 
1851   void drain(ObjectClosure* cl) {
1852     while (_head !=  NULL) {
1853       cl->do_object(pop());
1854     }
1855   }
1856 
1857   bool is_empty() {
1858     return _head == NULL;
1859   }
1860 
1861   static const size_t LargeObjectSizeThreshold = 1 << 20; // 1 MB
1862 };
1863 
1864 class VM_HeapDumper;
1865 
1866 // Support class using when iterating over the heap.
1867 class HeapObjectDumper : public ObjectClosure {
1868  private:
1869   AbstractDumpWriter* _writer;
1870   HeapDumpLargeObjectList* _list;
1871 
1872   AbstractDumpWriter* writer()                  { return _writer; }
1873   bool is_large(oop o);
1874  public:
1875   HeapObjectDumper(AbstractDumpWriter* writer, HeapDumpLargeObjectList* list = NULL) {
1876     _writer = writer;
1877     _list = list;
1878   }
1879 
1880   // called for each object in the heap
1881   void do_object(oop o);
1882 };
1883 
1884 void HeapObjectDumper::do_object(oop o) {
1885   // skip classes as these emitted as HPROF_GC_CLASS_DUMP records
1886   if (o->klass() == vmClasses::Class_klass()) {
1887     if (!java_lang_Class::is_primitive(o)) {
1888       return;
1889     }
1890   }
1891 
1892   if (DumperSupport::mask_dormant_archived_object(o) == NULL) {
1893     log_debug(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)", p2i(o), o->klass()->external_name());
1894     return;
1895   }
1896 
1897   // If large object list exists and it is large object/array,
1898   // add oop into the list and skip scan. VM thread will process it later.
1899   if (_list != NULL && is_large(o)) {
1900     _list->atomic_push(o);
1901     return;
1902   }
1903 
1904   if (o->is_instance()) {
1905     // create a HPROF_GC_INSTANCE record for each object
1906     DumperSupport::dump_instance(writer(), o);
1907   } else if (o->is_objArray() || o->is_flatArray()) {
1908     // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1909     DumperSupport::dump_object_array(writer(), arrayOop(o));
1910   } else if (o->is_typeArray()) {
1911     // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1912     DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1913   }
1914 }
1915 
1916 bool HeapObjectDumper::is_large(oop o) {
1917   size_t size = 0;
1918   if (o->is_instance()) {
1919     // Use o->size() * 8 as the upper limit of instance size to avoid iterating static fields
1920     size = o->size() * 8;
1921   } else if (o->is_objArray()) {
1922     objArrayOop array = objArrayOop(o);
1923     BasicType type = ArrayKlass::cast(array->klass())->element_type();
1924     assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1925     int length = array->length();
1926     int type_size = sizeof(address);
1927     size = (size_t)length * type_size;
1928   } else if (o->is_typeArray()) {
1929     typeArrayOop array = typeArrayOop(o);
1930     BasicType type = ArrayKlass::cast(array->klass())->element_type();
1931     assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1932     int length = array->length();
1933     int type_size = type2aelembytes(type);
1934     size = (size_t)length * type_size;
1935   }
1936   return size > HeapDumpLargeObjectList::LargeObjectSizeThreshold;
1937 }
1938 
1939 // The dumper controller for parallel heap dump
1940 class DumperController : public CHeapObj<mtInternal> {
1941  private:
1942    bool     _started;
1943    Monitor* _lock;
1944    uint   _dumper_number;
1945    uint   _complete_number;
1946 
1947  public:
1948    DumperController(uint number) :
1949      _started(false),
1950      _lock(new (std::nothrow) PaddedMonitor(Mutex::nonleaf, "DumperController_lock",
1951     Mutex::_safepoint_check_always)),
1952      _dumper_number(number),
1953      _complete_number(0) { }
1954 
1955    ~DumperController() { delete _lock; }
1956 
1957    void wait_for_start_signal() {
1958      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
1959      while (_started == false) {
1960        ml.wait();
1961      }
1962      assert(_started == true,  "dumper woke up with wrong state");
1963    }
1964 
1965    void start_dump() {
1966      assert (_started == false, "start dump with wrong state");
1967      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
1968      _started = true;
1969      ml.notify_all();
1970    }
1971 
1972    void dumper_complete() {
1973      assert (_started == true, "dumper complete with wrong state");
1974      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
1975      _complete_number++;
1976      ml.notify();
1977    }
1978 
1979    void wait_all_dumpers_complete() {
1980      assert (_started == true, "wrong state when wait for dumper complete");
1981      MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
1982      while (_complete_number != _dumper_number) {
1983         ml.wait();
1984      }
1985      _started = false;
1986    }
1987 };
1988 
1989 // The VM operation that performs the heap dump
1990 class VM_HeapDumper : public VM_GC_Operation, public AbstractGangTask {
1991  private:
1992   static VM_HeapDumper*   _global_dumper;
1993   static DumpWriter*      _global_writer;
1994   DumpWriter*             _local_writer;
1995   JavaThread*             _oome_thread;
1996   Method*                 _oome_constructor;
1997   bool                    _gc_before_heap_dump;
1998   GrowableArray<Klass*>*  _klass_map;
1999   ThreadStackTrace**      _stack_traces;
2000   int                     _num_threads;
2001   // parallel heap dump support
2002   uint                    _num_dumper_threads;
2003   uint                    _num_writer_threads;
2004   DumperController*       _dumper_controller;
2005   ParallelObjectIterator* _poi;
2006   HeapDumpLargeObjectList* _large_object_list;
2007 
2008   // VMDumperType is for thread that dumps both heap and non-heap data.
2009   static const size_t VMDumperType = 0;
2010   static const size_t WriterType = 1;
2011   static const size_t DumperType = 2;
2012   // worker id of VMDumper thread.
2013   static const size_t VMDumperWorkerId = 0;
2014 
2015   size_t get_worker_type(uint worker_id) {
2016     assert(_num_writer_threads >= 1, "Must be at least one writer");
2017     // worker id of VMDumper that dump heap and non-heap data
2018     if (worker_id == VMDumperWorkerId) {
2019       return VMDumperType;
2020     }
2021 
2022     // worker id of dumper starts from 1, which only dump heap datar
2023     if (worker_id < _num_dumper_threads) {
2024       return DumperType;
2025     }
2026 
2027     // worker id of writer starts from _num_dumper_threads
2028     return WriterType;
2029   }
2030 
2031   void prepare_parallel_dump(uint num_total) {
2032     assert (_dumper_controller == NULL, "dumper controller must be NULL");
2033     assert (num_total > 0, "active workers number must >= 1");
2034     // Dumper threads number must not be larger than active workers number.
2035     if (num_total < _num_dumper_threads) {
2036       _num_dumper_threads = num_total - 1;
2037     }
2038     // Calculate dumper and writer threads number.
2039     _num_writer_threads = num_total - _num_dumper_threads;
2040     // If dumper threads number is 1, only the VMThread works as a dumper.
2041     // If dumper threads number is equal to active workers, need at lest one worker thread as writer.
2042     if (_num_dumper_threads > 0 && _num_writer_threads == 0) {
2043       _num_writer_threads = 1;
2044       _num_dumper_threads = num_total - _num_writer_threads;
2045     }
2046     // Prepare parallel writer.
2047     if (_num_dumper_threads > 1) {
2048       ParDumpWriter::before_work();
2049       // Number of dumper threads that only iterate heap.
2050       uint _heap_only_dumper_threads = _num_dumper_threads - 1 /* VMDumper thread */;
2051       _dumper_controller = new (std::nothrow) DumperController(_heap_only_dumper_threads);
2052       _poi = Universe::heap()->parallel_object_iterator(_num_dumper_threads);
2053     }
2054   }
2055 
2056   void finish_parallel_dump() {
2057     if (_num_dumper_threads > 1) {
2058       ParDumpWriter::after_work();
2059     }
2060   }
2061 
2062   // accessors and setters
2063   static VM_HeapDumper* dumper()         {  assert(_global_dumper != NULL, "Error"); return _global_dumper; }
2064   static DumpWriter* writer()            {  assert(_global_writer != NULL, "Error"); return _global_writer; }
2065   void set_global_dumper() {
2066     assert(_global_dumper == NULL, "Error");
2067     _global_dumper = this;
2068   }
2069   void set_global_writer() {
2070     assert(_global_writer == NULL, "Error");
2071     _global_writer = _local_writer;
2072   }
2073   void clear_global_dumper() { _global_dumper = NULL; }
2074   void clear_global_writer() { _global_writer = NULL; }
2075 
2076   bool skip_operation() const;
2077 
2078   // writes a HPROF_LOAD_CLASS record
2079   class ClassesDo;
2080   static void do_load_class(Klass* k);
2081 
2082   // writes a HPROF_GC_CLASS_DUMP record for the given class
2083   // (and each array class too)
2084   static void do_class_dump(Klass* k);
2085 
2086   // writes a HPROF_GC_CLASS_DUMP records for a given basic type
2087   // array (and each multi-dimensional array too)
2088   static void do_basic_type_array_class_dump(Klass* k);
2089 
2090   // HPROF_GC_ROOT_THREAD_OBJ records
2091   int do_thread(JavaThread* thread, u4 thread_serial_num);
2092   void do_threads();
2093 
2094   void add_class_serial_number(Klass* k, int serial_num) {
2095     _klass_map->at_put_grow(serial_num, k);
2096   }
2097 
2098   // HPROF_TRACE and HPROF_FRAME records
2099   void dump_stack_traces();
2100 
2101   // large objects
2102   void dump_large_objects(ObjectClosure* writer);
2103 
2104  public:
2105   VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome, uint num_dump_threads) :
2106     VM_GC_Operation(0 /* total collections,      dummy, ignored */,
2107                     GCCause::_heap_dump /* GC Cause */,
2108                     0 /* total full collections, dummy, ignored */,
2109                     gc_before_heap_dump),
2110     AbstractGangTask("dump heap") {
2111     _local_writer = writer;
2112     _gc_before_heap_dump = gc_before_heap_dump;
2113     _klass_map = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, mtServiceability);
2114     _stack_traces = NULL;
2115     _num_threads = 0;
2116     _num_dumper_threads = num_dump_threads;
2117     _dumper_controller = NULL;
2118     _poi = NULL;
2119     _large_object_list = new (std::nothrow) HeapDumpLargeObjectList();
2120     if (oome) {
2121       assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
2122       // get OutOfMemoryError zero-parameter constructor
2123       InstanceKlass* oome_ik = vmClasses::OutOfMemoryError_klass();
2124       _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
2125                                                           vmSymbols::void_method_signature());
2126       // get thread throwing OOME when generating the heap dump at OOME
2127       _oome_thread = JavaThread::current();
2128     } else {
2129       _oome_thread = NULL;
2130       _oome_constructor = NULL;
2131     }
2132   }
2133 
2134   ~VM_HeapDumper() {
2135     if (_stack_traces != NULL) {
2136       for (int i=0; i < _num_threads; i++) {
2137         delete _stack_traces[i];
2138       }
2139       FREE_C_HEAP_ARRAY(ThreadStackTrace*, _stack_traces);
2140     }
2141     if (_poi != NULL) {
2142       delete _poi;
2143       _poi = NULL;
2144     }
2145     if (_dumper_controller != NULL) {
2146       delete _dumper_controller;
2147       _dumper_controller = NULL;
2148     }
2149     delete _klass_map;
2150     delete _large_object_list;
2151   }
2152 
2153   VMOp_Type type() const { return VMOp_HeapDumper; }
2154   void doit();
2155   void work(uint worker_id);
2156 };
2157 
2158 VM_HeapDumper* VM_HeapDumper::_global_dumper = NULL;
2159 DumpWriter*    VM_HeapDumper::_global_writer = NULL;
2160 
2161 bool VM_HeapDumper::skip_operation() const {
2162   return false;
2163 }
2164 
2165 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
2166 void DumperSupport::end_of_dump(AbstractDumpWriter* writer) {
2167   writer->finish_dump_segment();
2168 
2169   writer->write_u1(HPROF_HEAP_DUMP_END);
2170   writer->write_u4(0);
2171   writer->write_u4(0);
2172 }
2173 
2174 // writes a HPROF_LOAD_CLASS record for the class (and each of its
2175 // array classes)
2176 void VM_HeapDumper::do_load_class(Klass* k) {
2177   static u4 class_serial_num = 0;
2178 
2179   // len of HPROF_LOAD_CLASS record
2180   u4 remaining = 2*oopSize + 2*sizeof(u4);
2181 
2182   // write a HPROF_LOAD_CLASS for the class and each array class
2183   do {
2184     DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
2185 
2186     // class serial number is just a number
2187     writer()->write_u4(++class_serial_num);
2188 
2189     // class ID
2190     Klass* klass = k;
2191     writer()->write_classID(klass);
2192 
2193     // add the Klass* and class serial number pair
2194     dumper()->add_class_serial_number(klass, class_serial_num);
2195 
2196     writer()->write_u4(STACK_TRACE_ID);
2197 
2198     // class name ID
2199     Symbol* name = klass->name();
2200     writer()->write_symbolID(name);
2201 
2202     // write a LOAD_CLASS record for the array type (if it exists)
2203     k = klass->array_klass_or_null();
2204   } while (k != NULL);
2205 }
2206 
2207 // writes a HPROF_GC_CLASS_DUMP record for the given class
2208 void VM_HeapDumper::do_class_dump(Klass* k) {
2209   if (k->is_instance_klass()) {
2210     DumperSupport::dump_class_and_array_classes(writer(), k);
2211   }
2212 }
2213 
2214 // writes a HPROF_GC_CLASS_DUMP records for a given basic type
2215 // array (and each multi-dimensional array too)
2216 void VM_HeapDumper::do_basic_type_array_class_dump(Klass* k) {
2217   DumperSupport::dump_basic_type_array_class(writer(), k);
2218 }
2219 
2220 // Walk the stack of the given thread.
2221 // Dumps a HPROF_GC_ROOT_JAVA_FRAME record for each local
2222 // Dumps a HPROF_GC_ROOT_JNI_LOCAL record for each JNI local
2223 //
2224 // It returns the number of Java frames in this thread stack
2225 int VM_HeapDumper::do_thread(JavaThread* java_thread, u4 thread_serial_num) {
2226   JNILocalsDumper blk(writer(), thread_serial_num);
2227 
2228   oop threadObj = java_thread->threadObj();
2229   assert(threadObj != NULL, "sanity check");
2230 
2231   int stack_depth = 0;
2232   if (java_thread->has_last_Java_frame()) {
2233 
2234     // vframes are resource allocated
2235     Thread* current_thread = Thread::current();
2236     ResourceMark rm(current_thread);
2237     HandleMark hm(current_thread);
2238 
2239     RegisterMap reg_map(java_thread);
2240     frame f = java_thread->last_frame();
2241     vframe* vf = vframe::new_vframe(&f, &reg_map, java_thread);
2242     frame* last_entry_frame = NULL;
2243     int extra_frames = 0;
2244 
2245     if (java_thread == _oome_thread && _oome_constructor != NULL) {
2246       extra_frames++;
2247     }
2248     while (vf != NULL) {
2249       blk.set_frame_number(stack_depth);
2250       if (vf->is_java_frame()) {
2251 
2252         // java frame (interpreted, compiled, ...)
2253         javaVFrame *jvf = javaVFrame::cast(vf);
2254         if (!(jvf->method()->is_native())) {
2255           StackValueCollection* locals = jvf->locals();
2256           for (int slot=0; slot<locals->size(); slot++) {
2257             if (locals->at(slot)->type() == T_OBJECT) {
2258               oop o = locals->obj_at(slot)();
2259 
2260               if (o != NULL) {
2261                 u4 size = 1 + sizeof(address) + 4 + 4;
2262                 writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size);
2263                 writer()->write_objectID(o);
2264                 writer()->write_u4(thread_serial_num);
2265                 writer()->write_u4((u4) (stack_depth + extra_frames));
2266                 writer()->end_sub_record();
2267               }
2268             }
2269           }
2270           StackValueCollection *exprs = jvf->expressions();
2271           for(int index = 0; index < exprs->size(); index++) {
2272             if (exprs->at(index)->type() == T_OBJECT) {
2273                oop o = exprs->obj_at(index)();
2274                if (o != NULL) {
2275                  u4 size = 1 + sizeof(address) + 4 + 4;
2276                  writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size);
2277                  writer()->write_objectID(o);
2278                  writer()->write_u4(thread_serial_num);
2279                  writer()->write_u4((u4) (stack_depth + extra_frames));
2280                  writer()->end_sub_record();
2281                }
2282              }
2283           }
2284         } else {
2285           // native frame
2286           if (stack_depth == 0) {
2287             // JNI locals for the top frame.
2288             java_thread->active_handles()->oops_do(&blk);
2289           } else {
2290             if (last_entry_frame != NULL) {
2291               // JNI locals for the entry frame
2292               assert(last_entry_frame->is_entry_frame(), "checking");
2293               last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(&blk);
2294             }
2295           }
2296         }
2297         // increment only for Java frames
2298         stack_depth++;
2299         last_entry_frame = NULL;
2300 
2301       } else {
2302         // externalVFrame - if it's an entry frame then report any JNI locals
2303         // as roots when we find the corresponding native javaVFrame
2304         frame* fr = vf->frame_pointer();
2305         assert(fr != NULL, "sanity check");
2306         if (fr->is_entry_frame()) {
2307           last_entry_frame = fr;
2308         }
2309       }
2310       vf = vf->sender();
2311     }
2312   } else {
2313     // no last java frame but there may be JNI locals
2314     java_thread->active_handles()->oops_do(&blk);
2315   }
2316   return stack_depth;
2317 }
2318 
2319 
2320 // write a HPROF_GC_ROOT_THREAD_OBJ record for each java thread. Then walk
2321 // the stack so that locals and JNI locals are dumped.
2322 void VM_HeapDumper::do_threads() {
2323   for (int i=0; i < _num_threads; i++) {
2324     JavaThread* thread = _stack_traces[i]->thread();
2325     oop threadObj = thread->threadObj();
2326     u4 thread_serial_num = i+1;
2327     u4 stack_serial_num = thread_serial_num + STACK_TRACE_ID;
2328     u4 size = 1 + sizeof(address) + 4 + 4;
2329     writer()->start_sub_record(HPROF_GC_ROOT_THREAD_OBJ, size);
2330     writer()->write_objectID(threadObj);
2331     writer()->write_u4(thread_serial_num);  // thread number
2332     writer()->write_u4(stack_serial_num);   // stack trace serial number
2333     writer()->end_sub_record();
2334     int num_frames = do_thread(thread, thread_serial_num);
2335     assert(num_frames == _stack_traces[i]->get_stack_depth(),
2336            "total number of Java frames not matched");
2337   }
2338 }
2339 
2340 
2341 // The VM operation that dumps the heap. The dump consists of the following
2342 // records:
2343 //
2344 //  HPROF_HEADER
2345 //  [HPROF_UTF8]*
2346 //  [HPROF_LOAD_CLASS]*
2347 //  [[HPROF_FRAME]*|HPROF_TRACE]*
2348 //  [HPROF_GC_CLASS_DUMP]*
2349 //  [HPROF_HEAP_DUMP_SEGMENT]*
2350 //  HPROF_HEAP_DUMP_END
2351 //
2352 // The HPROF_TRACE records represent the stack traces where the heap dump
2353 // is generated and a "dummy trace" record which does not include
2354 // any frames. The dummy trace record is used to be referenced as the
2355 // unknown object alloc site.
2356 //
2357 // Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records.
2358 // To allow the heap dump be generated in a single pass we remember the position
2359 // of the dump length and fix it up after all sub-records have been written.
2360 // To generate the sub-records we iterate over the heap, writing
2361 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
2362 // records as we go. Once that is done we write records for some of the GC
2363 // roots.
2364 
2365 void VM_HeapDumper::doit() {
2366 
2367   CollectedHeap* ch = Universe::heap();
2368 
2369   ch->ensure_parsability(false); // must happen, even if collection does
2370                                  // not happen (e.g. due to GCLocker)
2371 
2372   if (_gc_before_heap_dump) {
2373     if (GCLocker::is_active()) {
2374       warning("GC locker is held; pre-heapdump GC was skipped");
2375     } else {
2376       ch->collect_as_vm_thread(GCCause::_heap_dump);
2377     }
2378   }
2379 
2380   // At this point we should be the only dumper active, so
2381   // the following should be safe.
2382   set_global_dumper();
2383   set_global_writer();
2384 
2385   WorkGang* gang = ch->safepoint_workers();
2386 
2387   if (gang == NULL) {
2388     // Use serial dump, set dumper threads and writer threads number to 1.
2389     _num_dumper_threads=1;
2390     _num_writer_threads=1;
2391     work(0);
2392   } else {
2393     prepare_parallel_dump(gang->active_workers());
2394     gang->run_task(this);
2395     finish_parallel_dump();
2396   }
2397 
2398   // Now we clear the global variables, so that a future dumper can run.
2399   clear_global_dumper();
2400   clear_global_writer();
2401 }
2402 
2403 void VM_HeapDumper::work(uint worker_id) {
2404   if (worker_id != 0) {
2405     if (get_worker_type(worker_id) == WriterType) {
2406       writer()->writer_loop();
2407       return;
2408     }
2409     if (_num_dumper_threads > 1 && get_worker_type(worker_id) == DumperType) {
2410       _dumper_controller->wait_for_start_signal();
2411     }
2412   } else {
2413     // The worker 0 on all non-heap data dumping and part of heap iteration.
2414     // Write the file header - we always use 1.0.2
2415     const char* header = "JAVA PROFILE 1.0.2";
2416 
2417     // header is few bytes long - no chance to overflow int
2418     writer()->write_raw((void*)header, (int)strlen(header));
2419     writer()->write_u1(0); // terminator
2420     writer()->write_u4(oopSize);
2421     // timestamp is current time in ms
2422     writer()->write_u8(os::javaTimeMillis());
2423     // HPROF_UTF8 records
2424     SymbolTableDumper sym_dumper(writer());
2425     SymbolTable::symbols_do(&sym_dumper);
2426 
2427     // write HPROF_LOAD_CLASS records
2428     {
2429       LockedClassesDo locked_load_classes(&do_load_class);
2430       ClassLoaderDataGraph::classes_do(&locked_load_classes);
2431     }
2432     Universe::basic_type_classes_do(&do_load_class);
2433 
2434     // write HPROF_FRAME and HPROF_TRACE records
2435     // this must be called after _klass_map is built when iterating the classes above.
2436     dump_stack_traces();
2437 
2438     // Writes HPROF_GC_CLASS_DUMP records
2439     {
2440       LockedClassesDo locked_dump_class(&do_class_dump);
2441       ClassLoaderDataGraph::classes_do(&locked_dump_class);
2442     }
2443     Universe::basic_type_classes_do(&do_basic_type_array_class_dump);
2444 
2445     // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
2446     do_threads();
2447 
2448     // HPROF_GC_ROOT_JNI_GLOBAL
2449     JNIGlobalsDumper jni_dumper(writer());
2450     JNIHandles::oops_do(&jni_dumper);
2451     // technically not jni roots, but global roots
2452     // for things like preallocated throwable backtraces
2453     Universe::vm_global()->oops_do(&jni_dumper);
2454     // HPROF_GC_ROOT_STICKY_CLASS
2455     // These should be classes in the NULL class loader data, and not all classes
2456     // if !ClassUnloading
2457     StickyClassDumper class_dumper(writer());
2458     ClassLoaderData::the_null_class_loader_data()->classes_do(&class_dumper);
2459   }
2460   // writes HPROF_GC_INSTANCE_DUMP records.
2461   // After each sub-record is written check_segment_length will be invoked
2462   // to check if the current segment exceeds a threshold. If so, a new
2463   // segment is started.
2464   // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
2465   // of the heap dump.
2466   if (_num_dumper_threads <= 1) {
2467     HeapObjectDumper obj_dumper(writer());
2468     Universe::heap()->object_iterate(&obj_dumper);
2469   } else {
2470     assert(get_worker_type(worker_id) == DumperType
2471           || get_worker_type(worker_id) == VMDumperType,
2472           "must be dumper thread to do heap iteration");
2473     if (get_worker_type(worker_id) == VMDumperType) {
2474       // Clear global writer's buffer.
2475       writer()->finish_dump_segment(true);
2476       // Notify dumpers to start heap iteration.
2477       _dumper_controller->start_dump();
2478     }
2479     // Heap iteration.
2480     {
2481        ParDumpWriter pw(writer());
2482        {
2483          HeapObjectDumper obj_dumper(&pw, _large_object_list);
2484          _poi->object_iterate(&obj_dumper, worker_id);
2485        }
2486 
2487        if (get_worker_type(worker_id) == VMDumperType) {
2488          _dumper_controller->wait_all_dumpers_complete();
2489          // clear internal buffer;
2490          pw.finish_dump_segment(true);
2491          // refresh the global_writer's buffer and position;
2492          writer()->refresh();
2493        } else {
2494          pw.finish_dump_segment(true);
2495          _dumper_controller->dumper_complete();
2496          return;
2497        }
2498     }
2499   }
2500 
2501   assert(get_worker_type(worker_id) == VMDumperType, "Heap dumper must be VMDumper");
2502   // Use writer() rather than ParDumpWriter to avoid memory consumption.
2503   HeapObjectDumper obj_dumper(writer());
2504   dump_large_objects(&obj_dumper);
2505   // Writes the HPROF_HEAP_DUMP_END record.
2506   DumperSupport::end_of_dump(writer());
2507   // We are done with writing. Release the worker threads.
2508   writer()->deactivate();
2509 }
2510 
2511 void VM_HeapDumper::dump_stack_traces() {
2512   // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
2513   DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4));
2514   writer()->write_u4((u4) STACK_TRACE_ID);
2515   writer()->write_u4(0);                    // thread number
2516   writer()->write_u4(0);                    // frame count
2517 
2518   _stack_traces = NEW_C_HEAP_ARRAY(ThreadStackTrace*, Threads::number_of_threads(), mtInternal);
2519   int frame_serial_num = 0;
2520   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
2521     oop threadObj = thread->threadObj();
2522     if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
2523       // dump thread stack trace
2524       Thread* current_thread = Thread::current();
2525       ResourceMark rm(current_thread);
2526       HandleMark hm(current_thread);
2527 
2528       ThreadStackTrace* stack_trace = new ThreadStackTrace(thread, false);
2529       stack_trace->dump_stack_at_safepoint(-1);
2530       _stack_traces[_num_threads++] = stack_trace;
2531 
2532       // write HPROF_FRAME records for this thread's stack trace
2533       int depth = stack_trace->get_stack_depth();
2534       int thread_frame_start = frame_serial_num;
2535       int extra_frames = 0;
2536       // write fake frame that makes it look like the thread, which caused OOME,
2537       // is in the OutOfMemoryError zero-parameter constructor
2538       if (thread == _oome_thread && _oome_constructor != NULL) {
2539         int oome_serial_num = _klass_map->find(_oome_constructor->method_holder());
2540         // the class serial number starts from 1
2541         assert(oome_serial_num > 0, "OutOfMemoryError class not found");
2542         DumperSupport::dump_stack_frame(writer(), ++frame_serial_num, oome_serial_num,
2543                                         _oome_constructor, 0);
2544         extra_frames++;
2545       }
2546       for (int j=0; j < depth; j++) {
2547         StackFrameInfo* frame = stack_trace->stack_frame_at(j);
2548         Method* m = frame->method();
2549         int class_serial_num = _klass_map->find(m->method_holder());
2550         // the class serial number starts from 1
2551         assert(class_serial_num > 0, "class not found");
2552         DumperSupport::dump_stack_frame(writer(), ++frame_serial_num, class_serial_num, m, frame->bci());
2553       }
2554       depth += extra_frames;
2555 
2556       // write HPROF_TRACE record for one thread
2557       DumperSupport::write_header(writer(), HPROF_TRACE, 3*sizeof(u4) + depth*oopSize);
2558       int stack_serial_num = _num_threads + STACK_TRACE_ID;
2559       writer()->write_u4(stack_serial_num);      // stack trace serial number
2560       writer()->write_u4((u4) _num_threads);     // thread serial number
2561       writer()->write_u4(depth);                 // frame count
2562       for (int j=1; j <= depth; j++) {
2563         writer()->write_id(thread_frame_start + j);
2564       }
2565     }
2566   }
2567 }
2568 
2569 // dump the large objects.
2570 void VM_HeapDumper::dump_large_objects(ObjectClosure* cl) {
2571   _large_object_list->drain(cl);
2572 }
2573 
2574 // dump the heap to given path.
2575 int HeapDumper::dump(const char* path, outputStream* out, int compression, bool overwrite, uint num_dump_threads) {
2576   assert(path != NULL && strlen(path) > 0, "path missing");
2577 
2578   // print message in interactive case
2579   if (out != NULL) {
2580     out->print_cr("Dumping heap to %s ...", path);
2581     timer()->start();
2582   }
2583   // create JFR event
2584   EventHeapDump event;
2585 
2586   AbstractCompressor* compressor = NULL;
2587 
2588   if (compression > 0) {
2589     compressor = new (std::nothrow) GZipCompressor(compression);
2590 
2591     if (compressor == NULL) {
2592       set_error("Could not allocate gzip compressor");
2593       return -1;
2594     }
2595   }
2596 
2597   DumpWriter writer(new (std::nothrow) FileWriter(path, overwrite), compressor);
2598 
2599   if (writer.error() != NULL) {
2600     set_error(writer.error());
2601     if (out != NULL) {
2602       out->print_cr("Unable to create %s: %s", path,
2603         (error() != NULL) ? error() : "reason unknown");
2604     }
2605     return -1;
2606   }
2607 
2608   // generate the dump
2609   VM_HeapDumper dumper(&writer, _gc_before_heap_dump, _oome, num_dump_threads);
2610   if (Thread::current()->is_VM_thread()) {
2611     assert(SafepointSynchronize::is_at_safepoint(), "Expected to be called at a safepoint");
2612     dumper.doit();
2613   } else {
2614     VMThread::execute(&dumper);
2615   }
2616 
2617   // record any error that the writer may have encountered
2618   set_error(writer.error());
2619 
2620   // emit JFR event
2621   if (error() == NULL) {
2622     event.set_destination(path);
2623     event.set_gcBeforeDump(_gc_before_heap_dump);
2624     event.set_size(writer.bytes_written());
2625     event.set_onOutOfMemoryError(_oome);
2626     event.commit();
2627   }
2628 
2629   // print message in interactive case
2630   if (out != NULL) {
2631     timer()->stop();
2632     if (error() == NULL) {
2633       out->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]",
2634                     writer.bytes_written(), timer()->seconds());
2635     } else {
2636       out->print_cr("Dump file is incomplete: %s", writer.error());
2637     }
2638   }
2639 
2640   return (writer.error() == NULL) ? 0 : -1;
2641 }
2642 
2643 // stop timer (if still active), and free any error string we might be holding
2644 HeapDumper::~HeapDumper() {
2645   if (timer()->is_active()) {
2646     timer()->stop();
2647   }
2648   set_error(NULL);
2649 }
2650 
2651 
2652 // returns the error string (resource allocated), or NULL
2653 char* HeapDumper::error_as_C_string() const {
2654   if (error() != NULL) {
2655     char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
2656     strcpy(str, error());
2657     return str;
2658   } else {
2659     return NULL;
2660   }
2661 }
2662 
2663 // set the error string
2664 void HeapDumper::set_error(char const* error) {
2665   if (_error != NULL) {
2666     os::free(_error);
2667   }
2668   if (error == NULL) {
2669     _error = NULL;
2670   } else {
2671     _error = os::strdup(error);
2672     assert(_error != NULL, "allocation failure");
2673   }
2674 }
2675 
2676 // Called by out-of-memory error reporting by a single Java thread
2677 // outside of a JVM safepoint
2678 void HeapDumper::dump_heap_from_oome() {
2679   HeapDumper::dump_heap(true);
2680 }
2681 
2682 // Called by error reporting by a single Java thread outside of a JVM safepoint,
2683 // or by heap dumping by the VM thread during a (GC) safepoint. Thus, these various
2684 // callers are strictly serialized and guaranteed not to interfere below. For more
2685 // general use, however, this method will need modification to prevent
2686 // inteference when updating the static variables base_path and dump_file_seq below.
2687 void HeapDumper::dump_heap() {
2688   HeapDumper::dump_heap(false);
2689 }
2690 
2691 void HeapDumper::dump_heap(bool oome) {
2692   static char base_path[JVM_MAXPATHLEN] = {'\0'};
2693   static uint dump_file_seq = 0;
2694   char* my_path;
2695   const int max_digit_chars = 20;
2696 
2697   const char* dump_file_name = "java_pid";
2698   const char* dump_file_ext  = HeapDumpGzipLevel > 0 ? ".hprof.gz" : ".hprof";
2699 
2700   // The dump file defaults to java_pid<pid>.hprof in the current working
2701   // directory. HeapDumpPath=<file> can be used to specify an alternative
2702   // dump file name or a directory where dump file is created.
2703   if (dump_file_seq == 0) { // first time in, we initialize base_path
2704     // Calculate potentially longest base path and check if we have enough
2705     // allocated statically.
2706     const size_t total_length =
2707                       (HeapDumpPath == NULL ? 0 : strlen(HeapDumpPath)) +
2708                       strlen(os::file_separator()) + max_digit_chars +
2709                       strlen(dump_file_name) + strlen(dump_file_ext) + 1;
2710     if (total_length > sizeof(base_path)) {
2711       warning("Cannot create heap dump file.  HeapDumpPath is too long.");
2712       return;
2713     }
2714 
2715     bool use_default_filename = true;
2716     if (HeapDumpPath == NULL || HeapDumpPath[0] == '\0') {
2717       // HeapDumpPath=<file> not specified
2718     } else {
2719       strcpy(base_path, HeapDumpPath);
2720       // check if the path is a directory (must exist)
2721       DIR* dir = os::opendir(base_path);
2722       if (dir == NULL) {
2723         use_default_filename = false;
2724       } else {
2725         // HeapDumpPath specified a directory. We append a file separator
2726         // (if needed).
2727         os::closedir(dir);
2728         size_t fs_len = strlen(os::file_separator());
2729         if (strlen(base_path) >= fs_len) {
2730           char* end = base_path;
2731           end += (strlen(base_path) - fs_len);
2732           if (strcmp(end, os::file_separator()) != 0) {
2733             strcat(base_path, os::file_separator());
2734           }
2735         }
2736       }
2737     }
2738     // If HeapDumpPath wasn't a file name then we append the default name
2739     if (use_default_filename) {
2740       const size_t dlen = strlen(base_path);  // if heap dump dir specified
2741       jio_snprintf(&base_path[dlen], sizeof(base_path)-dlen, "%s%d%s",
2742                    dump_file_name, os::current_process_id(), dump_file_ext);
2743     }
2744     const size_t len = strlen(base_path) + 1;
2745     my_path = (char*)os::malloc(len, mtInternal);
2746     if (my_path == NULL) {
2747       warning("Cannot create heap dump file.  Out of system memory.");
2748       return;
2749     }
2750     strncpy(my_path, base_path, len);
2751   } else {
2752     // Append a sequence number id for dumps following the first
2753     const size_t len = strlen(base_path) + max_digit_chars + 2; // for '.' and \0
2754     my_path = (char*)os::malloc(len, mtInternal);
2755     if (my_path == NULL) {
2756       warning("Cannot create heap dump file.  Out of system memory.");
2757       return;
2758     }
2759     jio_snprintf(my_path, len, "%s.%d", base_path, dump_file_seq);
2760   }
2761   dump_file_seq++;   // increment seq number for next time we dump
2762 
2763   HeapDumper dumper(false /* no GC before heap dump */,
2764                     oome  /* pass along out-of-memory-error flag */);
2765   dumper.dump(my_path, tty, HeapDumpGzipLevel);
2766   os::free(my_path);
2767 }