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

The following examples show how to use java.lang.management.ThreadMXBean#setThreadCpuTimeEnabled() . 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: Sudoku.java    From kodkod with MIT License 6 votes vote down vote up
long[] extract(Proof proof) { 
	final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
	bean.setThreadCpuTimeEnabled(true);
	final ReductionStrategy strategy;
	switch(this) { 
	case RCE : strategy = new AdaptiveRCEStrategy(proof.log()); break;
	case SCE : strategy = new SCEStrategy(proof.log()); break;
	case NCE : strategy = new NCEStrategy(proof.log()); break;
	default : throw new IllegalStateException("Unknown strategy: " + this);
	}
	final long start = bean.getCurrentThreadUserTime();
	proof.minimize(strategy);
	final int minCore = proof.highLevelCore().size();
	final long end = bean.getCurrentThreadUserTime();
	return new long[]{ minCore, toMillis(end-start) };
}
 
Example 3
Source File: OperatorProfiler.java    From rheem with Apache License 2.0 6 votes vote down vote up
/**
 * Executes and profiles the profiling task. Requires that this instance is prepared.
 */
public Result run() {
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    threadMXBean.setThreadCpuTimeEnabled(true);
    ProfilingUtils.sleep(1000);
    long startCpuTime = threadMXBean.getCurrentThreadCpuTime();
    final long outputCardinality = this.executeOperator();
    long endCpuTime = threadMXBean.getCurrentThreadCpuTime();

    long cpuCycles = this.calculateCpuCycles(startCpuTime, endCpuTime);
    return new Result(
            this.inputCardinalities,
            outputCardinality,
            this.provideDiskBytes(),
            this.provideNetworkBytes(),
            cpuCycles
    );
}
 
Example 4
Source File: JvmThreadingImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 5
Source File: JvmThreadingImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 6
Source File: JvmThreadingImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 7
Source File: TrackingLogger.java    From TNT4J with Apache License 2.0 5 votes vote down vote up
/**
 * Check and enable java timing for use by activities
 *
 */
private static void initJavaTiming() {
	ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
	boolean cpuTimingSupported = tmbean.isCurrentThreadCpuTimeSupported();
	if (cpuTimingSupported) {
		tmbean.setThreadCpuTimeEnabled(cpuTimingSupported);
	}
	boolean contTimingSupported = tmbean.isThreadContentionMonitoringSupported();
	if (contTimingSupported) {
		tmbean.setThreadContentionMonitoringEnabled(contTimingSupported);
	}
}
 
Example 8
Source File: Sudoku.java    From kodkod with MIT License 5 votes vote down vote up
long[] extract(Proof proof) { 
	final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
	bean.setThreadCpuTimeEnabled(true);
	final long start = bean.getCurrentThreadUserTime();
	final int initCore = proof.highLevelCore().size();
	final long end = bean.getCurrentThreadUserTime();
	return new long[]{ initCore, toMillis(end-start) };
}
 
Example 9
Source File: JvmThreadingImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 10
Source File: JvmThreadingImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 11
Source File: JvmThreadingImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 12
Source File: JvmThreadingImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 13
Source File: JvmThreadingImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 14
Source File: JvmThreadingImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 15
Source File: CueUtil.java    From OpenCue with Apache License 2.0 5 votes vote down vote up
public static final long getCpuUsage() {
    ThreadMXBean mx = ManagementFactory.getThreadMXBean();
    mx.setThreadCpuTimeEnabled(true);
    long result = 0;
    for (long id: mx.getAllThreadIds()) {
        result = result + mx.getThreadUserTime(id);
    }
    return result;
}
 
Example 16
Source File: ResourceMonitoring.java    From gocd with Apache License 2.0 5 votes vote down vote up
public void enableIfDiagnosticsModeIsEnabled() {
    if (systemEnvironment.get(SystemEnvironment.GO_DIAGNOSTICS_MODE)) {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        if (threadMXBean.isThreadContentionMonitoringSupported()) {
            threadMXBean.setThreadContentionMonitoringEnabled(true);
        }
        if (threadMXBean.isThreadCpuTimeSupported()) {
            threadMXBean.setThreadCpuTimeEnabled(true);
        }

        if (isThreadAllocatedMemorySupported(threadMXBean)) {
            setThreadAllocatedMemoryEnabled(threadMXBean);
        }
    }
}
 
Example 17
Source File: JvmThreadingImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public void setJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x)
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    // We can trust the received value, it has been checked in
    // checkJvmThreadCpuTimeMonitoring
    if(JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue())
        mbean.setThreadCpuTimeEnabled(true);
    else
        mbean.setThreadCpuTimeEnabled(false);
}
 
Example 18
Source File: HotThreadsMonitor.java    From ns4_gear_watchdog with Apache License 2.0 5 votes vote down vote up
private static void enableCpuTime(ThreadMXBean threadMXBean) {
    try {
        if (threadMXBean.isThreadCpuTimeSupported()) {
            if (!threadMXBean.isThreadCpuTimeEnabled()) {
                threadMXBean.setThreadCpuTimeEnabled(true);
            }
        }
    } catch (SecurityException ex) {
        // This should not happen - the security manager should not be enabled.
        logger.debug("Cannot enable Thread Cpu Time", ex);
    }
}
 
