< prev index next >

test/jdk/sun/management/HotspotRuntimeMBean/GetSafepointSyncTime.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -21,11 +21,11 @@
  * questions.
  */
 
 /*
  * @test
- * @bug     4858522
+ * @bug     4858522 8174734
  * @summary Basic unit test of HotspotRuntimeMBean.getSafepointSyncTime()
  * @author  Steve Bohne
  *
  * @run main/othervm -XX:+UsePerfData GetSafepointSyncTime
  */

@@ -41,53 +41,74 @@
     private static HotspotRuntimeMBean mbean =
         (HotspotRuntimeMBean)ManagementFactoryHelper.getHotspotRuntimeMBean();
 
     private static final long NUM_THREAD_DUMPS = 300;
 
-    // Careful with these values.
-    private static final long MIN_VALUE_FOR_PASS = 1;
-    private static final long MAX_VALUE_FOR_PASS = Long.MAX_VALUE;
+    static void checkPositive(long value, String label) {
+        if (value < 0)
+            throw new RuntimeException(label + " had a negative value of "
+                                       + value);
+    }
+
+    static void validate(long count1, long count2, long time1, long time2,
+                         String label) {
+        checkPositive(count1, label + ":count1");
+        checkPositive(count2, label + ":count2");
+        checkPositive(time1, label + ":time1");
+        checkPositive(time2, label + ":time2");
+
+        long countDiff = count2 - count1;
+        long timeDiff = time2 - time1;
+
+        if (countDiff < NUM_THREAD_DUMPS) {
+            throw new RuntimeException(label +
+                                       ": Expected at least " + NUM_THREAD_DUMPS +
+                                       "safepoints but only got " + countDiff);
+        }
+
+        // getSafepointSyncTime is the accumulated time spent getting to a
+        // safepoint, so each safepoint will add a little to this, but the
+        // resolution is only milliseconds so we may not see it.
+        if (timeDiff < 0) {
+            throw new RuntimeException(label + ": Safepoint sync time " +
+                                       "decreased unexpectedly " +
+                                       "(time1 = " + time1 + "; " +
+                                       "time2 = " + time2 + ")");
+        }
+
+        System.out.format("%s: Safepoint count=%d (diff=%d), sync time=%d ms (diff=%d)%n",
+                          label, count2, countDiff, time2, timeDiff);
+
+    }
 
     public static void main(String args[]) throws Exception {
         long count = mbean.getSafepointCount();
-        long value = mbean.getSafepointSyncTime();
+        long time = mbean.getSafepointSyncTime();
+
+        checkPositive(count, "count");
+        checkPositive(time, "time");
+
+        // Thread.getAllStackTraces() should cause a safepoint.
 
-        // Thread.getAllStackTraces() should cause safepoints.
-        // If this test is failing because it doesn't,
-        // MIN_VALUE_FOR_PASS should be reset to 0
         for (int i = 0; i < NUM_THREAD_DUMPS; i++) {
             Thread.getAllStackTraces();
         }
 
         long count1 = mbean.getSafepointCount();
-        long value1 = mbean.getSafepointSyncTime();
+        long time1 = mbean.getSafepointSyncTime();
 
-        System.out.format("Safepoint count=%d (diff=%d), sync time=%d ms (diff=%d)%n",
-                          count1, count1-count, value1, value1-value);
+        validate(count, count1, time, time1, "Pass 1");
 
-        if (value1 < MIN_VALUE_FOR_PASS || value1 > MAX_VALUE_FOR_PASS) {
-            throw new RuntimeException("Safepoint sync time " +
-                                       "illegal value: " + value1 + " ms " +
-                                       "(MIN = " + MIN_VALUE_FOR_PASS + "; " +
-                                       "MAX = " + MAX_VALUE_FOR_PASS + ")");
-        }
+        // repeat the experiment
 
         for (int i = 0; i < NUM_THREAD_DUMPS; i++) {
             Thread.getAllStackTraces();
         }
 
         long count2 = mbean.getSafepointCount();
-        long value2 = mbean.getSafepointSyncTime();
-
-        System.out.format("Safepoint count=%d (diff=%d), sync time=%d ms (diff=%d)%n",
-                          count2, count2-count1, value2, value2-value1);
+        long time2 = mbean.getSafepointSyncTime();
 
-        if (value2 <= value1) {
-            throw new RuntimeException("Safepoint sync time " +
-                                       "did not increase " +
-                                       "(value1 = " + value1 + "; " +
-                                       "value2 = " + value2 + ")");
-        }
+        validate(count1, count2, time1, time2, "Pass 2");
 
         System.out.println("Test passed.");
     }
 }
< prev index next >