Path: blob/master/test/jdk/sun/management/HotspotRuntimeMBean/GetSafepointSyncTime.java
41149 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4858522 817473426* @summary Basic unit test of HotspotRuntimeMBean.getSafepointSyncTime()27* @author Steve Bohne28*29* @run main/othervm -XX:+UsePerfData GetSafepointSyncTime30*/3132/*33* This test is just a sanity check and does not check for the correct value.34*/3536import sun.management.*;3738public class GetSafepointSyncTime {3940private static HotspotRuntimeMBean mbean =41(HotspotRuntimeMBean)ManagementFactoryHelper.getHotspotRuntimeMBean();4243private static final long NUM_THREAD_DUMPS = 300;4445static void checkPositive(long value, String label) {46if (value < 0)47throw new RuntimeException(label + " had a negative value of "48+ value);49}5051static void validate(long count1, long count2, long time1, long time2,52String label) {53checkPositive(count1, label + ":count1");54checkPositive(count2, label + ":count2");55checkPositive(time1, label + ":time1");56checkPositive(time2, label + ":time2");5758long countDiff = count2 - count1;59long timeDiff = time2 - time1;6061if (countDiff < NUM_THREAD_DUMPS) {62throw new RuntimeException(label +63": Expected at least " + NUM_THREAD_DUMPS +64" safepoints but only got " + countDiff);65}6667// getSafepointSyncTime is the accumulated time spent getting to a68// safepoint, so each safepoint will add a little to this, but the69// resolution is only milliseconds so we may not see it.70if (timeDiff < 0) {71throw new RuntimeException(label + ": Safepoint sync time " +72"decreased unexpectedly " +73"(time1 = " + time1 + "; " +74"time2 = " + time2 + ")");75}7677System.out.format("%s: Safepoint count=%d (diff=%d), sync time=%d ms (diff=%d)%n",78label, count2, countDiff, time2, timeDiff);7980}8182public static void main(String args[]) throws Exception {83long count = mbean.getSafepointCount();84long time = mbean.getSafepointSyncTime();8586checkPositive(count, "count");87checkPositive(time, "time");8889// Thread.getAllStackTraces() should cause a safepoint.9091for (int i = 0; i < NUM_THREAD_DUMPS; i++) {92Thread.getAllStackTraces();93}9495long count1 = mbean.getSafepointCount();96long time1 = mbean.getSafepointSyncTime();9798validate(count, count1, time, time1, "Pass 1");99100// repeat the experiment101102for (int i = 0; i < NUM_THREAD_DUMPS; i++) {103Thread.getAllStackTraces();104}105106long count2 = mbean.getSafepointCount();107long time2 = mbean.getSafepointSyncTime();108109validate(count1, count2, time1, time2, "Pass 2");110111System.out.println("Test passed.");112}113}114115116