Add proper synchronization when access metrics.

Motivation:

We also need to add synchronization when access fields to ensure we see the latest updates.

Modifications:

Add synchronization when read fields that are written concurrently.

Result:

Ensure correct visibility of updated.
This commit is contained in:
Norman Maurer 2016-03-14 09:09:24 +01:00
parent 8dbf5d02e5
commit daa4efcfef

View File

@ -442,7 +442,11 @@ abstract class PoolArena<T> implements PoolArenaMetric {
@Override @Override
public long numAllocations() { public long numAllocations() {
return allocationsTiny.value() + allocationsSmall.value() + allocationsNormal + allocationsHuge.value(); final long allocsNormal;
synchronized (this) {
allocsNormal = allocationsNormal;
}
return allocationsTiny.value() + allocationsSmall.value() + allocsNormal + allocationsHuge.value();
} }
@Override @Override
@ -456,27 +460,31 @@ abstract class PoolArena<T> implements PoolArenaMetric {
} }
@Override @Override
public long numNormalAllocations() { public synchronized long numNormalAllocations() {
return allocationsNormal; return allocationsNormal;
} }
@Override @Override
public long numDeallocations() { public long numDeallocations() {
return deallocationsTiny + deallocationsSmall + allocationsNormal + deallocationsHuge.value(); final long deallocs;
synchronized (this) {
deallocs = deallocationsTiny + deallocationsSmall + allocationsNormal;
}
return deallocs + deallocationsHuge.value();
} }
@Override @Override
public long numTinyDeallocations() { public synchronized long numTinyDeallocations() {
return deallocationsTiny; return deallocationsTiny;
} }
@Override @Override
public long numSmallDeallocations() { public synchronized long numSmallDeallocations() {
return deallocationsSmall; return deallocationsSmall;
} }
@Override @Override
public long numNormalDeallocations() { public synchronized long numNormalDeallocations() {
return deallocationsNormal; return deallocationsNormal;
} }
@ -492,7 +500,11 @@ abstract class PoolArena<T> implements PoolArenaMetric {
@Override @Override
public long numActiveAllocations() { public long numActiveAllocations() {
long val = numAllocations() - numDeallocations(); long val;
synchronized (this) {
val = allocationsNormal - deallocationsTiny - deallocationsSmall - allocationsNormal;
}
val -= deallocationsHuge.value();
return val >= 0 ? val : 0; return val >= 0 ? val : 0;
} }
@ -510,7 +522,10 @@ abstract class PoolArena<T> implements PoolArenaMetric {
@Override @Override
public long numActiveNormalAllocations() { public long numActiveNormalAllocations() {
long val = numNormalAllocations() - numNormalDeallocations(); final long val;
synchronized (this) {
val = allocationsNormal - deallocationsNormal;
}
return val >= 0 ? val : 0; return val >= 0 ? val : 0;
} }