1 /*
  2  * Copyright (c) 1997, 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_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(Cleanup)                               \
 41   template(ThreadDump)                            \
 42   template(PrintThreads)                          \
 43   template(FindDeadlocks)                         \
 44   template(ClearICs)                              \
 45   template(ForceSafepoint)                        \
 46   template(DeoptimizeFrame)                       \
 47   template(DeoptimizeAll)                         \
 48   template(ZombieAll)                             \
 49   template(Verify)                                \
 50   template(HeapDumper)                            \
 51   template(HeapDumpMerge)                         \
 52   template(CollectForMetadataAllocation)          \
 53   template(CollectForCodeCacheAllocation)         \
 54   template(GC_HeapInspection)                     \
 55   template(GenCollectFull)                        \
 56   template(GenCollectForAllocation)               \
 57   template(ParallelGCFailedAllocation)            \
 58   template(ParallelGCSystemGC)                    \
 59   template(G1CollectForAllocation)                \
 60   template(G1CollectFull)                         \
 61   template(G1PauseRemark)                         \
 62   template(G1PauseCleanup)                        \
 63   template(G1TryInitiateConcMark)                 \
 64   template(ZMarkEndOld)                           \
 65   template(ZMarkEndYoung)                         \
 66   template(ZMarkFlushOperation)                   \
 67   template(ZMarkStartYoung)                       \
 68   template(ZMarkStartYoungAndOld)                 \
 69   template(ZRelocateStartOld)                     \
 70   template(ZRelocateStartYoung)                   \
 71   template(ZRendezvousGCThreads)                  \
 72   template(ZVerifyOld)                            \
 73   template(XMarkStart)                            \
 74   template(XMarkEnd)                              \
 75   template(XRelocateStart)                        \
 76   template(XVerify)                               \
 77   template(HandshakeAllThreads)                   \
 78   template(PopulateDumpSharedSpace)               \
 79   template(JNIFunctionTableCopier)                \
 80   template(RedefineClasses)                       \
 81   template(GetObjectMonitorUsage)                 \
 82   template(GetAllStackTraces)                     \
 83   template(GetThreadListStackTraces)              \
 84   template(ChangeBreakpoints)                     \
 85   template(GetOrSetLocal)                         \
 86   template(VirtualThreadGetOrSetLocal)            \
 87   template(VirtualThreadGetCurrentLocation)       \
 88   template(ChangeSingleStep)                      \
 89   template(SetNotifyJvmtiEventsMode)              \
 90   template(HeapWalkOperation)                     \
 91   template(HeapIterateOperation)                  \
 92   template(ReportJavaOutOfMemory)                 \
 93   template(JFRCheckpoint)                         \
 94   template(ShenandoahFullGC)                      \
 95   template(ShenandoahInitMark)                    \
 96   template(ShenandoahFinalMarkStartEvac)          \
 97   template(ShenandoahInitUpdateRefs)              \
 98   template(ShenandoahFinalUpdateRefs)             \
 99   template(ShenandoahFinalRoots)                  \
100   template(ShenandoahDegeneratedGC)               \
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(PrintCompileQueue)                     \
110   template(PrintClassHierarchy)                   \
111   template(PrintClasses)                          \
112   template(ICBufferFull)                          \
113   template(PrintMetadata)                         \
114   template(GTestExecuteAtSafepoint)               \
115   template(GTestStopSafepoint)                    \
116   template(JFROldObject)                          \
117   template(JvmtiPostObjectFree)                   \
118   template(RendezvousGCThreads)
119 
120 class Thread;
121 class outputStream;
122 
123 class VM_Operation : public StackObj {
124  public:
125   enum VMOp_Type {
126     VM_OPS_DO(VM_OP_ENUM)
127     VMOp_Terminating
128   };
129 
130  private:
131   Thread*         _calling_thread;
132 
133   // The VM operation name array
134   static const char* _names[];
135 
136  public:
137   VM_Operation() : _calling_thread(nullptr) {}
138 
139   // VM operation support (used by VM thread)
140   Thread* calling_thread() const                 { return _calling_thread; }
141   void set_calling_thread(Thread* thread);
142 
143   // Called by VM thread - does in turn invoke doit(). Do not override this
144   void evaluate();
145 
146   // evaluate() is called by the VMThread and in turn calls doit().
147   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
148   // doit_prologue() is called in that thread before transferring control to
149   // the VMThread.
150   // If doit_prologue() returns true the VM operation will proceed, and
151   // doit_epilogue() will be called by the JavaThread once the VM operation
152   // completes. If doit_prologue() returns false the VM operation is cancelled.
153   virtual void doit()                            = 0;
154   virtual bool doit_prologue()                   { return true; };
155   virtual void doit_epilogue()                   {};
156 
157   // Configuration. Override these appropriately in subclasses.
158   virtual VMOp_Type type() const = 0;
159   virtual bool allow_nested_vm_operations() const { return false; }
160 
161   // You may override skip_thread_oop_barriers to return true if the operation
162   // does not access thread-private oops (including frames).
163   virtual bool skip_thread_oop_barriers() const { return false; }
164 
165   // An operation can either be done inside a safepoint
166   // or concurrently with Java threads running.
167   virtual bool evaluate_at_safepoint() const { return true; }
168 
169   // Debugging
170   virtual void print_on_error(outputStream* st) const;
171   virtual const char* name() const  { return _names[type()]; }
172   static const char* name(int type) {
173     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
174     return _names[type];
175   }
176   // Extra information about what triggered this operation.
177   virtual const char* cause() const { return nullptr; }
178 #ifndef PRODUCT
179   void print_on(outputStream* st) const { print_on_error(st); }
180 #endif
181 };
182 
183 #endif // SHARE_RUNTIME_VMOPERATION_HPP