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;
  24 
  25 import static org.graalvm.compiler.debug.DebugCloseable.VOID_CLOSEABLE;
  26 
  27 import java.util.concurrent.TimeUnit;
  28 
  29 import org.graalvm.util.Pair;
  30 
  31 final class TimerKeyImpl extends AccumulatedKey implements TimerKey {
  32     static class FlatTimer extends AbstractKey implements TimerKey {
  33         private TimerKeyImpl accm;
  34 
  35         FlatTimer(String nameFormat, Object nameArg1, Object nameArg2) {
  36             super(nameFormat, nameArg1, nameArg2);
  37         }
  38 
  39         @Override
  40         protected String createName(String format, Object arg1, Object arg2) {
  41             return super.createName(format, arg1, arg2) + FLAT_KEY_SUFFIX;
  42         }
  43 
  44         @Override
  45         public String toHumanReadableFormat(long value) {
  46             return valueToString(value);
  47         }
  48 
  49         @Override
  50         public TimeUnit getTimeUnit() {
  51             return accm.getTimeUnit();
  52         }
  53 
  54         @Override
  55         public DebugCloseable start(DebugContext debug) {
  56             return accm.start(debug);
  57         }
  58 
  59         @Override
  60         public Pair<String, String> toCSVFormat(long value) {
  61             return TimerKeyImpl.toCSVFormatHelper(value);
  62         }
  63 
  64         @Override
  65         public TimerKey doc(String doc) {
  66             throw new IllegalArgumentException("Cannot set documentation for derived key " + getName());
  67         }
  68 
  69         @Override
  70         public String getDocName() {
  71             return null;
  72         }
  73     }
  74 
  75     TimerKeyImpl(String nameFormat, Object nameArg1, Object nameArg2) {
  76         super(new FlatTimer(nameFormat, nameArg1, nameArg2), nameFormat, nameArg1, nameArg2);
  77         ((FlatTimer) flat).accm = this;
  78     }
  79 
  80     @Override
  81     public DebugCloseable start(DebugContext debug) {
  82         if (debug.isTimerEnabled(this)) {
  83             Timer result = new Timer(this, debug);
  84             debug.currentTimer = result;
  85             return result;
  86         } else {
  87             return VOID_CLOSEABLE;
  88         }
  89     }
  90 
  91     public static String valueToString(long value) {
  92         return String.format("%d.%d ms", value / 1000000, (value / 100000) % 10);
  93     }
  94 
  95     @Override
  96     public TimerKey getFlat() {
  97         return (FlatTimer) flat;
  98     }
  99 
 100     @Override
 101     public String toHumanReadableFormat(long value) {
 102         return valueToString(value);
 103     }
 104 
 105     @Override
 106     public TimeUnit getTimeUnit() {
 107         return TimeUnit.NANOSECONDS;
 108     }
 109 
 110     final class Timer extends CloseableCounter implements DebugCloseable {
 111         final DebugContext debug;
 112 
 113         Timer(AccumulatedKey counter, DebugContext debug) {
 114             super(debug, debug.currentTimer, counter);
 115             this.debug = debug;
 116         }
 117 
 118         @Override
 119         public void close() {
 120             super.close();
 121             debug.currentTimer = parent;
 122         }
 123 
 124         @Override
 125         protected long getCounterValue() {
 126             return TimeSource.getTimeNS();
 127         }
 128 
 129     }
 130 
 131     @Override
 132     public Pair<String, String> toCSVFormat(long value) {
 133         return toCSVFormatHelper(value);
 134     }
 135 
 136     static Pair<String, String> toCSVFormatHelper(long value) {
 137         return Pair.create(Long.toString(value / 1000), "us");
 138     }
 139 
 140     @Override
 141     public TimerKey doc(String doc) {
 142         setDoc(doc);
 143         return this;
 144     }
 145 }