001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.mmtk.utility.statistics;
014
015import org.mmtk.utility.Log;
016import org.mmtk.vm.VM;
017
018import org.vmmagic.pragma.*;
019
020/**
021 * This class implements a simple timer.
022 */
023@Uninterruptible
024public class Timer extends LongCounter {
025
026  /****************************************************************************
027   *
028   * Initialization
029   */
030
031  /**
032   * Constructor
033   *
034   * @param name The name to be associated with this counter
035   */
036  public Timer(String name) {
037    this(name, true, false);
038  }
039
040  /**
041   * Constructor
042   *
043   * @param name The name to be associated with this counter
044   * @param start True if this counter is to be implicitly started
045   * when <code>startAll()</code> is called (otherwise the counter
046   * must be explicitly started).
047   */
048  public Timer(String name, boolean start) {
049    this(name, start, false);
050  }
051
052  /**
053   * Constructor
054   *
055   * @param name The name to be associated with this counter
056   * @param start True if this counter is to be implicitly started
057   * when <code>startAll()</code> is called (otherwise the counter
058   * must be explicitly started).
059   * @param mergephases True if this counter does not separately
060   * report GC and Mutator phases.
061   */
062  public Timer(String name, boolean start, boolean mergephases) {
063    super(name, start, mergephases);
064  }
065
066  /****************************************************************************
067   *
068   * Counter-specific methods
069   */
070
071  /**
072   * Get the current value for this timer
073   *
074   * @return The current value for this timer
075   */
076  @Override
077  @Inline
078  protected final long getCurrentValue() {
079    return VM.statistics.nanoTime();
080  }
081
082  /**
083   * Print the total in microseconds
084   */
085  final void printTotalMicro() {
086    printMicro(totalCount);
087  }
088
089  /**
090   * Print the total in milliseconds
091   */
092  public final void printTotalMillis() {
093    printMillis(totalCount);
094  }
095
096  /**
097   * Print the total in seconds
098   */
099  public final void printTotalSecs() {
100    printSecs(totalCount);
101  }
102
103  /**
104   * Print a value (in milliseconds)
105   *
106   * @param value The value to be printed
107   */
108  @Override
109  final void printValue(long value) {
110    printMillis(value);
111  }
112
113  /**
114   * Print a value in microseconds
115   *
116   * @param value The value to be printed
117   */
118  final void printMicro(long value) {
119    Log.write(1000 * VM.statistics.nanosToMillis(value));
120  }
121
122  /**
123   * Print a value in milliseconds
124   *
125   * @param value The value to be printed
126   */
127  final void printMillis(long value) {
128    Log.write(VM.statistics.nanosToMillis(value));
129  }
130
131  /**
132   * Print a value in seconds
133   *
134   * @param value The value to be printed
135   */
136  final void printSecs(long value) {
137    Log.write(VM.statistics.nanosToSecs(value));
138  }
139
140  /**
141   * @return the current value of the timer in milliseconds
142   */
143  public final double getTotalMillis() {
144    return VM.statistics.nanosToMillis(totalCount);
145  }
146
147}
148