You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CPUTimeStopWatch.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. package org.eclipse.jgit.util;
  11. import java.lang.management.ManagementFactory;
  12. import java.lang.management.ThreadMXBean;
  13. /**
  14. * A simple stopwatch which measures elapsed CPU time of the current thread. CPU
  15. * time is the time spent on executing your own code plus the time spent on
  16. * executing operating system calls triggered by your application.
  17. * <p>
  18. * This stopwatch needs a VM which supports getting CPU Time information for the
  19. * current thread. The static method createInstance() will take care to return
  20. * only a new instance of this class if the VM is capable of returning CPU time.
  21. */
  22. public class CPUTimeStopWatch {
  23. private long start;
  24. private static ThreadMXBean mxBean=ManagementFactory.getThreadMXBean();
  25. /**
  26. * use this method instead of the constructor to be sure that the underlying
  27. * VM provides all features needed by this class.
  28. *
  29. * @return a new instance of {@link #CPUTimeStopWatch()} or
  30. * <code>null</code> if the VM does not support getting CPU time
  31. * information
  32. */
  33. public static CPUTimeStopWatch createInstance() {
  34. return mxBean.isCurrentThreadCpuTimeSupported() ? new CPUTimeStopWatch()
  35. : null;
  36. }
  37. /**
  38. * Starts the stopwatch. If the stopwatch is already started this will
  39. * restart the stopwatch.
  40. */
  41. public void start() {
  42. start = mxBean.getCurrentThreadCpuTime();
  43. }
  44. /**
  45. * Stops the stopwatch and return the elapsed CPU time in nanoseconds.
  46. * Should be called only on started stopwatches.
  47. *
  48. * @return the elapsed CPU time in nanoseconds. When called on non-started
  49. * stopwatches (either because {@link #start()} was never called or
  50. * {@link #stop()} was called after the last call to
  51. * {@link #start()}) this method will return 0.
  52. */
  53. public long stop() {
  54. long cpuTime = readout();
  55. start = 0;
  56. return cpuTime;
  57. }
  58. /**
  59. * Return the elapsed CPU time in nanoseconds. In contrast to
  60. * {@link #stop()} the stopwatch will continue to run after this call.
  61. *
  62. * @return the elapsed CPU time in nanoseconds. When called on non-started
  63. * stopwatches (either because {@link #start()} was never called or
  64. * {@link #stop()} was called after the last call to
  65. * {@link #start()}) this method will return 0.
  66. */
  67. public long readout() {
  68. return (start == 0) ? 0 : mxBean.getCurrentThreadCpuTime() - start;
  69. }
  70. }