1 /*
  2  * Copyright (c) 1998, 2025, 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 "compiler/compileBroker.hpp"
 26 #include "gc/shared/collectedHeap.hpp"
 27 #include "jfr/jfrEvents.hpp"
 28 #include "jfr/support/jfrThreadId.hpp"
 29 #include "logging/log.hpp"
 30 #include "logging/logStream.hpp"
 31 #include "logging/logConfiguration.hpp"
 32 #include "memory/resourceArea.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/oop.inline.hpp"
 35 #include "oops/verifyOopClosure.hpp"
 36 #include "runtime/atomic.hpp"
 37 #include "runtime/cpuTimeCounters.hpp"
 38 #include "runtime/handles.inline.hpp"
 39 #include "runtime/interfaceSupport.inline.hpp"
 40 #include "runtime/javaThread.inline.hpp"
 41 #include "runtime/jniHandles.hpp"
 42 #include "runtime/mutexLocker.hpp"
 43 #include "runtime/os.hpp"
 44 #include "runtime/perfData.hpp"
 45 #include "runtime/safepoint.hpp"
 46 #include "runtime/synchronizer.hpp"
 47 #include "runtime/timerTrace.hpp"
 48 #include "runtime/vmThread.hpp"
 49 #include "runtime/vmOperations.hpp"
 50 #include "utilities/dtrace.hpp"
 51 #include "utilities/events.hpp"
 52 #include "utilities/vmError.hpp"
 53 
 54 
 55 //------------------------------------------------------------------------------------------------------------------
 56 // Timeout machinery
 57 
 58 void VMOperationTimeoutTask::task() {
 59   assert(AbortVMOnVMOperationTimeout, "only if enabled");
 60   if (is_armed()) {
 61     jlong delay = nanos_to_millis(os::javaTimeNanos() - _arm_time);
 62     if (delay > AbortVMOnVMOperationTimeoutDelay) {
 63       fatal("%s VM operation took too long: " JLONG_FORMAT " ms elapsed since VM-op start (timeout: %zd ms)",
 64             _vm_op_name, delay, AbortVMOnVMOperationTimeoutDelay);
 65     }
 66   }
 67 }
 68 
 69 bool VMOperationTimeoutTask::is_armed() {
 70   return Atomic::load_acquire(&_armed) != 0;
 71 }
 72 
 73 void VMOperationTimeoutTask::arm(const char* vm_op_name) {
 74   _vm_op_name = vm_op_name;
 75   _arm_time = os::javaTimeNanos();
 76   Atomic::release_store_fence(&_armed, 1);
 77 }
 78 
 79 void VMOperationTimeoutTask::disarm() {
 80   Atomic::release_store_fence(&_armed, 0);
 81 
 82   // The two stores to `_armed` are counted in VM-op, but they should be
 83   // insignificant compared to the actual VM-op duration.
 84   jlong vm_op_duration = nanos_to_millis(os::javaTimeNanos() - _arm_time);
 85 
 86   // Repeat the timeout-check logic on the VM thread, because
 87   // VMOperationTimeoutTask might miss the arm-disarm window depending on
 88   // the scheduling.
 89   if (vm_op_duration > AbortVMOnVMOperationTimeoutDelay) {
 90     fatal("%s VM operation took too long: completed in " JLONG_FORMAT " ms (timeout: %zd ms)",
 91           _vm_op_name, vm_op_duration, AbortVMOnVMOperationTimeoutDelay);
 92   }
 93   _vm_op_name = nullptr;
 94 }
 95 
 96 //------------------------------------------------------------------------------------------------------------------
 97 // Implementation of VMThread stuff
 98 
 99 static VM_SafepointALot safepointALot_op;
100 static VM_ForceSafepoint no_op;
101 
102 bool              VMThread::_should_terminate   = false;
103 bool              VMThread::_terminated         = false;
104 Monitor*          VMThread::_terminate_lock     = nullptr;
105 VMThread*         VMThread::_vm_thread          = nullptr;
106 VM_Operation*     VMThread::_cur_vm_operation   = nullptr;
107 VM_Operation*     VMThread::_next_vm_operation  = &no_op; // Prevent any thread from setting an operation until VM thread is ready.
108 PerfCounter*      VMThread::_perf_accumulated_vm_operation_time = nullptr;
109 VMOperationTimeoutTask* VMThread::_timeout_task = nullptr;
110 
111 
112 void VMThread::create() {
113   assert(vm_thread() == nullptr, "we can only allocate one VMThread");
114   _vm_thread = new VMThread();
115 
116   if (AbortVMOnVMOperationTimeout) {
117     // Make sure we call the timeout task frequently enough, but not too frequent.
118     // Try to make the interval 10% of the timeout delay, so that we miss the timeout
119     // by those 10% at max. Periodic task also expects it to fit min/max intervals.
120     size_t interval = (size_t)AbortVMOnVMOperationTimeoutDelay / 10;
121     interval = interval / PeriodicTask::interval_gran * PeriodicTask::interval_gran;
122     interval = MAX2<size_t>(interval, PeriodicTask::min_interval);
123     interval = MIN2<size_t>(interval, PeriodicTask::max_interval);
124 
125     _timeout_task = new VMOperationTimeoutTask(interval);
126     _timeout_task->enroll();
127   } else {
128     assert(_timeout_task == nullptr, "sanity");
129   }
130 
131   _terminate_lock = new Monitor(Mutex::nosafepoint, "VMThreadTerminate_lock");
132 
133   if (UsePerfData) {
134     // jvmstat performance counters
135     JavaThread* THREAD = JavaThread::current(); // For exception macros.
136     _perf_accumulated_vm_operation_time =
137                  PerfDataManager::create_counter(SUN_THREADS, "vmOperationTime",
138                                                  PerfData::U_Ticks, CHECK);
139     CPUTimeCounters::create_counter(CPUTimeGroups::CPUTimeType::vm);
140   }
141 }
142 
143 VMThread::VMThread() : NamedThread(), _is_running(false) {
144   set_name("VM Thread");
145 }
146 
147 void VMThread::destroy() {
148   _vm_thread = nullptr;      // VM thread is gone
149 }
150 
151 static VM_Halt halt_op;
152 
153 void VMThread::run() {
154   assert(this == vm_thread(), "check");
155 
156   // Notify_lock wait checks on is_running() to rewait in
157   // case of spurious wakeup, it should wait on the last
158   // value set prior to the notify
159   Atomic::store(&_is_running, true);
160 
161   {
162     MutexLocker ml(Notify_lock);
163     Notify_lock->notify();
164   }
165   // Notify_lock is destroyed by Threads::create_vm()
166 
167   int prio = (VMThreadPriority == -1)
168     ? os::java_to_os_priority[NearMaxPriority]
169     : VMThreadPriority;
170   // Note that I cannot call os::set_priority because it expects Java
171   // priorities and I am *explicitly* using OS priorities so that it's
172   // possible to set the VM thread priority higher than any Java thread.
173   os::set_native_priority( this, prio );
174 
175   // Wait for VM_Operations until termination
176   this->loop();
177 
178   // Note the intention to exit before safepointing.
179   // 6295565  This has the effect of waiting for any large tty
180   // outputs to finish.
181   if (xtty != nullptr) {
182     ttyLocker ttyl;
183     xtty->begin_elem("destroy_vm");
184     xtty->stamp();
185     xtty->end_elem();
186     assert(should_terminate(), "termination flag must be set");
187   }
188 
189   // 4526887 let VM thread exit at Safepoint
190   _cur_vm_operation = &halt_op;
191   SafepointSynchronize::begin();
192 
193   if (VerifyBeforeExit) {
194     HandleMark hm(VMThread::vm_thread());
195     // Among other things, this ensures that Eden top is correct.
196     Universe::heap()->prepare_for_verify();
197     // Silent verification so as not to pollute normal output,
198     // unless we really asked for it.
199     Universe::verify();
200   }
201 
202   CompileBroker::set_should_block();
203 
204   // wait for threads (compiler threads or daemon threads) in the
205   // _thread_in_native state to block.
206   VM_Exit::wait_for_threads_in_native_to_block();
207 
208   // The ObjectMonitor subsystem uses perf counters so do this before
209   // we signal that the VM thread is gone. We don't want to run afoul
210   // of perfMemory_exit() in exit_globals().
211   ObjectSynchronizer::do_final_audit_and_print_stats();
212 
213   // signal other threads that VM process is gone
214   {
215     // Note: we must have the _no_safepoint_check_flag. Mutex::lock() allows
216     // VM thread to enter any lock at Safepoint as long as its _owner is null.
217     // If that happens after _terminate_lock->wait() has unset _owner
218     // but before it actually drops the lock and waits, the notification below
219     // may get lost and we will have a hang. To avoid this, we need to use
220     // Mutex::lock_without_safepoint_check().
221     MonitorLocker ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
222     _terminated = true;
223     ml.notify();
224   }
225 
226   // We are now racing with the VM termination being carried out in
227   // another thread, so we don't "delete this". Numerous threads don't
228   // get deleted when the VM terminates
229 
230 }
231 
232 
233 // Notify the VMThread that the last non-daemon JavaThread has terminated,
234 // and wait until operation is performed.
235 void VMThread::wait_for_vm_thread_exit() {
236   assert(JavaThread::current()->is_terminated(), "Should be terminated");
237   {
238     MonitorLocker mu(VMOperation_lock);
239     _should_terminate = true;
240     mu.notify_all();
241   }
242 
243   // Note: VM thread leaves at Safepoint. We are not stopped by Safepoint
244   // because this thread has been removed from the threads list. But anything
245   // that could get blocked by Safepoint should not be used after this point,
246   // otherwise we will hang, since there is no one can end the safepoint.
247 
248   // Wait until VM thread is terminated
249   // Note: it should be OK to use Terminator_lock here. But this is called
250   // at a very delicate time (VM shutdown) and we are operating in non- VM
251   // thread at Safepoint. It's safer to not share lock with other threads.
252   {
253     MonitorLocker ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
254     while (!VMThread::is_terminated()) {
255       ml.wait();
256     }
257   }
258 }
259 
260 static void post_vm_operation_event(EventExecuteVMOperation* event, VM_Operation* op) {
261   assert(event != nullptr, "invariant");
262   assert(op != nullptr, "invariant");
263   const bool evaluate_at_safepoint = op->evaluate_at_safepoint();
264   event->set_operation(op->type());
265   event->set_safepoint(evaluate_at_safepoint);
266   event->set_blocking(true);
267   event->set_caller(JFR_THREAD_ID(op->calling_thread()));
268   event->set_safepointId(evaluate_at_safepoint ? SafepointSynchronize::safepoint_id() : 0);
269   event->commit();
270 }
271 
272 void VMThread::evaluate_operation(VM_Operation* op) {
273   ResourceMark rm;
274 
275   {
276     PerfTraceTime vm_op_timer(perf_accumulated_vm_operation_time());
277     HOTSPOT_VMOPS_BEGIN(
278                      (char *) op->name(), strlen(op->name()),
279                      op->evaluate_at_safepoint() ? 0 : 1);
280 
281     EventExecuteVMOperation event;
282     op->evaluate();
283     if (event.should_commit()) {
284       post_vm_operation_event(&event, op);
285     }
286 
287     HOTSPOT_VMOPS_END(
288                      (char *) op->name(), strlen(op->name()),
289                      op->evaluate_at_safepoint() ? 0 : 1);
290   }
291 
292   if (UsePerfData && os::is_thread_cpu_time_supported()) {
293     assert(Thread::current() == this, "Must be called from VM thread");
294     // Update vm_thread_cpu_time after each VM operation.
295     ThreadTotalCPUTimeClosure tttc(CPUTimeGroups::CPUTimeType::vm);
296     tttc.do_thread(this);
297   }
298 }
299 
300 class HandshakeALotClosure : public HandshakeClosure {
301  public:
302   HandshakeALotClosure() : HandshakeClosure("HandshakeALot") {}
303   void do_thread(Thread* thread) {
304 #ifdef ASSERT
305     JavaThread::cast(thread)->verify_states_for_handshake();
306 #endif
307   }
308 };
309 
310 bool VMThread::handshake_or_safepoint_alot() {
311   assert(_cur_vm_operation == nullptr, "should not have an op yet");
312   assert(_next_vm_operation == nullptr, "should not have an op yet");
313   if (!HandshakeALot && !SafepointALot) {
314     return false;
315   }
316   static jlong last_alot_ms = 0;
317   jlong now_ms = nanos_to_millis(os::javaTimeNanos());
318   // If HandshakeALot or SafepointALot are set, but GuaranteedSafepointInterval is explicitly
319   // set to 0 on the command line, we emit the operation if it's been more than a second
320   // since the last one.
321   jlong interval = GuaranteedSafepointInterval != 0 ? GuaranteedSafepointInterval : 1000;
322   jlong deadline_ms = interval + last_alot_ms;
323   if (now_ms > deadline_ms) {
324     last_alot_ms = now_ms;
325     return true;
326   }
327   return false;
328 }
329 
330 bool VMThread::set_next_operation(VM_Operation *op) {
331   if (_next_vm_operation != nullptr) {
332     return false;
333   }
334   log_debug(vmthread)("Adding VM operation: %s", op->name());
335 
336   _next_vm_operation = op;
337 
338   HOTSPOT_VMOPS_REQUEST(
339                    (char *) op->name(), strlen(op->name()),
340                    op->evaluate_at_safepoint() ? 0 : 1);
341   return true;
342 }
343 
344 void VMThread::wait_until_executed(VM_Operation* op) {
345   MonitorLocker ml(VMOperation_lock,
346                    Thread::current()->is_Java_thread() ?
347                      Mutex::_safepoint_check_flag :
348                      Mutex::_no_safepoint_check_flag);
349   {
350     TraceTime timer("Installing VM operation", TRACETIME_LOG(Trace, vmthread));
351     while (true) {
352       if (VMThread::vm_thread()->set_next_operation(op)) {
353         ml.notify_all();
354         break;
355       }
356       // Wait to install this operation as the next operation in the VM Thread
357       log_trace(vmthread)("A VM operation already set, waiting");
358       ml.wait();
359     }
360   }
361   {
362     // Wait until the operation has been processed
363     TraceTime timer("Waiting for VM operation to be completed", TRACETIME_LOG(Trace, vmthread));
364     // _next_vm_operation is cleared holding VMOperation_lock after it has been
365     // executed. We wait until _next_vm_operation is not our op.
366     while (_next_vm_operation == op) {
367       // VM Thread can process it once we unlock the mutex on wait.
368       ml.wait();
369     }
370   }
371 }
372 
373 static void self_destruct_if_needed() {
374   // Support for self destruction
375   if ((SelfDestructTimer != 0.0) && !VMError::is_error_reported() &&
376       (os::elapsedTime() > SelfDestructTimer * 60.0)) {
377     tty->print_cr("VM self-destructed");
378     os::exit(-1);
379   }
380 }
381 
382 void VMThread::inner_execute(VM_Operation* op) {
383   assert(Thread::current()->is_VM_thread(), "Must be the VM thread");
384 
385   VM_Operation* prev_vm_operation = nullptr;
386   if (_cur_vm_operation != nullptr) {
387     // Check that the VM operation allows nested VM operation.
388     // This is normally not the case, e.g., the compiler
389     // does not allow nested scavenges or compiles.
390     if (!_cur_vm_operation->allow_nested_vm_operations()) {
391       fatal("Unexpected nested VM operation %s requested by operation %s",
392             op->name(), _cur_vm_operation->name());
393     }
394     op->set_calling_thread(_cur_vm_operation->calling_thread());
395     prev_vm_operation = _cur_vm_operation;
396   }
397 
398   _cur_vm_operation = op;
399 
400   HandleMark hm(VMThread::vm_thread());
401 
402   const char* const cause = op->cause();
403   stringStream ss;
404   ss.print("Executing%s%s VM operation: %s",
405            prev_vm_operation != nullptr ? " nested" : "",
406            op->evaluate_at_safepoint() ? " safepoint" : " non-safepoint",
407            op->name());
408   if (cause != nullptr) {
409     ss.print(" (%s)", cause);
410   }
411 
412   EventMarkVMOperation em("%s", ss.freeze());
413   log_debug(vmthread)("%s", ss.freeze());
414 
415   bool end_safepoint = false;
416   bool has_timeout_task = (_timeout_task != nullptr);
417   if (_cur_vm_operation->evaluate_at_safepoint() &&
418       !SafepointSynchronize::is_at_safepoint()) {
419     SafepointSynchronize::begin();
420     if (has_timeout_task) {
421       _timeout_task->arm(_cur_vm_operation->name());
422     }
423     end_safepoint = true;
424   }
425 
426   evaluate_operation(_cur_vm_operation);
427 
428   if (end_safepoint) {
429     if (has_timeout_task) {
430       _timeout_task->disarm();
431     }
432     SafepointSynchronize::end();
433   }
434 
435   _cur_vm_operation = prev_vm_operation;
436 }
437 
438 void VMThread::wait_for_operation() {
439   assert(Thread::current()->is_VM_thread(), "Must be the VM thread");
440   MonitorLocker ml_op_lock(VMOperation_lock, Mutex::_no_safepoint_check_flag);
441 
442   // Clear previous operation.
443   // On first call this clears a dummy place-holder.
444   _next_vm_operation = nullptr;
445   // Notify operation is done and notify a next operation can be installed.
446   ml_op_lock.notify_all();
447 
448   while (!should_terminate()) {
449     self_destruct_if_needed();
450     if (_next_vm_operation != nullptr) {
451       return;
452     }
453     if (handshake_or_safepoint_alot()) {
454       if (HandshakeALot) {
455         MutexUnlocker mul(VMOperation_lock);
456         HandshakeALotClosure hal_cl;
457         Handshake::execute(&hal_cl);
458       }
459       // When we unlocked above someone might have setup a new op.
460       if (_next_vm_operation != nullptr) {
461         return;
462       }
463       if (SafepointALot) {
464         _next_vm_operation = &safepointALot_op;
465         return;
466       }
467     }
468     assert(_next_vm_operation == nullptr, "Must be");
469     assert(_cur_vm_operation  == nullptr, "Must be");
470 
471     // We didn't find anything to execute, notify any waiter so they can install an op.
472     ml_op_lock.notify_all();
473     ml_op_lock.wait(GuaranteedSafepointInterval);
474   }
475 }
476 
477 void VMThread::loop() {
478   assert(_cur_vm_operation == nullptr, "no current one should be executing");
479 
480   SafepointSynchronize::init(_vm_thread);
481 
482   // Need to set a calling thread for ops not passed
483   // via the normal way.
484   no_op.set_calling_thread(_vm_thread);
485   safepointALot_op.set_calling_thread(_vm_thread);
486 
487   while (true) {
488     if (should_terminate()) break;
489     wait_for_operation();
490     if (should_terminate()) break;
491     assert(_next_vm_operation != nullptr, "Must have one");
492     inner_execute(_next_vm_operation);
493   }
494 }
495 
496 // A SkipGCALot object is used to elide the usual effect of gc-a-lot
497 // over a section of execution by a thread. Currently, it's used only to
498 // prevent re-entrant calls to GC.
499 class SkipGCALot : public StackObj {
500   private:
501    bool _saved;
502    Thread* _t;
503 
504   public:
505 #ifdef ASSERT
506     SkipGCALot(Thread* t) : _t(t) {
507       _saved = _t->skip_gcalot();
508       _t->set_skip_gcalot(true);
509     }
510 
511     ~SkipGCALot() {
512       assert(_t->skip_gcalot(), "Save-restore protocol invariant");
513       _t->set_skip_gcalot(_saved);
514     }
515 #else
516     SkipGCALot(Thread* t) { }
517     ~SkipGCALot() { }
518 #endif
519 };
520 
521 void VMThread::execute(VM_Operation* op) {
522   Thread* t = Thread::current();
523 
524   if (t->is_VM_thread()) {
525     op->set_calling_thread(t);
526     ((VMThread*)t)->inner_execute(op);
527     return;
528   }
529 
530   // Avoid re-entrant attempts to gc-a-lot
531   SkipGCALot sgcalot(t);
532 
533   // JavaThread or WatcherThread
534   if (t->is_Java_thread()) {
535     JavaThread::cast(t)->check_for_valid_safepoint_state();
536   }
537 
538   // New request from Java thread, evaluate prologue
539   if (!op->doit_prologue()) {
540     return;   // op was cancelled
541   }
542 
543   op->set_calling_thread(t);
544 
545   wait_until_executed(op);
546 
547   op->doit_epilogue();
548 }
549 
550 void VMThread::verify() {
551   oops_do(&VerifyOopClosure::verify_oop, nullptr);
552 }