Java Code Examples for com.sun.management.OperatingSystemMXBean#getSystemLoadAverage()

The following examples show how to use com.sun.management.OperatingSystemMXBean#getSystemLoadAverage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HealthStatisticsReader.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
public CartridgeStatistics getCartridgeStatistics() throws IOException {
    OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    double totalMemory = (double) (osBean.getTotalPhysicalMemorySize() / MB);
    double usedMemory = (double) ((totalMemory - (osBean.getFreePhysicalMemorySize() / MB)));
    double loadAvg = (double) osBean.getSystemLoadAverage();
    // assume system cores = available cores to JVM
    int cores = osBean.getAvailableProcessors();
    double memoryConsumption = (usedMemory / totalMemory) * 100;
    double loadAvgPercentage = (loadAvg / cores) * 100;

    if (log.isDebugEnabled()) {
        log.debug("Memory consumption: [totalMemory] " + totalMemory + "Mb [usedMemory] " + usedMemory + "Mb: " + memoryConsumption + "%");
        log.debug("Processor consumption: [loadAverage] " + loadAvg + " [cores] " + cores + ": " + loadAvgPercentage + "%");
    }

    return (new CartridgeStatistics(memoryConsumption, loadAvgPercentage));
}
 
Example 2
Source File: SystemStatusListener.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
        currentLoad = osBean.getSystemLoadAverage();
        /**
         * Java Doc copied from {@link OperatingSystemMXBean#getSystemCpuLoad()}:</br>
         * Returns the "recent cpu usage" for the whole system. This value is a double in the [0.0,1.0] interval.
         * A value of 0.0 means that all CPUs were idle during the recent period of time observed, while a value
         * of 1.0 means that all CPUs were actively running 100% of the time during the recent period being
         * observed. All values betweens 0.0 and 1.0 are possible depending of the activities going on in the
         * system. If the system recent cpu usage is not available, the method returns a negative value.
         */
        currentCpuUsage = osBean.getSystemCpuLoad();

        StringBuilder sb = new StringBuilder();
        if (currentLoad > SystemRuleManager.getHighestSystemLoad()) {
            sb.append("load:").append(currentLoad).append(";");
            sb.append("cpu:").append(currentCpuUsage).append(";");
            sb.append("qps:").append(Constants.ENTRY_NODE.passQps()).append(";");
            sb.append("rt:").append(Constants.ENTRY_NODE.avgRt()).append(";");
            sb.append("thread:").append(Constants.ENTRY_NODE.curThreadNum()).append(";");
            sb.append("success:").append(Constants.ENTRY_NODE.successQps()).append(";");
            sb.append("minRt:").append(Constants.ENTRY_NODE.minRt()).append(";");
            sb.append("maxSuccess:").append(Constants.ENTRY_NODE.maxSuccessQps()).append(";");
            RecordLog.info(sb.toString());
        }

    } catch (Throwable e) {
        RecordLog.info("could not get system error ", e);
    }
}
 
Example 3
Source File: ServerLoadStatus.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public void calculateSystemInfo() {
    this.osName = System.getProperty("os.name");
    int kb = 1024;
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
    this.systemLoadAverage = osmxb.getSystemLoadAverage();
    this.availableProcessors = osmxb.getAvailableProcessors();
    this.freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
    this.totalPhysicalMemorySize = osmxb.getTotalPhysicalMemorySize();
    this.usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb;

}
 
Example 4
Source File: SystemStatusListener.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
        currentLoad = osBean.getSystemLoadAverage();

        /*
         * Java Doc copied from {@link OperatingSystemMXBean#getSystemCpuLoad()}:</br>
         * Returns the "recent cpu usage" for the whole system. This value is a double in the [0.0,1.0] interval.
         * A value of 0.0 means that all CPUs were idle during the recent period of time observed, while a value
         * of 1.0 means that all CPUs were actively running 100% of the time during the recent period being
         * observed. All values between 0.0 and 1.0 are possible depending of the activities going on in the
         * system. If the system recent cpu usage is not available, the method returns a negative value.
         */
        double systemCpuUsage = osBean.getSystemCpuLoad();

        // calculate process cpu usage to support application running in container environment
        RuntimeMXBean runtimeBean = ManagementFactory.getPlatformMXBean(RuntimeMXBean.class);
        long newProcessCpuTime = osBean.getProcessCpuTime();
        long newProcessUpTime = runtimeBean.getUptime();
        int cpuCores = osBean.getAvailableProcessors();
        long processCpuTimeDiffInMs = TimeUnit.NANOSECONDS
                .toMillis(newProcessCpuTime - processCpuTime);
        long processUpTimeDiffInMs = newProcessUpTime - processUpTime;
        double processCpuUsage = (double) processCpuTimeDiffInMs / processUpTimeDiffInMs / cpuCores;
        processCpuTime = newProcessCpuTime;
        processUpTime = newProcessUpTime;

        currentCpuUsage = Math.max(processCpuUsage, systemCpuUsage);

        if (currentLoad > SystemRuleManager.getSystemLoadThreshold()) {
            writeSystemStatusLog();
        }
    } catch (Throwable e) {
        RecordLog.warn("[SystemStatusListener] Failed to get system metrics from JMX", e);
    }
}
 
Example 5
Source File: HostTask.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
private static HostInfo getHostInfo(MxBean mxBean) {
    OperatingSystemMXBean osBean = mxBean.getOsBean();
    ThreadMXBean threadBean = mxBean.getThreadBean();

    HostInfo hostInfo = new HostInfo();
    String osName = System.getProperty("os.name");
    int availableProcessors = Runtime.getRuntime().availableProcessors();
    String cpuLoadAverages = null;
    if (osName != null && osName.toLowerCase().contains("linux")) {
        try {
            File file = new File(LOADAVG_FILENAME);
            cpuLoadAverages = FileUtil.readFile(file);
        } catch (IOException e) {
            logger.error("get CPU Load Averages error", e);
        }
    }

    double systemLoadAverage = osBean.getSystemLoadAverage();
    // 可使用内存
    long totalMemory = Runtime.getRuntime().totalMemory() / KB;
    // 剩余内存
    long freeMemory = Runtime.getRuntime().freeMemory() / KB;
    // 最大可使用内存
    long maxMemory = Runtime.getRuntime().maxMemory() / KB;

    //总交换空间
    long totalSwapSpaceSize = osBean.getTotalSwapSpaceSize() / KB;
    //空闲交换空间
    long freeSwapSpaceSize = osBean.getFreeSwapSpaceSize() / KB;
    //总物理内存
    long totalPhysicalMemorySize = osBean.getTotalPhysicalMemorySize() / KB;
    //空闲物理内存
    long freePhysicalMemorySize = osBean.getFreePhysicalMemorySize() / KB;
    //系统CPU利用率
    double systemCpuLoad = osBean.getSystemCpuLoad();

    long totalThread = threadBean.getTotalStartedThreadCount();

    //获取磁盘信息
    getDiskInfo(hostInfo);

    hostInfo.setAvailableProcessors(availableProcessors);
    hostInfo.setSystemLoadAverage(systemLoadAverage);
    hostInfo.setCpuLoadAverages(Strings.nullToEmpty(cpuLoadAverages));
    hostInfo.setOsName(osName);
    hostInfo.setTotalMemory(totalMemory);
    hostInfo.setFreeMemory(freeMemory);
    hostInfo.setMaxMemory(maxMemory);
    hostInfo.setTotalSwapSpaceSize(totalSwapSpaceSize);
    hostInfo.setFreeSwapSpaceSize(freeSwapSpaceSize);
    hostInfo.setTotalPhysicalMemorySize(totalPhysicalMemorySize);
    hostInfo.setFreePhysicalMemorySize(freePhysicalMemorySize);
    hostInfo.setUsedMemory(totalPhysicalMemorySize - freePhysicalMemorySize);
    hostInfo.setTotalThread(totalThread);
    hostInfo.setCpuRatio(systemCpuLoad);
    return hostInfo;
}
 
Example 6
Source File: PerformanceStatus.java    From vi with Apache License 2.0 4 votes vote down vote up
public PerformanceStatus() {
    OperatingSystemMXBean bean= (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();

    MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
    heapMemoryUsage= memBean.getHeapMemoryUsage();
    nonHeapMemoryUsage = memBean.getNonHeapMemoryUsage();

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    currentThreadCount=threadBean.getThreadCount();
    daemonThreadCount= threadBean.getDaemonThreadCount();
    beanCreatedThreadCount= threadBean.getTotalStartedThreadCount();
    peakThreadCount = threadBean.getPeakThreadCount();

    ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    loadedClassCount=classLoadingBean.getLoadedClassCount();
    totalLoadedClassCount=classLoadingBean.getTotalLoadedClassCount();
    unloadedClassCount=classLoadingBean.getUnloadedClassCount();
    committedVirtualMemorySize = (bean.getCommittedVirtualMemorySize());
    freePhysicalMemorySize =(bean.getFreePhysicalMemorySize());
    totalPhysicalMemorySize =(bean.getTotalPhysicalMemorySize());

    freeSwapSpaceSize =(bean.getFreeSwapSpaceSize());
    totalSwapSpaceSize =(bean.getTotalSwapSpaceSize());
    processCpuTime =(bean.getProcessCpuTime());
    availableProcessors =bean.getAvailableProcessors();
    processCpuLoad =bean.getProcessCpuLoad();

    systemCpuLoad =bean.getSystemCpuLoad();
    systemLoadAverage =bean.getSystemLoadAverage();
    appStartUpTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(runtimeBean.getStartTime()));
    runtime = (new Date().getTime() - runtimeBean.getStartTime())/1000;
    os = bean.getName()+" "+bean.getVersion();

    if(HostInfo.isLinux()){
        try {
            availableMemory = (LinuxInfoUtil.getAvailableMemKB()*1024l);
        } catch (Throwable ignored) {
        }
    }

    File[] roots = File.listRoots();
    for(File file:roots){
        rootFiles.add(new RootFile(file.getAbsolutePath(),file.getTotalSpace(),file.getFreeSpace()));
    }
    getGCStatus();
}
 
Example 7
Source File: LocalHostOverview.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
LocalHostOverview() {
  osMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
  loadAverageAvailable = osMXBean.getSystemLoadAverage() >= 0;
}