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