List of usage examples for java.lang.management ThreadMXBean getThreadUserTime
public long getThreadUserTime(long id);
From source file:com.streamsets.datacollector.restapi.AdminResource.java
@GET @Path("/threads") @ApiOperation(value = "Returns Thread Dump along with stack trace", response = Map.class, responseContainer = "List", authorizations = @Authorization(value = "basic")) @Produces(MediaType.APPLICATION_JSON)/* w ww .ja va 2 s .c o m*/ @RolesAllowed({ AuthzRole.ADMIN, AuthzRole.ADMIN_REMOTE }) public Response getThreadsDump() throws IOException { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] threads = threadMXBean.dumpAllThreads(true, true); List<Map> augmented = new ArrayList<>(threads.length); for (ThreadInfo thread : threads) { Map<String, Object> map = new LinkedHashMap<>(); map.put("threadInfo", thread); map.put("userTimeNanosecs", threadMXBean.getThreadUserTime(thread.getThreadId())); map.put("cpuTimeNanosecs", threadMXBean.getThreadCpuTime(thread.getThreadId())); augmented.add(map); } return Response.ok(augmented).build(); }
From source file:com.twosigma.beakerx.kernel.magic.command.functionality.TimeMagicCommand.java
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);/*from ww w. j ava2 s . c om*/ 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."); } }
From source file:net.bull.javamelody.internal.model.JavaInformations.java
public static List<ThreadInformations> buildThreadInformationsList() { final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); final Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces(); final List<Thread> threads = new ArrayList<Thread>(stackTraces.keySet()); // si "1.6.0_01".compareTo(Parameters.JAVA_VERSION) > 0; // on rcuprait les threads sans stack trace en contournant bug 6434648 avant 1.6.0_01 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6434648 // hormis pour le thread courant qui obtient sa stack trace diffremment sans le bug // threads = getThreadsFromThreadGroups(); // final Thread currentThread = Thread.currentThread(); // stackTraces = Collections.singletonMap(currentThread, currentThread.getStackTrace()); final boolean cpuTimeEnabled = threadBean.isThreadCpuTimeSupported() && threadBean.isThreadCpuTimeEnabled(); final long[] deadlockedThreads = getDeadlockedThreads(threadBean); final List<ThreadInformations> threadInfosList = new ArrayList<ThreadInformations>(threads.size()); // hostAddress rcupr ici car il peut y avoir plus de 20000 threads final String hostAddress = Parameters.getHostAddress(); for (final Thread thread : threads) { final StackTraceElement[] stackTraceElements = stackTraces.get(thread); final List<StackTraceElement> stackTraceElementList = stackTraceElements == null ? null : new ArrayList<StackTraceElement>(Arrays.asList(stackTraceElements)); final long cpuTimeMillis; final long userTimeMillis; if (cpuTimeEnabled) { cpuTimeMillis = threadBean.getThreadCpuTime(thread.getId()) / 1000000; userTimeMillis = threadBean.getThreadUserTime(thread.getId()) / 1000000; } else {/*from w w w . j av a2 s . c om*/ 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; }