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