001/*
002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.debug.internal;
024
025import static com.oracle.graal.debug.DebugCloseable.*;
026
027import java.lang.management.*;
028import java.util.concurrent.*;
029
030import com.oracle.graal.debug.*;
031
032public final class TimerImpl extends AccumulatedDebugValue implements DebugTimer {
033
034    private static final ThreadMXBean threadMXBean = Management.getThreadMXBean();
035
036    /**
037     * Records the most recent active timer.
038     */
039    private static final ThreadLocal<CloseableCounterImpl> currentTimer = new ThreadLocal<>();
040
041    static class FlatTimer extends DebugValue implements DebugTimer {
042        private TimerImpl accm;
043
044        public FlatTimer(String name, boolean conditional) {
045            super(name + "_Flat", conditional);
046        }
047
048        @Override
049        public String toString(long value) {
050            return valueToString(value);
051        }
052
053        public TimeUnit getTimeUnit() {
054            return accm.getTimeUnit();
055        }
056
057        public DebugCloseable start() {
058            return accm.start();
059        }
060    }
061
062    public TimerImpl(String name, boolean conditional) {
063        super(name, conditional, new FlatTimer(name, conditional));
064        ((FlatTimer) flat).accm = this;
065    }
066
067    @Override
068    public DebugCloseable start() {
069        if (!isConditional() || Debug.isTimeEnabled()) {
070            AbstractTimer result;
071            if (threadMXBean.isCurrentThreadCpuTimeSupported()) {
072                result = new CpuTimeTimer(this);
073            } else {
074                result = new SystemNanosTimer(this);
075            }
076            currentTimer.set(result);
077            return result;
078        } else {
079            return VOID_CLOSEABLE;
080        }
081    }
082
083    public static String valueToString(long value) {
084        return String.format("%d.%d ms", value / 1000000, (value / 100000) % 10);
085    }
086
087    public DebugTimer getFlat() {
088        return (FlatTimer) flat;
089    }
090
091    @Override
092    public String toString(long value) {
093        return valueToString(value);
094    }
095
096    public TimeUnit getTimeUnit() {
097        return TimeUnit.NANOSECONDS;
098    }
099
100    private abstract static class AbstractTimer extends CloseableCounterImpl implements DebugCloseable {
101
102        private AbstractTimer(AccumulatedDebugValue counter) {
103            super(currentTimer.get(), counter);
104        }
105
106        @Override
107        public void close() {
108            super.close();
109            currentTimer.set(parent);
110        }
111    }
112
113    private final class SystemNanosTimer extends AbstractTimer {
114
115        public SystemNanosTimer(TimerImpl timer) {
116            super(timer);
117        }
118
119        @Override
120        protected long getCounterValue() {
121            return System.nanoTime();
122        }
123    }
124
125    private final class CpuTimeTimer extends AbstractTimer {
126
127        public CpuTimeTimer(TimerImpl timer) {
128            super(timer);
129        }
130
131        @Override
132        protected long getCounterValue() {
133            return threadMXBean.getCurrentThreadCpuTime();
134        }
135    }
136}