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