Java Code Examples for java.lang.management.ThreadMXBean#isCurrentThreadCpuTimeSupported()

The following examples show how to use java.lang.management.ThreadMXBean#isCurrentThreadCpuTimeSupported() . 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: JProf.java    From cpsolver with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Enable / disable the thread CPU timing, if needed */
private synchronized static void init() {
    if (sInitialized) return;
    sMode = Mode.valueOf(System.getProperty("jprof", sMode.name()));
    if (sMode != Mode.wall) {
        try {
            ThreadMXBean bean = ManagementFactory.getThreadMXBean();
            if (!bean.isCurrentThreadCpuTimeSupported()) {
                Logger.getLogger(JProf.class).warn("Measuring " + sMode.name() + " time is not supported, falling back to wall time.");
                sMode = Mode.wall;
            }
            if (!bean.isThreadCpuTimeEnabled())
                bean.setThreadCpuTimeEnabled(true);
        } catch (UnsupportedOperationException e) {
            Logger.getLogger(JProf.class).error("Unable to measure " + sMode.name() + " time, falling back to wall time: " + e.getMessage());
            sMode = Mode.wall;
            sMode = Mode.wall;
        }
    }
    Logger.getLogger(JProf.class).info("Using " + sMode.name() + " time.");
    sInitialized = true;
}
 
Example 2
Source File: OdaTiming.java    From OpenDA with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Get CPU time in nanoseconds. */

private long getCpuTime( ) {
	ThreadMXBean bean;
	try {
		bean = ManagementFactory.getThreadMXBean( );
	} catch (Error e) {
		// not supported (happens in the by means of IKVM converted version (.net)
		return 0L;
	}
	return bean.isCurrentThreadCpuTimeSupported( ) ?
        bean.getCurrentThreadCpuTime( ) : 0L;
}
 
Example 3
Source File: CpuClock.java    From javasimon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
CpuClock() {
	ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
	if (mxBean.isCurrentThreadCpuTimeSupported()) {
		threadMXBean = mxBean;
	} else {
		threadMXBean = null;
	}
}
 
Example 4
Source File: Timing.java    From kanzi with Apache License 2.0 5 votes vote down vote up
public long getSystemTime()
{
   ThreadMXBean bean = ManagementFactory.getThreadMXBean();

   return bean.isCurrentThreadCpuTimeSupported() ? 
      (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime()) : 0L;
}
 
Example 5
Source File: TestTenantContainer.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public long getCpuTime () {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported() ? bean
            .getCurrentThreadCpuTime() : 0L;
}
 
Example 6
Source File: Timing.java    From kanzi with Apache License 2.0 4 votes vote down vote up
public long getUserTime()
{
   ThreadMXBean bean = ManagementFactory.getThreadMXBean();
   return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadUserTime() : 0L;
}
 
Example 7
Source File: KillAllChildProcessTaskTest.java    From gocd with Apache License 2.0 4 votes vote down vote up
public long getSystemTime() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported() ?
            (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime()) : 0L;
}
 
Example 8
Source File: KillAllChildProcessTaskBuilderTest.java    From gocd with Apache License 2.0 4 votes vote down vote up
public long getSystemTime() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported() ?
            (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime()) : 0L;
}
 
Example 9
Source File: Timing.java    From oopsla15-artifact with Eclipse Public License 1.0 4 votes vote down vote up
public static long getCpuTime( ) {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
    return bean.isCurrentThreadCpuTimeSupported( ) ?
        bean.getCurrentThreadCpuTime( ) : 0L;
}
 
