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 #ifndef SHARE_RUNTIME_VMTHREAD_HPP
 26 #define SHARE_RUNTIME_VMTHREAD_HPP
 27 
 28 #include "runtime/atomic.hpp"
 29 #include "runtime/javaThread.hpp"
 30 #include "runtime/perfDataTypes.hpp"
 31 #include "runtime/nonJavaThread.hpp"
 32 #include "runtime/task.hpp"
 33 #include "runtime/vmOperation.hpp"
 34 
 35 // VM operation timeout handling: warn or abort the VM when VM operation takes
 36 // too long. Periodic tasks do not participate in safepoint protocol, and therefore
 37 // can fire when application threads are stopped.
 38 
 39 class VMOperationTimeoutTask : public PeriodicTask {
 40 private:
 41   volatile int _armed;
 42   jlong _arm_time;
 43   const char* _vm_op_name;
 44 public:
 45   VMOperationTimeoutTask(size_t interval_time) :
 46           PeriodicTask(interval_time), _armed(0), _arm_time(0), _vm_op_name(nullptr) {}
 47 
 48   virtual void task();
 49 
 50   bool is_armed();
 51   void arm(const char* vm_op_name);
 52   void disarm();
 53 };
 54 
 55 //
 56 // A single VMThread is used by other threads to offload heavy vm operations
 57 // like scavenge, garbage_collect etc.
 58 //
 59 
 60 class VMThread: public NamedThread {
 61  private:
 62   volatile bool _is_running;
 63 
 64   static ThreadPriority _current_priority;
 65 
 66   static bool _should_terminate;
 67   static bool _terminated;
 68   static Monitor * _terminate_lock;
 69   static PerfCounter* _perf_accumulated_vm_operation_time;
 70 
 71   static VMOperationTimeoutTask* _timeout_task;
 72 
 73   static bool handshake_alot();
 74   static void setup_periodic_safepoint_if_needed();
 75 
 76   void evaluate_operation(VM_Operation* op);
 77   void inner_execute(VM_Operation* op);
 78   void wait_for_operation();
 79 
 80   // Constructor
 81   VMThread();
 82 
 83   // No destruction allowed
 84   ~VMThread() {
 85     guarantee(false, "VMThread deletion must fix the race with VM termination");
 86   }
 87 
 88   // The ever running loop for the VMThread
 89   void loop();
 90 
 91  public:
 92   bool is_running() const { return Atomic::load(&_is_running); }
 93 
 94   // Tester
 95   bool is_VM_thread() const                      { return true; }
 96 
 97   // Called to stop the VM thread
 98   static void wait_for_vm_thread_exit();
 99   static bool should_terminate()                  { return _should_terminate; }
100   static bool is_terminated()                     { return _terminated == true; }
101 
102   // Execution of vm operation
103   static void execute(VM_Operation* op);
104 
105   // Returns the current vm operation if any.
106   static VM_Operation* vm_operation()             {
107     assert(Thread::current()->is_VM_thread(), "Must be");
108     return _cur_vm_operation;
109   }
110 
111   static VM_Operation::VMOp_Type vm_op_type()     {
112     VM_Operation* op = vm_operation();
113     assert(op != nullptr, "sanity");
114     return op->type();
115   }
116 
117   // Returns the single instance of VMThread.
118   static VMThread* vm_thread()                    { return _vm_thread; }
119 
120   void verify();
121 
122   // Performance measurement
123   static PerfCounter* perf_accumulated_vm_operation_time() {
124     return _perf_accumulated_vm_operation_time;
125   }
126 
127   // Entry for starting vm thread
128   virtual void run();
129 
130   // Creations/Destructions
131   static void create();
132   static void destroy();
133 
134   static void wait_until_executed(VM_Operation* op);
135 
136   // Printing
137   const char* type_name() const { return "VMThread"; }
138 
139  private:
140   // VM_Operation support
141   static VM_Operation*     _cur_vm_operation;   // Current VM operation
142   static VM_Operation*     _next_vm_operation;  // Next VM operation
143 
144   bool set_next_operation(VM_Operation *op);    // Set the _next_vm_operation if possible.
145 
146   // Pointer to single-instance of VM thread
147   static VMThread*     _vm_thread;
148 };
149 
150 #endif // SHARE_RUNTIME_VMTHREAD_HPP