Example usage for java.lang.management RuntimeMXBean getUptime

List of usage examples for java.lang.management RuntimeMXBean getUptime

Introduction

In this page you can find the example usage for java.lang.management RuntimeMXBean getUptime.

Prototype

public long getUptime();

Source Link

Document

Returns the uptime of the Java virtual machine in milliseconds.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
    System.out.println(mx.getUptime() + " ms");

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
    System.out.println(mx.getBootClassPath());
    System.out.println(mx.getClassPath());

    System.out.println(mx.getInputArguments());
    System.out.println(mx.getSystemProperties());

    System.out.println(new Date(mx.getStartTime()));
    System.out.println(mx.getUptime() + " ms");

}

From source file:gridool.util.system.SystemUtils.java

/** 
 * @return uptime in msec//from w  ww .j  av  a2 s.  c  o m
 */
public static long getUptime() {
    final RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
    return mx.getUptime();
}

From source file:com.android.tools.idea.diagnostics.crash.GoogleCrash.java

@NotNull
private static Map<String, String> getDefaultParameters() {
    Map<String, String> map = new HashMap<>();
    ApplicationInfo applicationInfo = getApplicationInfo();

    if (ANONYMIZED_UID != null) {
        map.put("guid", ANONYMIZED_UID);
    }//from ww w  . j  av  a 2 s  . c  o m
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    map.put("ptime", Long.toString(runtimeMXBean.getUptime()));

    // product specific key value pairs
    map.put(KEY_VERSION, applicationInfo == null ? "0.0.0.0" : applicationInfo.getStrictVersion());
    map.put(KEY_PRODUCT_ID, CrashReport.PRODUCT_ANDROID_STUDIO); // must match registration with Crash
    map.put("fullVersion", applicationInfo == null ? "0.0.0.0" : applicationInfo.getFullVersion());

    map.put("osName", StringUtil.notNullize(SystemInfo.OS_NAME));
    map.put("osVersion", StringUtil.notNullize(SystemInfo.OS_VERSION));
    map.put("osArch", StringUtil.notNullize(SystemInfo.OS_ARCH));
    map.put("locale", StringUtil.notNullize(LOCALE));

    map.put("vmName", StringUtil.notNullize(runtimeMXBean.getVmName()));
    map.put("vmVendor", StringUtil.notNullize(runtimeMXBean.getVmVendor()));
    map.put("vmVersion", StringUtil.notNullize(runtimeMXBean.getVmVersion()));

    MemoryUsage usage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    map.put("heapUsed", Long.toString(usage.getUsed()));
    map.put("heapCommitted", Long.toString(usage.getCommitted()));
    map.put("heapMax", Long.toString(usage.getMax()));

    return map;
}

From source file:io.ecarf.core.utils.Utils.java

/**
 * Log the CPU stats/* w w w.  j  a  v a2s .c om*/
 */
public static void logCpuStats() {
    OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
    long upTime = runtimeMXBean.getUptime();
    double loadAverage = operatingSystemMXBean.getSystemLoadAverage();
    log.info("Available processors: " + availableProcessors);
    log.info("JVM upTime (ms): " + upTime);
    log.info("CPU load average: " + loadAverage);
}

From source file:com.searchbox.framework.web.SystemController.java

@ModelAttribute("jvmInfo")
public static Map<String, Object> getJvmInfo() {
    Map<String, Object> jvm = new HashMap<String, Object>();

    final String javaVersion = System.getProperty("java.specification.version", "unknown");
    final String javaVendor = System.getProperty("java.specification.vendor", "unknown");
    final String javaName = System.getProperty("java.specification.name", "unknown");
    final String jreVersion = System.getProperty("java.version", "unknown");
    final String jreVendor = System.getProperty("java.vendor", "unknown");
    final String vmVersion = System.getProperty("java.vm.version", "unknown");
    final String vmVendor = System.getProperty("java.vm.vendor", "unknown");
    final String vmName = System.getProperty("java.vm.name", "unknown");

    // Summary Info
    jvm.put("version", jreVersion + " " + vmVersion);
    jvm.put("name", jreVendor + " " + vmName);

    // details/*  w  w w.  ja v  a 2 s .c o m*/
    Map<String, Object> java = new HashMap<String, Object>();
    java.put("vendor", javaVendor);
    java.put("name", javaName);
    java.put("version", javaVersion);
    jvm.put("spec", java);
    Map<String, Object> jre = new HashMap<String, Object>();
    jre.put("vendor", jreVendor);
    jre.put("version", jreVersion);
    jvm.put("jre", jre);
    Map<String, Object> vm = new HashMap<String, Object>();
    vm.put("vendor", vmVendor);
    vm.put("name", vmName);
    vm.put("version", vmVersion);
    jvm.put("vm", vm);

    Runtime runtime = Runtime.getRuntime();
    jvm.put("processors", runtime.availableProcessors());

    // not thread safe, but could be thread local
    DecimalFormat df = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance(Locale.ROOT));

    Map<String, Object> mem = new HashMap<String, Object>();
    Map<String, Object> raw = new HashMap<String, Object>();
    long free = runtime.freeMemory();
    long max = runtime.maxMemory();
    long total = runtime.totalMemory();
    long used = total - free;
    double percentUsed = ((double) (used) / (double) max) * 100;
    raw.put("free", free);
    mem.put("free", humanReadableUnits(free, df));
    raw.put("total", total);
    mem.put("total", humanReadableUnits(total, df));
    raw.put("max", max);
    mem.put("max", humanReadableUnits(max, df));
    raw.put("used", used);
    mem.put("used", humanReadableUnits(used, df) + " (%" + df.format(percentUsed) + ")");
    raw.put("used%", percentUsed);

    mem.put("raw", raw);
    jvm.put("memory", mem);

    // JMX properties -- probably should be moved to a different handler
    Map<String, Object> jmx = new HashMap<String, Object>();
    try {
        RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
        jmx.put("bootclasspath", mx.getBootClassPath());
        jmx.put("classpath", mx.getClassPath());

        // the input arguments passed to the Java virtual machine
        // which does not include the arguments to the main method.
        jmx.put("commandLineArgs", mx.getInputArguments());

        jmx.put("startTime", new Date(mx.getStartTime()));
        jmx.put("upTimeMS", mx.getUptime());

    } catch (Exception e) {
        LOGGER.warn("Error getting JMX properties", e);
    }
    jvm.put("jmx", jmx);
    return jvm;
}

From source file:de.unisb.cs.st.javalanche.mutation.runtime.jmx.MutationMxClient.java

