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