Example 10
Source File: MXBeanInteropTest1.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- ThreadMXBean") ;

    try {
        ObjectName threadName =
                new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;
        MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);
        errorCount += checkNonEmpty(mbInfo);
        System.out.println("getMBeanInfo\t\t" + mbInfo);
        ThreadMXBean thread = null ;

        thread =
                JMX.newMXBeanProxy(mbsc,
                threadName,
                ThreadMXBean.class) ;
        System.out.println("findMonitorDeadlockedThreads\t\t"
                + thread.findMonitorDeadlockedThreads());
        long[] threadIDs = thread.getAllThreadIds() ;
        System.out.println("getAllThreadIds\t\t"
                + threadIDs);

        for ( long threadID : threadIDs ) {
            System.out.println("getThreadInfo long\t\t"
                    + thread.getThreadInfo(threadID));
            System.out.println("getThreadInfo long, int\t\t"
                    + thread.getThreadInfo(threadID, 2));
        }

        System.out.println("getThreadInfo long[]\t\t"
                + thread.getThreadInfo(threadIDs));
        System.out.println("getThreadInfo long[], int\t\t"
                + thread.getThreadInfo(threadIDs, 2));
        System.out.println("getDaemonThreadCount\t\t"
                + thread.getDaemonThreadCount());
        System.out.println("getPeakThreadCount\t\t"
                + thread.getPeakThreadCount());
        System.out.println("getThreadCount\t\t"
                + thread.getThreadCount());
        System.out.println("getTotalStartedThreadCount\t\t"
                + thread.getTotalStartedThreadCount());
        boolean supported = thread.isThreadContentionMonitoringSupported() ;
        System.out.println("isThreadContentionMonitoringSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadContentionMonitoringEnabled\t\t"
                    + thread.isThreadContentionMonitoringEnabled());
        }

        supported = thread.isThreadCpuTimeSupported() ;
        System.out.println("isThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadCpuTimeEnabled\t\t"
                    + thread.isThreadCpuTimeEnabled());

            for (long id : threadIDs) {
                System.out.println("getThreadCpuTime(" + id + ")\t\t"
                        + thread.getThreadCpuTime(id));
                System.out.println("getThreadUserTime(" + id + ")\t\t"
                        + thread.getThreadUserTime(id));
            }
        }

        supported = thread.isCurrentThreadCpuTimeSupported() ;
        System.out.println("isCurrentThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("getCurrentThreadCpuTime\t\t"
                    + thread.getCurrentThreadCpuTime());
            System.out.println("getCurrentThreadUserTime\t\t"
                    + thread.getCurrentThreadUserTime());
        }

        thread.resetPeakThreadCount() ;

        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}
 
Example 11
Source File: EcdsaTest.java    From wycheproof with Apache License 2.0 4 votes vote down vote up
/**
 * Tests for a potential timing attack. This test checks if there is a correlation between the
 * timing of signature generation and the size of the one-time key k. This is for example the case
 * if a double and add method is used for the point multiplication. The test fails if such a
 * correlation can be shown with high confidence. Further analysis will be necessary to determine
 * how easy it is to exploit the bias in a timing attack.
 */
