1 /*
   2  * Copyright (c) 1997, 2017, 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(Exit)                                  \
 100   template(LinuxDllLoad)                          \
 101   template(RotateGCLog)                           \
 102   template(WhiteBoxOperation)                     \
 103   template(ClassLoaderStatsOperation)             \
 104   template(DumpHashtable)                         \
 105   template(DumpTouchedMethods)                    \
 106   template(MarkActiveNMethods)                    \
 107   template(PrintCompileQueue)                     \
 108   template(PrintClassHierarchy)                   \
 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   virtual 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 class VM_ClearICs: public VM_Operation {
 233  private:
 234   bool _preserve_static_stubs;
 235  public:
 236   VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; }
 237   void doit();
 238   VMOp_Type type() const { return VMOp_ClearICs; }
 239 };
 240 
 241 // dummy vm op, evaluated just to force a safepoint
 242 class VM_ForceSafepoint: public VM_Operation {
 243  public:
 244   VM_ForceSafepoint() {}
 245   void doit()         {}
 246   VMOp_Type type() const { return VMOp_ForceSafepoint; }
 247 };
 248 
 249 // dummy vm op, evaluated just to force a safepoint
 250 class VM_ForceAsyncSafepoint: public VM_Operation {
 251  public:
 252   VM_ForceAsyncSafepoint() {}
 253   void doit()              {}
 254   VMOp_Type type() const                         { return VMOp_ForceAsyncSafepoint; }
 255   Mode evaluation_mode() const                   { return _async_safepoint; }
 256   bool is_cheap_allocated() const                { return true; }
 257 };
 258 
 259 class VM_Deoptimize: public VM_Operation {
 260  public:
 261   VM_Deoptimize() {}
 262   VMOp_Type type() const                        { return VMOp_Deoptimize; }
 263   void doit();
 264   bool allow_nested_vm_operations() const        { return true; }
 265 };
 266 
 267 class VM_MarkActiveNMethods: public VM_Operation {
 268  public:
 269   VM_MarkActiveNMethods() {}
 270   VMOp_Type type() const                         { return VMOp_MarkActiveNMethods; }
 271   void doit();
 272   bool allow_nested_vm_operations() const        { return true; }
 273 };
 274 
 275 // Deopt helper that can deoptimize frames in threads other than the
 276 // current thread.  Only used through Deoptimization::deoptimize_frame.
 277 class VM_DeoptimizeFrame: public VM_Operation {
 278   friend class Deoptimization;
 279 
 280  private:
 281   JavaThread* _thread;
 282   intptr_t*   _id;
 283   int _reason;
 284   VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason);
 285 
 286  public:
 287   VMOp_Type type() const                         { return VMOp_DeoptimizeFrame; }
 288   void doit();
 289   bool allow_nested_vm_operations() const        { return true;  }
 290 };
 291 
 292 #ifndef PRODUCT
 293 class VM_DeoptimizeAll: public VM_Operation {
 294  private:
 295   Klass* _dependee;
 296  public:
 297   VM_DeoptimizeAll() {}
 298   VMOp_Type type() const                         { return VMOp_DeoptimizeAll; }
 299   void doit();
 300   bool allow_nested_vm_operations() const        { return true; }
 301 };
 302 
 303 
 304 class VM_ZombieAll: public VM_Operation {
 305  public:
 306   VM_ZombieAll() {}
 307   VMOp_Type type() const                         { return VMOp_ZombieAll; }
 308   void doit();
 309   bool allow_nested_vm_operations() const        { return true; }
 310 };
 311 #endif // PRODUCT
 312 
 313 class VM_UnlinkSymbols: public VM_Operation {
 314  public:
 315   VM_UnlinkSymbols() {}
 316   VMOp_Type type() const                         { return VMOp_UnlinkSymbols; }
 317   void doit();
 318   bool allow_nested_vm_operations() const        { return true; }
 319 };
 320 
 321 class VM_Verify: public VM_Operation {
 322  public:
 323   VMOp_Type type() const { return VMOp_Verify; }
 324   void doit();
 325 };
 326 
 327 
 328 class VM_PrintThreads: public VM_Operation {
 329  private:
 330   outputStream* _out;
 331   bool _print_concurrent_locks;
 332  public:
 333   VM_PrintThreads()                                                { _out = tty; _print_concurrent_locks = PrintConcurrentLocks; }
 334   VM_PrintThreads(outputStream* out, bool print_concurrent_locks)  { _out = out; _print_concurrent_locks = print_concurrent_locks; }
 335   VMOp_Type type() const                                           {  return VMOp_PrintThreads; }
 336   void doit();
 337   bool doit_prologue();
 338   void doit_epilogue();
 339 };
 340 
 341 class VM_PrintJNI: public VM_Operation {
 342  private:
 343   outputStream* _out;
 344  public:
 345   VM_PrintJNI()                         { _out = tty; }
 346   VM_PrintJNI(outputStream* out)        { _out = out; }
 347   VMOp_Type type() const                { return VMOp_PrintJNI; }
 348   void doit();
 349 };
 350 
 351 class DeadlockCycle;
 352 class VM_FindDeadlocks: public VM_Operation {
 353  private:
 354   bool           _concurrent_locks;
 355   DeadlockCycle* _deadlocks;
 356   outputStream*  _out;
 357 
 358  public:
 359   VM_FindDeadlocks(bool concurrent_locks) :  _concurrent_locks(concurrent_locks), _out(NULL), _deadlocks(NULL) {};
 360   VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _out(st), _deadlocks(NULL) {};
 361   ~VM_FindDeadlocks();
 362 
 363   DeadlockCycle* result()      { return _deadlocks; };
 364   VMOp_Type type() const       { return VMOp_FindDeadlocks; }
 365   void doit();
 366   bool doit_prologue();
 367 };
 368 
 369 class ThreadDumpResult;
 370 class ThreadSnapshot;
 371 class ThreadConcurrentLocks;
 372 
 373 class VM_ThreadDump : public VM_Operation {
 374  private:
 375   ThreadDumpResult*              _result;
 376   int                            _num_threads;
 377   GrowableArray<instanceHandle>* _threads;
 378   int                            _max_depth;
 379   bool                           _with_locked_monitors;
 380   bool                           _with_locked_synchronizers;
 381 
 382   ThreadSnapshot* snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl);
 383 
 384  public:
 385   VM_ThreadDump(ThreadDumpResult* result,
 386                 int max_depth,  // -1 indicates entire stack
 387                 bool with_locked_monitors,
 388                 bool with_locked_synchronizers);
 389 
 390   VM_ThreadDump(ThreadDumpResult* result,
 391                 GrowableArray<instanceHandle>* threads,
 392                 int num_threads, // -1 indicates entire stack
 393                 int max_depth,
 394                 bool with_locked_monitors,
 395                 bool with_locked_synchronizers);
 396 
 397   VMOp_Type type() const { return VMOp_ThreadDump; }
 398   void doit();
 399   bool doit_prologue();
 400   void doit_epilogue();
 401 };
 402 
 403 
 404 class VM_Exit: public VM_Operation {
 405  private:
 406   int  _exit_code;
 407   static volatile bool _vm_exited;
 408   static Thread * _shutdown_thread;
 409   static void wait_if_vm_exited();
 410  public:
 411   VM_Exit(int exit_code) {
 412     _exit_code = exit_code;
 413   }
 414   static int wait_for_threads_in_native_to_block();
 415   static int set_vm_exited();
 416   static bool vm_exited()                      { return _vm_exited; }
 417   static void block_if_vm_exited() {
 418     if (_vm_exited) {
 419       wait_if_vm_exited();
 420     }
 421   }
 422   VMOp_Type type() const { return VMOp_Exit; }
 423   void doit();
 424 };
 425 
 426 class VM_PrintCompileQueue: public VM_Operation {
 427  private:
 428   outputStream* _out;
 429 
 430  public:
 431   VM_PrintCompileQueue(outputStream* st) : _out(st) {}
 432   VMOp_Type type() const { return VMOp_PrintCompileQueue; }
 433   Mode evaluation_mode() const { return _safepoint; }
 434   void doit();
 435 };
 436 
 437 #if INCLUDE_SERVICES
 438 class VM_PrintClassHierarchy: public VM_Operation {
 439  private:
 440   outputStream* _out;
 441   bool _print_interfaces;
 442   bool _print_subclasses;
 443   char* _classname;
 444 
 445  public:
 446   VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) :
 447     _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses),
 448     _classname(classname) {}
 449   VMOp_Type type() const { return VMOp_PrintClassHierarchy; }
 450   void doit();
 451 };
 452 #endif // INCLUDE_SERVICES
 453 
 454 #endif // SHARE_VM_RUNTIME_VM_OPERATIONS_HPP