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

The following examples show how to use java.lang.management.ThreadMXBean#getCurrentThreadUserTime() . 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: 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 2
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 3
Source File: SocialGolfer.java    From kodkod with MIT License 5 votes vote down vote up
/**
	 * Usage: java examples.classicnp.SocialGolfer <players> <groups> <weeks> <group size>
	 */
	public static void main(String[] args) { 
		if (args.length<4) usage();
		try {
			final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
			bean.setThreadCpuTimeEnabled(true);
			final long start = bean.getCurrentThreadUserTime();
			final int players = Integer.parseInt(args[0]);
			final int groups = Integer.parseInt(args[1]);
			final int weeks = Integer.parseInt(args[2]);
			final int size = Integer.parseInt(args[3]);
			if (players<1 || groups<1 || weeks<1 || size<1) usage();
			final SocialGolfer model = new SocialGolfer();
			final Formula f = model.schedule();
			final Bounds b = model.bounds(players, groups, weeks, size);
//			System.out.println(PrettyPrinter.print(f, 2));
//			System.out.println(b);
			final Solver s = new Solver();
			s.options().setSolver(SATFactory.MiniSat);
			s.options().setBitwidth(32-Integer.numberOfLeadingZeros(groups*weeks));
//			s.options().setReporter(new ConsoleReporter());
			s.options().setSymmetryBreaking(1000);
			final Solution sol = s.solve(f, b);
			model.print(sol,s);
			final long end = bean.getCurrentThreadUserTime();
			System.out.println("Total time: " + (end-start)/1000000);
		} catch (NumberFormatException nfe) { 
			usage();
		}
	}
 
Example 4
Source File: Timing.java    From kanzi with Apache License 2.0 5 votes vote down vote up
public long getSystemTime()
{
   ThreadMXBean bean = ManagementFactory.getThreadMXBean();

   return bean.isCurrentThreadCpuTimeSupported() ? 
      (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime()) : 0L;
}
 
Example 5
Source File: KillAllChildProcessTaskBuilderTest.java    From gocd with Apache License 2.0 4 votes vote down vote up
public long getSystemTime() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported() ?
            (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime()) : 0L;
}
 
Example 6
Source File: KillAllChildProcessTaskTest.java    From gocd with Apache License 2.0 4 votes vote down vote up
public long getSystemTime() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported() ?
            (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime()) : 0L;
}
 
Example 7
Source File: TimeMagicCommand.java    From beakerx with Apache License 2.0 4 votes vote down vote up
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.");
  }
}
 
Example 8
Source File: Timing.java    From kanzi with Apache License 2.0 4 votes vote down vote up
public long getUserTime()
{
   ThreadMXBean bean = ManagementFactory.getThreadMXBean();
   return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadUserTime() : 0L;
}
 
Example 9
Source File: GeneralUtils.java    From ade with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the current thread's user time.
 * 
 * @return the current thread's user time, or 0 if user time is not supported
 *     for the current thread
 */
public static long getUserTime() {
    final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadUserTime() : 0L;
}
 
Example 10
Source File: GeneralUtils.java    From ade with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns the difference between the current thread's CPU time and user time.
 * 
 * @return the current thread's system time, or 0 if CPU time is not supported
 *     for the current thread
 */
public static long getSystemTime() {
    final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    final long systemTime = bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime();
    return bean.isCurrentThreadCpuTimeSupported() ? systemTime : 0L;
}