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