public static boolean connect(int i) {
    JMXConnector jmxc = null;// ww w.jav  a  2  s  . c o  m
    JMXServiceURL url = null;

    try {
        url = new JMXServiceURL(MXBeanRegisterer.ADDRESS + i);
        jmxc = JMXConnectorFactory.connect(url, null);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        return false;
        // System.out.println("Could not connect to address: " + url);
        // e.printStackTrace();
    }
    if (jmxc != null) {
        try {
            MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
            ObjectName objectName = new ObjectName(MXBeanRegisterer.OBJECT_NAME);
            Object numberOfMutations = mbsc.getAttribute(objectName, "NumberOfMutations");
            Object currentTest = mbsc.getAttribute(objectName, "CurrentTest");
            Object currentMutation = mbsc.getAttribute(objectName, "CurrentMutation");
            Object allMutations = mbsc.getAttribute(objectName, "Mutations");
            Object mutationsDuration = mbsc.getAttribute(objectName, "MutationDuration");
            Object testDuration = mbsc.getAttribute(objectName, "TestDuration");
            //            Object mutationSummary = mbsc.getAttribute(objectName,
            //                  "MutationSummary");

            final RuntimeMXBean remoteRuntime = ManagementFactory.newPlatformMXBeanProxy(mbsc,
                    ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);

            final MemoryMXBean remoteMemory = ManagementFactory.newPlatformMXBeanProxy(mbsc,
                    ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
            System.out.print("Connection: " + i + "  ");
            System.out.println("Target VM: " + remoteRuntime.getName() + " - " + remoteRuntime.getVmVendor()
                    + " - " + remoteRuntime.getSpecVersion() + " - " + remoteRuntime.getVmVersion());
            System.out.println(
                    "Running for: " + DurationFormatUtils.formatDurationHMS(remoteRuntime.getUptime()));
            System.out
                    .println("Memory usage: Heap - " + formatMemory(remoteMemory.getHeapMemoryUsage().getUsed())
                            + "  Non Heap - " + formatMemory(remoteMemory.getNonHeapMemoryUsage().getUsed()));

            String mutationDurationFormatted = DurationFormatUtils
                    .formatDurationHMS(Long.parseLong(mutationsDuration.toString()));
            String testDurationFormatted = DurationFormatUtils
                    .formatDurationHMS(Long.parseLong(testDuration.toString()));
            if (DEBUG_ADD) {
                System.out.println("Classpath: " + remoteRuntime.getClassPath());
                System.out.println("Args: " + remoteRuntime.getInputArguments());
                System.out.println("All Mutations: " + allMutations);
            }
            //            System.out.println(mutationSummary);
            System.out.println(
                    "Current mutation (Running for: " + mutationDurationFormatted + "): " + currentMutation);
            System.out.println("Mutations tested: " + numberOfMutations);
            System.out.println("Current test:   (Running for: " + testDurationFormatted + "): " + currentTest);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (AttributeNotFoundException e) {
            e.printStackTrace();
        } catch (InstanceNotFoundException e) {
            e.printStackTrace();
        } catch (MBeanException e) {
            e.printStackTrace();
        } catch (ReflectionException e) {
            e.printStackTrace();
        } finally {
            try {
                jmxc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}

From source file:com.evolveum.midpoint.web.util.WebMiscUtil.java

public static double getSystemLoad() {
    com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    int availableProcessors = operatingSystemMXBean.getAvailableProcessors();
    long prevUpTime = runtimeMXBean.getUptime();
    long prevProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();

    try {//from ww w . j  a v a 2s .c  o m
        Thread.sleep(150);
    } catch (Exception ignored) {
        //ignored
    }

    operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();
    long upTime = runtimeMXBean.getUptime();
    long processCpuTime = operatingSystemMXBean.getProcessCpuTime();
    long elapsedCpu = processCpuTime - prevProcessCpuTime;
    long elapsedTime = upTime - prevUpTime;

    double cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors));

    return cpuUsage;
}

From source file:org.onosproject.ospf.controller.impl.Controller.java

/**
 * get the up time.//  ww w.ja v a 2  s  . c  o m
 *
 * @return rb.getUptime
 */
public Long getUptime() {
    RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
    return rb.getUptime();
}

From source file:org.alfresco.reporting.cron.HarvestingJob.java

public void executeImpl(AtomicBoolean running, JobExecutionContext context) throws JobExecutionException {
    // actually do what needs to be done
    final JobDataMap jobData = context.getJobDetail().getJobDataMap();
    final String frequency = jobData.getString("frequency");

    int startupDelayMillis = 0;
    startupDelayMillis = Integer.parseInt(jobData.getString("startDelayMinutes")) * 60000;

    // wait system i starting
    RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
    logger.info("Uptime: " + rb.getUptime() + ", wait for: " + startupDelayMillis + ", lets go="
            + (startupDelayMillis < rb.getUptime()));

    if (startupDelayMillis < rb.getUptime()) {
        logger.info("--> Go!");
        // we are x minutes after startup of the repository, lets roll!
        AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>() {
            public Void doWork() {
                try {
                    if (frequency != null) {
                        Action action = actionService.createAction(HarvestingExecuter.NAME);
                        if (frequency != null) {
                            action.setParameterValue(HarvestingExecuter.PARAM_FREQUENCY, frequency);
                        } // end if frequency!=null
                        actionService.executeAction(action, null);
                    } // end if frequency!=null
                } catch (Exception e) {
                    // do nothing
                }//from w ww .j  a v a 2  s  . c  o  m
                return null;
            } // end doWork
        }, AuthenticationUtil.getSystemUserName());
    } else {
        logger.info(
                "Hey relax! Its too early to work... Let the repository get up first... Aborting this run.");
    }
}