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