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