1 /*
   2  * Copyright (c) 2013, 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 org.graalvm.compiler.debug.test;
  24 
  25 import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
  26 import static org.graalvm.compiler.debug.DebugContext.NO_CONFIG_CUSTOMIZERS;
  27 import static org.graalvm.compiler.debug.DebugContext.NO_DESCRIPTION;
  28 import static org.graalvm.compiler.debug.DebugContext.NO_GLOBAL_METRIC_VALUES;
  29 import static org.junit.Assert.assertEquals;
  30 
  31 import java.lang.management.ThreadMXBean;
  32 
  33 import org.graalvm.compiler.debug.DebugCloseable;
  34 import org.graalvm.compiler.debug.DebugContext;
  35 import org.graalvm.compiler.debug.DebugOptions;
  36 import org.graalvm.compiler.debug.Management;
  37 import org.graalvm.compiler.debug.TimerKey;
  38 import org.graalvm.compiler.options.OptionKey;
  39 import org.graalvm.compiler.options.OptionValues;
  40 import org.graalvm.util.EconomicMap;
  41 import org.junit.Assume;
  42 import org.junit.Before;
  43 import org.junit.Test;
  44 
  45 @SuppressWarnings("try")
  46 public class TimerKeyTest {
  47 
  48     private static final ThreadMXBean threadMXBean = Management.getThreadMXBean();
  49 
  50     @Before
  51     public void checkCapabilities() {
  52         Assume.assumeTrue("skipping management interface test", threadMXBean.isCurrentThreadCpuTimeSupported());
  53     }
  54 
  55     /**
  56      * Actively spins the current thread for at least a given number of milliseconds in such a way
  57      * that timers for the current thread keep ticking over.
  58      *
  59      * @return the number of milliseconds actually spent spinning which is guaranteed to be >=
  60      *         {@code ms}
  61      */
  62     private static long spin(long ms) {
  63         long start = threadMXBean.getCurrentThreadCpuTime();
  64         do {
  65             long durationMS = (threadMXBean.getCurrentThreadCpuTime() - start) / 1000;
  66             if (durationMS >= ms) {
  67                 return durationMS;
  68             }
  69         } while (true);
  70     }
  71 
  72     /**
  73      * Asserts that a timer replied recursively without any other interleaving timers has the same
  74      * flat and accumulated times.
  75      */
  76     @Test
  77     public void test2() {
  78         EconomicMap<OptionKey<?>, Object> map = EconomicMap.create();
  79         map.put(DebugOptions.Time, "");
  80         OptionValues options = new OptionValues(map);
  81         DebugContext debug = DebugContext.create(options, NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, NO_CONFIG_CUSTOMIZERS);
  82 
  83         TimerKey timerC = DebugContext.timer("TimerC");
  84         try (DebugCloseable c1 = timerC.start(debug)) {
  85             spin(50);
  86             try (DebugCloseable c2 = timerC.start(debug)) {
  87                 spin(50);
  88                 try (DebugCloseable c3 = timerC.start(debug)) {
  89                     spin(50);
  90                     try (DebugCloseable c4 = timerC.start(debug)) {
  91                         spin(50);
  92                         try (DebugCloseable c5 = timerC.start(debug)) {
  93                             spin(50);
  94                         }
  95                     }
  96                 }
  97             }
  98         }
  99         if (timerC.getFlat() != null) {
 100             assertEquals(timerC.getFlat().getCurrentValue(debug), timerC.getCurrentValue(debug));
 101         }
 102     }
 103 }