// TODO(bleichen): Determine if there are exploitable providers.
//
// SunEC currently fails this test. Since ECDSA typically is used with EC groups whose order
// is 224 bits or larger, it is unclear whether the same attacks that apply to DSA are practical.
//
// The ECDSA implementation in BouncyCastle leaks information about k through timing too.
// The test has not been optimized to detect this bias. It would require about 5'000'000 samples,
// which is too much for a simple unit test.
//
// BouncyCastle uses FixedPointCombMultiplier for ECDSA. This is a method using
// precomputation. The implementation is not constant time, since the precomputation table
// contains the point at infinity and adding this point is faster than ordinary point additions.
// The timing leak only has a small correlation to the size of k and at the moment it is is very
// unclear if the can be exploited. (Randomizing the precomputation table by adding the same
// random point to each element in the table and precomputing the necessary offset to undo the
// precomputation seems much easier than analyzing this.)
public void testTiming(String algorithm, String curve, ECParameterSpec ecParams)
    throws Exception {
  ThreadMXBean bean = ManagementFactory.getThreadMXBean();
  if (!bean.isCurrentThreadCpuTimeSupported()) {
    System.out.println("getCurrentThreadCpuTime is not supported. Skipping");
    return;
  }
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
  try {
    keyGen.initialize(ecParams);
  } catch (InvalidAlgorithmParameterException ex) {
    System.out.println("This provider does not support curve:" + curve);
    return;
  }
  KeyPair keyPair = keyGen.generateKeyPair();
  ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();

  String message = "Hello";
  String hashAlgorithm = getHashAlgorithm(algorithm);
  byte[] messageBytes = message.getBytes("UTF-8");
  byte[] digest = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
  BigInteger h = new BigInteger(1, digest);
  Signature signer = Signature.getInstance(algorithm);
  signer.initSign(priv);
  // The number of samples used for the test. This number is a bit low.
  // I.e. it just barely detects that SunEC leaks information about the size of k.
  int samples = 50000;
  long[] timing = new long[samples];
  BigInteger[] k = new BigInteger[samples];
  for (int i = 0; i < samples; i++) {
    long start = bean.getCurrentThreadCpuTime();
    signer.update(messageBytes);
    byte[] signature = signer.sign();
    timing[i] = bean.getCurrentThreadCpuTime() - start;
    k[i] = extractK(signature, h, priv);
  }
  long[] sorted = Arrays.copyOf(timing, timing.length);
  Arrays.sort(sorted);
  double n = priv.getParams().getOrder().doubleValue();
  double expectedAverage = n / 2;
  double maxSigma = 0;
  System.out.println("testTiming algorithm:" + algorithm);
  for (int idx = samples - 1; idx > 10; idx /= 2) {
    long cutoff = sorted[idx];
    int count = 0;
    BigInteger total = BigInteger.ZERO;
    for (int i = 0; i < samples; i++) {
      if (timing[i] <= cutoff) {
        total = total.add(k[i]);
        count += 1;
      }
    }
    double expectedStdDev = n / Math.sqrt(12 * count);
    double average = total.doubleValue() / count;
    // Number of standard deviations that the average is away from
    // the expected value:
    double sigmas = Math.abs(expectedAverage - average) / expectedStdDev;
    if (sigmas > maxSigma) {
      maxSigma = sigmas;
    }
    System.out.println(
        "count:"
            + count
            + " cutoff:"
            + cutoff
            + " relative average:"
            + (average / expectedAverage)
            + " sigmas:"
            + sigmas);
  }
  // Checks if the signatures with a small timing have a biased k.
  // We use 7 standard deviations, so that the probability of a false positive is smaller
  // than 10^{-10}.
  if (maxSigma >= 7) {
    fail("Signatures with short timing have a biased k");
  }
}
 
Example 12
Source File: MXBeanInteropTest1.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- ThreadMXBean") ;

    try {
        ObjectName threadName =
                new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;
        MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);
        errorCount += checkNonEmpty(mbInfo);
        System.out.println("getMBeanInfo\t\t" + mbInfo);
        ThreadMXBean thread = null ;

        thread =
                JMX.newMXBeanProxy(mbsc,
                threadName,
                ThreadMXBean.class) ;
        System.out.println("findMonitorDeadlockedThreads\t\t"
                + thread.findMonitorDeadlockedThreads());
        long[] threadIDs = thread.getAllThreadIds() ;
        System.out.println("getAllThreadIds\t\t"
                + threadIDs);

        for ( long threadID : threadIDs ) {
            System.out.println("getThreadInfo long\t\t"
                    + thread.getThreadInfo(threadID));
            System.out.println("getThreadInfo long, int\t\t"
                    + thread.getThreadInfo(threadID, 2));
        }

        System.out.println("getThreadInfo long[]\t\t"
                + thread.getThreadInfo(threadIDs));
        System.out.println("getThreadInfo long[], int\t\t"
                + thread.getThreadInfo(threadIDs, 2));
        System.out.println("getDaemonThreadCount\t\t"
                + thread.getDaemonThreadCount());
        System.out.println("getPeakThreadCount\t\t"
                + thread.getPeakThreadCount());
        System.out.println("getThreadCount\t\t"
                + thread.getThreadCount());
        System.out.println("getTotalStartedThreadCount\t\t"
                + thread.getTotalStartedThreadCount());
        boolean supported = thread.isThreadContentionMonitoringSupported() ;
        System.out.println("isThreadContentionMonitoringSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadContentionMonitoringEnabled\t\t"
                    + thread.isThreadContentionMonitoringEnabled());
        }

        supported = thread.isThreadCpuTimeSupported() ;
        System.out.println("isThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadCpuTimeEnabled\t\t"
                    + thread.isThreadCpuTimeEnabled());

            for (long id : threadIDs) {
                System.out.println("getThreadCpuTime(" + id + ")\t\t"
                        + thread.getThreadCpuTime(id));
                System.out.println("getThreadUserTime(" + id + ")\t\t"
                        + thread.getThreadUserTime(id));
            }
        }

        supported = thread.isCurrentThreadCpuTimeSupported() ;
        System.out.println("isCurrentThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("getCurrentThreadCpuTime\t\t"
                    + thread.getCurrentThreadCpuTime());
            System.out.println("getCurrentThreadUserTime\t\t"
                    + thread.getCurrentThreadUserTime());
        }

        thread.resetPeakThreadCount() ;

        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}
 
