1 /*
   2  * Copyright (c) 1997, 2014, 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 "utilities/top.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(ForceSafepoint)                        \
  48   template(ForceAsyncSafepoint)                   \
  49   template(Deoptimize)                            \
  50   template(DeoptimizeFrame)                       \
  51   template(DeoptimizeAll)                         \
  52   template(ZombieAll)                             \
  53   template(UnlinkSymbols)                         \
  54   template(Verify)                                \
  55   template(PrintJNI)                              \
  56   template(HeapDumper)                            \
  57   template(DeoptimizeTheWorld)                    \
  58   template(CollectForMetadataAllocation)          \
  59   template(GC_HeapInspection)                     \
  60   template(GenCollectFull)                        \
  61   template(GenCollectFullConcurrent)              \
  62   template(GenCollectForAllocation)               \
  63   template(ParallelGCFailedAllocation)            \
  64   template(ParallelGCSystemGC)                    \
  65   template(CGC_Operation)                         \
  66   template(CMS_Initial_Mark)                      \
  67   template(CMS_Final_Remark)                      \
  68   template(G1CollectFull)                         \
  69   template(G1CollectForAllocation)                \
  70   template(G1IncCollectionPause)                  \
  71   template(EnableBiasedLocking)                   \
  72   template(RevokeBias)                            \
  73   template(BulkRevokeBias)                        \
  74   template(PopulateDumpSharedSpace)               \
  75   template(JNIFunctionTableCopier)                \
  76   template(RedefineClasses)                       \
  77   template(UpdateForPopTopFrame)                  \
  78   template(SetFramePop)                           \
  79   template(GetOwnedMonitorInfo)                   \
  80   template(GetObjectMonitorUsage)                 \
  81   template(GetCurrentContendedMonitor)            \
  82   template(GetStackTrace)                         \
  83   template(GetMultipleStackTraces)                \
  84   template(GetAllStackTraces)                     \
  85   template(GetThreadListStackTraces)              \
  86   template(GetFrameCount)                         \
  87   template(GetFrameLocation)                      \
  88   template(ChangeBreakpoints)                     \
  89   template(GetOrSetLocal)                         \
  90   template(GetCurrentLocation)                    \
  91   template(EnterInterpOnlyMode)                   \
  92   template(ChangeSingleStep)                      \
  93   template(HeapWalkOperation)                     \
  94   template(HeapIterateOperation)                  \
  95   template(ReportJavaOutOfMemory)                 \
  96   template(JFRCheckpoint)                         \
  97   template(Exit)                                  \
  98   template(LinuxDllLoad)                          \
  99   template(RotateGCLog)                           \
 100   template(WhiteBoxOperation)                     \
 101   template(ClassLoaderStatsOperation)             \
 102 
 103 class VM_Operation: public CHeapObj<mtInternal> {
 104  public:
 105   enum Mode {
 106     _safepoint,       // blocking,        safepoint, vm_op C-heap allocated
 107     _no_safepoint,    // blocking,     no safepoint, vm_op C-Heap allocated
 108     _concurrent,      // non-blocking, no safepoint, vm_op C-Heap allocated
 109     _async_safepoint  // non-blocking,    safepoint, vm_op C-Heap allocated
 110   };
 111 
 112   enum VMOp_Type {
 113     VM_OPS_DO(VM_OP_ENUM)
 114     VMOp_Terminating
 115   };
 116 
 117  private:
 118   Thread*         _calling_thread;
 119   ThreadPriority  _priority;
 120   long            _timestamp;
 121   VM_Operation*   _next;
 122   VM_Operation*   _prev;
 123 
 124   // The VM operation name array
 125   static const char* _names[];
 126 
 127  public:
 128   VM_Operation()  { _calling_thread = NULL; _next = NULL; _prev = NULL; }
 129   virtual ~VM_Operation() {}
 130 
 131   // VM operation support (used by VM thread)
 132   Thread* calling_thread() const                 { return _calling_thread; }
 133   ThreadPriority priority()                      { return _priority; }
 134   void set_calling_thread(Thread* thread, ThreadPriority priority);
 135 
 136   long timestamp() const              { return _timestamp; }
 137   void set_timestamp(long timestamp)  { _timestamp = timestamp; }
 138 
 139   // Called by VM thread - does in turn invoke doit(). Do not override this
 140   void evaluate();
 141 
 142   // evaluate() is called by the VMThread and in turn calls doit().
 143   // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread,
 144   // doit_prologue() is called in that thread before transferring control to
 145   // the VMThread.
 146   // If doit_prologue() returns true the VM operation will proceed, and
 147   // doit_epilogue() will be called by the JavaThread once the VM operation
 148   // completes. If doit_prologue() returns false the VM operation is cancelled.
 149   virtual void doit()                            = 0;
 150   virtual bool doit_prologue()                   { return true; };
 151   virtual void doit_epilogue()                   {}; // Note: Not called if mode is: _concurrent
 152 
 153   // Type test
 154   virtual bool is_methodCompiler() const         { return false; }
 155 
 156   // Linking
 157   VM_Operation *next() const                     { return _next; }
 158   VM_Operation *prev() const                     { return _prev; }
 159   void set_next(VM_Operation *next)              { _next = next; }
 160   void set_prev(VM_Operation *prev)              { _prev = prev; }
 161 
 162   // Configuration. Override these appropriately in subclasses.
 163   virtual VMOp_Type type() const = 0;
 164   virtual Mode evaluation_mode() const            { return _safepoint; }
 165   virtual bool allow_nested_vm_operations() const { return false; }
 166   virtual bool is_cheap_allocated() const         { return false; }
 167   virtual void oops_do(OopClosure* f)              { /* do nothing */ };
 168 
 169   // CAUTION: <don't hang yourself with following rope>
 170   // If you override these methods, make sure that the evaluation
 171   // of these methods is race-free and non-blocking, since these
 172   // methods may be evaluated either by the mutators or by the
 173   // vm thread, either concurrently with mutators or with the mutators
 174   // stopped. In other words, taking locks is verboten, and if there
 175   // are any races in evaluating the conditions, they'd better be benign.
 176   virtual bool evaluate_at_safepoint() const {
 177     return evaluation_mode() == _safepoint  ||
 178            evaluation_mode() == _async_safepoint;
 179   }
 180   virtual bool evaluate_concurrently() const {
 181     return evaluation_mode() == _concurrent ||
 182            evaluation_mode() == _async_safepoint;
 183   }
 184 
 185   static const char* mode_to_string(Mode mode);
 186 
 187   // Debugging
 188   void print_on_error(outputStream* st) const;
 189   const char* name() const { return _names[type()]; }
 190   static const char* name(int type) {
 191     assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type");
 192     return _names[type];
 193   }
 194 #ifndef PRODUCT
 195   void print_on(outputStream* st) const { print_on_error(st); }
 196 #endif
 197 };
 198 
 199 class VM_ThreadStop: public VM_Operation {
 200  private:
 201   oop     _thread;        // The Thread that the Throwable is thrown against
 202   oop     _throwable;     // The Throwable thrown at the target Thread
 203  public:
 204   // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the
 205   // VM operation is executed.
 206   VM_ThreadStop(oop thread, oop throwable) {
 207     _thread    = thread;
 208     _throwable = throwable;
 209   }
 210   VMOp_Type type() const                         { return VMOp_ThreadStop; }
 211   oop target_thread() const                      { return _thread; }
 212   oop throwable() const                          { return _throwable;}
 213   void doit();
 214   // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated
 215   bool allow_nested_vm_operations() const        { return true; }
 216   Mode evaluation_mode() const                   { return _async_safepoint; }
 217   bool is_cheap_allocated() const                { return true; }
 218 
 219   // GC support
 220   void oops_do(OopClosure* f) {
 221     f->do_oop(&_thread); f->do_oop(&_throwable);
 222   }
 223 };
 224 
 225 // dummy vm op, evaluated just to force a safepoint
 226 class VM_ForceSafepoint: public VM_Operation {
 227  public:
 228   VM_ForceSafepoint() {}
 229   void doit()         {}
 230   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 231 };
 232 
 233 // dummy vm op, evaluated just to force a safepoint
 234 class VM_ForceAsyncSafepoint: public VM_Operation {
 235  public:
 236   VM_ForceAsyncSafepoint() {}
 237   void doit()              {}
 238   VMOp_Type type() const                         { return VMOp_ForceAsyncSafepoint; }
 239   Mode evaluation_mode() const                   { return _async_safepoint; }
 240   bool is_cheap_allocated() const                { return true; }
 241 };
 242 
 243 class VM_Deoptimize: public VM_Operation {
 244  public:
 245   VM_Deoptimize() {}
 246   VMOp_Type type() const                        { return VMOp_Deoptimize; }
 247   void doit();
 248   bool allow_nested_vm_operations() const        { return true; }
 249 };
 250 
 251 
 252 // Deopt helper that can deoptimize frames in threads other than the
 253 // current thread.  Only used through Deoptimization::deoptimize_frame.
 254 class VM_DeoptimizeFrame: public VM_Operation {
 255   friend class Deoptimization;
 256 
 257  private:
 258   JavaThread* _thread;
 259   intptr_t*   _id;
 260   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id);
 261 
 262  public:
 263   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 264   void doit();
 265   bool allow_nested_vm_operations() const        { return true;  }
 266 };
 267 
 268 #ifndef PRODUCT
 269 class VM_DeoptimizeAll: public VM_Operation {
 270  private:
 271   KlassHandle _dependee;
 272  public:
 273   VM_DeoptimizeAll() {}
 274   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 275   void doit();
 276   bool allow_nested_vm_operations() const        { return true; }
 277 };
 278 
 279 
 280 class VM_ZombieAll: public VM_Operation {
 281  public:
 282   VM_ZombieAll() {}
 283   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 284   void doit();
 285   bool allow_nested_vm_operations() const        { return true; }
 286 };
 287 #endif // PRODUCT
 288 
 289 class VM_UnlinkSymbols: public VM_Operation {
 290  public:
 291   VM_UnlinkSymbols() {}
 292   VMOp_Type type() const                         { return VMOp_UnlinkSymbols; }
 293   void doit();
 294   bool allow_nested_vm_operations() const        { return true; }
 295 };
 296 
 297 class VM_Verify: public VM_Operation {
 298  private:
 299   bool _silent;
 300  public:
 301   VM_Verify(bool silent = VerifySilently) : _silent(silent) {}
 302   VMOp_Type type() const { return VMOp_Verify; }
 303   void doit();
 304 };
 305 
 306 
 307 class VM_PrintThreads: public VM_Operation {
 308  private:
 309   outputStream* _out;
 310   bool _print_concurrent_locks;
 311  public:
 312   VM_PrintThreads()                                                { _out = tty; _print_concurrent_locks = PrintConcurrentLocks; }
 313   VM_PrintThreads(outputStream* out, bool print_concurrent_locks)  { _out = out; _print_concurrent_locks = print_concurrent_locks; }
 314   VMOp_Type type() const                                           {  return VMOp_PrintThreads; }
 315   void doit();
 316   bool doit_prologue();
 317   void doit_epilogue();
 318 };
 319 
 320 class VM_PrintJNI: public VM_Operation {
 321  private:
 322   outputStream* _out;
 323  public:
 324   VM_PrintJNI()                         { _out = tty; }
 325   VM_PrintJNI(outputStream* out)        { _out = out; }
 326   VMOp_Type type() const                { return VMOp_PrintJNI; }
 327   void doit();
 328 };
 329 
 330 class DeadlockCycle;
 331 class VM_FindDeadlocks: public VM_Operation {
 332  private:
 333   bool           _concurrent_locks;
 334   DeadlockCycle* _deadlocks;
 335   outputStream*  _out;
 336 
 337  public:
 338   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _out(NULL), _deadlocks(NULL) {};
 339   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _out(st), _deadlocks(NULL) {};
 340   ~VM_FindDeadlocks();
 341 
 342   DeadlockCycle* result()      { return _deadlocks; };
 343   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 344   void doit();
 345   bool doit_prologue();
 346 };
 347 
 348 class ThreadDumpResult;
 349 class ThreadSnapshot;
 350 class ThreadConcurrentLocks;
 351 
 352 class VM_ThreadDump : public VM_Operation {
 353  private:
 354   ThreadDumpResult*              _result;
 355   int                            _num_threads;
 356   GrowableArray<instanceHandle>* _threads;
 357   int                            _max_depth;
 358   bool                           _with_locked_monitors;
 359   bool                           _with_locked_synchronizers;
 360 
 361   ThreadSnapshot* snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 362 
 363  public:
 364   VM_ThreadDump(ThreadDumpResult* result,
 365                 int max_depth,  // -1 indicates entire stack
 366                 bool with_locked_monitors,
 367                 bool with_locked_synchronizers);
 368 
 369   VM_ThreadDump(ThreadDumpResult* result,
 370                 GrowableArray<instanceHandle>* threads,
 371                 int num_threads, // -1 indicates entire stack
 372                 int max_depth,
 373                 bool with_locked_monitors,
 374                 bool with_locked_synchronizers);
 375 
 376   VMOp_Type type() const { return VMOp_ThreadDump; }
 377   void doit();
 378   bool doit_prologue();
 379   void doit_epilogue();
 380 };
 381 
 382 
 383 class VM_Exit: public VM_Operation {
 384  private:
 385   int  _exit_code;
 386   static volatile bool _vm_exited;
 387   static Thread * _shutdown_thread;
 388   static void wait_if_vm_exited();
 389  public:
 390   VM_Exit(int exit_code) {
 391     _exit_code = exit_code;
 392   }
 393   static int wait_for_threads_in_native_to_block();
 394   static int set_vm_exited();
 395   static bool vm_exited()                      { return _vm_exited; }
 396   static void block_if_vm_exited() {
 397     if (_vm_exited) {
 398       wait_if_vm_exited();
 399     }
 400   }
 401   VMOp_Type type() const { return VMOp_Exit; }
 402   void doit();
 403 };
 404 
 405 
 406 class VM_RotateGCLog: public VM_Operation {
 407  private:
 408   outputStream* _out;
 409 
 410  public:
 411   VM_RotateGCLog(outputStream* st) : _out(st) {}
 412   VMOp_Type type() const { return VMOp_RotateGCLog; }
 413   void doit() { gclog_or_tty->rotate_log(true, _out); }
 414 };
 415 
 416 #endif // SHARE_VM_RUNTIME_VM_OPERATIONS_HPP