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(ZOperation)                            \
  73   template(HandshakeOneThread)                    \
  74   template(HandshakeAllThreads)                   \
  75   template(HandshakeFallback)                     \
  76   template(EnableBiasedLocking)                   \
  77   template(RevokeBias)                            \
  78   template(BulkRevokeBias)                        \
  79   template(PopulateDumpSharedSpace)               \
  80   template(JNIFunctionTableCopier)                \
  81   template(RedefineClasses)                       \
  82   template(UpdateForPopTopFrame)                  \
  83   template(SetFramePop)                           \
  84   template(GetOwnedMonitorInfo)                   \
  85   template(GetObjectMonitorUsage)                 \
  86   template(GetCurrentContendedMonitor)            \
  87   template(GetStackTrace)                         \
  88   template(GetMultipleStackTraces)                \
  89   template(GetAllStackTraces)                     \
  90   template(GetThreadListStackTraces)              \
  91   template(GetFrameCount)                         \
  92   template(GetFrameLocation)                      \
  93   template(ChangeBreakpoints)                     \
  94   template(GetOrSetLocal)                         \
  95   template(GetCurrentLocation)                    \
  96   template(EnterInterpOnlyMode)                   \
  97   template(ChangeSingleStep)                      \
  98   template(HeapWalkOperation)                     \
  99   template(HeapIterateOperation)                  \
 100   template(ReportJavaOutOfMemory)                 \
 101   template(JFRCheckpoint)                         \
 102   template(ShenandoahFullGC)                      \
 103   template(ShenandoahInitMark)                    \
 104   template(ShenandoahFinalMarkStartEvac)          \
 105   template(ShenandoahFinalEvac)                   \
 106   template(ShenandoahInitTraversalGC)             \
 107   template(ShenandoahFinalTraversalGC)            \
 108   template(ShenandoahInitUpdateRefs)              \
 109   template(ShenandoahFinalUpdateRefs)             \
 110   template(ShenandoahDegeneratedGC)               \
 111   template(Exit)                                  \
 112   template(LinuxDllLoad)                          \
 113   template(RotateGCLog)                           \
 114   template(WhiteBoxOperation)                     \
 115   template(ClassLoaderStatsOperation)             \
 116   template(ClassLoaderHierarchyOperation)         \
 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   // Debugging
 215   virtual void print_on_error(outputStream* st) const;
 216   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_ThreadStop: public VM_Operation {
 227  private:
 228   oop     _thread;        // The Thread that the Throwable is thrown against
 229   oop     _throwable;     // The Throwable thrown at the target Thread
 230  public:
 231   // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the
 232   // VM operation is executed.
 233   VM_ThreadStop(oop thread, oop throwable) {
 234     _thread    = thread;
 235     _throwable = throwable;
 236   }
 237   VMOp_Type type() const                         { return VMOp_ThreadStop; }
 238   oop target_thread() const                      { return _thread; }
 239   oop throwable() const                          { return _throwable;}
 240   void doit();
 241   // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated
 242   bool allow_nested_vm_operations() const        { return true; }
 243   Mode evaluation_mode() const                   { return _async_safepoint; }
 244   bool is_cheap_allocated() const                { return true; }
 245 
 246   // GC support
 247   void oops_do(OopClosure* f) {
 248     f->do_oop(&_thread); f->do_oop(&_throwable);
 249   }
 250 };
 251 
 252 class VM_ClearICs: public VM_Operation {
 253  private:
 254   bool _preserve_static_stubs;
 255  public:
 256   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 257   void doit();
 258   VMOp_Type type() const { return VMOp_ClearICs; }
 259 };
 260 
 261 // empty vm op, evaluated just to force a safepoint
 262 class VM_ForceSafepoint: public VM_Operation {
 263  public:
 264   void doit()         {}
 265   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 266 };
 267 
 268 // empty vm op, when forcing a safepoint to suspend a thread
 269 class VM_ThreadSuspend: public VM_ForceSafepoint {
 270  public:
 271   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 272 };
 273 
 274 // empty vm op, when forcing a safepoint due to ctw threshold is reached for the sweeper
 275 class VM_CTWThreshold: public VM_ForceSafepoint {
 276  public:
 277   VMOp_Type type() const { return VMOp_CTWThreshold; }
 278 };
 279 
 280 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 281 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 282  public:
 283   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 284 };
 285 
 286 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 287 class VM_ICBufferFull: public VM_ForceSafepoint {
 288  public:
 289   VMOp_Type type() const { return VMOp_ICBufferFull; }
 290 };
 291 
 292 // empty asynchronous vm op, when forcing a safepoint to scavenge monitors
 293 class VM_ScavengeMonitors: public VM_ForceSafepoint {
 294  public:
 295   VMOp_Type type() const                         { return VMOp_ScavengeMonitors; }
 296   Mode evaluation_mode() const                   { return _async_safepoint; }
 297   bool is_cheap_allocated() const                { return true; }
 298 };
 299 
 300 // Base class for invoking parts of a gtest in a safepoint.
 301 // Derived classes provide the doit method.
 302 // Typically also need to transition the gtest thread from native to VM.
 303 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 304  public:
 305   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 306 
 307  protected:
 308   VM_GTestExecuteAtSafepoint() {}
 309 };
 310 
 311 class VM_Deoptimize: public VM_Operation {
 312  public:
 313   VM_Deoptimize() {}
 314   VMOp_Type type() const                        { return VMOp_Deoptimize; }
 315   void doit();
 316   bool allow_nested_vm_operations() const        { return true; }
 317 };
 318 
 319 class VM_MarkActiveNMethods: public VM_Operation {
 320  public:
 321   VM_MarkActiveNMethods() {}
 322   VMOp_Type type() const                         { return VMOp_MarkActiveNMethods; }
 323   void doit();
 324   bool allow_nested_vm_operations() const        { return true; }
 325 };
 326 
 327 // Deopt helper that can deoptimize frames in threads other than the
 328 // current thread.  Only used through Deoptimization::deoptimize_frame.
 329 class VM_DeoptimizeFrame: public VM_Operation {
 330   friend class Deoptimization;
 331 
 332  private:
 333   JavaThread* _thread;
 334   intptr_t*   _id;
 335   int _reason;
 336   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 337 
 338  public:
 339   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 340   void doit();
 341   bool allow_nested_vm_operations() const        { return true;  }
 342 };
 343 
 344 #ifndef PRODUCT
 345 class VM_DeoptimizeAll: public VM_Operation {
 346  private:
 347   Klass* _dependee;
 348  public:
 349   VM_DeoptimizeAll() {}
 350   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 351   void doit();
 352   bool allow_nested_vm_operations() const        { return true; }
 353 };
 354 
 355 
 356 class VM_ZombieAll: public VM_Operation {
 357  public:
 358   VM_ZombieAll() {}
 359   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 360   void doit();
 361   bool allow_nested_vm_operations() const        { return true; }
 362 };
 363 #endif // PRODUCT
 364 
 365 class VM_UnlinkSymbols: public VM_Operation {
 366  public:
 367   VM_UnlinkSymbols() {}
 368   VMOp_Type type() const                         { return VMOp_UnlinkSymbols; }
 369   void doit();
 370   bool allow_nested_vm_operations() const        { return true; }
 371 };
 372 
 373 class VM_Verify: public VM_Operation {
 374  public:
 375   VMOp_Type type() const { return VMOp_Verify; }
 376   void doit();
 377 };
 378 
 379 
 380 class VM_PrintThreads: public VM_Operation {
 381  private:
 382   outputStream* _out;
 383   bool _print_concurrent_locks;
 384   bool _print_extended_info;
 385  public:
 386   VM_PrintThreads()
 387     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
 388   {}
 389   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
 390     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
 391   {}
 392   VMOp_Type type() const {
 393     return VMOp_PrintThreads;
 394   }
 395   void doit();
 396   bool doit_prologue();
 397   void doit_epilogue();
 398 };
 399 
 400 class VM_PrintJNI: public VM_Operation {
 401  private:
 402   outputStream* _out;
 403  public:
 404   VM_PrintJNI()                         { _out = tty; }
 405   VM_PrintJNI(outputStream* out)        { _out = out; }
 406   VMOp_Type type() const                { return VMOp_PrintJNI; }
 407   void doit();
 408 };
 409 
 410 class VM_PrintMetadata : public VM_Operation {
 411  private:
 412   outputStream* const _out;
 413   const size_t        _scale;
 414   const int           _flags;
 415 
 416  public:
 417   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
 418     : _out(out), _scale(scale), _flags(flags)
 419   {};
 420 
 421   VMOp_Type type() const  { return VMOp_PrintMetadata; }
 422   void doit();
 423 };
 424 
 425 class DeadlockCycle;
 426 class VM_FindDeadlocks: public VM_Operation {
 427  private:
 428   bool              _concurrent_locks;
 429   DeadlockCycle*    _deadlocks;
 430   outputStream*     _out;
 431   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
 432                               // which protects the JavaThreads in _deadlocks.
 433 
 434  public:
 435   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _out(NULL), _deadlocks(NULL), _setter() {};
 436   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _out(st), _deadlocks(NULL) {};
 437   ~VM_FindDeadlocks();
 438 
 439   DeadlockCycle* result()      { return _deadlocks; };
 440   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 441   void doit();
 442 };
 443 
 444 class ThreadDumpResult;
 445 class ThreadSnapshot;
 446 class ThreadConcurrentLocks;
 447 
 448 class VM_ThreadDump : public VM_Operation {
 449  private:
 450   ThreadDumpResult*              _result;
 451   int                            _num_threads;
 452   GrowableArray<instanceHandle>* _threads;
 453   int                            _max_depth;
 454   bool                           _with_locked_monitors;
 455   bool                           _with_locked_synchronizers;
 456 
 457   ThreadSnapshot* snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 458 
 459  public:
 460   VM_ThreadDump(ThreadDumpResult* result,
 461                 int max_depth,  // -1 indicates entire stack
 462                 bool with_locked_monitors,
 463                 bool with_locked_synchronizers);
 464 
 465   VM_ThreadDump(ThreadDumpResult* result,
 466                 GrowableArray<instanceHandle>* threads,
 467                 int num_threads, // -1 indicates entire stack
 468                 int max_depth,
 469                 bool with_locked_monitors,
 470                 bool with_locked_synchronizers);
 471 
 472   VMOp_Type type() const { return VMOp_ThreadDump; }
 473   void doit();
 474   bool doit_prologue();
 475   void doit_epilogue();
 476 };
 477 
 478 
 479 class VM_Exit: public VM_Operation {
 480  private:
 481   int  _exit_code;
 482   static volatile bool _vm_exited;
 483   static Thread * volatile _shutdown_thread;
 484   static void wait_if_vm_exited();
 485  public:
 486   VM_Exit(int exit_code) {
 487     _exit_code = exit_code;
 488   }
 489   static int wait_for_threads_in_native_to_block();
 490   static int set_vm_exited();
 491   static bool vm_exited()                      { return _vm_exited; }
 492   static Thread * shutdown_thread()            { return _shutdown_thread; }
 493   static void block_if_vm_exited() {
 494     if (_vm_exited) {
 495       wait_if_vm_exited();
 496     }
 497   }
 498   VMOp_Type type() const { return VMOp_Exit; }
 499   void doit();
 500 };
 501 
 502 class VM_PrintCompileQueue: public VM_Operation {
 503  private:
 504   outputStream* _out;
 505 
 506  public:
 507   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 508   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 509   Mode evaluation_mode() const { return _safepoint; }
 510   void doit();
 511 };
 512 
 513 #if INCLUDE_SERVICES
 514 class VM_PrintClassHierarchy: public VM_Operation {
 515  private:
 516   outputStream* _out;
 517   bool _print_interfaces;
 518   bool _print_subclasses;
 519   char* _classname;
 520 
 521  public:
 522   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 523     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 524     _classname(classname) {}
 525   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 526   void doit();
 527 };
 528 #endif // INCLUDE_SERVICES
 529 
 530 #endif // SHARE_VM_RUNTIME_VM_OPERATIONS_HPP