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