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(G1CollectForAllocation)                \
  67   template(G1CollectFull)                         \
  68   template(G1Concurrent)                          \
  69   template(G1TryInitiateConcMark)                 \
  70   template(ZMarkStart)                            \
  71   template(ZMarkEnd)                              \
  72   template(ZRelocateStart)                        \
  73   template(ZVerify)                               \
  74   template(HandshakeOneThread)                    \
  75   template(HandshakeAllThreads)                   \
  76   template(HandshakeFallback)                     \
  77   template(EnableBiasedLocking)                   \
  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(JVMCIResizeCounters)                   \
 116   template(ClassLoaderStatsOperation)             \
 117   template(ClassLoaderHierarchyOperation)         \
 118   template(DumpHashtable)                         \
 119   template(DumpTouchedMethods)                    \
 120   template(MarkActiveNMethods)                    \
 121   template(PrintCompileQueue)                     \
 122   template(PrintClassHierarchy)                   \
 123   template(ThreadSuspend)                         \
 124   template(ThreadsSuspendJVMTI)                   \
 125   template(ICBufferFull)                          \
 126   template(ScavengeMonitors)                      \
 127   template(PrintMetadata)                         \
 128   template(GTestExecuteAtSafepoint)               \
 129   template(JFROldObject)                          \
 130 
 131 class VM_Operation : public StackObj {
 132  public:
 133   // An operation can either be done inside a safepoint
 134   // or concurrently with Java threads running.
 135   enum Mode {
 136     _safepoint,
 137     _no_safepoint
 138   };
 139 
 140   enum VMOp_Type {
 141     VM_OPS_DO(VM_OP_ENUM)
 142     VMOp_Terminating
 143   };
 144 
 145  private:
 146   Thread*         _calling_thread;
 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), _timestamp(0),  _next(NULL), _prev(NULL) {}
 156 
 157   // VM operation support (used by VM thread)
 158   Thread* calling_thread() const                 { return _calling_thread; }
 159   void set_calling_thread(Thread* thread);
 160 
 161   long timestamp() const              { return _timestamp; }
 162   void set_timestamp(long timestamp)  { _timestamp = timestamp; }
 163 
 164   // Called by VM thread - does in turn invoke doit(). Do not override this
 165   void evaluate();
 166 
 167   // evaluate() is called by the VMThread and in turn calls doit().
 168   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
 169   // doit_prologue() is called in that thread before transferring control to
 170   // the VMThread.
 171   // If doit_prologue() returns true the VM operation will proceed, and
 172   // doit_epilogue() will be called by the JavaThread once the VM operation
 173   // completes. If doit_prologue() returns false the VM operation is cancelled.
 174   virtual void doit()                            = 0;
 175   virtual bool doit_prologue()                   { return true; };
 176   virtual void doit_epilogue()                   {};
 177 
 178   // Linking
 179   VM_Operation *next() const                     { return _next; }
 180   VM_Operation *prev() const                     { return _prev; }
 181   void set_next(VM_Operation *next)              { _next = next; }
 182   void set_prev(VM_Operation *prev)              { _prev = prev; }
 183 
 184   // Configuration. Override these appropriately in subclasses.
 185   virtual VMOp_Type type() const = 0;
 186   virtual Mode evaluation_mode() const            { return _safepoint; }
 187   virtual bool allow_nested_vm_operations() const { return false; }
 188   virtual void oops_do(OopClosure* f)              { /* do nothing */ };
 189 
 190   bool evaluate_at_safepoint() const {
 191     return evaluation_mode() == _safepoint;
 192   }
 193 
 194   static const char* mode_to_string(Mode mode);
 195 
 196   // Debugging
 197   virtual void print_on_error(outputStream* st) const;
 198   virtual const char* name() const  { return _names[type()]; }
 199   static const char* name(int type) {
 200     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 201     return _names[type];
 202   }
 203 #ifndef PRODUCT
 204   void print_on(outputStream* st) const { print_on_error(st); }
 205 #endif
 206 };
 207 
 208 class VM_None: public VM_Operation {
 209   const char* _reason;
 210  public:
 211   VM_None(const char* reason) : _reason(reason) {}
 212   const char* name() const { return _reason; }
 213   VMOp_Type type() const { return VMOp_None; }
 214   void doit() {};
 215 };
 216 
 217 class VM_Cleanup: public VM_Operation {
 218  public:
 219   VMOp_Type type() const { return VMOp_Cleanup; }
 220   void doit() {};
 221 };
 222 
 223 class VM_ThreadStop: public VM_Operation {
 224  private:
 225   oop     _thread;        // The Thread that the Throwable is thrown against
 226   oop     _throwable;     // The Throwable thrown at the target Thread
 227  public:
 228   // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the
 229   // VM operation is executed.
 230   VM_ThreadStop(oop thread, oop throwable) {
 231     _thread    = thread;
 232     _throwable = throwable;
 233   }
 234   VMOp_Type type() const                         { return VMOp_ThreadStop; }
 235   oop target_thread() const                      { return _thread; }
 236   oop throwable() const                          { return _throwable;}
 237   void doit();
 238   // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated
 239   bool allow_nested_vm_operations() const        { return true; }
 240 
 241   // GC support
 242   void oops_do(OopClosure* f) {
 243     f->do_oop(&_thread); f->do_oop(&_throwable);
 244   }
 245 };
 246 
 247 class VM_ClearICs: public VM_Operation {
 248  private:
 249   bool _preserve_static_stubs;
 250  public:
 251   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 252   void doit();
 253   VMOp_Type type() const { return VMOp_ClearICs; }
 254 };
 255 
 256 // empty vm op, evaluated just to force a safepoint
 257 class VM_ForceSafepoint: public VM_Operation {
 258  public:
 259   void doit()         {}
 260   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 261 };
 262 
 263 // empty vm op, when forcing a safepoint to suspend a thread
 264 class VM_ThreadSuspend: public VM_ForceSafepoint {
 265  public:
 266   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 267 };
 268 
 269 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 270 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 271  public:
 272   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 273 };
 274 
 275 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 276 class VM_ICBufferFull: public VM_ForceSafepoint {
 277  public:
 278   VMOp_Type type() const { return VMOp_ICBufferFull; }
 279 };
 280 
 281 // Base class for invoking parts of a gtest in a safepoint.
 282 // Derived classes provide the doit method.
 283 // Typically also need to transition the gtest thread from native to VM.
 284 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 285  public:
 286   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 287 
 288  protected:
 289   VM_GTestExecuteAtSafepoint() {}
 290 };
 291 
 292 class VM_MarkActiveNMethods: public VM_Operation {
 293  public:
 294   VM_MarkActiveNMethods() {}
 295   VMOp_Type type() const                         { return VMOp_MarkActiveNMethods; }
 296   void doit();
 297   bool allow_nested_vm_operations() const        { return true; }
 298 };
 299 
 300 // Deopt helper that can deoptimize frames in threads other than the
 301 // current thread.  Only used through Deoptimization::deoptimize_frame.
 302 class VM_DeoptimizeFrame: public VM_Operation {
 303   friend class Deoptimization;
 304 
 305  private:
 306   JavaThread* _thread;
 307   intptr_t*   _id;
 308   int _reason;
 309   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 310 
 311  public:
 312   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 313   void doit();
 314   bool allow_nested_vm_operations() const        { return true;  }
 315 };
 316 
 317 #ifndef PRODUCT
 318 class VM_DeoptimizeAll: public VM_Operation {
 319  private:
 320   Klass* _dependee;
 321  public:
 322   VM_DeoptimizeAll() {}
 323   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 324   void doit();
 325   bool allow_nested_vm_operations() const        { return true; }
 326 };
 327 
 328 
 329 class VM_ZombieAll: public VM_Operation {
 330  public:
 331   VM_ZombieAll() {}
 332   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 333   void doit();
 334   bool allow_nested_vm_operations() const        { return true; }
 335 };
 336 #endif // PRODUCT
 337 
 338 class VM_Verify: public VM_Operation {
 339  public:
 340   VMOp_Type type() const { return VMOp_Verify; }
 341   void doit();
 342 };
 343 
 344 
 345 class VM_PrintThreads: public VM_Operation {
 346  private:
 347   outputStream* _out;
 348   bool _print_concurrent_locks;
 349   bool _print_extended_info;
 350  public:
 351   VM_PrintThreads()
 352     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
 353   {}
 354   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
 355     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
 356   {}
 357   VMOp_Type type() const {
 358     return VMOp_PrintThreads;
 359   }
 360   void doit();
 361   bool doit_prologue();
 362   void doit_epilogue();
 363 };
 364 
 365 class VM_PrintJNI: public VM_Operation {
 366  private:
 367   outputStream* _out;
 368  public:
 369   VM_PrintJNI()                         { _out = tty; }
 370   VM_PrintJNI(outputStream* out)        { _out = out; }
 371   VMOp_Type type() const                { return VMOp_PrintJNI; }
 372   void doit();
 373 };
 374 
 375 class VM_PrintMetadata : public VM_Operation {
 376  private:
 377   outputStream* const _out;
 378   const size_t        _scale;
 379   const int           _flags;
 380 
 381  public:
 382   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
 383     : _out(out), _scale(scale), _flags(flags)
 384   {};
 385 
 386   VMOp_Type type() const  { return VMOp_PrintMetadata; }
 387   void doit();
 388 };
 389 
 390 class DeadlockCycle;
 391 class VM_FindDeadlocks: public VM_Operation {
 392  private:
 393   bool              _concurrent_locks;
 394   DeadlockCycle*    _deadlocks;
 395   outputStream*     _out;
 396   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
 397                               // which protects the JavaThreads in _deadlocks.
 398 
 399  public:
 400   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _deadlocks(NULL), _out(NULL), _setter() {};
 401   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _deadlocks(NULL), _out(st) {};
 402   ~VM_FindDeadlocks();
 403 
 404   DeadlockCycle* result()      { return _deadlocks; };
 405   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 406   void doit();
 407 };
 408 
 409 class ThreadDumpResult;
 410 class ThreadSnapshot;
 411 class ThreadConcurrentLocks;
 412 
 413 class VM_ThreadDump : public VM_Operation {
 414  private:
 415   ThreadDumpResult*              _result;
 416   int                            _num_threads;
 417   GrowableArray<instanceHandle>* _threads;
 418   int                            _max_depth;
 419   bool                           _with_locked_monitors;
 420   bool                           _with_locked_synchronizers;
 421 
 422   void snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 423 
 424  public:
 425   VM_ThreadDump(ThreadDumpResult* result,
 426                 int max_depth,  // -1 indicates entire stack
 427                 bool with_locked_monitors,
 428                 bool with_locked_synchronizers);
 429 
 430   VM_ThreadDump(ThreadDumpResult* result,
 431                 GrowableArray<instanceHandle>* threads,
 432                 int num_threads, // -1 indicates entire stack
 433                 int max_depth,
 434                 bool with_locked_monitors,
 435                 bool with_locked_synchronizers);
 436 
 437   VMOp_Type type() const { return VMOp_ThreadDump; }
 438   void doit();
 439   bool doit_prologue();
 440   void doit_epilogue();
 441 };
 442 
 443 
 444 class VM_Exit: public VM_Operation {
 445  private:
 446   int  _exit_code;
 447   static volatile bool _vm_exited;
 448   static Thread * volatile _shutdown_thread;
 449   static void wait_if_vm_exited();
 450  public:
 451   VM_Exit(int exit_code) {
 452     _exit_code = exit_code;
 453   }
 454   static int wait_for_threads_in_native_to_block();
 455   static int set_vm_exited();
 456   static bool vm_exited()                      { return _vm_exited; }
 457   static Thread * shutdown_thread()            { return _shutdown_thread; }
 458   static void block_if_vm_exited() {
 459     if (_vm_exited) {
 460       wait_if_vm_exited();
 461     }
 462   }
 463   VMOp_Type type() const { return VMOp_Exit; }
 464   void doit();
 465 };
 466 
 467 class VM_PrintCompileQueue: public VM_Operation {
 468  private:
 469   outputStream* _out;
 470 
 471  public:
 472   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 473   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 474   Mode evaluation_mode() const { return _safepoint; }
 475   void doit();
 476 };
 477 
 478 #if INCLUDE_SERVICES
 479 class VM_PrintClassHierarchy: public VM_Operation {
 480  private:
 481   outputStream* _out;
 482   bool _print_interfaces;
 483   bool _print_subclasses;
 484   char* _classname;
 485 
 486  public:
 487   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 488     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 489     _classname(classname) {}
 490   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 491   void doit();
 492 };
 493 #endif // INCLUDE_SERVICES
 494 
 495 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP