1 /*
   2  * Copyright (c) 2012, 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 package org.graalvm.compiler.debug.internal;
  24 
  25 import static org.graalvm.compiler.debug.DebugCloseable.VOID_CLOSEABLE;
  26 
  27 import java.util.concurrent.TimeUnit;
  28 
  29 import org.graalvm.compiler.debug.Debug;
  30 import org.graalvm.compiler.debug.DebugCloseable;
  31 import org.graalvm.compiler.debug.DebugTimer;
  32 import org.graalvm.compiler.debug.TimeSource;
  33 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl;
  34 
  35 public final class TimerImpl extends AccumulatedDebugValue implements DebugTimer {
  36     private final boolean intercepting;
  37 
  38     /**
  39      * Records the most recent active timer.
  40      */
  41     private static final ThreadLocal<CloseableCounterImpl> currentTimer = new ThreadLocal<>();
  42 
  43     static class FlatTimer extends DebugValue implements DebugTimer {
  44         private TimerImpl accm;
  45 
  46         FlatTimer(String name, boolean conditional) {
  47             super(name + "_Flat", conditional);
  48         }
  49 
  50         @Override
  51         public String toString(long value) {
  52             return valueToString(value);
  53         }
  54 
  55         @Override
  56         public TimeUnit getTimeUnit() {
  57             return accm.getTimeUnit();
  58         }
  59 
  60         @Override
  61         public DebugCloseable start() {
  62             return accm.start();
  63         }
  64 
  65         @Override
  66         public String rawUnit() {
  67             return "us";
  68         }
  69 
  70         @Override
  71         public String toRawString(long value) {
  72             return valueToRawString(value);
  73         }
  74     }
  75 
  76     public TimerImpl(String name, boolean conditional, boolean intercepting) {
  77         super(name, conditional, new FlatTimer(name, conditional));
  78         ((FlatTimer) flat).accm = this;
  79         this.intercepting = intercepting;
  80     }
  81 
  82     @Override
  83     public DebugCloseable start() {
  84         if (!isConditional() || Debug.isTimeEnabled()) {
  85             AbstractTimer result = intercepting ? new InterceptingTimer(this) : new Timer(this);
  86             currentTimer.set(result);
  87             return result;
  88         } else {
  89             return VOID_CLOSEABLE;
  90         }
  91     }
  92 
  93     public static String valueToString(long value) {
  94         return String.format("%d.%d ms", value / 1000000, (value / 100000) % 10);
  95     }
  96 
  97     @Override
  98     public DebugTimer getFlat() {
  99         return (FlatTimer) flat;
 100     }
 101 
 102     @Override
 103     public String toString(long value) {
 104         return valueToString(value);
 105     }
 106 
 107     @Override
 108     public TimeUnit getTimeUnit() {
 109         return TimeUnit.NANOSECONDS;
 110     }
 111 
 112     private abstract class AbstractTimer extends CloseableCounterImpl implements DebugCloseable {
 113 
 114         private AbstractTimer(AccumulatedDebugValue counter) {
 115             super(currentTimer.get(), counter);
 116         }
 117 
 118         @Override
 119         public void close() {
 120             super.close();
 121             currentTimer.set(parent);
 122         }
 123     }
 124 
 125     private final class Timer extends AbstractTimer {
 126 
 127         private Timer(TimerImpl timer) {
 128             super(timer);
 129         }
 130 
 131         @Override
 132         protected long getCounterValue() {
 133             return TimeSource.getTimeNS();
 134         }
 135 
 136     }
 137 
 138     private final class InterceptingTimer extends AbstractTimer {
 139 
 140         private InterceptingTimer(TimerImpl timer) {
 141             super(timer);
 142         }
 143 
 144         @Override
 145         protected long getCounterValue() {
 146             return TimeSource.getTimeNS();
 147         }
 148 
 149         @Override
 150         protected void interceptDifferenceAccm(long difference) {
 151             if (Debug.isMethodMeterEnabled()) {
 152                 MethodMetricsImpl.addToCurrentScopeMethodMetrics(counter.getName(), difference);
 153             }
 154         }
 155 
 156         @Override
 157         protected void interceptDifferenceFlat(long difference) {
 158             if (Debug.isMethodMeterEnabled()) {
 159                 MethodMetricsImpl.addToCurrentScopeMethodMetrics(counter.flat.getName(), difference);
 160             }
 161         }
 162     }
 163 
 164     @Override
 165     public String rawUnit() {
 166         return "us";
 167     }
 168 
 169     @Override
 170     public String toRawString(long value) {
 171         return valueToRawString(value);
 172     }
 173 
 174     public static String valueToRawString(long value) {
 175         return Long.toString(value / 1000);
 176     }
 177 
 178 }