前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊Elasticsearch的ProcessProbe

聊聊Elasticsearch的ProcessProbe

原创
作者头像
code4it
发布2019-05-21 23:39:49
6710
发布2019-05-21 23:39:49
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下Elasticsearch的ProcessProbe

ProcessProbe

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/monitor/process/ProcessProbe.java

代码语言:javascript
复制
public class ProcessProbe {
​
    private static final OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
​
    private static final Method getMaxFileDescriptorCountField;
    private static final Method getOpenFileDescriptorCountField;
    private static final Method getProcessCpuLoad;
    private static final Method getProcessCpuTime;
    private static final Method getCommittedVirtualMemorySize;
​
    static {
        getMaxFileDescriptorCountField = getUnixMethod("getMaxFileDescriptorCount");
        getOpenFileDescriptorCountField = getUnixMethod("getOpenFileDescriptorCount");
        getProcessCpuLoad = getMethod("getProcessCpuLoad");
        getProcessCpuTime = getMethod("getProcessCpuTime");
        getCommittedVirtualMemorySize = getMethod("getCommittedVirtualMemorySize");
    }
​
    private static class ProcessProbeHolder {
        private static final ProcessProbe INSTANCE = new ProcessProbe();
    }
​
    public static ProcessProbe getInstance() {
        return ProcessProbeHolder.INSTANCE;
    }
​
    private ProcessProbe() {
    }
​
    /**
     * Returns the maximum number of file descriptors allowed on the system, or -1 if not supported.
     */
    public long getMaxFileDescriptorCount() {
        if (getMaxFileDescriptorCountField == null) {
            return -1;
        }
        try {
            return (Long) getMaxFileDescriptorCountField.invoke(osMxBean);
        } catch (Exception t) {
            return -1;
        }
    }
​
    /**
     * Returns the number of opened file descriptors associated with the current process, or -1 if not supported.
     */
    public long getOpenFileDescriptorCount() {
        if (getOpenFileDescriptorCountField == null) {
            return -1;
        }
        try {
            return (Long) getOpenFileDescriptorCountField.invoke(osMxBean);
        } catch (Exception t) {
            return -1;
        }
    }
​
    /**
     * Returns the process CPU usage in percent
     */
    public short getProcessCpuPercent() {
        return Probes.getLoadAndScaleToPercent(getProcessCpuLoad, osMxBean);
    }
​
    /**
     * Returns the CPU time (in milliseconds) used by the process on which the Java virtual machine is running, or -1 if not supported.
     */
    public long getProcessCpuTotalTime() {
        if (getProcessCpuTime != null) {
            try {
                long time = (long) getProcessCpuTime.invoke(osMxBean);
                if (time >= 0) {
                    return (time / 1_000_000L);
                }
            } catch (Exception t) {
                return -1;
            }
        }
        return -1;
    }
​
    /**
     * Returns the size (in bytes) of virtual memory that is guaranteed to be available to the running process
     */
    public long getTotalVirtualMemorySize() {
        if (getCommittedVirtualMemorySize != null) {
            try {
                long virtual = (long) getCommittedVirtualMemorySize.invoke(osMxBean);
                if (virtual >= 0) {
                    return virtual;
                }
            } catch (Exception t) {
                return -1;
            }
        }
        return -1;
    }
​
    public ProcessInfo processInfo(long refreshInterval) {
        return new ProcessInfo(jvmInfo().pid(), BootstrapInfo.isMemoryLocked(), refreshInterval);
    }
​
    public ProcessStats processStats() {
        ProcessStats.Cpu cpu = new ProcessStats.Cpu(getProcessCpuPercent(), getProcessCpuTotalTime());
        ProcessStats.Mem mem = new ProcessStats.Mem(getTotalVirtualMemorySize());
        return new ProcessStats(System.currentTimeMillis(), getOpenFileDescriptorCount(), getMaxFileDescriptorCount(), cpu, mem);
    }
​
    /**
     * Returns a given method of the OperatingSystemMXBean,
     * or null if the method is not found or unavailable.
     */
    private static Method getMethod(String methodName) {
        try {
            return Class.forName("com.sun.management.OperatingSystemMXBean").getMethod(methodName);
        } catch (Exception t) {
            // not available
            return null;
        }
    }
​
    /**
     * Returns a given method of the UnixOperatingSystemMXBean,
     * or null if the method is not found or unavailable.
     */
    private static Method getUnixMethod(String methodName) {
        try {
            return Class.forName("com.sun.management.UnixOperatingSystemMXBean").getMethod(methodName);
        } catch (Exception t) {
            // not available
            return null;
        }
    }
}
  • ProcessProbe使用static代码块反射获取了getMaxFileDescriptorCountField、getOpenFileDescriptorCountField、getProcessCpuLoad、getProcessCpuTime、getCommittedVirtualMemorySize这几个method
  • getMaxFileDescriptorCountField、getOpenFileDescriptorCountField使用getUnixMethod方法,从com.sun.management.UnixOperatingSystemMXBean获取
  • getProcessCpuLoad、getProcessCpuTime、getCommittedVirtualMemorySize使用getMethod方法,从com.sun.management.OperatingSystemMXBean获取
  • getMaxFileDescriptorCount、getOpenFileDescriptorCount、getProcessCpuTotalTime、getTotalVirtualMemorySize方法首先都判断对应的method是否为null,如果为null则返回-1,否则则反射获取对应的值,出现异常的话,返回-1
  • processStats方法返回ProcessStats,它由ProcessStats.Cpu、ProcessStats.Mem、getOpenFileDescriptorCount()、getMaxFileDescriptorCount()构成

小结

  • ProcessProbe使用static代码块反射获取了getMaxFileDescriptorCountField、getOpenFileDescriptorCountField、getProcessCpuLoad、getProcessCpuTime、getCommittedVirtualMemorySize这几个method
  • getMaxFileDescriptorCount、getOpenFileDescriptorCount、getProcessCpuTotalTime、getTotalVirtualMemorySize方法首先都判断对应的method是否为null,如果为null则返回-1,否则则反射获取对应的值,出现异常的话,返回-1
  • processStats方法返回ProcessStats,它由ProcessStats.Cpu、ProcessStats.Mem、getOpenFileDescriptorCount()、getMaxFileDescriptorCount()构成

doc

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ProcessProbe
  • 小结
  • doc
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档