提交 2e8b8e63 编写于 作者: D dsimms

8028280: ParkEvent leak when running modified runThese which only loads classes

Summary: Use spin lock to manage ParkEvent and PlatformEvent free lists.
Reviewed-by: dholmes, fparain, dcubed, acorn
上级 ae7e40e7
......@@ -2636,9 +2636,21 @@ int os::sleep(Thread* thread, jlong millis, bool interruptible) {
}
}
int os::naked_sleep() {
// %% make the sleep time an integer flag. for now use 1 millisec.
return os::sleep(Thread::current(), 1, false);
void os::naked_short_sleep(jlong ms) {
struct timespec req;
assert(ms < 1000, "Un-interruptable sleep, short time use only");
req.tv_sec = 0;
if (ms > 0) {
req.tv_nsec = (ms % 1000) * 1000000;
}
else {
req.tv_nsec = 1;
}
nanosleep(&req, NULL);
return;
}
// Sleep forever; naked call to OS-specific sleep; use with CAUTION
......
......@@ -3871,9 +3871,33 @@ int os::sleep(Thread* thread, jlong millis, bool interruptible) {
}
}
int os::naked_sleep() {
// %% make the sleep time an integer flag. for now use 1 millisec.
return os::sleep(Thread::current(), 1, false);
//
// Short sleep, direct OS call.
//
// Note: certain versions of Linux CFS scheduler (since 2.6.23) do not guarantee
// sched_yield(2) will actually give up the CPU:
//
// * Alone on this pariticular CPU, keeps running.
// * Before the introduction of "skip_buddy" with "compat_yield" disabled
// (pre 2.6.39).
//
// So calling this with 0 is an alternative.
//
void os::naked_short_sleep(jlong ms) {
struct timespec req;
assert(ms < 1000, "Un-interruptable sleep, short time use only");
req.tv_sec = 0;
if (ms > 0) {
req.tv_nsec = (ms % 1000) * 1000000;
}
else {
req.tv_nsec = 1;
}
nanosleep(&req, NULL);
return;
}
// Sleep forever; naked call to OS-specific sleep; use with CAUTION
......
......@@ -3540,9 +3540,14 @@ int os::sleep(Thread* thread, jlong millis, bool interruptible) {
return os_sleep(millis, interruptible);
}
int os::naked_sleep() {
// %% make the sleep time an integer flag. for now use 1 millisec.
return os_sleep(1, false);
void os::naked_short_sleep(jlong ms) {
assert(ms < 1000, "Un-interruptable sleep, short time use only");
// usleep is deprecated and removed from POSIX, in favour of nanosleep, but
// Solaris requires -lrt for this.
usleep((ms * 1000));
return;
}
// Sleep forever; naked call to OS-specific sleep; use with CAUTION
......
......@@ -3496,6 +3496,16 @@ int os::sleep(Thread* thread, jlong ms, bool interruptable) {
return result;
}
//
// Short sleep, direct OS call.
//
// ms = 0, means allow others (if any) to run.
//
void os::naked_short_sleep(jlong ms) {
assert(ms < 1000, "Un-interruptable sleep, short time use only");
Sleep(ms);
}
// Sleep forever; naked call to OS-specific sleep; use with CAUTION
void os::infinite_sleep() {
while (true) { // sleep forever ...
......
......@@ -430,7 +430,10 @@ class os: AllStatic {
static intx current_thread_id();
static int current_process_id();
static int sleep(Thread* thread, jlong ms, bool interruptable);
static int naked_sleep();
// Short standalone OS sleep suitable for slow path spin loop.
// Ignores Thread.interrupt() (so keep it short).
// ms = 0, will sleep for the least amount of time allowed by the OS.
static void naked_short_sleep(jlong ms);
static void infinite_sleep(); // never returns, use with CAUTION
static void yield(); // Yields to all threads with same priority
enum YieldResult {
......
......@@ -59,58 +59,22 @@ ParkEvent * ParkEvent::Allocate (Thread * t) {
// Start by trying to recycle an existing but unassociated
// ParkEvent from the global free list.
for (;;) {
ev = FreeList ;
if (ev == NULL) break ;
// 1: Detach - sequester or privatize the list
// Tantamount to ev = Swap (&FreeList, NULL)
if (Atomic::cmpxchg_ptr (NULL, &FreeList, ev) != ev) {
continue ;
// Using a spin lock since we are part of the mutex impl.
// 8028280: using concurrent free list without memory management can leak
// pretty badly it turns out.
Thread::SpinAcquire(&ListLock, "ParkEventFreeListAllocate");
{
ev = FreeList;
if (ev != NULL) {
FreeList = ev->FreeNext;
}
// We've detached the list. The list in-hand is now
// local to this thread. This thread can operate on the
// list without risk of interference from other threads.
// 2: Extract -- pop the 1st element from the list.
ParkEvent * List = ev->FreeNext ;
if (List == NULL) break ;
for (;;) {
// 3: Try to reattach the residual list
guarantee (List != NULL, "invariant") ;
ParkEvent * Arv = (ParkEvent *) Atomic::cmpxchg_ptr (List, &FreeList, NULL) ;
if (Arv == NULL) break ;
// New nodes arrived. Try to detach the recent arrivals.
if (Atomic::cmpxchg_ptr (NULL, &FreeList, Arv) != Arv) {
continue ;
}
guarantee (Arv != NULL, "invariant") ;
// 4: Merge Arv into List
ParkEvent * Tail = List ;
while (Tail->FreeNext != NULL) Tail = Tail->FreeNext ;
Tail->FreeNext = Arv ;
}
break ;
}
Thread::SpinRelease(&ListLock);
if (ev != NULL) {
guarantee (ev->AssociatedWith == NULL, "invariant") ;
} else {
// Do this the hard way -- materialize a new ParkEvent.
// In rare cases an allocating thread might detach a long list --
// installing null into FreeList -- and then stall or be obstructed.
// A 2nd thread calling Allocate() would see FreeList == null.
// The list held privately by the 1st thread is unavailable to the 2nd thread.
// In that case the 2nd thread would have to materialize a new ParkEvent,
// even though free ParkEvents existed in the system. In this case we end up
// with more ParkEvents in circulation than we need, but the race is
// rare and the outcome is benign. Ideally, the # of extant ParkEvents
// is equal to the maximum # of threads that existed at any one time.
// Because of the race mentioned above, segments of the freelist
// can be transiently inaccessible. At worst we may end up with the
// # of ParkEvents in circulation slightly above the ideal.
// Note that if we didn't have the TSM/immortal constraint, then
// when reattaching, above, we could trim the list.
ev = new ParkEvent () ;
guarantee ((intptr_t(ev) & 0xFF) == 0, "invariant") ;
}
......@@ -124,13 +88,14 @@ void ParkEvent::Release (ParkEvent * ev) {
if (ev == NULL) return ;
guarantee (ev->FreeNext == NULL , "invariant") ;
ev->AssociatedWith = NULL ;
for (;;) {
// Push ev onto FreeList
// The mechanism is "half" lock-free.
ParkEvent * List = FreeList ;
ev->FreeNext = List ;
if (Atomic::cmpxchg_ptr (ev, &FreeList, List) == List) break ;
// Note that if we didn't have the TSM/immortal constraint, then
// when reattaching we could trim the list.
Thread::SpinAcquire(&ListLock, "ParkEventFreeListRelease");
{
ev->FreeNext = FreeList;
FreeList = ev;
}
Thread::SpinRelease(&ListLock);
}
// Override operator new and delete so we can ensure that the
......@@ -164,56 +129,21 @@ Parker * Parker::Allocate (JavaThread * t) {
// Start by trying to recycle an existing but unassociated
// Parker from the global free list.
for (;;) {
p = FreeList ;
if (p == NULL) break ;
// 1: Detach
// Tantamount to p = Swap (&FreeList, NULL)
if (Atomic::cmpxchg_ptr (NULL, &FreeList, p) != p) {
continue ;
// 8028280: using concurrent free list without memory management can leak
// pretty badly it turns out.
Thread::SpinAcquire(&ListLock, "ParkerFreeListAllocate");
{
p = FreeList;
if (p != NULL) {
FreeList = p->FreeNext;
}
// We've detached the list. The list in-hand is now
// local to this thread. This thread can operate on the
// list without risk of interference from other threads.
// 2: Extract -- pop the 1st element from the list.
Parker * List = p->FreeNext ;
if (List == NULL) break ;
for (;;) {
// 3: Try to reattach the residual list
guarantee (List != NULL, "invariant") ;
Parker * Arv = (Parker *) Atomic::cmpxchg_ptr (List, &FreeList, NULL) ;
if (Arv == NULL) break ;
// New nodes arrived. Try to detach the recent arrivals.
if (Atomic::cmpxchg_ptr (NULL, &FreeList, Arv) != Arv) {
continue ;
}
guarantee (Arv != NULL, "invariant") ;
// 4: Merge Arv into List
Parker * Tail = List ;
while (Tail->FreeNext != NULL) Tail = Tail->FreeNext ;
Tail->FreeNext = Arv ;
}
break ;
}
Thread::SpinRelease(&ListLock);
if (p != NULL) {
guarantee (p->AssociatedWith == NULL, "invariant") ;
} else {
// Do this the hard way -- materialize a new Parker..
// In rare cases an allocating thread might detach
// a long list -- installing null into FreeList --and
// then stall. Another thread calling Allocate() would see
// FreeList == null and then invoke the ctor. In this case we
// end up with more Parkers in circulation than we need, but
// the race is rare and the outcome is benign.
// Ideally, the # of extant Parkers is equal to the
// maximum # of threads that existed at any one time.
// Because of the race mentioned above, segments of the
// freelist can be transiently inaccessible. At worst
// we may end up with the # of Parkers in circulation
// slightly above the ideal.
p = new Parker() ;
}
p->AssociatedWith = t ; // Associate p with t
......@@ -227,11 +157,12 @@ void Parker::Release (Parker * p) {
guarantee (p->AssociatedWith != NULL, "invariant") ;
guarantee (p->FreeNext == NULL , "invariant") ;
p->AssociatedWith = NULL ;
for (;;) {
// Push p onto FreeList
Parker * List = FreeList ;
p->FreeNext = List ;
if (Atomic::cmpxchg_ptr (p, &FreeList, List) == List) break ;
Thread::SpinAcquire(&ListLock, "ParkerFreeListRelease");
{
p->FreeNext = FreeList;
FreeList = p;
}
Thread::SpinRelease(&ListLock);
}
......@@ -4446,9 +4446,7 @@ void Thread::SpinAcquire (volatile int * adr, const char * LockName) {
++ctr ;
if ((ctr & 0xFFF) == 0 || !os::is_MP()) {
if (Yields > 5) {
// Consider using a simple NakedSleep() instead.
// Then SpinAcquire could be called by non-JVM threads
Thread::current()->_ParkEvent->park(1) ;
os::naked_short_sleep(1);
} else {
os::NakedYield() ;
++Yields ;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册