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