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