1 /*
   2  * Copyright (c) 1997, 2015, 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(ShenandoahFullGC)                      \
 100   template(ShenandoahInitMark)                    \
 101   template(ShenandoahFinalMarkStartEvac)          \
 102   template(ShenandoahVerifyHeapAfterEvacuation)   \
 103   template(ShenandoahPartialGC)                   \
 104   template(ShenandoahInitUpdateRefs)              \
 105   template(ShenandoahFinalUpdateRefs)             \
 106   template(Exit)                                  \
 107   template(LinuxDllLoad)                          \
 108   template(RotateGCLog)                           \
 109   template(WhiteBoxOperation)                     \
 110   template(ClassLoaderStatsOperation)             \
 111   template(DumpHashtable)                         \
 112   template(DumpTouchedMethods)                    \
 113   template(MarkActiveNMethods)                    \
 114   template(PrintCompileQueue)                     \
 115   template(PrintClassHierarchy)                   \
 116 
 117 class VM_Operation: public CHeapObj<mtInternal> {
 118  public:
 119   enum Mode {
 120     _safepoint,       // blocking,        safepoint, vm_op C-heap allocated
 121     _no_safepoint,    // blocking,     no safepoint, vm_op C-Heap allocated
 122     _concurrent,      // non-blocking, no safepoint, vm_op C-Heap allocated
 123     _async_safepoint  // non-blocking,    safepoint, vm_op C-Heap allocated
 124   };
 125 
 126   enum VMOp_Type {
 127     VM_OPS_DO(VM_OP_ENUM)
 128     VMOp_Terminating
 129   };
 130 
 131  private:
 132   Thread*         _calling_thread;
 133   ThreadPriority  _priority;
 134   long            _timestamp;
 135   VM_Operation*   _next;
 136   VM_Operation*   _prev;
 137 
 138   // The VM operation name array
 139   static const char* _names[];
 140 
 141  public:
 142   VM_Operation()  { _calling_thread = NULL; _next = NULL; _prev = NULL; }
 143   virtual ~VM_Operation() {}
 144 
 145   // VM operation support (used by VM thread)
 146   Thread* calling_thread() const                 { return _calling_thread; }
 147   ThreadPriority priority()                      { return _priority; }
 148   void set_calling_thread(Thread* thread, ThreadPriority priority);
 149 
 150   long timestamp() const              { return _timestamp; }
 151   void set_timestamp(long timestamp)  { _timestamp = timestamp; }
 152 
 153   // Called by VM thread - does in turn invoke doit(). Do not override this
 154   void evaluate();
 155 
 156   // evaluate() is called by the VMThread and in turn calls doit().
 157   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
 158   // doit_prologue() is called in that thread before transferring control to
 159   // the VMThread.
 160   // If doit_prologue() returns true the VM operation will proceed, and
 161   // doit_epilogue() will be called by the JavaThread once the VM operation
 162   // completes. If doit_prologue() returns false the VM operation is cancelled.
 163   virtual void doit()                            = 0;
 164   virtual bool doit_prologue()                   { return true; };
 165   virtual void doit_epilogue()                   {}; // Note: Not called if mode is: _concurrent
 166 
 167   // Type test
 168   virtual bool is_methodCompiler() const         { return false; }
 169 
 170   // Linking
 171   VM_Operation *next() const                     { return _next; }
 172   VM_Operation *prev() const                     { return _prev; }
 173   void set_next(VM_Operation *next)              { _next = next; }
 174   void set_prev(VM_Operation *prev)              { _prev = prev; }
 175 
 176   // Configuration. Override these appropriately in subclasses.
 177   virtual VMOp_Type type() const = 0;
 178   virtual Mode evaluation_mode() const            { return _safepoint; }
 179   virtual bool allow_nested_vm_operations() const { return false; }
 180   virtual bool is_cheap_allocated() const         { return false; }
 181   virtual void oops_do(OopClosure* f)              { /* do nothing */ };
 182 
 183   // CAUTION: <don't hang yourself with following rope>
 184   // If you override these methods, make sure that the evaluation
 185   // of these methods is race-free and non-blocking, since these
 186   // methods may be evaluated either by the mutators or by the
 187   // vm thread, either concurrently with mutators or with the mutators
 188   // stopped. In other words, taking locks is verboten, and if there
 189   // are any races in evaluating the conditions, they'd better be benign.
 190   virtual bool evaluate_at_safepoint() const {
 191     return evaluation_mode() == _safepoint  ||
 192            evaluation_mode() == _async_safepoint;
 193   }
 194   virtual bool evaluate_concurrently() const {
 195     return evaluation_mode() == _concurrent ||
 196            evaluation_mode() == _async_safepoint;
 197   }
 198 
 199   static const char* mode_to_string(Mode mode);
 200 
 201   // Safepoint cleanup
 202   // Return true if this VM_Operation takes care of idle monitor deflation.
 203   // Idle monitor deflation is usually done by the safepoint cleanup phase
 204   // in SafepointSynchronize::do_cleanup_tasks(). However, a VM_Operation
 205   // may want to take care of this itself, for example if a GC operation
 206   // scans the thread stack anyway, it probably can piggy-back monitor
 207   // deflation. Note that this is only possible if the oop marks are preserved
 208   // during the VM operation (for example, most current GCs *don't* preserve
 209   // the mark word, but displace it and temporarily use the mark word as
 210   // forwarding pointer).
 211   virtual bool deflates_idle_monitors() { return false; }
 212 
 213   // Return true if this VM_Operation takes care of nmethod marking.
 214   // NMethod marking is usually done by the safepoint cleanup phase
 215   // in SafepointSynchronize::do_cleanup_tasks(). However, a VM_Operation
 216   // may want to take care of this itself, for example if a GC operation
 217   // scans the thread stack anyway, it can just as well piggy-back nmethod
 218   // marking.
 219   virtual bool marks_nmethods() { return false; }
 220 
 221   // Debugging
 222   virtual void print_on_error(outputStream* st) const;
 223   const char* name() const { return _names[type()]; }
 224   static const char* name(int type) {
 225     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 226     return _names[type];
 227   }
 228 #ifndef PRODUCT
 229   void print_on(outputStream* st) const { print_on_error(st); }
 230 #endif
 231 };
 232 
 233 class VM_ThreadStop: public VM_Operation {
 234  private:
 235   oop     _thread;        // The Thread that the Throwable is thrown against
 236   oop     _throwable;     // The Throwable thrown at the target Thread
 237  public:
 238   // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the
 239   // VM operation is executed.
 240   VM_ThreadStop(oop thread, oop throwable) {
 241     _thread    = thread;
 242     _throwable = throwable;
 243   }
 244   VMOp_Type type() const                         { return VMOp_ThreadStop; }
 245   oop target_thread() const                      { return _thread; }
 246   oop throwable() const                          { return _throwable;}
 247   void doit();
 248   // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated
 249   bool allow_nested_vm_operations() const        { return true; }
 250   Mode evaluation_mode() const                   { return _async_safepoint; }
 251   bool is_cheap_allocated() const                { return true; }
 252 
 253   // GC support
 254   void oops_do(OopClosure* f) {
 255     f->do_oop(&_thread); f->do_oop(&_throwable);
 256   }
 257 };
 258 
 259 class VM_ClearICs: public VM_Operation {
 260  private:
 261   bool _preserve_static_stubs;
 262  public:
 263   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 264   void doit();
 265   VMOp_Type type() const { return VMOp_ClearICs; }
 266 };
 267 
 268 // dummy vm op, evaluated just to force a safepoint
 269 class VM_ForceSafepoint: public VM_Operation {
 270  public:
 271   VM_ForceSafepoint() {}
 272   void doit()         {}
 273   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 274 };
 275 
 276 // dummy vm op, evaluated just to force a safepoint
 277 class VM_ForceAsyncSafepoint: public VM_Operation {
 278  public:
 279   VM_ForceAsyncSafepoint() {}
 280   void doit()              {}
 281   VMOp_Type type() const                         { return VMOp_ForceAsyncSafepoint; }
 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   KlassHandle _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 DeadlockCycle;
 379 class VM_FindDeadlocks: public VM_Operation {
 380  private:
 381   bool           _concurrent_locks;
 382   DeadlockCycle* _deadlocks;
 383   outputStream*  _out;
 384 
 385  public:
 386   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _out(NULL), _deadlocks(NULL) {};
 387   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _out(st), _deadlocks(NULL) {};
 388   ~VM_FindDeadlocks();
 389 
 390   DeadlockCycle* result()      { return _deadlocks; };
 391   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 392   void doit();
 393   bool doit_prologue();
 394 };
 395 
 396 class ThreadDumpResult;
 397 class ThreadSnapshot;
 398 class ThreadConcurrentLocks;
 399 
 400 class VM_ThreadDump : public VM_Operation {
 401  private:
 402   ThreadDumpResult*              _result;
 403   int                            _num_threads;
 404   GrowableArray<instanceHandle>* _threads;
 405   int                            _max_depth;
 406   bool                           _with_locked_monitors;
 407   bool                           _with_locked_synchronizers;
 408 
 409   ThreadSnapshot* snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 410 
 411  public:
 412   VM_ThreadDump(ThreadDumpResult* result,
 413                 int max_depth,  // -1 indicates entire stack
 414                 bool with_locked_monitors,
 415                 bool with_locked_synchronizers);
 416 
 417   VM_ThreadDump(ThreadDumpResult* result,
 418                 GrowableArray<instanceHandle>* threads,
 419                 int num_threads, // -1 indicates entire stack
 420                 int max_depth,
 421                 bool with_locked_monitors,
 422                 bool with_locked_synchronizers);
 423 
 424   VMOp_Type type() const { return VMOp_ThreadDump; }
 425   void doit();
 426   bool doit_prologue();
 427   void doit_epilogue();
 428 };
 429 
 430 
 431 class VM_Exit: public VM_Operation {
 432  private:
 433   int  _exit_code;
 434   static volatile bool _vm_exited;
 435   static Thread * _shutdown_thread;
 436   static void wait_if_vm_exited();
 437  public:
 438   VM_Exit(int exit_code) {
 439     _exit_code = exit_code;
 440   }
 441   static int wait_for_threads_in_native_to_block();
 442   static int set_vm_exited();
 443   static bool vm_exited()                      { return _vm_exited; }
 444   static void block_if_vm_exited() {
 445     if (_vm_exited) {
 446       wait_if_vm_exited();
 447     }
 448   }
 449   VMOp_Type type() const { return VMOp_Exit; }
 450   void doit();
 451 };
 452 
 453 class VM_PrintCompileQueue: public VM_Operation {
 454  private:
 455   outputStream* _out;
 456 
 457  public:
 458   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 459   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 460   Mode evaluation_mode() const { return _safepoint; }
 461   void doit();
 462 };
 463 
 464 #if INCLUDE_SERVICES
 465 class VM_PrintClassHierarchy: public VM_Operation {
 466  private:
 467   outputStream* _out;
 468   bool _print_interfaces;
 469   bool _print_subclasses;
 470   char* _classname;
 471 
 472  public:
 473   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 474     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 475     _classname(classname) {}
 476   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 477   void doit();
 478 };
 479 #endif // INCLUDE_SERVICES
 480 
 481 #endif // SHARE_VM_RUNTIME_VM_OPERATIONS_HPP