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(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(ZVerify)                               \
  75   template(HandshakeOneThread)                    \
  76   template(HandshakeAllThreads)                   \
  77   template(HandshakeFallback)                     \
  78   template(EnableBiasedLocking)                   \
  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(JVMCIResizeCounters)                   \
 117   template(ClassLoaderStatsOperation)             \
 118   template(ClassLoaderHierarchyOperation)         \
 119   template(DumpHashtable)                         \
 120   template(DumpTouchedMethods)                    \
 121   template(MarkActiveNMethods)                    \
 122   template(PrintCompileQueue)                     \
 123   template(PrintClassHierarchy)                   \
 124   template(ThreadSuspend)                         \
 125   template(ThreadsSuspendJVMTI)                   \
 126   template(ICBufferFull)                          \
 127   template(ScavengeMonitors)                      \
 128   template(PrintMetadata)                         \
 129   template(GTestExecuteAtSafepoint)               \
 130   template(JFROldObject)                          \
 131 
 132 class VM_Operation: public CHeapObj<mtInternal> {
 133  public:
 134   enum Mode {
 135     _safepoint,       // blocking,        safepoint, vm_op C-heap allocated
 136     _no_safepoint,    // blocking,     no safepoint, vm_op C-Heap allocated
 137     _concurrent,      // non-blocking, no safepoint, vm_op C-Heap allocated
 138     _async_safepoint  // non-blocking,    safepoint, vm_op C-Heap allocated
 139   };
 140 
 141   enum VMOp_Type {
 142     VM_OPS_DO(VM_OP_ENUM)
 143     VMOp_Terminating
 144   };
 145 
 146  private:
 147   Thread*         _calling_thread;
 148   long            _timestamp;
 149   VM_Operation*   _next;
 150   VM_Operation*   _prev;
 151 
 152   // The VM operation name array
 153   static const char* _names[];
 154 
 155  public:
 156   VM_Operation()  { _calling_thread = NULL; _next = NULL; _prev = NULL; }
 157   virtual ~VM_Operation() {}
 158 
 159   // VM operation support (used by VM thread)
 160   Thread* calling_thread() const                 { return _calling_thread; }
 161   void set_calling_thread(Thread* thread);
 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   virtual 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_None: public VM_Operation {
 227   const char* _reason;
 228  public:
 229   VM_None(const char* reason) : _reason(reason) {}
 230   const char* name() const { return _reason; }
 231   VMOp_Type type() const { return VMOp_None; }
 232   void doit() {};
 233 };
 234 
 235 class VM_Cleanup: public VM_Operation {
 236  public:
 237   VMOp_Type type() const { return VMOp_Cleanup; }
 238   void doit() {};
 239 };
 240 
 241 class VM_ThreadStop: public VM_Operation {
 242  private:
 243   oop     _thread;        // The Thread that the Throwable is thrown against
 244   oop     _throwable;     // The Throwable thrown at the target Thread
 245  public:
 246   // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the
 247   // VM operation is executed.
 248   VM_ThreadStop(oop thread, oop throwable) {
 249     _thread    = thread;
 250     _throwable = throwable;
 251   }
 252   VMOp_Type type() const                         { return VMOp_ThreadStop; }
 253   oop target_thread() const                      { return _thread; }
 254   oop throwable() const                          { return _throwable;}
 255   void doit();
 256   // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated
 257   bool allow_nested_vm_operations() const        { return true; }
 258   Mode evaluation_mode() const                   { return _async_safepoint; }
 259   bool is_cheap_allocated() const                { return true; }
 260 
 261   // GC support
 262   void oops_do(OopClosure* f) {
 263     f->do_oop(&_thread); f->do_oop(&_throwable);
 264   }
 265 };
 266 
 267 class VM_ClearICs: public VM_Operation {
 268  private:
 269   bool _preserve_static_stubs;
 270  public:
 271   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 272   void doit();
 273   VMOp_Type type() const { return VMOp_ClearICs; }
 274 };
 275 
 276 // empty vm op, evaluated just to force a safepoint
 277 class VM_ForceSafepoint: public VM_Operation {
 278  public:
 279   void doit()         {}
 280   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 281 };
 282 
 283 // empty vm op, when forcing a safepoint to suspend a thread
 284 class VM_ThreadSuspend: public VM_ForceSafepoint {
 285  public:
 286   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 287 };
 288 
 289 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 290 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 291  public:
 292   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 293 };
 294 
 295 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 296 class VM_ICBufferFull: public VM_ForceSafepoint {
 297  public:
 298   VMOp_Type type() const { return VMOp_ICBufferFull; }
 299 };
 300 
 301 // empty asynchronous vm op, when forcing a safepoint to scavenge monitors
 302 class VM_ScavengeMonitors: public VM_ForceSafepoint {
 303  public:
 304   VMOp_Type type() const                         { return VMOp_ScavengeMonitors; }
 305   Mode evaluation_mode() const                   { return _async_safepoint; }
 306   bool is_cheap_allocated() const                { return true; }
 307 };
 308 
 309 // Base class for invoking parts of a gtest in a safepoint.
 310 // Derived classes provide the doit method.
 311 // Typically also need to transition the gtest thread from native to VM.
 312 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 313  public:
 314   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 315 
 316  protected:
 317   VM_GTestExecuteAtSafepoint() {}
 318 };
 319 
 320 class VM_MarkActiveNMethods: public VM_Operation {
 321  public:
 322   VM_MarkActiveNMethods() {}
 323   VMOp_Type type() const                         { return VMOp_MarkActiveNMethods; }
 324   void doit();
 325   bool allow_nested_vm_operations() const        { return true; }
 326 };
 327 
 328 // Deopt helper that can deoptimize frames in threads other than the
 329 // current thread.  Only used through Deoptimization::deoptimize_frame.
 330 class VM_DeoptimizeFrame: public VM_Operation {
 331   friend class Deoptimization;
 332 
 333  private:
 334   JavaThread* _thread;
 335   intptr_t*   _id;
 336   int _reason;
 337   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 338 
 339  public:
 340   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 341   void doit();
 342   bool allow_nested_vm_operations() const        { return true;  }
 343 };
 344 
 345 #ifndef PRODUCT
 346 class VM_DeoptimizeAll: public VM_Operation {
 347  private:
 348   Klass* _dependee;
 349  public:
 350   VM_DeoptimizeAll() {}
 351   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 352   void doit();
 353   bool allow_nested_vm_operations() const        { return true; }
 354 };
 355 
 356 
 357 class VM_ZombieAll: public VM_Operation {
 358  public:
 359   VM_ZombieAll() {}
 360   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 361   void doit();
 362   bool allow_nested_vm_operations() const        { return true; }
 363 };
 364 #endif // PRODUCT
 365 
 366 class VM_Verify: public VM_Operation {
 367  public:
 368   VMOp_Type type() const { return VMOp_Verify; }
 369   void doit();
 370 };
 371 
 372 
 373 class VM_PrintThreads: public VM_Operation {
 374  private:
 375   outputStream* _out;
 376   bool _print_concurrent_locks;
 377   bool _print_extended_info;
 378  public:
 379   VM_PrintThreads()
 380     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
 381   {}
 382   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
 383     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
 384   {}
 385   VMOp_Type type() const {
 386     return VMOp_PrintThreads;
 387   }
 388   void doit();
 389   bool doit_prologue();
 390   void doit_epilogue();
 391 };
 392 
 393 class VM_PrintJNI: public VM_Operation {
 394  private:
 395   outputStream* _out;
 396  public:
 397   VM_PrintJNI()                         { _out = tty; }
 398   VM_PrintJNI(outputStream* out)        { _out = out; }
 399   VMOp_Type type() const                { return VMOp_PrintJNI; }
 400   void doit();
 401 };
 402 
 403 class VM_PrintMetadata : public VM_Operation {
 404  private:
 405   outputStream* const _out;
 406   const size_t        _scale;
 407   const int           _flags;
 408 
 409  public:
 410   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
 411     : _out(out), _scale(scale), _flags(flags)
 412   {};
 413 
 414   VMOp_Type type() const  { return VMOp_PrintMetadata; }
 415   void doit();
 416 };
 417 
 418 class DeadlockCycle;
 419 class VM_FindDeadlocks: public VM_Operation {
 420  private:
 421   bool              _concurrent_locks;
 422   DeadlockCycle*    _deadlocks;
 423   outputStream*     _out;
 424   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
 425                               // which protects the JavaThreads in _deadlocks.
 426 
 427  public:
 428   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _deadlocks(NULL), _out(NULL), _setter() {};
 429   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _deadlocks(NULL), _out(st) {};
 430   ~VM_FindDeadlocks();
 431 
 432   DeadlockCycle* result()      { return _deadlocks; };
 433   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 434   void doit();
 435 };
 436 
 437 class ThreadDumpResult;
 438 class ThreadSnapshot;
 439 class ThreadConcurrentLocks;
 440 
 441 class VM_ThreadDump : public VM_Operation {
 442  private:
 443   ThreadDumpResult*              _result;
 444   int                            _num_threads;
 445   GrowableArray<instanceHandle>* _threads;
 446   int                            _max_depth;
 447   bool                           _with_locked_monitors;
 448   bool                           _with_locked_synchronizers;
 449 
 450   void snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 451 
 452  public:
 453   VM_ThreadDump(ThreadDumpResult* result,
 454                 int max_depth,  // -1 indicates entire stack
 455                 bool with_locked_monitors,
 456                 bool with_locked_synchronizers);
 457 
 458   VM_ThreadDump(ThreadDumpResult* result,
 459                 GrowableArray<instanceHandle>* threads,
 460                 int num_threads, // -1 indicates entire stack
 461                 int max_depth,
 462                 bool with_locked_monitors,
 463                 bool with_locked_synchronizers);
 464 
 465   VMOp_Type type() const { return VMOp_ThreadDump; }
 466   void doit();
 467   bool doit_prologue();
 468   void doit_epilogue();
 469 };
 470 
 471 
 472 class VM_Exit: public VM_Operation {
 473  private:
 474   int  _exit_code;
 475   static volatile bool _vm_exited;
 476   static Thread * volatile _shutdown_thread;
 477   static void wait_if_vm_exited();
 478  public:
 479   VM_Exit(int exit_code) {
 480     _exit_code = exit_code;
 481   }
 482   static int wait_for_threads_in_native_to_block();
 483   static int set_vm_exited();
 484   static bool vm_exited()                      { return _vm_exited; }
 485   static Thread * shutdown_thread()            { return _shutdown_thread; }
 486   static void block_if_vm_exited() {
 487     if (_vm_exited) {
 488       wait_if_vm_exited();
 489     }
 490   }
 491   VMOp_Type type() const { return VMOp_Exit; }
 492   void doit();
 493 };
 494 
 495 class VM_PrintCompileQueue: public VM_Operation {
 496  private:
 497   outputStream* _out;
 498 
 499  public:
 500   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 501   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 502   Mode evaluation_mode() const { return _safepoint; }
 503   void doit();
 504 };
 505 
 506 #if INCLUDE_SERVICES
 507 class VM_PrintClassHierarchy: public VM_Operation {
 508  private:
 509   outputStream* _out;
 510   bool _print_interfaces;
 511   bool _print_subclasses;
 512   char* _classname;
 513 
 514  public:
 515   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 516     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 517     _classname(classname) {}
 518   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 519   void doit();
 520 };
 521 #endif // INCLUDE_SERVICES
 522 
 523 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP