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