1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.debug;
  26 
  27 import org.graalvm.compiler.serviceprovider.GraalServices;
  28 
  29 /**
  30  * A consistent source of timing data that should be used by all facilities in the debug package.
  31  */
  32 public class TimeSource {
  33     private static final boolean USING_THREAD_CPU_TIME = GraalServices.isCurrentThreadCpuTimeSupported();
  34 
  35     /**
  36      * Gets the current time of this thread in nanoseconds from the most accurate timer available on
  37      * the system. The returned value will be the current time in nanoseconds precision but not
  38      * necessarily nanoseconds accuracy.
  39      * <p>
  40      * The intended use case of this method is to measure the time a certain action takes by making
  41      * successive calls to it. It should not be used to measure total times in the sense of a time
  42      * stamp.
  43      *
  44      * @return the current thread's time in nanoseconds
  45      */
  46     public static long getTimeNS() {
  47         return USING_THREAD_CPU_TIME ? GraalServices.getCurrentThreadCpuTime() : System.nanoTime();
  48     }
  49 
  50 }