Example 13
Source File: MXBeanInteropTest1.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- ThreadMXBean") ;

    try {
        ObjectName threadName =
                new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;
        MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);
        errorCount += checkNonEmpty(mbInfo);
        System.out.println("getMBeanInfo\t\t" + mbInfo);
        ThreadMXBean thread = null ;

        thread =
                JMX.newMXBeanProxy(mbsc,
                threadName,
                ThreadMXBean.class) ;
        System.out.println("findMonitorDeadlockedThreads\t\t"
                + thread.findMonitorDeadlockedThreads());
        long[] threadIDs = thread.getAllThreadIds() ;
        System.out.println("getAllThreadIds\t\t"
                + threadIDs);

        for ( long threadID : threadIDs ) {
            System.out.println("getThreadInfo long\t\t"
                    + thread.getThreadInfo(threadID));
            System.out.println("getThreadInfo long, int\t\t"
                    + thread.getThreadInfo(threadID, 2));
        }

        System.out.println("getThreadInfo long[]\t\t"
                + thread.getThreadInfo(threadIDs));
        System.out.println("getThreadInfo long[], int\t\t"
                + thread.getThreadInfo(threadIDs, 2));
        System.out.println("getDaemonThreadCount\t\t"
                + thread.getDaemonThreadCount());
        System.out.println("getPeakThreadCount\t\t"
                + thread.getPeakThreadCount());
        System.out.println("getThreadCount\t\t"
                + thread.getThreadCount());
        System.out.println("getTotalStartedThreadCount\t\t"
                + thread.getTotalStartedThreadCount());
        boolean supported = thread.isThreadContentionMonitoringSupported() ;
        System.out.println("isThreadContentionMonitoringSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadContentionMonitoringEnabled\t\t"
                    + thread.isThreadContentionMonitoringEnabled());
        }

        supported = thread.isThreadCpuTimeSupported() ;
        System.out.println("isThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadCpuTimeEnabled\t\t"
                    + thread.isThreadCpuTimeEnabled());

            for (long id : threadIDs) {
                System.out.println("getThreadCpuTime(" + id + ")\t\t"
                        + thread.getThreadCpuTime(id));
                System.out.println("getThreadUserTime(" + id + ")\t\t"
                        + thread.getThreadUserTime(id));
            }
        }

        supported = thread.isCurrentThreadCpuTimeSupported() ;
        System.out.println("isCurrentThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("getCurrentThreadCpuTime\t\t"
                    + thread.getCurrentThreadCpuTime());
            System.out.println("getCurrentThreadUserTime\t\t"
                    + thread.getCurrentThreadUserTime());
        }

        thread.resetPeakThreadCount() ;

        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}
 
