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(ShenandoahPartialGC)                   \
 104   template(ShenandoahPreUpdateRoots)              \
 105   template(ShenandoahPostUpdateRoots)             \
 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   // Debugging
 202   virtual void print_on_error(outputStream* st) const;
 203   const char* name() const { return _names[type()]; }
 204   static const char* name(int type) {
 205     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 206     return _names[type];
 207   }
 208 #ifndef PRODUCT
 209   void print_on(outputStream* st) const { print_on_error(st); }
 210 #endif
 211 };
 212 
 213 class VM_ThreadStop: public VM_Operation {
 214  private:
 215   oop     _thread;        // The Thread that the Throwable is thrown against
 216   oop     _throwable;     // The Throwable thrown at the target Thread
 217  public:
 218   // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the
 219   // VM operation is executed.
 220   VM_ThreadStop(oop thread, oop throwable) {
 221     _thread    = thread;
 222     _throwable = throwable;
 223   }
 224   VMOp_Type type() const                         { return VMOp_ThreadStop; }
 225   oop target_thread() const                      { return _thread; }
 226   oop throwable() const                          { return _throwable;}
 227   void doit();
 228   // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated
 229   bool allow_nested_vm_operations() const        { return true; }
 230   Mode evaluation_mode() const                   { return _async_safepoint; }
 231   bool is_cheap_allocated() const                { return true; }
 232 
 233   // GC support
 234   void oops_do(OopClosure* f) {
 235     f->do_oop(&_thread); f->do_oop(&_throwable);
 236   }
 237 };
 238 
 239 class VM_ClearICs: public VM_Operation {
 240  private:
 241   bool _preserve_static_stubs;
 242  public:
 243   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 244   void doit();
 245   VMOp_Type type() const { return VMOp_ClearICs; }
 246 };
 247 
 248 // dummy vm op, evaluated just to force a safepoint
 249 class VM_ForceSafepoint: public VM_Operation {
 250  public:
 251   VM_ForceSafepoint() {}
 252   void doit()         {}
 253   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 254 };
 255 
 256 // dummy vm op, evaluated just to force a safepoint
 257 class VM_ForceAsyncSafepoint: public VM_Operation {
 258  public:
 259   VM_ForceAsyncSafepoint() {}
 260   void doit()              {}
 261   VMOp_Type type() const                         { return VMOp_ForceAsyncSafepoint; }
 262   Mode evaluation_mode() const                   { return _async_safepoint; }
 263   bool is_cheap_allocated() const                { return true; }
 264 };
 265 
 266 class VM_Deoptimize: public VM_Operation {
 267  public:
 268   VM_Deoptimize() {}
 269   VMOp_Type type() const                        { return VMOp_Deoptimize; }
 270   void doit();
 271   bool allow_nested_vm_operations() const        { return true; }
 272 };
 273 
 274 class VM_MarkActiveNMethods: public VM_Operation {
 275  public:
 276   VM_MarkActiveNMethods() {}
 277   VMOp_Type type() const                         { return VMOp_MarkActiveNMethods; }
 278   void doit();
 279   bool allow_nested_vm_operations() const        { return true; }
 280 };
 281 
 282 // Deopt helper that can deoptimize frames in threads other than the
 283 // current thread.  Only used through Deoptimization::deoptimize_frame.
 284 class VM_DeoptimizeFrame: public VM_Operation {
 285   friend class Deoptimization;
 286 
 287  private:
 288   JavaThread* _thread;
 289   intptr_t*   _id;
 290   int _reason;
 291   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 292 
 293  public:
 294   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 295   void doit();
 296   bool allow_nested_vm_operations() const        { return true;  }
 297 };
 298 
 299 #ifndef PRODUCT
 300 class VM_DeoptimizeAll: public VM_Operation {
 301  private:
 302   KlassHandle _dependee;
 303  public:
 304   VM_DeoptimizeAll() {}
 305   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 306   void doit();
 307   bool allow_nested_vm_operations() const        { return true; }
 308 };
 309 
 310 
 311 class VM_ZombieAll: public VM_Operation {
 312  public:
 313   VM_ZombieAll() {}
 314   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 315   void doit();
 316   bool allow_nested_vm_operations() const        { return true; }
 317 };
 318 #endif // PRODUCT
 319 
 320 class VM_UnlinkSymbols: public VM_Operation {
 321  public:
 322   VM_UnlinkSymbols() {}
 323   VMOp_Type type() const                         { return VMOp_UnlinkSymbols; }
 324   void doit();
 325   bool allow_nested_vm_operations() const        { return true; }
 326 };
 327 
 328 class VM_Verify: public VM_Operation {
 329  public:
 330   VMOp_Type type() const { return VMOp_Verify; }
 331   void doit();
 332 };
 333 
 334 
 335 class VM_PrintThreads: public VM_Operation {
 336  private:
 337   outputStream* _out;
 338   bool _print_concurrent_locks;
 339  public:
 340   VM_PrintThreads()                                                { _out = tty; _print_concurrent_locks = PrintConcurrentLocks; }
 341   VM_PrintThreads(outputStream* out, bool print_concurrent_locks)  { _out = out; _print_concurrent_locks = print_concurrent_locks; }
 342   VMOp_Type type() const                                           {  return VMOp_PrintThreads; }
 343   void doit();
 344   bool doit_prologue();
 345   void doit_epilogue();
 346 };
 347 
 348 class VM_PrintJNI: public VM_Operation {
 349  private:
 350   outputStream* _out;
 351  public:
 352   VM_PrintJNI()                         { _out = tty; }
 353   VM_PrintJNI(outputStream* out)        { _out = out; }
 354   VMOp_Type type() const                { return VMOp_PrintJNI; }
 355   void doit();
 356 };
 357 
 358 class DeadlockCycle;
 359 class VM_FindDeadlocks: public VM_Operation {
 360  private:
 361   bool           _concurrent_locks;
 362   DeadlockCycle* _deadlocks;
 363   outputStream*  _out;
 364 
 365  public:
 366   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _out(NULL), _deadlocks(NULL) {};
 367   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _out(st), _deadlocks(NULL) {};
 368   ~VM_FindDeadlocks();
 369 
 370   DeadlockCycle* result()      { return _deadlocks; };
 371   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 372   void doit();
 373   bool doit_prologue();
 374 };
 375 
 376 class ThreadDumpResult;
 377 class ThreadSnapshot;
 378 class ThreadConcurrentLocks;
 379 
 380 class VM_ThreadDump : public VM_Operation {
 381  private:
 382   ThreadDumpResult*              _result;
 383   int                            _num_threads;
 384   GrowableArray<instanceHandle>* _threads;
 385   int                            _max_depth;
 386   bool                           _with_locked_monitors;
 387   bool                           _with_locked_synchronizers;
 388 
 389   ThreadSnapshot* snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 390 
 391  public:
 392   VM_ThreadDump(ThreadDumpResult* result,
 393                 int max_depth,  // -1 indicates entire stack
 394                 bool with_locked_monitors,
 395                 bool with_locked_synchronizers);
 396 
 397   VM_ThreadDump(ThreadDumpResult* result,
 398                 GrowableArray<instanceHandle>* threads,
 399                 int num_threads, // -1 indicates entire stack
 400                 int max_depth,
 401                 bool with_locked_monitors,
 402                 bool with_locked_synchronizers);
 403 
 404   VMOp_Type type() const { return VMOp_ThreadDump; }
 405   void doit();
 406   bool doit_prologue();
 407   void doit_epilogue();
 408 };
 409 
 410 
 411 class VM_Exit: public VM_Operation {
 412  private:
 413   int  _exit_code;
 414   static volatile bool _vm_exited;
 415   static Thread * _shutdown_thread;
 416   static void wait_if_vm_exited();
 417  public:
 418   VM_Exit(int exit_code) {
 419     _exit_code = exit_code;
 420   }
 421   static int wait_for_threads_in_native_to_block();
 422   static int set_vm_exited();
 423   static bool vm_exited()                      { return _vm_exited; }
 424   static void block_if_vm_exited() {
 425     if (_vm_exited) {
 426       wait_if_vm_exited();
 427     }
 428   }
 429   VMOp_Type type() const { return VMOp_Exit; }
 430   void doit();
 431 };
 432 
 433 class VM_PrintCompileQueue: public VM_Operation {
 434  private:
 435   outputStream* _out;
 436 
 437  public:
 438   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 439   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 440   Mode evaluation_mode() const { return _safepoint; }
 441   void doit();
 442 };
 443 
 444 #if INCLUDE_SERVICES
 445 class VM_PrintClassHierarchy: public VM_Operation {
 446  private:
 447   outputStream* _out;
 448   bool _print_interfaces;
 449   bool _print_subclasses;
 450   char* _classname;
 451 
 452  public:
 453   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 454     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 455     _classname(classname) {}
 456   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 457   void doit();
 458 };
 459 #endif // INCLUDE_SERVICES
 460 
 461 #endif // SHARE_VM_RUNTIME_VM_OPERATIONS_HPP