1 /* 2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "code/codeCache.hpp" 29 #include "compiler/compileBroker.hpp" 30 #include "gc/shared/collectedHeap.hpp" 31 #include "gc/shared/isGCActiveMark.hpp" 32 #include "logging/log.hpp" 33 #include "logging/logStream.hpp" 34 #include "logging/logConfiguration.hpp" 35 #include "memory/heapInspection.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "memory/universe.hpp" 38 #include "oops/symbol.hpp" 39 #include "runtime/arguments.hpp" 40 #include "runtime/deoptimization.hpp" 41 #include "runtime/frame.inline.hpp" 42 #include "runtime/interfaceSupport.inline.hpp" 43 #include "runtime/sweeper.hpp" 44 #include "runtime/synchronizer.hpp" 45 #include "runtime/thread.inline.hpp" 46 #include "runtime/threadSMR.inline.hpp" 47 #include "runtime/vmOperations.hpp" 48 #include "services/threadService.hpp" 49 50 #define VM_OP_NAME_INITIALIZE(name) #name, 51 52 const char* VM_Operation::_names[VM_Operation::VMOp_Terminating] = \ 53 { VM_OPS_DO(VM_OP_NAME_INITIALIZE) }; 54 55 void VM_Operation::set_calling_thread(Thread* thread, ThreadPriority priority) { 56 _calling_thread = thread; 57 assert(MinPriority <= priority && priority <= MaxPriority, "sanity check"); 58 _priority = priority; 59 } 60 61 62 void VM_Operation::evaluate() { 63 ResourceMark rm; 64 LogTarget(Debug, vmoperation) lt; 65 if (lt.is_enabled()) { 66 LogStream ls(lt); 67 ls.print("begin "); 68 print_on_error(&ls); 69 ls.cr(); 70 } 71 doit(); 72 if (lt.is_enabled()) { 73 LogStream ls(lt); 74 ls.print("end "); 75 print_on_error(&ls); 76 ls.cr(); 77 } 78 } 79 80 const char* VM_Operation::mode_to_string(Mode mode) { 81 switch(mode) { 82 case _safepoint : return "safepoint"; 83 case _no_safepoint : return "no safepoint"; 84 case _concurrent : return "concurrent"; 85 case _async_safepoint: return "async safepoint"; 86 default : return "unknown"; 87 } 88 } 89 // Called by fatal error handler. 90 void VM_Operation::print_on_error(outputStream* st) const { 91 st->print("VM_Operation (" PTR_FORMAT "): ", p2i(this)); 92 st->print("%s", name()); 93 94 const char* mode = mode_to_string(evaluation_mode()); 95 st->print(", mode: %s", mode); 96 97 if (calling_thread()) { 98 st->print(", requested by thread " PTR_FORMAT, p2i(calling_thread())); 99 } 100 } 101 102 void VM_ThreadStop::doit() { 103 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); 104 ThreadsListHandle tlh; 105 JavaThread* target = java_lang_Thread::thread(target_thread()); 106 // Note that this now allows multiple ThreadDeath exceptions to be 107 // thrown at a thread. 108 if (target != NULL && (!EnableThreadSMRExtraValidityChecks || tlh.includes(target))) { 109 // The target thread has run and has not exited yet. 110 target->send_thread_stop(throwable()); 111 } 112 } 113 114 void VM_ClearICs::doit() { 115 if (_preserve_static_stubs) { 116 CodeCache::cleanup_inline_caches(); 117 } else { 118 CodeCache::clear_inline_caches(); 119 } 120 } 121 122 void VM_Deoptimize::doit() { 123 // We do not want any GCs to happen while we are in the middle of this VM operation 124 ResourceMark rm; 125 DeoptimizationMarker dm; 126 127 // Deoptimize all activations depending on marked nmethods 128 Deoptimization::deoptimize_dependents(); 129 130 // Make the dependent methods not entrant 131 CodeCache::make_marked_nmethods_not_entrant(); 132 } 133 134 void VM_MarkActiveNMethods::doit() { 135 NMethodSweeper::mark_active_nmethods(); 136 } 137 138 VM_DeoptimizeFrame::VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason) { 139 _thread = thread; 140 _id = id; 141 _reason = reason; 142 } 143 144 145 void VM_DeoptimizeFrame::doit() { 146 assert(_reason > Deoptimization::Reason_none && _reason < Deoptimization::Reason_LIMIT, "invalid deopt reason"); 147 Deoptimization::deoptimize_frame_internal(_thread, _id, (Deoptimization::DeoptReason)_reason); 148 } 149 150 151 #ifndef PRODUCT 152 153 void VM_DeoptimizeAll::doit() { 154 DeoptimizationMarker dm; 155 JavaThreadIteratorWithHandle jtiwh; 156 // deoptimize all java threads in the system 157 if (DeoptimizeALot) { 158 for (; JavaThread *thread = jtiwh.next(); ) { 159 if (thread->has_last_Java_frame()) { 160 thread->deoptimize(); 161 } 162 } 163 } else if (DeoptimizeRandom) { 164 165 // Deoptimize some selected threads and frames 166 int tnum = os::random() & 0x3; 167 int fnum = os::random() & 0x3; 168 int tcount = 0; 169 for (; JavaThread *thread = jtiwh.next(); ) { 170 if (thread->has_last_Java_frame()) { 171 if (tcount++ == tnum) { 172 tcount = 0; 173 int fcount = 0; 174 // Deoptimize some selected frames. 175 // Biased llocking wants a updated register map 176 for(StackFrameStream fst(thread, UseBiasedLocking); !fst.is_done(); fst.next()) { 177 if (fst.current()->can_be_deoptimized()) { 178 if (fcount++ == fnum) { 179 fcount = 0; 180 Deoptimization::deoptimize(thread, *fst.current(), fst.register_map()); 181 } 182 } 183 } 184 } 185 } 186 } 187 } 188 } 189 190 191 void VM_ZombieAll::doit() { 192 JavaThread *thread = (JavaThread *)calling_thread(); 193 assert(thread->is_Java_thread(), "must be a Java thread"); 194 thread->make_zombies(); 195 } 196 197 #endif // !PRODUCT 198 199 void VM_Verify::doit() { 200 Universe::heap()->prepare_for_verify(); 201 Universe::verify(); 202 } 203 204 bool VM_PrintThreads::doit_prologue() { 205 // Get Heap_lock if concurrent locks will be dumped 206 if (_print_concurrent_locks) { 207 Heap_lock->lock(); 208 } 209 return true; 210 } 211 212 void VM_PrintThreads::doit() { 213 Threads::print_on(_out, true, false, _print_concurrent_locks, _print_extended_info); 214 } 215 216 void VM_PrintThreads::doit_epilogue() { 217 if (_print_concurrent_locks) { 218 // Release Heap_lock 219 Heap_lock->unlock(); 220 } 221 } 222 223 void VM_PrintJNI::doit() { 224 JNIHandles::print_on(_out); 225 } 226 227 void VM_PrintMetadata::doit() { 228 MetaspaceUtils::print_report(_out, _scale, _flags); 229 } 230 231 VM_FindDeadlocks::~VM_FindDeadlocks() { 232 if (_deadlocks != NULL) { 233 DeadlockCycle* cycle = _deadlocks; 234 while (cycle != NULL) { 235 DeadlockCycle* d = cycle; 236 cycle = cycle->next(); 237 delete d; 238 } 239 } 240 } 241 242 void VM_FindDeadlocks::doit() { 243 // Update the hazard ptr in the originating thread to the current 244 // list of threads. This VM operation needs the current list of 245 // threads for proper deadlock detection and those are the 246 // JavaThreads we need to be protected when we return info to the 247 // originating thread. 248 _setter.set(); 249 250 _deadlocks = ThreadService::find_deadlocks_at_safepoint(_setter.list(), _concurrent_locks); 251 if (_out != NULL) { 252 int num_deadlocks = 0; 253 for (DeadlockCycle* cycle = _deadlocks; cycle != NULL; cycle = cycle->next()) { 254 num_deadlocks++; 255 cycle->print_on_with(_setter.list(), _out); 256 } 257 258 if (num_deadlocks == 1) { 259 _out->print_cr("\nFound 1 deadlock.\n"); 260 _out->flush(); 261 } else if (num_deadlocks > 1) { 262 _out->print_cr("\nFound %d deadlocks.\n", num_deadlocks); 263 _out->flush(); 264 } 265 } 266 } 267 268 VM_ThreadDump::VM_ThreadDump(ThreadDumpResult* result, 269 int max_depth, 270 bool with_locked_monitors, 271 bool with_locked_synchronizers) { 272 _result = result; 273 _num_threads = 0; // 0 indicates all threads 274 _threads = NULL; 275 _result = result; 276 _max_depth = max_depth; 277 _with_locked_monitors = with_locked_monitors; 278 _with_locked_synchronizers = with_locked_synchronizers; 279 } 280 281 VM_ThreadDump::VM_ThreadDump(ThreadDumpResult* result, 282 GrowableArray<instanceHandle>* threads, 283 int num_threads, 284 int max_depth, 285 bool with_locked_monitors, 286 bool with_locked_synchronizers) { 287 _result = result; 288 _num_threads = num_threads; 289 _threads = threads; 290 _result = result; 291 _max_depth = max_depth; 292 _with_locked_monitors = with_locked_monitors; 293 _with_locked_synchronizers = with_locked_synchronizers; 294 } 295 296 bool VM_ThreadDump::doit_prologue() { 297 if (_with_locked_synchronizers) { 298 // Acquire Heap_lock to dump concurrent locks 299 Heap_lock->lock(); 300 } 301 302 return true; 303 } 304 305 void VM_ThreadDump::doit_epilogue() { 306 if (_with_locked_synchronizers) { 307 // Release Heap_lock 308 Heap_lock->unlock(); 309 } 310 } 311 312 void VM_ThreadDump::doit() { 313 ResourceMark rm; 314 315 // Set the hazard ptr in the originating thread to protect the 316 // current list of threads. This VM operation needs the current list 317 // of threads for a proper dump and those are the JavaThreads we need 318 // to be protected when we return info to the originating thread. 319 _result->set_t_list(); 320 321 ConcurrentLocksDump concurrent_locks(true); 322 if (_with_locked_synchronizers) { 323 concurrent_locks.dump_at_safepoint(); 324 } 325 326 if (_num_threads == 0) { 327 // Snapshot all live threads 328 329 for (uint i = 0; i < _result->t_list()->length(); i++) { 330 JavaThread* jt = _result->t_list()->thread_at(i); 331 if (jt->is_exiting() || 332 jt->is_hidden_from_external_view()) { 333 // skip terminating threads and hidden threads 334 continue; 335 } 336 ThreadConcurrentLocks* tcl = NULL; 337 if (_with_locked_synchronizers) { 338 tcl = concurrent_locks.thread_concurrent_locks(jt); 339 } 340 snapshot_thread(jt, tcl); 341 } 342 } else { 343 // Snapshot threads in the given _threads array 344 // A dummy snapshot is created if a thread doesn't exist 345 346 for (int i = 0; i < _num_threads; i++) { 347 instanceHandle th = _threads->at(i); 348 if (th() == NULL) { 349 // skip if the thread doesn't exist 350 // Add a dummy snapshot 351 _result->add_thread_snapshot(); 352 continue; 353 } 354 355 // Dump thread stack only if the thread is alive and not exiting 356 // and not VM internal thread. 357 JavaThread* jt = java_lang_Thread::thread(th()); 358 if (jt != NULL && !_result->t_list()->includes(jt)) { 359 // _threads[i] doesn't refer to a valid JavaThread; this check 360 // is primarily for JVM_DumpThreads() which doesn't have a good 361 // way to validate the _threads array. 362 jt = NULL; 363 } 364 if (jt == NULL || /* thread not alive */ 365 jt->is_exiting() || 366 jt->is_hidden_from_external_view()) { 367 // add a NULL snapshot if skipped 368 _result->add_thread_snapshot(); 369 continue; 370 } 371 ThreadConcurrentLocks* tcl = NULL; 372 if (_with_locked_synchronizers) { 373 tcl = concurrent_locks.thread_concurrent_locks(jt); 374 } 375 snapshot_thread(jt, tcl); 376 } 377 } 378 } 379 380 void VM_ThreadDump::snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl) { 381 ThreadSnapshot* snapshot = _result->add_thread_snapshot(java_thread); 382 snapshot->dump_stack_at_safepoint(_max_depth, _with_locked_monitors); 383 snapshot->set_concurrent_locks(tcl); 384 } 385 386 volatile bool VM_Exit::_vm_exited = false; 387 Thread * volatile VM_Exit::_shutdown_thread = NULL; 388 389 int VM_Exit::set_vm_exited() { 390 391 Thread * thr_cur = Thread::current(); 392 393 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already"); 394 395 int num_active = 0; 396 397 _shutdown_thread = thr_cur; 398 _vm_exited = true; // global flag 399 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) { 400 if (thr!=thr_cur && thr->thread_state() == _thread_in_native) { 401 ++num_active; 402 thr->set_terminated(JavaThread::_vm_exited); // per-thread flag 403 } 404 } 405 406 return num_active; 407 } 408 409 int VM_Exit::wait_for_threads_in_native_to_block() { 410 // VM exits at safepoint. This function must be called at the final safepoint 411 // to wait for threads in _thread_in_native state to be quiescent. 412 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already"); 413 414 Thread * thr_cur = Thread::current(); 415 Monitor timer(Mutex::leaf, "VM_Exit timer", true, 416 Monitor::_safepoint_check_never); 417 418 // Compiler threads need longer wait because they can access VM data directly 419 // while in native. If they are active and some structures being used are 420 // deleted by the shutdown sequence, they will crash. On the other hand, user 421 // threads must go through native=>Java/VM transitions first to access VM 422 // data, and they will be stopped during state transition. In theory, we 423 // don't have to wait for user threads to be quiescent, but it's always 424 // better to terminate VM when current thread is the only active thread, so 425 // wait for user threads too. Numbers are in 10 milliseconds. 426 int max_wait_user_thread = 30; // at least 300 milliseconds 427 int max_wait_compiler_thread = 1000; // at least 10 seconds 428 429 int max_wait = max_wait_compiler_thread; 430 431 int attempts = 0; 432 JavaThreadIteratorWithHandle jtiwh; 433 while (true) { 434 int num_active = 0; 435 int num_active_compiler_thread = 0; 436 437 jtiwh.rewind(); 438 for (; JavaThread *thr = jtiwh.next(); ) { 439 if (thr!=thr_cur && thr->thread_state() == _thread_in_native) { 440 num_active++; 441 if (thr->is_Compiler_thread()) { 442 #if INCLUDE_JVMCI 443 CompilerThread* ct = (CompilerThread*) thr; 444 if (ct->compiler() == NULL || !ct->compiler()->is_jvmci()) { 445 num_active_compiler_thread++; 446 } else { 447 // A JVMCI compiler thread never accesses VM data structures 448 // while in _thread_in_native state so there's no need to wait 449 // for it and potentially add a 300 millisecond delay to VM 450 // shutdown. 451 num_active--; 452 } 453 #else 454 num_active_compiler_thread++; 455 #endif 456 } 457 } 458 } 459 460 if (num_active == 0) { 461 return 0; 462 } else if (attempts > max_wait) { 463 return num_active; 464 } else if (num_active_compiler_thread == 0 && attempts > max_wait_user_thread) { 465 return num_active; 466 } 467 468 attempts++; 469 470 MonitorLocker ml(&timer, Mutex::_no_safepoint_check_flag); 471 ml.wait(10); 472 } 473 } 474 475 bool VM_Exit::doit_prologue() { 476 if (AsyncDeflateIdleMonitors && log_is_enabled(Info, monitorinflation)) { 477 // AsyncDeflateIdleMonitors does a special deflation at the VM_Exit 478 // safepoint in order to reduce the in-use monitor population that 479 // is reported by ObjectSynchronizer::log_in_use_monitor_details() 480 // at VM exit. 481 ObjectSynchronizer::set_is_special_deflation_requested(true); 482 } 483 return true; 484 } 485 486 void VM_Exit::doit() { 487 488 if (VerifyBeforeExit) { 489 HandleMark hm(VMThread::vm_thread()); 490 // Among other things, this ensures that Eden top is correct. 491 Universe::heap()->prepare_for_verify(); 492 // Silent verification so as not to pollute normal output, 493 // unless we really asked for it. 494 Universe::verify(); 495 } 496 497 CompileBroker::set_should_block(); 498 499 // Wait for a short period for threads in native to block. Any thread 500 // still executing native code after the wait will be stopped at 501 // native==>Java/VM barriers. 502 // Among 16276 JCK tests, 94% of them come here without any threads still 503 // running in native; the other 6% are quiescent within 250ms (Ultra 80). 504 wait_for_threads_in_native_to_block(); 505 506 set_vm_exited(); 507 508 // We'd like to call IdealGraphPrinter::clean_up() to finalize the 509 // XML logging, but we can't safely do that here. The logic to make 510 // XML termination logging safe is tied to the termination of the 511 // VMThread, and it doesn't terminate on this exit path. See 8222534. 512 513 // cleanup globals resources before exiting. exit_globals() currently 514 // cleans up outputStream resources and PerfMemory resources. 515 exit_globals(); 516 517 LogConfiguration::finalize(); 518 519 // Check for exit hook 520 exit_hook_t exit_hook = Arguments::exit_hook(); 521 if (exit_hook != NULL) { 522 // exit hook should exit. 523 exit_hook(_exit_code); 524 // ... but if it didn't, we must do it here 525 vm_direct_exit(_exit_code); 526 } else { 527 vm_direct_exit(_exit_code); 528 } 529 } 530 531 532 void VM_Exit::wait_if_vm_exited() { 533 if (_vm_exited && 534 Thread::current_or_null() != _shutdown_thread) { 535 // _vm_exited is set at safepoint, and the Threads_lock is never released 536 // we will block here until the process dies 537 Threads_lock->lock_without_safepoint_check(); 538 ShouldNotReachHere(); 539 } 540 } 541 542 void VM_PrintCompileQueue::doit() { 543 CompileBroker::print_compile_queues(_out); 544 } 545 546 #if INCLUDE_SERVICES 547 void VM_PrintClassHierarchy::doit() { 548 KlassHierarchy::print_class_hierarchy(_out, _print_interfaces, _print_subclasses, _classname); 549 } 550 #endif