专栏首页码匠的流水账聊聊skywalking的MemoryProvider
原创

聊聊skywalking的MemoryProvider

本文主要研究一下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指标

doc

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊skywalking的MemoryProvider

    skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/...

    codecraft
  • 聊聊rocketmq的retryAnotherBrokerWhenNotStoreOK

    本文主要研究一下rocketmq的retryAnotherBrokerWhenNotStoreOK

    codecraft
  • 聊聊rocketmq的retryAnotherBrokerWhenNotStoreOK

    本文主要研究一下rocketmq的retryAnotherBrokerWhenNotStoreOK

    codecraft
  • 聊聊skywalking的MemoryProvider

    skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/...

    codecraft
  • JS与Android IOS 交互

    week
  • [每日一题]老王赛马

    题目描述 赛马是一古老的游戏,早在公元前四世纪的中国,处在诸侯割据的状态,历史上称为“战国时期”。在魏国作官的孙膑,因为受到同僚庞涓的迫害,被齐国使臣救出后,到...

    编程范 源代码公司
  • 【每日一题】问题 1250: 素数回文

    题目描述 赛马是一古老的游戏,早在公元前四世纪的中国,处在诸侯割据的状态,历史上称为“战国时期”。在魏国作官的孙膑,因为受到同僚庞涓的迫害,被齐国使臣救出后,...

    编程范 源代码公司
  • 2020 FFmpeg 滤镜详解

    在多媒体处理中,术语滤镜(filter)指的是修改未编码的原始音视频数据帧的一种软件工具。

    jerrypxiao
  • Swift3.0 - 类和结构体的区别

    结论: 在数据量比较大的排序中,结构体排序的速度比较慢,因为结构体是值类型,排序的时候,需要大量的赋值运算。而对象只需要交换地址即可。

    酷走天涯
  • 连连看

    连连看游戏规则:只要将相同的两张牌用三根以内的直线连在一起就可以消除,规则简单容易上手。游戏速度节奏快,画面清晰可爱,适合细心的玩家。

    DeROy

扫码关注云+社区

领取腾讯云代金券