Skip to content

Commit

Permalink
Add eviction metrics in binary allocator (#14432) (#14469)
Browse files Browse the repository at this point in the history
* add eviction metrics

* fix

* fix overflow

---------

Signed-off-by: OneSizeFitQuorum <[email protected]>
Co-authored-by: OneSizeFitQuorum <[email protected]>
  • Loading branch information
MrQuansy and OneSizeFitsQuorum authored Dec 17, 2024
1 parent c01baa0 commit a966a9f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ public BinaryAllocatorMetrics getMetrics() {
return metrics;
}

private void evict(double ratio) {
private long evict(double ratio) {
long evictedSize = 0;
for (Arena arena : heapArenas) {
arena.evict(ratio);
evictedSize += arena.evict(ratio);
}
return evictedSize;
}

public static BinaryAllocator getInstance() {
Expand Down Expand Up @@ -231,17 +233,19 @@ public void runGcEviction(long curGcTimePercent) {
return;
}

long evictedSize = 0;
if (curGcTimePercent > SHUTDOWN_GC_TIME_PERCENTAGE) {
LOGGER.info(
"Binary allocator is shutting down because of high GC time percentage {}%.",
curGcTimePercent);
evict(1.0);
evictedSize = evict(1.0);
close(false);
} else if (curGcTimePercent > HALF_GC_TIME_PERCENTAGE) {
evict(0.5);
evictedSize = evict(0.5);
} else if (curGcTimePercent > WARNING_GC_TIME_PERCENTAGE) {
evict(0.2);
evictedSize = evict(0.2);
}
metrics.updateGcEvictionCounter(evictedSize);
}

public class SampleEvictor extends Evictor {
Expand All @@ -252,9 +256,11 @@ public SampleEvictor(String name, Duration evictorShutdownTimeoutDuration) {

@Override
public void run() {
long evictedSize = 0;
for (Arena arena : heapArenas) {
arena.runSampleEviction();
evictedSize += arena.runSampleEviction();
}
metrics.updateSampleEvictionCounter(evictedSize);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ public void deallocate(byte[] bytes) {
regions[sizeIdx].deallocate(bytes);
}

public void evict(double ratio) {
public long evict(double ratio) {
long evictedSize = 0;
for (SlabRegion region : regions) {
region.evict(ratio);
evictedSize += region.evict(ratio);
}
return evictedSize;
}

public void close() {
Expand Down Expand Up @@ -109,33 +111,39 @@ public void decRegisteredThread() {
this.numRegisteredThread.decrementAndGet();
}

public void runSampleEviction() {
public long runSampleEviction() {
// update metric
int allocateFromSlabDelta = 0;
int allocateFromJVMDelta = 0;
long allocateFromSlabDelta = 0;
long allocateFromJVMDelta = 0;
for (SlabRegion region : regions) {
allocateFromSlabDelta +=
region.byteArraySize * (region.allocationsFromAllocator.get() - region.prevAllocations);
(long) region.byteArraySize
* (region.allocationsFromAllocator.get() - region.prevAllocations);
region.prevAllocations = region.allocationsFromAllocator.get();
allocateFromJVMDelta +=
region.byteArraySize * (region.allocationsFromJVM.get() - region.prevAllocationsFromJVM);
(long) region.byteArraySize
* (region.allocationsFromJVM.get() - region.prevAllocationsFromJVM);
region.prevAllocationsFromJVM = region.allocationsFromJVM.get();
}
binaryAllocator.getMetrics().updateCounter(allocateFromSlabDelta, allocateFromJVMDelta);
binaryAllocator
.getMetrics()
.updateAllocationCounter(allocateFromSlabDelta, allocateFromJVMDelta);

// Start sampling
for (SlabRegion region : regions) {
region.updateSample();
}

sampleCount++;
long evictedSize = 0;
if (sampleCount == EVICT_SAMPLE_COUNT) {
// Evict
for (SlabRegion region : regions) {
region.resize();
evictedSize += region.resize();
}
sampleCount = 0;
}
return evictedSize;
}

private static class SlabRegion {
Expand Down Expand Up @@ -182,22 +190,25 @@ private void updateSample() {
average.sample(getActiveSize());
}

private void resize() {
private long resize() {
average.update();
int needRemain = (int) Math.ceil(average.average()) - getActiveSize();
evict(getQueueSize() - needRemain);
return evict(getQueueSize() - needRemain);
}

private void evict(double ratio) {
evict((int) (getQueueSize() * ratio));
private long evict(double ratio) {
return evict((int) (getQueueSize() * ratio));
}

private void evict(int num) {
private long evict(int num) {
long evicted = 0;
while (num > 0 && !queue.isEmpty()) {
queue.poll();
evictions.incrementAndGet();
num--;
evicted += byteArraySize;
}
return evicted;
}

private long getTotalUsedMemory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public class BinaryAllocatorMetrics implements IMetricSet {
private static final String ALLOCATE_FROM_SLAB = "allocate-from-slab";
private static final String ALLOCATE_FROM_JVM = "allocate-from-jvm";
private static final String ACTIVE_MEMORY = "active-memory";
private static final String EVICTED_BY_SAMPLE_EVICTION = "evicted-by-sample-eviction";
private static final String EVICTED_BY_GC_EVICTION = "evicted-by-gc-eviction";

private final BinaryAllocator binaryAllocator;
private Counter allocateFromSlab;
private Counter allocateFromJVM;
private Counter evictedBySampleEviction;
private Counter evictedByGcEviction;

public BinaryAllocatorMetrics(final BinaryAllocator binaryAllocator) {
this.binaryAllocator = binaryAllocator;
Expand Down Expand Up @@ -71,6 +75,18 @@ public void bindTo(AbstractMetricService metricService) {
MetricLevel.IMPORTANT,
Tag.NAME.toString(),
ALLOCATE_FROM_JVM);
evictedBySampleEviction =
metricService.getOrCreateCounter(
Metric.BINARY_ALLOCATOR.toString(),
MetricLevel.IMPORTANT,
Tag.NAME.toString(),
EVICTED_BY_SAMPLE_EVICTION);
evictedByGcEviction =
metricService.getOrCreateCounter(
Metric.BINARY_ALLOCATOR.toString(),
MetricLevel.IMPORTANT,
Tag.NAME.toString(),
EVICTED_BY_GC_EVICTION);
}

@Override
Expand All @@ -95,10 +111,28 @@ public void unbindFrom(AbstractMetricService metricService) {
Metric.BINARY_ALLOCATOR.toString(),
Tag.NAME.toString(),
ALLOCATE_FROM_JVM);
metricService.remove(
MetricType.COUNTER,
Metric.BINARY_ALLOCATOR.toString(),
Tag.NAME.toString(),
EVICTED_BY_SAMPLE_EVICTION);
metricService.remove(
MetricType.COUNTER,
Metric.BINARY_ALLOCATOR.toString(),
Tag.NAME.toString(),
EVICTED_BY_GC_EVICTION);
}

public void updateCounter(int allocateFromSlabDelta, int allocateFromJVMDelta) {
public void updateAllocationCounter(long allocateFromSlabDelta, long allocateFromJVMDelta) {
allocateFromSlab.inc(allocateFromSlabDelta);
allocateFromJVM.inc(allocateFromJVMDelta);
}

public void updateGcEvictionCounter(long evictedByGcEvictionDelta) {
evictedByGcEviction.inc(evictedByGcEvictionDelta);
}

public void updateSampleEvictionCounter(long evictedBySampleEvictionDelta) {
evictedBySampleEviction.inc(evictedBySampleEvictionDelta);
}
}

0 comments on commit a966a9f

Please sign in to comment.