1 /*
   2  * Copyright (c) 1997, 2019, 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(ThreadStop)                            \
  46   template(ThreadDump)                            \
  47   template(PrintThreads)                          \
  48   template(FindDeadlocks)                         \
  49   template(ClearICs)                              \
  50   template(ForceSafepoint)                        \
  51   template(ForceAsyncSafepoint)                   \
  52   template(Deoptimize)                            \
  53   template(DeoptimizeFrame)                       \
  54   template(DeoptimizeAll)                         \
  55   template(ZombieAll)                             \
  56   template(Verify)                                \
  57   template(PrintJNI)                              \
  58   template(HeapDumper)                            \
  59   template(DeoptimizeTheWorld)                    \
  60   template(CollectForMetadataAllocation)          \
  61   template(GC_HeapInspection)                     \
  62   template(GenCollectFull)                        \
  63   template(GenCollectFullConcurrent)              \
  64   template(GenCollectForAllocation)               \
  65   template(ParallelGCFailedAllocation)            \
  66   template(ParallelGCSystemGC)                    \
  67   template(CMS_Initial_Mark)                      \
  68   template(CMS_Final_Remark)                      \
  69   template(G1CollectForAllocation)                \
  70   template(G1CollectFull)                         \
  71   template(G1Concurrent)                          \
  72   template(ZMarkStart)                            \
  73   template(ZMarkEnd)                              \
  74   template(ZRelocateStart)                        \
  75   template(ZVerify)                               \
  76   template(HandshakeOneThread)                    \
  77   template(HandshakeAllThreads)                   \
  78   template(HandshakeFallback)                     \
  79   template(EnableBiasedLocking)                   \
  80   template(BulkRevokeBias)                        \
  81   template(PopulateDumpSharedSpace)               \
  82   template(JNIFunctionTableCopier)                \
  83   template(RedefineClasses)                       \
  84   template(UpdateForPopTopFrame)                  \
  85   template(SetFramePop)                           \
  86   template(GetOwnedMonitorInfo)                   \
  87   template(GetObjectMonitorUsage)                 \
  88   template(GetCurrentContendedMonitor)            \
  89   template(GetStackTrace)                         \
  90   template(GetMultipleStackTraces)                \
  91   template(GetAllStackTraces)                     \
  92   template(GetThreadListStackTraces)              \
  93   template(GetFrameCount)                         \
  94   template(GetFrameLocation)                      \
  95   template(GetFiberThread)                        \
  96   template(GetFiberStackTrace)                    \
  97   template(GetFiberFrameCount)                    \
  98   template(GetFiberFrameLocation)                 \
  99   template(ChangeBreakpoints)                     \
 100   template(GetOrSetLocal)                         \
 101   template(GetCurrentLocation)                    \
 102   template(EnterInterpOnlyMode)                   \
 103   template(ChangeSingleStep)                      \
 104   template(HeapWalkOperation)                     \
 105   template(HeapIterateOperation)                  \
 106   template(ReportJavaOutOfMemory)                 \
 107   template(JFRCheckpoint)                         \
 108   template(ShenandoahFullGC)                      \
 109   template(ShenandoahInitMark)                    \
 110   template(ShenandoahFinalMarkStartEvac)          \
 111   template(ShenandoahFinalEvac)                   \
 112   template(ShenandoahInitTraversalGC)             \
 113   template(ShenandoahFinalTraversalGC)            \
 114   template(ShenandoahInitUpdateRefs)              \
 115   template(ShenandoahFinalUpdateRefs)             \
 116   template(ShenandoahDegeneratedGC)               \
 117   template(Exit)                                  \
 118   template(LinuxDllLoad)                          \
 119   template(RotateGCLog)                           \
 120   template(WhiteBoxOperation)                     \
 121   template(JVMCIResizeCounters)                   \
 122   template(ClassLoaderStatsOperation)             \
 123   template(ClassLoaderHierarchyOperation)         \
 124   template(DumpHashtable)                         \
 125   template(DumpTouchedMethods)                    \
 126   template(MarkActiveNMethods)                    \
 127   template(PrintCompileQueue)                     \
 128   template(PrintClassHierarchy)                   \
 129   template(ThreadSuspend)                         \
 130   template(ThreadsSuspendJVMTI)                   \
 131   template(ICBufferFull)                          \
 132   template(ScavengeMonitors)                      \
 133   template(PrintMetadata)                         \
 134   template(GTestExecuteAtSafepoint)               \
 135   template(JFROldObject)                          \
 136 
 137 class VM_Operation: public CHeapObj<mtInternal> {
 138  public:
 139   enum Mode {
 140     _safepoint,       // blocking,        safepoint, vm_op C-heap allocated
 141     _no_safepoint,    // blocking,     no safepoint, vm_op C-Heap allocated
 142     _concurrent,      // non-blocking, no safepoint, vm_op C-Heap allocated
 143     _async_safepoint  // non-blocking,    safepoint, vm_op C-Heap allocated
 144   };
 145 
 146   enum VMOp_Type {
 147     VM_OPS_DO(VM_OP_ENUM)
 148     VMOp_Terminating
 149   };
 150 
 151  private:
 152   Thread*         _calling_thread;
 153   ThreadPriority  _priority;
 154   long            _timestamp;
 155   VM_Operation*   _next;
 156   VM_Operation*   _prev;
 157 
 158   // The VM operation name array
 159   static const char* _names[];
 160 
 161  public:
 162   VM_Operation()  { _calling_thread = NULL; _next = NULL; _prev = NULL; }
 163   virtual ~VM_Operation() {}
 164 
 165   // VM operation support (used by VM thread)
 166   Thread* calling_thread() const                 { return _calling_thread; }
 167   ThreadPriority priority()                      { return _priority; }
 168   void set_calling_thread(Thread* thread, ThreadPriority priority);
 169 
 170   long timestamp() const              { return _timestamp; }
 171   void set_timestamp(long timestamp)  { _timestamp = timestamp; }
 172 
 173   // Called by VM thread - does in turn invoke doit(). Do not override this
 174   void evaluate();
 175 
 176   // evaluate() is called by the VMThread and in turn calls doit().
 177   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
 178   // doit_prologue() is called in that thread before transferring control to
 179   // the VMThread.
 180   // If doit_prologue() returns true the VM operation will proceed, and
 181   // doit_epilogue() will be called by the JavaThread once the VM operation
 182   // completes. If doit_prologue() returns false the VM operation is cancelled.
 183   virtual void doit()                            = 0;
 184   virtual bool doit_prologue()                   { return true; };
 185   virtual void doit_epilogue()                   {}; // Note: Not called if mode is: _concurrent
 186 
 187   // Type test
 188   virtual bool is_methodCompiler() const         { return false; }
 189 
 190   // Linking
 191   VM_Operation *next() const                     { return _next; }
 192   VM_Operation *prev() const                     { return _prev; }
 193   void set_next(VM_Operation *next)              { _next = next; }
 194   void set_prev(VM_Operation *prev)              { _prev = prev; }
 195 
 196   // Configuration. Override these appropriately in subclasses.
 197   virtual VMOp_Type type() const = 0;
 198   virtual Mode evaluation_mode() const            { return _safepoint; }
 199   virtual bool allow_nested_vm_operations() const { return false; }
 200   virtual bool is_cheap_allocated() const         { return false; }
 201   virtual void oops_do(OopClosure* f)              { /* do nothing */ };
 202 
 203   // CAUTION: <don't hang yourself with following rope>
 204   // If you override these methods, make sure that the evaluation
 205   // of these methods is race-free and non-blocking, since these
 206   // methods may be evaluated either by the mutators or by the
 207   // vm thread, either concurrently with mutators or with the mutators
 208   // stopped. In other words, taking locks is verboten, and if there
 209   // are any races in evaluating the conditions, they'd better be benign.
 210   virtual bool evaluate_at_safepoint() const {
 211     return evaluation_mode() == _safepoint  ||
 212            evaluation_mode() == _async_safepoint;
 213   }
 214   virtual bool evaluate_concurrently() const {
 215     return evaluation_mode() == _concurrent ||
 216            evaluation_mode() == _async_safepoint;
 217   }
 218 
 219   static const char* mode_to_string(Mode mode);
 220 
 221   // Debugging
 222   virtual void print_on_error(outputStream* st) const;
 223   virtual const char* name() const  { return _names[type()]; }
 224   static const char* name(int type) {
 225     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 226     return _names[type];
 227   }
 228 #ifndef PRODUCT
 229   void print_on(outputStream* st) const { print_on_error(st); }
 230 #endif
 231 };
 232 
 233 class VM_None: public VM_Operation {
 234   const char* _reason;
 235  public:
 236   VM_None(const char* reason) : _reason(reason) {}
 237   const char* name() const { return _reason; }
 238   VMOp_Type type() const { return VMOp_None; }
 239   void doit() {};
 240 };
 241 
 242 class VM_Cleanup: public VM_Operation {
 243  public:
 244   VMOp_Type type() const { return VMOp_Cleanup; }
 245   void doit() {};
 246 };
 247 
 248 class VM_ThreadStop: public VM_Operation {
 249  private:
 250   oop     _thread;        // The Thread that the Throwable is thrown against
 251   oop     _throwable;     // The Throwable thrown at the target Thread
 252  public:
 253   // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the
 254   // VM operation is executed.
 255   VM_ThreadStop(oop thread, oop throwable) {
 256     _thread    = thread;
 257     _throwable = throwable;
 258   }
 259   VMOp_Type type() const                         { return VMOp_ThreadStop; }
 260   oop target_thread() const                      { return _thread; }
 261   oop throwable() const                          { return _throwable;}
 262   void doit();
 263   // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated
 264   bool allow_nested_vm_operations() const        { return true; }
 265   Mode evaluation_mode() const                   { return _async_safepoint; }
 266   bool is_cheap_allocated() const                { return true; }
 267 
 268   // GC support
 269   void oops_do(OopClosure* f) {
 270     f->do_oop(&_thread); f->do_oop(&_throwable);
 271   }
 272 };
 273 
 274 class VM_ClearICs: public VM_Operation {
 275  private:
 276   bool _preserve_static_stubs;
 277  public:
 278   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 279   void doit();
 280   VMOp_Type type() const { return VMOp_ClearICs; }
 281 };
 282 
 283 // empty vm op, evaluated just to force a safepoint
 284 class VM_ForceSafepoint: public VM_Operation {
 285  public:
 286   void doit()         {}
 287   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 288 };
 289 
 290 // empty vm op, when forcing a safepoint to suspend a thread
 291 class VM_ThreadSuspend: public VM_ForceSafepoint {
 292  public:
 293   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 294 };
 295 
 296 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 297 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 298  public:
 299   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 300 };
 301 
 302 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 303 class VM_ICBufferFull: public VM_ForceSafepoint {
 304  public:
 305   VMOp_Type type() const { return VMOp_ICBufferFull; }
 306 };
 307 
 308 // empty asynchronous vm op, when forcing a safepoint to scavenge monitors
 309 class VM_ScavengeMonitors: public VM_ForceSafepoint {
 310  public:
 311   VMOp_Type type() const                         { return VMOp_ScavengeMonitors; }
 312   Mode evaluation_mode() const                   { return _async_safepoint; }
 313   bool is_cheap_allocated() const                { return true; }
 314 };
 315 
 316 // Base class for invoking parts of a gtest in a safepoint.
 317 // Derived classes provide the doit method.
 318 // Typically also need to transition the gtest thread from native to VM.
 319 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 320  public:
 321   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 322 
 323  protected:
 324   VM_GTestExecuteAtSafepoint() {}
 325 };
 326 
 327 class VM_Deoptimize: public VM_Operation {
 328  public:
 329   VM_Deoptimize() {}
 330   VMOp_Type type() const                        { return VMOp_Deoptimize; }
 331   void doit();
 332   bool allow_nested_vm_operations() const        { return true; }
 333 };
 334 
 335 class VM_MarkActiveNMethods: public VM_Operation {
 336  public:
 337   VM_MarkActiveNMethods() {}
 338   VMOp_Type type() const                         { return VMOp_MarkActiveNMethods; }
 339   void doit();
 340   bool allow_nested_vm_operations() const        { return true; }
 341 };
 342 
 343 // Deopt helper that can deoptimize frames in threads other than the
 344 // current thread.  Only used through Deoptimization::deoptimize_frame.
 345 class VM_DeoptimizeFrame: public VM_Operation {
 346   friend class Deoptimization;
 347 
 348  private:
 349   JavaThread* _thread;
 350   intptr_t*   _id;
 351   int _reason;
 352   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 353 
 354  public:
 355   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 356   void doit();
 357   bool allow_nested_vm_operations() const        { return true;  }
 358 };
 359 
 360 #ifndef PRODUCT
 361 class VM_DeoptimizeAll: public VM_Operation {
 362  private:
 363   Klass* _dependee;
 364  public:
 365   VM_DeoptimizeAll() {}
 366   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 367   void doit();
 368   bool allow_nested_vm_operations() const        { return true; }
 369 };
 370 
 371 
 372 class VM_ZombieAll: public VM_Operation {
 373  public:
 374   VM_ZombieAll() {}
 375   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 376   void doit();
 377   bool allow_nested_vm_operations() const        { return true; }
 378 };
 379 #endif // PRODUCT
 380 
 381 class VM_Verify: public VM_Operation {
 382  public:
 383   VMOp_Type type() const { return VMOp_Verify; }
 384   void doit();
 385 };
 386 
 387 
 388 class VM_PrintThreads: public VM_Operation {
 389  private:
 390   outputStream* _out;
 391   bool _print_concurrent_locks;
 392   bool _print_extended_info;
 393  public:
 394   VM_PrintThreads()
 395     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
 396   {}
 397   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
 398     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
 399   {}
 400   VMOp_Type type() const {
 401     return VMOp_PrintThreads;
 402   }
 403   void doit();
 404   bool doit_prologue();
 405   void doit_epilogue();
 406 };
 407 
 408 class VM_PrintJNI: public VM_Operation {
 409  private:
 410   outputStream* _out;
 411  public:
 412   VM_PrintJNI()                         { _out = tty; }
 413   VM_PrintJNI(outputStream* out)        { _out = out; }
 414   VMOp_Type type() const                { return VMOp_PrintJNI; }
 415   void doit();
 416 };
 417 
 418 class VM_PrintMetadata : public VM_Operation {
 419  private:
 420   outputStream* const _out;
 421   const size_t        _scale;
 422   const int           _flags;
 423 
 424  public:
 425   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
 426     : _out(out), _scale(scale), _flags(flags)
 427   {};
 428 
 429   VMOp_Type type() const  { return VMOp_PrintMetadata; }
 430   void doit();
 431 };
 432 
 433 class DeadlockCycle;
 434 class VM_FindDeadlocks: public VM_Operation {
 435  private:
 436   bool              _concurrent_locks;
 437   DeadlockCycle*    _deadlocks;
 438   outputStream*     _out;
 439   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
 440                               // which protects the JavaThreads in _deadlocks.
 441 
 442  public:
 443   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _deadlocks(NULL), _out(NULL), _setter() {};
 444   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _deadlocks(NULL), _out(st) {};
 445   ~VM_FindDeadlocks();
 446 
 447   DeadlockCycle* result()      { return _deadlocks; };
 448   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 449   void doit();
 450 };
 451 
 452 class ThreadDumpResult;
 453 class ThreadSnapshot;
 454 class ThreadConcurrentLocks;
 455 
 456 class VM_ThreadDump : public VM_Operation {
 457  private:
 458   ThreadDumpResult*              _result;
 459   int                            _num_threads;
 460   GrowableArray<instanceHandle>* _threads;
 461   int                            _max_depth;
 462   bool                           _with_locked_monitors;
 463   bool                           _with_locked_synchronizers;
 464 
 465   void snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 466 
 467  public:
 468   VM_ThreadDump(ThreadDumpResult* result,
 469                 int max_depth,  // -1 indicates entire stack
 470                 bool with_locked_monitors,
 471                 bool with_locked_synchronizers);
 472 
 473   VM_ThreadDump(ThreadDumpResult* result,
 474                 GrowableArray<instanceHandle>* threads,
 475                 int num_threads, // -1 indicates entire stack
 476                 int max_depth,
 477                 bool with_locked_monitors,
 478                 bool with_locked_synchronizers);
 479 
 480   VMOp_Type type() const { return VMOp_ThreadDump; }
 481   void doit();
 482   bool doit_prologue();
 483   void doit_epilogue();
 484 };
 485 
 486 
 487 class VM_Exit: public VM_Operation {
 488  private:
 489   int  _exit_code;
 490   static volatile bool _vm_exited;
 491   static Thread * volatile _shutdown_thread;
 492   static void wait_if_vm_exited();
 493  public:
 494   VM_Exit(int exit_code) {
 495     _exit_code = exit_code;
 496   }
 497   static int wait_for_threads_in_native_to_block();
 498   static int set_vm_exited();
 499   static bool vm_exited()                      { return _vm_exited; }
 500   static Thread * shutdown_thread()            { return _shutdown_thread; }
 501   static void block_if_vm_exited() {
 502     if (_vm_exited) {
 503       wait_if_vm_exited();
 504     }
 505   }
 506   VMOp_Type type() const { return VMOp_Exit; }
 507   void doit();
 508 };
 509 
 510 class VM_PrintCompileQueue: public VM_Operation {
 511  private:
 512   outputStream* _out;
 513 
 514  public:
 515   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 516   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 517   Mode evaluation_mode() const { return _safepoint; }
 518   void doit();
 519 };
 520 
 521 #if INCLUDE_SERVICES
 522 class VM_PrintClassHierarchy: public VM_Operation {
 523  private:
 524   outputStream* _out;
 525   bool _print_interfaces;
 526   bool _print_subclasses;
 527   char* _classname;
 528 
 529  public:
 530   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 531     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 532     _classname(classname) {}
 533   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 534   void doit();
 535 };
 536 #endif // INCLUDE_SERVICES
 537 
 538 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP