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.jikesrvm.runtime;
014
015import org.vmmagic.pragma.Uninterruptible;
016
017/**
018 * Primitives from which to build interval and absolute timers.
019 */
020@Uninterruptible
021public class Time {
022
023  /** boot time in milliseconds */
024  private static long bootTime;
025
026  public static void boot() {
027    bootTime = currentTimeMillis();
028  }
029
030  /**
031   * Convert a long representing a time in nanoseconds into
032   * a double representing the same time in milliseconds.
033   * @param c a time in nanoseconds
034   * @return c converted to milliseconds
035   */
036  public static double nanosToMillis(long c) {
037    return (c) / 1e6;
038  }
039
040  /**
041   * Return the value of a hardware cycle counter (RDTSC on IA32, time base register on PPC).
042   * This is a very cheap, but also unreliable "timing" mechanism.
043   * There is absolutely no guarantee that the values returned from this method will
044   * either by monotonic (i.e., "time" can go backwards) or
045   * smooth ("time" can appear to move at a variable rate).
046   * This method should only be used for approximate timing in frequently executed code.
047   * We intentionally do not provide an API for converting from cycles to seconds because
048   * the conversion cannot be reliably supported on all of our platforms.
049   *
050   * @return the value of the hardware cycle counter
051   */
052  public static long cycles() {
053    return Magic.getTimeBase();
054  }
055
056  /**
057   * Same semantics as java.lang.System.nanoTime();
058   * This (or java.lang.System.nanoTime) is the
059   * preferred API for VM internal timing functions.
060   * @return a monotonic timer value in nanoseconds.
061   */
062  public static long nanoTime() {
063    return SysCall.sysCall.sysNanoTime();
064  }
065
066  /**
067   * @return current time in milliseconds (epoch Jan 1 1970).
068   */
069  public static long currentTimeMillis() {
070    return SysCall.sysCall.sysCurrentTimeMillis();
071  }
072
073  public static double nanosToSecs(long nanos) {
074    return (nanos) / 1E9;
075  }
076
077  public static long secsToNanos(double secs) {
078    return (long)(secs * 1E9);
079  }
080
081  public static long bootTime() {
082    return bootTime;
083  }
084}