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