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 #ifndef SHARE_RUNTIME_VMTHREAD_HPP
26 #define SHARE_RUNTIME_VMTHREAD_HPP
27
28 #include "runtime/atomicAccess.hpp"
29 #include "runtime/javaThread.hpp"
30 #include "runtime/nonJavaThread.hpp"
31 #include "runtime/perfDataTypes.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_or_safepoint_alot();
74
75 void evaluate_operation(VM_Operation* op);
76 void inner_execute(VM_Operation* op);
77 void wait_for_operation();
78
79 // Constructor
80 VMThread();
81
82 // No destruction allowed
83 ~VMThread() {
84 guarantee(false, "VMThread deletion must fix the race with VM termination");
85 }
86
87 // The ever running loop for the VMThread
88 void loop();
89
90 public:
91 bool is_running() const { return AtomicAccess::load(&_is_running); }
92
93 // Tester
94 bool is_VM_thread() const { return true; }
95
96 // Called to stop the VM thread
97 static void wait_for_vm_thread_exit();
98 static bool should_terminate() { return _should_terminate; }
99 static bool is_terminated() { return _terminated == true; }
100
101 // Execution of vm operation
102 static void execute(VM_Operation* op);
103
104 // Returns the current vm operation if any.
105 static VM_Operation* vm_operation() {
106 assert(Thread::current()->is_VM_thread(), "Must be");
107 return _cur_vm_operation;
108 }
109
110 static VM_Operation::VMOp_Type vm_op_type() {
111 VM_Operation* op = vm_operation();
112 assert(op != nullptr, "sanity");
113 return op->type();
114 }
115
116 // Returns the single instance of VMThread.
117 static VMThread* vm_thread() { return _vm_thread; }
118
119 void verify();
120
121 // Performance measurement
122 static PerfCounter* perf_accumulated_vm_operation_time() {
123 return _perf_accumulated_vm_operation_time;
124 }
125
126 // Entry for starting vm thread
127 virtual void run();
128
129 // Creations/Destructions
130 static void create();
131 static void destroy();
132
133 static void wait_until_executed(VM_Operation* op);
134
135 // Printing
136 const char* type_name() const { return "VMThread"; }
137
138 static void init_counters();
139 static void print_counters_on(outputStream* st);
140
141 private:
142 // VM_Operation support
143 static VM_Operation* _cur_vm_operation; // Current VM operation
144 static VM_Operation* _next_vm_operation; // Next VM operation
145
146 bool set_next_operation(VM_Operation *op); // Set the _next_vm_operation if possible.
147
148 // Pointer to single-instance of VM thread
149 static VMThread* _vm_thread;
150
151 static PerfTickCounters* get_perf_timer_for(VM_Operation* op);
152 static PerfCounter* get_perf_counter_for(VM_Operation* op);
153 };
154
155 #endif // SHARE_RUNTIME_VMTHREAD_HPP