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