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