Example 14
Source File: MXBeanInteropTest1.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- ThreadMXBean") ;

    try {
        ObjectName threadName =
                new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;
        MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);
        errorCount += checkNonEmpty(mbInfo);
        System.out.println("getMBeanInfo\t\t" + mbInfo);
        ThreadMXBean thread = null ;

        thread =
                JMX.newMXBeanProxy(mbsc,
                threadName,
                ThreadMXBean.class) ;
        System.out.println("findMonitorDeadlockedThreads\t\t"
                + thread.findMonitorDeadlockedThreads());
        long[] threadIDs = thread.getAllThreadIds() ;
        System.out.println("getAllThreadIds\t\t"
                + threadIDs);

        for ( long threadID : threadIDs ) {
            System.out.println("getThreadInfo long\t\t"
                    + thread.getThreadInfo(threadID));
            System.out.println("getThreadInfo long, int\t\t"
                    + thread.getThreadInfo(threadID, 2));
        }

        System.out.println("getThreadInfo long[]\t\t"
                + thread.getThreadInfo(threadIDs));
        System.out.println("getThreadInfo long[], int\t\t"
                + thread.getThreadInfo(threadIDs, 2));
        System.out.println("getDaemonThreadCount\t\t"
                + thread.getDaemonThreadCount());
        System.out.println("getPeakThreadCount\t\t"
                + thread.getPeakThreadCount());
        System.out.println("getThreadCount\t\t"
                + thread.getThreadCount());
        System.out.println("getTotalStartedThreadCount\t\t"
                + thread.getTotalStartedThreadCount());
        boolean supported = thread.isThreadContentionMonitoringSupported() ;
        System.out.println("isThreadContentionMonitoringSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadContentionMonitoringEnabled\t\t"
                    + thread.isThreadContentionMonitoringEnabled());
        }

        supported = thread.isThreadCpuTimeSupported() ;
        System.out.println("isThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadCpuTimeEnabled\t\t"
                    + thread.isThreadCpuTimeEnabled());

            for (long id : threadIDs) {
                System.out.println("getThreadCpuTime(" + id + ")\t\t"
                        + thread.getThreadCpuTime(id));
                System.out.println("getThreadUserTime(" + id + ")\t\t"
                        + thread.getThreadUserTime(id));
            }
        }

        supported = thread.isCurrentThreadCpuTimeSupported() ;
        System.out.println("isCurrentThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("getCurrentThreadCpuTime\t\t"
                    + thread.getCurrentThreadCpuTime());
            System.out.println("getCurrentThreadUserTime\t\t"
                    + thread.getCurrentThreadUserTime());
        }

        thread.resetPeakThreadCount() ;

        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}
 
Example 15
Source File: MXBeanInteropTest1.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- ThreadMXBean") ;

    try {
        ObjectName threadName =
                new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;
        MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);
        errorCount += checkNonEmpty(mbInfo);
        System.out.println("getMBeanInfo\t\t" + mbInfo);
        ThreadMXBean thread = null ;

        thread =
                JMX.newMXBeanProxy(mbsc,
                threadName,
                ThreadMXBean.class) ;
        System.out.println("findMonitorDeadlockedThreads\t\t"
                + thread.findMonitorDeadlockedThreads());
        long[] threadIDs = thread.getAllThreadIds() ;
        System.out.println("getAllThreadIds\t\t"
                + threadIDs);

        for ( long threadID : threadIDs ) {
            System.out.println("getThreadInfo long\t\t"
                    + thread.getThreadInfo(threadID));
            System.out.println("getThreadInfo long, int\t\t"
                    + thread.getThreadInfo(threadID, 2));
        }

        System.out.println("getThreadInfo long[]\t\t"
                + thread.getThreadInfo(threadIDs));
        System.out.println("getThreadInfo long[], int\t\t"
                + thread.getThreadInfo(threadIDs, 2));
        System.out.println("getDaemonThreadCount\t\t"
                + thread.getDaemonThreadCount());
        System.out.println("getPeakThreadCount\t\t"
                + thread.getPeakThreadCount());
        System.out.println("getThreadCount\t\t"
                + thread.getThreadCount());
        System.out.println("getTotalStartedThreadCount\t\t"
                + thread.getTotalStartedThreadCount());
        boolean supported = thread.isThreadContentionMonitoringSupported() ;
        System.out.println("isThreadContentionMonitoringSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadContentionMonitoringEnabled\t\t"
                    + thread.isThreadContentionMonitoringEnabled());
        }

        supported = thread.isThreadCpuTimeSupported() ;
        System.out.println("isThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadCpuTimeEnabled\t\t"
                    + thread.isThreadCpuTimeEnabled());

            for (long id : threadIDs) {
                System.out.println("getThreadCpuTime(" + id + ")\t\t"
                        + thread.getThreadCpuTime(id));
                System.out.println("getThreadUserTime(" + id + ")\t\t"
                        + thread.getThreadUserTime(id));
            }
        }

        supported = thread.isCurrentThreadCpuTimeSupported() ;
        System.out.println("isCurrentThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("getCurrentThreadCpuTime\t\t"
                    + thread.getCurrentThreadCpuTime());
            System.out.println("getCurrentThreadUserTime\t\t"
                    + thread.getCurrentThreadUserTime());
        }

        thread.resetPeakThreadCount() ;

        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}
 
