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