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