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