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