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