Example 19
Source File: AbstractVirtualSensor.java    From gsn with GNU General Public License v3.0 4 votes vote down vote up
public Hashtable<String, Object> getStatistics(){
	Hashtable<String, Object> stat = anomalyDetector.getStatistics(); 
	stat.put("vs."+virtualSensorConfiguration.getName().replaceAll("\\.", "_") +".output.produced.counter", outputCount);
	stat.put("vs."+virtualSensorConfiguration.getName().replaceAll("\\.", "_") +".input.produced.counter", inputCount);

       /*
       *    We know the IDs of threads associated with this VSensor
       *    Using the IDs, we would extract the CPU times, sum them up and put them in the stats map
       */
       ThreadMXBean threadBean = Main.getThreadMXBean();
       if (!threadBean.isThreadCpuTimeEnabled()) {
           logger.info("ThreadCpuTime is disabled. Enabling it | Thread time measurement might not be accurate");
           threadBean.setThreadCpuTimeEnabled(true);
       }

       Iterator <Map.Entry<Long,String>> iter = threads.entrySet().iterator();
       long totalCpuTime = 0L;

       //TODO: Not using thread names | Should be used for logging in debug mode

       while (iter.hasNext()) {
           Map.Entry<Long,String> entry = iter.next();
           Long id = entry.getKey();

           long cpuTime = threadBean.getThreadCpuTime(id);

           if (cpuTime == -1) {    // Thread is not alive anymore
               iter.remove();
               continue;
           }
           
           if(Long.MAX_VALUE-totalCpuTime > cpuTime){
           	totalCpuTime += cpuTime;
           }else{
           	totalCpuTime = cpuTime-(Long.MAX_VALUE-totalCpuTime);
           }
       }

       stat.put("vs."+virtualSensorConfiguration.getName().replaceAll("\\.","_")+".cputime.totalCpuTime.counter", totalCpuTime);

	return stat;
}
 
Example 20
Source File: TimerTest.java    From Wikidata-Toolkit with Apache License 2.0 4 votes vote down vote up
@Test
public void basicTimerOperation() {
	Timer timer = new Timer("Test timer", Timer.RECORD_ALL);
	assertEquals(timer.getName(), "Test timer");
	long threadId = timer.getThreadId();

	assertEquals(timer.getAvgCpuTime(), 0);
	assertEquals(timer.getAvgWallTime(), 0);

	ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();
	if (!tmxb.isThreadCpuTimeEnabled()) {
		tmxb.setThreadCpuTimeEnabled(true);
	}

	long cpuTime1 = tmxb.getThreadCpuTime(threadId);
	long wallTime1 = System.nanoTime();
	timer.start();
	doDummyComputation();
	assertTrue("Timer should be running", timer.isRunning());
	timer.stop();
	cpuTime1 = tmxb.getThreadCpuTime(threadId) - cpuTime1;
	wallTime1 = System.nanoTime() - wallTime1;
	assertTrue(
			"Unrealistic CPU time: " + timer.getTotalCpuTime()
					+ " should be closer to " + cpuTime1,
			(cpuTime1 - TimerTest.TIME_TOLERANCE) <= timer
					.getTotalCpuTime()
					&& timer.getTotalCpuTime() <= cpuTime1);
	assertTrue(
			"Unrealistic wall time: " + timer.getTotalWallTime()
					+ " should be closer to " + wallTime1,
			(wallTime1 - 2 * TimerTest.TIME_TOLERANCE) <= timer
					.getTotalWallTime()
					&& timer.getTotalWallTime() <= wallTime1);

	long cpuTime2 = tmxb.getThreadCpuTime(threadId);
	long wallTime2 = System.nanoTime();
	timer.start();
	doDummyComputation();
	timer.stop();
	cpuTime1 += tmxb.getThreadCpuTime(threadId) - cpuTime2;
	wallTime1 += System.nanoTime() - wallTime2;
	assertTrue(
			"Unrealistic total CPU time: " + timer.getTotalCpuTime()
					+ " should be closer to " + cpuTime1,
			(cpuTime1 - 2 * TimerTest.TIME_TOLERANCE) <= timer
					.getTotalCpuTime()
					&& timer.getTotalCpuTime() <= cpuTime1);
	assertTrue(
			"Unrealistic total wall time: " + timer.getTotalWallTime()
					+ " should be closer to " + wallTime1,
			(wallTime1 - 4 * TimerTest.TIME_TOLERANCE) <= timer
					.getTotalWallTime()
					&& timer.getTotalWallTime() <= wallTime1);

	assertEquals(timer.getTotalCpuTime() / 2, timer.getAvgCpuTime());
	assertEquals(timer.getTotalWallTime() / 2, timer.getAvgWallTime());

	timer.reset();
	assertEquals(timer.getTotalCpuTime(), 0);
	assertEquals(timer.getTotalWallTime(), 0);
	assertFalse("Timer should not be running", timer.isRunning());
}