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