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