1 /*
   2  * Copyright (c) 1997, 2018, 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_VM_RUNTIME_VM_OPERATIONS_HPP
  26 #define SHARE_VM_RUNTIME_VM_OPERATIONS_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(Dummy)                                 \
  44   template(ThreadStop)                            \
  45   template(ThreadDump)                            \
  46   template(PrintThreads)                          \
  47   template(FindDeadlocks)                         \
  48   template(ClearICs)                              \
  49   template(ForceSafepoint)                        \
  50   template(ForceAsyncSafepoint)                   \
  51   template(Deoptimize)                            \
  52   template(DeoptimizeFrame)                       \
  53   template(DeoptimizeAll)                         \
  54   template(ZombieAll)                             \
  55   template(UnlinkSymbols)                         \
  56   template(Verify)                                \
  57   template(PrintJNI)                              \
  58   template(HeapDumper)                            \
  59   template(DeoptimizeTheWorld)                    \
  60   template(CollectForMetadataAllocation)          \
  61   template(GC_HeapInspection)                     \
  62   template(GenCollectFull)                        \
  63   template(GenCollectFullConcurrent)              \
  64   template(GenCollectForAllocation)               \
  65   template(ParallelGCFailedAllocation)            \
  66   template(ParallelGCSystemGC)                    \
  67   template(CGC_Operation)                         \
  68   template(CMS_Initial_Mark)                      \
  69   template(CMS_Final_Remark)                      \
  70   template(G1CollectForAllocation)                \
  71   template(G1CollectFull)                         \
  72   template(HandshakeOneThread)                    \
  73   template(HandshakeAllThreads)                   \
  74   template(HandshakeFallback)                     \
  75   template(EnableBiasedLocking)                   \
  76   template(RevokeBias)                            \
  77   template(BulkRevokeBias)                        \
  78   template(PopulateDumpSharedSpace)               \
  79   template(JNIFunctionTableCopier)                \
  80   template(RedefineClasses)                       \
  81   template(UpdateForPopTopFrame)                  \
  82   template(SetFramePop)                           \
  83   template(GetOwnedMonitorInfo)                   \
  84   template(GetObjectMonitorUsage)                 \
  85   template(GetCurrentContendedMonitor)            \
  86   template(GetStackTrace)                         \
  87   template(GetMultipleStackTraces)                \
  88   template(GetAllStackTraces)                     \
  89   template(GetThreadListStackTraces)              \
  90   template(GetFrameCount)                         \
  91   template(GetFrameLocation)                      \
  92   template(ChangeBreakpoints)                     \
  93   template(GetOrSetLocal)                         \
  94   template(GetCurrentLocation)                    \
  95   template(EnterInterpOnlyMode)                   \
  96   template(ChangeSingleStep)                      \
  97   template(HeapWalkOperation)                     \
  98   template(HeapIterateOperation)                  \
  99   template(ReportJavaOutOfMemory)                 \
 100   template(JFRCheckpoint)                         \
 101   template(Exit)                                  \
 102   template(LinuxDllLoad)                          \
 103   template(RotateGCLog)                           \
 104   template(WhiteBoxOperation)                     \
 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(CTWThreshold)                          \
 114   template(ThreadsSuspendJVMTI)                   \
 115   template(ICBufferFull)                          \
 116   template(ScavengeMonitors)                      \
 117   template(PrintMetadata)                         \
 118   template(GTestExecuteAtSafepoint)               \
 119 
 120 class VM_Operation: public CHeapObj<mtInternal> {
 121  public:
 122   enum Mode {
 123     _safepoint,       // blocking,        safepoint, vm_op C-heap allocated
 124     _no_safepoint,    // blocking,     no safepoint, vm_op C-Heap allocated
 125     _concurrent,      // non-blocking, no safepoint, vm_op C-Heap allocated
 126     _async_safepoint  // non-blocking,    safepoint, vm_op C-Heap allocated
 127   };
 128 
 129   enum VMOp_Type {
 130     VM_OPS_DO(VM_OP_ENUM)
 131     VMOp_Terminating
 132   };
 133 
 134  private:
 135   Thread*         _calling_thread;
 136   ThreadPriority  _priority;
 137   long            _timestamp;
 138   VM_Operation*   _next;
 139   VM_Operation*   _prev;
 140 
 141   // The VM operation name array
 142   static const char* _names[];
 143 
 144  public:
 145   VM_Operation()  { _calling_thread = NULL; _next = NULL; _prev = NULL; }
 146   virtual ~VM_Operation() {}
 147 
 148   // VM operation support (used by VM thread)
 149   Thread* calling_thread() const                 { return _calling_thread; }
 150   ThreadPriority priority()                      { return _priority; }
 151   void set_calling_thread(Thread* thread, ThreadPriority priority);
 152 
 153   long timestamp() const              { return _timestamp; }
 154   void set_timestamp(long timestamp)  { _timestamp = timestamp; }
 155 
 156   // Called by VM thread - does in turn invoke doit(). Do not override this
 157   void evaluate();
 158 
 159   // evaluate() is called by the VMThread and in turn calls doit().
 160   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
 161   // doit_prologue() is called in that thread before transferring control to
 162   // the VMThread.
 163   // If doit_prologue() returns true the VM operation will proceed, and
 164   // doit_epilogue() will be called by the JavaThread once the VM operation
 165   // completes. If doit_prologue() returns false the VM operation is cancelled.
 166   virtual void doit()                            = 0;
 167   virtual bool doit_prologue()                   { return true; };
 168   virtual void doit_epilogue()                   {}; // Note: Not called if mode is: _concurrent
 169 
 170   // Type test
 171   virtual bool is_methodCompiler() const         { return false; }
 172 
 173   // Linking
 174   VM_Operation *next() const                     { return _next; }
 175   VM_Operation *prev() const                     { return _prev; }
 176   void set_next(VM_Operation *next)              { _next = next; }
 177   void set_prev(VM_Operation *prev)              { _prev = prev; }
 178 
 179   // Configuration. Override these appropriately in subclasses.
 180   virtual VMOp_Type type() const = 0;
 181   virtual Mode evaluation_mode() const            { return _safepoint; }
 182   virtual bool allow_nested_vm_operations() const { return false; }
 183   virtual bool is_cheap_allocated() const         { return false; }
 184   virtual void oops_do(OopClosure* f)              { /* do nothing */ };
 185 
 186   // CAUTION: <don't hang yourself with following rope>
 187   // If you override these methods, make sure that the evaluation
 188   // of these methods is race-free and non-blocking, since these
 189   // methods may be evaluated either by the mutators or by the
 190   // vm thread, either concurrently with mutators or with the mutators
 191   // stopped. In other words, taking locks is verboten, and if there
 192   // are any races in evaluating the conditions, they'd better be benign.
 193   virtual bool evaluate_at_safepoint() const {
 194     return evaluation_mode() == _safepoint  ||
 195            evaluation_mode() == _async_safepoint;
 196   }
 197   virtual bool evaluate_concurrently() const {
 198     return evaluation_mode() == _concurrent ||
 199            evaluation_mode() == _async_safepoint;
 200   }
 201 
 202   static const char* mode_to_string(Mode mode);
 203 
 204   // Debugging
 205   virtual void print_on_error(outputStream* st) const;
 206   const char* name() const { return _names[type()]; }
 207   static const char* name(int type) {
 208     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 209     return _names[type];
 210   }
 211 #ifndef PRODUCT
 212   void print_on(outputStream* st) const { print_on_error(st); }
 213 #endif
 214 };
 215 
 216 class VM_ThreadStop: public VM_Operation {
 217  private:
 218   oop     _thread;        // The Thread that the Throwable is thrown against
 219   oop     _throwable;     // The Throwable thrown at the target Thread
 220  public:
 221   // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the
 222   // VM operation is executed.
 223   VM_ThreadStop(oop thread, oop throwable) {
 224     _thread    = thread;
 225     _throwable = throwable;
 226   }
 227   VMOp_Type type() const                         { return VMOp_ThreadStop; }
 228   oop target_thread() const                      { return _thread; }
 229   oop throwable() const                          { return _throwable;}
 230   void doit();
 231   // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated
 232   bool allow_nested_vm_operations() const        { return true; }
 233   Mode evaluation_mode() const                   { return _async_safepoint; }
 234   bool is_cheap_allocated() const                { return true; }
 235 
 236   // GC support
 237   void oops_do(OopClosure* f) {
 238     f->do_oop(&_thread); f->do_oop(&_throwable);
 239   }
 240 };
 241 
 242 class VM_ClearICs: public VM_Operation {
 243  private:
 244   bool _preserve_static_stubs;
 245  public:
 246   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 247   void doit();
 248   VMOp_Type type() const { return VMOp_ClearICs; }
 249 };
 250 
 251 // empty vm op, evaluated just to force a safepoint
 252 class VM_ForceSafepoint: public VM_Operation {
 253  public:
 254   void doit()         {}
 255   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 256 };
 257 
 258 // empty vm op, when forcing a safepoint to suspend a thread
 259 class VM_ThreadSuspend: public VM_ForceSafepoint {
 260  public:
 261   VMOp_Type type() const { return VMOp_ThreadSuspend; }
 262 };
 263 
 264 // empty vm op, when forcing a safepoint due to ctw threshold is reached for the sweeper
 265 class VM_CTWThreshold: public VM_ForceSafepoint {
 266  public:
 267   VMOp_Type type() const { return VMOp_CTWThreshold; }
 268 };
 269 
 270 // empty vm op, when forcing a safepoint to suspend threads from jvmti
 271 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint {
 272  public:
 273   VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; }
 274 };
 275 
 276 // empty vm op, when forcing a safepoint due to inline cache buffers being full
 277 class VM_ICBufferFull: public VM_ForceSafepoint {
 278  public:
 279   VMOp_Type type() const { return VMOp_ICBufferFull; }
 280 };
 281 
 282 // empty asynchronous vm op, when forcing a safepoint to scavenge monitors
 283 class VM_ScavengeMonitors: public VM_ForceSafepoint {
 284  public:
 285   VMOp_Type type() const                         { return VMOp_ScavengeMonitors; }
 286   Mode evaluation_mode() const                   { return _async_safepoint; }
 287   bool is_cheap_allocated() const                { return true; }
 288 };
 289 
 290 // Base class for invoking parts of a gtest in a safepoint.
 291 // Derived classes provide the doit method.
 292 // Typically also need to transition the gtest thread from native to VM.
 293 class VM_GTestExecuteAtSafepoint: public VM_Operation {
 294  public:
 295   VMOp_Type type() const                         { return VMOp_GTestExecuteAtSafepoint; }
 296 
 297  protected:
 298   VM_GTestExecuteAtSafepoint() {}
 299 };
 300 
 301 class VM_Deoptimize: public VM_Operation {
 302  public:
 303   VM_Deoptimize() {}
 304   VMOp_Type type() const                        { return VMOp_Deoptimize; }
 305   void doit();
 306   bool allow_nested_vm_operations() const        { return true; }
 307 };
 308 
 309 class VM_MarkActiveNMethods: public VM_Operation {
 310  public:
 311   VM_MarkActiveNMethods() {}
 312   VMOp_Type type() const                         { return VMOp_MarkActiveNMethods; }
 313   void doit();
 314   bool allow_nested_vm_operations() const        { return true; }
 315 };
 316 
 317 // Deopt helper that can deoptimize frames in threads other than the
 318 // current thread.  Only used through Deoptimization::deoptimize_frame.
 319 class VM_DeoptimizeFrame: public VM_Operation {
 320   friend class Deoptimization;
 321 
 322  private:
 323   JavaThread* _thread;
 324   intptr_t*   _id;
 325   int _reason;
 326   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 327 
 328  public:
 329   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 330   void doit();
 331   bool allow_nested_vm_operations() const        { return true;  }
 332 };
 333 
 334 #ifndef PRODUCT
 335 class VM_DeoptimizeAll: public VM_Operation {
 336  private:
 337   Klass* _dependee;
 338  public:
 339   VM_DeoptimizeAll() {}
 340   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 341   void doit();
 342   bool allow_nested_vm_operations() const        { return true; }
 343 };
 344 
 345 
 346 class VM_ZombieAll: public VM_Operation {
 347  public:
 348   VM_ZombieAll() {}
 349   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 350   void doit();
 351   bool allow_nested_vm_operations() const        { return true; }
 352 };
 353 #endif // PRODUCT
 354 
 355 class VM_UnlinkSymbols: public VM_Operation {
 356  public:
 357   VM_UnlinkSymbols() {}
 358   VMOp_Type type() const                         { return VMOp_UnlinkSymbols; }
 359   void doit();
 360   bool allow_nested_vm_operations() const        { return true; }
 361 };
 362 
 363 class VM_Verify: public VM_Operation {
 364  public:
 365   VMOp_Type type() const { return VMOp_Verify; }
 366   void doit();
 367 };
 368 
 369 
 370 class VM_PrintThreads: public VM_Operation {
 371  private:
 372   outputStream* _out;
 373   bool _print_concurrent_locks;
 374   bool _extended_thread_info;
 375  public:
 376   VM_PrintThreads()
 377   { _out = tty; _print_concurrent_locks = PrintConcurrentLocks; ; _extended_thread_info = false; }
 378   VM_PrintThreads(outputStream* out, bool print_concurrent_locks, bool extended_thread_info)
 379   { _out = out; _print_concurrent_locks = print_concurrent_locks; _extended_thread_info = extended_thread_info; }
 380   VMOp_Type type() const
 381   { return VMOp_PrintThreads; }
 382   void doit();
 383   bool doit_prologue();
 384   void doit_epilogue();
 385 };
 386 
 387 class VM_PrintJNI: public VM_Operation {
 388  private:
 389   outputStream* _out;
 390  public:
 391   VM_PrintJNI()                         { _out = tty; }
 392   VM_PrintJNI(outputStream* out)        { _out = out; }
 393   VMOp_Type type() const                { return VMOp_PrintJNI; }
 394   void doit();
 395 };
 396 
 397 class VM_PrintMetadata : public VM_Operation {
 398  private:
 399   outputStream* const _out;
 400   const size_t        _scale;
 401   const int           _flags;
 402 
 403  public:
 404   VM_PrintMetadata(outputStream* out, size_t scale, int flags)
 405     : _out(out), _scale(scale), _flags(flags)
 406   {};
 407 
 408   VMOp_Type type() const  { return VMOp_PrintMetadata; }
 409   void doit();
 410 };
 411 
 412 class DeadlockCycle;
 413 class VM_FindDeadlocks: public VM_Operation {
 414  private:
 415   bool              _concurrent_locks;
 416   DeadlockCycle*    _deadlocks;
 417   outputStream*     _out;
 418   ThreadsListSetter _setter;  // Helper to set hazard ptr in the originating thread
 419                               // which protects the JavaThreads in _deadlocks.
 420 
 421  public:
 422   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _out(NULL), _deadlocks(NULL), _setter() {};
 423   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _out(st), _deadlocks(NULL) {};
 424   ~VM_FindDeadlocks();
 425 
 426   DeadlockCycle* result()      { return _deadlocks; };
 427   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 428   void doit();
 429 };
 430 
 431 class ThreadDumpResult;
 432 class ThreadSnapshot;
 433 class ThreadConcurrentLocks;
 434 
 435 class VM_ThreadDump : public VM_Operation {
 436  private:
 437   ThreadDumpResult*              _result;
 438   int                            _num_threads;
 439   GrowableArray<instanceHandle>* _threads;
 440   int                            _max_depth;
 441   bool                           _with_locked_monitors;
 442   bool                           _with_locked_synchronizers;
 443 
 444   ThreadSnapshot* snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 445 
 446  public:
 447   VM_ThreadDump(ThreadDumpResult* result,
 448                 int max_depth,  // -1 indicates entire stack
 449                 bool with_locked_monitors,
 450                 bool with_locked_synchronizers);
 451 
 452   VM_ThreadDump(ThreadDumpResult* result,
 453                 GrowableArray<instanceHandle>* threads,
 454                 int num_threads, // -1 indicates entire stack
 455                 int max_depth,
 456                 bool with_locked_monitors,
 457                 bool with_locked_synchronizers);
 458 
 459   VMOp_Type type() const { return VMOp_ThreadDump; }
 460   void doit();
 461   bool doit_prologue();
 462   void doit_epilogue();
 463 };
 464 
 465 
 466 class VM_Exit: public VM_Operation {
 467  private:
 468   int  _exit_code;
 469   static volatile bool _vm_exited;
 470   static Thread * volatile _shutdown_thread;
 471   static void wait_if_vm_exited();
 472  public:
 473   VM_Exit(int exit_code) {
 474     _exit_code = exit_code;
 475   }
 476   static int wait_for_threads_in_native_to_block();
 477   static int set_vm_exited();
 478   static bool vm_exited()                      { return _vm_exited; }
 479   static Thread * shutdown_thread()            { return _shutdown_thread; }
 480   static void block_if_vm_exited() {
 481     if (_vm_exited) {
 482       wait_if_vm_exited();
 483     }
 484   }
 485   VMOp_Type type() const { return VMOp_Exit; }
 486   void doit();
 487 };
 488 
 489 class VM_PrintCompileQueue: public VM_Operation {
 490  private:
 491   outputStream* _out;
 492 
 493  public:
 494   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 495   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 496   Mode evaluation_mode() const { return _safepoint; }
 497   void doit();
 498 };
 499 
 500 #if INCLUDE_SERVICES
 501 class VM_PrintClassHierarchy: public VM_Operation {
 502  private:
 503   outputStream* _out;
 504   bool _print_interfaces;
 505   bool _print_subclasses;
 506   char* _classname;
 507 
 508  public:
 509   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 510     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 511     _classname(classname) {}
 512   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 513   void doit();
 514 };
 515 #endif // INCLUDE_SERVICES
 516 
 517 #endif // SHARE_VM_RUNTIME_VM_OPERATIONS_HPP