Example 16
Source File: MXBeanInteropTest1.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- ThreadMXBean") ;

    try {
        ObjectName threadName =
                new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;
        MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);
        errorCount += checkNonEmpty(mbInfo);
        System.out.println("getMBeanInfo\t\t" + mbInfo);
        ThreadMXBean thread = null ;

        thread =
                JMX.newMXBeanProxy(mbsc,
                threadName,
                ThreadMXBean.class) ;
        System.out.println("findMonitorDeadlockedThreads\t\t"
                + thread.findMonitorDeadlockedThreads());
        long[] threadIDs = thread.getAllThreadIds() ;
        System.out.println("getAllThreadIds\t\t"
                + threadIDs);

        for ( long threadID : threadIDs ) {
            System.out.println("getThreadInfo long\t\t"
                    + thread.getThreadInfo(threadID));
            System.out.println("getThreadInfo long, int\t\t"
                    + thread.getThreadInfo(threadID, 2));
        }

        System.out.println("getThreadInfo long[]\t\t"
                + thread.getThreadInfo(threadIDs));
        System.out.println("getThreadInfo long[], int\t\t"
                + thread.getThreadInfo(threadIDs, 2));
        System.out.println("getDaemonThreadCount\t\t"
                + thread.getDaemonThreadCount());
        System.out.println("getPeakThreadCount\t\t"
                + thread.getPeakThreadCount());
        System.out.println("getThreadCount\t\t"
                + thread.getThreadCount());
        System.out.println("getTotalStartedThreadCount\t\t"
                + thread.getTotalStartedThreadCount());
        boolean supported = thread.isThreadContentionMonitoringSupported() ;
        System.out.println("isThreadContentionMonitoringSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadContentionMonitoringEnabled\t\t"
                    + thread.isThreadContentionMonitoringEnabled());
        }

        supported = thread.isThreadCpuTimeSupported() ;
        System.out.println("isThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadCpuTimeEnabled\t\t"
                    + thread.isThreadCpuTimeEnabled());

            for (long id : threadIDs) {
                System.out.println("getThreadCpuTime(" + id + ")\t\t"
                        + thread.getThreadCpuTime(id));
                System.out.println("getThreadUserTime(" + id + ")\t\t"
                        + thread.getThreadUserTime(id));
            }
        }

        supported = thread.isCurrentThreadCpuTimeSupported() ;
        System.out.println("isCurrentThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("getCurrentThreadCpuTime\t\t"
                    + thread.getCurrentThreadCpuTime());
            System.out.println("getCurrentThreadUserTime\t\t"
                    + thread.getCurrentThreadUserTime());
        }

        thread.resetPeakThreadCount() ;

        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}
 
