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