1 /* 2 * Copyright (c) 1997, 2018, 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 "runtime/threadSMR.hpp" 33 #include "code/codeCache.hpp" 34 35 // The following classes are used for operations 36 // initiated by a Java thread but that must 37 // take place in the VMThread. 38 39 #define VM_OP_ENUM(type) VMOp_##type, 40 41 // Note: When new VM_XXX comes up, add 'XXX' to the template table. 42 #define VM_OPS_DO(template) \ 43 template(Dummy) \ 44 template(ThreadStop) \ 45 template(ThreadDump) \ 46 template(PrintThreads) \ 47 template(FindDeadlocks) \ 48 template(ClearICs) \ 49 template(ForceSafepoint) \ 50 template(ForceAsyncSafepoint) \ 51 template(Deoptimize) \ 52 template(DeoptimizeFrame) \ 53 template(DeoptimizeAll) \ 54 template(ZombieAll) \ 55 template(UnlinkSymbols) \ 56 template(Verify) \ 57 template(PrintJNI) \ 58 template(HeapDumper) \ 59 template(DeoptimizeTheWorld) \ 60 template(CollectForMetadataAllocation) \ 61 template(GC_HeapInspection) \ 62 template(GenCollectFull) \ 63 template(GenCollectFullConcurrent) \ 64 template(GenCollectForAllocation) \ 65 template(ParallelGCFailedAllocation) \ 66 template(ParallelGCSystemGC) \ 67 template(CGC_Operation) \ 68 template(CMS_Initial_Mark) \ 69 template(CMS_Final_Remark) \ 70 template(G1CollectForAllocation) \ 71 template(G1CollectFull) \ 72 template(HandshakeOneThread) \ 73 template(HandshakeAllThreads) \ 74 template(HandshakeFallback) \ 75 template(EnableBiasedLocking) \ 76 template(RevokeBias) \ 77 template(BulkRevokeBias) \ 78 template(PopulateDumpSharedSpace) \ 79 template(JNIFunctionTableCopier) \ 80 template(RedefineClasses) \ 81 template(UpdateForPopTopFrame) \ 82 template(SetFramePop) \ 83 template(GetOwnedMonitorInfo) \ 84 template(GetObjectMonitorUsage) \ 85 template(GetCurrentContendedMonitor) \ 86 template(GetStackTrace) \ 87 template(GetMultipleStackTraces) \ 88 template(GetAllStackTraces) \ 89 template(GetThreadListStackTraces) \ 90 template(GetFrameCount) \ 91 template(GetFrameLocation) \ 92 template(ChangeBreakpoints) \ 93 template(GetOrSetLocal) \ 94 template(GetCurrentLocation) \ 95 template(EnterInterpOnlyMode) \ 96 template(ChangeSingleStep) \ 97 template(HeapWalkOperation) \ 98 template(HeapIterateOperation) \ 99 template(ReportJavaOutOfMemory) \ 100 template(JFRCheckpoint) \ 101 template(ShenandoahFullGC) \ 102 template(ShenandoahInitMark) \ 103 template(ShenandoahFinalMarkStartEvac) \ 104 template(ShenandoahFinalEvac) \ 105 template(ShenandoahInitTraversalGC) \ 106 template(ShenandoahFinalTraversalGC) \ 107 template(ShenandoahInitUpdateRefs) \ 108 template(ShenandoahFinalUpdateRefs) \ 109 template(ShenandoahDegeneratedGC) \ 110 template(Exit) \ 111 template(LinuxDllLoad) \ 112 template(RotateGCLog) \ 113 template(WhiteBoxOperation) \ 114 template(ClassLoaderStatsOperation) \ 115 template(DumpHashtable) \ 116 template(DumpTouchedMethods) \ 117 template(MarkActiveNMethods) \ 118 template(PrintCompileQueue) \ 119 template(PrintClassHierarchy) \ 120 template(ThreadSuspend) \ 121 template(CTWThreshold) \ 122 template(ThreadsSuspendJVMTI) \ 123 template(ICBufferFull) \ 124 template(ScavengeMonitors) \ 125 template(PrintMetadata) \ 126 template(GTestExecuteAtSafepoint) \ 127 128 class VM_Operation: public CHeapObj<mtInternal> { 129 public: 130 enum Mode { 131 _safepoint, // blocking, safepoint, vm_op C-heap allocated 132 _no_safepoint, // blocking, no safepoint, vm_op C-Heap allocated 133 _concurrent, // non-blocking, no safepoint, vm_op C-Heap allocated 134 _async_safepoint // non-blocking, safepoint, vm_op C-Heap allocated 135 }; 136 137 enum VMOp_Type { 138 VM_OPS_DO(VM_OP_ENUM) 139 VMOp_Terminating 140 }; 141 142 private: 143 Thread* _calling_thread; 144 ThreadPriority _priority; 145 long _timestamp; 146 VM_Operation* _next; 147 VM_Operation* _prev; 148 149 // The VM operation name array 150 static const char* _names[]; 151 152 public: 153 VM_Operation() { _calling_thread = NULL; _next = NULL; _prev = NULL; } 154 virtual ~VM_Operation() {} 155 156 // VM operation support (used by VM thread) 157 Thread* calling_thread() const { return _calling_thread; } 158 ThreadPriority priority() { return _priority; } 159 void set_calling_thread(Thread* thread, ThreadPriority priority); 160 161 long timestamp() const { return _timestamp; } 162 void set_timestamp(long timestamp) { _timestamp = timestamp; } 163 164 // Called by VM thread - does in turn invoke doit(). Do not override this 165 void evaluate(); 166 167 // evaluate() is called by the VMThread and in turn calls doit(). 168 // If the thread invoking VMThread::execute((VM_Operation*) is a JavaThread, 169 // doit_prologue() is called in that thread before transferring control to 170 // the VMThread. 171 // If doit_prologue() returns true the VM operation will proceed, and 172 // doit_epilogue() will be called by the JavaThread once the VM operation 173 // completes. If doit_prologue() returns false the VM operation is cancelled. 174 virtual void doit() = 0; 175 virtual bool doit_prologue() { return true; }; 176 virtual void doit_epilogue() {}; // Note: Not called if mode is: _concurrent 177 178 // Type test 179 virtual bool is_methodCompiler() const { return false; } 180 181 // Linking 182 VM_Operation *next() const { return _next; } 183 VM_Operation *prev() const { return _prev; } 184 void set_next(VM_Operation *next) { _next = next; } 185 void set_prev(VM_Operation *prev) { _prev = prev; } 186 187 // Configuration. Override these appropriately in subclasses. 188 virtual VMOp_Type type() const = 0; 189 virtual Mode evaluation_mode() const { return _safepoint; } 190 virtual bool allow_nested_vm_operations() const { return false; } 191 virtual bool is_cheap_allocated() const { return false; } 192 virtual void oops_do(OopClosure* f) { /* do nothing */ }; 193 194 // CAUTION: <don't hang yourself with following rope> 195 // If you override these methods, make sure that the evaluation 196 // of these methods is race-free and non-blocking, since these 197 // methods may be evaluated either by the mutators or by the 198 // vm thread, either concurrently with mutators or with the mutators 199 // stopped. In other words, taking locks is verboten, and if there 200 // are any races in evaluating the conditions, they'd better be benign. 201 virtual bool evaluate_at_safepoint() const { 202 return evaluation_mode() == _safepoint || 203 evaluation_mode() == _async_safepoint; 204 } 205 virtual bool evaluate_concurrently() const { 206 return evaluation_mode() == _concurrent || 207 evaluation_mode() == _async_safepoint; 208 } 209 210 static const char* mode_to_string(Mode mode); 211 212 // Safepoint cleanup 213 // Return true if this VM_Operation takes care of idle monitor deflation. 214 // Idle monitor deflation is usually done by the safepoint cleanup phase 215 // in SafepointSynchronize::do_cleanup_tasks(). However, a VM_Operation 216 // may want to take care of this itself, for example if a GC operation 217 // scans the thread stack anyway, it probably can piggy-back monitor 218 // deflation. Note that this is only possible if the oop marks are preserved 219 // during the VM operation (for example, most current GCs *don't* preserve 220 // the mark word, but displace it and temporarily use the mark word as 221 // forwarding pointer). 222 virtual bool deflates_idle_monitors() { return false; } 223 224 // Return true if this VM_Operation takes care of nmethod marking. 225 // NMethod marking is usually done by the safepoint cleanup phase 226 // in SafepointSynchronize::do_cleanup_tasks(). However, a VM_Operation 227 // may want to take care of this itself, for example if a GC operation 228 // scans the thread stack anyway, it can just as well piggy-back nmethod 229 // marking. 230 virtual bool marks_nmethods() { return false; } 231 232 // Debugging 233 virtual void print_on_error(outputStream* st) const; 234 const char* name() const { return _names[type()]; } 235 static const char* name(int type) { 236 assert(type >= 0 && type < VMOp_Terminating, "invalid VM operation type"); 237 return _names[type]; 238 } 239 #ifndef PRODUCT 240 void print_on(outputStream* st) const { print_on_error(st); } 241 #endif 242 }; 243 244 class VM_ThreadStop: public VM_Operation { 245 private: 246 oop _thread; // The Thread that the Throwable is thrown against 247 oop _throwable; // The Throwable thrown at the target Thread 248 public: 249 // All oops are passed as JNI handles, since there is no guarantee that a GC might happen before the 250 // VM operation is executed. 251 VM_ThreadStop(oop thread, oop throwable) { 252 _thread = thread; 253 _throwable = throwable; 254 } 255 VMOp_Type type() const { return VMOp_ThreadStop; } 256 oop target_thread() const { return _thread; } 257 oop throwable() const { return _throwable;} 258 void doit(); 259 // We deoptimize if top-most frame is compiled - this might require a C2I adapter to be generated 260 bool allow_nested_vm_operations() const { return true; } 261 Mode evaluation_mode() const { return _async_safepoint; } 262 bool is_cheap_allocated() const { return true; } 263 264 // GC support 265 void oops_do(OopClosure* f) { 266 f->do_oop(&_thread); f->do_oop(&_throwable); 267 } 268 }; 269 270 class VM_ClearICs: public VM_Operation { 271 private: 272 bool _preserve_static_stubs; 273 public: 274 VM_ClearICs(bool preserve_static_stubs) { _preserve_static_stubs = preserve_static_stubs; } 275 void doit(); 276 VMOp_Type type() const { return VMOp_ClearICs; } 277 }; 278 279 // empty vm op, evaluated just to force a safepoint 280 class VM_ForceSafepoint: public VM_Operation { 281 public: 282 void doit() {} 283 VMOp_Type type() const { return VMOp_ForceSafepoint; } 284 }; 285 286 // empty vm op, when forcing a safepoint to suspend a thread 287 class VM_ThreadSuspend: public VM_ForceSafepoint { 288 public: 289 VMOp_Type type() const { return VMOp_ThreadSuspend; } 290 }; 291 292 // empty vm op, when forcing a safepoint due to ctw threshold is reached for the sweeper 293 class VM_CTWThreshold: public VM_ForceSafepoint { 294 public: 295 VMOp_Type type() const { return VMOp_CTWThreshold; } 296 }; 297 298 // empty vm op, when forcing a safepoint to suspend threads from jvmti 299 class VM_ThreadsSuspendJVMTI: public VM_ForceSafepoint { 300 public: 301 VMOp_Type type() const { return VMOp_ThreadsSuspendJVMTI; } 302 }; 303 304 // empty vm op, when forcing a safepoint due to inline cache buffers being full 305 class VM_ICBufferFull: public VM_ForceSafepoint { 306 public: 307 VMOp_Type type() const { return VMOp_ICBufferFull; } 308 }; 309 310 // empty asynchronous vm op, when forcing a safepoint to scavenge monitors 311 class VM_ScavengeMonitors: public VM_ForceSafepoint { 312 public: 313 VMOp_Type type() const { return VMOp_ScavengeMonitors; } 314 Mode evaluation_mode() const { return _async_safepoint; } 315 bool is_cheap_allocated() const { return true; } 316 }; 317 318 // Base class for invoking parts of a gtest in a safepoint. 319 // Derived classes provide the doit method. 320 // Typically also need to transition the gtest thread from native to VM. 321 class VM_GTestExecuteAtSafepoint: public VM_Operation { 322 public: 323 VMOp_Type type() const { return VMOp_GTestExecuteAtSafepoint; } 324 325 protected: 326 VM_GTestExecuteAtSafepoint() {} 327 }; 328 329 class VM_Deoptimize: public VM_Operation { 330 public: 331 VM_Deoptimize() {} 332 VMOp_Type type() const { return VMOp_Deoptimize; } 333 void doit(); 334 bool allow_nested_vm_operations() const { return true; } 335 }; 336 337 class VM_MarkActiveNMethods: public VM_Operation { 338 public: 339 VM_MarkActiveNMethods() {} 340 VMOp_Type type() const { return VMOp_MarkActiveNMethods; } 341 void doit(); 342 bool allow_nested_vm_operations() const { return true; } 343 }; 344 345 // Deopt helper that can deoptimize frames in threads other than the 346 // current thread. Only used through Deoptimization::deoptimize_frame. 347 class VM_DeoptimizeFrame: public VM_Operation { 348 friend class Deoptimization; 349 350 private: 351 JavaThread* _thread; 352 intptr_t* _id; 353 int _reason; 354 VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason); 355 356 public: 357 VMOp_Type type() const { return VMOp_DeoptimizeFrame; } 358 void doit(); 359 bool allow_nested_vm_operations() const { return true; } 360 }; 361 362 #ifndef PRODUCT 363 class VM_DeoptimizeAll: public VM_Operation { 364 private: 365 Klass* _dependee; 366 public: 367 VM_DeoptimizeAll() {} 368 VMOp_Type type() const { return VMOp_DeoptimizeAll; } 369 void doit(); 370 bool allow_nested_vm_operations() const { return true; } 371 }; 372 373 374 class VM_ZombieAll: public VM_Operation { 375 public: 376 VM_ZombieAll() {} 377 VMOp_Type type() const { return VMOp_ZombieAll; } 378 void doit(); 379 bool allow_nested_vm_operations() const { return true; } 380 }; 381 #endif // PRODUCT 382 383 class VM_UnlinkSymbols: public VM_Operation { 384 public: 385 VM_UnlinkSymbols() {} 386 VMOp_Type type() const { return VMOp_UnlinkSymbols; } 387 void doit(); 388 bool allow_nested_vm_operations() const { return true; } 389 }; 390 391 class VM_Verify: public VM_Operation { 392 public: 393 VMOp_Type type() const { return VMOp_Verify; } 394 void doit(); 395 }; 396 397 398 class VM_PrintThreads: public VM_Operation { 399 private: 400 outputStream* _out; 401 bool _print_concurrent_locks; 402 public: 403 VM_PrintThreads() { _out = tty; _print_concurrent_locks = PrintConcurrentLocks; } 404 VM_PrintThreads(outputStream* out, bool print_concurrent_locks) { _out = out; _print_concurrent_locks = print_concurrent_locks; } 405 VMOp_Type type() const { return VMOp_PrintThreads; } 406 void doit(); 407 bool doit_prologue(); 408 void doit_epilogue(); 409 }; 410 411 class VM_PrintJNI: public VM_Operation { 412 private: 413 outputStream* _out; 414 public: 415 VM_PrintJNI() { _out = tty; } 416 VM_PrintJNI(outputStream* out) { _out = out; } 417 VMOp_Type type() const { return VMOp_PrintJNI; } 418 void doit(); 419 }; 420 421 class VM_PrintMetadata : public VM_Operation { 422 private: 423 outputStream* _out; 424 size_t _scale; 425 public: 426 VM_PrintMetadata(outputStream* out, size_t scale) : _out(out), _scale(scale) {}; 427 428 VMOp_Type type() const { return VMOp_PrintMetadata; } 429 void doit(); 430 }; 431 432 class DeadlockCycle; 433 class VM_FindDeadlocks: public VM_Operation { 434 private: 435 bool _concurrent_locks; 436 DeadlockCycle* _deadlocks; 437 outputStream* _out; 438 ThreadsListSetter _setter; // Helper to set hazard ptr in the originating thread 439 // which protects the JavaThreads in _deadlocks. 440 441 public: 442 VM_FindDeadlocks(bool concurrent_locks) : _concurrent_locks(concurrent_locks), _out(NULL), _deadlocks(NULL), _setter() {}; 443 VM_FindDeadlocks(outputStream* st) : _concurrent_locks(true), _out(st), _deadlocks(NULL) {}; 444 ~VM_FindDeadlocks(); 445 446 DeadlockCycle* result() { return _deadlocks; }; 447 VMOp_Type type() const { return VMOp_FindDeadlocks; } 448 void doit(); 449 bool doit_prologue(); 450 }; 451 452 class ThreadDumpResult; 453 class ThreadSnapshot; 454 class ThreadConcurrentLocks; 455 456 class VM_ThreadDump : public VM_Operation { 457 private: 458 ThreadDumpResult* _result; 459 int _num_threads; 460 GrowableArray<instanceHandle>* _threads; 461 int _max_depth; 462 bool _with_locked_monitors; 463 bool _with_locked_synchronizers; 464 465 ThreadSnapshot* snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl); 466 467 public: 468 VM_ThreadDump(ThreadDumpResult* result, 469 int max_depth, // -1 indicates entire stack 470 bool with_locked_monitors, 471 bool with_locked_synchronizers); 472 473 VM_ThreadDump(ThreadDumpResult* result, 474 GrowableArray<instanceHandle>* threads, 475 int num_threads, // -1 indicates entire stack 476 int max_depth, 477 bool with_locked_monitors, 478 bool with_locked_synchronizers); 479 480 VMOp_Type type() const { return VMOp_ThreadDump; } 481 void doit(); 482 bool doit_prologue(); 483 void doit_epilogue(); 484 }; 485 486 487 class VM_Exit: public VM_Operation { 488 private: 489 int _exit_code; 490 static volatile bool _vm_exited; 491 static Thread * volatile _shutdown_thread; 492 static void wait_if_vm_exited(); 493 public: 494 VM_Exit(int exit_code) { 495 _exit_code = exit_code; 496 } 497 static int wait_for_threads_in_native_to_block(); 498 static int set_vm_exited(); 499 static bool vm_exited() { return _vm_exited; } 500 static Thread * shutdown_thread() { return _shutdown_thread; } 501 static void block_if_vm_exited() { 502 if (_vm_exited) { 503 wait_if_vm_exited(); 504 } 505 } 506 VMOp_Type type() const { return VMOp_Exit; } 507 void doit(); 508 }; 509 510 class VM_PrintCompileQueue: public VM_Operation { 511 private: 512 outputStream* _out; 513 514 public: 515 VM_PrintCompileQueue(outputStream* st) : _out(st) {} 516 VMOp_Type type() const { return VMOp_PrintCompileQueue; } 517 Mode evaluation_mode() const { return _safepoint; } 518 void doit(); 519 }; 520 521 #if INCLUDE_SERVICES 522 class VM_PrintClassHierarchy: public VM_Operation { 523 private: 524 outputStream* _out; 525 bool _print_interfaces; 526 bool _print_subclasses; 527 char* _classname; 528 529 public: 530 VM_PrintClassHierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) : 531 _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses), 532 _classname(classname) {} 533 VMOp_Type type() const { return VMOp_PrintClassHierarchy; } 534 void doit(); 535 }; 536 #endif // INCLUDE_SERVICES 537 538 #endif // SHARE_VM_RUNTIME_VM_OPERATIONS_HPP