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(GetObjectMonitorUsage)                 \
  80   template(GetAllStackTraces)                     \
  81   template(GetThreadListStackTraces)              \
  82   template(ChangeBreakpoints)                     \
  83   template(GetOrSetLocal)                         \
  84   template(ChangeSingleStep)                      \
  85   template(HeapWalkOperation)                     \
  86   template(HeapIterateOperation)                  \
  87   template(ReportJavaOutOfMemory)                 \
  88   template(JFRCheckpoint)                         \
  89   template(ShenandoahFullGC)                      \
  90   template(ShenandoahInitMark)                    \
  91   template(ShenandoahFinalMarkStartEvac)          \
  92   template(ShenandoahInitUpdateRefs)              \
  93   template(ShenandoahFinalUpdateRefs)             \
  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(ThreadsSuspendJVMTI)                   \
 109   template(ICBufferFull)                          \
 110   template(ScavengeMonitors)                      \
 111   template(PrintMetadata)                         \
 112   template(GTestExecuteAtSafepoint)               \
 113   template(JFROldObject)                          \
 114 
 115 class VM_Operation : public StackObj {
 116  public:
 117   enum VMOp_Type {
 118     VM_OPS_DO(VM_OP_ENUM)
 119     VMOp_Terminating
 120   };
 121 
 122  private:
 123   Thread*         _calling_thread;
 124   VM_Operation*   _next;
 125   VM_Operation*   _prev;
 126 
 127   // The VM operation name array
 128   static const char* _names[];
 129 
 130  public:
 131   VM_Operation() : _calling_thread(NULL), _next(NULL), _prev(NULL) {}
 132 
 133   // VM operation support (used by VM thread)
 134   Thread* calling_thread() const                 { return _calling_thread; }
 135   void set_calling_thread(Thread* thread);
 136 
 137   // Called by VM thread - does in turn invoke doit(). Do not override this
 138   void evaluate();
 139 
 140   // evaluate() is called by the VMThread and in turn calls doit().
 141   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
 142   // doit_prologue() is called in that thread before transferring control to
 143   // the VMThread.
 144   // If doit_prologue() returns true the VM operation will proceed, and
 145   // doit_epilogue() will be called by the JavaThread once the VM operation
 146   // completes. If doit_prologue() returns false the VM operation is cancelled.
 147   virtual void doit()                            = 0;
 148   virtual bool doit_prologue()                   { return true; };
 149   virtual void doit_epilogue()                   {};
 150 
 151   // Linking
 152   VM_Operation *next() const                     { return _next; }
 153   VM_Operation *prev() const                     { return _prev; }
 154   void set_next(VM_Operation *next)              { _next = next; }
 155   void set_prev(VM_Operation *prev)              { _prev = prev; }
 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   // An operation can either be done inside a safepoint
 162   // or concurrently with Java threads running.
 163   virtual bool evaluate_at_safepoint() const { return true; }
 164 
 165   // Debugging
 166   virtual void print_on_error(outputStream* st) const;
 167   virtual const char* name() const  { return _names[type()]; }
 168   static const char* name(int type) {
 169     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 170     return _names[type];
 171   }
 172 #ifndef PRODUCT
 173   void print_on(outputStream* st) const { print_on_error(st); }
 174 #endif
 175 };
 176 
 177 class VM_None: public VM_Operation {
 178   const char* _reason;
 179  public:
 180   VM_None(const char* reason) : _reason(reason) {}
 181   const char* name() const { return _reason; }
 182   VMOp_Type type() const { return VMOp_None; }
 183   void doit() {};
 184 };
 185 
 186 class VM_Cleanup: public VM_Operation {
 187  public:
 188   VMOp_Type type() const { return VMOp_Cleanup; }
 189   void doit() {};
 190 };
 191 
 192 class VM_ClearICs: public VM_Operation {
 193  private:
 194   bool _preserve_static_stubs;
 195  public:
 196   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 197   void doit();
 198   VMOp_Type type() const { return VMOp_ClearICs; }
 199 };
 200 
 201 // empty vm op, evaluated just to force a safepoint
 202 class VM_ForceSafepoint: public VM_Operation {
 203  public:
 204   void doit()         {}
 205   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 206 };
 207 
 208 // empty vm op, when forcing a safepoint to suspend a thread
 209 class VM_ThreadSuspend: public VM_ForceSafepoint {
 210  public:
 211   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 212 };
 213 
 214 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 215 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 216  public:
 217   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 218 };
 219 
 220 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 221 class VM_ICBufferFull: public VM_ForceSafepoint {
 222  public:
 223   VMOp_Type type() const { return VMOp_ICBufferFull; }
 224 };
 225 
 226 // Base class for invoking parts of a gtest in a safepoint.
 227 // Derived classes provide the doit method.
 228 // Typically also need to transition the gtest thread from native to VM.
 229 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 230  public:
 231   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 232 
 233  protected:
 234   VM_GTestExecuteAtSafepoint() {}
 235 };
 236 
 237 class VM_CleanClassLoaderDataMetaspaces : public VM_Operation {
 238  public:
 239   VM_CleanClassLoaderDataMetaspaces() {}
 240   VMOp_Type type() const                         { return VMOp_CleanClassLoaderDataMetaspaces; }
 241   void doit();
 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 #if INCLUDE_SERVICES
 423 class VM_PrintClassHierarchy: public VM_Operation {
 424  private:
 425   outputStream* _out;
 426   bool _print_interfaces;
 427   bool _print_subclasses;
 428   char* _classname;
 429 
 430  public:
 431   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 432     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 433     _classname(classname) {}
 434   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 435   void doit();
 436 };
 437 #endif // INCLUDE_SERVICES
 438 
 439 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP