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.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 }