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_ClearICs: public VM_Operation {
 214  private:
 215   bool _preserve_static_stubs;
 216  public:
 217   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 218   void doit();
 219   VMOp_Type type() const { return VMOp_ClearICs; }
 220 };
 221 
 222 // empty vm op, evaluated just to force a safepoint
 223 class VM_ForceSafepoint: public VM_Operation {
 224  public:
 225   void doit()         {}
 226   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 227 };
 228 
 229 // empty vm op, when forcing a safepoint to suspend a thread
 230 class VM_ThreadSuspend: public VM_ForceSafepoint {
 231  public:
 232   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 233 };
 234 
 235 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 236 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 237  public:
 238   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 239 };
 240 
 241 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 242 class VM_ICBufferFull: public VM_ForceSafepoint {
 243  public:
 244   VMOp_Type type() const { return VMOp_ICBufferFull; }
 245 };
 246 
 247 // Base class for invoking parts of a gtest in a safepoint.
 248 // Derived classes provide the doit method.
 249 // Typically also need to transition the gtest thread from native to VM.
 250 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 251  public:
 252   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 253 
 254  protected:
 255   VM_GTestExecuteAtSafepoint() {}
 256 };
 257 
 258 class VM_MarkActiveNMethods: public VM_Operation {
 259  public:
 260   VM_MarkActiveNMethods() {}
 261   VMOp_Type type() const                         { return VMOp_MarkActiveNMethods; }
 262   void doit();
 263   bool allow_nested_vm_operations() const        { return true; }
 264 };
 265 
 266 // Deopt helper that can deoptimize frames in threads other than the
 267 // current thread.  Only used through Deoptimization::deoptimize_frame.
 268 class VM_DeoptimizeFrame: public VM_Operation {
 269   friend class Deoptimization;
 270 
 271  private:
 272   JavaThread* _thread;
 273   intptr_t*   _id;
 274   int _reason;
 275   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 276 
 277  public:
 278   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 279   void doit();
 280   bool allow_nested_vm_operations() const        { return true;  }
 281 };
 282 
 283 #ifndef PRODUCT
 284 class VM_DeoptimizeAll: public VM_Operation {
 285  private:
 286   Klass* _dependee;
 287  public:
 288   VM_DeoptimizeAll() {}
 289   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 290   void doit();
 291   bool allow_nested_vm_operations() const        { return true; }
 292 };
 293 
 294 
 295 class VM_ZombieAll: public VM_Operation {
 296  public:
 297   VM_ZombieAll() {}
 298   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 299   void doit();
 300   bool allow_nested_vm_operations() const        { return true; }
 301 };
 302 #endif // PRODUCT
 303 
 304 class VM_Verify: public VM_Operation {
 305  public:
 306   VMOp_Type type() const { return VMOp_Verify; }
 307   void doit();
 308 };
 309 
 310 
 311 class VM_PrintThreads: public VM_Operation {
 312  private:
 313   outputStream* _out;
 314   bool _print_concurrent_locks;
 315   bool _print_extended_info;
 316  public:
 317   VM_PrintThreads()
 318     : _out(tty), _print_concurrent_locks(PrintConcurrentLocks), _print_extended_info(false)
 319   {}
 320   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool print_extended_info)
 321     : _out(out), _print_concurrent_locks(print_concurrent_locks), _print_extended_info(print_extended_info)
 322   {}
 323   VMOp_Type type() const {
 324     return VMOp_PrintThreads;
 325   }
 326   void doit();
 327   bool doit_prologue();
 328   void doit_epilogue();
 329 };
 330 
 331 class VM_PrintJNI: public VM_Operation {
 332  private:
 333   outputStream* _out;
 334  public:
 335   VM_PrintJNI()                         { _out = tty; }
 336   VM_PrintJNI(outputStream* out)        { _out = out; }
 337   VMOp_Type type() const                { return VMOp_PrintJNI; }
 338   void doit();
 339 };
 340 
 341 class VM_PrintMetadata : public VM_Operation {
 342  private:
 343   outputStream* const _out;
 344   const size_t        _scale;
 345   const int           _flags;
 346 
 347  public:
 348   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
 349     : _out(out), _scale(scale), _flags(flags)
 350   {};
 351 
 352   VMOp_Type type() const  { return VMOp_PrintMetadata; }
 353   void doit();
 354 };
 355 
 356 class DeadlockCycle;
 357 class VM_FindDeadlocks: public VM_Operation {
 358  private:
 359   bool              _concurrent_locks;
 360   DeadlockCycle*    _deadlocks;
 361   outputStream*     _out;
 362   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
 363                               // which protects the JavaThreads in _deadlocks.
 364 
 365  public:
 366   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _deadlocks(NULL), _out(NULL), _setter() {};
 367   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _deadlocks(NULL), _out(st) {};
 368   ~VM_FindDeadlocks();
 369 
 370   DeadlockCycle* result()      { return _deadlocks; };
 371   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 372   void doit();
 373 };
 374 
 375 class ThreadDumpResult;
 376 class ThreadSnapshot;
 377 class ThreadConcurrentLocks;
 378 
 379 class VM_ThreadDump : public VM_Operation {
 380  private:
 381   ThreadDumpResult*              _result;
 382   int                            _num_threads;
 383   GrowableArray<instanceHandle>* _threads;
 384   int                            _max_depth;
 385   bool                           _with_locked_monitors;
 386   bool                           _with_locked_synchronizers;
 387 
 388   void snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 389 
 390  public:
 391   VM_ThreadDump(ThreadDumpResult* result,
 392                 int max_depth,  // -1 indicates entire stack
 393                 bool with_locked_monitors,
 394                 bool with_locked_synchronizers);
 395 
 396   VM_ThreadDump(ThreadDumpResult* result,
 397                 GrowableArray<instanceHandle>* threads,
 398                 int num_threads, // -1 indicates entire stack
 399                 int max_depth,
 400                 bool with_locked_monitors,
 401                 bool with_locked_synchronizers);
 402 
 403   VMOp_Type type() const { return VMOp_ThreadDump; }
 404   void doit();
 405   bool doit_prologue();
 406   void doit_epilogue();
 407 };
 408 
 409 
 410 class VM_Exit: public VM_Operation {
 411  private:
 412   int  _exit_code;
 413   static volatile bool _vm_exited;
 414   static Thread * volatile _shutdown_thread;
 415   static void wait_if_vm_exited();
 416  public:
 417   VM_Exit(int exit_code) {
 418     _exit_code = exit_code;
 419   }
 420   static int wait_for_threads_in_native_to_block();
 421   static int set_vm_exited();
 422   static bool vm_exited()                      { return _vm_exited; }
 423   static Thread * shutdown_thread()            { return _shutdown_thread; }
 424   static void block_if_vm_exited() {
 425     if (_vm_exited) {
 426       wait_if_vm_exited();
 427     }
 428   }
 429   VMOp_Type type() const { return VMOp_Exit; }
 430   void doit();
 431 };
 432 
 433 class VM_PrintCompileQueue: public VM_Operation {
 434  private:
 435   outputStream* _out;
 436 
 437  public:
 438   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 439   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 440   void doit();
 441 };
 442 
 443 #if INCLUDE_SERVICES
 444 class VM_PrintClassHierarchy: public VM_Operation {
 445  private:
 446   outputStream* _out;
 447   bool _print_interfaces;
 448   bool _print_subclasses;
 449   char* _classname;
 450 
 451  public:
 452   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 453     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 454     _classname(classname) {}
 455   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 456   void doit();
 457 };
 458 #endif // INCLUDE_SERVICES
 459 
 460 #endif // SHARE_RUNTIME_VMOPERATIONS_HPP