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