Skip to content

Commit

Permalink
7979: Reduce allocation rate in ParserStats
Browse files Browse the repository at this point in the history
Reviewed-by: jpbempel
  • Loading branch information
richardstartin authored and Jean-Philippe Bempel committed Nov 23, 2022
1 parent c34d1ad commit 68387c5
Showing 1 changed file with 21 additions and 29 deletions.
Expand Up @@ -43,6 +43,7 @@
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.function.Consumer;

import org.openjdk.jmc.common.IDescribable;
Expand Down Expand Up @@ -73,10 +74,10 @@ public class ParserStats {
private final AtomicLong skippedEventCount = new AtomicLong();
private final ConcurrentHashMap<String, EventTypeStats> statsByType = new ConcurrentHashMap<>();
private final ConcurrentLinkedDeque<ConstantPoolInfo> constantPoolInfoList = new ConcurrentLinkedDeque<>();
private final ConcurrentHashMap<String, Long> entryPoolSizeByType = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, AtomicLong> entryPoolSizeByType = new ConcurrentHashMap<>();
private IItemCollection poolStats;
private IItemCollection constants;
private Map<String, IConstantPoolExtension> constantPoolExtensions = new ConcurrentHashMap<>();
private final Map<String, IConstantPoolExtension> constantPoolExtensions = new ConcurrentHashMap<>();

public void setVersion(short majorVersion, short minorVersion) {
this.majorVersion = majorVersion;
Expand All @@ -92,26 +93,15 @@ public void setSkippedEventCount(long skippedEventCount) {
}

public void updateEventStats(String eventTypeName, long size) {
statsByType.compute(eventTypeName, (key, stats) -> {
if (stats == null) {
return new EventTypeStats(eventTypeName, size);
}
stats.add(size);
return stats;
});
statsByType.computeIfAbsent(eventTypeName, EventTypeStats::new).add(size);
}

public void addConstantPool(long id, String name, FastAccessNumberMap<Object> constantPool) {
constantPoolInfoList.add(new ConstantPoolInfo(id, name, constantPool));
}

public void addEntryPoolSize(String typeIdentifier, long size) {
entryPoolSizeByType.compute(typeIdentifier, (key, value) -> {
if (value == null) {
return size;
}
return value + size;
});
entryPoolSizeByType.computeIfAbsent(typeIdentifier, id -> new AtomicLong()).addAndGet(size);
}

public void addConstantPoolExtension(IConstantPoolExtension extension) {
Expand Down Expand Up @@ -145,22 +135,22 @@ public long getCount(String eventTypeName) {
if (stats == null) {
return 0;
}
return stats.count;
return stats.getCount();
}

public long getTotalSize(String eventTypeName) {
EventTypeStats stats = statsByType.get(eventTypeName);
if (stats == null) {
return 0;
}
return stats.totalSize;
return stats.getTotalSize();
}

public IItemCollection getConstantPools() {
if (poolStats == null) {
Map<String, ConstPoolItem> poolStatsByName = new HashMap<>();
for (ConstantPoolInfo info : constantPoolInfoList) {
ConstPoolItem poolItem = poolStatsByName.computeIfAbsent(info.name, key -> createPoolItem(info));
ConstPoolItem poolItem = poolStatsByName.computeIfAbsent(info.name, this::createPoolItem);
poolItem.count += getConstantPoolCount(info.constantPool);
}
poolStats = ItemCollectionToolkit.build(poolStatsByName.values().stream());
Expand Down Expand Up @@ -321,10 +311,10 @@ public IType<?> getType() {
}
}

private ConstPoolItem createPoolItem(ConstantPoolInfo info) {
Long totalSize = entryPoolSizeByType.get(info.name);
private ConstPoolItem createPoolItem(String infoName) {
AtomicLong totalSize = entryPoolSizeByType.get(infoName);
long entrySize = totalSize != null ? totalSize.longValue() : 0;
return new ConstPoolItem(info.name, 0, entrySize);
return new ConstPoolItem(infoName, 0, entrySize);
}

private long getConstantPoolCount(FastAccessNumberMap<Object> constantPool) {
Expand All @@ -339,18 +329,20 @@ private long getConstantPoolCount(FastAccessNumberMap<Object> constantPool) {

private static class EventTypeStats implements IEventStats {
private final String eventTypeName;
private long count;
private long totalSize;

public EventTypeStats(String eventTypeName, long size) {
private static final AtomicLongFieldUpdater<EventTypeStats> COUNT_UPDATER = AtomicLongFieldUpdater
.newUpdater(EventTypeStats.class, "count");
private volatile long count;
private static final AtomicLongFieldUpdater<EventTypeStats> TOTAL_SIZE_UPDATER = AtomicLongFieldUpdater
.newUpdater(EventTypeStats.class, "totalSize");
private volatile long totalSize;

public EventTypeStats(String eventTypeName) {
this.eventTypeName = eventTypeName;
this.count = 1;
this.totalSize = size;
}

public void add(long size) {
count++;
totalSize += size;
COUNT_UPDATER.incrementAndGet(this);
TOTAL_SIZE_UPDATER.addAndGet(this, size);
}

@Override
Expand Down

0 comments on commit 68387c5

Please sign in to comment.