21 * questions.
22 *
23 */
24
25 #include "compiler/compileBroker.hpp"
26 #include "gc/shared/collectedHeap.hpp"
27 #include "gc/shared/vmThreadCpuTimeScope.inline.hpp"
28 #include "jfr/jfrEvents.hpp"
29 #include "jfr/support/jfrThreadId.hpp"
30 #include "logging/log.hpp"
31 #include "logging/logConfiguration.hpp"
32 #include "logging/logStream.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "memory/universe.hpp"
35 #include "oops/oop.inline.hpp"
36 #include "oops/verifyOopClosure.hpp"
37 #include "runtime/atomic.hpp"
38 #include "runtime/cpuTimeCounters.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/interfaceSupport.inline.hpp"
41 #include "runtime/javaThread.inline.hpp"
42 #include "runtime/jniHandles.hpp"
43 #include "runtime/mutexLocker.hpp"
44 #include "runtime/os.hpp"
45 #include "runtime/perfData.hpp"
46 #include "runtime/safepoint.hpp"
47 #include "runtime/synchronizer.hpp"
48 #include "runtime/timerTrace.hpp"
49 #include "runtime/vmOperations.hpp"
50 #include "runtime/vmThread.hpp"
51 #include "utilities/dtrace.hpp"
52 #include "utilities/events.hpp"
53 #include "utilities/vmError.hpp"
54
55
56 //------------------------------------------------------------------------------------------------------------------
57 // Timeout machinery
58
59 void VMOperationTimeoutTask::task() {
60 assert(AbortVMOnVMOperationTimeout, "only if enabled");
61 if (is_armed()) {
62 jlong delay = nanos_to_millis(os::javaTimeNanos() - _arm_time);
63 if (delay > AbortVMOnVMOperationTimeoutDelay) {
64 fatal("%s VM operation took too long: " JLONG_FORMAT " ms elapsed since VM-op start (timeout: %zd ms)",
65 _vm_op_name, delay, AbortVMOnVMOperationTimeoutDelay);
66 }
67 }
68 }
69
70 bool VMOperationTimeoutTask::is_armed() {
257 }
258 }
259 }
260
261 static void post_vm_operation_event(EventExecuteVMOperation* event, VM_Operation* op) {
262 assert(event != nullptr, "invariant");
263 assert(op != nullptr, "invariant");
264 const bool evaluate_at_safepoint = op->evaluate_at_safepoint();
265 event->set_operation(op->type());
266 event->set_safepoint(evaluate_at_safepoint);
267 event->set_blocking(true);
268 event->set_caller(JFR_THREAD_ID(op->calling_thread()));
269 event->set_safepointId(evaluate_at_safepoint ? SafepointSynchronize::safepoint_id() : 0);
270 event->commit();
271 }
272
273 void VMThread::evaluate_operation(VM_Operation* op) {
274 ResourceMark rm;
275
276 {
277 PerfTraceTime vm_op_timer(perf_accumulated_vm_operation_time());
278 HOTSPOT_VMOPS_BEGIN(
279 (char*) op->name(), strlen(op->name()),
280 op->evaluate_at_safepoint() ? 0 : 1);
281
282 EventExecuteVMOperation event;
283 VMThreadCPUTimeScope CPUTimeScope(this, op->is_gc_operation());
284 op->evaluate();
285 if (event.should_commit()) {
286 post_vm_operation_event(&event, op);
287 }
288
289 HOTSPOT_VMOPS_END(
290 (char*) op->name(), strlen(op->name()),
291 op->evaluate_at_safepoint() ? 0 : 1);
292 }
293 }
294
295 class ALotOfHandshakeClosure : public HandshakeClosure {
296 public:
297 ALotOfHandshakeClosure() : HandshakeClosure("ALotOfHandshakeClosure") {}
499 public:
500 #ifdef ASSERT
501 SkipGCALot(Thread* t) : _t(t) {
502 _saved = _t->skip_gcalot();
503 _t->set_skip_gcalot(true);
504 }
505
506 ~SkipGCALot() {
507 assert(_t->skip_gcalot(), "Save-restore protocol invariant");
508 _t->set_skip_gcalot(_saved);
509 }
510 #else
511 SkipGCALot(Thread* t) { }
512 ~SkipGCALot() { }
513 #endif
514 };
515
516 void VMThread::execute(VM_Operation* op) {
517 Thread* t = Thread::current();
518
519 if (t->is_VM_thread()) {
520 op->set_calling_thread(t);
521 ((VMThread*)t)->inner_execute(op);
522 return;
523 }
524
525 // The current thread must not belong to the SuspendibleThreadSet, because an
526 // on-the-fly safepoint can be waiting for the current thread, and the
527 // current thread will be blocked in wait_until_executed, resulting in
528 // deadlock.
529 assert(!t->is_suspendible_thread(), "precondition");
530 assert(!t->is_indirectly_suspendible_thread(), "precondition");
531
532 // Avoid re-entrant attempts to gc-a-lot
533 SkipGCALot sgcalot(t);
534
535 // JavaThread or WatcherThread
536 if (t->is_Java_thread()) {
537 JavaThread::cast(t)->check_for_valid_safepoint_state();
538 }
539
540 // New request from Java thread, evaluate prologue
541 if (!op->doit_prologue()) {
542 return; // op was cancelled
543 }
544
545 op->set_calling_thread(t);
546
547 wait_until_executed(op);
548
549 op->doit_epilogue();
550 }
551
552 void VMThread::verify() {
553 oops_do(&VerifyOopClosure::verify_oop, nullptr);
554 }
|
21 * questions.
22 *
23 */
24
25 #include "compiler/compileBroker.hpp"
26 #include "gc/shared/collectedHeap.hpp"
27 #include "gc/shared/vmThreadCpuTimeScope.inline.hpp"
28 #include "jfr/jfrEvents.hpp"
29 #include "jfr/support/jfrThreadId.hpp"
30 #include "logging/log.hpp"
31 #include "logging/logConfiguration.hpp"
32 #include "logging/logStream.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "memory/universe.hpp"
35 #include "oops/oop.inline.hpp"
36 #include "oops/verifyOopClosure.hpp"
37 #include "runtime/atomic.hpp"
38 #include "runtime/cpuTimeCounters.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/interfaceSupport.inline.hpp"
41 #include "runtime/java.hpp"
42 #include "runtime/javaThread.inline.hpp"
43 #include "runtime/jniHandles.hpp"
44 #include "runtime/mutexLocker.hpp"
45 #include "runtime/os.hpp"
46 #include "runtime/perfData.inline.hpp"
47 #include "runtime/safepoint.hpp"
48 #include "runtime/synchronizer.hpp"
49 #include "runtime/timerTrace.hpp"
50 #include "runtime/vmOperation.hpp"
51 #include "runtime/vmOperations.hpp"
52 #include "runtime/vmThread.hpp"
53 #include "services/management.hpp"
54 #include "utilities/dtrace.hpp"
55 #include "utilities/events.hpp"
56 #include "utilities/vmError.hpp"
57
58
59 //------------------------------------------------------------------------------------------------------------------
60 // Timeout machinery
61
62 void VMOperationTimeoutTask::task() {
63 assert(AbortVMOnVMOperationTimeout, "only if enabled");
64 if (is_armed()) {
65 jlong delay = nanos_to_millis(os::javaTimeNanos() - _arm_time);
66 if (delay > AbortVMOnVMOperationTimeoutDelay) {
67 fatal("%s VM operation took too long: " JLONG_FORMAT " ms elapsed since VM-op start (timeout: %zd ms)",
68 _vm_op_name, delay, AbortVMOnVMOperationTimeoutDelay);
69 }
70 }
71 }
72
73 bool VMOperationTimeoutTask::is_armed() {
260 }
261 }
262 }
263
264 static void post_vm_operation_event(EventExecuteVMOperation* event, VM_Operation* op) {
265 assert(event != nullptr, "invariant");
266 assert(op != nullptr, "invariant");
267 const bool evaluate_at_safepoint = op->evaluate_at_safepoint();
268 event->set_operation(op->type());
269 event->set_safepoint(evaluate_at_safepoint);
270 event->set_blocking(true);
271 event->set_caller(JFR_THREAD_ID(op->calling_thread()));
272 event->set_safepointId(evaluate_at_safepoint ? SafepointSynchronize::safepoint_id() : 0);
273 event->commit();
274 }
275
276 void VMThread::evaluate_operation(VM_Operation* op) {
277 ResourceMark rm;
278
279 {
280 PerfTraceElapsedTime vm_op_timer(perf_accumulated_vm_operation_time());
281 HOTSPOT_VMOPS_BEGIN(
282 (char*) op->name(), strlen(op->name()),
283 op->evaluate_at_safepoint() ? 0 : 1);
284
285 EventExecuteVMOperation event;
286 VMThreadCPUTimeScope CPUTimeScope(this, op->is_gc_operation());
287 op->evaluate();
288 if (event.should_commit()) {
289 post_vm_operation_event(&event, op);
290 }
291
292 HOTSPOT_VMOPS_END(
293 (char*) op->name(), strlen(op->name()),
294 op->evaluate_at_safepoint() ? 0 : 1);
295 }
296 }
297
298 class ALotOfHandshakeClosure : public HandshakeClosure {
299 public:
300 ALotOfHandshakeClosure() : HandshakeClosure("ALotOfHandshakeClosure") {}
502 public:
503 #ifdef ASSERT
504 SkipGCALot(Thread* t) : _t(t) {
505 _saved = _t->skip_gcalot();
506 _t->set_skip_gcalot(true);
507 }
508
509 ~SkipGCALot() {
510 assert(_t->skip_gcalot(), "Save-restore protocol invariant");
511 _t->set_skip_gcalot(_saved);
512 }
513 #else
514 SkipGCALot(Thread* t) { }
515 ~SkipGCALot() { }
516 #endif
517 };
518
519 void VMThread::execute(VM_Operation* op) {
520 Thread* t = Thread::current();
521
522 PerfTraceTimedEvent p(VMThread::get_perf_timer_for(op), VMThread::get_perf_counter_for(op), \
523 Thread::current()->profile_vm_ops()); \
524
525 if (t->is_VM_thread()) {
526 op->set_calling_thread(t);
527 ((VMThread*)t)->inner_execute(op);
528 return;
529 }
530
531 // The current thread must not belong to the SuspendibleThreadSet, because an
532 // on-the-fly safepoint can be waiting for the current thread, and the
533 // current thread will be blocked in wait_until_executed, resulting in
534 // deadlock.
535 assert(!t->is_suspendible_thread(), "precondition");
536 assert(!t->is_indirectly_suspendible_thread(), "precondition");
537
538 // Avoid re-entrant attempts to gc-a-lot
539 SkipGCALot sgcalot(t);
540
541 // JavaThread or WatcherThread
542 if (t->is_Java_thread()) {
543 JavaThread::cast(t)->check_for_valid_safepoint_state();
544 }
545
546 // New request from Java thread, evaluate prologue
547 if (!op->doit_prologue()) {
548 return; // op was cancelled
549 }
550
551 op->set_calling_thread(t);
552
553 wait_until_executed(op);
554
555 op->doit_epilogue();
556 }
557
558 void VMThread::verify() {
559 oops_do(&VerifyOopClosure::verify_oop, nullptr);
560 }
561
562 #define DECLARE_COUNTER(name) \
563 PerfTickCounters* _perf_##name##_timer = nullptr; \
564 PerfCounter* _perf_##name##_count = nullptr;
565
566 VM_OPS_DO(DECLARE_COUNTER)
567
568 #undef DECLARE_COUNTER
569
570 #define SWITCH_TIMER(name) \
571 case VM_Operation::VMOp_##name: return _perf_##name##_timer;
572
573 PerfTickCounters* VMThread::get_perf_timer_for(VM_Operation *op) {
574 switch(op->type()) {
575 VM_OPS_DO(SWITCH_TIMER)
576 default: ShouldNotReachHere();
577 }
578 }
579
580 #undef SWITCH_TIMER
581
582 #define SWITCH_COUNT(name) \
583 case VM_Operation::VMOp_##name: return _perf_##name##_count;
584
585 PerfCounter* VMThread::get_perf_counter_for(VM_Operation *op) {
586 switch(op->type()) {
587 VM_OPS_DO(SWITCH_COUNT)
588 default: ShouldNotReachHere();
589 }
590 }
591
592 #undef SWITCH_COUNT
593
594 #define INIT_COUNTER(name) \
595 NEWPERFTICKCOUNTERS(_perf_##name##_timer, SUN_RT, #name "_time"); \
596 NEWPERFEVENTCOUNTER(_perf_##name##_count, SUN_RT, #name "_count"); \
597
598 void VMThread::init_counters() {
599 if (ProfileVMOps && UsePerfData) {
600 EXCEPTION_MARK;
601
602 VM_OPS_DO(INIT_COUNTER)
603
604 if (HAS_PENDING_EXCEPTION) {
605 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
606 }
607 }
608 }
609
610 #undef INIT_COUNTER
611
612 static jlong total_count() {
613 jlong total = 0;
614 #define ACC_COUNT(name) total += _perf_##name##_count->get_value();
615 VM_OPS_DO(ACC_COUNT)
616 #undef ACC_COUNT
617 return total;
618 }
619
620 static jlong total_elapsed_time_in_ms() {
621 jlong total_elapsed_ticks = 0;
622 #define ACC_COUNT(name) total_elapsed_ticks += _perf_##name##_timer->elapsed_counter_value();
623 VM_OPS_DO(ACC_COUNT)
624 #undef ACC_COUNT
625 return Management::ticks_to_ms(total_elapsed_ticks);
626 }
627
628 #define PRINT_COUNTER(name) {\
629 jlong count = _perf_##name##_count->get_value(); \
630 if (count > 0) { \
631 st->print_cr(" %-40s = " JLONG_FORMAT_W(6) "us (" JLONG_FORMAT_W(5) " events)", #name, _perf_##name##_timer->elapsed_counter_value_us(), count); \
632 }}
633
634 void VMThread::print_counters_on(outputStream* st) {
635 if (ProfileVMOps && UsePerfData) {
636 st->print_cr("VMOperation: Total: " JLONG_FORMAT " events (elapsed " JLONG_FORMAT "ms) for thread \"main\":",
637 total_count(), total_elapsed_time_in_ms());
638 VM_OPS_DO(PRINT_COUNTER)
639 } else {
640 st->print_cr(" VMOperations: no info (%s is disabled)", (UsePerfData ? "ProfileVMCalls" : "UsePerfData"));
641 }
642 }
643
644 #undef PRINT_COUNTER
|