1 /*
2 * Copyright (c) 1997, 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_VMOPERATION_HPP
26 #define SHARE_RUNTIME_VMOPERATION_HPP
27
28 #include "memory/allocation.hpp"
29
30 // The following classes are used for operations
31 // initiated by a Java thread but that must
32 // take place in the VMThread.
33
34 #define VM_OP_ENUM(type) VMOp_##type,
35
36 // Note: When new VM_XXX comes up, add 'XXX' to the template table.
37 #define VM_OPS_DO(template) \
38 template(Halt) \
39 template(SafepointALot) \
40 template(ThreadDump) \
41 template(PrintThreads) \
42 template(FindDeadlocks) \
43 template(ClearICs) \
44 template(ForceSafepoint) \
45 template(DeoptimizeFrame) \
46 template(DeoptimizeAll) \
47 template(ZombieAll) \
48 template(Verify) \
49 template(HeapDumper) \
50 template(CollectForMetadataAllocation) \
51 template(CollectForCodeCacheAllocation) \
52 template(GC_HeapInspection) \
53 template(SerialCollectForAllocation) \
54 template(SerialGCCollect) \
55 template(ParallelCollectForAllocation) \
56 template(ParallelGCCollect) \
57 template(G1CollectForAllocation) \
58 template(G1CollectFull) \
59 template(G1PauseRemark) \
60 template(G1PauseCleanup) \
61 template(G1TryInitiateConcMark) \
62 template(G1RendezvousGCThreads) \
63 template(ZMarkEndOld) \
64 template(ZMarkEndYoung) \
65 template(ZMarkFlushOperation) \
66 template(ZMarkStartYoung) \
67 template(ZMarkStartYoungAndOld) \
68 template(ZRelocateStartOld) \
69 template(ZRelocateStartYoung) \
70 template(ZRendezvousGCThreads) \
71 template(ZVerifyOld) \
72 template(XMarkStart) \
73 template(XMarkEnd) \
74 template(XRelocateStart) \
75 template(XVerify) \
76 template(HandshakeAllThreads) \
77 template(PopulateDumpSharedSpace) \
78 template(JNIFunctionTableCopier) \
79 template(RedefineClasses) \
80 template(GetObjectMonitorUsage) \
81 template(GetAllStackTraces) \
82 template(GetThreadListStackTraces) \
83 template(ChangeBreakpoints) \
84 template(GetOrSetLocal) \
85 template(VirtualThreadGetOrSetLocal) \
86 template(ChangeSingleStep) \
87 template(SetNotifyJvmtiEventsMode) \
88 template(HeapWalkOperation) \
89 template(HeapIterateOperation) \
90 template(ReportJavaOutOfMemory) \
91 template(JFRSafepointClear) \
92 template(JFRSafepointWrite) \
93 template(ShenandoahFullGC) \
94 template(ShenandoahInitMark) \
95 template(ShenandoahFinalMarkStartEvac) \
96 template(ShenandoahInitUpdateRefs) \
97 template(ShenandoahFinalUpdateRefs) \
98 template(ShenandoahFinalRoots) \
99 template(ShenandoahDegeneratedGC) \
100 template(EpsilonCollect) \
101 template(Exit) \
102 template(LinuxDllLoad) \
103 template(WhiteBoxOperation) \
104 template(JVMCIResizeCounters) \
105 template(ClassLoaderStatsOperation) \
106 template(ClassLoaderHierarchyOperation) \
107 template(DumpHashtable) \
108 template(CleanClassLoaderDataMetaspaces) \
109 template(RehashStringTable) \
110 template(RehashSymbolTable) \
111 template(PrintCompileQueue) \
112 template(PrintClassHierarchy) \
113 template(PrintClasses) \
114 template(PrintMetadata) \
115 template(GTestExecuteAtSafepoint) \
116 template(GTestStopSafepoint) \
117 template(JFROldObject) \
118 template(JvmtiPostObjectFree) \
119 template(RendezvousGCThreads) \
120 template(JFRInitializeCPUTimeSampler) \
121 template(JFRTerminateCPUTimeSampler) \
122 template(ReinitializeMDO)
123
124 class Thread;
125 class outputStream;
126
127 class VM_Operation : public StackObj {
128 public:
129 enum VMOp_Type {
130 VM_OPS_DO(VM_OP_ENUM)
131 VMOp_Terminating
132 };
133
134 private:
135 Thread* _calling_thread;
136
137 // The VM operation name array
138 static const char* _names[];
139
140 public:
141 VM_Operation() : _calling_thread(nullptr) {}
142
143 // VM operation support (used by VM thread)
144 Thread* calling_thread() const { return _calling_thread; }
145 void set_calling_thread(Thread* thread);
146
147 // Called by VM thread - does in turn invoke doit(). Do not override this
148 void evaluate();
149
150 // evaluate() is called by the VMThread and in turn calls doit().
151 // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
152 // doit_prologue() is called in that thread before transferring control to
153 // the VMThread.
154 // If doit_prologue() returns true the VM operation will proceed, and
155 // doit_epilogue() will be called by the JavaThread once the VM operation
156 // completes. If doit_prologue() returns false the VM operation is cancelled.
157 virtual void doit() = 0;
158 virtual bool doit_prologue() { return true; };
159 virtual void doit_epilogue() {};
160
161 // Configuration. Override these appropriately in subclasses.
162 virtual VMOp_Type type() const = 0;
163 virtual bool allow_nested_vm_operations() const { return false; }
164
165 // VMOp_Type may belong to a category of the operation.
166 // Override is_XX_operation() appropriately in subclasses.
167 virtual bool is_gc_operation() const { return false; }
168
169 // You may override skip_thread_oop_barriers to return true if the operation
170 // does not access thread-private oops (including frames).
171 virtual bool skip_thread_oop_barriers() const { return false; }
172
173 // An operation can either be done inside a safepoint
174 // or concurrently with Java threads running.
175 virtual bool evaluate_at_safepoint() const { return true; }
176
177 // Debugging
178 virtual void print_on_error(outputStream* st) const;
179 virtual const char* name() const { return _names[type()]; }
180 static const char* name(int type) {
181 assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
182 return _names[type];
183 }
184 // Extra information about what triggered this operation.
185 virtual const char* cause() const { return nullptr; }
186 #ifndef PRODUCT
187 void print_on(outputStream* st) const { print_on_error(st); }
188 #endif
189 };
190
191 #endif // SHARE_RUNTIME_VMOPERATION_HPP