2
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2023, Alibaba Group Holding Limited. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
26
#include "precompiled.hpp"
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"
38
#include "memory/allocation.inline.hpp"
39
#include "memory/resourceArea.hpp"
40
#include "memory/universe.hpp"
41
#include "oops/fieldStreams.inline.hpp"
42
#include "oops/klass.inline.hpp"
43
#include "oops/objArrayKlass.hpp"
44
#include "oops/objArrayOop.inline.hpp"
45
#include "oops/oop.inline.hpp"
46
#include "oops/typeArrayOop.inline.hpp"
47
#include "runtime/continuationWrapper.inline.hpp"
48
#include "runtime/frame.inline.hpp"
49
#include "runtime/handles.inline.hpp"
50
#include "runtime/javaCalls.hpp"
51
#include "runtime/javaThread.inline.hpp"
52
#include "runtime/jniHandles.hpp"
53
#include "runtime/os.hpp"
54
#include "runtime/threads.hpp"
55
#include "runtime/threadSMR.hpp"
56
#include "runtime/vframe.hpp"
57
#include "runtime/vmOperations.hpp"
58
#include "runtime/vmThread.hpp"
59
#include "runtime/timerTrace.hpp"
60
#include "services/heapDumper.hpp"
61
#include "services/heapDumperCompression.hpp"
62
#include "services/threadService.hpp"
63
#include "utilities/checkedCast.hpp"
64
#include "utilities/macros.hpp"
65
#include "utilities/ostream.hpp"
67
#include "os_linux.hpp"
71
* HPROF binary format - description copied from:
72
* src/share/demo/jvmti/hprof/hprof_io.c
75
* header "JAVA PROFILE 1.0.2" (0-terminated)
77
* u4 size of identifiers. Identifiers are used to represent
78
* UTF8 strings, objects, stack traces, etc. They usually
79
* have the same size as host pointers.
81
* u4 low word number of milliseconds since 0:00 GMT, 1/1/70
82
* [record]* a sequence of records.
87
* u1 a TAG denoting the type of the record
88
* u4 number of *microseconds* since the time stamp in the
89
* header. (wraps around in a little more than an hour)
90
* u4 number of bytes *remaining* in the record. Note that
91
* this number excludes the tag and the length field itself.
92
* [u1]* BODY of the record (a sequence of bytes)
95
* The following TAGs are supported:
98
*----------------------------------------------------------
99
* HPROF_UTF8 a UTF8-encoded name
102
* [u1]* UTF8 characters (no trailing zero)
104
* HPROF_LOAD_CLASS a newly loaded class
106
* u4 class serial number (> 0)
108
* u4 stack trace serial number
111
* HPROF_UNLOAD_CLASS an unloading class
113
* u4 class serial_number
115
* HPROF_FRAME a Java stack frame
119
* id method signature ID
120
* id source file name ID
121
* u4 class serial number
122
* i4 line number. >0: normal
124
* -2: compiled method
127
* HPROF_TRACE a Java stack trace
129
* u4 stack trace serial number
130
* u4 thread serial number
131
* u4 number of frames
132
* [id]* stack frame IDs
135
* HPROF_ALLOC_SITES a set of heap allocation sites, obtained after GC
137
* u2 flags 0x0001: incremental vs. complete
138
* 0x0002: sorted by allocation vs. live
139
* 0x0004: whether to force a GC
141
* u4 total live bytes
142
* u4 total live instances
143
* u8 total bytes allocated
144
* u8 total instances allocated
145
* u4 number of sites that follow
146
* [u1 is_array: 0: normal object
156
* u4 class serial number (may be zero during startup)
157
* u4 stack trace serial number
158
* u4 number of bytes alive
159
* u4 number of instances alive
160
* u4 number of bytes allocated
161
* u4]* number of instance allocated
163
* HPROF_START_THREAD a newly started thread.
165
* u4 thread serial number (> 0)
166
* id thread object ID
167
* u4 stack trace serial number
169
* id thread group name ID
170
* id thread group parent name ID
172
* HPROF_END_THREAD a terminating thread.
174
* u4 thread serial number
176
* HPROF_HEAP_SUMMARY heap summary
178
* u4 total live bytes
179
* u4 total live instances
180
* u8 total bytes allocated
181
* u8 total instances allocated
183
* HPROF_HEAP_DUMP denote a heap dump
185
* [heap dump sub-records]*
187
* There are four kinds of heap dump sub-records:
191
* HPROF_GC_ROOT_UNKNOWN unknown root
195
* HPROF_GC_ROOT_THREAD_OBJ thread object
197
* id thread object ID (may be 0 for a
198
* thread newly attached through JNI)
199
* u4 thread sequence number
200
* u4 stack trace sequence number
202
* HPROF_GC_ROOT_JNI_GLOBAL JNI global ref root
205
* id JNI global ref ID
207
* HPROF_GC_ROOT_JNI_LOCAL JNI local ref
210
* u4 thread serial number
211
* u4 frame # in stack trace (-1 for empty)
213
* HPROF_GC_ROOT_JAVA_FRAME Java stack frame
216
* u4 thread serial number
217
* u4 frame # in stack trace (-1 for empty)
219
* HPROF_GC_ROOT_NATIVE_STACK Native stack
222
* u4 thread serial number
224
* HPROF_GC_ROOT_STICKY_CLASS System class
228
* HPROF_GC_ROOT_THREAD_BLOCK Reference from thread block
231
* u4 thread serial number
233
* HPROF_GC_ROOT_MONITOR_USED Busy monitor
237
* HPROF_GC_CLASS_DUMP dump of a class object
240
* u4 stack trace serial number
241
* id super class object ID
242
* id class loader object ID
243
* id signers object ID
244
* id protection domain object ID
248
* u4 instance size (in bytes)
250
* u2 size of constant pool
251
* [u2, constant pool index,
264
* u2 number of static fields
265
* [id, static field name,
269
* u2 number of inst. fields (not inc. super)
270
* [id, instance field name,
273
* HPROF_GC_INSTANCE_DUMP dump of a normal object
276
* u4 stack trace serial number
278
* u4 number of bytes that follow
279
* [vl]* instance field values (class, followed
280
* by super, super's super ...)
282
* HPROF_GC_OBJ_ARRAY_DUMP dump of an object array
285
* u4 stack trace serial number
286
* u4 number of elements
290
* HPROF_GC_PRIM_ARRAY_DUMP dump of a primitive array
293
* u4 stack trace serial number
294
* u4 number of elements
306
* HPROF_CPU_SAMPLES a set of sample traces of running threads
308
* u4 total number of samples
311
* u4]* stack trace serial number
313
* HPROF_CONTROL_SETTINGS the settings of on/off switches
315
* u4 0x00000001: alloc traces on/off
316
* 0x00000002: cpu sampling on/off
317
* u2 stack trace depth
320
* When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
321
* be generated as a sequence of heap dump segments. This sequence is
322
* terminated by an end record. The additional tags allowed by format
323
* "JAVA PROFILE 1.0.2" are:
325
* HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment
327
* [heap dump sub-records]*
328
* The same sub-record types allowed by HPROF_HEAP_DUMP
330
* HPROF_HEAP_DUMP_END denotes the end of a heap dump
340
HPROF_LOAD_CLASS = 0x02,
341
HPROF_UNLOAD_CLASS = 0x03,
344
HPROF_ALLOC_SITES = 0x06,
345
HPROF_HEAP_SUMMARY = 0x07,
346
HPROF_START_THREAD = 0x0A,
347
HPROF_END_THREAD = 0x0B,
348
HPROF_HEAP_DUMP = 0x0C,
349
HPROF_CPU_SAMPLES = 0x0D,
350
HPROF_CONTROL_SETTINGS = 0x0E,
352
// 1.0.2 record types
353
HPROF_HEAP_DUMP_SEGMENT = 0x1C,
354
HPROF_HEAP_DUMP_END = 0x2C,
357
HPROF_ARRAY_OBJECT = 0x01,
358
HPROF_NORMAL_OBJECT = 0x02,
359
HPROF_BOOLEAN = 0x04,
368
// data-dump sub-records
369
HPROF_GC_ROOT_UNKNOWN = 0xFF,
370
HPROF_GC_ROOT_JNI_GLOBAL = 0x01,
371
HPROF_GC_ROOT_JNI_LOCAL = 0x02,
372
HPROF_GC_ROOT_JAVA_FRAME = 0x03,
373
HPROF_GC_ROOT_NATIVE_STACK = 0x04,
374
HPROF_GC_ROOT_STICKY_CLASS = 0x05,
375
HPROF_GC_ROOT_THREAD_BLOCK = 0x06,
376
HPROF_GC_ROOT_MONITOR_USED = 0x07,
377
HPROF_GC_ROOT_THREAD_OBJ = 0x08,
378
HPROF_GC_CLASS_DUMP = 0x20,
379
HPROF_GC_INSTANCE_DUMP = 0x21,
380
HPROF_GC_OBJ_ARRAY_DUMP = 0x22,
381
HPROF_GC_PRIM_ARRAY_DUMP = 0x23
384
// Default stack trace ID (used for dummy HPROF_TRACE record)
387
INITIAL_CLASS_COUNT = 200
390
// Supports I/O operations for a dump
391
// Base class for dump and parallel dump
392
class AbstractDumpWriter : public CHeapObj<mtInternal> {
395
io_buffer_max_size = 1*M,
396
dump_segment_header_size = 9
399
char* _buffer; // internal buffer
403
bool _in_dump_segment; // Are we currently in a dump segment?
404
bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
405
DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
406
DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
408
char* buffer() const { return _buffer; }
409
size_t buffer_size() const { return _size; }
410
void set_position(size_t pos) { _pos = pos; }
412
// Can be called if we have enough room in the buffer.
413
void write_fast(const void* s, size_t len);
415
// Returns true if we have enough room in the buffer for 'len' bytes.
416
bool can_write_fast(size_t len);
418
void write_address(address a);
421
AbstractDumpWriter() :
423
_size(io_buffer_max_size),
425
_in_dump_segment(false) { }
427
// Total number of bytes written to the disk
428
virtual julong bytes_written() const = 0;
429
// Return non-null if error occurred
430
virtual char const* error() const = 0;
432
size_t position() const { return _pos; }
434
virtual void write_raw(const void* s, size_t len);
439
void write_objectID(oop o);
440
void write_rootID(oop* p);
441
void write_symbolID(Symbol* o);
442
void write_classID(Klass* k);
445
// Start a new sub-record. Starts a new heap dump segment if needed.
446
void start_sub_record(u1 tag, u4 len);
447
// Ends the current sub-record.
448
void end_sub_record();
449
// Finishes the current dump segment if not already finished.
450
void finish_dump_segment();
451
// Flush internal buffer to persistent storage
452
virtual void flush() = 0;
455
void AbstractDumpWriter::write_fast(const 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);
463
bool AbstractDumpWriter::can_write_fast(size_t len) {
464
return buffer_size() - position() >= len;
468
void AbstractDumpWriter::write_raw(const 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);
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);
480
set_position(position() + to_write);
484
memcpy(buffer() + position(), s, len);
485
set_position(position() + len);
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)
492
void AbstractDumpWriter::write_u1(u1 x) {
493
WRITE_KNOWN_TYPE(&x, 1);
496
void AbstractDumpWriter::write_u2(u2 x) {
498
Bytes::put_Java_u2((address)&v, x);
499
WRITE_KNOWN_TYPE(&v, 2);
502
void AbstractDumpWriter::write_u4(u4 x) {
504
Bytes::put_Java_u4((address)&v, x);
505
WRITE_KNOWN_TYPE(&v, 4);
508
void AbstractDumpWriter::write_u8(u8 x) {
510
Bytes::put_Java_u8((address)&v, x);
511
WRITE_KNOWN_TYPE(&v, 8);
514
void AbstractDumpWriter::write_address(address a) {
522
void AbstractDumpWriter::write_objectID(oop o) {
523
write_address(cast_from_oop<address>(o));
526
void AbstractDumpWriter::write_rootID(oop* p) {
527
write_address((address)p);
530
void AbstractDumpWriter::write_symbolID(Symbol* s) {
531
write_address((address)((uintptr_t)s));
534
void AbstractDumpWriter::write_id(u4 x) {
542
// We use java mirror as the class ID
543
void AbstractDumpWriter::write_classID(Klass* k) {
544
write_objectID(k->java_mirror());
547
void AbstractDumpWriter::finish_dump_segment() {
548
if (_in_dump_segment) {
549
assert(_sub_record_left == 0, "Last sub-record not written completely");
550
assert(_sub_record_ended, "sub-record must have ended");
552
// Fix up the dump segment length if we haven't written a huge sub-record last
553
// (in which case the segment length was already set to the correct value initially).
554
if (!_is_huge_sub_record) {
555
assert(position() > dump_segment_header_size, "Dump segment should have some content");
556
Bytes::put_Java_u4((address) (buffer() + 5),
557
(u4) (position() - dump_segment_header_size));
559
// Finish process huge sub record
560
// Set _is_huge_sub_record to false so the parallel dump writer can flush data to file.
561
_is_huge_sub_record = false;
564
_in_dump_segment = false;
569
void AbstractDumpWriter::start_sub_record(u1 tag, u4 len) {
570
if (!_in_dump_segment) {
571
if (position() > 0) {
575
assert(position() == 0 && buffer_size() > dump_segment_header_size, "Must be at the start");
577
write_u1(HPROF_HEAP_DUMP_SEGMENT);
578
write_u4(0); // timestamp
579
// Will be fixed up later if we add more sub-records. If this is a huge sub-record,
580
// this is already the correct length, since we don't add more sub-records.
582
assert(Bytes::get_Java_u4((address)(buffer() + 5)) == len, "Inconsistent size!");
583
_in_dump_segment = true;
584
_is_huge_sub_record = len > buffer_size() - dump_segment_header_size;
585
} else if (_is_huge_sub_record || (len > buffer_size() - position())) {
586
// This object will not fit in completely or the last sub-record was huge.
587
// Finish the current segment and try again.
588
finish_dump_segment();
589
start_sub_record(tag, len);
594
debug_only(_sub_record_left = len);
595
debug_only(_sub_record_ended = false);
600
void AbstractDumpWriter::end_sub_record() {
601
assert(_in_dump_segment, "must be in dump segment");
602
assert(_sub_record_left == 0, "sub-record not written completely");
603
assert(!_sub_record_ended, "Must not have ended yet");
604
debug_only(_sub_record_ended = true);
607
// Supports I/O operations for a dump
609
class DumpWriter : public AbstractDumpWriter {
612
AbstractCompressor* _compressor;
613
size_t _bytes_written;
615
// Compression support
626
DumpWriter(const char* path, bool overwrite, AbstractCompressor* compressor);
628
julong bytes_written() const override { return (julong) _bytes_written; }
629
char const* error() const override { return _error; }
630
void set_error(const char* error) { _error = (char*)error; }
631
bool has_error() const { return _error != nullptr; }
632
const char* get_file_path() const { return _writer->get_file_path(); }
633
AbstractCompressor* compressor() { return _compressor; }
634
bool is_overwrite() const { return _writer->is_overwrite(); }
636
void flush() override;
639
// internals for DumpMerger
640
friend class DumpMerger;
641
void set_bytes_written(julong bytes_written) { _bytes_written = bytes_written; }
642
int get_fd() const { return _writer->get_fd(); }
643
void set_compressor(AbstractCompressor* p) { _compressor = p; }
646
DumpWriter::DumpWriter(const char* path, bool overwrite, AbstractCompressor* compressor) :
647
AbstractDumpWriter(),
648
_writer(new (std::nothrow) FileWriter(path, overwrite)),
649
_compressor(compressor),
652
_out_buffer(nullptr),
655
_tmp_buffer(nullptr),
657
_error = (char*)_writer->open_writer();
658
if (_error == nullptr) {
659
_buffer = (char*)os::malloc(io_buffer_max_size, mtInternal);
660
if (compressor != nullptr) {
661
_error = (char*)_compressor->init(io_buffer_max_size, &_out_size, &_tmp_size);
662
if (_error == nullptr) {
664
_out_buffer = (char*)os::malloc(_out_size, mtInternal);
667
_tmp_buffer = (char*)os::malloc(_tmp_size, mtInternal);
672
// initialize internal buffer
674
_size = io_buffer_max_size;
677
DumpWriter::~DumpWriter(){
678
if (_buffer != nullptr) {
681
if (_out_buffer != nullptr) {
682
os::free(_out_buffer);
684
if (_tmp_buffer != nullptr) {
685
os::free(_tmp_buffer);
687
if (_writer != nullptr) {
693
// flush any buffered bytes to the file
694
void DumpWriter::flush() {
702
char* result = nullptr;
703
if (_compressor == nullptr) {
704
result = (char*)_writer->write_buf(_buffer, _pos);
705
_bytes_written += _pos;
709
result = (char*)_writer->write_buf(_out_buffer, _out_pos);
710
_bytes_written += _out_pos;
713
_pos = 0; // reset pos to make internal buffer available
715
if (result != nullptr) {
720
void DumpWriter::do_compress() {
721
const char* msg = _compressor->compress(_buffer, _pos, _out_buffer, _out_size,
722
_tmp_buffer, _tmp_size, &_out_pos);
724
if (msg != nullptr) {
729
class DumperClassCacheTable;
730
class DumperClassCacheTableEntry;
732
// Support class with a collection of functions used when dumping the heap
733
class DumperSupport : AllStatic {
736
// write a header of the given type
737
static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
739
// returns hprof tag for the given type signature
740
static hprofTag sig2tag(Symbol* sig);
741
// returns hprof tag for the given basic type
742
static hprofTag type2tag(BasicType type);
743
// Returns the size of the data to write.
744
static u4 sig2size(Symbol* sig);
746
// returns the size of the instance of the given class
747
static u4 instance_size(InstanceKlass* ik, DumperClassCacheTableEntry* class_cache_entry = nullptr);
750
static void dump_float(AbstractDumpWriter* writer, jfloat f);
752
static void dump_double(AbstractDumpWriter* writer, jdouble d);
753
// dumps the raw value of the given field
754
static void dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset);
755
// returns the size of the static fields; also counts the static fields
756
static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
757
// dumps static fields of the given class
758
static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
759
// dump the raw values of the instance fields of the given object
760
static void dump_instance_fields(AbstractDumpWriter* writer, oop o, DumperClassCacheTableEntry* class_cache_entry);
761
// get the count of the instance fields for a given class
762
static u2 get_instance_fields_count(InstanceKlass* ik);
763
// dumps the definition of the instance fields for a given class
764
static void dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k);
765
// creates HPROF_GC_INSTANCE_DUMP record for the given object
766
static void dump_instance(AbstractDumpWriter* writer, oop o, DumperClassCacheTable* class_cache);
767
// creates HPROF_GC_CLASS_DUMP record for the given instance class
768
static void dump_instance_class(AbstractDumpWriter* writer, Klass* k);
769
// creates HPROF_GC_CLASS_DUMP record for a given array class
770
static void dump_array_class(AbstractDumpWriter* writer, Klass* k);
772
// creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
773
static void dump_object_array(AbstractDumpWriter* writer, objArrayOop array);
774
// creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
775
static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
776
// create HPROF_FRAME record for the given method and bci
777
static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
779
// check if we need to truncate an array
780
static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
782
// fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
783
static void end_of_dump(AbstractDumpWriter* writer);
785
static oop mask_dormant_archived_object(oop o, oop ref_obj) {
786
if (o != nullptr && o->klass()->java_mirror_no_keepalive() == nullptr) {
787
// Ignore this object since the corresponding java mirror is not loaded.
788
// Might be a dormant archive object.
789
report_dormant_archived_object(o, ref_obj);
796
static void report_dormant_archived_object(oop o, oop ref_obj) {
797
if (log_is_enabled(Trace, cds, heap)) {
799
if (ref_obj != nullptr) {
800
log_trace(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
801
p2i(o), o->klass()->external_name(),
802
p2i(ref_obj), ref_obj->klass()->external_name());
804
log_trace(cds, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)",
805
p2i(o), o->klass()->external_name());
811
// Hash table of klasses to the klass metadata. This should greatly improve the
812
// hash dumping performance. This hash table is supposed to be used by a single
815
class DumperClassCacheTableEntry : public CHeapObj<mtServiceability> {
816
friend class DumperClassCacheTable;
818
GrowableArray<char> _sigs_start;
819
GrowableArray<int> _offsets;
824
DumperClassCacheTableEntry() : _instance_size(0), _entries(0) {};
826
int field_count() { return _entries; }
827
char sig_start(int field_idx) { return _sigs_start.at(field_idx); }
828
int offset(int field_idx) { return _offsets.at(field_idx); }
829
u4 instance_size() { return _instance_size; }
832
class DumperClassCacheTable {
834
// ResourceHashtable SIZE is specified at compile time so we
835
// use 1031 which is the first prime after 1024.
836
static constexpr size_t TABLE_SIZE = 1031;
838
// Maintain the cache for N classes. This limits memory footprint
839
// impact, regardless of how many classes we have in the dump.
840
// This also improves look up performance by keeping the statically
841
// sized table from overloading.
842
static constexpr int CACHE_TOP = 256;
844
typedef ResourceHashtable<InstanceKlass*, DumperClassCacheTableEntry*,
845
TABLE_SIZE, AnyObj::C_HEAP, mtServiceability> PtrTable;
848
// Single-slot cache to handle the major case of objects of the same
849
// class back-to-back, e.g. from T[].
850
InstanceKlass* _last_ik;
851
DumperClassCacheTableEntry* _last_entry;
853
void unlink_all(PtrTable* table) {
854
class CleanupEntry: StackObj {
856
bool do_entry(InstanceKlass*& key, DumperClassCacheTableEntry*& entry) {
861
table->unlink(&cleanup);
865
DumperClassCacheTableEntry* lookup_or_create(InstanceKlass* ik) {
866
if (_last_ik == ik) {
870
DumperClassCacheTableEntry* entry;
871
DumperClassCacheTableEntry** from_cache = _ptrs->get(ik);
872
if (from_cache == nullptr) {
873
entry = new DumperClassCacheTableEntry();
874
for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
875
if (!fld.access_flags().is_static()) {
876
Symbol* sig = fld.signature();
877
entry->_sigs_start.push(sig->char_at(0));
878
entry->_offsets.push(fld.offset());
880
entry->_instance_size += DumperSupport::sig2size(sig);
884
if (_ptrs->number_of_entries() >= CACHE_TOP) {
885
// We do not track the individual hit rates for table entries.
886
// Purge the entire table, and let the cache catch up with new
891
_ptrs->put(ik, entry);
896
// Remember for single-slot cache.
903
DumperClassCacheTable() : _ptrs(new (mtServiceability) PtrTable), _last_ik(nullptr), _last_entry(nullptr) {}
905
~DumperClassCacheTable() {
911
// write a header of the given type
912
void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
913
writer->write_u1(tag);
914
writer->write_u4(0); // current ticks
915
writer->write_u4(len);
918
// returns hprof tag for the given type signature
919
hprofTag DumperSupport::sig2tag(Symbol* sig) {
920
switch (sig->char_at(0)) {
921
case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
922
case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
923
case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
924
case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
925
case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
926
case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
927
case JVM_SIGNATURE_INT : return HPROF_INT;
928
case JVM_SIGNATURE_LONG : return HPROF_LONG;
929
case JVM_SIGNATURE_SHORT : return HPROF_SHORT;
930
case JVM_SIGNATURE_BOOLEAN : return HPROF_BOOLEAN;
931
default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
935
hprofTag DumperSupport::type2tag(BasicType type) {
937
case T_BYTE : return HPROF_BYTE;
938
case T_CHAR : return HPROF_CHAR;
939
case T_FLOAT : return HPROF_FLOAT;
940
case T_DOUBLE : return HPROF_DOUBLE;
941
case T_INT : return HPROF_INT;
942
case T_LONG : return HPROF_LONG;
943
case T_SHORT : return HPROF_SHORT;
944
case T_BOOLEAN : return HPROF_BOOLEAN;
945
default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
949
u4 DumperSupport::sig2size(Symbol* sig) {
950
switch (sig->char_at(0)) {
951
case JVM_SIGNATURE_CLASS:
952
case JVM_SIGNATURE_ARRAY: return sizeof(address);
953
case JVM_SIGNATURE_BOOLEAN:
954
case JVM_SIGNATURE_BYTE: return 1;
955
case JVM_SIGNATURE_SHORT:
956
case JVM_SIGNATURE_CHAR: return 2;
957
case JVM_SIGNATURE_INT:
958
case JVM_SIGNATURE_FLOAT: return 4;
959
case JVM_SIGNATURE_LONG:
960
case JVM_SIGNATURE_DOUBLE: return 8;
961
default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
965
template<typename T, typename F> T bit_cast(F from) { // replace with the real thing when we can use c++20
967
static_assert(sizeof(to) == sizeof(from), "must be of the same size");
968
memcpy(&to, &from, sizeof(to));
973
void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
975
writer->write_u4(0x7fc00000); // collapsing NaNs
977
writer->write_u4(bit_cast<u4>(f));
982
void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
984
writer->write_u8(0x7ff80000ull << 32); // collapsing NaNs
986
writer->write_u8(bit_cast<u8>(d));
990
// dumps the raw value of the given field
991
void DumperSupport::dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset) {
993
case JVM_SIGNATURE_CLASS :
994
case JVM_SIGNATURE_ARRAY : {
995
oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
996
o = mask_dormant_archived_object(o, obj);
997
assert(oopDesc::is_oop_or_null(o), "Expected an oop or nullptr at " PTR_FORMAT, p2i(o));
998
writer->write_objectID(o);
1001
case JVM_SIGNATURE_BYTE : {
1002
jbyte b = obj->byte_field(offset);
1003
writer->write_u1(b);
1006
case JVM_SIGNATURE_CHAR : {
1007
jchar c = obj->char_field(offset);
1008
writer->write_u2(c);
1011
case JVM_SIGNATURE_SHORT : {
1012
jshort s = obj->short_field(offset);
1013
writer->write_u2(s);
1016
case JVM_SIGNATURE_FLOAT : {
1017
jfloat f = obj->float_field(offset);
1018
dump_float(writer, f);
1021
case JVM_SIGNATURE_DOUBLE : {
1022
jdouble d = obj->double_field(offset);
1023
dump_double(writer, d);
1026
case JVM_SIGNATURE_INT : {
1027
jint i = obj->int_field(offset);
1028
writer->write_u4(i);
1031
case JVM_SIGNATURE_LONG : {
1032
jlong l = obj->long_field(offset);
1033
writer->write_u8(l);
1036
case JVM_SIGNATURE_BOOLEAN : {
1037
jboolean b = obj->bool_field(offset);
1038
writer->write_u1(b);
1042
ShouldNotReachHere();
1048
// returns the size of the instance of the given class
1049
u4 DumperSupport::instance_size(InstanceKlass* ik, DumperClassCacheTableEntry* class_cache_entry) {
1050
if (class_cache_entry != nullptr) {
1051
return class_cache_entry->instance_size();
1054
for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
1055
if (!fld.access_flags().is_static()) {
1056
size += sig2size(fld.signature());
1063
u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) {
1067
for (JavaFieldStream fldc(ik); !fldc.done(); fldc.next()) {
1068
if (fldc.access_flags().is_static()) {
1070
size += sig2size(fldc.signature());
1074
// Add in resolved_references which is referenced by the cpCache
1075
// The resolved_references is an array per InstanceKlass holding the
1076
// strings and other oops resolved from the constant pool.
1077
oop resolved_references = ik->constants()->resolved_references_or_null();
1078
if (resolved_references != nullptr) {
1080
size += sizeof(address);
1082
// Add in the resolved_references of the used previous versions of the class
1083
// in the case of RedefineClasses
1084
InstanceKlass* prev = ik->previous_versions();
1085
while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1087
size += sizeof(address);
1088
prev = prev->previous_versions();
1092
// Also provide a pointer to the init_lock if present, so there aren't unreferenced int[0]
1094
oop init_lock = ik->init_lock();
1095
if (init_lock != nullptr) {
1097
size += sizeof(address);
1100
// We write the value itself plus a name and a one byte type tag per field.
1101
return checked_cast<u4>(size + field_count * (sizeof(address) + 1));
1104
// dumps static fields of the given class
1105
void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1106
InstanceKlass* ik = InstanceKlass::cast(k);
1108
// dump the field descriptors and raw values
1109
for (JavaFieldStream fld(ik); !fld.done(); fld.next()) {
1110
if (fld.access_flags().is_static()) {
1111
Symbol* sig = fld.signature();
1113
writer->write_symbolID(fld.name()); // name
1114
writer->write_u1(sig2tag(sig)); // type
1117
dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
1121
// Add resolved_references for each class that has them
1122
oop resolved_references = ik->constants()->resolved_references_or_null();
1123
if (resolved_references != nullptr) {
1124
writer->write_symbolID(vmSymbols::resolved_references_name()); // name
1125
writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1126
writer->write_objectID(resolved_references);
1128
// Also write any previous versions
1129
InstanceKlass* prev = ik->previous_versions();
1130
while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1131
writer->write_symbolID(vmSymbols::resolved_references_name()); // name
1132
writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1133
writer->write_objectID(prev->constants()->resolved_references());
1134
prev = prev->previous_versions();
1138
// Add init lock to the end if the class is not yet initialized
1139
oop init_lock = ik->init_lock();
1140
if (init_lock != nullptr) {
1141
writer->write_symbolID(vmSymbols::init_lock_name()); // name
1142
writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type
1143
writer->write_objectID(init_lock);
1147
// dump the raw values of the instance fields of the given object
1148
void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o, DumperClassCacheTableEntry* class_cache_entry) {
1149
assert(class_cache_entry != nullptr, "Pre-condition: must be provided");
1150
for (int idx = 0; idx < class_cache_entry->field_count(); idx++) {
1151
dump_field_value(writer, class_cache_entry->sig_start(idx), o, class_cache_entry->offset(idx));
1155
// dumps the definition of the instance fields for a given class
1156
u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1159
for (JavaFieldStream fldc(ik); !fldc.done(); fldc.next()) {
1160
if (!fldc.access_flags().is_static()) field_count++;
1166
// dumps the definition of the instance fields for a given class
1167
void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, Klass* k) {
1168
InstanceKlass* ik = InstanceKlass::cast(k);
1170
// dump the field descriptors
1171
for (JavaFieldStream fld(ik); !fld.done(); fld.next()) {
1172
if (!fld.access_flags().is_static()) {
1173
Symbol* sig = fld.signature();
1175
writer->write_symbolID(fld.name()); // name
1176
writer->write_u1(sig2tag(sig)); // type
1181
// creates HPROF_GC_INSTANCE_DUMP record for the given object
1182
void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o, DumperClassCacheTable* class_cache) {
1183
InstanceKlass* ik = InstanceKlass::cast(o->klass());
1185
DumperClassCacheTableEntry* cache_entry = class_cache->lookup_or_create(ik);
1187
u4 is = instance_size(ik, cache_entry);
1188
u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1190
writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1191
writer->write_objectID(o);
1192
writer->write_u4(STACK_TRACE_ID);
1195
writer->write_classID(ik);
1197
// number of bytes that follow
1198
writer->write_u4(is);
1201
dump_instance_fields(writer, o, cache_entry);
1203
writer->end_sub_record();
1206
// creates HPROF_GC_CLASS_DUMP record for the given instance class
1207
void DumperSupport::dump_instance_class(AbstractDumpWriter* writer, Klass* k) {
1208
InstanceKlass* ik = InstanceKlass::cast(k);
1210
// We can safepoint and do a heap dump at a point where we have a Klass,
1211
// but no java mirror class has been setup for it. So we need to check
1212
// that the class is at least loaded, to avoid crash from a null mirror.
1213
if (!ik->is_loaded()) {
1217
u2 static_fields_count = 0;
1218
u4 static_size = get_static_fields_size(ik, static_fields_count);
1219
u2 instance_fields_count = get_instance_fields_count(ik);
1220
u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1221
u4 size = checked_cast<u4>(1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size);
1223
writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1226
writer->write_classID(ik);
1227
writer->write_u4(STACK_TRACE_ID);
1230
InstanceKlass* java_super = ik->java_super();
1231
if (java_super == nullptr) {
1232
writer->write_objectID(oop(nullptr));
1234
writer->write_classID(java_super);
1237
writer->write_objectID(ik->class_loader());
1238
writer->write_objectID(ik->signers());
1239
writer->write_objectID(ik->protection_domain());
1242
writer->write_objectID(oop(nullptr));
1243
writer->write_objectID(oop(nullptr));
1246
writer->write_u4(DumperSupport::instance_size(ik));
1248
// size of constant pool - ignored by HAT 1.1
1249
writer->write_u2(0);
1252
writer->write_u2(static_fields_count);
1253
dump_static_fields(writer, ik);
1255
// description of instance fields
1256
writer->write_u2(instance_fields_count);
1257
dump_instance_field_descriptors(writer, ik);
1259
writer->end_sub_record();
1262
// creates HPROF_GC_CLASS_DUMP record for the given array class
1263
void DumperSupport::dump_array_class(AbstractDumpWriter* writer, Klass* k) {
1264
InstanceKlass* ik = nullptr; // bottom class for object arrays, null for primitive type arrays
1265
if (k->is_objArray_klass()) {
1266
Klass *bk = ObjArrayKlass::cast(k)->bottom_klass();
1267
assert(bk != nullptr, "checking");
1268
if (bk->is_instance_klass()) {
1269
ik = InstanceKlass::cast(bk);
1273
u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1274
writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1275
writer->write_classID(k);
1276
writer->write_u4(STACK_TRACE_ID);
1278
// super class of array classes is java.lang.Object
1279
InstanceKlass* java_super = k->java_super();
1280
assert(java_super != nullptr, "checking");
1281
writer->write_classID(java_super);
1283
writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->class_loader());
1284
writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->signers());
1285
writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->protection_domain());
1287
writer->write_objectID(oop(nullptr)); // reserved
1288
writer->write_objectID(oop(nullptr));
1289
writer->write_u4(0); // instance size
1290
writer->write_u2(0); // constant pool
1291
writer->write_u2(0); // static fields
1292
writer->write_u2(0); // instance fields
1294
writer->end_sub_record();
1298
// Hprof uses an u4 as record length field,
1299
// which means we need to truncate arrays that are too long.
1300
int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1301
BasicType type = ArrayKlass::cast(array->klass())->element_type();
1302
assert(type >= T_BOOLEAN && type <= T_OBJECT, "invalid array element type");
1304
int length = array->length();
1307
if (type == T_OBJECT) {
1308
type_size = sizeof(address);
1310
type_size = type2aelembytes(type);
1313
size_t length_in_bytes = (size_t)length * type_size;
1314
uint max_bytes = max_juint - header_size;
1316
if (length_in_bytes > max_bytes) {
1317
length = max_bytes / type_size;
1318
length_in_bytes = (size_t)length * type_size;
1320
warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1321
type2name_tab[type], array->length(), length);
1326
// creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1327
void DumperSupport::dump_object_array(AbstractDumpWriter* writer, objArrayOop array) {
1328
// sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1329
short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1330
int length = calculate_array_max_length(writer, array, header_size);
1331
u4 size = checked_cast<u4>(header_size + length * sizeof(address));
1333
writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1334
writer->write_objectID(array);
1335
writer->write_u4(STACK_TRACE_ID);
1336
writer->write_u4(length);
1339
writer->write_classID(array->klass());
1342
for (int index = 0; index < length; index++) {
1343
oop o = array->obj_at(index);
1344
o = mask_dormant_archived_object(o, array);
1345
writer->write_objectID(o);
1348
writer->end_sub_record();
1351
#define WRITE_ARRAY(Array, Type, Size, Length) \
1352
for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1354
// creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1355
void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1356
BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1357
// 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1358
short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1360
int length = calculate_array_max_length(writer, array, header_size);
1361
int type_size = type2aelembytes(type);
1362
u4 length_in_bytes = (u4)length * type_size;
1363
u4 size = header_size + length_in_bytes;
1365
writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1366
writer->write_objectID(array);
1367
writer->write_u4(STACK_TRACE_ID);
1368
writer->write_u4(length);
1369
writer->write_u1(type2tag(type));
1373
writer->end_sub_record();
1377
// If the byte ordering is big endian then we can copy most types directly
1381
if (Endian::is_Java_byte_ordering_different()) {
1382
WRITE_ARRAY(array, int, u4, length);
1384
writer->write_raw(array->int_at_addr(0), length_in_bytes);
1389
writer->write_raw(array->byte_at_addr(0), length_in_bytes);
1393
if (Endian::is_Java_byte_ordering_different()) {
1394
WRITE_ARRAY(array, char, u2, length);
1396
writer->write_raw(array->char_at_addr(0), length_in_bytes);
1401
if (Endian::is_Java_byte_ordering_different()) {
1402
WRITE_ARRAY(array, short, u2, length);
1404
writer->write_raw(array->short_at_addr(0), length_in_bytes);
1409
if (Endian::is_Java_byte_ordering_different()) {
1410
WRITE_ARRAY(array, bool, u1, length);
1412
writer->write_raw(array->bool_at_addr(0), length_in_bytes);
1417
if (Endian::is_Java_byte_ordering_different()) {
1418
WRITE_ARRAY(array, long, u8, length);
1420
writer->write_raw(array->long_at_addr(0), length_in_bytes);
1425
// handle float/doubles in a special value to ensure than NaNs are
1426
// written correctly. TO DO: Check if we can avoid this on processors that
1430
for (int i = 0; i < length; i++) {
1431
dump_float(writer, array->float_at(i));
1436
for (int i = 0; i < length; i++) {
1437
dump_double(writer, array->double_at(i));
1441
default : ShouldNotReachHere();
1444
writer->end_sub_record();
1447
// create a HPROF_FRAME record of the given Method* and bci
1448
void DumperSupport::dump_stack_frame(AbstractDumpWriter* writer,
1449
int frame_serial_num,
1450
int class_serial_num,
1454
if (m->is_native()) {
1455
line_number = -3; // native frame
1457
line_number = m->line_number_from_bci(bci);
1460
write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
1461
writer->write_id(frame_serial_num); // frame serial number
1462
writer->write_symbolID(m->name()); // method's name
1463
writer->write_symbolID(m->signature()); // method's signature
1465
assert(m->method_holder()->is_instance_klass(), "not InstanceKlass");
1466
writer->write_symbolID(m->method_holder()->source_file_name()); // source file name
1467
writer->write_u4(class_serial_num); // class serial number
1468
writer->write_u4((u4) line_number); // line number
1472
// Support class used to generate HPROF_UTF8 records from the entries in the
1475
class SymbolTableDumper : public SymbolClosure {
1477
AbstractDumpWriter* _writer;
1478
AbstractDumpWriter* writer() const { return _writer; }
1480
SymbolTableDumper(AbstractDumpWriter* writer) { _writer = writer; }
1481
void do_symbol(Symbol** p);
1484
void SymbolTableDumper::do_symbol(Symbol** p) {
1487
int len = sym->utf8_length();
1489
char* s = sym->as_utf8();
1490
DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1491
writer()->write_symbolID(sym);
1492
writer()->write_raw(s, len);
1496
// Support class used to generate HPROF_GC_CLASS_DUMP records
1498
class ClassDumper : public KlassClosure {
1500
AbstractDumpWriter* _writer;
1501
AbstractDumpWriter* writer() const { return _writer; }
1504
ClassDumper(AbstractDumpWriter* writer) : _writer(writer) {}
1506
void do_klass(Klass* k) {
1507
if (k->is_instance_klass()) {
1508
DumperSupport::dump_instance_class(writer(), k);
1510
DumperSupport::dump_array_class(writer(), k);
1515
// Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
1517
class JNILocalsDumper : public OopClosure {
1519
AbstractDumpWriter* _writer;
1520
u4 _thread_serial_num;
1522
AbstractDumpWriter* writer() const { return _writer; }
1524
JNILocalsDumper(AbstractDumpWriter* writer, u4 thread_serial_num) {
1526
_thread_serial_num = thread_serial_num;
1527
_frame_num = -1; // default - empty stack
1529
void set_frame_number(int n) { _frame_num = n; }
1530
void do_oop(oop* obj_p);
1531
void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1534
void JNILocalsDumper::do_oop(oop* obj_p) {
1535
// ignore null handles
1538
u4 size = 1 + sizeof(address) + 4 + 4;
1539
writer()->start_sub_record(HPROF_GC_ROOT_JNI_LOCAL, size);
1540
writer()->write_objectID(o);
1541
writer()->write_u4(_thread_serial_num);
1542
writer()->write_u4((u4)_frame_num);
1543
writer()->end_sub_record();
1548
// Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
1550
class JNIGlobalsDumper : public OopClosure {
1552
AbstractDumpWriter* _writer;
1553
AbstractDumpWriter* writer() const { return _writer; }
1556
JNIGlobalsDumper(AbstractDumpWriter* writer) {
1559
void do_oop(oop* obj_p);
1560
void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
1563
void JNIGlobalsDumper::do_oop(oop* obj_p) {
1564
oop o = NativeAccess<AS_NO_KEEPALIVE>::oop_load(obj_p);
1567
if (o == nullptr) return;
1568
// we ignore global ref to symbols and other internal objects
1569
if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
1570
u4 size = 1 + 2 * sizeof(address);
1571
writer()->start_sub_record(HPROF_GC_ROOT_JNI_GLOBAL, size);
1572
writer()->write_objectID(o);
1573
writer()->write_rootID(obj_p); // global ref ID
1574
writer()->end_sub_record();
1578
// Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
1580
class StickyClassDumper : public KlassClosure {
1582
AbstractDumpWriter* _writer;
1583
AbstractDumpWriter* writer() const { return _writer; }
1585
StickyClassDumper(AbstractDumpWriter* writer) {
1588
void do_klass(Klass* k) {
1589
if (k->is_instance_klass()) {
1590
InstanceKlass* ik = InstanceKlass::cast(k);
1591
u4 size = 1 + sizeof(address);
1592
writer()->start_sub_record(HPROF_GC_ROOT_STICKY_CLASS, size);
1593
writer()->write_classID(ik);
1594
writer()->end_sub_record();
1599
// Support class used to generate HPROF_GC_ROOT_JAVA_FRAME records.
1601
class JavaStackRefDumper : public StackObj {
1603
AbstractDumpWriter* _writer;
1604
u4 _thread_serial_num;
1606
AbstractDumpWriter* writer() const { return _writer; }
1608
JavaStackRefDumper(AbstractDumpWriter* writer, u4 thread_serial_num)
1609
: _writer(writer), _thread_serial_num(thread_serial_num), _frame_num(-1) // default - empty stack
1613
void set_frame_number(int n) { _frame_num = n; }
1615
void dump_java_stack_refs(StackValueCollection* values);
1618
void JavaStackRefDumper::dump_java_stack_refs(StackValueCollection* values) {
1619
for (int index = 0; index < values->size(); index++) {
1620
if (values->at(index)->type() == T_OBJECT) {
1621
oop o = values->obj_at(index)();
1623
u4 size = 1 + sizeof(address) + 4 + 4;
1624
writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size);
1625
writer()->write_objectID(o);
1626
writer()->write_u4(_thread_serial_num);
1627
writer()->write_u4((u4)_frame_num);
1628
writer()->end_sub_record();
1634
// Class to collect, store and dump thread-related data:
1635
// - HPROF_TRACE and HPROF_FRAME records;
1636
// - HPROF_GC_ROOT_THREAD_OBJ/HPROF_GC_ROOT_JAVA_FRAME/HPROF_GC_ROOT_JNI_LOCAL subrecords.
1637
class ThreadDumper : public CHeapObj<mtInternal> {
1639
enum class ThreadType { Platform, MountedVirtual, UnmountedVirtual };
1642
ThreadType _thread_type;
1643
JavaThread* _java_thread;
1646
GrowableArray<StackFrameInfo*>* _frames;
1647
// non-null if the thread is OOM thread
1648
Method* _oome_constructor;
1649
int _thread_serial_num;
1650
int _start_frame_serial_num;
1652
vframe* get_top_frame() const;
1655
static bool should_dump_pthread(JavaThread* thread) {
1656
return thread->threadObj() != nullptr && !thread->is_exiting() && !thread->is_hidden_from_external_view();
1659
static bool should_dump_vthread(oop vt) {
1660
return java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW
1661
&& java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::TERMINATED;
1664
static bool is_vthread_mounted(oop vt) {
1665
// The code should be consistent with the "mounted virtual thread" case
1666
// (VM_HeapDumper::dump_stack_traces(), ThreadDumper::get_top_frame()).
1667
// I.e. virtual thread is mounted if its carrierThread is not null
1668
// and is_vthread_mounted() for the carrier thread returns true.
1669
oop carrier_thread = java_lang_VirtualThread::carrier_thread(vt);
1670
if (carrier_thread == nullptr) {
1673
JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
1674
return java_thread->is_vthread_mounted();
1677
ThreadDumper(ThreadType thread_type, JavaThread* java_thread, oop thread_oop);
1679
// affects frame_count
1680
void add_oom_frame(Method* oome_constructor) {
1681
assert(_start_frame_serial_num == 0, "add_oom_frame cannot be called after init_serial_nums");
1682
_oome_constructor = oome_constructor;
1685
void init_serial_nums(volatile int* thread_counter, volatile int* frame_counter) {
1686
assert(_start_frame_serial_num == 0, "already initialized");
1687
_thread_serial_num = Atomic::fetch_then_add(thread_counter, 1);
1688
_start_frame_serial_num = Atomic::fetch_then_add(frame_counter, frame_count());
1691
bool oom_thread() const {
1692
return _oome_constructor != nullptr;
1695
int frame_count() const {
1696
return _frames->length() + (oom_thread() ? 1 : 0);
1699
u4 thread_serial_num() const {
1700
return (u4)_thread_serial_num;
1703
u4 stack_trace_serial_num() const {
1704
return (u4)(_thread_serial_num + STACK_TRACE_ID);
1707
// writes HPROF_TRACE and HPROF_FRAME records
1708
// returns number of dumped frames
1709
void dump_stack_traces(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map);
1711
// writes HPROF_GC_ROOT_THREAD_OBJ subrecord
1712
void dump_thread_obj(AbstractDumpWriter* writer);
1714
// Walk the stack of the thread.
1715
// Dumps a HPROF_GC_ROOT_JAVA_FRAME subrecord for each local
1716
// Dumps a HPROF_GC_ROOT_JNI_LOCAL subrecord for each JNI local
1717
void dump_stack_refs(AbstractDumpWriter* writer);
1721
ThreadDumper::ThreadDumper(ThreadType thread_type, JavaThread* java_thread, oop thread_oop)
1722
: _thread_type(thread_type), _java_thread(java_thread), _thread_oop(thread_oop),
1723
_oome_constructor(nullptr),
1724
_thread_serial_num(0), _start_frame_serial_num(0)
1727
if (_thread_type == ThreadType::UnmountedVirtual) {
1728
assert(_java_thread == nullptr, "sanity");
1729
assert(_thread_oop != nullptr, "sanity");
1731
assert(_java_thread != nullptr, "sanity");
1732
assert(_thread_oop != nullptr, "sanity");
1735
_frames = new (mtServiceability) GrowableArray<StackFrameInfo*>(10, mtServiceability);
1736
bool stop_at_vthread_entry = _thread_type == ThreadType::MountedVirtual;
1738
// vframes are resource allocated
1739
Thread* current_thread = Thread::current();
1740
ResourceMark rm(current_thread);
1741
HandleMark hm(current_thread);
1743
for (vframe* vf = get_top_frame(); vf != nullptr; vf = vf->sender()) {
1744
if (stop_at_vthread_entry && vf->is_vthread_entry()) {
1747
if (vf->is_java_frame()) {
1748
javaVFrame* jvf = javaVFrame::cast(vf);
1749
_frames->append(new StackFrameInfo(jvf, false));
1751
// ignore non-Java frames
1756
void ThreadDumper::dump_stack_traces(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map) {
1757
assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_nums are not initialized");
1759
// write HPROF_FRAME records for this thread's stack trace
1760
int depth = _frames->length();
1761
int frame_serial_num = _start_frame_serial_num;
1765
// write fake frame that makes it look like the thread, which caused OOME,
1766
// is in the OutOfMemoryError zero-parameter constructor
1767
int oome_serial_num = klass_map->find(_oome_constructor->method_holder());
1768
// the class serial number starts from 1
1769
assert(oome_serial_num > 0, "OutOfMemoryError class not found");
1770
DumperSupport::dump_stack_frame(writer, ++frame_serial_num, oome_serial_num, _oome_constructor, 0);
1774
for (int j = 0; j < _frames->length(); j++) {
1775
StackFrameInfo* frame = _frames->at(j);
1776
Method* m = frame->method();
1777
int class_serial_num = klass_map->find(m->method_holder());
1778
// the class serial number starts from 1
1779
assert(class_serial_num > 0, "class not found");
1780
DumperSupport::dump_stack_frame(writer, ++frame_serial_num, class_serial_num, m, frame->bci());
1783
// write HPROF_TRACE record for the thread
1784
DumperSupport::write_header(writer, HPROF_TRACE, checked_cast<u4>(3 * sizeof(u4) + depth * oopSize));
1785
writer->write_u4(stack_trace_serial_num()); // stack trace serial number
1786
writer->write_u4(thread_serial_num()); // thread serial number
1787
writer->write_u4((u4)depth); // frame count (including oom frame)
1788
for (int j = 1; j <= depth; j++) {
1789
writer->write_id(_start_frame_serial_num + j);
1793
void ThreadDumper::dump_thread_obj(AbstractDumpWriter * writer) {
1794
assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_num is not initialized");
1796
u4 size = 1 + sizeof(address) + 4 + 4;
1797
writer->start_sub_record(HPROF_GC_ROOT_THREAD_OBJ, size);
1798
writer->write_objectID(_thread_oop);
1799
writer->write_u4(thread_serial_num()); // thread serial number
1800
writer->write_u4(stack_trace_serial_num()); // stack trace serial number
1801
writer->end_sub_record();
1804
void ThreadDumper::dump_stack_refs(AbstractDumpWriter * writer) {
1805
assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_num is not initialized");
1807
JNILocalsDumper blk(writer, thread_serial_num());
1808
if (_thread_type == ThreadType::Platform) {
1809
if (!_java_thread->has_last_Java_frame()) {
1810
// no last java frame but there may be JNI locals
1811
_java_thread->active_handles()->oops_do(&blk);
1816
JavaStackRefDumper java_ref_dumper(writer, thread_serial_num());
1818
// vframes are resource allocated
1819
Thread* current_thread = Thread::current();
1820
ResourceMark rm(current_thread);
1821
HandleMark hm(current_thread);
1823
bool stopAtVthreadEntry = _thread_type == ThreadType::MountedVirtual;
1824
frame* last_entry_frame = nullptr;
1825
bool is_top_frame = true;
1831
for (vframe* vf = get_top_frame(); vf != nullptr; vf = vf->sender()) {
1832
if (stopAtVthreadEntry && vf->is_vthread_entry()) {
1836
if (vf->is_java_frame()) {
1837
javaVFrame* jvf = javaVFrame::cast(vf);
1838
if (!(jvf->method()->is_native())) {
1839
java_ref_dumper.set_frame_number(depth);
1840
java_ref_dumper.dump_java_stack_refs(jvf->locals());
1841
java_ref_dumper.dump_java_stack_refs(jvf->expressions());
1844
blk.set_frame_number(depth);
1846
// JNI locals for the top frame.
1847
assert(_java_thread != nullptr, "impossible for unmounted vthread");
1848
_java_thread->active_handles()->oops_do(&blk);
1850
if (last_entry_frame != nullptr) {
1851
// JNI locals for the entry frame
1852
assert(last_entry_frame->is_entry_frame(), "checking");
1853
last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(&blk);
1857
last_entry_frame = nullptr;
1858
// increment only for Java frames
1861
// externalVFrame - for an entry frame then we report the JNI locals
1862
// when we find the corresponding javaVFrame
1863
frame* fr = vf->frame_pointer();
1864
assert(fr != nullptr, "sanity check");
1865
if (fr->is_entry_frame()) {
1866
last_entry_frame = fr;
1869
is_top_frame = false;
1871
assert(depth == frame_count(), "total number of Java frames not matched");
1874
vframe* ThreadDumper::get_top_frame() const {
1875
if (_thread_type == ThreadType::UnmountedVirtual) {
1876
ContinuationWrapper cont(java_lang_VirtualThread::continuation(_thread_oop));
1877
if (cont.is_empty()) {
1880
assert(!cont.is_mounted(), "sanity check");
1881
stackChunkOop chunk = cont.last_nonempty_chunk();
1882
if (chunk == nullptr || chunk->is_empty()) {
1886
RegisterMap reg_map(cont.continuation(), RegisterMap::UpdateMap::include);
1887
frame fr = chunk->top_frame(®_map);
1888
vframe* vf = vframe::new_vframe(&fr, ®_map, nullptr); // don't need JavaThread
1892
RegisterMap reg_map(_java_thread,
1893
RegisterMap::UpdateMap::include,
1894
RegisterMap::ProcessFrames::include,
1895
RegisterMap::WalkContinuation::skip);
1896
switch (_thread_type) {
1897
case ThreadType::Platform:
1898
if (!_java_thread->has_last_Java_frame()) {
1901
return _java_thread->is_vthread_mounted()
1902
? _java_thread->carrier_last_java_vframe(®_map)
1903
: _java_thread->platform_thread_last_java_vframe(®_map);
1905
case ThreadType::MountedVirtual:
1906
return _java_thread->last_java_vframe(®_map);
1908
default: // make compilers happy
1911
ShouldNotReachHere();
1915
// Callback to dump thread-related data for unmounted virtual threads;
1916
// implemented by VM_HeapDumper.
1917
class UnmountedVThreadDumper {
1919
virtual void dump_vthread(oop vt, AbstractDumpWriter* segment_writer) = 0;
1922
// Support class used when iterating over the heap.
1923
class HeapObjectDumper : public ObjectClosure {
1925
AbstractDumpWriter* _writer;
1926
AbstractDumpWriter* writer() { return _writer; }
1927
UnmountedVThreadDumper* _vthread_dumper;
1929
DumperClassCacheTable _class_cache;
1932
HeapObjectDumper(AbstractDumpWriter* writer, UnmountedVThreadDumper* vthread_dumper)
1933
: _writer(writer), _vthread_dumper(vthread_dumper) {}
1935
// called for each object in the heap
1936
void do_object(oop o);
1939
void HeapObjectDumper::do_object(oop o) {
1940
// skip classes as these emitted as HPROF_GC_CLASS_DUMP records
1941
if (o->klass() == vmClasses::Class_klass()) {
1942
if (!java_lang_Class::is_primitive(o)) {
1947
if (DumperSupport::mask_dormant_archived_object(o, nullptr) == nullptr) {
1951
if (o->is_instance()) {
1952
// create a HPROF_GC_INSTANCE record for each object
1953
DumperSupport::dump_instance(writer(), o, &_class_cache);
1954
// If we encounter an unmounted virtual thread it needs to be dumped explicitly
1955
// (mounted virtual threads are dumped with their carriers).
1956
if (java_lang_VirtualThread::is_instance(o)
1957
&& ThreadDumper::should_dump_vthread(o) && !ThreadDumper::is_vthread_mounted(o)) {
1958
_vthread_dumper->dump_vthread(o, writer());
1960
} else if (o->is_objArray()) {
1961
// create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
1962
DumperSupport::dump_object_array(writer(), objArrayOop(o));
1963
} else if (o->is_typeArray()) {
1964
// create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
1965
DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
1969
// The dumper controller for parallel heap dump
1970
class DumperController : public CHeapObj<mtInternal> {
1973
Mutex* _global_writer_lock;
1975
const uint _dumper_number;
1976
uint _complete_number;
1978
bool _started; // VM dumper started and acquired global writer lock
1981
DumperController(uint number) :
1982
// _lock and _global_writer_lock are used for synchronization between GC worker threads inside safepoint,
1983
// so we lock with _no_safepoint_check_flag.
1984
// signal_start() acquires _lock when global writer is locked,
1985
// its rank must be less than _global_writer_lock rank.
1986
_lock(new (std::nothrow) PaddedMonitor(Mutex::nosafepoint - 1, "DumperController_lock")),
1987
_global_writer_lock(new (std::nothrow) Mutex(Mutex::nosafepoint, "DumpWriter_lock")),
1988
_dumper_number(number),
1989
_complete_number(0),
1993
~DumperController() {
1995
delete _global_writer_lock;
1998
// parallel (non VM) dumpers must wait until VM dumper acquires global writer lock
1999
void wait_for_start_signal() {
2000
MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2001
while (_started == false) {
2006
void signal_start() {
2007
MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2012
void lock_global_writer() {
2013
_global_writer_lock->lock_without_safepoint_check();
2016
void unlock_global_writer() {
2017
_global_writer_lock->unlock();
2020
void dumper_complete(DumpWriter* local_writer, DumpWriter* global_writer) {
2021
MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2023
// propagate local error to global if any
2024
if (local_writer->has_error()) {
2025
global_writer->set_error(local_writer->error());
2030
void wait_all_dumpers_complete() {
2031
MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2032
while (_complete_number != _dumper_number) {
2038
// DumpMerger merges separate dump files into a complete one
2039
class DumpMerger : public StackObj {
2041
DumpWriter* _writer;
2047
void merge_file(const char* path);
2049
void set_error(const char* msg);
2052
DumpMerger(const char* path, DumpWriter* writer, int dump_seq) :
2055
_has_error(_writer->has_error()),
2056
_dump_seq(dump_seq) {}
2060
// returns path for the parallel DumpWriter (resource allocated)
2061
static char* get_writer_path(const char* base_path, int seq);
2065
char* DumpMerger::get_writer_path(const char* base_path, int seq) {
2066
// approximate required buffer size
2067
size_t buf_size = strlen(base_path)
2069
+ 10 // number (that's enough for 2^32 parallel dumpers)
2072
char* path = NEW_RESOURCE_ARRAY(char, buf_size);
2073
memset(path, 0, buf_size);
2075
os::snprintf(path, buf_size, "%s.p%d", base_path, seq);
2081
void DumpMerger::merge_done() {
2082
// Writes the HPROF_HEAP_DUMP_END record.
2084
DumperSupport::end_of_dump(_writer);
2087
_dump_seq = 0; //reset
2090
void DumpMerger::set_error(const char* msg) {
2091
assert(msg != nullptr, "sanity check");
2092
log_error(heapdump)("%s (file: %s)", msg, _path);
2093
_writer->set_error(msg);
2098
// Merge segmented heap files via sendfile, it's more efficient than the
2099
// read+write combination, which would require transferring data to and from
2101
void DumpMerger::merge_file(const char* path) {
2102
TraceTime timer("Merge segmented heap file directly", TRACETIME_LOG(Info, heapdump));
2104
int segment_fd = os::open(path, O_RDONLY, 0);
2105
if (segment_fd == -1) {
2106
set_error("Can not open segmented heap file during merging");
2111
if (os::stat(path, &st) != 0) {
2112
::close(segment_fd);
2113
set_error("Can not get segmented heap file size during merging");
2117
// A successful call to sendfile may write fewer bytes than requested; the
2118
// caller should be prepared to retry the call if there were unsent bytes.
2120
while (offset < st.st_size) {
2121
int ret = os::Linux::sendfile(_writer->get_fd(), segment_fd, &offset, st.st_size);
2123
::close(segment_fd);
2124
set_error("Failed to merge segmented heap file");
2129
// As sendfile variant does not call the write method of the global writer,
2130
// bytes_written is also incorrect for this variant, we need to explicitly
2131
// accumulate bytes_written for the global writer in this case
2132
julong accum = _writer->bytes_written() + st.st_size;
2133
_writer->set_bytes_written(accum);
2134
::close(segment_fd);
2137
// Generic implementation using read+write
2138
void DumpMerger::merge_file(const char* path) {
2139
TraceTime timer("Merge segmented heap file", TRACETIME_LOG(Info, heapdump));
2141
fileStream segment_fs(path, "rb");
2142
if (!segment_fs.is_open()) {
2143
set_error("Can not open segmented heap file during merging");
2150
// Use _writer buffer for reading.
2151
while ((cnt = segment_fs.read(_writer->buffer(), 1, _writer->buffer_size())) != 0) {
2152
_writer->set_position(cnt);
2157
if (segment_fs.fileSize() != total) {
2158
set_error("Merged heap dump is incomplete");
2163
void DumpMerger::do_merge() {
2164
TraceTime timer("Merge heap files complete", TRACETIME_LOG(Info, heapdump));
2166
// Since contents in segmented heap file were already zipped, we don't need to zip
2167
// them again during merging.
2168
AbstractCompressor* saved_compressor = _writer->compressor();
2169
_writer->set_compressor(nullptr);
2171
// Merge the content of the remaining files into base file. Regardless of whether
2172
// the merge process is successful or not, these segmented files will be deleted.
2173
for (int i = 0; i < _dump_seq; i++) {
2175
const char* path = get_writer_path(_path, i);
2179
// Delete selected segmented heap file nevertheless
2180
if (remove(path) != 0) {
2181
log_info(heapdump)("Removal of segment file (%d) failed (%d)", i, errno);
2185
// restore compressor for further use
2186
_writer->set_compressor(saved_compressor);
2190
// The VM operation that performs the heap dump
2191
class VM_HeapDumper : public VM_GC_Operation, public WorkerTask, public UnmountedVThreadDumper {
2193
static VM_HeapDumper* _global_dumper;
2194
static DumpWriter* _global_writer;
2195
DumpWriter* _local_writer;
2196
JavaThread* _oome_thread;
2197
Method* _oome_constructor;
2198
bool _gc_before_heap_dump;
2199
GrowableArray<Klass*>* _klass_map;
2201
ThreadDumper** _thread_dumpers; // platform, carrier and mounted virtual threads
2202
int _thread_dumpers_count;
2203
volatile int _thread_serial_num;
2204
volatile int _frame_serial_num;
2206
volatile int _dump_seq;
2207
// parallel heap dump support
2208
uint _num_dumper_threads;
2209
DumperController* _dumper_controller;
2210
ParallelObjectIterator* _poi;
2212
// Dumper id of VMDumper thread.
2213
static const int VMDumperId = 0;
2214
// VM dumper dumps both heap and non-heap data, other dumpers dump heap-only data.
2215
static bool is_vm_dumper(int dumper_id) { return dumper_id == VMDumperId; }
2216
// the 1st dumper calling get_next_dumper_id becomes VM dumper
2217
int get_next_dumper_id() {
2218
return Atomic::fetch_then_add(&_dump_seq, 1);
2221
// accessors and setters
2222
static VM_HeapDumper* dumper() { assert(_global_dumper != nullptr, "Error"); return _global_dumper; }
2223
static DumpWriter* writer() { assert(_global_writer != nullptr, "Error"); return _global_writer; }
2225
void set_global_dumper() {
2226
assert(_global_dumper == nullptr, "Error");
2227
_global_dumper = this;
2229
void set_global_writer() {
2230
assert(_global_writer == nullptr, "Error");
2231
_global_writer = _local_writer;
2233
void clear_global_dumper() { _global_dumper = nullptr; }
2234
void clear_global_writer() { _global_writer = nullptr; }
2236
bool skip_operation() const;
2238
// writes a HPROF_LOAD_CLASS record to global writer
2239
static void do_load_class(Klass* k);
2241
// HPROF_GC_ROOT_THREAD_OBJ records for platform and mounted virtual threads
2242
void dump_threads(AbstractDumpWriter* writer);
2244
void add_class_serial_number(Klass* k, int serial_num) {
2245
_klass_map->at_put_grow(serial_num, k);
2248
bool is_oom_thread(JavaThread* thread) const {
2249
return thread == _oome_thread && _oome_constructor != nullptr;
2252
// HPROF_TRACE and HPROF_FRAME records for platform and mounted virtual threads
2253
void dump_stack_traces(AbstractDumpWriter* writer);
2256
VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome, uint num_dump_threads) :
2257
VM_GC_Operation(0 /* total collections, dummy, ignored */,
2258
GCCause::_heap_dump /* GC Cause */,
2259
0 /* total full collections, dummy, ignored */,
2260
gc_before_heap_dump),
2261
WorkerTask("dump heap") {
2262
_local_writer = writer;
2263
_gc_before_heap_dump = gc_before_heap_dump;
2264
_klass_map = new (mtServiceability) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, mtServiceability);
2266
_thread_dumpers = nullptr;
2267
_thread_dumpers_count = 0;
2268
_thread_serial_num = 1;
2269
_frame_serial_num = 1;
2271
_dump_seq = VMDumperId;
2272
_num_dumper_threads = num_dump_threads;
2273
_dumper_controller = nullptr;
2276
assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
2277
// get OutOfMemoryError zero-parameter constructor
2278
InstanceKlass* oome_ik = vmClasses::OutOfMemoryError_klass();
2279
_oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
2280
vmSymbols::void_method_signature());
2281
// get thread throwing OOME when generating the heap dump at OOME
2282
_oome_thread = JavaThread::current();
2284
_oome_thread = nullptr;
2285
_oome_constructor = nullptr;
2290
if (_thread_dumpers != nullptr) {
2291
for (int i = 0; i < _thread_dumpers_count; i++) {
2292
delete _thread_dumpers[i];
2294
FREE_C_HEAP_ARRAY(ThreadDumper*, _thread_dumpers);
2297
if (_dumper_controller != nullptr) {
2298
delete _dumper_controller;
2299
_dumper_controller = nullptr;
2303
int dump_seq() { return _dump_seq; }
2304
bool is_parallel_dump() { return _num_dumper_threads > 1; }
2305
void prepare_parallel_dump(WorkerThreads* workers);
2307
VMOp_Type type() const { return VMOp_HeapDumper; }
2308
virtual bool doit_prologue();
2310
void work(uint worker_id);
2312
// UnmountedVThreadDumper implementation
2313
void dump_vthread(oop vt, AbstractDumpWriter* segment_writer);
2316
VM_HeapDumper* VM_HeapDumper::_global_dumper = nullptr;
2317
DumpWriter* VM_HeapDumper::_global_writer = nullptr;
2319
bool VM_HeapDumper::skip_operation() const {
2323
// fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
2324
void DumperSupport::end_of_dump(AbstractDumpWriter* writer) {
2325
writer->finish_dump_segment();
2327
writer->write_u1(HPROF_HEAP_DUMP_END);
2328
writer->write_u4(0);
2329
writer->write_u4(0);
2332
// writes a HPROF_LOAD_CLASS record for the class
2333
void VM_HeapDumper::do_load_class(Klass* k) {
2334
static u4 class_serial_num = 0;
2336
// len of HPROF_LOAD_CLASS record
2337
u4 remaining = 2*oopSize + 2*sizeof(u4);
2339
DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
2341
// class serial number is just a number
2342
writer()->write_u4(++class_serial_num);
2345
writer()->write_classID(k);
2347
// add the Klass* and class serial number pair
2348
dumper()->add_class_serial_number(k, class_serial_num);
2350
writer()->write_u4(STACK_TRACE_ID);
2353
Symbol* name = k->name();
2354
writer()->write_symbolID(name);
2357
// Write a HPROF_GC_ROOT_THREAD_OBJ record for platform/carrier and mounted virtual threads.
2358
// Then walk the stack so that locals and JNI locals are dumped.
2359
void VM_HeapDumper::dump_threads(AbstractDumpWriter* writer) {
2360
for (int i = 0; i < _thread_dumpers_count; i++) {
2361
_thread_dumpers[i]->dump_thread_obj(writer);
2362
_thread_dumpers[i]->dump_stack_refs(writer);
2366
bool VM_HeapDumper::doit_prologue() {
2367
if (_gc_before_heap_dump && UseZGC) {
2368
// ZGC cannot perform a synchronous GC cycle from within the VM thread.
2369
// So ZCollectedHeap::collect_as_vm_thread() is a noop. To respect the
2370
// _gc_before_heap_dump flag a synchronous GC cycle is performed from
2371
// the caller thread in the prologue.
2372
Universe::heap()->collect(GCCause::_heap_dump);
2374
return VM_GC_Operation::doit_prologue();
2377
void VM_HeapDumper::prepare_parallel_dump(WorkerThreads* workers) {
2378
uint num_active_workers = workers != nullptr ? workers->active_workers() : 0;
2379
uint num_requested_dump_threads = _num_dumper_threads;
2380
// check if we can dump in parallel based on requested and active threads
2381
if (num_active_workers <= 1 || num_requested_dump_threads <= 1) {
2382
_num_dumper_threads = 1;
2384
_num_dumper_threads = clamp(num_requested_dump_threads, 2U, num_active_workers);
2386
_dumper_controller = new (std::nothrow) DumperController(_num_dumper_threads);
2387
bool can_parallel = _num_dumper_threads > 1;
2388
log_info(heapdump)("Requested dump threads %u, active dump threads %u, "
2389
"actual dump threads %u, parallelism %s",
2390
num_requested_dump_threads, num_active_workers,
2391
_num_dumper_threads, can_parallel ? "true" : "false");
2394
// The VM operation that dumps the heap. The dump consists of the following
2399
// [HPROF_LOAD_CLASS]*
2400
// [[HPROF_FRAME]*|HPROF_TRACE]*
2401
// [HPROF_GC_CLASS_DUMP]*
2402
// [HPROF_HEAP_DUMP_SEGMENT]*
2403
// HPROF_HEAP_DUMP_END
2405
// The HPROF_TRACE records represent the stack traces where the heap dump
2406
// is generated and a "dummy trace" record which does not include
2407
// any frames. The dummy trace record is used to be referenced as the
2408
// unknown object alloc site.
2410
// Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records.
2411
// To allow the heap dump be generated in a single pass we remember the position
2412
// of the dump length and fix it up after all sub-records have been written.
2413
// To generate the sub-records we iterate over the heap, writing
2414
// HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
2415
// records as we go. Once that is done we write records for some of the GC
2418
void VM_HeapDumper::doit() {
2420
CollectedHeap* ch = Universe::heap();
2422
ch->ensure_parsability(false); // must happen, even if collection does
2423
// not happen (e.g. due to GCLocker)
2425
if (_gc_before_heap_dump) {
2426
if (GCLocker::is_active()) {
2427
warning("GC locker is held; pre-heapdump GC was skipped");
2429
ch->collect_as_vm_thread(GCCause::_heap_dump);
2433
// At this point we should be the only dumper active, so
2434
// the following should be safe.
2435
set_global_dumper();
2436
set_global_writer();
2438
WorkerThreads* workers = ch->safepoint_workers();
2439
prepare_parallel_dump(workers);
2441
if (!is_parallel_dump()) {
2444
ParallelObjectIterator poi(_num_dumper_threads);
2446
workers->run_task(this, _num_dumper_threads);
2450
// Now we clear the global variables, so that a future dumper can run.
2451
clear_global_dumper();
2452
clear_global_writer();
2455
void VM_HeapDumper::work(uint worker_id) {
2456
// VM Dumper works on all non-heap data dumping and part of heap iteration.
2457
int dumper_id = get_next_dumper_id();
2459
if (is_vm_dumper(dumper_id)) {
2460
// lock global writer, it will be unlocked after VM Dumper finishes with non-heap data
2461
_dumper_controller->lock_global_writer();
2462
_dumper_controller->signal_start();
2464
_dumper_controller->wait_for_start_signal();
2467
if (is_vm_dumper(dumper_id)) {
2468
TraceTime timer("Dump non-objects", TRACETIME_LOG(Info, heapdump));
2469
// Write the file header - we always use 1.0.2
2470
const char* header = "JAVA PROFILE 1.0.2";
2472
// header is few bytes long - no chance to overflow int
2473
writer()->write_raw(header, strlen(header) + 1); // NUL terminated
2474
writer()->write_u4(oopSize);
2475
// timestamp is current time in ms
2476
writer()->write_u8(os::javaTimeMillis());
2477
// HPROF_UTF8 records
2478
SymbolTableDumper sym_dumper(writer());
2479
SymbolTable::symbols_do(&sym_dumper);
2481
// write HPROF_LOAD_CLASS records
2483
LockedClassesDo locked_load_classes(&do_load_class);
2484
ClassLoaderDataGraph::classes_do(&locked_load_classes);
2487
// write HPROF_FRAME and HPROF_TRACE records
2488
// this must be called after _klass_map is built when iterating the classes above.
2489
dump_stack_traces(writer());
2491
// unlock global writer, so parallel dumpers can dump stack traces of unmounted virtual threads
2492
_dumper_controller->unlock_global_writer();
2495
// HPROF_HEAP_DUMP/HPROF_HEAP_DUMP_SEGMENT starts here
2498
// share global compressor, local DumpWriter is not responsible for its life cycle
2499
DumpWriter segment_writer(DumpMerger::get_writer_path(writer()->get_file_path(), dumper_id),
2500
writer()->is_overwrite(), writer()->compressor());
2501
if (!segment_writer.has_error()) {
2502
if (is_vm_dumper(dumper_id)) {
2503
// dump some non-heap subrecords to heap dump segment
2504
TraceTime timer("Dump non-objects (part 2)", TRACETIME_LOG(Info, heapdump));
2505
// Writes HPROF_GC_CLASS_DUMP records
2506
ClassDumper class_dumper(&segment_writer);
2507
ClassLoaderDataGraph::classes_do(&class_dumper);
2509
// HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
2510
dump_threads(&segment_writer);
2512
// HPROF_GC_ROOT_JNI_GLOBAL
2513
JNIGlobalsDumper jni_dumper(&segment_writer);
2514
JNIHandles::oops_do(&jni_dumper);
2515
// technically not jni roots, but global roots
2516
// for things like preallocated throwable backtraces
2517
Universe::vm_global()->oops_do(&jni_dumper);
2518
// HPROF_GC_ROOT_STICKY_CLASS
2519
// These should be classes in the null class loader data, and not all classes
2520
// if !ClassUnloading
2521
StickyClassDumper stiky_class_dumper(&segment_writer);
2522
ClassLoaderData::the_null_class_loader_data()->classes_do(&stiky_class_dumper);
2526
// writes HPROF_GC_INSTANCE_DUMP records.
2527
// After each sub-record is written check_segment_length will be invoked
2528
// to check if the current segment exceeds a threshold. If so, a new
2529
// segment is started.
2530
// The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
2531
// of the heap dump.
2533
TraceTime timer(is_parallel_dump() ? "Dump heap objects in parallel" : "Dump heap objects", TRACETIME_LOG(Info, heapdump));
2534
HeapObjectDumper obj_dumper(&segment_writer, this);
2535
if (!is_parallel_dump()) {
2536
Universe::heap()->object_iterate(&obj_dumper);
2539
_poi->object_iterate(&obj_dumper, worker_id);
2542
segment_writer.finish_dump_segment();
2543
segment_writer.flush();
2546
_dumper_controller->dumper_complete(&segment_writer, writer());
2548
if (is_vm_dumper(dumper_id)) {
2549
_dumper_controller->wait_all_dumpers_complete();
2551
// flush global writer
2554
// At this point, all fragments of the heapdump have been written to separate files.
2555
// We need to merge them into a complete heapdump and write HPROF_HEAP_DUMP_END at that time.
2559
void VM_HeapDumper::dump_stack_traces(AbstractDumpWriter* writer) {
2560
// write a HPROF_TRACE record without any frames to be referenced as object alloc sites
2561
DumperSupport::write_header(writer, HPROF_TRACE, 3 * sizeof(u4));
2562
writer->write_u4((u4)STACK_TRACE_ID);
2563
writer->write_u4(0); // thread number
2564
writer->write_u4(0); // frame count
2566
// max number if every platform thread is carrier with mounted virtual thread
2567
_thread_dumpers = NEW_C_HEAP_ARRAY(ThreadDumper*, Threads::number_of_threads() * 2, mtInternal);
2569
for (JavaThreadIteratorWithHandle jtiwh; JavaThread * thread = jtiwh.next(); ) {
2570
if (ThreadDumper::should_dump_pthread(thread)) {
2571
bool add_oom_frame = is_oom_thread(thread);
2573
oop mounted_vt = thread->is_vthread_mounted() ? thread->vthread() : nullptr;
2574
if (mounted_vt != nullptr && !ThreadDumper::should_dump_vthread(mounted_vt)) {
2575
mounted_vt = nullptr;
2578
// mounted vthread (if any)
2579
if (mounted_vt != nullptr) {
2580
ThreadDumper* thread_dumper = new ThreadDumper(ThreadDumper::ThreadType::MountedVirtual, thread, mounted_vt);
2581
_thread_dumpers[_thread_dumpers_count++] = thread_dumper;
2582
if (add_oom_frame) {
2583
thread_dumper->add_oom_frame(_oome_constructor);
2584
// we add oom frame to the VT stack, don't add it to the carrier thread stack
2585
add_oom_frame = false;
2587
thread_dumper->init_serial_nums(&_thread_serial_num, &_frame_serial_num);
2588
thread_dumper->dump_stack_traces(writer, _klass_map);
2591
// platform or carrier thread
2592
ThreadDumper* thread_dumper = new ThreadDumper(ThreadDumper::ThreadType::Platform, thread, thread->threadObj());
2593
_thread_dumpers[_thread_dumpers_count++] = thread_dumper;
2594
if (add_oom_frame) {
2595
thread_dumper->add_oom_frame(_oome_constructor);
2597
thread_dumper->init_serial_nums(&_thread_serial_num, &_frame_serial_num);
2598
thread_dumper->dump_stack_traces(writer, _klass_map);
2603
void VM_HeapDumper::dump_vthread(oop vt, AbstractDumpWriter* segment_writer) {
2604
// unmounted vthread has no JavaThread
2605
ThreadDumper thread_dumper(ThreadDumper::ThreadType::UnmountedVirtual, nullptr, vt);
2606
thread_dumper.init_serial_nums(&_thread_serial_num, &_frame_serial_num);
2608
// write HPROF_TRACE/HPROF_FRAME records to global writer
2609
_dumper_controller->lock_global_writer();
2610
thread_dumper.dump_stack_traces(writer(), _klass_map);
2611
_dumper_controller->unlock_global_writer();
2613
// write HPROF_GC_ROOT_THREAD_OBJ/HPROF_GC_ROOT_JAVA_FRAME/HPROF_GC_ROOT_JNI_LOCAL subrecord
2614
// to segment writer
2615
thread_dumper.dump_thread_obj(segment_writer);
2616
thread_dumper.dump_stack_refs(segment_writer);
2619
// dump the heap to given path.
2620
int HeapDumper::dump(const char* path, outputStream* out, int compression, bool overwrite, uint num_dump_threads) {
2621
assert(path != nullptr && strlen(path) > 0, "path missing");
2623
// print message in interactive case
2624
if (out != nullptr) {
2625
out->print_cr("Dumping heap to %s ...", path);
2629
if (_oome && num_dump_threads > 1) {
2630
// Each additional parallel writer requires several MB of internal memory
2631
// (DumpWriter buffer, DumperClassCacheTable, GZipCompressor buffers).
2632
// For the OOM handling we may already be limited in memory.
2633
// Lets ensure we have at least 20MB per thread.
2634
julong max_threads = os::free_memory() / (20 * M);
2635
if (num_dump_threads > max_threads) {
2636
num_dump_threads = MAX2<uint>(1, (uint)max_threads);
2641
EventHeapDump event;
2643
AbstractCompressor* compressor = nullptr;
2645
if (compression > 0) {
2646
compressor = new (std::nothrow) GZipCompressor(compression);
2648
if (compressor == nullptr) {
2649
set_error("Could not allocate gzip compressor");
2654
DumpWriter writer(path, overwrite, compressor);
2656
if (writer.error() != nullptr) {
2657
set_error(writer.error());
2658
if (out != nullptr) {
2659
out->print_cr("Unable to create %s: %s", path,
2660
(error() != nullptr) ? error() : "reason unknown");
2665
// generate the segmented heap dump into separate files
2666
VM_HeapDumper dumper(&writer, _gc_before_heap_dump, _oome, num_dump_threads);
2667
VMThread::execute(&dumper);
2669
// record any error that the writer may have encountered
2670
set_error(writer.error());
2672
// Heap dump process is done in two phases
2674
// Phase 1: Concurrent threads directly write heap data to multiple heap files.
2675
// This is done by VM_HeapDumper, which is performed within safepoint.
2677
// Phase 2: Merge multiple heap files into one complete heap dump file.
2678
// This is done by DumpMerger, which is performed outside safepoint
2680
DumpMerger merger(path, &writer, dumper.dump_seq());
2681
// Perform heapdump file merge operation in the current thread prevents us
2682
// from occupying the VM Thread, which in turn affects the occurrence of
2683
// GC and other VM operations.
2685
if (writer.error() != nullptr) {
2686
set_error(writer.error());
2690
if (error() == nullptr) {
2691
event.set_destination(path);
2692
event.set_gcBeforeDump(_gc_before_heap_dump);
2693
event.set_size(writer.bytes_written());
2694
event.set_onOutOfMemoryError(_oome);
2695
event.set_overwrite(overwrite);
2696
event.set_compression(compression);
2699
log_debug(cds, heap)("Error %s while dumping heap", error());
2702
// print message in interactive case
2703
if (out != nullptr) {
2705
if (error() == nullptr) {
2706
out->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]",
2707
writer.bytes_written(), timer()->seconds());
2709
out->print_cr("Dump file is incomplete: %s", writer.error());
2713
if (compressor != nullptr) {
2716
return (writer.error() == nullptr) ? 0 : -1;
2719
// stop timer (if still active), and free any error string we might be holding
2720
HeapDumper::~HeapDumper() {
2721
if (timer()->is_active()) {
2728
// returns the error string (resource allocated), or null
2729
char* HeapDumper::error_as_C_string() const {
2730
if (error() != nullptr) {
2731
char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
2732
strcpy(str, error());
2739
// set the error string
2740
void HeapDumper::set_error(char const* error) {
2741
if (_error != nullptr) {
2744
if (error == nullptr) {
2747
_error = os::strdup(error);
2748
assert(_error != nullptr, "allocation failure");
2752
// Called by out-of-memory error reporting by a single Java thread
2753
// outside of a JVM safepoint
2754
void HeapDumper::dump_heap_from_oome() {
2755
HeapDumper::dump_heap(true);
2758
// Called by error reporting by a single Java thread outside of a JVM safepoint,
2759
// or by heap dumping by the VM thread during a (GC) safepoint. Thus, these various
2760
// callers are strictly serialized and guaranteed not to interfere below. For more
2761
// general use, however, this method will need modification to prevent
2762
// inteference when updating the static variables base_path and dump_file_seq below.
2763
void HeapDumper::dump_heap() {
2764
HeapDumper::dump_heap(false);
2767
void HeapDumper::dump_heap(bool oome) {
2768
static char base_path[JVM_MAXPATHLEN] = {'\0'};
2769
static uint dump_file_seq = 0;
2771
const int max_digit_chars = 20;
2773
const char* dump_file_name = "java_pid";
2774
const char* dump_file_ext = HeapDumpGzipLevel > 0 ? ".hprof.gz" : ".hprof";
2776
// The dump file defaults to java_pid<pid>.hprof in the current working
2777
// directory. HeapDumpPath=<file> can be used to specify an alternative
2778
// dump file name or a directory where dump file is created.
2779
if (dump_file_seq == 0) { // first time in, we initialize base_path
2780
// Calculate potentially longest base path and check if we have enough
2781
// allocated statically.
2782
const size_t total_length =
2783
(HeapDumpPath == nullptr ? 0 : strlen(HeapDumpPath)) +
2784
strlen(os::file_separator()) + max_digit_chars +
2785
strlen(dump_file_name) + strlen(dump_file_ext) + 1;
2786
if (total_length > sizeof(base_path)) {
2787
warning("Cannot create heap dump file. HeapDumpPath is too long.");
2791
bool use_default_filename = true;
2792
if (HeapDumpPath == nullptr || HeapDumpPath[0] == '\0') {
2793
// HeapDumpPath=<file> not specified
2795
strcpy(base_path, HeapDumpPath);
2796
// check if the path is a directory (must exist)
2797
DIR* dir = os::opendir(base_path);
2798
if (dir == nullptr) {
2799
use_default_filename = false;
2801
// HeapDumpPath specified a directory. We append a file separator
2804
size_t fs_len = strlen(os::file_separator());
2805
if (strlen(base_path) >= fs_len) {
2806
char* end = base_path;
2807
end += (strlen(base_path) - fs_len);
2808
if (strcmp(end, os::file_separator()) != 0) {
2809
strcat(base_path, os::file_separator());
2814
// If HeapDumpPath wasn't a file name then we append the default name
2815
if (use_default_filename) {
2816
const size_t dlen = strlen(base_path); // if heap dump dir specified
2817
jio_snprintf(&base_path[dlen], sizeof(base_path)-dlen, "%s%d%s",
2818
dump_file_name, os::current_process_id(), dump_file_ext);
2820
const size_t len = strlen(base_path) + 1;
2821
my_path = (char*)os::malloc(len, mtInternal);
2822
if (my_path == nullptr) {
2823
warning("Cannot create heap dump file. Out of system memory.");
2826
strncpy(my_path, base_path, len);
2828
// Append a sequence number id for dumps following the first
2829
const size_t len = strlen(base_path) + max_digit_chars + 2; // for '.' and \0
2830
my_path = (char*)os::malloc(len, mtInternal);
2831
if (my_path == nullptr) {
2832
warning("Cannot create heap dump file. Out of system memory.");
2835
jio_snprintf(my_path, len, "%s.%d", base_path, dump_file_seq);
2837
dump_file_seq++; // increment seq number for next time we dump
2839
HeapDumper dumper(false /* no GC before heap dump */,
2840
oome /* pass along out-of-memory-error flag */);
2841
dumper.dump(my_path, tty, HeapDumpGzipLevel);