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