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