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