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