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