Skip to content

Commit

Permalink
Bug 1343 - optimize getStackTrace call in ConcurrencyManager to fix …
Browse files Browse the repository at this point in the history
…throughput degradation (#1346)

Signed-off-by: Joshua Dettinger <joshuad@us.ibm.com>
Signed-off-by: Will Dazey <dazeydev.3@gmail.com>
  • Loading branch information
dazey3 committed Oct 26, 2021
1 parent 8a7e963 commit 0c1a376
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -81,6 +82,11 @@ public class ConcurrencyManager implements Serializable {
private static final Set<Thread> THREADS_WAITING_TO_RELEASE_DEFERRED_LOCKS = new HashSet<>();
private static final Map<Thread, String> THREADS_WAITING_TO_RELEASE_DEFERRED_LOCKS_BUILD_OBJECT_COMPLETE_GOES_NOWHERE = new ConcurrentHashMap<>();

private static final String ACQUIRE_METHOD_NAME = ConcurrencyManager.class.getName() + ".acquire(...)";
private static final String ACQUIRE_READ_LOCK_METHOD_NAME = ConcurrencyManager.class.getName() + ".acquireReadLock(...)";
private static final String ACQUIRE_WITH_WAIT_METHOD_NAME = ConcurrencyManager.class.getName() + ".acquireWithWait(...)";
private static final String ACQUIRE_DEFERRED_LOCK_METHOD_NAME = ConcurrencyManager.class.getName() + ".acquireDeferredLock(...)";

/**
* Initialize the newly allocated instance of this class.
* Set the depth to zero.
Expand Down Expand Up @@ -118,8 +124,7 @@ public synchronized void acquire(boolean forMerge) throws ConcurrencyException {
// is just storing debug metadata that we can use when we detect the system is frozen in a dead lock
final boolean currentThreadWillEnterTheWhileWait = ((this.activeThread != null) || (this.numberOfReaders.get() > 0)) && (this.activeThread != currentThread);
if(currentThreadWillEnterTheWhileWait) {
StackTraceElement stackTraceElement = currentThread.getStackTrace()[1];
putThreadAsWaitingToAcquireLockForWriting(currentThread, stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "(...)");
putThreadAsWaitingToAcquireLockForWriting(currentThread, ACQUIRE_METHOD_NAME);
}
while (((this.activeThread != null) || (this.numberOfReaders.get() > 0)) && (this.activeThread != Thread.currentThread())) {
// This must be in a while as multiple threads may be released, or another thread may rush the acquire after one is released.
Expand Down Expand Up @@ -197,8 +202,7 @@ public synchronized boolean acquireWithWait(boolean forMerge, int wait) throws C
return true;
} else {
try {
StackTraceElement stackTraceElement = currentThread.getStackTrace()[1];
putThreadAsWaitingToAcquireLockForWriting(currentThread, stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "(...)");
putThreadAsWaitingToAcquireLockForWriting(currentThread, ACQUIRE_WITH_WAIT_METHOD_NAME);
wait(wait);
} catch (InterruptedException e) {
return false;
Expand Down Expand Up @@ -246,8 +250,7 @@ public void acquireDeferredLock() throws ConcurrencyException {
final long whileStartTimeMillis = System.currentTimeMillis();
final boolean currentThreadWillEnterTheWhileWait = this.numberOfReaders.get() != 0;
if(currentThreadWillEnterTheWhileWait) {
StackTraceElement stackTraceElement = currentThread.getStackTrace()[1];
putThreadAsWaitingToAcquireLockForWriting(currentThread, stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "(...)");
putThreadAsWaitingToAcquireLockForWriting(currentThread, ACQUIRE_DEFERRED_LOCK_METHOD_NAME);
}
while (this.numberOfReaders.get() != 0) {
// There are readers of this object, wait until they are done before determining if
Expand Down Expand Up @@ -323,8 +326,7 @@ public synchronized void acquireReadLock() throws ConcurrencyException {
ReadLockManager readLockManager = getReadLockManager(currentThread);
final boolean currentThreadWillEnterTheWhileWait = (this.activeThread != null) && (this.activeThread != currentThread);
if (currentThreadWillEnterTheWhileWait) {
StackTraceElement stackTraceElement = currentThread.getStackTrace()[1];
putThreadAsWaitingToAcquireLockForReading(currentThread, stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "(...)");
putThreadAsWaitingToAcquireLockForReading(currentThread, ACQUIRE_READ_LOCK_METHOD_NAME);
}
// Cannot check for starving writers as will lead to deadlocks.
while ((this.activeThread != null) && (this.activeThread != Thread.currentThread())) {
Expand Down

0 comments on commit 0c1a376

Please sign in to comment.