1 /*
   2  * Copyright (c) 2003, 2013, 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  * @test
  26  * @bug     4858522
  27  * @summary Basic unit test of HotspotRuntimeMBean.getSafepointSyncTime()
  28  * @author  Steve Bohne
  29  * @run main/othervm -XX:+UsePerfData GetSafepointSyncTime
  30  */
  31 
  32 /*
  33  * This test is just a sanity check and does not check for the correct value.
  34  */
  35 
  36 import sun.management.*;
  37 
  38 public class GetSafepointSyncTime {
  39 
  40     private static HotspotRuntimeMBean mbean =
  41         (HotspotRuntimeMBean)ManagementFactoryHelper.getHotspotRuntimeMBean();
  42 
  43     private static final long NUM_THREAD_DUMPS = 300;
  44 
  45     // Careful with these values.
  46     private static final long MIN_VALUE_FOR_PASS = 1;
  47     private static final long MAX_VALUE_FOR_PASS = Long.MAX_VALUE;
  48 
  49     public static void main(String args[]) throws Exception {
  50         long count = mbean.getSafepointCount();
  51         long value = mbean.getSafepointSyncTime();
  52 
  53         // Thread.getAllStackTraces() should cause safepoints.
  54         // If this test is failing because it doesn't,
  55         // MIN_VALUE_FOR_PASS should be reset to 0
  56         for (int i = 0; i < NUM_THREAD_DUMPS; i++) {
  57             Thread.getAllStackTraces();
  58         }
  59 
  60         long count1 = mbean.getSafepointCount();
  61         long value1 = mbean.getSafepointSyncTime();
  62 
  63         System.out.format("Safepoint count=%d (diff=%d), sync time=%d ms (diff=%d)%n",
  64                           count1, count1-count, value1, value1-value);
  65 
  66         if (value1 < MIN_VALUE_FOR_PASS || value1 > MAX_VALUE_FOR_PASS) {
  67             throw new RuntimeException("Safepoint sync time " +
  68                                        "illegal value: " + value1 + " ms " +
  69                                        "(MIN = " + MIN_VALUE_FOR_PASS + "; " +
  70                                        "MAX = " + MAX_VALUE_FOR_PASS + ")");
  71         }
  72 
  73         for (int i = 0; i < NUM_THREAD_DUMPS; i++) {
  74             Thread.getAllStackTraces();
  75         }
  76 
  77         long count2 = mbean.getSafepointCount();
  78         long value2 = mbean.getSafepointSyncTime();
  79 
  80         System.out.format("Safepoint count=%d (diff=%d), sync time=%d ms (diff=%d)%n",
  81                           count2, count2-count1, value2, value2-value1);
  82 
  83         if (value2 <= value1) {
  84             throw new RuntimeException("Safepoint sync time " +
  85                                        "did not increase " +
  86                                        "(value1 = " + value1 + "; " +
  87                                        "value2 = " + value2 + ")");
  88         }
  89 
  90         System.out.println("Test passed.");
  91     }
  92 }