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