1 /* 2 * Copyright (c) 2014, 2015, 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 jdk.vm.ci.common; 24 25 import java.util.concurrent.atomic.AtomicInteger; 26 27 /** 28 * A facility for timing a step in the runtime initialization sequence. This is independent from all 29 * other JVMCI code so as to not perturb the initialization sequence. It is enabled by setting the 30 * {@code "jvmci.inittimer"} system property to {@code "true"}. 31 */ 32 public final class InitTimer implements AutoCloseable { 33 private final String name; 34 private final long start; 35 36 private InitTimer(String name) { 37 int n = nesting.getAndIncrement(); 38 if (n == 0) { 39 initializingThread = Thread.currentThread(); 40 System.out.println("INITIALIZING THREAD: " + initializingThread); 41 } else { 42 assert Thread.currentThread() == initializingThread : Thread.currentThread() + " != " + initializingThread; 43 } 44 this.name = name; 45 this.start = System.currentTimeMillis(); 46 System.out.println("START: " + SPACES.substring(0, n * 2) + name); 47 } 48 49 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "only the initializing thread accesses this field") 50 public void close() { 51 final long end = System.currentTimeMillis(); 52 int n = nesting.decrementAndGet(); 53 System.out.println(" DONE: " + SPACES.substring(0, n * 2) + name + " [" + (end - start) + " ms]"); 54 if (n == 0) { 55 initializingThread = null; 56 } 57 } 58 59 public static InitTimer timer(String name) { 60 return ENABLED ? new InitTimer(name) : null; 61 } 62 63 public static InitTimer timer(String name, Object suffix) { 64 return ENABLED ? new InitTimer(name + suffix) : null; 65 } 66 67 /** 68 * Specifies if initialization timing is enabled. Note: This property cannot use 69 * {@code HotSpotJVMCIRuntime.Option} since that class is not visible from this package. 70 */ 71 private static final boolean ENABLED = Boolean.getBoolean("jvmci.InitTimer"); 72 73 public static final AtomicInteger nesting = ENABLED ? new AtomicInteger() : null; 74 public static final String SPACES = " "; 75 76 /** 77 * Used to assert the invariant that all related initialization happens on the same thread. 78 */ 79 static Thread initializingThread; 80 }