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