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