Example usage for java.lang.management ThreadMXBean isThreadCpuTimeEnabled

List of usage examples for java.lang.management ThreadMXBean isThreadCpuTimeEnabled

Introduction

In this page you can find the example usage for java.lang.management ThreadMXBean isThreadCpuTimeEnabled.

Prototype

public boolean isThreadCpuTimeEnabled();

Source Link

Document

Tests if thread CPU time measurement is enabled.

Usage

From source file:eu.planets_project.tb.gui.backing.exp.ExpTypeMigrate.java

public static void main(String args[]) {

    Properties p = System.getProperties();

    ByteArrayOutputStream byos = new ByteArrayOutputStream();
    try {/*  www. j a  va  2s  .c  o m*/
        p.storeToXML(byos, "Automatically generated for PLANETS Service ", "UTF-8");
        String res = byos.toString("UTF-8");
        System.out.println(res);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // This.
    List<String> pl = new ArrayList<String>();
    for (Object key : p.keySet()) {
        pl.add((String) key);
    }
    Collections.sort(pl);

    //
    for (String key : pl) {
        System.out.println(key + " = " + p.getProperty(key));
    }

    /*
     * http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html#getCurrentThreadCpuTime()
     * 
     * http://www.java-forums.org/new-java/5303-how-determine-cpu-usage-using-java.html
     * 
     */

    ThreadMXBean TMB = ManagementFactory.getThreadMXBean();
    int mscale = 1000000;
    long time = 0, time2 = 0;
    long cput = 0, cput2 = 0;
    double cpuperc = -1;

    //Begin loop.
    for (int i = 0; i < 10; i++) {

        if (TMB.isThreadCpuTimeSupported()) {
            if (!TMB.isThreadCpuTimeEnabled()) {
                TMB.setThreadCpuTimeEnabled(true);
            }

            //                if(new Date().getTime() * mscale - time > 1000000000) //Reset once per second
            //                {
            System.out.println("Resetting...");
            time = System.currentTimeMillis() * mscale;
            cput = TMB.getCurrentThreadCpuTime();
            //                cput = TMB.getCurrentThreadUserTime();
            //                }

        }

        //Do cpu intensive stuff
        for (int k = 0; k < 10; k++) {
            for (int j = 0; j < 100000; j++) {
                double a = Math.pow(i, j);
                double b = a / j + Math.random();
                a = b * Math.random();
                b = a * Math.random();
            }

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        if (TMB.isThreadCpuTimeSupported()) {
            //                if(new Date().getTime() * mscale - time != 0) {
            cput2 = TMB.getCurrentThreadCpuTime();
            System.out.println("cpu: " + (cput2 - cput) / (1000.0 * mscale));
            //                cput2 = TMB.getCurrentThreadUserTime();

            time2 = System.currentTimeMillis() * mscale;
            System.out.println("time: " + (time2 - time) / (1000.0 * mscale));

            cpuperc = 100.0 * (cput2 - cput) / (double) (time2 - time);
            System.out.println("cpu perc = " + cpuperc);
            //                }
        }
        //End Loop
    }
    System.out.println("Done.");
}

From source file:com.espertech.esper.regression.client.MyMetricFunctions.java

public static boolean takeCPUTime(long nanoSecTarget) {
    if (nanoSecTarget < 100) {
        throw new RuntimeException("CPU time wait nsec less then zero, was " + nanoSecTarget);
    }//ww  w. j  a  v a  2s .co  m

    ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
    if (!mbean.isThreadCpuTimeEnabled()) {
        throw new RuntimeException("ThreadMXBean CPU time reporting not enabled");
    }

    long before = mbean.getCurrentThreadCpuTime();

    while (true) {
        long after = mbean.getCurrentThreadCpuTime();
        long spent = after - before;
        if (spent > nanoSecTarget) {
            break;
        }
    }

    return true;
}

From source file:net.bull.javamelody.internal.model.JavaInformations.java

public static List<ThreadInformations> buildThreadInformationsList() {
    final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    final Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
    final List<Thread> threads = new ArrayList<Thread>(stackTraces.keySet());

    // si "1.6.0_01".compareTo(Parameters.JAVA_VERSION) > 0;
    // on rcuprait les threads sans stack trace en contournant bug 6434648 avant 1.6.0_01
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6434648
    // hormis pour le thread courant qui obtient sa stack trace diffremment sans le bug
    //      threads = getThreadsFromThreadGroups();
    //      final Thread currentThread = Thread.currentThread();
    //      stackTraces = Collections.singletonMap(currentThread, currentThread.getStackTrace());

    final boolean cpuTimeEnabled = threadBean.isThreadCpuTimeSupported() && threadBean.isThreadCpuTimeEnabled();
    final long[] deadlockedThreads = getDeadlockedThreads(threadBean);
    final List<ThreadInformations> threadInfosList = new ArrayList<ThreadInformations>(threads.size());
    // hostAddress rcupr ici car il peut y avoir plus de 20000 threads
    final String hostAddress = Parameters.getHostAddress();
    for (final Thread thread : threads) {
        final StackTraceElement[] stackTraceElements = stackTraces.get(thread);
        final List<StackTraceElement> stackTraceElementList = stackTraceElements == null ? null
                : new ArrayList<StackTraceElement>(Arrays.asList(stackTraceElements));
        final long cpuTimeMillis;
        final long userTimeMillis;
        if (cpuTimeEnabled) {
            cpuTimeMillis = threadBean.getThreadCpuTime(thread.getId()) / 1000000;
            userTimeMillis = threadBean.getThreadUserTime(thread.getId()) / 1000000;
        } else {/*from  ww w  .  jav  a  2 s .co m*/
            cpuTimeMillis = -1;
            userTimeMillis = -1;
        }
        final boolean deadlocked = deadlockedThreads != null
                && Arrays.binarySearch(deadlockedThreads, thread.getId()) >= 0;
        // stackTraceElementList est une ArrayList et non unmodifiableList pour lisibilit xml
        threadInfosList.add(new ThreadInformations(thread, stackTraceElementList, cpuTimeMillis, userTimeMillis,
                deadlocked, hostAddress));
    }
    // on retourne ArrayList et non unmodifiableList pour lisibilit du xml par xstream
    return threadInfosList;
}