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

The following examples show how to use java.lang.management.ThreadMXBean#isThreadCpuTimeEnabled() . 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: ThreadDumpProvider.java    From TNT4J with Apache License 2.0 6 votes vote down vote up
@Override
public DumpCollection getDump() {
	Dump dump = new Dump("JavaThreadStack", this);

	ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
	boolean contentionSupported = tmbean.isThreadContentionMonitoringSupported()
			&& tmbean.isThreadContentionMonitoringEnabled();
	boolean cputSupported = tmbean.isThreadCpuTimeSupported() && tmbean.isThreadCpuTimeEnabled();
	dump.add("java.thread.contention.supported", contentionSupported);
	dump.add("java.thread.cpu.supported", cputSupported);

	long[] dead = tmbean.findMonitorDeadlockedThreads();
	dump.add("java.thread.deadlock.count", dead == null ? 0 : dead.length);

	long[] tids = tmbean.getAllThreadIds();
	ThreadInfo[] tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
	for (ThreadInfo ti : tinfos) {
		dump.add(ti.getThreadName() + "-" + ti.getThreadId(), ti);
	}
	return dump;
}
 
Example 2
Source File: LiveJvmServiceImpl.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private static Availability getThreadCpuTimeAvailability() {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    if (!threadMXBean.isThreadCpuTimeSupported()) {
        return Availability.newBuilder()
                .setAvailable(false)
                .setReason("java.lang.management.ThreadMXBean"
                        + ".isThreadCpuTimeSupported() returned false")
                .build();
    }
    if (!threadMXBean.isThreadCpuTimeEnabled()) {
        return Availability.newBuilder()
                .setAvailable(false)
                .setReason("java.lang.management.ThreadMXBean"
                        + ".isThreadCpuTimeEnabled() returned false")
                .build();
    }
    return Availability.newBuilder().setAvailable(true).build();
}
 
Example 3
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 4
Source File: JvmThreadingImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 5
Source File: JvmThreadingImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 6
Source File: JvmThreadingImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 7
Source File: JvmThreadingImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 8
Source File: JvmThreadingImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 9
Source File: JvmThreadingImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 10
Source File: JvmThreadingImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 11
Source File: JvmThreadingImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 12
Source File: JvmThreadingImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 13
Source File: JvmThreadingImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 14
Source File: JvmThreadingImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 15
Source File: JvmThreadingImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the "JvmThreadCpuTimeMonitoring" variable.
 */
public EnumJvmThreadCpuTimeMonitoring getJvmThreadCpuTimeMonitoring()
    throws SnmpStatusException {

    ThreadMXBean mbean = getThreadMXBean();

    if(!mbean.isThreadCpuTimeSupported()) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Unsupported ThreadCpuTimeMonitoring");
        return JvmThreadCpuTimeMonitoringUnsupported;
    }

    try {
        if(mbean.isThreadCpuTimeEnabled()) {
            log.debug("getJvmThreadCpuTimeMonitoring",
                  "Enabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringEnabled;
        } else {
            log.debug("getJvmThreadCpuTimeMonitoring",
                      "Disabled ThreadCpuTimeMonitoring");
            return JvmThreadCpuTimeMonitoringDisabled;
        }
    }catch(UnsupportedOperationException e) {
        log.debug("getJvmThreadCpuTimeMonitoring",
                  "Newly unsupported ThreadCpuTimeMonitoring");

        return JvmThreadCpuTimeMonitoringUnsupported;
    }
}
 
Example 16
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 17
Source File: JavaInformations.java    From javamelody with Apache License 2.0 4 votes vote down vote up
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 récupèrait 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 différemment 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 récupéré 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 {
			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;
}
 
Example 18
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());
}
 
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;
}