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