Example 17
Source File: MXBeanInteropTest1.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {
    int errorCount = 0 ;
    System.out.println("---- ThreadMXBean") ;

    try {
        ObjectName threadName =
                new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;
        MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);
        errorCount += checkNonEmpty(mbInfo);
        System.out.println("getMBeanInfo\t\t" + mbInfo);
        ThreadMXBean thread = null ;

        thread =
                JMX.newMXBeanProxy(mbsc,
                threadName,
                ThreadMXBean.class) ;
        System.out.println("findMonitorDeadlockedThreads\t\t"
                + thread.findMonitorDeadlockedThreads());
        long[] threadIDs = thread.getAllThreadIds() ;
        System.out.println("getAllThreadIds\t\t"
                + threadIDs);

        for ( long threadID : threadIDs ) {
            System.out.println("getThreadInfo long\t\t"
                    + thread.getThreadInfo(threadID));
            System.out.println("getThreadInfo long, int\t\t"
                    + thread.getThreadInfo(threadID, 2));
        }

        System.out.println("getThreadInfo long[]\t\t"
                + thread.getThreadInfo(threadIDs));
        System.out.println("getThreadInfo long[], int\t\t"
                + thread.getThreadInfo(threadIDs, 2));
        System.out.println("getDaemonThreadCount\t\t"
                + thread.getDaemonThreadCount());
        System.out.println("getPeakThreadCount\t\t"
                + thread.getPeakThreadCount());
        System.out.println("getThreadCount\t\t"
                + thread.getThreadCount());
        System.out.println("getTotalStartedThreadCount\t\t"
                + thread.getTotalStartedThreadCount());
        boolean supported = thread.isThreadContentionMonitoringSupported() ;
        System.out.println("isThreadContentionMonitoringSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadContentionMonitoringEnabled\t\t"
                    + thread.isThreadContentionMonitoringEnabled());
        }

        supported = thread.isThreadCpuTimeSupported() ;
        System.out.println("isThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("isThreadCpuTimeEnabled\t\t"
                    + thread.isThreadCpuTimeEnabled());

            for (long id : threadIDs) {
                System.out.println("getThreadCpuTime(" + id + ")\t\t"
                        + thread.getThreadCpuTime(id));
                System.out.println("getThreadUserTime(" + id + ")\t\t"
                        + thread.getThreadUserTime(id));
            }
        }

        supported = thread.isCurrentThreadCpuTimeSupported() ;
        System.out.println("isCurrentThreadCpuTimeSupported\t\t"
                + supported);

        if ( supported ) {
            System.out.println("getCurrentThreadCpuTime\t\t"
                    + thread.getCurrentThreadCpuTime());
            System.out.println("getCurrentThreadUserTime\t\t"
                    + thread.getCurrentThreadUserTime());
        }

        thread.resetPeakThreadCount() ;

        System.out.println("---- OK\n") ;
    } catch (Exception e) {
        Utils.printThrowable(e, true) ;
        errorCount++ ;
        System.out.println("---- ERROR\n") ;
    }

    return errorCount ;
}
 
Example 18
Source File: GeneralUtils.java    From ade with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the current thread's CPU time.
 * 
 * @return the current thread's CPU time, or 0 if CPU time is not supported
 *     for the current thread
 */
public static long getCpuTime() {
    final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadCpuTime() : 0L;
}
 
Example 19
Source File: GeneralUtils.java    From ade with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the current thread's user time.
 * 
 * @return the current thread's user time, or 0 if user time is not supported
 *     for the current thread
 */
public static long getUserTime() {
    final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadUserTime() : 0L;
}
 
Example 20
Source File: GeneralUtils.java    From ade with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the difference between the current thread's CPU time and user time.
 * 
 * @return the current thread's system time, or 0 if CPU time is not supported
 *     for the current thread
 */
public static long getSystemTime() {
    final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    final long systemTime = bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime();
    return bean.isCurrentThreadCpuTimeSupported() ? systemTime : 0L;
}