1 /*
2 * Copyright (c) 2005, 2025, 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.
5 *
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.
9 *
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).
15 *
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.
19 *
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
22 * questions.
23 *
24 */
25
26 #include "classfile/classLoaderData.inline.hpp"
27 #include "classfile/classLoaderDataGraph.hpp"
28 #include "classfile/javaClasses.inline.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/vmClasses.hpp"
31 #include "classfile/vmSymbols.hpp"
32 #include "gc/shared/gcLocker.hpp"
33 #include "gc/shared/gcVMOperations.hpp"
34 #include "gc/shared/workerThread.hpp"
35 #include "jfr/jfrEvents.hpp"
36 #include "jvm.h"
37 #include "memory/allocation.inline.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "memory/universe.hpp"
40 #include "oops/fieldStreams.inline.hpp"
41 #include "oops/flatArrayKlass.hpp"
42 #include "oops/flatArrayOop.inline.hpp"
43 #include "oops/klass.inline.hpp"
44 #include "oops/objArrayKlass.hpp"
45 #include "oops/objArrayOop.inline.hpp"
46 #include "oops/oop.inline.hpp"
47 #include "oops/typeArrayOop.inline.hpp"
48 #include "runtime/arguments.hpp"
49 #include "runtime/continuationWrapper.inline.hpp"
50 #include "runtime/fieldDescriptor.inline.hpp"
51 #include "runtime/frame.inline.hpp"
52 #include "runtime/handles.inline.hpp"
53 #include "runtime/javaCalls.hpp"
54 #include "runtime/javaThread.inline.hpp"
55 #include "runtime/jniHandles.hpp"
56 #include "runtime/os.hpp"
57 #include "runtime/threads.hpp"
58 #include "runtime/threadSMR.hpp"
59 #include "runtime/timerTrace.hpp"
60 #include "runtime/vframe.hpp"
61 #include "runtime/vmOperations.hpp"
62 #include "runtime/vmThread.hpp"
63 #include "services/heapDumper.hpp"
64 #include "services/heapDumperCompression.hpp"
65 #include "services/threadService.hpp"
66 #include "utilities/checkedCast.hpp"
67 #include "utilities/macros.hpp"
68 #include "utilities/ostream.hpp"
69 #ifdef LINUX
70 #include "os_linux.hpp"
71 #endif
72
73 /*
74 * HPROF binary format - description copied from:
75 * src/share/demo/jvmti/hprof/hprof_io.c
76 *
77 *
78 * header "JAVA PROFILE 1.0.2" (0-terminated)
79 *
80 * u4 size of identifiers. Identifiers are used to represent
81 * UTF8 strings, objects, stack traces, etc. They usually
82 * have the same size as host pointers.
83 * u4 high word
84 * u4 low word number of milliseconds since 0:00 GMT, 1/1/70
85 * [record]* a sequence of records.
86 *
87 *
88 * Record format:
89 *
90 * u1 a TAG denoting the type of the record
91 * u4 number of *microseconds* since the time stamp in the
92 * header. (wraps around in a little more than an hour)
93 * u4 number of bytes *remaining* in the record. Note that
94 * this number excludes the tag and the length field itself.
95 * [u1]* BODY of the record (a sequence of bytes)
96 *
97 *
98 * The following TAGs are supported:
99 *
100 * TAG BODY notes
101 *----------------------------------------------------------
102 * HPROF_UTF8 a UTF8-encoded name
103 *
104 * id name ID
105 * [u1]* UTF8 characters (no trailing zero)
106 *
107 * HPROF_LOAD_CLASS a newly loaded class
108 *
109 * u4 class serial number (> 0)
110 * id class object ID
111 * u4 stack trace serial number
112 * id class name ID
113 *
114 * HPROF_UNLOAD_CLASS an unloading class
115 *
116 * u4 class serial_number
117 *
118 * HPROF_FRAME a Java stack frame
119 *
120 * id stack frame ID
121 * id method name ID
122 * id method signature ID
123 * id source file name ID
124 * u4 class serial number
125 * i4 line number. >0: normal
126 * -1: unknown
127 * -2: compiled method
128 * -3: native method
129 *
130 * HPROF_TRACE a Java stack trace
131 *
132 * u4 stack trace serial number
133 * u4 thread serial number
134 * u4 number of frames
135 * [id]* stack frame IDs
136 *
137 *
138 * HPROF_ALLOC_SITES a set of heap allocation sites, obtained after GC
139 *
140 * u2 flags 0x0001: incremental vs. complete
141 * 0x0002: sorted by allocation vs. live
142 * 0x0004: whether to force a GC
143 * u4 cutoff ratio
144 * u4 total live bytes
145 * u4 total live instances
146 * u8 total bytes allocated
147 * u8 total instances allocated
148 * u4 number of sites that follow
149 * [u1 is_array: 0: normal object
150 * 2: object array
151 * 4: boolean array
152 * 5: char array
153 * 6: float array
154 * 7: double array
155 * 8: byte array
156 * 9: short array
157 * 10: int array
158 * 11: long array
159 * u4 class serial number (may be zero during startup)
160 * u4 stack trace serial number
161 * u4 number of bytes alive
162 * u4 number of instances alive
163 * u4 number of bytes allocated
164 * u4]* number of instance allocated
165 *
166 * HPROF_START_THREAD a newly started thread.
167 *
168 * u4 thread serial number (> 0)
169 * id thread object ID
170 * u4 stack trace serial number
171 * id thread name ID
172 * id thread group name ID
173 * id thread group parent name ID
174 *
175 * HPROF_END_THREAD a terminating thread.
176 *
177 * u4 thread serial number
178 *
179 * HPROF_HEAP_SUMMARY heap summary
180 *
181 * u4 total live bytes
182 * u4 total live instances
183 * u8 total bytes allocated
184 * u8 total instances allocated
185 *
186 * HPROF_HEAP_DUMP denote a heap dump
187 *
188 * [heap dump sub-records]*
189 *
190 * There are four kinds of heap dump sub-records:
191 *
192 * u1 sub-record type
193 *
194 * HPROF_GC_ROOT_UNKNOWN unknown root
195 *
196 * id object ID
197 *
198 * HPROF_GC_ROOT_THREAD_OBJ thread object
199 *
200 * id thread object ID (may be 0 for a
201 * thread newly attached through JNI)
202 * u4 thread sequence number
203 * u4 stack trace sequence number
204 *
205 * HPROF_GC_ROOT_JNI_GLOBAL JNI global ref root
206 *
207 * id object ID
208 * id JNI global ref ID
209 *
210 * HPROF_GC_ROOT_JNI_LOCAL JNI local ref
211 *
212 * id object ID
213 * u4 thread serial number
214 * u4 frame # in stack trace (-1 for empty)
215 *
216 * HPROF_GC_ROOT_JAVA_FRAME Java stack frame
217 *
218 * id object ID
219 * u4 thread serial number
220 * u4 frame # in stack trace (-1 for empty)
221 *
222 * HPROF_GC_ROOT_NATIVE_STACK Native stack
223 *
224 * id object ID
225 * u4 thread serial number
226 *
227 * HPROF_GC_ROOT_STICKY_CLASS System class
228 *
229 * id object ID
230 *
231 * HPROF_GC_ROOT_THREAD_BLOCK Reference from thread block
232 *
233 * id object ID
234 * u4 thread serial number
235 *
236 * HPROF_GC_ROOT_MONITOR_USED Busy monitor
237 *
238 * id object ID
239 *
240 * HPROF_GC_CLASS_DUMP dump of a class object
241 *
242 * id class object ID
243 * u4 stack trace serial number
244 * id super class object ID
245 * id class loader object ID
246 * id signers object ID
247 * id protection domain object ID
248 * id reserved
249 * id reserved
250 *
251 * u4 instance size (in bytes)
252 *
253 * u2 size of constant pool
254 * [u2, constant pool index,
255 * ty, type
256 * 2: object
257 * 4: boolean
258 * 5: char
259 * 6: float
260 * 7: double
261 * 8: byte
262 * 9: short
263 * 10: int
264 * 11: long
265 * vl]* and value
266 *
267 * u2 number of static fields
268 * [id, static field name,
269 * ty, type,
270 * vl]* and value
271 *
272 * u2 number of inst. fields (not inc. super)
273 * [id, instance field name,
274 * ty]* type
275 *
276 * HPROF_GC_INSTANCE_DUMP dump of a normal object
277 *
278 * id object ID
279 * u4 stack trace serial number
280 * id class object ID
281 * u4 number of bytes that follow
282 * [vl]* instance field values (class, followed
283 * by super, super's super ...)
284 *
285 * HPROF_GC_OBJ_ARRAY_DUMP dump of an object array
286 *
287 * id array object ID
288 * u4 stack trace serial number
289 * u4 number of elements
290 * id array class ID
291 * [id]* elements
292 *
293 * HPROF_GC_PRIM_ARRAY_DUMP dump of a primitive array
294 *
295 * id array object ID
296 * u4 stack trace serial number
297 * u4 number of elements
298 * u1 element type
299 * 4: boolean array
300 * 5: char array
301 * 6: float array
302 * 7: double array
303 * 8: byte array
304 * 9: short array
305 * 10: int array
306 * 11: long array
307 * [u1]* elements
308 *
309 * HPROF_CPU_SAMPLES a set of sample traces of running threads
310 *
311 * u4 total number of samples
312 * u4 # of traces
313 * [u4 # of samples
314 * u4]* stack trace serial number
315 *
316 * HPROF_CONTROL_SETTINGS the settings of on/off switches
317 *
318 * u4 0x00000001: alloc traces on/off
319 * 0x00000002: cpu sampling on/off
320 * u2 stack trace depth
321 *
322 * HPROF_FLAT_ARRAYS list of flat arrays
323 *
324 * [flat array sub-records]*
325 *
326 * HPROF_FLAT_ARRAY flat array
327 *
328 * id array object ID (dumped as HPROF_GC_PRIM_ARRAY_DUMP)
329 * id element class ID (dumped by HPROF_GC_CLASS_DUMP)
330 *
331 * HPROF_INLINED_FIELDS decribes inlined fields
332 *
333 * [class with inlined fields sub-records]*
334 *
335 * HPROF_CLASS_WITH_INLINED_FIELDS
336 *
337 * id class ID (dumped as HPROF_GC_CLASS_DUMP)
338 *
339 * u2 number of instance inlined fields (not including super)
340 * [u2, inlined field index,
341 * u2, synthetic field count,
342 * id, original field name,
343 * id]* inlined field class ID (dumped by HPROF_GC_CLASS_DUMP)
344 *
345 * When the header is "JAVA PROFILE 1.0.2" a heap dump can optionally
346 * be generated as a sequence of heap dump segments. This sequence is
347 * terminated by an end record. The additional tags allowed by format
348 * "JAVA PROFILE 1.0.2" are:
349 *
350 * HPROF_HEAP_DUMP_SEGMENT denote a heap dump segment
351 *
352 * [heap dump sub-records]*
353 * The same sub-record types allowed by HPROF_HEAP_DUMP
354 *
355 * HPROF_HEAP_DUMP_END denotes the end of a heap dump
356 *
357 */
358
359
360 // HPROF tags
361
362 enum hprofTag : u1 {
363 // top-level records
364 HPROF_UTF8 = 0x01,
365 HPROF_LOAD_CLASS = 0x02,
366 HPROF_UNLOAD_CLASS = 0x03,
367 HPROF_FRAME = 0x04,
368 HPROF_TRACE = 0x05,
369 HPROF_ALLOC_SITES = 0x06,
370 HPROF_HEAP_SUMMARY = 0x07,
371 HPROF_START_THREAD = 0x0A,
372 HPROF_END_THREAD = 0x0B,
373 HPROF_HEAP_DUMP = 0x0C,
374 HPROF_CPU_SAMPLES = 0x0D,
375 HPROF_CONTROL_SETTINGS = 0x0E,
376
377 // 1.0.2 record types
378 HPROF_HEAP_DUMP_SEGMENT = 0x1C,
379 HPROF_HEAP_DUMP_END = 0x2C,
380
381 // inlined object support
382 HPROF_FLAT_ARRAYS = 0x12,
383 HPROF_INLINED_FIELDS = 0x13,
384 // inlined object subrecords
385 HPROF_FLAT_ARRAY = 0x01,
386 HPROF_CLASS_WITH_INLINED_FIELDS = 0x01,
387
388 // field types
389 HPROF_ARRAY_OBJECT = 0x01,
390 HPROF_NORMAL_OBJECT = 0x02,
391 HPROF_BOOLEAN = 0x04,
392 HPROF_CHAR = 0x05,
393 HPROF_FLOAT = 0x06,
394 HPROF_DOUBLE = 0x07,
395 HPROF_BYTE = 0x08,
396 HPROF_SHORT = 0x09,
397 HPROF_INT = 0x0A,
398 HPROF_LONG = 0x0B,
399
400 // data-dump sub-records
401 HPROF_GC_ROOT_UNKNOWN = 0xFF,
402 HPROF_GC_ROOT_JNI_GLOBAL = 0x01,
403 HPROF_GC_ROOT_JNI_LOCAL = 0x02,
404 HPROF_GC_ROOT_JAVA_FRAME = 0x03,
405 HPROF_GC_ROOT_NATIVE_STACK = 0x04,
406 HPROF_GC_ROOT_STICKY_CLASS = 0x05,
407 HPROF_GC_ROOT_THREAD_BLOCK = 0x06,
408 HPROF_GC_ROOT_MONITOR_USED = 0x07,
409 HPROF_GC_ROOT_THREAD_OBJ = 0x08,
410 HPROF_GC_CLASS_DUMP = 0x20,
411 HPROF_GC_INSTANCE_DUMP = 0x21,
412 HPROF_GC_OBJ_ARRAY_DUMP = 0x22,
413 HPROF_GC_PRIM_ARRAY_DUMP = 0x23
414 };
415
416 // Default stack trace ID (used for dummy HPROF_TRACE record)
417 enum {
418 STACK_TRACE_ID = 1,
419 INITIAL_CLASS_COUNT = 200
420 };
421
422
423 class AbstractDumpWriter;
424
425 class InlinedObjects {
426
427 struct ClassInlinedFields {
428 const Klass *klass;
429 uintx base_index; // base index of the inlined field names (1st field has index base_index+1).
430 ClassInlinedFields(const Klass *klass = nullptr, uintx base_index = 0) : klass(klass), base_index(base_index) {}
431
432 // For GrowableArray::find_sorted().
433 static int compare(const ClassInlinedFields& a, const ClassInlinedFields& b) {
434 return a.klass - b.klass;
435 }
436 // For GrowableArray::sort().
437 static int compare(ClassInlinedFields* a, ClassInlinedFields* b) {
438 return compare(*a, *b);
439 }
440 };
441
442 uintx _min_string_id;
443 uintx _max_string_id;
444
445 GrowableArray<ClassInlinedFields> *_inlined_field_map;
446
447 // counters for classes with inlined fields and for the fields
448 int _classes_count;
449 int _inlined_fields_count;
450
451 static InlinedObjects *_instance;
452
453 static void inlined_field_names_callback(InlinedObjects* _this, const Klass *klass, uintx base_index, int count);
454
455 GrowableArray<oop> *_flat_arrays;
456
457 public:
458 InlinedObjects()
459 : _min_string_id(0), _max_string_id(0),
460 _inlined_field_map(nullptr),
461 _classes_count(0), _inlined_fields_count(0),
462 _flat_arrays(nullptr) {
463 }
464
465 static InlinedObjects* get_instance() {
466 return _instance;
467 }
468
469 void init();
470 void release();
471
472 void dump_inlined_field_names(AbstractDumpWriter *writer);
473
474 uintx get_base_index_for(Klass* k);
475 uintx get_next_string_id(uintx id);
476
477 void dump_classed_with_inlined_fields(AbstractDumpWriter* writer);
478
479 void add_flat_array(oop array);
480 void dump_flat_arrays(AbstractDumpWriter* writer);
481
482 };
483
484 InlinedObjects *InlinedObjects::_instance = nullptr;
485
486
487 // Supports I/O operations for a dump
488 // Base class for dump and parallel dump
489 class AbstractDumpWriter : public CHeapObj<mtInternal> {
490 protected:
491 enum {
492 io_buffer_max_size = 1*M,
493 dump_segment_header_size = 9
494 };
495
496 char* _buffer; // internal buffer
497 size_t _size;
498 size_t _pos;
499
500 bool _in_dump_segment; // Are we currently in a dump segment?
501 bool _is_huge_sub_record; // Are we writing a sub-record larger than the buffer size?
502 DEBUG_ONLY(size_t _sub_record_left;) // The bytes not written for the current sub-record.
503 DEBUG_ONLY(bool _sub_record_ended;) // True if we have called the end_sub_record().
504
505 char* buffer() const { return _buffer; }
506 size_t buffer_size() const { return _size; }
507 void set_position(size_t pos) { _pos = pos; }
508
509 // Can be called if we have enough room in the buffer.
510 void write_fast(const void* s, size_t len);
511
512 // Returns true if we have enough room in the buffer for 'len' bytes.
513 bool can_write_fast(size_t len);
514
515 void write_address(address a);
516
517 public:
518 AbstractDumpWriter() :
519 _buffer(nullptr),
520 _size(io_buffer_max_size),
521 _pos(0),
522 _in_dump_segment(false) { }
523
524 // Total number of bytes written to the disk
525 virtual julong bytes_written() const = 0;
526 // Return non-null if error occurred
527 virtual char const* error() const = 0;
528
529 size_t position() const { return _pos; }
530 // writer functions
531 virtual void write_raw(const void* s, size_t len);
532 void write_u1(u1 x);
533 void write_u2(u2 x);
534 void write_u4(u4 x);
535 void write_u8(u8 x);
536 void write_objectID(oop o);
537 void write_rootID(oop* p);
538 void write_symbolID(Symbol* o);
539 void write_classID(Klass* k);
540 void write_id(u4 x);
541
542 // Start a new sub-record. Starts a new heap dump segment if needed.
543 void start_sub_record(u1 tag, u4 len);
544 // Ends the current sub-record.
545 void end_sub_record();
546 // Finishes the current dump segment if not already finished.
547 void finish_dump_segment();
548 // Flush internal buffer to persistent storage
549 virtual void flush() = 0;
550 };
551
552 void AbstractDumpWriter::write_fast(const void* s, size_t len) {
553 assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
554 assert(buffer_size() - position() >= len, "Must fit");
555 DEBUG_ONLY(_sub_record_left -= len);
556 memcpy(buffer() + position(), s, len);
557 set_position(position() + len);
558 }
559
560 bool AbstractDumpWriter::can_write_fast(size_t len) {
561 return buffer_size() - position() >= len;
562 }
563
564 // write raw bytes
565 void AbstractDumpWriter::write_raw(const void* s, size_t len) {
566 assert(!_in_dump_segment || (_sub_record_left >= len), "sub-record too large");
567 DEBUG_ONLY(_sub_record_left -= len);
568
569 // flush buffer to make room.
570 while (len > buffer_size() - position()) {
571 assert(!_in_dump_segment || _is_huge_sub_record,
572 "Cannot overflow in non-huge sub-record.");
573 size_t to_write = buffer_size() - position();
574 memcpy(buffer() + position(), s, to_write);
575 s = (void*) ((char*) s + to_write);
576 len -= to_write;
577 set_position(position() + to_write);
578 flush();
579 }
580
581 memcpy(buffer() + position(), s, len);
582 set_position(position() + len);
583 }
584
585 // Makes sure we inline the fast write into the write_u* functions. This is a big speedup.
586 #define WRITE_KNOWN_TYPE(p, len) do { if (can_write_fast((len))) write_fast((p), (len)); \
587 else write_raw((p), (len)); } while (0)
588
589 void AbstractDumpWriter::write_u1(u1 x) {
590 WRITE_KNOWN_TYPE(&x, 1);
591 }
592
593 void AbstractDumpWriter::write_u2(u2 x) {
594 u2 v;
595 Bytes::put_Java_u2((address)&v, x);
596 WRITE_KNOWN_TYPE(&v, 2);
597 }
598
599 void AbstractDumpWriter::write_u4(u4 x) {
600 u4 v;
601 Bytes::put_Java_u4((address)&v, x);
602 WRITE_KNOWN_TYPE(&v, 4);
603 }
604
605 void AbstractDumpWriter::write_u8(u8 x) {
606 u8 v;
607 Bytes::put_Java_u8((address)&v, x);
608 WRITE_KNOWN_TYPE(&v, 8);
609 }
610
611 void AbstractDumpWriter::write_address(address a) {
612 #ifdef _LP64
613 write_u8((u8)a);
614 #else
615 write_u4((u4)a);
616 #endif
617 }
618
619 void AbstractDumpWriter::write_objectID(oop o) {
620 write_address(cast_from_oop<address>(o));
621 }
622
623 void AbstractDumpWriter::write_rootID(oop* p) {
624 write_address((address)p);
625 }
626
627 void AbstractDumpWriter::write_symbolID(Symbol* s) {
628 write_address((address)((uintptr_t)s));
629 }
630
631 void AbstractDumpWriter::write_id(u4 x) {
632 #ifdef _LP64
633 write_u8((u8) x);
634 #else
635 write_u4(x);
636 #endif
637 }
638
639 // We use java mirror as the class ID
640 void AbstractDumpWriter::write_classID(Klass* k) {
641 write_objectID(k->java_mirror());
642 }
643
644 void AbstractDumpWriter::finish_dump_segment() {
645 if (_in_dump_segment) {
646 assert(_sub_record_left == 0, "Last sub-record not written completely");
647 assert(_sub_record_ended, "sub-record must have ended");
648
649 // Fix up the dump segment length if we haven't written a huge sub-record last
650 // (in which case the segment length was already set to the correct value initially).
651 if (!_is_huge_sub_record) {
652 assert(position() > dump_segment_header_size, "Dump segment should have some content");
653 Bytes::put_Java_u4((address) (buffer() + 5),
654 (u4) (position() - dump_segment_header_size));
655 } else {
656 // Finish process huge sub record
657 // Set _is_huge_sub_record to false so the parallel dump writer can flush data to file.
658 _is_huge_sub_record = false;
659 }
660
661 _in_dump_segment = false;
662 flush();
663 }
664 }
665
666 void AbstractDumpWriter::start_sub_record(u1 tag, u4 len) {
667 if (!_in_dump_segment) {
668 if (position() > 0) {
669 flush();
670 }
671
672 assert(position() == 0 && buffer_size() > dump_segment_header_size, "Must be at the start");
673
674 write_u1(HPROF_HEAP_DUMP_SEGMENT);
675 write_u4(0); // timestamp
676 // Will be fixed up later if we add more sub-records. If this is a huge sub-record,
677 // this is already the correct length, since we don't add more sub-records.
678 write_u4(len);
679 assert(Bytes::get_Java_u4((address)(buffer() + 5)) == len, "Inconsistent size!");
680 _in_dump_segment = true;
681 _is_huge_sub_record = len > buffer_size() - dump_segment_header_size;
682 } else if (_is_huge_sub_record || (len > buffer_size() - position())) {
683 // This object will not fit in completely or the last sub-record was huge.
684 // Finish the current segment and try again.
685 finish_dump_segment();
686 start_sub_record(tag, len);
687
688 return;
689 }
690
691 DEBUG_ONLY(_sub_record_left = len);
692 DEBUG_ONLY(_sub_record_ended = false);
693
694 write_u1(tag);
695 }
696
697 void AbstractDumpWriter::end_sub_record() {
698 assert(_in_dump_segment, "must be in dump segment");
699 assert(_sub_record_left == 0, "sub-record not written completely");
700 assert(!_sub_record_ended, "Must not have ended yet");
701 DEBUG_ONLY(_sub_record_ended = true);
702 }
703
704 // Supports I/O operations for a dump
705
706 class DumpWriter : public AbstractDumpWriter {
707 private:
708 FileWriter* _writer;
709 AbstractCompressor* _compressor;
710 size_t _bytes_written;
711 char* _error;
712 // Compression support
713 char* _out_buffer;
714 size_t _out_size;
715 size_t _out_pos;
716 char* _tmp_buffer;
717 size_t _tmp_size;
718
719 private:
720 void do_compress();
721
722 public:
723 DumpWriter(const char* path, bool overwrite, AbstractCompressor* compressor);
724 ~DumpWriter();
725 julong bytes_written() const override { return (julong) _bytes_written; }
726 char const* error() const override { return _error; }
727 void set_error(const char* error) { _error = (char*)error; }
728 bool has_error() const { return _error != nullptr; }
729 const char* get_file_path() const { return _writer->get_file_path(); }
730 AbstractCompressor* compressor() { return _compressor; }
731 bool is_overwrite() const { return _writer->is_overwrite(); }
732
733 void flush() override;
734
735 private:
736 // internals for DumpMerger
737 friend class DumpMerger;
738 void set_bytes_written(julong bytes_written) { _bytes_written = bytes_written; }
739 int get_fd() const { return _writer->get_fd(); }
740 void set_compressor(AbstractCompressor* p) { _compressor = p; }
741 };
742
743 DumpWriter::DumpWriter(const char* path, bool overwrite, AbstractCompressor* compressor) :
744 AbstractDumpWriter(),
745 _writer(new (std::nothrow) FileWriter(path, overwrite)),
746 _compressor(compressor),
747 _bytes_written(0),
748 _error(nullptr),
749 _out_buffer(nullptr),
750 _out_size(0),
751 _out_pos(0),
752 _tmp_buffer(nullptr),
753 _tmp_size(0) {
754 _error = (char*)_writer->open_writer();
755 if (_error == nullptr) {
756 _buffer = (char*)os::malloc(io_buffer_max_size, mtInternal);
757 if (compressor != nullptr) {
758 _error = (char*)_compressor->init(io_buffer_max_size, &_out_size, &_tmp_size);
759 if (_error == nullptr) {
760 if (_out_size > 0) {
761 _out_buffer = (char*)os::malloc(_out_size, mtInternal);
762 }
763 if (_tmp_size > 0) {
764 _tmp_buffer = (char*)os::malloc(_tmp_size, mtInternal);
765 }
766 }
767 }
768 }
769 // initialize internal buffer
770 _pos = 0;
771 _size = io_buffer_max_size;
772 }
773
774 DumpWriter::~DumpWriter(){
775 if (_buffer != nullptr) {
776 os::free(_buffer);
777 }
778 if (_out_buffer != nullptr) {
779 os::free(_out_buffer);
780 }
781 if (_tmp_buffer != nullptr) {
782 os::free(_tmp_buffer);
783 }
784 if (_writer != nullptr) {
785 delete _writer;
786 }
787 _bytes_written = -1;
788 }
789
790 // flush any buffered bytes to the file
791 void DumpWriter::flush() {
792 if (_pos <= 0) {
793 return;
794 }
795 if (has_error()) {
796 _pos = 0;
797 return;
798 }
799 char* result = nullptr;
800 if (_compressor == nullptr) {
801 result = (char*)_writer->write_buf(_buffer, _pos);
802 _bytes_written += _pos;
803 } else {
804 do_compress();
805 if (!has_error()) {
806 result = (char*)_writer->write_buf(_out_buffer, _out_pos);
807 _bytes_written += _out_pos;
808 }
809 }
810 _pos = 0; // reset pos to make internal buffer available
811
812 if (result != nullptr) {
813 set_error(result);
814 }
815 }
816
817 void DumpWriter::do_compress() {
818 const char* msg = _compressor->compress(_buffer, _pos, _out_buffer, _out_size,
819 _tmp_buffer, _tmp_size, &_out_pos);
820
821 if (msg != nullptr) {
822 set_error(msg);
823 }
824 }
825
826 class DumperClassCacheTable;
827 class DumperClassCacheTableEntry;
828
829 // Support class with a collection of functions used when dumping the heap
830 class DumperSupport : AllStatic {
831 public:
832
833 // write a header of the given type
834 static void write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len);
835
836 // returns hprof tag for the given type signature
837 static hprofTag sig2tag(Symbol* sig);
838 // returns hprof tag for the given basic type
839 static hprofTag type2tag(BasicType type);
840 // Returns the size of the data to write.
841 static u4 sig2size(Symbol* sig);
842
843 // calculates the total size of the all fields of the given class.
844 static u4 instance_size(InstanceKlass* ik, DumperClassCacheTableEntry* class_cache_entry = nullptr);
845
846 // dump a jfloat
847 static void dump_float(AbstractDumpWriter* writer, jfloat f);
848 // dump a jdouble
849 static void dump_double(AbstractDumpWriter* writer, jdouble d);
850 // dumps the raw value of the given field
851 static void dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset);
852 // returns the size of the static fields; also counts the static fields
853 static u4 get_static_fields_size(InstanceKlass* ik, u2& field_count);
854 // dumps static fields of the given class
855 static void dump_static_fields(AbstractDumpWriter* writer, Klass* k);
856 // dump the raw values of the instance fields of the given identity or inlined object;
857 // for identity objects offset is 0 and 'klass' is o->klass(),
858 // for inlined objects offset is the offset in the holder object, 'klass' is inlined object class
859 static void dump_instance_fields(AbstractDumpWriter* writer, oop o, int offset, DumperClassCacheTable* class_cache, DumperClassCacheTableEntry* class_cache_entry);
860 // dump the raw values of the instance fields of the given inlined object;
861 // dump_instance_fields wrapper for inlined objects
862 static void dump_inlined_object_fields(AbstractDumpWriter* writer, oop o, int offset, DumperClassCacheTable* class_cache, DumperClassCacheTableEntry* class_cache_entry);
863
864 // get the count of the instance fields for a given class
865 static u2 get_instance_fields_count(InstanceKlass* ik);
866 // dumps the definition of the instance fields for a given class
867 static void dump_instance_field_descriptors(AbstractDumpWriter* writer, InstanceKlass* k, uintx *inlined_fields_index = nullptr);
868 // creates HPROF_GC_INSTANCE_DUMP record for the given object
869 static void dump_instance(AbstractDumpWriter* writer, oop o, DumperClassCacheTable* class_cache);
870 // creates HPROF_GC_CLASS_DUMP record for the given instance class
871 static void dump_instance_class(AbstractDumpWriter* writer, InstanceKlass* ik);
872 // creates HPROF_GC_CLASS_DUMP record for a given array class
873 static void dump_array_class(AbstractDumpWriter* writer, Klass* k);
874
875 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
876 static void dump_object_array(AbstractDumpWriter* writer, objArrayOop array);
877 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given flat array
878 static void dump_flat_array(AbstractDumpWriter* writer, flatArrayOop array, DumperClassCacheTable* class_cache);
879 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
880 static void dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array);
881 // create HPROF_FRAME record for the given method and bci
882 static void dump_stack_frame(AbstractDumpWriter* writer, int frame_serial_num, int class_serial_num, Method* m, int bci);
883
884 // check if we need to truncate an array
885 static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size);
886 // extended version to dump flat arrays as primitive arrays;
887 // type_size specifies size of the inlined objects.
888 static int calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, int type_size, short header_size);
889
890 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
891 static void end_of_dump(AbstractDumpWriter* writer);
892
893 static oop mask_dormant_archived_object(oop o, oop ref_obj) {
894 if (o != nullptr && o->klass()->java_mirror_no_keepalive() == nullptr) {
895 // Ignore this object since the corresponding java mirror is not loaded.
896 // Might be a dormant archive object.
897 report_dormant_archived_object(o, ref_obj);
898 return nullptr;
899 } else {
900 return o;
901 }
902 }
903
904 // helper methods for inlined fields.
905 static bool is_inlined_field(const fieldDescriptor& fld) {
906 return fld.is_flat();
907 }
908 static InlineKlass* get_inlined_field_klass(const fieldDescriptor& fld) {
909 assert(is_inlined_field(fld), "must be inlined field");
910 InstanceKlass* holder_klass = fld.field_holder();
911 return InlineKlass::cast(holder_klass->get_inline_type_field_klass(fld.index()));
912 }
913
914 static void report_dormant_archived_object(oop o, oop ref_obj) {
915 if (log_is_enabled(Trace, aot, heap)) {
916 ResourceMark rm;
917 if (ref_obj != nullptr) {
918 log_trace(aot, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s) referenced by " INTPTR_FORMAT " (%s)",
919 p2i(o), o->klass()->external_name(),
920 p2i(ref_obj), ref_obj->klass()->external_name());
921 } else {
922 log_trace(aot, heap)("skipped dormant archived object " INTPTR_FORMAT " (%s)",
923 p2i(o), o->klass()->external_name());
924 }
925 }
926 }
927 };
928
929 // Hash table of klasses to the klass metadata. This should greatly improve the
930 // hash dumping performance. This hash table is supposed to be used by a single
931 // thread only.
932 //
933 class DumperClassCacheTableEntry : public CHeapObj<mtServiceability> {
934 friend class DumperClassCacheTable;
935 private:
936 GrowableArray<char> _sigs_start;
937 GrowableArray<int> _offsets;
938 GrowableArray<InlineKlass*> _inline_klasses;
939 u4 _instance_size;
940 int _entries;
941
942 public:
943 DumperClassCacheTableEntry() : _instance_size(0), _entries(0) {};
944
945 int field_count() { return _entries; }
946 char sig_start(int field_idx) { return _sigs_start.at(field_idx); }
947 void push_sig_start_inlined() { _sigs_start.push('Q'); }
948 bool is_inlined(int field_idx){ return _sigs_start.at(field_idx) == 'Q'; }
949 InlineKlass* inline_klass(int field_idx) { assert(is_inlined(field_idx), "Not inlined"); return _inline_klasses.at(field_idx); }
950 int offset(int field_idx) { return _offsets.at(field_idx); }
951 u4 instance_size() { return _instance_size; }
952 };
953
954 class DumperClassCacheTable {
955 private:
956 // HashTable SIZE is specified at compile time so we
957 // use 1031 which is the first prime after 1024.
958 static constexpr size_t TABLE_SIZE = 1031;
959
960 // Maintain the cache for N classes. This limits memory footprint
961 // impact, regardless of how many classes we have in the dump.
962 // This also improves look up performance by keeping the statically
963 // sized table from overloading.
964 static constexpr int CACHE_TOP = 256;
965
966 typedef HashTable<InstanceKlass*, DumperClassCacheTableEntry*,
967 TABLE_SIZE, AnyObj::C_HEAP, mtServiceability> PtrTable;
968 PtrTable* _ptrs;
969
970 // Single-slot cache to handle the major case of objects of the same
971 // class back-to-back, e.g. from T[].
972 InstanceKlass* _last_ik;
973 DumperClassCacheTableEntry* _last_entry;
974
975 void unlink_all(PtrTable* table) {
976 class CleanupEntry: StackObj {
977 public:
978 bool do_entry(InstanceKlass*& key, DumperClassCacheTableEntry*& entry) {
979 delete entry;
980 return true;
981 }
982 } cleanup;
983 table->unlink(&cleanup);
984 }
985
986 public:
987 DumperClassCacheTableEntry* lookup_or_create(InstanceKlass* ik) {
988 if (_last_ik == ik) {
989 return _last_entry;
990 }
991
992 DumperClassCacheTableEntry* entry;
993 DumperClassCacheTableEntry** from_cache = _ptrs->get(ik);
994 if (from_cache == nullptr) {
995 entry = new DumperClassCacheTableEntry();
996 for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
997 if (!fld.access_flags().is_static()) {
998 InlineKlass* inlineKlass = nullptr;
999 if (DumperSupport::is_inlined_field(fld.field_descriptor())) {
1000 inlineKlass = DumperSupport::get_inlined_field_klass(fld.field_descriptor());
1001 entry->push_sig_start_inlined();
1002 entry->_instance_size += DumperSupport::instance_size(inlineKlass);
1003 } else {
1004 Symbol* sig = fld.signature();
1005 entry->_sigs_start.push(sig->char_at(0));
1006 entry->_instance_size += DumperSupport::sig2size(sig);
1007 }
1008 entry->_inline_klasses.push(inlineKlass);
1009 entry->_offsets.push(fld.offset());
1010 entry->_entries++;
1011 }
1012 }
1013
1014 if (_ptrs->number_of_entries() >= CACHE_TOP) {
1015 // We do not track the individual hit rates for table entries.
1016 // Purge the entire table, and let the cache catch up with new
1017 // distribution.
1018 unlink_all(_ptrs);
1019 }
1020
1021 _ptrs->put(ik, entry);
1022 } else {
1023 entry = *from_cache;
1024 }
1025
1026 // Remember for single-slot cache.
1027 _last_ik = ik;
1028 _last_entry = entry;
1029
1030 return entry;
1031 }
1032
1033 DumperClassCacheTable() : _ptrs(new (mtServiceability) PtrTable), _last_ik(nullptr), _last_entry(nullptr) {}
1034
1035 ~DumperClassCacheTable() {
1036 unlink_all(_ptrs);
1037 delete _ptrs;
1038 }
1039 };
1040
1041 // write a header of the given type
1042 void DumperSupport:: write_header(AbstractDumpWriter* writer, hprofTag tag, u4 len) {
1043 writer->write_u1(tag);
1044 writer->write_u4(0); // current ticks
1045 writer->write_u4(len);
1046 }
1047
1048 // returns hprof tag for the given type signature
1049 hprofTag DumperSupport::sig2tag(Symbol* sig) {
1050 switch (sig->char_at(0)) {
1051 case JVM_SIGNATURE_CLASS : return HPROF_NORMAL_OBJECT;
1052 case JVM_SIGNATURE_ARRAY : return HPROF_NORMAL_OBJECT;
1053 case JVM_SIGNATURE_BYTE : return HPROF_BYTE;
1054 case JVM_SIGNATURE_CHAR : return HPROF_CHAR;
1055 case JVM_SIGNATURE_FLOAT : return HPROF_FLOAT;
1056 case JVM_SIGNATURE_DOUBLE : return HPROF_DOUBLE;
1057 case JVM_SIGNATURE_INT : return HPROF_INT;
1058 case JVM_SIGNATURE_LONG : return HPROF_LONG;
1059 case JVM_SIGNATURE_SHORT : return HPROF_SHORT;
1060 case JVM_SIGNATURE_BOOLEAN : return HPROF_BOOLEAN;
1061 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
1062 }
1063 }
1064
1065 hprofTag DumperSupport::type2tag(BasicType type) {
1066 switch (type) {
1067 case T_BYTE : return HPROF_BYTE;
1068 case T_CHAR : return HPROF_CHAR;
1069 case T_FLOAT : return HPROF_FLOAT;
1070 case T_DOUBLE : return HPROF_DOUBLE;
1071 case T_INT : return HPROF_INT;
1072 case T_LONG : return HPROF_LONG;
1073 case T_SHORT : return HPROF_SHORT;
1074 case T_BOOLEAN : return HPROF_BOOLEAN;
1075 default : ShouldNotReachHere(); /* to shut up compiler */ return HPROF_BYTE;
1076 }
1077 }
1078
1079 u4 DumperSupport::sig2size(Symbol* sig) {
1080 switch (sig->char_at(0)) {
1081 case JVM_SIGNATURE_CLASS:
1082 case JVM_SIGNATURE_ARRAY: return sizeof(address);
1083 case JVM_SIGNATURE_BOOLEAN:
1084 case JVM_SIGNATURE_BYTE: return 1;
1085 case JVM_SIGNATURE_SHORT:
1086 case JVM_SIGNATURE_CHAR: return 2;
1087 case JVM_SIGNATURE_INT:
1088 case JVM_SIGNATURE_FLOAT: return 4;
1089 case JVM_SIGNATURE_LONG:
1090 case JVM_SIGNATURE_DOUBLE: return 8;
1091 default: ShouldNotReachHere(); /* to shut up compiler */ return 0;
1092 }
1093 }
1094
1095 template<typename T, typename F> T bit_cast(F from) { // replace with the real thing when we can use c++20
1096 T to;
1097 static_assert(sizeof(to) == sizeof(from), "must be of the same size");
1098 memcpy(&to, &from, sizeof(to));
1099 return to;
1100 }
1101
1102 // dump a jfloat
1103 void DumperSupport::dump_float(AbstractDumpWriter* writer, jfloat f) {
1104 if (g_isnan(f)) {
1105 writer->write_u4(0x7fc00000); // collapsing NaNs
1106 } else {
1107 writer->write_u4(bit_cast<u4>(f));
1108 }
1109 }
1110
1111 // dump a jdouble
1112 void DumperSupport::dump_double(AbstractDumpWriter* writer, jdouble d) {
1113 if (g_isnan(d)) {
1114 writer->write_u8(0x7ff80000ull << 32); // collapsing NaNs
1115 } else {
1116 writer->write_u8(bit_cast<u8>(d));
1117 }
1118 }
1119
1120
1121 // dumps the raw value of the given field
1122 void DumperSupport::dump_field_value(AbstractDumpWriter* writer, char type, oop obj, int offset) {
1123 switch (type) {
1124 case JVM_SIGNATURE_CLASS :
1125 case JVM_SIGNATURE_ARRAY : {
1126 oop o = obj->obj_field_access<ON_UNKNOWN_OOP_REF | AS_NO_KEEPALIVE>(offset);
1127 o = mask_dormant_archived_object(o, obj);
1128 assert(oopDesc::is_oop_or_null(o), "Expected an oop or nullptr at " PTR_FORMAT, p2i(o));
1129 writer->write_objectID(o);
1130 break;
1131 }
1132 case JVM_SIGNATURE_BYTE : {
1133 jbyte b = obj->byte_field(offset);
1134 writer->write_u1(b);
1135 break;
1136 }
1137 case JVM_SIGNATURE_CHAR : {
1138 jchar c = obj->char_field(offset);
1139 writer->write_u2(c);
1140 break;
1141 }
1142 case JVM_SIGNATURE_SHORT : {
1143 jshort s = obj->short_field(offset);
1144 writer->write_u2(s);
1145 break;
1146 }
1147 case JVM_SIGNATURE_FLOAT : {
1148 jfloat f = obj->float_field(offset);
1149 dump_float(writer, f);
1150 break;
1151 }
1152 case JVM_SIGNATURE_DOUBLE : {
1153 jdouble d = obj->double_field(offset);
1154 dump_double(writer, d);
1155 break;
1156 }
1157 case JVM_SIGNATURE_INT : {
1158 jint i = obj->int_field(offset);
1159 writer->write_u4(i);
1160 break;
1161 }
1162 case JVM_SIGNATURE_LONG : {
1163 jlong l = obj->long_field(offset);
1164 writer->write_u8(l);
1165 break;
1166 }
1167 case JVM_SIGNATURE_BOOLEAN : {
1168 jboolean b = obj->bool_field(offset);
1169 writer->write_u1(b);
1170 break;
1171 }
1172 default : {
1173 ShouldNotReachHere();
1174 break;
1175 }
1176 }
1177 }
1178
1179 // calculates the total size of the all fields of the given class.
1180 u4 DumperSupport::instance_size(InstanceKlass* ik, DumperClassCacheTableEntry* class_cache_entry) {
1181 if (class_cache_entry != nullptr) {
1182 return class_cache_entry->instance_size();
1183 } else {
1184 u4 size = 0;
1185 for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
1186 if (!fld.access_flags().is_static()) {
1187 if (is_inlined_field(fld.field_descriptor())) {
1188 size += instance_size(get_inlined_field_klass(fld.field_descriptor()));
1189 } else {
1190 size += sig2size(fld.signature());
1191 }
1192 }
1193 }
1194 return size;
1195 }
1196 }
1197
1198 u4 DumperSupport::get_static_fields_size(InstanceKlass* ik, u2& field_count) {
1199 field_count = 0;
1200 u4 size = 0;
1201
1202 for (JavaFieldStream fldc(ik); !fldc.done(); fldc.next()) {
1203 if (fldc.access_flags().is_static()) {
1204 assert(!is_inlined_field(fldc.field_descriptor()), "static fields cannot be inlined");
1205
1206 field_count++;
1207 size += sig2size(fldc.signature());
1208 }
1209 }
1210
1211 // Add in resolved_references which is referenced by the cpCache
1212 // The resolved_references is an array per InstanceKlass holding the
1213 // strings and other oops resolved from the constant pool.
1214 oop resolved_references = ik->constants()->resolved_references_or_null();
1215 if (resolved_references != nullptr) {
1216 field_count++;
1217 size += sizeof(address);
1218
1219 // Add in the resolved_references of the used previous versions of the class
1220 // in the case of RedefineClasses
1221 InstanceKlass* prev = ik->previous_versions();
1222 while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1223 field_count++;
1224 size += sizeof(address);
1225 prev = prev->previous_versions();
1226 }
1227 }
1228
1229 // Also provide a pointer to the init_lock if present, so there aren't unreferenced int[0]
1230 // arrays.
1231 oop init_lock = ik->init_lock();
1232 if (init_lock != nullptr) {
1233 field_count++;
1234 size += sizeof(address);
1235 }
1236
1237 // We write the value itself plus a name and a one byte type tag per field.
1238 return checked_cast<u4>(size + field_count * (sizeof(address) + 1));
1239 }
1240
1241 // dumps static fields of the given class
1242 void DumperSupport::dump_static_fields(AbstractDumpWriter* writer, Klass* k) {
1243 InstanceKlass* ik = InstanceKlass::cast(k);
1244
1245 // dump the field descriptors and raw values
1246 for (JavaFieldStream fld(ik); !fld.done(); fld.next()) {
1247 if (fld.access_flags().is_static()) {
1248 assert(!is_inlined_field(fld.field_descriptor()), "static fields cannot be inlined");
1249
1250 Symbol* sig = fld.signature();
1251
1252 writer->write_symbolID(fld.name()); // name
1253 writer->write_u1(sig2tag(sig)); // type
1254
1255 // value
1256 dump_field_value(writer, sig->char_at(0), ik->java_mirror(), fld.offset());
1257 }
1258 }
1259
1260 // Add resolved_references for each class that has them
1261 oop resolved_references = ik->constants()->resolved_references_or_null();
1262 if (resolved_references != nullptr) {
1263 writer->write_symbolID(vmSymbols::resolved_references_name()); // name
1264 writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1265 writer->write_objectID(resolved_references);
1266
1267 // Also write any previous versions
1268 InstanceKlass* prev = ik->previous_versions();
1269 while (prev != nullptr && prev->constants()->resolved_references_or_null() != nullptr) {
1270 writer->write_symbolID(vmSymbols::resolved_references_name()); // name
1271 writer->write_u1(sig2tag(vmSymbols::object_array_signature())); // type
1272 writer->write_objectID(prev->constants()->resolved_references());
1273 prev = prev->previous_versions();
1274 }
1275 }
1276
1277 // Add init lock to the end if the class is not yet initialized
1278 oop init_lock = ik->init_lock();
1279 if (init_lock != nullptr) {
1280 writer->write_symbolID(vmSymbols::init_lock_name()); // name
1281 writer->write_u1(sig2tag(vmSymbols::int_array_signature())); // type
1282 writer->write_objectID(init_lock);
1283 }
1284 }
1285
1286 // dump the raw values of the instance fields of the given identity or inlined object;
1287 // for identity objects offset is 0 and 'klass' is o->klass(),
1288 // for inlined objects offset is the offset in the holder object, 'klass' is inlined object class.
1289 void DumperSupport::dump_instance_fields(AbstractDumpWriter* writer, oop o, int offset, DumperClassCacheTable* class_cache, DumperClassCacheTableEntry* class_cache_entry) {
1290 assert(class_cache_entry != nullptr, "Pre-condition: must be provided");
1291 for (int idx = 0; idx < class_cache_entry->field_count(); idx++) {
1292 if (class_cache_entry->is_inlined(idx)) {
1293 InlineKlass* field_klass = class_cache_entry->inline_klass(idx);
1294 int fields_offset = offset + (class_cache_entry->offset(idx) - field_klass->payload_offset());
1295 DumperClassCacheTableEntry* inline_class_cache_entry = class_cache->lookup_or_create(field_klass);
1296 dump_inlined_object_fields(writer, o, fields_offset, class_cache, inline_class_cache_entry);
1297 } else {
1298 dump_field_value(writer, class_cache_entry->sig_start(idx), o, class_cache_entry->offset(idx));
1299 }
1300 }
1301 }
1302
1303 void DumperSupport::dump_inlined_object_fields(AbstractDumpWriter* writer, oop o, int offset, DumperClassCacheTable* class_cache, DumperClassCacheTableEntry* class_cache_entry) {
1304 // the object is inlined, so all its fields are stored without headers.
1305 dump_instance_fields(writer, o, offset, class_cache, class_cache_entry);
1306 }
1307
1308 // gets the count of the instance fields for a given class
1309 u2 DumperSupport::get_instance_fields_count(InstanceKlass* ik) {
1310 u2 field_count = 0;
1311
1312 for (JavaFieldStream fldc(ik); !fldc.done(); fldc.next()) {
1313 if (!fldc.access_flags().is_static()) {
1314 if (is_inlined_field(fldc.field_descriptor())) {
1315 // add "synthetic" fields for inlined fields.
1316 field_count += get_instance_fields_count(get_inlined_field_klass(fldc.field_descriptor()));
1317 } else {
1318 field_count++;
1319 }
1320 }
1321 }
1322
1323 return field_count;
1324 }
1325
1326 // dumps the definition of the instance fields for a given class
1327 // inlined_fields_id is not-nullptr for inlined fields (to get synthetic field name IDs
1328 // by using InlinedObjects::get_next_string_id()).
1329 void DumperSupport::dump_instance_field_descriptors(AbstractDumpWriter* writer, InstanceKlass* ik, uintx* inlined_fields_id) {
1330 // inlined_fields_id != nullptr means ik is a class of inlined field.
1331 // Inlined field id pointer for this class; lazyly initialized
1332 // if the class has inlined field(s) and the caller didn't provide inlined_fields_id.
1333 uintx *this_klass_inlined_fields_id = inlined_fields_id;
1334 uintx inlined_id = 0;
1335
1336 // dump the field descriptors
1337 for (JavaFieldStream fld(ik); !fld.done(); fld.next()) {
1338 if (!fld.access_flags().is_static()) {
1339 if (is_inlined_field(fld.field_descriptor())) {
1340 // dump "synthetic" fields for inlined fields.
1341 if (this_klass_inlined_fields_id == nullptr) {
1342 inlined_id = InlinedObjects::get_instance()->get_base_index_for(ik);
1343 this_klass_inlined_fields_id = &inlined_id;
1344 }
1345 dump_instance_field_descriptors(writer, get_inlined_field_klass(fld.field_descriptor()), this_klass_inlined_fields_id);
1346 } else {
1347 Symbol* sig = fld.signature();
1348 Symbol* name = nullptr;
1349 // Use inlined_fields_id provided by caller.
1350 if (inlined_fields_id != nullptr) {
1351 uintx name_id = InlinedObjects::get_instance()->get_next_string_id(*inlined_fields_id);
1352
1353 // name_id == 0 is returned on error. use original field signature.
1354 if (name_id != 0) {
1355 *inlined_fields_id = name_id;
1356 name = reinterpret_cast<Symbol*>(name_id);
1357 }
1358 }
1359 if (name == nullptr) {
1360 name = fld.name();
1361 }
1362
1363 writer->write_symbolID(name); // name
1364 writer->write_u1(sig2tag(sig)); // type
1365 }
1366 }
1367 }
1368 }
1369
1370 // creates HPROF_GC_INSTANCE_DUMP record for the given object
1371 void DumperSupport::dump_instance(AbstractDumpWriter* writer, oop o, DumperClassCacheTable* class_cache) {
1372 InstanceKlass* ik = InstanceKlass::cast(o->klass());
1373
1374 DumperClassCacheTableEntry* cache_entry = class_cache->lookup_or_create(ik);
1375
1376 u4 is = instance_size(ik, cache_entry);
1377 u4 size = 1 + sizeof(address) + 4 + sizeof(address) + 4 + is;
1378
1379 writer->start_sub_record(HPROF_GC_INSTANCE_DUMP, size);
1380 writer->write_objectID(o);
1381 writer->write_u4(STACK_TRACE_ID);
1382
1383 // class ID
1384 writer->write_classID(ik);
1385
1386 // number of bytes that follow
1387 writer->write_u4(is);
1388
1389 // field values
1390 dump_instance_fields(writer, o, 0, class_cache, cache_entry);
1391
1392 writer->end_sub_record();
1393 }
1394
1395 // creates HPROF_GC_CLASS_DUMP record for the given instance class
1396 void DumperSupport::dump_instance_class(AbstractDumpWriter* writer, InstanceKlass* ik) {
1397 // We can safepoint and do a heap dump at a point where we have a Klass,
1398 // but no java mirror class has been setup for it. So we need to check
1399 // that the class is at least loaded, to avoid crash from a null mirror.
1400 if (!ik->is_loaded()) {
1401 return;
1402 }
1403
1404 u2 static_fields_count = 0;
1405 u4 static_size = get_static_fields_size(ik, static_fields_count);
1406 u2 instance_fields_count = get_instance_fields_count(ik);
1407 u4 instance_fields_size = instance_fields_count * (sizeof(address) + 1);
1408 u4 size = checked_cast<u4>(1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + static_size + 2 + instance_fields_size);
1409
1410 writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1411
1412 // class ID
1413 writer->write_classID(ik);
1414 writer->write_u4(STACK_TRACE_ID);
1415
1416 // super class ID
1417 InstanceKlass* super = ik->super();
1418 if (super == nullptr) {
1419 writer->write_objectID(oop(nullptr));
1420 } else {
1421 writer->write_classID(super);
1422 }
1423
1424 writer->write_objectID(ik->class_loader());
1425 writer->write_objectID(ik->signers());
1426 writer->write_objectID(ik->protection_domain());
1427
1428 // reserved
1429 writer->write_objectID(oop(nullptr));
1430 writer->write_objectID(oop(nullptr));
1431
1432 // instance size
1433 writer->write_u4(HeapWordSize * ik->size_helper());
1434
1435 // size of constant pool - ignored by HAT 1.1
1436 writer->write_u2(0);
1437
1438 // static fields
1439 writer->write_u2(static_fields_count);
1440 dump_static_fields(writer, ik);
1441
1442 // description of instance fields
1443 writer->write_u2(instance_fields_count);
1444 dump_instance_field_descriptors(writer, ik);
1445
1446 writer->end_sub_record();
1447 }
1448
1449 // creates HPROF_GC_CLASS_DUMP record for the given array class
1450 void DumperSupport::dump_array_class(AbstractDumpWriter* writer, Klass* k) {
1451 InstanceKlass* ik = nullptr; // bottom class for object arrays, null for primitive type arrays
1452 if (k->is_objArray_klass()) {
1453 Klass *bk = ObjArrayKlass::cast(k)->bottom_klass();
1454 assert(bk != nullptr, "checking");
1455 if (bk->is_instance_klass()) {
1456 ik = InstanceKlass::cast(bk);
1457 }
1458 }
1459
1460 u4 size = 1 + sizeof(address) + 4 + 6 * sizeof(address) + 4 + 2 + 2 + 2;
1461 writer->start_sub_record(HPROF_GC_CLASS_DUMP, size);
1462 writer->write_classID(k);
1463 writer->write_u4(STACK_TRACE_ID);
1464
1465 // super class of array classes is java.lang.Object
1466 InstanceKlass* java_super = k->java_super();
1467 assert(java_super != nullptr, "checking");
1468 writer->write_classID(java_super);
1469
1470 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->class_loader());
1471 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->signers());
1472 writer->write_objectID(ik == nullptr ? oop(nullptr) : ik->protection_domain());
1473
1474 writer->write_objectID(oop(nullptr)); // reserved
1475 writer->write_objectID(oop(nullptr));
1476 writer->write_u4(0); // instance size
1477 writer->write_u2(0); // constant pool
1478 writer->write_u2(0); // static fields
1479 writer->write_u2(0); // instance fields
1480
1481 writer->end_sub_record();
1482
1483 }
1484
1485 // Hprof uses an u4 as record length field,
1486 // which means we need to truncate arrays that are too long.
1487 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, int type_size, short header_size) {
1488 int length = array->length();
1489
1490 size_t length_in_bytes = (size_t)length * type_size;
1491 uint max_bytes = max_juint - header_size;
1492
1493 if (length_in_bytes > max_bytes) {
1494 length = max_bytes / type_size;
1495 length_in_bytes = (size_t)length * type_size;
1496
1497 BasicType type = ArrayKlass::cast(array->klass())->element_type();
1498 warning("cannot dump array of type %s[] with length %d; truncating to length %d",
1499 type2name_tab[type], array->length(), length);
1500 }
1501 return length;
1502 }
1503
1504 int DumperSupport::calculate_array_max_length(AbstractDumpWriter* writer, arrayOop array, short header_size) {
1505 BasicType type = ArrayKlass::cast(array->klass())->element_type();
1506 assert((type >= T_BOOLEAN && type <= T_OBJECT) || type == T_FLAT_ELEMENT, "invalid array element type");
1507 int type_size;
1508 if (type == T_OBJECT) {
1509 type_size = sizeof(address);
1510 } else if (type == T_FLAT_ELEMENT) {
1511 // TODO: FIXME
1512 fatal("Not supported yet"); // FIXME: JDK-8325678
1513 } else {
1514 type_size = type2aelembytes(type);
1515 }
1516
1517 return calculate_array_max_length(writer, array, type_size, header_size);
1518 }
1519
1520 // creates HPROF_GC_OBJ_ARRAY_DUMP record for the given object array
1521 void DumperSupport::dump_object_array(AbstractDumpWriter* writer, objArrayOop array) {
1522 // sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID) + sizeof(classID)
1523 short header_size = 1 + 2 * 4 + 2 * sizeof(address);
1524 int length = calculate_array_max_length(writer, array, header_size);
1525 u4 size = checked_cast<u4>(header_size + length * sizeof(address));
1526
1527 writer->start_sub_record(HPROF_GC_OBJ_ARRAY_DUMP, size);
1528 writer->write_objectID(array);
1529 writer->write_u4(STACK_TRACE_ID);
1530 writer->write_u4(length);
1531
1532 // array class ID
1533 writer->write_classID(array->klass());
1534
1535 // [id]* elements
1536 for (int index = 0; index < length; index++) {
1537 oop o = array->obj_at(index);
1538 o = mask_dormant_archived_object(o, array);
1539 writer->write_objectID(o);
1540 }
1541
1542 writer->end_sub_record();
1543 }
1544
1545 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given flat array
1546 void DumperSupport::dump_flat_array(AbstractDumpWriter* writer, flatArrayOop array, DumperClassCacheTable* class_cache) {
1547 FlatArrayKlass* array_klass = FlatArrayKlass::cast(array->klass());
1548 InlineKlass* element_klass = array_klass->element_klass();
1549 int element_size = instance_size(element_klass);
1550 /* id array object ID
1551 * u4 stack trace serial number
1552 * u4 number of elements
1553 * u1 element type
1554 */
1555 short header_size = 1 + sizeof(address) + 2 * 4 + 1;
1556
1557 // TODO: use T_SHORT/T_INT/T_LONG if needed to avoid truncation
1558 BasicType type = T_BYTE;
1559 int type_size = type2aelembytes(type);
1560 int length = calculate_array_max_length(writer, array, element_size, header_size);
1561 u4 length_in_bytes = (u4)(length * element_size);
1562 u4 size = header_size + length_in_bytes;
1563
1564 writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1565 writer->write_objectID(array);
1566 writer->write_u4(STACK_TRACE_ID);
1567 // TODO: round up array length for T_SHORT/T_INT/T_LONG
1568 writer->write_u4(length * element_size);
1569 writer->write_u1(type2tag(type));
1570
1571 for (int index = 0; index < length; index++) {
1572 // need offset in the holder to read inlined object. calculate it from flatArrayOop::value_at_addr()
1573 int offset = (int)((address)array->value_at_addr(index, array_klass->layout_helper())
1574 - cast_from_oop<address>(array));
1575 DumperClassCacheTableEntry* class_cache_entry = class_cache->lookup_or_create(element_klass);
1576 dump_inlined_object_fields(writer, array, offset, class_cache, class_cache_entry);
1577 }
1578
1579 // TODO: write padding bytes for T_SHORT/T_INT/T_LONG
1580
1581 InlinedObjects::get_instance()->add_flat_array(array);
1582
1583 writer->end_sub_record();
1584 }
1585
1586 #define WRITE_ARRAY(Array, Type, Size, Length) \
1587 for (int i = 0; i < Length; i++) { writer->write_##Size((Size)Array->Type##_at(i)); }
1588
1589 // creates HPROF_GC_PRIM_ARRAY_DUMP record for the given type array
1590 void DumperSupport::dump_prim_array(AbstractDumpWriter* writer, typeArrayOop array) {
1591 BasicType type = TypeArrayKlass::cast(array->klass())->element_type();
1592 // 2 * sizeof(u1) + 2 * sizeof(u4) + sizeof(objectID)
1593 short header_size = 2 * 1 + 2 * 4 + sizeof(address);
1594
1595 int length = calculate_array_max_length(writer, array, header_size);
1596 int type_size = type2aelembytes(type);
1597 u4 length_in_bytes = (u4)length * type_size;
1598 u4 size = header_size + length_in_bytes;
1599
1600 writer->start_sub_record(HPROF_GC_PRIM_ARRAY_DUMP, size);
1601 writer->write_objectID(array);
1602 writer->write_u4(STACK_TRACE_ID);
1603 writer->write_u4(length);
1604 writer->write_u1(type2tag(type));
1605
1606 // nothing to copy
1607 if (length == 0) {
1608 writer->end_sub_record();
1609 return;
1610 }
1611
1612 // If the byte ordering is big endian then we can copy most types directly
1613
1614 switch (type) {
1615 case T_INT : {
1616 if (Endian::is_Java_byte_ordering_different()) {
1617 WRITE_ARRAY(array, int, u4, length);
1618 } else {
1619 writer->write_raw(array->int_at_addr(0), length_in_bytes);
1620 }
1621 break;
1622 }
1623 case T_BYTE : {
1624 writer->write_raw(array->byte_at_addr(0), length_in_bytes);
1625 break;
1626 }
1627 case T_CHAR : {
1628 if (Endian::is_Java_byte_ordering_different()) {
1629 WRITE_ARRAY(array, char, u2, length);
1630 } else {
1631 writer->write_raw(array->char_at_addr(0), length_in_bytes);
1632 }
1633 break;
1634 }
1635 case T_SHORT : {
1636 if (Endian::is_Java_byte_ordering_different()) {
1637 WRITE_ARRAY(array, short, u2, length);
1638 } else {
1639 writer->write_raw(array->short_at_addr(0), length_in_bytes);
1640 }
1641 break;
1642 }
1643 case T_BOOLEAN : {
1644 if (Endian::is_Java_byte_ordering_different()) {
1645 WRITE_ARRAY(array, bool, u1, length);
1646 } else {
1647 writer->write_raw(array->bool_at_addr(0), length_in_bytes);
1648 }
1649 break;
1650 }
1651 case T_LONG : {
1652 if (Endian::is_Java_byte_ordering_different()) {
1653 WRITE_ARRAY(array, long, u8, length);
1654 } else {
1655 writer->write_raw(array->long_at_addr(0), length_in_bytes);
1656 }
1657 break;
1658 }
1659
1660 // handle float/doubles in a special value to ensure than NaNs are
1661 // written correctly. TO DO: Check if we can avoid this on processors that
1662 // use IEEE 754.
1663
1664 case T_FLOAT : {
1665 for (int i = 0; i < length; i++) {
1666 dump_float(writer, array->float_at(i));
1667 }
1668 break;
1669 }
1670 case T_DOUBLE : {
1671 for (int i = 0; i < length; i++) {
1672 dump_double(writer, array->double_at(i));
1673 }
1674 break;
1675 }
1676 default : ShouldNotReachHere();
1677 }
1678
1679 writer->end_sub_record();
1680 }
1681
1682 // create a HPROF_FRAME record of the given Method* and bci
1683 void DumperSupport::dump_stack_frame(AbstractDumpWriter* writer,
1684 int frame_serial_num,
1685 int class_serial_num,
1686 Method* m,
1687 int bci) {
1688 int line_number;
1689 if (m->is_native()) {
1690 line_number = -3; // native frame
1691 } else {
1692 line_number = m->line_number_from_bci(bci);
1693 }
1694
1695 write_header(writer, HPROF_FRAME, 4*oopSize + 2*sizeof(u4));
1696 writer->write_id(frame_serial_num); // frame serial number
1697 writer->write_symbolID(m->name()); // method's name
1698 writer->write_symbolID(m->signature()); // method's signature
1699
1700 assert(m->method_holder()->is_instance_klass(), "not InstanceKlass");
1701 writer->write_symbolID(m->method_holder()->source_file_name()); // source file name
1702 writer->write_u4(class_serial_num); // class serial number
1703 writer->write_u4((u4) line_number); // line number
1704 }
1705
1706
1707 class InlinedFieldNameDumper : public LockedClassesDo {
1708 public:
1709 typedef void (*Callback)(InlinedObjects *owner, const Klass *klass, uintx base_index, int count);
1710
1711 private:
1712 AbstractDumpWriter* _writer;
1713 InlinedObjects *_owner;
1714 Callback _callback;
1715 uintx _index;
1716
1717 void dump_inlined_field_names(GrowableArray<Symbol*>* super_names, Symbol* field_name, InlineKlass* klass) {
1718 super_names->push(field_name);
1719 for (HierarchicalFieldStream<JavaFieldStream> fld(klass); !fld.done(); fld.next()) {
1720 if (!fld.access_flags().is_static()) {
1721 if (DumperSupport::is_inlined_field(fld.field_descriptor())) {
1722 dump_inlined_field_names(super_names, fld.name(), DumperSupport::get_inlined_field_klass(fld.field_descriptor()));
1723 } else {
1724 // get next string ID.
1725 uintx next_index = _owner->get_next_string_id(_index);
1726 if (next_index == 0) {
1727 // something went wrong (overflow?)
1728 // stop generation; the rest of inlined objects will have original field names.
1729 return;
1730 }
1731 _index = next_index;
1732
1733 // Calculate length.
1734 int len = fld.name()->utf8_length();
1735 for (GrowableArrayIterator<Symbol*> it = super_names->begin(); it != super_names->end(); ++it) {
1736 len += (*it)->utf8_length() + 1; // +1 for ".".
1737 }
1738
1739 DumperSupport::write_header(_writer, HPROF_UTF8, oopSize + len);
1740 _writer->write_symbolID(reinterpret_cast<Symbol*>(_index));
1741 // Write the string value.
1742 // 1) super_names.
1743 for (GrowableArrayIterator<Symbol*> it = super_names->begin(); it != super_names->end(); ++it) {
1744 _writer->write_raw((*it)->bytes(), (*it)->utf8_length());
1745 _writer->write_u1('.');
1746 }
1747 // 2) field name.
1748 _writer->write_raw(fld.name()->bytes(), fld.name()->utf8_length());
1749 }
1750 }
1751 }
1752 super_names->pop();
1753 }
1754
1755 void dump_inlined_field_names(Symbol* field_name, InlineKlass* field_klass) {
1756 GrowableArray<Symbol*> super_names(4, mtServiceability);
1757 dump_inlined_field_names(&super_names, field_name, field_klass);
1758 }
1759
1760 public:
1761 InlinedFieldNameDumper(AbstractDumpWriter* writer, InlinedObjects* owner, Callback callback)
1762 : _writer(writer), _owner(owner), _callback(callback), _index(0) {
1763 }
1764
1765 void do_klass(Klass* k) {
1766 if (!k->is_instance_klass()) {
1767 return;
1768 }
1769 InstanceKlass* ik = InstanceKlass::cast(k);
1770 // if (ik->has_inline_type_fields()) {
1771 // return;
1772 // }
1773
1774 uintx base_index = _index;
1775 int count = 0;
1776
1777 for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
1778 if (!fld.access_flags().is_static()) {
1779 if (DumperSupport::is_inlined_field(fld.field_descriptor())) {
1780 dump_inlined_field_names(fld.name(), DumperSupport::get_inlined_field_klass(fld.field_descriptor()));
1781 count++;
1782 }
1783 }
1784 }
1785
1786 if (count != 0) {
1787 _callback(_owner, k, base_index, count);
1788 }
1789 }
1790 };
1791
1792 class InlinedFieldsDumper : public LockedClassesDo {
1793 private:
1794 AbstractDumpWriter* _writer;
1795
1796 public:
1797 InlinedFieldsDumper(AbstractDumpWriter* writer) : _writer(writer) {}
1798
1799 void do_klass(Klass* k) {
1800 if (!k->is_instance_klass()) {
1801 return;
1802 }
1803 InstanceKlass* ik = InstanceKlass::cast(k);
1804 // if (ik->has_inline_type_fields()) {
1805 // return;
1806 // }
1807
1808 // We can be at a point where java mirror does not exist yet.
1809 // So we need to check that the class is at least loaded, to avoid crash from a null mirror.
1810 if (!ik->is_loaded()) {
1811 return;
1812 }
1813
1814 u2 inlined_count = 0;
1815 for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
1816 if (!fld.access_flags().is_static()) {
1817 if (DumperSupport::is_inlined_field(fld.field_descriptor())) {
1818 inlined_count++;
1819 }
1820 }
1821 }
1822 if (inlined_count != 0) {
1823 _writer->write_u1(HPROF_CLASS_WITH_INLINED_FIELDS);
1824
1825 // class ID
1826 _writer->write_classID(ik);
1827 // number of inlined fields
1828 _writer->write_u2(inlined_count);
1829 u2 index = 0;
1830 for (HierarchicalFieldStream<JavaFieldStream> fld(ik); !fld.done(); fld.next()) {
1831 if (!fld.access_flags().is_static()) {
1832 if (DumperSupport::is_inlined_field(fld.field_descriptor())) {
1833 // inlined field index
1834 _writer->write_u2(index);
1835 // synthetic field count
1836 u2 field_count = DumperSupport::get_instance_fields_count(DumperSupport::get_inlined_field_klass(fld.field_descriptor()));
1837 _writer->write_u2(field_count);
1838 // original field name
1839 _writer->write_symbolID(fld.name());
1840 // inlined field class ID
1841 _writer->write_classID(DumperSupport::get_inlined_field_klass(fld.field_descriptor()));
1842
1843 index += field_count;
1844 } else {
1845 index++;
1846 }
1847 }
1848 }
1849 }
1850 }
1851 };
1852
1853
1854 void InlinedObjects::init() {
1855 _instance = this;
1856
1857 struct Closure : public SymbolClosure {
1858 uintx _min_id = max_uintx;
1859 uintx _max_id = 0;
1860 Closure() : _min_id(max_uintx), _max_id(0) {}
1861
1862 void do_symbol(Symbol** p) {
1863 uintx val = reinterpret_cast<uintx>(*p);
1864 if (val < _min_id) {
1865 _min_id = val;
1866 }
1867 if (val > _max_id) {
1868 _max_id = val;
1869 }
1870 }
1871 } closure;
1872
1873 SymbolTable::symbols_do(&closure);
1874
1875 _min_string_id = closure._min_id;
1876 _max_string_id = closure._max_id;
1877 }
1878
1879 void InlinedObjects::release() {
1880 _instance = nullptr;
1881
1882 if (_inlined_field_map != nullptr) {
1883 delete _inlined_field_map;
1884 _inlined_field_map = nullptr;
1885 }
1886 if (_flat_arrays != nullptr) {
1887 delete _flat_arrays;
1888 _flat_arrays = nullptr;
1889 }
1890 }
1891
1892 void InlinedObjects::inlined_field_names_callback(InlinedObjects* _this, const Klass* klass, uintx base_index, int count) {
1893 if (_this->_inlined_field_map == nullptr) {
1894 _this->_inlined_field_map = new (mtServiceability) GrowableArray<ClassInlinedFields>(100, mtServiceability);
1895 }
1896 _this->_inlined_field_map->append(ClassInlinedFields(klass, base_index));
1897
1898 // counters for dumping classes with inlined fields
1899 _this->_classes_count++;
1900 _this->_inlined_fields_count += count;
1901 }
1902
1903 void InlinedObjects::dump_inlined_field_names(AbstractDumpWriter* writer) {
1904 InlinedFieldNameDumper nameDumper(writer, this, inlined_field_names_callback);
1905 ClassLoaderDataGraph::classes_do(&nameDumper);
1906
1907 if (_inlined_field_map != nullptr) {
1908 // prepare the map for get_base_index_for().
1909 _inlined_field_map->sort(ClassInlinedFields::compare);
1910 }
1911 }
1912
1913 uintx InlinedObjects::get_base_index_for(Klass* k) {
1914 if (_inlined_field_map != nullptr) {
1915 bool found = false;
1916 int idx = _inlined_field_map->find_sorted<ClassInlinedFields, ClassInlinedFields::compare>(ClassInlinedFields(k, 0), found);
1917 if (found) {
1918 return _inlined_field_map->at(idx).base_index;
1919 }
1920 }
1921
1922 // return max_uintx, so get_next_string_id returns 0.
1923 return max_uintx;
1924 }
1925
1926 uintx InlinedObjects::get_next_string_id(uintx id) {
1927 if (++id == _min_string_id) {
1928 return _max_string_id + 1;
1929 }
1930 return id;
1931 }
1932
1933 void InlinedObjects::dump_classed_with_inlined_fields(AbstractDumpWriter* writer) {
1934 if (_classes_count != 0) {
1935 // Record for each class contains tag(u1), class ID and count(u2)
1936 // for each inlined field index(u2), synthetic fields count(u2), original field name and class ID
1937 int size = _classes_count * (1 + sizeof(address) + 2)
1938 + _inlined_fields_count * (2 + 2 + sizeof(address) + sizeof(address));
1939 DumperSupport::write_header(writer, HPROF_INLINED_FIELDS, (u4)size);
1940
1941 InlinedFieldsDumper dumper(writer);
1942 ClassLoaderDataGraph::classes_do(&dumper);
1943 }
1944 }
1945
1946 void InlinedObjects::add_flat_array(oop array) {
1947 if (_flat_arrays == nullptr) {
1948 _flat_arrays = new (mtServiceability) GrowableArray<oop>(100, mtServiceability);
1949 }
1950 _flat_arrays->append(array);
1951 }
1952
1953 void InlinedObjects::dump_flat_arrays(AbstractDumpWriter* writer) {
1954 if (_flat_arrays != nullptr) {
1955 // For each flat array the record contains tag (u1), object ID and class ID.
1956 int size = _flat_arrays->length() * (1 + sizeof(address) + sizeof(address));
1957
1958 DumperSupport::write_header(writer, HPROF_FLAT_ARRAYS, (u4)size);
1959 for (GrowableArrayIterator<oop> it = _flat_arrays->begin(); it != _flat_arrays->end(); ++it) {
1960 flatArrayOop array = flatArrayOop(*it);
1961 FlatArrayKlass* array_klass = FlatArrayKlass::cast(array->klass());
1962 InlineKlass* element_klass = array_klass->element_klass();
1963 writer->write_u1(HPROF_FLAT_ARRAY);
1964 writer->write_objectID(array);
1965 writer->write_classID(element_klass);
1966 }
1967 }
1968 }
1969
1970
1971 // Support class used to generate HPROF_UTF8 records from the entries in the
1972 // SymbolTable.
1973
1974 class SymbolTableDumper : public SymbolClosure {
1975 private:
1976 AbstractDumpWriter* _writer;
1977 AbstractDumpWriter* writer() const { return _writer; }
1978 public:
1979 SymbolTableDumper(AbstractDumpWriter* writer) { _writer = writer; }
1980 void do_symbol(Symbol** p);
1981 };
1982
1983 void SymbolTableDumper::do_symbol(Symbol** p) {
1984 ResourceMark rm;
1985 Symbol* sym = *p;
1986 int len = sym->utf8_length();
1987 if (len > 0) {
1988 char* s = sym->as_utf8();
1989 DumperSupport::write_header(writer(), HPROF_UTF8, oopSize + len);
1990 writer()->write_symbolID(sym);
1991 writer()->write_raw(s, len);
1992 }
1993 }
1994
1995 // Support class used to generate HPROF_GC_CLASS_DUMP records
1996
1997 class ClassDumper : public KlassClosure {
1998 private:
1999 AbstractDumpWriter* _writer;
2000 AbstractDumpWriter* writer() const { return _writer; }
2001
2002 public:
2003 ClassDumper(AbstractDumpWriter* writer) : _writer(writer) {}
2004
2005 void do_klass(Klass* k) {
2006 if (k->is_instance_klass()) {
2007 DumperSupport::dump_instance_class(writer(), InstanceKlass::cast(k));
2008 } else {
2009 DumperSupport::dump_array_class(writer(), k);
2010 }
2011 }
2012 };
2013
2014 // Support class used to generate HPROF_LOAD_CLASS records
2015
2016 class LoadedClassDumper : public LockedClassesDo {
2017 private:
2018 AbstractDumpWriter* _writer;
2019 GrowableArray<Klass*>* _klass_map;
2020 u4 _class_serial_num;
2021 AbstractDumpWriter* writer() const { return _writer; }
2022 void add_class_serial_number(Klass* k, int serial_num) {
2023 _klass_map->at_put_grow(serial_num, k);
2024 }
2025 public:
2026 LoadedClassDumper(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map)
2027 : _writer(writer), _klass_map(klass_map), _class_serial_num(0) {}
2028
2029 void do_klass(Klass* k) {
2030 // len of HPROF_LOAD_CLASS record
2031 u4 remaining = 2 * oopSize + 2 * sizeof(u4);
2032 DumperSupport::write_header(writer(), HPROF_LOAD_CLASS, remaining);
2033 // class serial number is just a number
2034 writer()->write_u4(++_class_serial_num);
2035 // class ID
2036 writer()->write_classID(k);
2037 // add the Klass* and class serial number pair
2038 add_class_serial_number(k, _class_serial_num);
2039 writer()->write_u4(STACK_TRACE_ID);
2040 // class name ID
2041 Symbol* name = k->name();
2042 writer()->write_symbolID(name);
2043 }
2044 };
2045
2046 // Support class used to generate HPROF_GC_ROOT_JNI_LOCAL records
2047
2048 class JNILocalsDumper : public OopClosure {
2049 private:
2050 AbstractDumpWriter* _writer;
2051 u4 _thread_serial_num;
2052 int _frame_num;
2053 AbstractDumpWriter* writer() const { return _writer; }
2054 public:
2055 JNILocalsDumper(AbstractDumpWriter* writer, u4 thread_serial_num) {
2056 _writer = writer;
2057 _thread_serial_num = thread_serial_num;
2058 _frame_num = -1; // default - empty stack
2059 }
2060 void set_frame_number(int n) { _frame_num = n; }
2061 void do_oop(oop* obj_p);
2062 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
2063 };
2064
2065 void JNILocalsDumper::do_oop(oop* obj_p) {
2066 // ignore null handles
2067 oop o = *obj_p;
2068 if (o != nullptr) {
2069 u4 size = 1 + sizeof(address) + 4 + 4;
2070 writer()->start_sub_record(HPROF_GC_ROOT_JNI_LOCAL, size);
2071 writer()->write_objectID(o);
2072 writer()->write_u4(_thread_serial_num);
2073 writer()->write_u4((u4)_frame_num);
2074 writer()->end_sub_record();
2075 }
2076 }
2077
2078
2079 // Support class used to generate HPROF_GC_ROOT_JNI_GLOBAL records
2080
2081 class JNIGlobalsDumper : public OopClosure {
2082 private:
2083 AbstractDumpWriter* _writer;
2084 AbstractDumpWriter* writer() const { return _writer; }
2085
2086 public:
2087 JNIGlobalsDumper(AbstractDumpWriter* writer) {
2088 _writer = writer;
2089 }
2090 void do_oop(oop* obj_p);
2091 void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); }
2092 };
2093
2094 void JNIGlobalsDumper::do_oop(oop* obj_p) {
2095 oop o = NativeAccess<AS_NO_KEEPALIVE>::oop_load(obj_p);
2096
2097 // ignore these
2098 if (o == nullptr) return;
2099 // we ignore global ref to symbols and other internal objects
2100 if (o->is_instance() || o->is_objArray() || o->is_typeArray()) {
2101 u4 size = 1 + 2 * sizeof(address);
2102 writer()->start_sub_record(HPROF_GC_ROOT_JNI_GLOBAL, size);
2103 writer()->write_objectID(o);
2104 writer()->write_rootID(obj_p); // global ref ID
2105 writer()->end_sub_record();
2106 }
2107 };
2108
2109 // Support class used to generate HPROF_GC_ROOT_STICKY_CLASS records
2110
2111 class StickyClassDumper : public KlassClosure {
2112 private:
2113 AbstractDumpWriter* _writer;
2114 AbstractDumpWriter* writer() const { return _writer; }
2115 public:
2116 StickyClassDumper(AbstractDumpWriter* writer) {
2117 _writer = writer;
2118 }
2119 void do_klass(Klass* k) {
2120 if (k->is_instance_klass()) {
2121 InstanceKlass* ik = InstanceKlass::cast(k);
2122 u4 size = 1 + sizeof(address);
2123 writer()->start_sub_record(HPROF_GC_ROOT_STICKY_CLASS, size);
2124 writer()->write_classID(ik);
2125 writer()->end_sub_record();
2126 }
2127 }
2128 };
2129
2130 // Support class used to generate HPROF_GC_ROOT_JAVA_FRAME records.
2131
2132 class JavaStackRefDumper : public StackObj {
2133 private:
2134 AbstractDumpWriter* _writer;
2135 u4 _thread_serial_num;
2136 int _frame_num;
2137 AbstractDumpWriter* writer() const { return _writer; }
2138 public:
2139 JavaStackRefDumper(AbstractDumpWriter* writer, u4 thread_serial_num)
2140 : _writer(writer), _thread_serial_num(thread_serial_num), _frame_num(-1) // default - empty stack
2141 {
2142 }
2143
2144 void set_frame_number(int n) { _frame_num = n; }
2145
2146 void dump_java_stack_refs(StackValueCollection* values);
2147 };
2148
2149 void JavaStackRefDumper::dump_java_stack_refs(StackValueCollection* values) {
2150 for (int index = 0; index < values->size(); index++) {
2151 if (values->at(index)->type() == T_OBJECT) {
2152 oop o = values->obj_at(index)();
2153 if (o != nullptr) {
2154 u4 size = 1 + sizeof(address) + 4 + 4;
2155 writer()->start_sub_record(HPROF_GC_ROOT_JAVA_FRAME, size);
2156 writer()->write_objectID(o);
2157 writer()->write_u4(_thread_serial_num);
2158 writer()->write_u4((u4)_frame_num);
2159 writer()->end_sub_record();
2160 }
2161 }
2162 }
2163 }
2164
2165 // Class to collect, store and dump thread-related data:
2166 // - HPROF_TRACE and HPROF_FRAME records;
2167 // - HPROF_GC_ROOT_THREAD_OBJ/HPROF_GC_ROOT_JAVA_FRAME/HPROF_GC_ROOT_JNI_LOCAL subrecords.
2168 class ThreadDumper : public CHeapObj<mtInternal> {
2169 public:
2170 enum class ThreadType { Platform, MountedVirtual, UnmountedVirtual };
2171
2172 private:
2173 ThreadType _thread_type;
2174 JavaThread* _java_thread;
2175 oop _thread_oop;
2176
2177 GrowableArray<StackFrameInfo*>* _frames;
2178 // non-null if the thread is OOM thread
2179 Method* _oome_constructor;
2180 int _thread_serial_num;
2181 int _start_frame_serial_num;
2182
2183 vframe* get_top_frame() const;
2184
2185 public:
2186 static bool should_dump_pthread(JavaThread* thread) {
2187 return thread->threadObj() != nullptr && !thread->is_exiting() && !thread->is_hidden_from_external_view();
2188 }
2189
2190 static bool should_dump_vthread(oop vt) {
2191 return java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW
2192 && java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::TERMINATED;
2193 }
2194
2195 static bool is_vthread_mounted(oop vt) {
2196 // The code should be consistent with the "mounted virtual thread" case
2197 // (VM_HeapDumper::dump_stack_traces(), ThreadDumper::get_top_frame()).
2198 // I.e. virtual thread is mounted if its carrierThread is not null
2199 // and is_vthread_mounted() for the carrier thread returns true.
2200 oop carrier_thread = java_lang_VirtualThread::carrier_thread(vt);
2201 if (carrier_thread == nullptr) {
2202 return false;
2203 }
2204 JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
2205 return java_thread->is_vthread_mounted();
2206 }
2207
2208 ThreadDumper(ThreadType thread_type, JavaThread* java_thread, oop thread_oop);
2209 ~ThreadDumper() {
2210 for (int index = 0; index < _frames->length(); index++) {
2211 delete _frames->at(index);
2212 }
2213 delete _frames;
2214 }
2215
2216 // affects frame_count
2217 void add_oom_frame(Method* oome_constructor) {
2218 assert(_start_frame_serial_num == 0, "add_oom_frame cannot be called after init_serial_nums");
2219 _oome_constructor = oome_constructor;
2220 }
2221
2222 void init_serial_nums(volatile int* thread_counter, volatile int* frame_counter) {
2223 assert(_start_frame_serial_num == 0, "already initialized");
2224 _thread_serial_num = AtomicAccess::fetch_then_add(thread_counter, 1);
2225 _start_frame_serial_num = AtomicAccess::fetch_then_add(frame_counter, frame_count());
2226 }
2227
2228 bool oom_thread() const {
2229 return _oome_constructor != nullptr;
2230 }
2231
2232 int frame_count() const {
2233 return _frames->length() + (oom_thread() ? 1 : 0);
2234 }
2235
2236 u4 thread_serial_num() const {
2237 return (u4)_thread_serial_num;
2238 }
2239
2240 u4 stack_trace_serial_num() const {
2241 return (u4)(_thread_serial_num + STACK_TRACE_ID);
2242 }
2243
2244 // writes HPROF_TRACE and HPROF_FRAME records
2245 // returns number of dumped frames
2246 void dump_stack_traces(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map);
2247
2248 // writes HPROF_GC_ROOT_THREAD_OBJ subrecord
2249 void dump_thread_obj(AbstractDumpWriter* writer);
2250
2251 // Walk the stack of the thread.
2252 // Dumps a HPROF_GC_ROOT_JAVA_FRAME subrecord for each local
2253 // Dumps a HPROF_GC_ROOT_JNI_LOCAL subrecord for each JNI local
2254 void dump_stack_refs(AbstractDumpWriter* writer);
2255
2256 };
2257
2258 ThreadDumper::ThreadDumper(ThreadType thread_type, JavaThread* java_thread, oop thread_oop)
2259 : _thread_type(thread_type), _java_thread(java_thread), _thread_oop(thread_oop),
2260 _oome_constructor(nullptr),
2261 _thread_serial_num(0), _start_frame_serial_num(0)
2262 {
2263 // sanity checks
2264 if (_thread_type == ThreadType::UnmountedVirtual) {
2265 assert(_java_thread == nullptr, "sanity");
2266 assert(_thread_oop != nullptr, "sanity");
2267 } else {
2268 assert(_java_thread != nullptr, "sanity");
2269 assert(_thread_oop != nullptr, "sanity");
2270 }
2271
2272 _frames = new (mtServiceability) GrowableArray<StackFrameInfo*>(10, mtServiceability);
2273 bool stop_at_vthread_entry = _thread_type == ThreadType::MountedVirtual;
2274
2275 // vframes are resource allocated
2276 Thread* current_thread = Thread::current();
2277 ResourceMark rm(current_thread);
2278 HandleMark hm(current_thread);
2279
2280 for (vframe* vf = get_top_frame(); vf != nullptr; vf = vf->sender()) {
2281 if (stop_at_vthread_entry && vf->is_vthread_entry()) {
2282 break;
2283 }
2284 if (vf->is_java_frame()) {
2285 javaVFrame* jvf = javaVFrame::cast(vf);
2286 _frames->append(new StackFrameInfo(jvf, false));
2287 } else {
2288 // ignore non-Java frames
2289 }
2290 }
2291 }
2292
2293 void ThreadDumper::dump_stack_traces(AbstractDumpWriter* writer, GrowableArray<Klass*>* klass_map) {
2294 assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_nums are not initialized");
2295
2296 // write HPROF_FRAME records for this thread's stack trace
2297 int depth = _frames->length();
2298 int frame_serial_num = _start_frame_serial_num;
2299
2300 if (oom_thread()) {
2301 // OOM thread
2302 // write fake frame that makes it look like the thread, which caused OOME,
2303 // is in the OutOfMemoryError zero-parameter constructor
2304 int oome_serial_num = klass_map->find(_oome_constructor->method_holder());
2305 // the class serial number starts from 1
2306 assert(oome_serial_num > 0, "OutOfMemoryError class not found");
2307 DumperSupport::dump_stack_frame(writer, ++frame_serial_num, oome_serial_num, _oome_constructor, 0);
2308 depth++;
2309 }
2310
2311 for (int j = 0; j < _frames->length(); j++) {
2312 StackFrameInfo* frame = _frames->at(j);
2313 Method* m = frame->method();
2314 int class_serial_num = klass_map->find(m->method_holder());
2315 // the class serial number starts from 1
2316 assert(class_serial_num > 0, "class not found");
2317 DumperSupport::dump_stack_frame(writer, ++frame_serial_num, class_serial_num, m, frame->bci());
2318 }
2319
2320 // write HPROF_TRACE record for the thread
2321 DumperSupport::write_header(writer, HPROF_TRACE, checked_cast<u4>(3 * sizeof(u4) + depth * oopSize));
2322 writer->write_u4(stack_trace_serial_num()); // stack trace serial number
2323 writer->write_u4(thread_serial_num()); // thread serial number
2324 writer->write_u4((u4)depth); // frame count (including oom frame)
2325 for (int j = 1; j <= depth; j++) {
2326 writer->write_id(_start_frame_serial_num + j);
2327 }
2328 }
2329
2330 void ThreadDumper::dump_thread_obj(AbstractDumpWriter * writer) {
2331 assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_num is not initialized");
2332
2333 u4 size = 1 + sizeof(address) + 4 + 4;
2334 writer->start_sub_record(HPROF_GC_ROOT_THREAD_OBJ, size);
2335 writer->write_objectID(_thread_oop);
2336 writer->write_u4(thread_serial_num()); // thread serial number
2337 writer->write_u4(stack_trace_serial_num()); // stack trace serial number
2338 writer->end_sub_record();
2339 }
2340
2341 void ThreadDumper::dump_stack_refs(AbstractDumpWriter * writer) {
2342 assert(_thread_serial_num != 0 && _start_frame_serial_num != 0, "serial_num is not initialized");
2343
2344 JNILocalsDumper blk(writer, thread_serial_num());
2345 if (_thread_type == ThreadType::Platform) {
2346 if (!_java_thread->has_last_Java_frame()) {
2347 // no last java frame but there may be JNI locals
2348 _java_thread->active_handles()->oops_do(&blk);
2349 return;
2350 }
2351 }
2352
2353 JavaStackRefDumper java_ref_dumper(writer, thread_serial_num());
2354
2355 // vframes are resource allocated
2356 Thread* current_thread = Thread::current();
2357 ResourceMark rm(current_thread);
2358 HandleMark hm(current_thread);
2359
2360 bool stopAtVthreadEntry = _thread_type == ThreadType::MountedVirtual;
2361 frame* last_entry_frame = nullptr;
2362 bool is_top_frame = true;
2363 int depth = 0;
2364 if (oom_thread()) {
2365 depth++;
2366 }
2367
2368 for (vframe* vf = get_top_frame(); vf != nullptr; vf = vf->sender()) {
2369 if (stopAtVthreadEntry && vf->is_vthread_entry()) {
2370 break;
2371 }
2372
2373 if (vf->is_java_frame()) {
2374 javaVFrame* jvf = javaVFrame::cast(vf);
2375 if (!(jvf->method()->is_native())) {
2376 java_ref_dumper.set_frame_number(depth);
2377 java_ref_dumper.dump_java_stack_refs(jvf->locals());
2378 java_ref_dumper.dump_java_stack_refs(jvf->expressions());
2379 } else {
2380 // native frame
2381 blk.set_frame_number(depth);
2382 if (is_top_frame) {
2383 // JNI locals for the top frame if mounted
2384 assert(_java_thread != nullptr || jvf->method()->is_synchronized()
2385 || jvf->method()->is_object_wait0(), "impossible for unmounted vthread");
2386 if (_java_thread != nullptr) {
2387 _java_thread->active_handles()->oops_do(&blk);
2388 }
2389 } else {
2390 if (last_entry_frame != nullptr) {
2391 // JNI locals for the entry frame
2392 assert(last_entry_frame->is_entry_frame(), "checking");
2393 last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(&blk);
2394 }
2395 }
2396 }
2397 last_entry_frame = nullptr;
2398 // increment only for Java frames
2399 depth++;
2400 } else {
2401 // externalVFrame - for an entry frame then we report the JNI locals
2402 // when we find the corresponding javaVFrame
2403 frame* fr = vf->frame_pointer();
2404 assert(fr != nullptr, "sanity check");
2405 if (fr->is_entry_frame()) {
2406 last_entry_frame = fr;
2407 }
2408 }
2409 is_top_frame = false;
2410 }
2411 assert(depth == frame_count(), "total number of Java frames not matched");
2412 }
2413
2414 vframe* ThreadDumper::get_top_frame() const {
2415 if (_thread_type == ThreadType::UnmountedVirtual) {
2416 ContinuationWrapper cont(java_lang_VirtualThread::continuation(_thread_oop));
2417 if (cont.is_empty()) {
2418 return nullptr;
2419 }
2420 assert(!cont.is_mounted(), "sanity check");
2421 stackChunkOop chunk = cont.last_nonempty_chunk();
2422 if (chunk == nullptr || chunk->is_empty()) {
2423 return nullptr;
2424 }
2425
2426 RegisterMap reg_map(cont.continuation(), RegisterMap::UpdateMap::include);
2427 frame fr = chunk->top_frame(®_map);
2428 vframe* vf = vframe::new_vframe(&fr, ®_map, nullptr); // don't need JavaThread
2429 return vf;
2430 }
2431
2432 RegisterMap reg_map(_java_thread,
2433 RegisterMap::UpdateMap::include,
2434 RegisterMap::ProcessFrames::include,
2435 RegisterMap::WalkContinuation::skip);
2436 switch (_thread_type) {
2437 case ThreadType::Platform:
2438 if (!_java_thread->has_last_Java_frame()) {
2439 return nullptr;
2440 }
2441 return _java_thread->is_vthread_mounted()
2442 ? _java_thread->carrier_last_java_vframe(®_map)
2443 : _java_thread->platform_thread_last_java_vframe(®_map);
2444
2445 case ThreadType::MountedVirtual:
2446 return _java_thread->last_java_vframe(®_map);
2447
2448 default: // make compilers happy
2449 break;
2450 }
2451 ShouldNotReachHere();
2452 return nullptr;
2453 }
2454
2455 // Callback to dump thread-related data for unmounted virtual threads;
2456 // implemented by VM_HeapDumper.
2457 class UnmountedVThreadDumper {
2458 public:
2459 virtual void dump_vthread(oop vt, AbstractDumpWriter* segment_writer) = 0;
2460 };
2461
2462 // Support class used when iterating over the heap.
2463 class HeapObjectDumper : public ObjectClosure {
2464 private:
2465 AbstractDumpWriter* _writer;
2466 AbstractDumpWriter* writer() { return _writer; }
2467 UnmountedVThreadDumper* _vthread_dumper;
2468
2469 DumperClassCacheTable _class_cache;
2470
2471 public:
2472 HeapObjectDumper(AbstractDumpWriter* writer, UnmountedVThreadDumper* vthread_dumper)
2473 : _writer(writer), _vthread_dumper(vthread_dumper) {}
2474
2475 // called for each object in the heap
2476 void do_object(oop o);
2477 };
2478
2479 void HeapObjectDumper::do_object(oop o) {
2480 // skip classes as these emitted as HPROF_GC_CLASS_DUMP records
2481 if (o->klass() == vmClasses::Class_klass()) {
2482 if (!java_lang_Class::is_primitive(o)) {
2483 return;
2484 }
2485 }
2486
2487 if (DumperSupport::mask_dormant_archived_object(o, nullptr) == nullptr) {
2488 return;
2489 }
2490
2491 if (o->is_instance()) {
2492 // create a HPROF_GC_INSTANCE record for each object
2493 DumperSupport::dump_instance(writer(), o, &_class_cache);
2494 // If we encounter an unmounted virtual thread it needs to be dumped explicitly
2495 // (mounted virtual threads are dumped with their carriers).
2496 if (java_lang_VirtualThread::is_instance(o)
2497 && ThreadDumper::should_dump_vthread(o) && !ThreadDumper::is_vthread_mounted(o)) {
2498 _vthread_dumper->dump_vthread(o, writer());
2499 }
2500 } else if (o->is_objArray()) {
2501 // create a HPROF_GC_OBJ_ARRAY_DUMP record for each object array
2502 DumperSupport::dump_object_array(writer(), objArrayOop(o));
2503 } else if (o->is_flatArray()) {
2504 DumperSupport::dump_flat_array(writer(), flatArrayOop(o), &_class_cache);
2505 } else if (o->is_typeArray()) {
2506 // create a HPROF_GC_PRIM_ARRAY_DUMP record for each type array
2507 DumperSupport::dump_prim_array(writer(), typeArrayOop(o));
2508 }
2509 }
2510
2511 // The dumper controller for parallel heap dump
2512 class DumperController : public CHeapObj<mtInternal> {
2513 private:
2514 Monitor* _lock;
2515 Mutex* _global_writer_lock;
2516
2517 const uint _dumper_number;
2518 uint _complete_number;
2519
2520 bool _started; // VM dumper started and acquired global writer lock
2521
2522 public:
2523 DumperController(uint number) :
2524 // _lock and _global_writer_lock are used for synchronization between GC worker threads inside safepoint,
2525 // so we lock with _no_safepoint_check_flag.
2526 // signal_start() acquires _lock when global writer is locked,
2527 // its rank must be less than _global_writer_lock rank.
2528 _lock(new (std::nothrow) PaddedMonitor(Mutex::nosafepoint - 1, "DumperController_lock")),
2529 _global_writer_lock(new (std::nothrow) Mutex(Mutex::nosafepoint, "DumpWriter_lock")),
2530 _dumper_number(number),
2531 _complete_number(0),
2532 _started(false)
2533 {}
2534
2535 ~DumperController() {
2536 delete _lock;
2537 delete _global_writer_lock;
2538 }
2539
2540 // parallel (non VM) dumpers must wait until VM dumper acquires global writer lock
2541 void wait_for_start_signal() {
2542 MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2543 while (_started == false) {
2544 ml.wait();
2545 }
2546 }
2547
2548 void signal_start() {
2549 MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2550 _started = true;
2551 ml.notify_all();
2552 }
2553
2554 void lock_global_writer() {
2555 _global_writer_lock->lock_without_safepoint_check();
2556 }
2557
2558 void unlock_global_writer() {
2559 _global_writer_lock->unlock();
2560 }
2561
2562 void dumper_complete(DumpWriter* local_writer, DumpWriter* global_writer) {
2563 MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2564 _complete_number++;
2565 // propagate local error to global if any
2566 if (local_writer->has_error()) {
2567 global_writer->set_error(local_writer->error());
2568 }
2569 ml.notify();
2570 }
2571
2572 void wait_all_dumpers_complete() {
2573 MonitorLocker ml(_lock, Mutex::_no_safepoint_check_flag);
2574 while (_complete_number != _dumper_number) {
2575 ml.wait();
2576 }
2577 }
2578 };
2579
2580 // DumpMerger merges separate dump files into a complete one
2581 class DumpMerger : public StackObj {
2582 private:
2583 DumpWriter* _writer;
2584 InlinedObjects* _inlined_objects;
2585 const char* _path;
2586 bool _has_error;
2587 int _dump_seq;
2588
2589 private:
2590 void merge_file(const char* path);
2591 void merge_done();
2592 void set_error(const char* msg);
2593
2594 public:
2595 DumpMerger(const char* path, DumpWriter* writer, InlinedObjects* inlined_objects, int dump_seq) :
2596 _writer(writer),
2597 _inlined_objects(inlined_objects),
2598 _path(path),
2599 _has_error(_writer->has_error()),
2600 _dump_seq(dump_seq) {}
2601
2602 void do_merge();
2603
2604 // returns path for the parallel DumpWriter (resource allocated)
2605 static char* get_writer_path(const char* base_path, int seq);
2606
2607 };
2608
2609 char* DumpMerger::get_writer_path(const char* base_path, int seq) {
2610 // approximate required buffer size
2611 size_t buf_size = strlen(base_path)
2612 + 2 // ".p"
2613 + 10 // number (that's enough for 2^32 parallel dumpers)
2614 + 1; // '\0'
2615
2616 char* path = NEW_RESOURCE_ARRAY(char, buf_size);
2617 memset(path, 0, buf_size);
2618
2619 os::snprintf_checked(path, buf_size, "%s.p%d", base_path, seq);
2620
2621 return path;
2622 }
2623
2624
2625 void DumpMerger::merge_done() {
2626 // Writes the HPROF_HEAP_DUMP_END record.
2627 if (!_has_error) {
2628 DumperSupport::end_of_dump(_writer);
2629 _inlined_objects->dump_flat_arrays(_writer);
2630 _writer->flush();
2631 _inlined_objects->release();
2632 }
2633 _dump_seq = 0; //reset
2634 }
2635
2636 void DumpMerger::set_error(const char* msg) {
2637 assert(msg != nullptr, "sanity check");
2638 log_error(heapdump)("%s (file: %s)", msg, _path);
2639 _writer->set_error(msg);
2640 _has_error = true;
2641 }
2642
2643 #ifdef LINUX
2644 // Merge segmented heap files via sendfile, it's more efficient than the
2645 // read+write combination, which would require transferring data to and from
2646 // user space.
2647 void DumpMerger::merge_file(const char* path) {
2648 TraceTime timer("Merge segmented heap file directly", TRACETIME_LOG(Info, heapdump));
2649
2650 int segment_fd = os::open(path, O_RDONLY, 0);
2651 if (segment_fd == -1) {
2652 set_error("Can not open segmented heap file during merging");
2653 return;
2654 }
2655
2656 struct stat st;
2657 if (os::stat(path, &st) != 0) {
2658 ::close(segment_fd);
2659 set_error("Can not get segmented heap file size during merging");
2660 return;
2661 }
2662
2663 // A successful call to sendfile may write fewer bytes than requested; the
2664 // caller should be prepared to retry the call if there were unsent bytes.
2665 jlong offset = 0;
2666 while (offset < st.st_size) {
2667 int ret = os::Linux::sendfile(_writer->get_fd(), segment_fd, &offset, st.st_size);
2668 if (ret == -1) {
2669 ::close(segment_fd);
2670 set_error("Failed to merge segmented heap file");
2671 return;
2672 }
2673 }
2674
2675 // As sendfile variant does not call the write method of the global writer,
2676 // bytes_written is also incorrect for this variant, we need to explicitly
2677 // accumulate bytes_written for the global writer in this case
2678 julong accum = _writer->bytes_written() + st.st_size;
2679 _writer->set_bytes_written(accum);
2680 ::close(segment_fd);
2681 }
2682 #else
2683 // Generic implementation using read+write
2684 void DumpMerger::merge_file(const char* path) {
2685 TraceTime timer("Merge segmented heap file", TRACETIME_LOG(Info, heapdump));
2686
2687 fileStream segment_fs(path, "rb");
2688 if (!segment_fs.is_open()) {
2689 set_error("Can not open segmented heap file during merging");
2690 return;
2691 }
2692
2693 jlong total = 0;
2694 size_t cnt = 0;
2695
2696 // Use _writer buffer for reading.
2697 while ((cnt = segment_fs.read(_writer->buffer(), 1, _writer->buffer_size())) != 0) {
2698 _writer->set_position(cnt);
2699 _writer->flush();
2700 total += cnt;
2701 }
2702
2703 if (segment_fs.fileSize() != total) {
2704 set_error("Merged heap dump is incomplete");
2705 }
2706 }
2707 #endif
2708
2709 void DumpMerger::do_merge() {
2710 TraceTime timer("Merge heap files complete", TRACETIME_LOG(Info, heapdump));
2711
2712 // Since contents in segmented heap file were already zipped, we don't need to zip
2713 // them again during merging.
2714 AbstractCompressor* saved_compressor = _writer->compressor();
2715 _writer->set_compressor(nullptr);
2716
2717 // Merge the content of the remaining files into base file. Regardless of whether
2718 // the merge process is successful or not, these segmented files will be deleted.
2719 for (int i = 0; i < _dump_seq; i++) {
2720 ResourceMark rm;
2721 const char* path = get_writer_path(_path, i);
2722 if (!_has_error) {
2723 merge_file(path);
2724 }
2725 // Delete selected segmented heap file nevertheless
2726 if (remove(path) != 0) {
2727 log_info(heapdump)("Removal of segment file (%d) failed (%d)", i, errno);
2728 }
2729 }
2730
2731 // restore compressor for further use
2732 _writer->set_compressor(saved_compressor);
2733 merge_done();
2734 }
2735
2736 // The VM operation that performs the heap dump
2737 class VM_HeapDumper : public VM_GC_Operation, public WorkerTask, public UnmountedVThreadDumper {
2738 private:
2739 DumpWriter* _writer;
2740 JavaThread* _oome_thread;
2741 Method* _oome_constructor;
2742 bool _gc_before_heap_dump;
2743 GrowableArray<Klass*>* _klass_map;
2744
2745 ThreadDumper** _thread_dumpers; // platform, carrier and mounted virtual threads
2746 int _thread_dumpers_count;
2747 volatile int _thread_serial_num;
2748 volatile int _frame_serial_num;
2749
2750 volatile int _dump_seq;
2751
2752 // Inlined object support.
2753 InlinedObjects _inlined_objects;
2754
2755 // parallel heap dump support
2756 uint _num_dumper_threads;
2757 DumperController* _dumper_controller;
2758 ParallelObjectIterator* _poi;
2759
2760 // Dumper id of VMDumper thread.
2761 static const int VMDumperId = 0;
2762 // VM dumper dumps both heap and non-heap data, other dumpers dump heap-only data.
2763 static bool is_vm_dumper(int dumper_id) { return dumper_id == VMDumperId; }
2764 // the 1st dumper calling get_next_dumper_id becomes VM dumper
2765 int get_next_dumper_id() {
2766 return AtomicAccess::fetch_then_add(&_dump_seq, 1);
2767 }
2768
2769 DumpWriter* writer() const { return _writer; }
2770
2771 bool skip_operation() const;
2772
2773 // HPROF_GC_ROOT_THREAD_OBJ records for platform and mounted virtual threads
2774 void dump_threads(AbstractDumpWriter* writer);
2775
2776 bool is_oom_thread(JavaThread* thread) const {
2777 return thread == _oome_thread && _oome_constructor != nullptr;
2778 }
2779
2780 // HPROF_TRACE and HPROF_FRAME records for platform and mounted virtual threads
2781 void dump_stack_traces(AbstractDumpWriter* writer);
2782
2783 public:
2784 VM_HeapDumper(DumpWriter* writer, bool gc_before_heap_dump, bool oome, uint num_dump_threads) :
2785 VM_GC_Operation(0 /* total collections, dummy, ignored */,
2786 GCCause::_heap_dump /* GC Cause */,
2787 0 /* total full collections, dummy, ignored */,
2788 gc_before_heap_dump),
2789 WorkerTask("dump heap") {
2790 _writer = writer;
2791 _gc_before_heap_dump = gc_before_heap_dump;
2792 _klass_map = new (mtServiceability) GrowableArray<Klass*>(INITIAL_CLASS_COUNT, mtServiceability);
2793
2794 _thread_dumpers = nullptr;
2795 _thread_dumpers_count = 0;
2796 _thread_serial_num = 1;
2797 _frame_serial_num = 1;
2798
2799 _dump_seq = VMDumperId;
2800 _num_dumper_threads = num_dump_threads;
2801 _dumper_controller = nullptr;
2802 _poi = nullptr;
2803 if (oome) {
2804 assert(!Thread::current()->is_VM_thread(), "Dump from OutOfMemoryError cannot be called by the VMThread");
2805 // get OutOfMemoryError zero-parameter constructor
2806 InstanceKlass* oome_ik = vmClasses::OutOfMemoryError_klass();
2807 _oome_constructor = oome_ik->find_method(vmSymbols::object_initializer_name(),
2808 vmSymbols::void_method_signature());
2809 // get thread throwing OOME when generating the heap dump at OOME
2810 _oome_thread = JavaThread::current();
2811 } else {
2812 _oome_thread = nullptr;
2813 _oome_constructor = nullptr;
2814 }
2815 }
2816
2817 ~VM_HeapDumper() {
2818 if (_thread_dumpers != nullptr) {
2819 for (int i = 0; i < _thread_dumpers_count; i++) {
2820 delete _thread_dumpers[i];
2821 }
2822 FREE_C_HEAP_ARRAY(ThreadDumper*, _thread_dumpers);
2823 }
2824
2825 if (_dumper_controller != nullptr) {
2826 delete _dumper_controller;
2827 _dumper_controller = nullptr;
2828 }
2829 delete _klass_map;
2830 }
2831 int dump_seq() { return _dump_seq; }
2832 bool is_parallel_dump() { return _num_dumper_threads > 1; }
2833 void prepare_parallel_dump(WorkerThreads* workers);
2834
2835 InlinedObjects* inlined_objects() { return &_inlined_objects; }
2836
2837 VMOp_Type type() const { return VMOp_HeapDumper; }
2838 virtual bool doit_prologue();
2839 void doit();
2840 void work(uint worker_id);
2841
2842 // UnmountedVThreadDumper implementation
2843 void dump_vthread(oop vt, AbstractDumpWriter* segment_writer);
2844 };
2845
2846 bool VM_HeapDumper::skip_operation() const {
2847 return false;
2848 }
2849
2850 // fixes up the current dump record and writes HPROF_HEAP_DUMP_END record
2851 void DumperSupport::end_of_dump(AbstractDumpWriter* writer) {
2852 writer->finish_dump_segment();
2853
2854 writer->write_u1(HPROF_HEAP_DUMP_END);
2855 writer->write_u4(0);
2856 writer->write_u4(0);
2857 }
2858
2859 // Write a HPROF_GC_ROOT_THREAD_OBJ record for platform/carrier and mounted virtual threads.
2860 // Then walk the stack so that locals and JNI locals are dumped.
2861 void VM_HeapDumper::dump_threads(AbstractDumpWriter* writer) {
2862 for (int i = 0; i < _thread_dumpers_count; i++) {
2863 _thread_dumpers[i]->dump_thread_obj(writer);
2864 _thread_dumpers[i]->dump_stack_refs(writer);
2865 }
2866 }
2867
2868 bool VM_HeapDumper::doit_prologue() {
2869 if (_gc_before_heap_dump && (UseZGC || UseShenandoahGC)) {
2870 // ZGC and Shenandoah cannot perform a synchronous GC cycle from within the VM thread.
2871 // So collect_as_vm_thread() is a noop. To respect the _gc_before_heap_dump flag a
2872 // synchronous GC cycle is performed from the caller thread in the prologue.
2873 Universe::heap()->collect(GCCause::_heap_dump);
2874 }
2875 return VM_GC_Operation::doit_prologue();
2876 }
2877
2878 void VM_HeapDumper::prepare_parallel_dump(WorkerThreads* workers) {
2879 uint num_active_workers = workers != nullptr ? workers->active_workers() : 0;
2880 uint num_requested_dump_threads = _num_dumper_threads;
2881 // check if we can dump in parallel based on requested and active threads
2882 if (num_active_workers <= 1 || num_requested_dump_threads <= 1) {
2883 _num_dumper_threads = 1;
2884 } else {
2885 _num_dumper_threads = clamp(num_requested_dump_threads, 2U, num_active_workers);
2886 }
2887 _dumper_controller = new (std::nothrow) DumperController(_num_dumper_threads);
2888 bool can_parallel = _num_dumper_threads > 1;
2889 log_info(heapdump)("Requested dump threads %u, active dump threads %u, "
2890 "actual dump threads %u, parallelism %s",
2891 num_requested_dump_threads, num_active_workers,
2892 _num_dumper_threads, can_parallel ? "true" : "false");
2893 }
2894
2895 // The VM operation that dumps the heap. The dump consists of the following
2896 // records:
2897 //
2898 // HPROF_HEADER
2899 // [HPROF_UTF8]*
2900 // [HPROF_LOAD_CLASS]*
2901 // [[HPROF_FRAME]*|HPROF_TRACE]*
2902 // [HPROF_GC_CLASS_DUMP]*
2903 // [HPROF_HEAP_DUMP_SEGMENT]*
2904 // HPROF_HEAP_DUMP_END
2905 //
2906 // The HPROF_TRACE records represent the stack traces where the heap dump
2907 // is generated and a "dummy trace" record which does not include
2908 // any frames. The dummy trace record is used to be referenced as the
2909 // unknown object alloc site.
2910 //
2911 // Each HPROF_HEAP_DUMP_SEGMENT record has a length followed by sub-records.
2912 // To allow the heap dump be generated in a single pass we remember the position
2913 // of the dump length and fix it up after all sub-records have been written.
2914 // To generate the sub-records we iterate over the heap, writing
2915 // HPROF_GC_INSTANCE_DUMP, HPROF_GC_OBJ_ARRAY_DUMP, and HPROF_GC_PRIM_ARRAY_DUMP
2916 // records as we go. Once that is done we write records for some of the GC
2917 // roots.
2918
2919 void VM_HeapDumper::doit() {
2920
2921 CollectedHeap* ch = Universe::heap();
2922
2923 ch->ensure_parsability(false); // must happen, even if collection does
2924 // not happen (e.g. due to GCLocker)
2925
2926 if (_gc_before_heap_dump) {
2927 if (GCLocker::is_active()) {
2928 warning("GC locker is held; pre-heapdump GC was skipped");
2929 } else {
2930 ch->collect_as_vm_thread(GCCause::_heap_dump);
2931 }
2932 }
2933
2934 WorkerThreads* workers = ch->safepoint_workers();
2935 prepare_parallel_dump(workers);
2936
2937 if (!is_parallel_dump()) {
2938 work(VMDumperId);
2939 } else {
2940 ParallelObjectIterator poi(_num_dumper_threads);
2941 _poi = &poi;
2942 workers->run_task(this, _num_dumper_threads);
2943 _poi = nullptr;
2944 }
2945 }
2946
2947 void VM_HeapDumper::work(uint worker_id) {
2948 // VM Dumper works on all non-heap data dumping and part of heap iteration.
2949 int dumper_id = get_next_dumper_id();
2950
2951 if (is_vm_dumper(dumper_id)) {
2952 // lock global writer, it will be unlocked after VM Dumper finishes with non-heap data
2953 _dumper_controller->lock_global_writer();
2954 _dumper_controller->signal_start();
2955 } else {
2956 _dumper_controller->wait_for_start_signal();
2957 }
2958
2959 if (is_vm_dumper(dumper_id)) {
2960 TraceTime timer("Dump non-objects", TRACETIME_LOG(Info, heapdump));
2961 // Write the file header - we always use 1.0.2
2962 const char* header = "JAVA PROFILE 1.0.2";
2963
2964 // header is few bytes long - no chance to overflow int
2965 writer()->write_raw(header, strlen(header) + 1); // NUL terminated
2966 writer()->write_u4(oopSize);
2967 // timestamp is current time in ms
2968 writer()->write_u8(os::javaTimeMillis());
2969 // HPROF_UTF8 records
2970 SymbolTableDumper sym_dumper(writer());
2971 SymbolTable::symbols_do(&sym_dumper);
2972
2973 // HPROF_UTF8 records for inlined field names.
2974 inlined_objects()->init();
2975 inlined_objects()->dump_inlined_field_names(writer());
2976
2977 // HPROF_INLINED_FIELDS
2978 inlined_objects()->dump_classed_with_inlined_fields(writer());
2979
2980 // write HPROF_LOAD_CLASS records
2981 {
2982 LoadedClassDumper loaded_class_dumper(writer(), _klass_map);
2983 ClassLoaderDataGraph::classes_do(&loaded_class_dumper);
2984 }
2985
2986 // write HPROF_FRAME and HPROF_TRACE records
2987 // this must be called after _klass_map is built when iterating the classes above.
2988 dump_stack_traces(writer());
2989
2990 // unlock global writer, so parallel dumpers can dump stack traces of unmounted virtual threads
2991 _dumper_controller->unlock_global_writer();
2992 }
2993
2994 // HPROF_HEAP_DUMP/HPROF_HEAP_DUMP_SEGMENT starts here
2995
2996 ResourceMark rm;
2997 // share global compressor, local DumpWriter is not responsible for its life cycle
2998 DumpWriter segment_writer(DumpMerger::get_writer_path(writer()->get_file_path(), dumper_id),
2999 writer()->is_overwrite(), writer()->compressor());
3000 if (!segment_writer.has_error()) {
3001 if (is_vm_dumper(dumper_id)) {
3002 // dump some non-heap subrecords to heap dump segment
3003 TraceTime timer("Dump non-objects (part 2)", TRACETIME_LOG(Info, heapdump));
3004 // Writes HPROF_GC_CLASS_DUMP records
3005 ClassDumper class_dumper(&segment_writer);
3006 ClassLoaderDataGraph::classes_do(&class_dumper);
3007
3008 // HPROF_GC_ROOT_THREAD_OBJ + frames + jni locals
3009 dump_threads(&segment_writer);
3010
3011 // HPROF_GC_ROOT_JNI_GLOBAL
3012 JNIGlobalsDumper jni_dumper(&segment_writer);
3013 JNIHandles::oops_do(&jni_dumper);
3014 // technically not jni roots, but global roots
3015 // for things like preallocated throwable backtraces
3016 Universe::vm_global()->oops_do(&jni_dumper);
3017 // HPROF_GC_ROOT_STICKY_CLASS
3018 // These should be classes in the null class loader data, and not all classes
3019 // if !ClassUnloading
3020 StickyClassDumper stiky_class_dumper(&segment_writer);
3021 ClassLoaderData::the_null_class_loader_data()->classes_do(&stiky_class_dumper);
3022 }
3023
3024 // Heap iteration.
3025 // writes HPROF_GC_INSTANCE_DUMP records.
3026 // After each sub-record is written check_segment_length will be invoked
3027 // to check if the current segment exceeds a threshold. If so, a new
3028 // segment is started.
3029 // The HPROF_GC_CLASS_DUMP and HPROF_GC_INSTANCE_DUMP are the vast bulk
3030 // of the heap dump.
3031
3032 TraceTime timer(is_parallel_dump() ? "Dump heap objects in parallel" : "Dump heap objects", TRACETIME_LOG(Info, heapdump));
3033 HeapObjectDumper obj_dumper(&segment_writer, this);
3034 if (!is_parallel_dump()) {
3035 Universe::heap()->object_iterate(&obj_dumper);
3036 } else {
3037 // == Parallel dump
3038 _poi->object_iterate(&obj_dumper, worker_id);
3039 }
3040
3041 segment_writer.finish_dump_segment();
3042 segment_writer.flush();
3043 }
3044
3045 _dumper_controller->dumper_complete(&segment_writer, writer());
3046
3047 if (is_vm_dumper(dumper_id)) {
3048 _dumper_controller->wait_all_dumpers_complete();
3049
3050 // flush global writer
3051 writer()->flush();
3052
3053 // At this point, all fragments of the heapdump have been written to separate files.
3054 // We need to merge them into a complete heapdump and write HPROF_HEAP_DUMP_END at that time.
3055 }
3056 }
3057
3058 void VM_HeapDumper::dump_stack_traces(AbstractDumpWriter* writer) {
3059 // write a HPROF_TRACE record without any frames to be referenced as object alloc sites
3060 DumperSupport::write_header(writer, HPROF_TRACE, 3 * sizeof(u4));
3061 writer->write_u4((u4)STACK_TRACE_ID);
3062 writer->write_u4(0); // thread number
3063 writer->write_u4(0); // frame count
3064
3065 // max number if every platform thread is carrier with mounted virtual thread
3066 _thread_dumpers = NEW_C_HEAP_ARRAY(ThreadDumper*, Threads::number_of_threads() * 2, mtInternal);
3067
3068 for (JavaThreadIteratorWithHandle jtiwh; JavaThread * thread = jtiwh.next(); ) {
3069 if (ThreadDumper::should_dump_pthread(thread)) {
3070 bool add_oom_frame = is_oom_thread(thread);
3071
3072 oop mounted_vt = thread->is_vthread_mounted() ? thread->vthread() : nullptr;
3073 if (mounted_vt != nullptr && !ThreadDumper::should_dump_vthread(mounted_vt)) {
3074 mounted_vt = nullptr;
3075 }
3076
3077 // mounted vthread (if any)
3078 if (mounted_vt != nullptr) {
3079 ThreadDumper* thread_dumper = new ThreadDumper(ThreadDumper::ThreadType::MountedVirtual, thread, mounted_vt);
3080 _thread_dumpers[_thread_dumpers_count++] = thread_dumper;
3081 if (add_oom_frame) {
3082 thread_dumper->add_oom_frame(_oome_constructor);
3083 // we add oom frame to the VT stack, don't add it to the carrier thread stack
3084 add_oom_frame = false;
3085 }
3086 thread_dumper->init_serial_nums(&_thread_serial_num, &_frame_serial_num);
3087 thread_dumper->dump_stack_traces(writer, _klass_map);
3088 }
3089
3090 // platform or carrier thread
3091 ThreadDumper* thread_dumper = new ThreadDumper(ThreadDumper::ThreadType::Platform, thread, thread->threadObj());
3092 _thread_dumpers[_thread_dumpers_count++] = thread_dumper;
3093 if (add_oom_frame) {
3094 thread_dumper->add_oom_frame(_oome_constructor);
3095 }
3096 thread_dumper->init_serial_nums(&_thread_serial_num, &_frame_serial_num);
3097 thread_dumper->dump_stack_traces(writer, _klass_map);
3098 }
3099 }
3100 }
3101
3102 void VM_HeapDumper::dump_vthread(oop vt, AbstractDumpWriter* segment_writer) {
3103 // unmounted vthread has no JavaThread
3104 ThreadDumper thread_dumper(ThreadDumper::ThreadType::UnmountedVirtual, nullptr, vt);
3105 thread_dumper.init_serial_nums(&_thread_serial_num, &_frame_serial_num);
3106
3107 // write HPROF_TRACE/HPROF_FRAME records to global writer
3108 _dumper_controller->lock_global_writer();
3109 thread_dumper.dump_stack_traces(writer(), _klass_map);
3110 _dumper_controller->unlock_global_writer();
3111
3112 // write HPROF_GC_ROOT_THREAD_OBJ/HPROF_GC_ROOT_JAVA_FRAME/HPROF_GC_ROOT_JNI_LOCAL subrecord
3113 // to segment writer
3114 thread_dumper.dump_thread_obj(segment_writer);
3115 thread_dumper.dump_stack_refs(segment_writer);
3116 }
3117
3118 // dump the heap to given path.
3119 int HeapDumper::dump(const char* path, outputStream* out, int compression, bool overwrite, uint num_dump_threads) {
3120 assert(path != nullptr && strlen(path) > 0, "path missing");
3121
3122 // print message in interactive case
3123 if (out != nullptr) {
3124 out->print_cr("Dumping heap to %s ...", path);
3125 timer()->start();
3126 }
3127
3128 if (_oome && num_dump_threads > 1) {
3129 // Each additional parallel writer requires several MB of internal memory
3130 // (DumpWriter buffer, DumperClassCacheTable, GZipCompressor buffers).
3131 // For the OOM handling we may already be limited in memory.
3132 // Lets ensure we have at least 20MB per thread.
3133 physical_memory_size_type free_memory = 0;
3134 // Return value ignored - defaulting to 0 on failure.
3135 (void)os::free_memory(free_memory);
3136 julong max_threads = free_memory / (20 * M);
3137 if (num_dump_threads > max_threads) {
3138 num_dump_threads = MAX2<uint>(1, (uint)max_threads);
3139 }
3140 }
3141
3142 // create JFR event
3143 EventHeapDump event;
3144
3145 AbstractCompressor* compressor = nullptr;
3146
3147 if (compression > 0) {
3148 compressor = new (std::nothrow) GZipCompressor(compression);
3149
3150 if (compressor == nullptr) {
3151 set_error("Could not allocate gzip compressor");
3152 return -1;
3153 }
3154 }
3155
3156 DumpWriter writer(path, overwrite, compressor);
3157
3158 if (writer.error() != nullptr) {
3159 set_error(writer.error());
3160 if (out != nullptr) {
3161 out->print_cr("Unable to create %s: %s", path,
3162 (error() != nullptr) ? error() : "reason unknown");
3163 }
3164 return -1;
3165 }
3166
3167 // generate the segmented heap dump into separate files
3168 VM_HeapDumper dumper(&writer, _gc_before_heap_dump, _oome, num_dump_threads);
3169 VMThread::execute(&dumper);
3170
3171 // record any error that the writer may have encountered
3172 set_error(writer.error());
3173
3174 // Heap dump process is done in two phases
3175 //
3176 // Phase 1: Concurrent threads directly write heap data to multiple heap files.
3177 // This is done by VM_HeapDumper, which is performed within safepoint.
3178 //
3179 // Phase 2: Merge multiple heap files into one complete heap dump file.
3180 // This is done by DumpMerger, which is performed outside safepoint
3181
3182 DumpMerger merger(path, &writer, dumper.inlined_objects(), dumper.dump_seq());
3183 // Perform heapdump file merge operation in the current thread prevents us
3184 // from occupying the VM Thread, which in turn affects the occurrence of
3185 // GC and other VM operations.
3186 merger.do_merge();
3187 if (writer.error() != nullptr) {
3188 set_error(writer.error());
3189 }
3190
3191 // emit JFR event
3192 if (error() == nullptr) {
3193 event.set_destination(path);
3194 event.set_gcBeforeDump(_gc_before_heap_dump);
3195 event.set_size(writer.bytes_written());
3196 event.set_onOutOfMemoryError(_oome);
3197 event.set_overwrite(overwrite);
3198 event.set_compression(compression);
3199 event.commit();
3200 } else {
3201 log_debug(aot, heap)("Error %s while dumping heap", error());
3202 }
3203
3204 // print message in interactive case
3205 if (out != nullptr) {
3206 timer()->stop();
3207 if (error() == nullptr) {
3208 out->print_cr("Heap dump file created [" JULONG_FORMAT " bytes in %3.3f secs]",
3209 writer.bytes_written(), timer()->seconds());
3210 } else {
3211 out->print_cr("Dump file is incomplete: %s", writer.error());
3212 }
3213 }
3214
3215 if (compressor != nullptr) {
3216 delete compressor;
3217 }
3218 return (writer.error() == nullptr) ? 0 : -1;
3219 }
3220
3221 // stop timer (if still active), and free any error string we might be holding
3222 HeapDumper::~HeapDumper() {
3223 if (timer()->is_active()) {
3224 timer()->stop();
3225 }
3226 set_error(nullptr);
3227 }
3228
3229
3230 // returns the error string (resource allocated), or null
3231 char* HeapDumper::error_as_C_string() const {
3232 if (error() != nullptr) {
3233 char* str = ResourceArea::strdup(error());
3234 return str;
3235 } else {
3236 return nullptr;
3237 }
3238 }
3239
3240 // set the error string
3241 void HeapDumper::set_error(char const* error) {
3242 if (_error != nullptr) {
3243 os::free(_error);
3244 }
3245 if (error == nullptr) {
3246 _error = nullptr;
3247 } else {
3248 _error = os::strdup(error);
3249 assert(_error != nullptr, "allocation failure");
3250 }
3251 }
3252
3253 // Called by out-of-memory error reporting by a single Java thread
3254 // outside of a JVM safepoint
3255 void HeapDumper::dump_heap_from_oome() {
3256 HeapDumper::dump_heap(true);
3257 }
3258
3259 // Called by error reporting by a single Java thread outside of a JVM safepoint,
3260 // or by heap dumping by the VM thread during a (GC) safepoint. Thus, these various
3261 // callers are strictly serialized and guaranteed not to interfere below. For more
3262 // general use, however, this method will need modification to prevent
3263 // inteference when updating the static variables base_path and dump_file_seq below.
3264 void HeapDumper::dump_heap() {
3265 HeapDumper::dump_heap(false);
3266 }
3267
3268 void HeapDumper::dump_heap(bool oome) {
3269 static char base_path[JVM_MAXPATHLEN] = {'\0'};
3270 static uint dump_file_seq = 0;
3271 char my_path[JVM_MAXPATHLEN];
3272 const int max_digit_chars = 20;
3273 const char* dump_file_name = HeapDumpGzipLevel > 0 ? "java_pid%p.hprof.gz" : "java_pid%p.hprof";
3274
3275 // The dump file defaults to java_pid<pid>.hprof in the current working
3276 // directory. HeapDumpPath=<file> can be used to specify an alternative
3277 // dump file name or a directory where dump file is created.
3278 if (dump_file_seq == 0) { // first time in, we initialize base_path
3279 // Set base path (name or directory, default or custom, without seq no), doing %p substitution.
3280 const char *path_src = (HeapDumpPath != nullptr && HeapDumpPath[0] != '\0') ? HeapDumpPath : dump_file_name;
3281 if (!Arguments::copy_expand_pid(path_src, strlen(path_src), base_path, JVM_MAXPATHLEN - max_digit_chars)) {
3282 warning("Cannot create heap dump file. HeapDumpPath is too long.");
3283 return;
3284 }
3285 // Check if the path is an existing directory
3286 DIR* dir = os::opendir(base_path);
3287 if (dir != nullptr) {
3288 os::closedir(dir);
3289 // Path is a directory. Append a file separator (if needed).
3290 size_t fs_len = strlen(os::file_separator());
3291 if (strlen(base_path) >= fs_len) {
3292 char* end = base_path;
3293 end += (strlen(base_path) - fs_len);
3294 if (strcmp(end, os::file_separator()) != 0) {
3295 strcat(base_path, os::file_separator());
3296 }
3297 }
3298 // Then add the default name, with %p substitution. Use my_path temporarily.
3299 if (!Arguments::copy_expand_pid(dump_file_name, strlen(dump_file_name), my_path, JVM_MAXPATHLEN - max_digit_chars)) {
3300 warning("Cannot create heap dump file. HeapDumpPath is too long.");
3301 return;
3302 }
3303 const size_t dlen = strlen(base_path);
3304 jio_snprintf(&base_path[dlen], sizeof(base_path) - dlen, "%s", my_path);
3305 }
3306 strncpy(my_path, base_path, JVM_MAXPATHLEN);
3307 } else {
3308 // Append a sequence number id for dumps following the first
3309 const size_t len = strlen(base_path) + max_digit_chars + 2; // for '.' and \0
3310 jio_snprintf(my_path, len, "%s.%d", base_path, dump_file_seq);
3311 }
3312 dump_file_seq++; // increment seq number for next time we dump
3313
3314 HeapDumper dumper(false /* no GC before heap dump */,
3315 oome /* pass along out-of-memory-error flag */);
3316 dumper.dump(my_path, tty, HeapDumpGzipLevel);
3317 }