View Javadoc
1   /*
2    * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.util;
12  
13  import java.lang.management.ManagementFactory;
14  import java.lang.management.ThreadMXBean;
15  
16  /**
17   * A simple stopwatch which measures elapsed CPU time of the current thread. CPU
18   * time is the time spent on executing your own code plus the time spent on
19   * executing operating system calls triggered by your application.
20   * <p>
21   * This stopwatch needs a VM which supports getting CPU Time information for the
22   * current thread. The static method createInstance() will take care to return
23   * only a new instance of this class if the VM is capable of returning CPU time.
24   */
25  public class CPUTimeStopWatch {
26  	private long start;
27  
28  	private static ThreadMXBean mxBean=ManagementFactory.getThreadMXBean();
29  
30  	/**
31  	 * use this method instead of the constructor to be sure that the underlying
32  	 * VM provides all features needed by this class.
33  	 *
34  	 * @return a new instance of {@link #CPUTimeStopWatch()} or
35  	 *         <code>null</code> if the VM does not support getting CPU time
36  	 *         information
37  	 */
38  	public static CPUTimeStopWatch createInstance() {
39  		return mxBean.isCurrentThreadCpuTimeSupported() ? new CPUTimeStopWatch()
40  				: null;
41  	}
42  
43  	/**
44  	 * Starts the stopwatch. If the stopwatch is already started this will
45  	 * restart the stopwatch.
46  	 */
47  	public void start() {
48  		start = mxBean.getCurrentThreadCpuTime();
49  	}
50  
51  	/**
52  	 * Stops the stopwatch and return the elapsed CPU time in nanoseconds.
53  	 * Should be called only on started stopwatches.
54  	 *
55  	 * @return the elapsed CPU time in nanoseconds. When called on non-started
56  	 *         stopwatches (either because {@link #start()} was never called or
57  	 *         {@link #stop()} was called after the last call to
58  	 *         {@link #start()}) this method will return 0.
59  	 */
60  	public long stop() {
61  		long cpuTime = readout();
62  		start = 0;
63  		return cpuTime;
64  	}
65  
66  	/**
67  	 * Return the elapsed CPU time in nanoseconds. In contrast to
68  	 * {@link #stop()} the stopwatch will continue to run after this call.
69  	 *
70  	 * @return the elapsed CPU time in nanoseconds. When called on non-started
71  	 *         stopwatches (either because {@link #start()} was never called or
72  	 *         {@link #stop()} was called after the last call to
73  	 *         {@link #start()}) this method will return 0.
74  	 */
75  	public long readout() {
76  		return (start == 0) ? 0 : mxBean.getCurrentThreadCpuTime() - start;
77  	}
78  }