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 24 25 package org.graalvm.compiler.debug.test; 26 27 import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM; 28 import static org.graalvm.compiler.debug.DebugContext.NO_CONFIG_CUSTOMIZERS; 29 import static org.graalvm.compiler.debug.DebugContext.NO_DESCRIPTION; 30 import static org.graalvm.compiler.debug.DebugContext.NO_GLOBAL_METRIC_VALUES; 31 import static org.junit.Assert.assertEquals; 32 33 import jdk.internal.vm.compiler.collections.EconomicMap; 34 import org.graalvm.compiler.debug.DebugCloseable; 35 import org.graalvm.compiler.debug.DebugContext; 36 import org.graalvm.compiler.debug.DebugOptions; 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.compiler.serviceprovider.GraalServices; 41 import org.junit.Assume; 42 import org.junit.AssumptionViolatedException; 43 import org.junit.Before; 44 import org.junit.Test; 45 46 @SuppressWarnings("try") 47 public class TimerKeyTest { 48 49 @Before 50 public void checkCapabilities() { 51 assumeManagementLibraryIsLoadable(); 52 Assume.assumeTrue("skipping management interface test", GraalServices.isCurrentThreadCpuTimeSupported()); 53 } 54 55 /** @see <a href="https://bugs.openjdk.java.net/browse/JDK-8076557">JDK-8076557</a> */ 56 static void assumeManagementLibraryIsLoadable() { 57 try { 58 /* Trigger loading of the management library using the bootstrap class loader. */ 59 GraalServices.getCurrentThreadAllocatedBytes(); 60 } catch (UnsatisfiedLinkError | NoClassDefFoundError | UnsupportedOperationException e) { 61 throw new AssumptionViolatedException("Management interface is unavailable: " + e); 62 } 63 } 64 65 /** 66 * Actively spins the current thread for at least a given number of milliseconds in such a way 67 * that timers for the current thread keep ticking over. 68 * 69 * @return the number of milliseconds actually spent spinning which is guaranteed to be >= 70 * {@code ms} 71 */ 72 private static long spin(long ms) { 73 long start = GraalServices.getCurrentThreadCpuTime(); 74 do { 75 long durationMS = (GraalServices.getCurrentThreadCpuTime() - start) / 1000; 76 if (durationMS >= ms) { 77 return durationMS; 78 } 79 } while (true); 80 } 81 82 /** 83 * Asserts that a timer replied recursively without any other interleaving timers has the same 84 * flat and accumulated times. 85 */ 86 @Test 87 public void test2() { 88 EconomicMap<OptionKey<?>, Object> map = EconomicMap.create(); 89 map.put(DebugOptions.Time, ""); 90 OptionValues options = new OptionValues(map); 91 DebugContext debug = DebugContext.create(options, NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, NO_CONFIG_CUSTOMIZERS); 92 93 TimerKey timerC = DebugContext.timer("TimerC"); 94 try (DebugCloseable c1 = timerC.start(debug)) { 95 spin(50); 96 try (DebugCloseable c2 = timerC.start(debug)) { 97 spin(50); 98 try (DebugCloseable c3 = timerC.start(debug)) { 99 spin(50); 100 try (DebugCloseable c4 = timerC.start(debug)) { 101 spin(50); 102 try (DebugCloseable c5 = timerC.start(debug)) { 103 spin(50); 104 } 105 } 106 } 107 } 108 } 109 if (timerC.getFlat() != null) { 110 assertEquals(timerC.getFlat().getCurrentValue(debug), timerC.getCurrentValue(debug)); 111 } 112 } 113 }