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