Java Code Examples for java.lang.management.ThreadMXBean#getThreadCpuTime()
The following examples show how to use
java.lang.management.ThreadMXBean#getThreadCpuTime() .
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: JvmThreadInstanceEntryImpl.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Getter for the "JvmThreadInstCpuTimeNs" variable. */ public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException { long l = 0; final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean(); try { if (tmb.isThreadCpuTimeSupported()) { l = tmb.getThreadCpuTime(info.getThreadId()); log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l); //Cpu time measurement is disabled or the id is not valid. if(l == -1) l = 0; } } catch (UnsatisfiedLinkError e) { // XXX Revisit: catch TO BE EVENTUALLY REMOVED log.debug("getJvmThreadInstCpuTimeNs", "Operation not supported: " + e); } return new Long(l); }
Example 2
Source File: JvmThreadInstanceEntryImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * Getter for the "JvmThreadInstCpuTimeNs" variable. */ public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException { long l = 0; final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean(); try { if (tmb.isThreadCpuTimeSupported()) { l = tmb.getThreadCpuTime(info.getThreadId()); log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l); //Cpu time measurement is disabled or the id is not valid. if(l == -1) l = 0; } } catch (UnsatisfiedLinkError e) { // XXX Revisit: catch TO BE EVENTUALLY REMOVED log.debug("getJvmThreadInstCpuTimeNs", "Operation not supported: " + e); } return new Long(l); }
Example 3
Source File: JvmThreadInstanceEntryImpl.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
/** * Getter for the "JvmThreadInstCpuTimeNs" variable. */ public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException { long l = 0; final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean(); try { if (tmb.isThreadCpuTimeSupported()) { l = tmb.getThreadCpuTime(info.getThreadId()); log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l); //Cpu time measurement is disabled or the id is not valid. if(l == -1) l = 0; } } catch (UnsatisfiedLinkError e) { // XXX Revisit: catch TO BE EVENTUALLY REMOVED log.debug("getJvmThreadInstCpuTimeNs", "Operation not supported: " + e); } return new Long(l); }
Example 4
Source File: ThreadingManager.java From vi with Apache License 2.0 | 6 votes |
public static List<TInfo> getAllThreadInfo(){ List<TInfo> threads = new ArrayList<>(); ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long[] ids = threadBean.getAllThreadIds(); ThreadInfo[] infos = threadBean.getThreadInfo(ids); for (ThreadInfo info : infos){ long id = info.getThreadId(); TInfo tInfo = new TInfo(); tInfo.name = info.getThreadName(); tInfo.id = id; tInfo.state = info.getThreadState(); tInfo.cpuTime = threadBean.getThreadCpuTime(id); threads.add(tInfo); } Collections.sort(threads,new Comparator<TInfo>() { @Override public int compare(TInfo o1, TInfo o2) { return Long.compare(o2.cpuTime,o1.cpuTime); } }); return threads; }
Example 5
Source File: Timing.java From kanzi with Apache License 2.0 | 6 votes |
public long getCpuTime(long[] ids) { ThreadMXBean bean = ManagementFactory.getThreadMXBean(); if (bean.isThreadCpuTimeSupported() == false) return 0L; long time = 0L; for (int i=0; i<ids.length; i++) { long t = bean.getThreadCpuTime(ids[i]); if (t != -1) time += t; } return time; }
Example 6
Source File: JvmThreadInstanceEntryImpl.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * Getter for the "JvmThreadInstCpuTimeNs" variable. */ public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException { long l = 0; final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean(); try { if (tmb.isThreadCpuTimeSupported()) { l = tmb.getThreadCpuTime(info.getThreadId()); log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l); //Cpu time measurement is disabled or the id is not valid. if(l == -1) l = 0; } } catch (UnsatisfiedLinkError e) { // XXX Revisit: catch TO BE EVENTUALLY REMOVED log.debug("getJvmThreadInstCpuTimeNs", "Operation not supported: " + e); } return new Long(l); }
Example 7
Source File: JvmThreadInstanceEntryImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * Getter for the "JvmThreadInstCpuTimeNs" variable. */ public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException { long l = 0; final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean(); try { if (tmb.isThreadCpuTimeSupported()) { l = tmb.getThreadCpuTime(info.getThreadId()); log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l); //Cpu time measurement is disabled or the id is not valid. if(l == -1) l = 0; } } catch (UnsatisfiedLinkError e) { // XXX Revisit: catch TO BE EVENTUALLY REMOVED log.debug("getJvmThreadInstCpuTimeNs", "Operation not supported: " + e); } return new Long(l); }
Example 8
Source File: JvmThreadInstanceEntryImpl.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * Getter for the "JvmThreadInstCpuTimeNs" variable. */ public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException { long l = 0; final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean(); try { if (tmb.isThreadCpuTimeSupported()) { l = tmb.getThreadCpuTime(info.getThreadId()); log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l); //Cpu time measurement is disabled or the id is not valid. if(l == -1) l = 0; } } catch (UnsatisfiedLinkError e) { // XXX Revisit: catch TO BE EVENTUALLY REMOVED log.debug("getJvmThreadInstCpuTimeNs", "Operation not supported: " + e); } return new Long(l); }
Example 9
Source File: GetStatusServlet.java From pentaho-kettle with Apache License 2.0 | 6 votes |
private static void getSystemInfo( SlaveServerStatus serverStatus ) { OperatingSystemMXBean operatingSystemMXBean = java.lang.management.ManagementFactory.getOperatingSystemMXBean(); ThreadMXBean threadMXBean = java.lang.management.ManagementFactory.getThreadMXBean(); RuntimeMXBean runtimeMXBean = java.lang.management.ManagementFactory.getRuntimeMXBean(); int cores = Runtime.getRuntime().availableProcessors(); long freeMemory = Runtime.getRuntime().freeMemory(); long totalMemory = Runtime.getRuntime().totalMemory(); String osArch = operatingSystemMXBean.getArch(); String osName = operatingSystemMXBean.getName(); String osVersion = operatingSystemMXBean.getVersion(); double loadAvg = operatingSystemMXBean.getSystemLoadAverage(); int threadCount = threadMXBean.getThreadCount(); long allThreadsCpuTime = 0L; long[] threadIds = threadMXBean.getAllThreadIds(); for ( int i = 0; i < threadIds.length; i++ ) { allThreadsCpuTime += threadMXBean.getThreadCpuTime( threadIds[ i ] ); } long uptime = runtimeMXBean.getUptime(); serverStatus.setCpuCores( cores ); serverStatus.setCpuProcessTime( allThreadsCpuTime ); serverStatus.setUptime( uptime ); serverStatus.setThreadCount( threadCount ); serverStatus.setLoadAvg( loadAvg ); serverStatus.setOsName( osName ); serverStatus.setOsVersion( osVersion ); serverStatus.setOsArchitecture( osArch ); serverStatus.setMemoryFree( freeMemory ); serverStatus.setMemoryTotal( totalMemory ); }
Example 10
Source File: InvalidThreadID.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String argv[]) { ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); int cnt = 0; long [] idArr = {0, -1, -2, (Long.MIN_VALUE + 1), Long.MIN_VALUE}; if (mbean.isThreadCpuTimeSupported()) { for (int i = 0; i < idArr.length; i++) { try { mbean.getThreadCpuTime(idArr[i]); System.out.println("Test failed. IllegalArgumentException" + " expected for ID = " + idArr[i]); } catch (IllegalArgumentException iae) { cnt++; } } if (cnt != idArr.length) { throw new RuntimeException("Unexpected number of " + "IllegalArgumentException = " + cnt + " expected = " + idArr.length); } // CPU time for a non-existence thread long time = mbean.getThreadCpuTime(999999); if (time < 0 && time != -1) { throw new RuntimeException("Cpu time for thread 999999" + " is invalid = " + time + " expected to be -1."); } } System.out.println("Test passed."); }
Example 11
Source File: InvalidThreadID.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String argv[]) { ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); int cnt = 0; long [] idArr = {0, -1, -2, (Long.MIN_VALUE + 1), Long.MIN_VALUE}; if (mbean.isThreadCpuTimeSupported()) { for (int i = 0; i < idArr.length; i++) { try { mbean.getThreadCpuTime(idArr[i]); System.out.println("Test failed. IllegalArgumentException" + " expected for ID = " + idArr[i]); } catch (IllegalArgumentException iae) { cnt++; } } if (cnt != idArr.length) { throw new RuntimeException("Unexpected number of " + "IllegalArgumentException = " + cnt + " expected = " + idArr.length); } // CPU time for a non-existence thread long time = mbean.getThreadCpuTime(999999); if (time < 0 && time != -1) { throw new RuntimeException("Cpu time for thread 999999" + " is invalid = " + time + " expected to be -1."); } } System.out.println("Test passed."); }
Example 12
Source File: InvalidThreadID.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String argv[]) { ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); int cnt = 0; long [] idArr = {0, -1, -2, (Long.MIN_VALUE + 1), Long.MIN_VALUE}; if (mbean.isThreadCpuTimeSupported()) { for (int i = 0; i < idArr.length; i++) { try { mbean.getThreadCpuTime(idArr[i]); System.out.println("Test failed. IllegalArgumentException" + " expected for ID = " + idArr[i]); } catch (IllegalArgumentException iae) { cnt++; } } if (cnt != idArr.length) { throw new RuntimeException("Unexpected number of " + "IllegalArgumentException = " + cnt + " expected = " + idArr.length); } // CPU time for a non-existence thread long time = mbean.getThreadCpuTime(999999); if (time < 0 && time != -1) { throw new RuntimeException("Cpu time for thread 999999" + " is invalid = " + time + " expected to be -1."); } } System.out.println("Test passed."); }
Example 13
Source File: ThreadInfoTask.java From bistoury with GNU General Public License v3.0 | 5 votes |
private ThreadInfo getThreadInfo(VirtualMachineUtil.VMConnector connect, Map<String, Object> result) { try { ThreadMXBean threadMXBean = connect.getThreadMXBean(); long threadCpuTime = threadMXBean.getThreadCpuTime(threadId); result.put("cpuTime", threadCpuTime); return threadMXBean.getThreadInfo(threadId, maxDepth); } catch (IOException e) { logger.error("get thread info error", e); return null; } }
Example 14
Source File: InvalidThreadID.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String argv[]) { ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); int cnt = 0; long [] idArr = {0, -1, -2, (Long.MIN_VALUE + 1), Long.MIN_VALUE}; if (mbean.isThreadCpuTimeSupported()) { for (int i = 0; i < idArr.length; i++) { try { mbean.getThreadCpuTime(idArr[i]); System.out.println("Test failed. IllegalArgumentException" + " expected for ID = " + idArr[i]); } catch (IllegalArgumentException iae) { cnt++; } } if (cnt != idArr.length) { throw new RuntimeException("Unexpected number of " + "IllegalArgumentException = " + cnt + " expected = " + idArr.length); } // CPU time for a non-existence thread long time = mbean.getThreadCpuTime(999999); if (time < 0 && time != -1) { throw new RuntimeException("Cpu time for thread 999999" + " is invalid = " + time + " expected to be -1."); } } System.out.println("Test passed."); }
Example 15
Source File: ServerStatus.java From pentaho-kettle with Apache License 2.0 | 5 votes |
public ServerStatus() { OperatingSystemMXBean operatingSystemMXBean = java.lang.management.ManagementFactory.getOperatingSystemMXBean(); ThreadMXBean threadMXBean = java.lang.management.ManagementFactory.getThreadMXBean(); RuntimeMXBean runtimeMXBean = java.lang.management.ManagementFactory.getRuntimeMXBean(); int cores = Runtime.getRuntime().availableProcessors(); long freeMemory = Runtime.getRuntime().freeMemory(); long totalMemory = Runtime.getRuntime().totalMemory(); String osArch = operatingSystemMXBean.getArch(); String osName = operatingSystemMXBean.getName(); String osVersion = operatingSystemMXBean.getVersion(); double loadAvg = operatingSystemMXBean.getSystemLoadAverage(); int threadCount = threadMXBean.getThreadCount(); long allThreadsCpuTime = 0L; long[] threadIds = threadMXBean.getAllThreadIds(); for ( int i = 0; i < threadIds.length; i++ ) { allThreadsCpuTime += threadMXBean.getThreadCpuTime( threadIds[i] ); } long uptime = runtimeMXBean.getUptime(); setCpuCores( cores ); setCpuProcessTime( allThreadsCpuTime ); setUptime( uptime ); setThreadCount( threadCount ); setLoadAvg( loadAvg ); setOsName( osName ); setOsVersion( osVersion ); setOsArchitecture( osArch ); setMemoryFree( freeMemory ); setMemoryTotal( totalMemory ); }
Example 16
Source File: InvalidThreadID.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public static void main(String argv[]) { ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); int cnt = 0; long [] idArr = {0, -1, -2, (Long.MIN_VALUE + 1), Long.MIN_VALUE}; if (mbean.isThreadCpuTimeSupported()) { for (int i = 0; i < idArr.length; i++) { try { mbean.getThreadCpuTime(idArr[i]); System.out.println("Test failed. IllegalArgumentException" + " expected for ID = " + idArr[i]); } catch (IllegalArgumentException iae) { cnt++; } } if (cnt != idArr.length) { throw new RuntimeException("Unexpected number of " + "IllegalArgumentException = " + cnt + " expected = " + idArr.length); } // CPU time for a non-existence thread long time = mbean.getThreadCpuTime(999999); if (time < 0 && time != -1) { throw new RuntimeException("Cpu time for thread 999999" + " is invalid = " + time + " expected to be -1."); } } System.out.println("Test passed."); }
Example 17
Source File: TPerformance.java From Voovan with Apache License 2.0 | 5 votes |
/** * 获取当前进程 cpu 使用量 * @return cpu 使用量, 如使用2核, 返回200% */ public static double getProcessCpuUsage() { ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long totalCpuUsedTime = 0; for (long id : threadBean.getAllThreadIds()) { totalCpuUsedTime += threadBean.getThreadCpuTime(id); } long curtime = System.nanoTime(); long usedTime = totalCpuUsedTime - prevCpuUsedTime; //cpu 用时差 long totalPassedTime = curtime - prevGetTime; //时间差 prevGetTime = curtime; prevCpuUsedTime = totalCpuUsedTime; return (((double) usedTime) / totalPassedTime) * 100; }
Example 18
Source File: TimerTest.java From Wikidata-Toolkit with Apache License 2.0 | 4 votes |
@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 |
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: TimeMagicCommand.java From beakerx with Apache License 2.0 | 4 votes |
public MagicCommandOutput time(String codeToExecute, Message message, int executionCount, boolean showResult) { CompletableFuture<TimeMeasureData> compileTime = new CompletableFuture<>(); ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); long currentThreadId = Thread.currentThread().getId(); Long startWallTime = System.nanoTime(); Long startCpuTotalTime = threadMXBean.getCurrentThreadCpuTime(); Long startUserTime = threadMXBean.getCurrentThreadUserTime(); SimpleEvaluationObject simpleEvaluationObject = createSimpleEvaluationObject(codeToExecute, kernel, message, executionCount); if (!showResult) { simpleEvaluationObject.noResult(); } TryResult either = kernel.executeCode(codeToExecute, simpleEvaluationObject); Long endWallTime = System.nanoTime(); Long endCpuTotalTime = threadMXBean.getThreadCpuTime(currentThreadId); Long endUserTime = threadMXBean.getThreadUserTime(currentThreadId); compileTime.complete(new TimeMeasureData(endCpuTotalTime - startCpuTotalTime, endUserTime - startUserTime, endWallTime - startWallTime)); String messageInfo = "CPU times: user %s, sys: %s, total: %s \nWall Time: %s\n"; try { TimeMeasureData timeMeasuredData = compileTime.get(); return new MagicCommandOutput(MagicCommandOutput.Status.OK, String.format(messageInfo, format(timeMeasuredData.getCpuUserTime()), format(timeMeasuredData.getCpuTotalTime() - timeMeasuredData.getCpuUserTime()), format(timeMeasuredData.getCpuTotalTime()), format(timeMeasuredData.getWallTime())), either, simpleEvaluationObject); } catch (InterruptedException | ExecutionException e) { return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, "There occurs problem during measuring time for your statement."); } }