001/*
002 * Copyright (c) 2014, 2015, 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 jdk.internal.jvmci.hotspot;
024
025/**
026 * A facility for timing a step in the runtime initialization sequence. This must be independent
027 * from all other JVMCI code so as to not perturb the initialization sequence.
028 */
029public final class InitTimer implements AutoCloseable {
030    final String name;
031    final long start;
032
033    private InitTimer(String name) {
034        this.name = name;
035        this.start = System.currentTimeMillis();
036        System.out.println("START: " + SPACES.substring(0, timerDepth * 2) + name);
037        assert Thread.currentThread() == initializingThread : Thread.currentThread() + " != " + initializingThread;
038        timerDepth++;
039    }
040
041    @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "only the initializing thread accesses this field")
042    public void close() {
043        final long end = System.currentTimeMillis();
044        timerDepth--;
045        System.out.println(" DONE: " + SPACES.substring(0, timerDepth * 2) + name + " [" + (end - start) + " ms]");
046    }
047
048    public static InitTimer timer(String name) {
049        return ENABLED ? new InitTimer(name) : null;
050    }
051
052    public static InitTimer timer(String name, Object suffix) {
053        return ENABLED ? new InitTimer(name + suffix) : null;
054    }
055
056    /**
057     * Specifies if initialization timing is enabled. This can only be set via a system property as
058     * the timing facility is used to time initialization of {@link HotSpotOptions}.
059     */
060    private static final boolean ENABLED = Boolean.getBoolean("jvmci.runtime.TimeInit");
061
062    public static int timerDepth = 0;
063    public static final String SPACES = "                                            ";
064
065    /**
066     * Used to assert the invariant that all initialization happens on the same thread.
067     */
068    public static final Thread initializingThread;
069    static {
070        if (ENABLED) {
071            initializingThread = Thread.currentThread();
072            System.out.println("INITIALIZING THREAD: " + initializingThread);
073        } else {
074            initializingThread = null;
075        }
076    }
077}