1 /*
   2  * Copyright (c) 1997, 2020, 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(ThreadDump)                            \
  46   template(PrintThreads)                          \
  47   template(FindDeadlocks)                         \
  48   template(ClearICs)                              \
  49   template(ForceSafepoint)                        \
  50   template(ForceAsyncSafepoint)                   \
  51   template(DeoptimizeFrame)                       \
  52   template(DeoptimizeAll)                         \
  53   template(ZombieAll)                             \
  54   template(Verify)                                \
  55   template(PrintJNI)                              \
  56   template(HeapDumper)                            \
  57   template(DeoptimizeTheWorld)                    \
  58   template(CollectForMetadataAllocation)          \
  59   template(GC_HeapInspection)                     \
  60   template(GenCollectFull)                        \
  61   template(GenCollectFullConcurrent)              \
  62   template(GenCollectForAllocation)               \
  63   template(ParallelGCFailedAllocation)            \
  64   template(ParallelGCSystemGC)                    \
  65   template(G1CollectForAllocation)                \
  66   template(G1CollectFull)                         \
  67   template(G1Concurrent)                          \
  68   template(G1TryInitiateConcMark)                 \
  69   template(ZMarkStart)                            \
  70   template(ZMarkEnd)                              \
  71   template(ZRelocateStart)                        \
  72   template(ZVerify)                               \
  73   template(HandshakeOneThread)                    \
  74   template(HandshakeAllThreads)                   \
  75   template(HandshakeFallback)                     \
  76   template(EnableBiasedLocking)                   \
  77   template(BulkRevokeBias)                        \
  78   template(PopulateDumpSharedSpace)               \
  79   template(JNIFunctionTableCopier)                \
  80   template(RedefineClasses)                       \
  81   template(UpdateForPopTopFrame)                  \
  82   template(SetFramePop)                           \
  83   template(GetObjectMonitorUsage)                 \
  84   template(GetMultipleStackTraces)                \
  85   template(GetAllStackTraces)                     \
  86   template(GetThreadListStackTraces)              \
  87   template(ChangeBreakpoints)                     \
  88   template(GetOrSetLocal)                         \
  89   template(ChangeSingleStep)                      \
  90   template(HeapWalkOperation)                     \
  91   template(HeapIterateOperation)                  \
  92   template(ReportJavaOutOfMemory)                 \
  93   template(JFRCheckpoint)                         \
  94   template(ShenandoahFullGC)                      \
  95   template(ShenandoahInitMark)                    \
  96   template(ShenandoahFinalMarkStartEvac)          \
  97   template(ShenandoahInitUpdateRefs)              \
  98   template(ShenandoahFinalUpdateRefs)             \
  99   template(ShenandoahDegeneratedGC)               \
 100   template(Exit)                                  \
 101   template(LinuxDllLoad)                          \
 102   template(RotateGCLog)                           \
 103   template(WhiteBoxOperation)                     \
 104   template(JVMCIResizeCounters)                   \
 105   template(ClassLoaderStatsOperation)             \
 106   template(ClassLoaderHierarchyOperation)         \
 107   template(DumpHashtable)                         \
 108   template(DumpTouchedMethods)                    \
 109   template(MarkActiveNMethods)                    \
 110   template(PrintCompileQueue)                     \
 111   template(PrintClassHierarchy)                   \
 112   template(ThreadSuspend)                         \
 113   template(ThreadsSuspendJVMTI)                   \
 114   template(ICBufferFull)                          \
 115   template(ScavengeMonitors)                      \
 116   template(PrintMetadata)                         \
 117   template(GTestExecuteAtSafepoint)               \
 118   template(JFROldObject)                          \
 119 
 120 class VM_Operation : public StackObj {
 121  public:
 122   enum VMOp_Type {
 123     VM_OPS_DO(VM_OP_ENUM)
 124     VMOp_Terminating
 125   };
 126 
 127  private:
 128   Thread*         _calling_thread;
 129   VM_Operation*   _next;
 130   VM_Operation*   _prev;
 131 
 132   // The VM operation name array
 133   static const char* _names[];
 134 
 135  public:
 136   VM_Operation() : _calling_thread(NULL), _next(NULL), _prev(NULL) {}
 137 
 138   // VM operation support (used by VM thread)
 139   Thread* calling_thread() const                 { return _calling_thread; }
 140   void set_calling_thread(Thread* thread);
 141 
 142   // Called by VM thread - does in turn invoke doit(). Do not override this
 143   void evaluate();
 144 
 145   // evaluate() is called by the VMThread and in turn calls doit().
 146   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
 147   // doit_prologue() is called in that thread before transferring control to
 148   // the VMThread.
 149   // If doit_prologue() returns true the VM operation will proceed, and
 150   // doit_epilogue() will be called by the JavaThread once the VM operation
 151   // completes. If doit_prologue() returns false the VM operation is cancelled.
 152   virtual void doit()                            = 0;
 153   virtual bool doit_prologue()                   { return true; };
 154   virtual void doit_epilogue()                   {};
 155 
 156   // Linking
 157   VM_Operation *next() const                     { return _next; }
 158   VM_Operation *prev() const                     { return _prev; }
 159   void set_next(VM_Operation *next)              { _next = next; }
 160   void set_prev(VM_Operation *prev)              { _prev = prev; }
 161 
 162   // Configuration. Override these appropriately in subclasses.
 163   virtual VMOp_Type type() const = 0;
 164   virtual bool allow_nested_vm_operations() const { return false; }
 165 
 166   // An operation can either be done inside a safepoint
 167   // or concurrently with Java threads running.
 168   virtual bool evaluate_at_safepoint() const { return true; }
 169 
 170   // Debugging
 171   virtual void print_on_error(outputStream* st) const;
 172   virtual const char* name() const  { return _names[type()]; }
 173   static const char* name(int type) {
 174     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 175     return _names[type];
 176   }
 177 #ifndef PRODUCT
 178   void print_on(outputStream* st) const { print_on_error(st); }
 179 #endif
 180 };
 181 
 182 class VM_None: public VM_Operation {
 183   const char* _reason;
 184  public:
 185   VM_None(const char* reason) : _reason(reason) {}
 186   const char* name() const { return _reason; }
 187   VMOp_Type type() const { return VMOp_None; }
 188   void doit() {};
 189 };
 190 
 191 class VM_Cleanup: public VM_Operation {
 192  public:
 193   VMOp_Type type() const { return VMOp_Cleanup; }
 194   void doit() {};
 195 };
 196 
 197 class VM_ClearICs: public VM_Operation {
 198  private:
 199   bool _preserve_static_stubs;
 200  public:
 201   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 202   void doit();
 203   VMOp_Type type() const { return VMOp_ClearICs; }
 204 };
 205 
 206 // empty vm op, evaluated just to force a safepoint
 207 class VM_ForceSafepoint: public VM_Operation {
 208  public:
 209   void doit()         {}
 210   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 211 };
 212 
 213 // empty vm op, when forcing a safepoint to suspend a thread
 214 class VM_ThreadSuspend: public VM_ForceSafepoint {
 215  public:
 216   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 217 };
 218 
 219 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 220 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 221  public:
 222   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 223 };
 224 
 225 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 226 class VM_ICBufferFull: public VM_ForceSafepoint {
 227  public:
 228   VMOp_Type type() const { return VMOp_ICBufferFull; }
 229 };
 230 
 231 // Base class for invoking parts of a gtest in a safepoint.
 232 // Derived classes provide the doit method.
 233 // Typically also need to transition the gtest thread from native to VM.
 234 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 235  public:
 236   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 237 
 238  protected:
 239   VM_GTestExecuteAtSafepoint() {}
 240 };
 241 
 242 // Deopt helper that can deoptimize frames in threads other than the
 243 // current thread.  Only used through Deoptimization::deoptimize_frame.
 244 class VM_DeoptimizeFrame: public VM_Operation {
 245   friend class Deoptimization;
 246 
 247  private:
 248   JavaThread* _thread;
 249   intptr_t*   _id;
 250   int _reason;
 251   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 252 
 253  public:
 254   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 255   void doit();
 256   bool allow_nested_vm_operations() const        { return true;  }
 257 };
 258 
 259 #ifndef PRODUCT
 260 class VM_DeoptimizeAll: public VM_Operation {
 261  private:
 262   Klass* _dependee;
 263  public:
 264   VM_DeoptimizeAll() {}
 265   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 266   void doit();
 267   bool allow_nested_vm_operations() const        { return true; }
 268 };
 269 
 270 
 271 class VM_ZombieAll: public VM_Operation {
 272  public:
 273   VM_ZombieAll() {}
 274   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 275   void doit();
 276   bool allow_nested_vm_operations() const        { return true; }
 277 };
 278 #endif // PRODUCT
 279 
 280 class VM_Verify: public VM_Operation {
 281  public:
 282   VMOp_Type type() const { return VMOp_Verify; }
 283   void doit();
 284 };
 285 
 286 
 287 class VM_PrintThreads: public VM_Operation {
 288  private:
 289   outputStream* _out;
 290   bool _print_concurrent_locks;
 291   bool _print_extended_info;
 292  public:
 293   VM_PrintThreads()
 294     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
 295   {}
 296   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
 297     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
 298   {}
 299   VMOp_Type type() const {
 300     return VMOp_PrintThreads;
 301   }
 302   void doit();
 303   bool doit_prologue();
 304   void doit_epilogue();
 305 };
 306 
 307 class VM_PrintJNI: public VM_Operation {
 308  private:
 309   outputStream* _out;
 310  public:
 311   VM_PrintJNI()                         { _out = tty; }
 312   VM_PrintJNI(outputStream* out)        { _out = out; }
 313   VMOp_Type type() const                { return VMOp_PrintJNI; }
 314   void doit();
 315 };
 316 
 317 class VM_PrintMetadata : public VM_Operation {
 318  private:
 319   outputStream* const _out;
 320   const size_t        _scale;
 321   const int           _flags;
 322 
 323  public:
 324   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
 325     : _out(out), _scale(scale), _flags(flags)
 326   {};
 327 
 328   VMOp_Type type() const  { return VMOp_PrintMetadata; }
 329   void doit();
 330 };
 331 
 332 class DeadlockCycle;
 333 class VM_FindDeadlocks: public VM_Operation {
 334  private:
 335   bool              _concurrent_locks;
 336   DeadlockCycle*    _deadlocks;
 337   outputStream*     _out;
 338   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
 339                               // which protects the JavaThreads in _deadlocks.
 340 
 341  public:
 342   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _deadlocks(NULL), _out(NULL), _setter() {};
 343   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _deadlocks(NULL), _out(st) {};
 344   ~VM_FindDeadlocks();
 345 
 346   DeadlockCycle* result()      { return _deadlocks; };
 347   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 348   void doit();
 349 };
 350 
 351 class ThreadDumpResult;
 352 class ThreadSnapshot;
 353 class ThreadConcurrentLocks;
 354 
 355 class VM_ThreadDump : public VM_Operation {
 356  private:
 357   ThreadDumpResult*              _result;
 358   int                            _num_threads;
 359   GrowableArray<instanceHandle>* _threads;
 360   int                            _max_depth;
 361   bool                           _with_locked_monitors;
 362   bool                           _with_locked_synchronizers;
 363 
 364   void snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 365 
 366  public:
 367   VM_ThreadDump(ThreadDumpResult* result,
 368                 int max_depth,  // -1 indicates entire stack
 369                 bool with_locked_monitors,
 370                 bool with_locked_synchronizers);
 371 
 372   VM_ThreadDump(ThreadDumpResult* result,
 373                 GrowableArray<instanceHandle>* threads,
 374                 int num_threads, // -1 indicates entire stack
 375                 int max_depth,
 376                 bool with_locked_monitors,
 377                 bool with_locked_synchronizers);
 378 
 379   VMOp_Type type() const { return VMOp_ThreadDump; }
 380   void doit();
 381   bool doit_prologue();
 382   void doit_epilogue();
 383 };
 384 
 385 
 386 class VM_Exit: public VM_Operation {
 387  private:
 388   int  _exit_code;
 389   static volatile bool _vm_exited;
 390   static Thread * volatile _shutdown_thread;
 391   static void wait_if_vm_exited();
 392  public:
 393   VM_Exit(int exit_code) {
 394     _exit_code = exit_code;
 395   }
 396   static int wait_for_threads_in_native_to_block();
 397   static int set_vm_exited();
 398   static bool vm_exited()                      { return _vm_exited; }
 399   static Thread * shutdown_thread()            { return _shutdown_thread; }
 400   static void block_if_vm_exited() {
 401     if (_vm_exited) {
 402       wait_if_vm_exited();
 403     }
 404   }
 405   VMOp_Type type() const { return VMOp_Exit; }
 406   bool doit_prologue();
 407   void doit();
 408 };
 409 
 410 class VM_PrintCompileQueue: public VM_Operation {
 411  private:
 412   outputStream* _out;
 413 
 414  public:
 415   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 416   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 417   void doit();
 418 };
 419 
 420 #if INCLUDE_SERVICES
 421 class VM_PrintClassHierarchy: public VM_Operation {
 422  private:
 423   outputStream* _out;
 424   bool _print_interfaces;
 425   bool _print_subclasses;
 426   char* _classname;
 427 
 428  public:
 429   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 430     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 431     _classname(classname) {}
 432   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 433   void doit();
 434 };
 435 #endif // INCLUDE_SERVICES
 436 
 437 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP