本文主要研究一下skywalking的MemoryProviderjava
skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/memory/MemoryProvider.javagit
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; } }
init、used、committed、max
)skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/memorypool/MemoryPoolProvider.javagithub
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; } } }
表示不支持的垃圾收集器類型
);其getMemoryPoolMetricsList則經過metricAccessor.getMemoryPoolMetricsList()返回MemoryPool指標skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/memorypool/MemoryPoolModule.javaapache
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(); }
CODE_CACHE_USAGE、NEWGEN_USAGE、OLDGEN_USAGE、SURVIVOR_USAGE、METASPACE_USAGE、PERMGEN_USAGE
),而後構建對應的MemoryPool指標(type、init、max、committed、used
);因爲不一樣垃圾收集器的對應的name不同,於是這裏經過抽象方法暴露給子類去實現,其子類有SerialCollectorModule、ParallelCollectorModule、CMSCollectorModule、G1CollectorModuleMemoryProvider經過ManagementFactory.getMemoryMXBean()獲取MemoryMXBean,以後獲取了heapMemoryUsage以及nonHeapMemoryUsage指標(init、used、committed、max
);MemoryPoolProvider經過ManagementFactory.getMemoryPoolMXBeans()獲取MemoryPoolMXBean列表,以後遍歷該列表獲取對應的MemoryPoolMetricsAccessor,若找不到則默認爲UnknownMemoryPool;其getMemoryPoolMetricsList則經過metricAccessor.getMemoryPoolMetricsList()返回MemoryPool指標jvm