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