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