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