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