Skip to content

Commit

Permalink
8230422: Convert uninterruptible os::sleep calls to os::naked_short_s…
Browse files Browse the repository at this point in the history
…leep

Reviewed-by: kbarrett, dcubed, shade
  • Loading branch information
David Holmes committed Sep 7, 2019
1 parent f9e0cdb commit 394d6ac
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/hotspot/cpu/x86/rdtsc_x86.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -62,7 +62,7 @@ static void do_time_measurements(volatile jlong& time_base,
fstart = os::rdtsc();

// use sleep to prevent compiler from optimizing
os::sleep(Thread::current(), FT_SLEEP_MILLISECS, true);
os::sleep(JavaThread::current(), FT_SLEEP_MILLISECS);

end = os::elapsed_counter();
OrderAccess::fence();
Expand Down
25 changes: 12 additions & 13 deletions src/hotspot/share/gc/cms/concurrentMarkSweepGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2645,7 +2645,7 @@ ConcurrentMarkSweepGeneration::expand_and_allocate(size_t word_size,
MutexLocker x(freelistLock(), Mutex::_no_safepoint_check_flag);
expand_for_gc_cause(word_size*HeapWordSize, MinHeapDeltaBytes, CMSExpansionCause::_satisfy_allocation);
if (GCExpandToAllocateDelayMillis > 0) {
os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
os::naked_sleep(GCExpandToAllocateDelayMillis);
}
return have_lock_and_allocate(word_size, tlab);
}
Expand Down Expand Up @@ -2684,7 +2684,7 @@ HeapWord* ConcurrentMarkSweepGeneration::expand_and_par_lab_allocate(CMSParGCThr
// A competing par_promote might beat us to the expansion space,
// so we may go around the loop again if promotion fails again.
if (GCExpandToAllocateDelayMillis > 0) {
os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
os::naked_sleep(GCExpandToAllocateDelayMillis);
}
}
}
Expand All @@ -2711,7 +2711,7 @@ bool ConcurrentMarkSweepGeneration::expand_and_ensure_spooling_space(
// A competing allocation might beat us to the expansion space,
// so we may go around the loop again if allocation fails again.
if (GCExpandToAllocateDelayMillis > 0) {
os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
os::naked_sleep(GCExpandToAllocateDelayMillis);
}
}
}
Expand Down Expand Up @@ -3544,7 +3544,7 @@ void CMSConcMarkingTask::coordinator_yield() {
for (unsigned i = 0; i < CMSCoordinatorYieldSleepCount &&
ConcurrentMarkSweepThread::should_yield() &&
!CMSCollector::foregroundGCIsActive(); ++i) {
os::sleep(Thread::current(), 1, false);
os::naked_short_sleep(1);
}

ConcurrentMarkSweepThread::synchronize(true);
Expand Down Expand Up @@ -5541,7 +5541,7 @@ void CMSCollector::reset_concurrent() {
for (unsigned i = 0; i < CMSYieldSleepCount &&
ConcurrentMarkSweepThread::should_yield() &&
!CMSCollector::foregroundGCIsActive(); ++i) {
os::sleep(Thread::current(), 1, false);
os::naked_short_sleep(1);
}

ConcurrentMarkSweepThread::synchronize(true);
Expand Down Expand Up @@ -5995,7 +5995,7 @@ void MarkRefsIntoAndScanClosure::do_yield_work() {
ConcurrentMarkSweepThread::should_yield() &&
!CMSCollector::foregroundGCIsActive();
++i) {
os::sleep(Thread::current(), 1, false);
os::naked_short_sleep(1);
}

ConcurrentMarkSweepThread::synchronize(true);
Expand Down Expand Up @@ -6150,7 +6150,7 @@ void ScanMarkedObjectsAgainCarefullyClosure::do_yield_work() {
for (unsigned i = 0; i < CMSYieldSleepCount &&
ConcurrentMarkSweepThread::should_yield() &&
!CMSCollector::foregroundGCIsActive(); ++i) {
os::sleep(Thread::current(), 1, false);
os::naked_short_sleep(1);
}

ConcurrentMarkSweepThread::synchronize(true);
Expand Down Expand Up @@ -6217,7 +6217,7 @@ void SurvivorSpacePrecleanClosure::do_yield_work() {
for (unsigned i = 0; i < CMSYieldSleepCount &&
ConcurrentMarkSweepThread::should_yield() &&
!CMSCollector::foregroundGCIsActive(); ++i) {
os::sleep(Thread::current(), 1, false);
os::naked_short_sleep(1);
}

ConcurrentMarkSweepThread::synchronize(true);
Expand Down Expand Up @@ -6368,7 +6368,7 @@ void MarkFromRootsClosure::do_yield_work() {
for (unsigned i = 0; i < CMSYieldSleepCount &&
ConcurrentMarkSweepThread::should_yield() &&
!CMSCollector::foregroundGCIsActive(); ++i) {
os::sleep(Thread::current(), 1, false);
os::naked_short_sleep(1);
}

ConcurrentMarkSweepThread::synchronize(true);
Expand Down Expand Up @@ -6982,7 +6982,7 @@ void CMSPrecleanRefsYieldClosure::do_yield_work() {
for (unsigned i = 0; i < CMSYieldSleepCount &&
ConcurrentMarkSweepThread::should_yield() &&
!CMSCollector::foregroundGCIsActive(); ++i) {
os::sleep(Thread::current(), 1, false);
os::naked_short_sleep(1);
}

ConcurrentMarkSweepThread::synchronize(true);
Expand Down Expand Up @@ -7547,7 +7547,7 @@ void SweepClosure::do_yield_work(HeapWord* addr) {
for (unsigned i = 0; i < CMSYieldSleepCount &&
ConcurrentMarkSweepThread::should_yield() &&
!CMSCollector::foregroundGCIsActive(); ++i) {
os::sleep(Thread::current(), 1, false);
os::naked_short_sleep(1);
}

ConcurrentMarkSweepThread::synchronize(true);
Expand Down Expand Up @@ -7845,15 +7845,14 @@ bool CMSCollector::par_take_from_overflow_list(size_t num,
}
// Grab the entire list; we'll put back a suffix
oop prefix = cast_to_oop(Atomic::xchg((oopDesc*)BUSY, &_overflow_list));
Thread* tid = Thread::current();
// Before "no_of_gc_threads" was introduced CMSOverflowSpinCount was
// set to ParallelGCThreads.
size_t CMSOverflowSpinCount = (size_t) no_of_gc_threads; // was ParallelGCThreads;
size_t sleep_time_millis = MAX2((size_t)1, num/100);
// If the list is busy, we spin for a short while,
// sleeping between attempts to get the list.
for (size_t spin = 0; prefix == BUSY && spin < CMSOverflowSpinCount; spin++) {
os::sleep(tid, sleep_time_millis, false);
os::naked_sleep(sleep_time_millis);
if (_overflow_list == NULL) {
// Nothing left to take
return false;
Expand Down
5 changes: 2 additions & 3 deletions src/hotspot/share/gc/cms/parNewGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,13 +1304,12 @@ bool ParNewGeneration::take_from_overflow_list_work(ParScanThreadState* par_scan
// Otherwise, there was something there; try claiming the list.
oop prefix = cast_to_oop(Atomic::xchg((oopDesc*)BUSY, &_overflow_list));
// Trim off a prefix of at most objsFromOverflow items
Thread* tid = Thread::current();
size_t spin_count = ParallelGCThreads;
size_t sleep_time_millis = MAX2((size_t)1, objsFromOverflow/100);
for (size_t spin = 0; prefix == BUSY && spin < spin_count; spin++) {
// someone grabbed it before we did ...
// ... we spin for a short while...
os::sleep(tid, sleep_time_millis, false);
// ... we spin/block for a short while...
os::naked_sleep(sleep_time_millis);
if (_overflow_list == NULL) {
// nothing left to take
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/gc/parallel/psCardTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void PSCardTable::scavenge_contents_parallel(ObjectStartArray* start_array,
// Delay 1 worker so that it proceeds after all the work
// has been completed.
if (stripe_number < 2) {
os::sleep(Thread::current(), GCWorkerDelayMillis, false);
os::naked_sleep(GCWorkerDelayMillis);
}
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/hotspot/share/gc/parallel/psOldGen.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -225,15 +225,15 @@ HeapWord* PSOldGen::allocate(size_t word_size) {
HeapWord* PSOldGen::expand_and_allocate(size_t word_size) {
expand(word_size*HeapWordSize);
if (GCExpandToAllocateDelayMillis > 0) {
os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
os::naked_sleep(GCExpandToAllocateDelayMillis);
}
return allocate_noexpand(word_size);
}

HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) {
expand(word_size*HeapWordSize);
if (GCExpandToAllocateDelayMillis > 0) {
os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
os::naked_sleep(GCExpandToAllocateDelayMillis);
}
return cas_allocate_noexpand(word_size);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ inline oop PSPromotionManager::copy_to_survivor_space(oop o) {
// Delay the initialization of the promotion lab (plab).
// This exposes uninitialized plabs to card table processing.
if (GCWorkerDelayMillis > 0) {
os::sleep(Thread::current(), GCWorkerDelayMillis, false);
os::naked_sleep(GCWorkerDelayMillis);
}
#endif
_old_lab.initialize(MemRegion(lab_base, OldPLABSize));
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/gc/serial/tenuredGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ TenuredGeneration::expand_and_allocate(size_t word_size,
while (true) {
expand(byte_size, _min_heap_delta_bytes);
if (GCExpandToAllocateDelayMillis > 0) {
os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
os::naked_sleep(GCExpandToAllocateDelayMillis);
}
result = _the_space->par_allocate(word_size);
if ( result != NULL) {
Expand Down
3 changes: 1 addition & 2 deletions src/hotspot/share/gc/shared/taskqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void ParallelTaskTerminator::yield() {

void ParallelTaskTerminator::sleep(uint millis) {
assert(_offered_termination <= _n_threads, "Invariant");
os::sleep(Thread::current(), millis, false);
os::naked_sleep(millis);
}

bool
Expand Down Expand Up @@ -280,4 +280,3 @@ TaskTerminator::~TaskTerminator() {
delete _terminator;
}
}

2 changes: 1 addition & 1 deletion src/hotspot/share/gc/shenandoah/shenandoahPacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void ShenandoahPacer::pace_for_alloc(size_t words) {
}
cur = MAX2<size_t>(1, cur);

os::sleep(Thread::current(), cur, true);
os::sleep(JavaThread::current(), cur);

double end = os::elapsedTime();
total = (size_t)((end - start) * 1000);
Expand Down
3 changes: 2 additions & 1 deletion src/hotspot/share/jvmci/jvmciCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void JVMCICompiler::initialize() {
}

void JVMCICompiler::bootstrap(TRAPS) {
assert(THREAD->is_Java_thread(), "must be");
if (Arguments::mode() == Arguments::_int) {
// Nothing to do in -Xint mode
return;
Expand Down Expand Up @@ -80,7 +81,7 @@ void JVMCICompiler::bootstrap(TRAPS) {
do {
// Loop until there is something in the queue.
do {
os::sleep(THREAD, 100, true);
os::sleep((JavaThread*)THREAD, 100);
qsize = CompileBroker::queue_size(CompLevel_full_optimization);
} while (!_bootstrap_compilation_request_handled && first_round && qsize == 0);
first_round = false;
Expand Down
6 changes: 2 additions & 4 deletions src/hotspot/share/jvmci/jvmciRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,10 +941,8 @@ void JVMCIRuntime::exit_on_pending_exception(JVMCIEnv* JVMCIENV, const char* mes
describe_pending_hotspot_exception(THREAD, true);
}
} else {
// Allow error reporting thread to print the stack trace. Windows
// doesn't allow uninterruptible wait for JavaThreads
const bool interruptible = true;
os::sleep(THREAD, 200, interruptible);
// Allow error reporting thread to print the stack trace.
os::sleep(THREAD, 200);
}

before_exit(THREAD);
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/prims/jvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2978,7 +2978,7 @@ JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
} else {
ThreadState old_state = thread->osthread()->get_state();
thread->osthread()->set_state(SLEEPING);
if (os::sleep(thread, millis, true) == OS_INTRPT) {
if (os::sleep(thread, millis) == OS_INTRPT) {
// An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
// us while we were sleeping. We do not overwrite those.
if (!HAS_PENDING_EXCEPTION) {
Expand Down
99 changes: 41 additions & 58 deletions src/hotspot/share/runtime/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,19 @@ os::SuspendResume::State os::SuspendResume::switch_state(os::SuspendResume::Stat
}
#endif

int os::sleep(Thread* thread, jlong millis, bool interruptible) {
// Convenience wrapper around naked_short_sleep to allow for longer sleep
// times. Only for use by non-JavaThreads.
void os::naked_sleep(jlong millis) {
assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
const jlong limit = 999;
while (millis > limit) {
naked_short_sleep(limit);
millis -= limit;
}
naked_short_sleep(millis);
}

int os::sleep(JavaThread* thread, jlong millis) {
assert(thread == Thread::current(), "thread consistency check");

ParkEvent * const slp = thread->_SleepEvent;
Expand All @@ -1850,72 +1862,43 @@ int os::sleep(Thread* thread, jlong millis, bool interruptible) {
// interrupt state.
OrderAccess::fence();

if (interruptible) {
jlong prevtime = javaTimeNanos();

assert(thread->is_Java_thread(), "sanity check");
JavaThread *jt = (JavaThread *) thread;

for (;;) {
// interruption has precedence over timing out
if (os::is_interrupted(thread, true)) {
return OS_INTRPT;
}
jlong prevtime = javaTimeNanos();

jlong newtime = javaTimeNanos();

if (newtime - prevtime < 0) {
// time moving backwards, should only happen if no monotonic clock
// not a guarantee() because JVM should not abort on kernel/glibc bugs
assert(!os::supports_monotonic_clock(),
"unexpected time moving backwards detected in os::sleep(interruptible)");
} else {
millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
}

if (millis <= 0) {
return OS_OK;
}
for (;;) {
// interruption has precedence over timing out
if (os::is_interrupted(thread, true)) {
return OS_INTRPT;
}

prevtime = newtime;
jlong newtime = javaTimeNanos();

{
ThreadBlockInVM tbivm(jt);
OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
if (newtime - prevtime < 0) {
// time moving backwards, should only happen if no monotonic clock
// not a guarantee() because JVM should not abort on kernel/glibc bugs
assert(!os::supports_monotonic_clock(),
"unexpected time moving backwards detected in os::sleep()");
} else {
millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
}

jt->set_suspend_equivalent();
// cleared by handle_special_suspend_equivalent_condition() or
// java_suspend_self() via check_and_wait_while_suspended()
if (millis <= 0) {
return OS_OK;
}

slp->park(millis);
prevtime = newtime;

// were we externally suspended while we were waiting?
jt->check_and_wait_while_suspended();
}
}
} else {
OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
jlong prevtime = javaTimeNanos();

for (;;) {
// It'd be nice to avoid the back-to-back javaTimeNanos() calls on
// the 1st iteration ...
jlong newtime = javaTimeNanos();

if (newtime - prevtime < 0) {
// time moving backwards, should only happen if no monotonic clock
// not a guarantee() because JVM should not abort on kernel/glibc bugs
assert(!os::supports_monotonic_clock(),
"unexpected time moving backwards detected on os::sleep(!interruptible)");
} else {
millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
}
{
ThreadBlockInVM tbivm(thread);
OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);

if (millis <= 0) break ;
thread->set_suspend_equivalent();
// cleared by handle_special_suspend_equivalent_condition() or
// java_suspend_self() via check_and_wait_while_suspended()

prevtime = newtime;
slp->park(millis);

// were we externally suspended while we were waiting?
thread->check_and_wait_while_suspended();
}
return OS_OK ;
}
}
Loading

0 comments on commit 394d6ac

Please sign in to comment.