DebugTimerTest.java revision 12651:6ef01bd40ce2
119370Spst/*
219370Spst * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3130803Smarcel * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4130803Smarcel *
5130803Smarcel * This code is free software; you can redistribute it and/or modify it
6130803Smarcel * under the terms of the GNU General Public License version 2 only, as
798944Sobrien * published by the Free Software Foundation.
819370Spst *
998944Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1098944Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1198944Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1298944Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1319370Spst * accompanied this code).
1498944Sobrien *
1598944Sobrien * You should have received a copy of the GNU General Public License version
1698944Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1798944Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1819370Spst *
1998944Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2098944Sobrien * or visit www.oracle.com if you need additional information or have any
2198944Sobrien * questions.
2298944Sobrien */
2319370Spstpackage org.graalvm.compiler.debug.test;
2419370Spst
2519370Spstimport static org.junit.Assert.assertEquals;
2619370Spstimport static org.junit.Assert.assertTrue;
2719370Spst
2819370Spstimport java.lang.management.ThreadMXBean;
2919370Spst
3019370Spstimport org.junit.Assert;
3119370Spstimport org.junit.Assume;
32130803Smarcelimport org.junit.Before;
3319370Spstimport org.junit.Test;
3419370Spst
35130803Smarcelimport org.graalvm.compiler.debug.Debug;
36130803Smarcelimport org.graalvm.compiler.debug.DebugCloseable;
37130803Smarcelimport org.graalvm.compiler.debug.DebugConfig;
3819370Spstimport org.graalvm.compiler.debug.DebugConfigScope;
3919370Spstimport org.graalvm.compiler.debug.DebugTimer;
40130803Smarcelimport org.graalvm.compiler.debug.Management;
4119370Spst
4219370Spst@SuppressWarnings("try")
4319370Spstpublic class DebugTimerTest {
4419370Spst
4519370Spst    private static final ThreadMXBean threadMXBean = Management.getThreadMXBean();
4619370Spst
4719370Spst    @Before
4819370Spst    public void checkCapabilities() {
4919370Spst        Assume.assumeTrue("skipping management interface test", threadMXBean.isCurrentThreadCpuTimeSupported());
5019370Spst    }
5119370Spst
5219370Spst    /**
5319370Spst     * Actively spins the current thread for at least a given number of milliseconds in such a way
5419370Spst     * that timers for the current thread keep ticking over.
5519370Spst     *
5619370Spst     * @return the number of milliseconds actually spent spinning which is guaranteed to be >=
5719370Spst     *         {@code ms}
5898944Sobrien     */
5998944Sobrien    private static long spin(long ms) {
6019370Spst        long start = threadMXBean.getCurrentThreadCpuTime();
6198944Sobrien        do {
6298944Sobrien            long durationMS = (threadMXBean.getCurrentThreadCpuTime() - start) / 1000;
6319370Spst            if (durationMS >= ms) {
6498944Sobrien                return durationMS;
6519370Spst            }
6698944Sobrien        } while (true);
6719370Spst    }
6898944Sobrien
6919370Spst    @Test
7098944Sobrien    public void test1() {
7198944Sobrien        DebugConfig debugConfig = Debug.fixedConfig(0, 0, false, false, true, false, false, null, null, System.out);
7219370Spst        try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) {
7398944Sobrien
7446283Sdfr            DebugTimer timerA = Debug.timer("TimerA");
7598944Sobrien            DebugTimer timerB = Debug.timer("TimerB");
7646283Sdfr
7798944Sobrien            long spinA;
7898944Sobrien            long spinB;
7998944Sobrien
8098944Sobrien            try (DebugCloseable a1 = timerA.start()) {
8198944Sobrien                spinA = spin(50);
8298944Sobrien                try (DebugCloseable b1 = timerB.start()) {
8319370Spst                    spinB = spin(50);
84130803Smarcel                }
8519370Spst            }
8698944Sobrien
8798944Sobrien            Assert.assertTrue(timerB.getCurrentValue() < timerA.getCurrentValue());
8819370Spst            if (timerA.getFlat() != null && timerB.getFlat() != null) {
8919370Spst                assertTrue(spinB >= spinA || timerB.getFlat().getCurrentValue() < timerA.getFlat().getCurrentValue());
9019370Spst                assertEquals(timerA.getFlat().getCurrentValue(), timerA.getCurrentValue() - timerB.getFlat().getCurrentValue(), 10D);
91130803Smarcel            }
92130803Smarcel        }
93130803Smarcel    }
9419370Spst
9598944Sobrien    @Test
9619370Spst    public void test2() {
97130803Smarcel        DebugConfig debugConfig = Debug.fixedConfig(0, 0, false, false, true, false, false, null, null, System.out);
98130803Smarcel        try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) {
99130803Smarcel            DebugTimer timerC = Debug.timer("TimerC");
100130803Smarcel            try (DebugCloseable c1 = timerC.start()) {
10119370Spst                spin(50);
102130803Smarcel                try (DebugCloseable c2 = timerC.start()) {
103130803Smarcel                    spin(50);
10419370Spst                    try (DebugCloseable c3 = timerC.start()) {
105130803Smarcel                        spin(50);
106130803Smarcel                        try (DebugCloseable c4 = timerC.start()) {
107130803Smarcel                            spin(50);
10819370Spst                            try (DebugCloseable c5 = timerC.start()) {
10919370Spst                                spin(50);
11019370Spst                            }
11119370Spst                        }
11219370Spst                    }
11319370Spst                }
11419370Spst            }
11519370Spst            if (timerC.getFlat() != null) {
11619370Spst                assertEquals(timerC.getFlat().getCurrentValue(), timerC.getCurrentValue());
11719370Spst            }
11819370Spst        }
119130803Smarcel    }
12019370Spst
121130803Smarcel    @Test
122130803Smarcel    public void test3() {
12319370Spst        DebugConfig debugConfig = Debug.fixedConfig(0, 0, false, false, true, false, false, null, null, System.out);
12419370Spst        try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) {
12519370Spst
12619370Spst            DebugTimer timerD = Debug.timer("TimerD");
12719370Spst            DebugTimer timerE = Debug.timer("TimerE");
12898944Sobrien
12998944Sobrien            long spinD1;
13019370Spst            long spinE;
13119370Spst
13219370Spst            try (DebugCloseable d1 = timerD.start()) {
13319370Spst                spinD1 = spin(50);
13498944Sobrien                try (DebugCloseable e1 = timerE.start()) {
13519370Spst                    spinE = spin(50);
13619370Spst                    try (DebugCloseable d2 = timerD.start()) {
13719370Spst                        spin(50);
13819370Spst                        try (DebugCloseable d3 = timerD.start()) {
13998944Sobrien                            spin(50);
14019370Spst                        }
141130803Smarcel                    }
14219370Spst                }
14398944Sobrien            }
14419370Spst
145130803Smarcel            Assert.assertTrue(timerE.getCurrentValue() < timerD.getCurrentValue());
14698944Sobrien            if (timerD.getFlat() != null && timerE.getFlat() != null) {
14798944Sobrien                assertTrue(spinE >= spinD1 || timerE.getFlat().getCurrentValue() < timerD.getFlat().getCurrentValue());
14819370Spst                assertEquals(timerD.getFlat().getCurrentValue(), timerD.getCurrentValue() - timerE.getFlat().getCurrentValue(), 10D);
149130803Smarcel            }
15019370Spst        }
15119370Spst    }
15219370Spst}
15319370Spst