Example usage for java.lang.management ThreadMXBean getCurrentThreadUserTime

List of usage examples for java.lang.management ThreadMXBean getCurrentThreadUserTime

Introduction

In this page you can find the example usage for java.lang.management ThreadMXBean getCurrentThreadUserTime.

Prototype

public long getCurrentThreadUserTime();

Source Link

Document

Returns the CPU time that the current thread has executed in user mode in nanoseconds.

Usage

From source file:com.thoughtworks.go.server.service.builders.KillAllChildProcessTaskBuilderTest.java

public long getSystemTime() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported()
            ? (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime())
            : 0L;// www .  j a  v a2  s  .c  om
}

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   www  .  j  av  a2 s  .  com
    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:org.renjin.primitives.System.java

/**
 * Returns object of class "proc_time" which is a numeric vector of
 * length 5, containing the user, system, and total elapsed times for
 * the currently running R process, and the cumulative sum of user
 * and system times of any child processes spawned by it on which it
 * has waited. //from   ww  w .  j av a2  s .c om
 *
 * _The user time is the CPU time charged for the execution of user
 *  instructions of the calling process. The system time is the CPU
 *  time charged for execution by the system on behalf of the calling
 *  process._
 */
@Builtin("proc.time")
public static DoubleVector procTime() {

    DoubleArrayVector.Builder result = new DoubleArrayVector.Builder();
    StringVector.Builder names = new StringVector.Builder();

    long totalCPUTime;
    long userCPUTime;
    long elapsedTime;

    // There doesn't seem to be any platform-independent way of accessing
    // CPU use for the whole JVM process, so we'll have to make do
    // with the timings for the thread we're running on.
    //
    // Additionally, the MX Beans may not be available in all environments,
    // so we need to fallback to app
    try {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        totalCPUTime = threadMXBean.getCurrentThreadCpuTime();
        userCPUTime = threadMXBean.getCurrentThreadUserTime();

        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        elapsedTime = runtimeMXBean.getUptime();
    } catch (Error e) {
        // ThreadMXBean is not available in all environments
        // Specifically, AppEngine will throw variously SecurityErrors or
        // ClassNotFoundErrors if we try to access these classes
        userCPUTime = totalCPUTime = java.lang.System.nanoTime();
        elapsedTime = new Date().getTime();
    }

    // user.self
    names.add("user.self");
    result.add(userCPUTime / NANOSECONDS_PER_SECOND);

    // sys.self
    names.add("sys.self");
    result.add((totalCPUTime - userCPUTime) / NANOSECONDS_PER_SECOND);

    // elapsed
    // (wall clock time)
    names.add("elapsed");
    result.add(elapsedTime / MILLISECONDS_PER_SECOND);

    // AFAIK, we don't have any platform independent way of accessing
    // this info.

    // user.child
    names.add("user.child");
    result.add(0);

    // sys.child
    names.add("sys.child");
    result.add(0);

    result.setAttribute(Symbols.NAMES, names.build());
    result.setAttribute(Symbols.CLASS, StringVector.valueOf("proc_time"));
    return result.build();

}