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