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   template(ClassPrintLayout)                      \
121 
122 class VM_Operation : public StackObj {
123  public:
124   enum VMOp_Type {
125     VM_OPS_DO(VM_OP_ENUM)
126     VMOp_Terminating
127   };
128 
129  private:
130   Thread*         _calling_thread;
131   VM_Operation*   _next;
132   VM_Operation*   _prev;
133 
134   // The VM operation name array
135   static const char* _names[];
136 
137  public:
138   VM_Operation() : _calling_thread(NULL), _next(NULL), _prev(NULL) {}
139 
140   // VM operation support (used by VM thread)
141   Thread* calling_thread() const                 { return _calling_thread; }
142   void set_calling_thread(Thread* thread);
143 
144   // Called by VM thread - does in turn invoke doit(). Do not override this
145   void evaluate();
146 
147   // evaluate() is called by the VMThread and in turn calls doit().
148   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
149   // doit_prologue() is called in that thread before transferring control to
150   // the VMThread.
151   // If doit_prologue() returns true the VM operation will proceed, and
152   // doit_epilogue() will be called by the JavaThread once the VM operation
153   // completes. If doit_prologue() returns false the VM operation is cancelled.
154   virtual void doit()                            = 0;
155   virtual bool doit_prologue()                   { return true; };
156   virtual void doit_epilogue()                   {};
157 
158   // Linking
159   VM_Operation *next() const                     { return _next; }
160   VM_Operation *prev() const                     { return _prev; }
161   void set_next(VM_Operation *next)              { _next = next; }
162   void set_prev(VM_Operation *prev)              { _prev = prev; }
163 
164   // Configuration. Override these appropriately in subclasses.
165   virtual VMOp_Type type() const = 0;
166   virtual bool allow_nested_vm_operations() const { return false; }
167 
168   // An operation can either be done inside a safepoint
169   // or concurrently with Java threads running.
170   virtual bool evaluate_at_safepoint() const { return true; }
171 
172   // Debugging
173   virtual void print_on_error(outputStream* st) const;
174   virtual const char* name() const  { return _names[type()]; }
175   static const char* name(int type) {
176     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
177     return _names[type];
178   }
179 #ifndef PRODUCT
180   void print_on(outputStream* st) const { print_on_error(st); }
181 #endif
182 };
183 
184 class VM_None: public VM_Operation {
185   const char* _reason;
186  public:
187   VM_None(const char* reason) : _reason(reason) {}
188   const char* name() const { return _reason; }
189   VMOp_Type type() const { return VMOp_None; }
190   void doit() {};
191 };
192 
193 class VM_Cleanup: public VM_Operation {
194  public:
195   VMOp_Type type() const { return VMOp_Cleanup; }
196   void doit() {};
197 };
198 
199 class VM_ClearICs: public VM_Operation {
200  private:
201   bool _preserve_static_stubs;
202  public:
203   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
204   void doit();
205   VMOp_Type type() const { return VMOp_ClearICs; }
206 };
207 
208 // empty vm op, evaluated just to force a safepoint
209 class VM_ForceSafepoint: public VM_Operation {
210  public:
211   void doit()         {}
212   VMOp_Type type() const { return VMOp_ForceSafepoint; }
213 };
214 
215 // empty vm op, when forcing a safepoint to suspend a thread
216 class VM_ThreadSuspend: public VM_ForceSafepoint {
217  public:
218   VMOp_Type type() const { return VMOp_ThreadSuspend; }
219 };
220 
221 // empty vm op, when forcing a safepoint to suspend threads from jvmti
222 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
223  public:
224   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
225 };
226 
227 // empty vm op, when forcing a safepoint due to inline cache buffers being full
228 class VM_ICBufferFull: public VM_ForceSafepoint {
229  public:
230   VMOp_Type type() const { return VMOp_ICBufferFull; }
231 };
232 
233 // Base class for invoking parts of a gtest in a safepoint.
234 // Derived classes provide the doit method.
235 // Typically also need to transition the gtest thread from native to VM.
236 class VM_GTestExecuteAtSafepoint: public VM_Operation {
237  public:
238   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
239 
240  protected:
241   VM_GTestExecuteAtSafepoint() {}
242 };
243 
244 // Deopt helper that can deoptimize frames in threads other than the
245 // current thread.  Only used through Deoptimization::deoptimize_frame.
246 class VM_DeoptimizeFrame: public VM_Operation {
247   friend class Deoptimization;
248 
249  private:
250   JavaThread* _thread;
251   intptr_t*   _id;
252   int _reason;
253   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
254 
255  public:
256   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
257   void doit();
258   bool allow_nested_vm_operations() const        { return true;  }
259 };
260 
261 #ifndef PRODUCT
262 class VM_DeoptimizeAll: public VM_Operation {
263  private:
264   Klass* _dependee;
265  public:
266   VM_DeoptimizeAll() {}
267   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
268   void doit();
269   bool allow_nested_vm_operations() const        { return true; }
270 };
271 
272 
273 class VM_ZombieAll: public VM_Operation {
274  public:
275   VM_ZombieAll() {}
276   VMOp_Type type() const                         { return VMOp_ZombieAll; }
277   void doit();
278   bool allow_nested_vm_operations() const        { return true; }
279 };
280 #endif // PRODUCT
281 
282 class VM_Verify: public VM_Operation {
283  public:
284   VMOp_Type type() const { return VMOp_Verify; }
285   void doit();
286 };
287 
288 
289 class VM_PrintThreads: public VM_Operation {
290  private:
291   outputStream* _out;
292   bool _print_concurrent_locks;
293   bool _print_extended_info;
294  public:
295   VM_PrintThreads()
296     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
297   {}
298   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
299     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
300   {}
301   VMOp_Type type() const {
302     return VMOp_PrintThreads;
303   }
304   void doit();
305   bool doit_prologue();
306   void doit_epilogue();
307 };
308 
309 class VM_PrintJNI: public VM_Operation {
310  private:
311   outputStream* _out;
312  public:
313   VM_PrintJNI()                         { _out = tty; }
314   VM_PrintJNI(outputStream* out)        { _out = out; }
315   VMOp_Type type() const                { return VMOp_PrintJNI; }
316   void doit();
317 };
318 
319 class VM_PrintMetadata : public VM_Operation {
320  private:
321   outputStream* const _out;
322   const size_t        _scale;
323   const int           _flags;
324 
325  public:
326   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
327     : _out(out), _scale(scale), _flags(flags)
328   {};
329 
330   VMOp_Type type() const  { return VMOp_PrintMetadata; }
331   void doit();
332 };
333 
334 class DeadlockCycle;
335 class VM_FindDeadlocks: public VM_Operation {
336  private:
337   bool              _concurrent_locks;
338   DeadlockCycle*    _deadlocks;
339   outputStream*     _out;
340   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
341                               // which protects the JavaThreads in _deadlocks.
342 
343  public:
344   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _deadlocks(NULL), _out(NULL), _setter() {};
345   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _deadlocks(NULL), _out(st) {};
346   ~VM_FindDeadlocks();
347 
348   DeadlockCycle* result()      { return _deadlocks; };
349   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
350   void doit();
351 };
352 
353 class ThreadDumpResult;
354 class ThreadSnapshot;
355 class ThreadConcurrentLocks;
356 
357 class VM_ThreadDump : public VM_Operation {
358  private:
359   ThreadDumpResult*              _result;
360   int                            _num_threads;
361   GrowableArray<instanceHandle>* _threads;
362   int                            _max_depth;
363   bool                           _with_locked_monitors;
364   bool                           _with_locked_synchronizers;
365 
366   void snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
367 
368  public:
369   VM_ThreadDump(ThreadDumpResult* result,
370                 int max_depth,  // -1 indicates entire stack
371                 bool with_locked_monitors,
372                 bool with_locked_synchronizers);
373 
374   VM_ThreadDump(ThreadDumpResult* result,
375                 GrowableArray<instanceHandle>* threads,
376                 int num_threads, // -1 indicates entire stack
377                 int max_depth,
378                 bool with_locked_monitors,
379                 bool with_locked_synchronizers);
380 
381   VMOp_Type type() const { return VMOp_ThreadDump; }
382   void doit();
383   bool doit_prologue();
384   void doit_epilogue();
385 };
386 
387 
388 class VM_Exit: public VM_Operation {
389  private:
390   int  _exit_code;
391   static volatile bool _vm_exited;
392   static Thread * volatile _shutdown_thread;
393   static void wait_if_vm_exited();
394  public:
395   VM_Exit(int exit_code) {
396     _exit_code = exit_code;
397   }
398   static int wait_for_threads_in_native_to_block();
399   static int set_vm_exited();
400   static bool vm_exited()                      { return _vm_exited; }
401   static Thread * shutdown_thread()            { return _shutdown_thread; }
402   static void block_if_vm_exited() {
403     if (_vm_exited) {
404       wait_if_vm_exited();
405     }
406   }
407   VMOp_Type type() const { return VMOp_Exit; }
408   bool doit_prologue();
409   void doit();
410 };
411 
412 class VM_PrintCompileQueue: public VM_Operation {
413  private:
414   outputStream* _out;
415 
416  public:
417   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
418   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
419   void doit();
420 };
421 
422 class VM_PrintClassLayout: public VM_Operation {
423  private:
424   outputStream* _out;
425   char* _class_name;
426  public:
427   VM_PrintClassLayout(outputStream* st, char* class_name): _out(st), _class_name(class_name) {}
428   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
429   void doit();
430 };
431 
432 #if INCLUDE_SERVICES
433 class VM_PrintClassHierarchy: public VM_Operation {
434  private:
435   outputStream* _out;
436   bool _print_interfaces;
437   bool _print_subclasses;
438   char* _classname;
439 
440  public:
441   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
442     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
443     _classname(classname) {}
444   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
445   void doit();
446 };
447 #endif // INCLUDE_SERVICES
448 
449 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP