All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hecloud.runtime.common.utils.SystemTool Maven / Gradle / Ivy

package com.hecloud.runtime.common.utils;

import com.hecloud.runtime.common.model.GenericResult;

import java.lang.management.*;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author LoveinBJ
 */
public class SystemTool {

    private final long MB = 1024 * 1024L;
    private NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
    private NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));

    public GenericResult> status() {
        Map state = new HashMap<>(16, 0.75F);
        OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        ThreadMXBean threads = ManagementFactory.getThreadMXBean();
        MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
        ClassLoadingMXBean classloader = ManagementFactory.getClassLoadingMXBean();
        state.put("os.user", System.getProperty("user.name"));
        state.put("os.name", os.getName() + "-" + os.getVersion());
        state.put("os.arch", os.getArch());
        state.put("os.cpu", Runtime.getRuntime().availableProcessors());
        state.put("os.ip", NicDetector.getIPList().toArray());
        state.put("jvm.name", runtime.getVmName() + " " + runtime.getVmVersion());
        state.put("jvm.vendor", runtime.getVmVendor());
        state.put("jvm.version", System.getProperty("java.version"));
        state.put("jvm.uptime", toDuration(runtime.getUptime()));
        state.put("jvm.memory.total", Runtime.getRuntime().totalMemory() / MB);
        state.put("jvm.memory.free", Runtime.getRuntime().freeMemory() / MB);
        state.put("jvm.memory.max", Runtime.getRuntime().maxMemory() / MB);
        state.put("jvm.heap.current", memory.getHeapMemoryUsage().getUsed() / MB);
        state.put("jvm.heap.max", memory.getHeapMemoryUsage().getMax() / MB);
        state.put("jvm.heap.commited", memory.getHeapMemoryUsage().getCommitted() / MB);
        state.put("jvm.threads.live", threads.getThreadCount());
        state.put("jvm.threads.daemon", threads.getDaemonThreadCount());
        state.put("jvm.threads.peak", threads.getPeakThreadCount());
        state.put("jvm.threads.total", threads.getTotalStartedThreadCount());

        state.put("jvm.class.unload", classloader.getUnloadedClassCount());
        state.put("jvm.class.total", classloader.getTotalLoadedClassCount());
        state.put("jvm.class.current", classloader.getLoadedClassCount());

        return new GenericResult<>(true, "ok", state);
    }

    protected String printSizeInKb(double size) {
        return fmtI.format((long) (size / 1024)) + " kbytes";
    }

    protected String toDuration(double uptime) {
        uptime /= 1000;
        if (uptime < 60) {
            return fmtD.format(uptime) + " seconds";
        }
        uptime /= 60;
        if (uptime < 60) {
            long minutes = (long) uptime;
            String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
            return s;
        }
        uptime /= 60;
        if (uptime < 24) {
            long hours = (long) uptime;
            long minutes = (long) ((uptime - hours) * 60);
            String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
            if (minutes != 0) {
                s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
            }
            return s;
        }
        uptime /= 24;
        long days = (long) uptime;
        long hours = (long) ((uptime - days) * 24);
        String s = fmtI.format(days) + (days > 1 ? " days" : " day");
        if (hours != 0) {
            s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
        }
        return s;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy