1 /*
  2  * Copyright (c) 1997, 2020, 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_VMOPERATIONS_HPP
 26 #define SHARE_RUNTIME_VMOPERATIONS_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "oops/oop.hpp"
 30 #include "runtime/thread.hpp"
 31 #include "runtime/threadSMR.hpp"
 32 
 33 // The following classes are used for operations
 34 // initiated by a Java thread but that must
 35 // take place in the VMThread.
 36 
 37 #define VM_OP_ENUM(type)   VMOp_##type,
 38 
 39 // Note: When new VM_XXX comes up, add 'XXX' to the template table.
 40 #define VM_OPS_DO(template)                       \
 41   template(None)                                  \
 42   template(Cleanup)                               \
 43   template(ThreadDump)                            \
 44   template(PrintThreads)                          \
 45   template(FindDeadlocks)                         \
 46   template(ClearICs)                              \
 47   template(ForceSafepoint)                        \
 48   template(ForceAsyncSafepoint)                   \
 49   template(DeoptimizeFrame)                       \
 50   template(DeoptimizeAll)                         \
 51   template(ZombieAll)                             \
 52   template(Verify)                                \
 53   template(PrintJNI)                              \
 54   template(HeapDumper)                            \
 55   template(DeoptimizeTheWorld)                    \
 56   template(CollectForMetadataAllocation)          \
 57   template(GC_HeapInspection)                     \
 58   template(GenCollectFull)                        \
 59   template(GenCollectFullConcurrent)              \
 60   template(GenCollectForAllocation)               \
 61   template(ParallelGCFailedAllocation)            \
 62   template(ParallelGCSystemGC)                    \
 63   template(G1CollectForAllocation)                \
 64   template(G1CollectFull)                         \
 65   template(G1Concurrent)                          \
 66   template(G1TryInitiateConcMark)                 \
 67   template(ZMarkStart)                            \
 68   template(ZMarkEnd)                              \
 69   template(ZRelocateStart)                        \
 70   template(ZVerify)                               \
 71   template(HandshakeOneThread)                    \
 72   template(HandshakeAllThreads)                   \
 73   template(HandshakeFallback)                     \
 74   template(EnableBiasedLocking)                   \
 75   template(BulkRevokeBias)                        \
 76   template(PopulateDumpSharedSpace)               \
 77   template(JNIFunctionTableCopier)                \
 78   template(RedefineClasses)                       \
 79   template(UpdateForPopTopFrame)                  \
 80   template(SetFramePop)                           \
 81   template(GetObjectMonitorUsage)                 \
 82   template(GetStackTrace)                         \
 83   template(GetMultipleStackTraces)                \
 84   template(GetAllStackTraces)                     \
 85   template(GetThreadListStackTraces)              \
 86   template(GetFrameCount)                         \
 87   template(GetFrameLocation)                      \
 88   template(ChangeBreakpoints)                     \
 89   template(GetOrSetLocal)                         \
 90   template(GetCurrentLocation)                    \
 91   template(ChangeSingleStep)                      \
 92   template(HeapWalkOperation)                     \
 93   template(HeapIterateOperation)                  \
 94   template(ReportJavaOutOfMemory)                 \
 95   template(JFRCheckpoint)                         \
 96   template(ShenandoahFullGC)                      \
 97   template(ShenandoahInitMark)                    \
 98   template(ShenandoahFinalMarkStartEvac)          \
 99   template(ShenandoahInitUpdateRefs)              \
100   template(ShenandoahFinalUpdateRefs)             \
101   template(ShenandoahDegeneratedGC)               \
102   template(Exit)                                  \
103   template(LinuxDllLoad)                          \
104   template(RotateGCLog)                           \
105   template(WhiteBoxOperation)                     \
106   template(JVMCIResizeCounters)                   \
107   template(ClassLoaderStatsOperation)             \
108   template(ClassLoaderHierarchyOperation)         \
109   template(DumpHashtable)                         \
110   template(DumpTouchedMethods)                    \
111   template(PrintCompileQueue)                     \
112   template(PrintClassHierarchy)                   \
113   template(ThreadSuspend)                         \
114   template(ThreadsSuspendJVMTI)                   \
115   template(ICBufferFull)                          \
116   template(ScavengeMonitors)                      \
117   template(PrintMetadata)                         \
118   template(GTestExecuteAtSafepoint)               \
119   template(JFROldObject)                          \
120 
121 class VM_Operation : public StackObj {
122  public:
123   enum VMOp_Type {
124     VM_OPS_DO(VM_OP_ENUM)
125     VMOp_Terminating
126   };
127 
128  private:
129   Thread*         _calling_thread;
130   VM_Operation*   _next;
131   VM_Operation*   _prev;
132 
133   // The VM operation name array
134   static const char* _names[];
135 
136  public:
137   VM_Operation() : _calling_thread(NULL), _next(NULL), _prev(NULL) {}
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   // Linking
158   VM_Operation *next() const                     { return _next; }
159   VM_Operation *prev() const                     { return _prev; }
160   void set_next(VM_Operation *next)              { _next = next; }
161   void set_prev(VM_Operation *prev)              { _prev = prev; }
162 
163   // Configuration. Override these appropriately in subclasses.
164   virtual VMOp_Type type() const = 0;
165   virtual bool allow_nested_vm_operations() const { return false; }
166 
167   // An operation can either be done inside a safepoint
168   // or concurrently with Java threads running.
169   virtual bool evaluate_at_safepoint() const { return true; }
170 
171   // Debugging
172   virtual void print_on_error(outputStream* st) const;
173   virtual const char* name() const  { return _names[type()]; }
174   static const char* name(int type) {
175     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
176     return _names[type];
177   }
178 #ifndef PRODUCT
179   void print_on(outputStream* st) const { print_on_error(st); }
180 #endif
181 };
182 
183 class VM_None: public VM_Operation {
184   const char* _reason;
185  public:
186   VM_None(const char* reason) : _reason(reason) {}
187   const char* name() const { return _reason; }
188   VMOp_Type type() const { return VMOp_None; }
189   void doit() {};
190 };
191 
192 class VM_Cleanup: public VM_Operation {
193  public:
194   VMOp_Type type() const { return VMOp_Cleanup; }
195   void doit() {};
196 };
197 
198 class VM_ClearICs: public VM_Operation {
199  private:
200   bool _preserve_static_stubs;
201  public:
202   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
203   void doit();
204   VMOp_Type type() const { return VMOp_ClearICs; }
205 };
206 
207 // empty vm op, evaluated just to force a safepoint
208 class VM_ForceSafepoint: public VM_Operation {
209  public:
210   void doit()         {}
211   VMOp_Type type() const { return VMOp_ForceSafepoint; }
212 };
213 
214 // empty vm op, when forcing a safepoint to suspend a thread
215 class VM_ThreadSuspend: public VM_ForceSafepoint {
216  public:
217   VMOp_Type type() const { return VMOp_ThreadSuspend; }
218 };
219 
220 // empty vm op, when forcing a safepoint to suspend threads from jvmti
221 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
222  public:
223   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
224 };
225 
226 // empty vm op, when forcing a safepoint due to inline cache buffers being full
227 class VM_ICBufferFull: public VM_ForceSafepoint {
228  public:
229   VMOp_Type type() const { return VMOp_ICBufferFull; }
230 };
231 
232 // Base class for invoking parts of a gtest in a safepoint.
233 // Derived classes provide the doit method.
234 // Typically also need to transition the gtest thread from native to VM.
235 class VM_GTestExecuteAtSafepoint: public VM_Operation {
236  public:
237   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
238 
239  protected:
240   VM_GTestExecuteAtSafepoint() {}
241 };
242 
243 // Deopt helper that can deoptimize frames in threads other than the
244 // current thread.  Only used through Deoptimization::deoptimize_frame.
245 class VM_DeoptimizeFrame: public VM_Operation {
246   friend class Deoptimization;
247 
248  private:
249   JavaThread* _thread;
250   intptr_t*   _id;
251   int _reason;
252   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
253 
254  public:
255   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
256   void doit();
257   bool allow_nested_vm_operations() const        { return true;  }
258 };
259 
260 #ifndef PRODUCT
261 class VM_DeoptimizeAll: public VM_Operation {
262  private:
263   Klass* _dependee;
264  public:
265   VM_DeoptimizeAll() {}
266   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
267   void doit();
268   bool allow_nested_vm_operations() const        { return true; }
269 };
270 
271 
272 class VM_ZombieAll: public VM_Operation {
273  public:
274   VM_ZombieAll() {}
275   VMOp_Type type() const                         { return VMOp_ZombieAll; }
276   void doit();
277   bool allow_nested_vm_operations() const        { return true; }
278 };
279 #endif // PRODUCT
280 
281 class VM_Verify: public VM_Operation {
282  public:
283   VMOp_Type type() const { return VMOp_Verify; }
284   void doit();
285 };
286 
287 
288 class VM_PrintThreads: public VM_Operation {
289  private:
290   outputStream* _out;
291   bool _print_concurrent_locks;
292   bool _print_extended_info;
293  public:
294   VM_PrintThreads()
295     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
296   {}
297   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
298     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
299   {}
300   VMOp_Type type() const {
301     return VMOp_PrintThreads;
302   }
303   void doit();
304   bool doit_prologue();
305   void doit_epilogue();
306 };
307 
308 class VM_PrintJNI: public VM_Operation {
309  private:
310   outputStream* _out;
311  public:
312   VM_PrintJNI()                         { _out = tty; }
313   VM_PrintJNI(outputStream* out)        { _out = out; }
314   VMOp_Type type() const                { return VMOp_PrintJNI; }
315   void doit();
316 };
317 
318 class VM_PrintMetadata : public VM_Operation {
319  private:
320   outputStream* const _out;
321   const size_t        _scale;
322   const int           _flags;
323 
324  public:
325   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
326     : _out(out), _scale(scale), _flags(flags)
327   {};
328 
329   VMOp_Type type() const  { return VMOp_PrintMetadata; }
330   void doit();
331 };
332 
333 class DeadlockCycle;
334 class VM_FindDeadlocks: public VM_Operation {
335  private:
336   bool              _concurrent_locks;
337   DeadlockCycle*    _deadlocks;
338   outputStream*     _out;
339   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
340                               // which protects the JavaThreads in _deadlocks.
341 
342  public:
343   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _deadlocks(NULL), _out(NULL), _setter() {};
344   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _deadlocks(NULL), _out(st) {};
345   ~VM_FindDeadlocks();
346 
347   DeadlockCycle* result()      { return _deadlocks; };
348   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
349   void doit();
350 };
351 
352 class ThreadDumpResult;
353 class ThreadSnapshot;
354 class ThreadConcurrentLocks;
355 
356 class VM_ThreadDump : public VM_Operation {
357  private:
358   ThreadDumpResult*              _result;
359   int                            _num_threads;
360   GrowableArray<instanceHandle>* _threads;
361   int                            _max_depth;
362   bool                           _with_locked_monitors;
363   bool                           _with_locked_synchronizers;
364 
365   void snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
366 
367  public:
368   VM_ThreadDump(ThreadDumpResult* result,
369                 int max_depth,  // -1 indicates entire stack
370                 bool with_locked_monitors,
371                 bool with_locked_synchronizers);
372 
373   VM_ThreadDump(ThreadDumpResult* result,
374                 GrowableArray<instanceHandle>* threads,
375                 int num_threads, // -1 indicates entire stack
376                 int max_depth,
377                 bool with_locked_monitors,
378                 bool with_locked_synchronizers);
379 
380   VMOp_Type type() const { return VMOp_ThreadDump; }
381   void doit();
382   bool doit_prologue();
383   void doit_epilogue();
384 };
385 
386 
387 class VM_Exit: public VM_Operation {
388  private:
389   int  _exit_code;
390   static volatile bool _vm_exited;
391   static Thread * volatile _shutdown_thread;
392   static void wait_if_vm_exited();
393  public:
394   VM_Exit(int exit_code) {
395     _exit_code = exit_code;
396   }
397   static int wait_for_threads_in_native_to_block();
398   static int set_vm_exited();
399   static bool vm_exited()                      { return _vm_exited; }
400   static Thread * shutdown_thread()            { return _shutdown_thread; }
401   static void block_if_vm_exited() {
402     if (_vm_exited) {
403       wait_if_vm_exited();
404     }
405   }
406   VMOp_Type type() const { return VMOp_Exit; }
407   bool doit_prologue();
408   void doit();
409 };
410 
411 class VM_PrintCompileQueue: public VM_Operation {
412  private:
413   outputStream* _out;
414 
415  public:
416   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
417   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
418   void doit();
419 };
420 
421 #if INCLUDE_SERVICES
422 class VM_PrintClassHierarchy: public VM_Operation {
423  private:
424   outputStream* _out;
425   bool _print_interfaces;
426   bool _print_subclasses;
427   char* _classname;
428 
429  public:
430   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
431     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
432     _classname(classname) {}
433   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
434   void doit();
435 };
436 #endif // INCLUDE_SERVICES
437 
438 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP