Example usage for java.lang.management ManagementFactory getOperatingSystemMXBean

List of usage examples for java.lang.management ManagementFactory getOperatingSystemMXBean

Introduction

In this page you can find the example usage for java.lang.management ManagementFactory getOperatingSystemMXBean.

Prototype

public static OperatingSystemMXBean getOperatingSystemMXBean() 

Source Link

Document

Returns the managed bean for the operating system on which the Java virtual machine is running.

Usage

From source file:com.qwazr.utils.RuntimeUtils.java

public static Long getOpenFileCount() {
    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    if (os instanceof UnixOperatingSystemMXBean)
        return ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount();
    return null;/*from  w ww  .j  av  a2  s.  c om*/
}

From source file:api.Status.java

public static Result getStatus() {
    ObjectNode metrics = Json.newObject();

    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    TreeMap<String, Object> values = new TreeMap<String, Object>();
    for (Method method : os.getClass().getDeclaredMethods()) {
        method.setAccessible(true);/*from  w  w  w.j a va2s.c  o  m*/
        if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
            Object value;
            try {
                value = method.invoke(os);
                values.put(method.getName(), value);
            } catch (Exception e) {
                Logger.warn("Error when invoking " + os.getClass().getName()
                        + " (OperatingSystemMXBean) method " + method.getName() + ": " + e);
            } // try
        } // if
    } // for

    metrics.put("jvmLoad", (Double) values.get("getProcessCpuLoad"));
    metrics.put("cpuLoad", (Double) values.get("getSystemCpuLoad"));
    metrics.put("openFD", (Long) values.get("getOpenFileDescriptorCount"));
    metrics.put("maxFD", (Long) values.get("getMaxFileDescriptorCount"));
    metrics.put("freeMemory", (Long) values.get("getFreePhysicalMemorySize"));
    metrics.put("totalMemory", (Long) values.get("getTotalPhysicalMemorySize"));

    return ok(metrics.toString());
}

From source file:org.apache.stratos.cartridge.agent.statistics.publisher.HealthStatisticsReader.java

public static double getMemoryConsumption() {
    OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    double totalMemory = (double) (osBean.getTotalPhysicalMemorySize() / MB);
    double usedMemory = (double) ((totalMemory - (osBean.getFreePhysicalMemorySize() / MB)));

    if (log.isDebugEnabled()) {
        log.debug(//  w  w  w . j  a v a2  s  . c  o m
                "Calculating memory consumption: [totalMemory] " + totalMemory + " [usedMemory] " + usedMemory);
    }
    double memoryConsumption = (usedMemory / totalMemory) * 100;
    if (log.isDebugEnabled()) {
        log.debug("Calculating memory consumption: [percentage] " + memoryConsumption);
    }
    return memoryConsumption;
}

From source file:VASSAL.tools.lang.MemoryUtils.java

/**
 * Gets the amount of physical memory (RAM) in this machine, in bytes.
 *
 * @return the amount of RAM, in bytes; or -1 if the amount of RAM
 * cannot be queried.//ww  w  .  j a  v  a  2s.  c om
 */
public static long getPhysicalMemory() {
    if (!SystemUtils.IS_OS_WINDOWS || SystemUtils.IS_OS_WINDOWS_98 || SystemUtils.IS_OS_WINDOWS_ME) {
        // Windows 98, ME support a maximum of 2GB RAM, so are unaffected
        // by the bug which causes incorrect reporting over 2GB. Hence,
        // we can handle them in the normal way.
        final Object o = ManagementFactory.getOperatingSystemMXBean();

        try {
            if (o instanceof OperatingSystemMXBean) {
                final OperatingSystemMXBean osb = (OperatingSystemMXBean) o;
                return osb.getTotalPhysicalMemorySize();
            }
        } catch (NoClassDefFoundError e) {
            // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
        }

        // We didn't get a com.sun.management.OperatingSystemMXBean.
        return -1;
    } else {
        // FIXME: totalPhysicalMemorySize() doesn't return the correct result
        // on Windows machines with more than 2GB RAM, so we have to call
        // GlobalMemoryStatusEx ourselves instead of letting the bean do it
        // for us. See Sun Bug 6853676. This case can be removed when the bug
        // is fixed in a released JVM.

        // The Windows Kernel32 call GlobalMemoryStatusEx() fills a
        // MEMORYSTATUSEX structure with various facts about memory usage.
        final Kernel32.MEMORYSTATUSEX mstat = new Kernel32.MEMORYSTATUSEX();

        if (Kernel32.INSTANCE.GlobalMemoryStatusEx(mstat)) {
            return mstat.ullTotalPhys;
        } else {
            // GlobalMemoryStatusEx failed
            final PointerByReference lpBuffer = new PointerByReference();
            final int errno = Native.getLastError();
            final int msglen = Kernel32.INSTANCE.FormatMessage(
                    Kernel32.FORMAT_MESSAGE_ALLOCATE_BUFFER | Kernel32.FORMAT_MESSAGE_FROM_SYSTEM, Pointer.NULL,
                    errno, 0, lpBuffer, 0, Pointer.NULL);

            final String message = msglen > 0 ? lpBuffer.getValue().getStringArray(0)[0] : "no message";

            throw new RuntimeException("Error " + errno + ": " + message);
        }
    }
}

From source file:ro.nextreports.server.web.debug.InfoUtil.java

public static List<Info> getGeneralJVMInfo() {
    List<Info> infos = new ArrayList<Info>();

    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
    infos.add(new Info("uptime", "" + Duration.milliseconds(runtimeBean.getUptime()).toString()));
    infos.add(new Info("name", runtimeBean.getName()));
    infos.add(new Info("pid", runtimeBean.getName().split("@")[0]));

    OperatingSystemMXBean systemBean = ManagementFactory.getOperatingSystemMXBean();
    infos.add(new Info("os name", "" + systemBean.getName()));
    infos.add(new Info("os version", "" + systemBean.getVersion()));
    infos.add(new Info("system load average", "" + systemBean.getSystemLoadAverage()));
    infos.add(new Info("available processors", "" + systemBean.getAvailableProcessors()));

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    infos.add(new Info("thread count", "" + threadBean.getThreadCount()));
    infos.add(new Info("peak thread count", "" + threadBean.getPeakThreadCount()));

    MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
    infos.add(new Info("heap memory used",
            FileUtils.byteCountToDisplaySize(memoryBean.getHeapMemoryUsage().getUsed())));
    infos.add(new Info("non-heap memory used",
            FileUtils.byteCountToDisplaySize(memoryBean.getNonHeapMemoryUsage().getUsed())));

    return infos;
}

From source file:org.apache.stratos.cartridge.agent.statistics.publisher.HealthStatisticsReader.java

public static double getLoadAverage() {
    double loadAvg = (double) ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage();
    // assume system cores = available cores to JVM
    int cores = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors();

    if (log.isDebugEnabled()) {
        log.debug("Calculating load average consumption: [loadAverage] " + loadAvg + " [cores] " + cores);
    }/*from  www.  j ava 2  s.  c o m*/

    double loadAvgPercentage = (loadAvg / cores) * 100;
    if (log.isDebugEnabled()) {
        log.debug("Calculating load average consumption: [percentage] " + loadAvgPercentage);
    }
    return loadAvgPercentage;
}

From source file:com.epam.ta.reportportal.util.SystemInformatorService.java

public SystemInformatorService() {
    this.oper = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
}

From source file:com.alibaba.dragoon.client.jmx.Threading.java

public Threading() {
    Threading = ManagementFactory.getThreadMXBean();
    OperatingSystem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    Runtime = ManagementFactory.getRuntimeMXBean();

    try {//from ww w .j ava  2s  .c  o m
        gc = new GC();

        // ?
        lastGCTime = gc.getFullGCCollectionTime() + gc.getYoungGCCollectionTime();
        lastCPUTime = getProcessCpuTime();
        lastGCUpTime = lastCPUUpTime = getUpTime();

    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:net.centro.rtb.monitoringcenter.infos.OperatingSystemInfo.java

public static OperatingSystemInfo create() {
    OperatingSystemInfo operatingSystemInfo = new OperatingSystemInfo();

    OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();

    operatingSystemInfo.name = operatingSystemMXBean.getName();
    operatingSystemInfo.version = operatingSystemMXBean.getVersion();

    if (SystemUtils.IS_OS_LINUX) {
        operatingSystemInfo.distributionName = resolveLinuxDistributionName();
    }/*from   w  ww.j av  a  2  s . c  o  m*/

    operatingSystemInfo.architecture = operatingSystemMXBean.getArch();
    operatingSystemInfo.numberOfLogicalProcessors = operatingSystemMXBean.getAvailableProcessors();

    if (com.sun.management.OperatingSystemMXBean.class.isAssignableFrom(operatingSystemMXBean.getClass())) {
        com.sun.management.OperatingSystemMXBean sunOsMxBean = com.sun.management.OperatingSystemMXBean.class
                .cast(operatingSystemMXBean);
        operatingSystemInfo.physicalMemorySizeInBytes = sunOsMxBean.getTotalPhysicalMemorySize();
        operatingSystemInfo.swapSpaceSizeInBytes = sunOsMxBean.getTotalSwapSpaceSize();
    }

    Map<String, Long> diskSpaceInBytesByRootPaths = new HashMap<>();
    for (File rootFile : File.listRoots()) {
        diskSpaceInBytesByRootPaths.put(rootFile.getAbsolutePath(), rootFile.getTotalSpace());
    }
    operatingSystemInfo.diskSpaceInBytesByRootPaths = diskSpaceInBytesByRootPaths;

    return operatingSystemInfo;
}

From source file:api.Status.java

public static Result getMeta() {
    ObjectNode meta = Json.newObject();//from  w w  w .ja  va 2 s  .  com

    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    meta.put("osName", os.getName());
    meta.put("osVersion", os.getVersion());
    meta.put("arch", os.getArch());
    meta.put("cpus", os.getAvailableProcessors());

    return ok(meta.toString());
}