序
本文主要研究一下skywalking的MemoryProvider
MemoryProvider
skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/memory/MemoryProvider.java
public enum MemoryProvider {
INSTANCE;
private final MemoryMXBean memoryMXBean;
MemoryProvider() {
this.memoryMXBean = ManagementFactory.getMemoryMXBean();
}
public List<Memory> getMemoryMetricList() {
List<Memory> memoryList = new LinkedList<Memory>();
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
Memory.Builder heapMemoryBuilder = Memory.newBuilder();
heapMemoryBuilder.setIsHeap(true);
heapMemoryBuilder.setInit(heapMemoryUsage.getInit());
heapMemoryBuilder.setUsed(heapMemoryUsage.getUsed());
heapMemoryBuilder.setCommitted(heapMemoryUsage.getCommitted());
heapMemoryBuilder.setMax(heapMemoryUsage.getMax());
memoryList.add(heapMemoryBuilder.build());
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
Memory.Builder nonHeapMemoryBuilder = Memory.newBuilder();
nonHeapMemoryBuilder.setIsHeap(false);
nonHeapMemoryBuilder.setInit(nonHeapMemoryUsage.getInit());
nonHeapMemoryBuilder.setUsed(nonHeapMemoryUsage.getUsed());
nonHeapMemoryBuilder.setCommitted(nonHeapMemoryUsage.getCommitted());
nonHeapMemoryBuilder.setMax(nonHeapMemoryUsage.getMax());
memoryList.add(nonHeapMemoryBuilder.build());
return memoryList;
}
}
- MemoryProvider通过ManagementFactory.getMemoryMXBean()获取MemoryMXBean,之后获取了heapMemoryUsage以及nonHeapMemoryUsage指标(
init、used、committed、max
)
MemoryPoolProvider
skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/memorypool/MemoryPoolProvider.java
public enum MemoryPoolProvider {
INSTANCE;
private MemoryPoolMetricsAccessor metricAccessor;
private List<MemoryPoolMXBean> beans;
MemoryPoolProvider() {
beans = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean bean : beans) {
String name = bean.getName();
MemoryPoolMetricsAccessor accessor = findByBeanName(name);
if (accessor != null) {
metricAccessor = accessor;
break;
}
}
if (metricAccessor == null) {
metricAccessor = new UnknownMemoryPool();
}
}
public List<MemoryPool> getMemoryPoolMetricsList() {
return metricAccessor.getMemoryPoolMetricsList();
}
private MemoryPoolMetricsAccessor findByBeanName(String name) {
if (name.indexOf("PS") > -1) {
//Parallel (Old) collector ( -XX:+UseParallelOldGC )
return new ParallelCollectorModule(beans);
} else if (name.indexOf("CMS") > -1) {
// CMS collector ( -XX:+UseConcMarkSweepGC )
return new CMSCollectorModule(beans);
} else if (name.indexOf("G1") > -1) {
// G1 collector ( -XX:+UseG1GC )
return new G1CollectorModule(beans);
} else if (name.equals("Survivor Space")) {
// Serial collector ( -XX:+UseSerialGC )
return new SerialCollectorModule(beans);
} else {
// Unknown
return null;
}
}
}
- MemoryPoolProvider通过ManagementFactory.getMemoryPoolMXBeans()获取MemoryPoolMXBean列表,之后遍历该列表获取对应的MemoryPoolMetricsAccessor,若找不到则默认为UnknownMemoryPool(
表示不支持的垃圾收集器类型
);其getMemoryPoolMetricsList则通过metricAccessor.getMemoryPoolMetricsList()返回MemoryPool指标
MemoryPoolMetricsAccessor
skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/memorypool/MemoryPoolModule.java
public abstract class MemoryPoolModule implements MemoryPoolMetricsAccessor {
private List<MemoryPoolMXBean> beans;
public MemoryPoolModule(List<MemoryPoolMXBean> beans) {
this.beans = beans;
}
@Override
public List<MemoryPool> getMemoryPoolMetricsList() {
List<MemoryPool> poolList = new LinkedList<MemoryPool>();
for (MemoryPoolMXBean bean : beans) {
String name = bean.getName();
PoolType type;
if (contains(getCodeCacheNames(), name)) {
type = PoolType.CODE_CACHE_USAGE;
} else if (contains(getEdenNames(), name)) {
type = PoolType.NEWGEN_USAGE;
} else if (contains(getOldNames(), name)) {
type = PoolType.OLDGEN_USAGE;
} else if (contains(getSurvivorNames(), name)) {
type = PoolType.SURVIVOR_USAGE;
} else if (contains(getMetaspaceNames(), name)) {
type = PoolType.METASPACE_USAGE;
} else if (contains(getPermNames(), name)) {
type = PoolType.PERMGEN_USAGE;
} else {
continue;
}
MemoryUsage usage = bean.getUsage();
poolList.add(MemoryPool.newBuilder().setType(type)
.setInit(usage.getInit())
.setMax(usage.getMax())
.setCommited(usage.getCommitted())
.setUsed(usage.getUsed())
.build());
}
return poolList;
}
private boolean contains(String[] possibleNames, String name) {
for (String possibleName : possibleNames) {
if (name.equals(possibleName)) {
return true;
}
}
return false;
}
protected abstract String[] getPermNames();
protected abstract String[] getCodeCacheNames();
protected abstract String[] getEdenNames();
protected abstract String[] getOldNames();
protected abstract String[] getSurvivorNames();
protected abstract String[] getMetaspaceNames();
}
- MemoryPoolModule声明实现了MemoryPoolMetricsAccessor接口,其getMemoryPoolMetricsList遍历MemoryPoolMXBean列表,找出对应的type(
CODE_CACHE_USAGE、NEWGEN_USAGE、OLDGEN_USAGE、SURVIVOR_USAGE、METASPACE_USAGE、PERMGEN_USAGE
),然后构建对应的MemoryPool指标(type、init、max、committed、used
);由于不同垃圾收集器的对应的name不一样,因而这里通过抽象方法暴露给子类去实现,其子类有SerialCollectorModule、ParallelCollectorModule、CMSCollectorModule、G1CollectorModule
小结
MemoryProvider通过ManagementFactory.getMemoryMXBean()获取MemoryMXBean,之后获取了heapMemoryUsage以及nonHeapMemoryUsage指标(init、used、committed、max
);MemoryPoolProvider通过ManagementFactory.getMemoryPoolMXBeans()获取MemoryPoolMXBean列表,之后遍历该列表获取对应的MemoryPoolMetricsAccessor,若找不到则默认为UnknownMemoryPool;其getMemoryPoolMetricsList则通过metricAccessor.getMemoryPoolMetricsList()返回MemoryPool指标
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。