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