8174734: Safepoint sync time did not increase
authordholmes
Wed, 06 Jun 2018 17:10:37 -0400
changeset 50432 3a91f09a46be
parent 50431 64e4b1686141
child 50433 2bafeb7a1f6b
8174734: Safepoint sync time did not increase Reviewed-by: coleenp, hseigel
test/jdk/ProblemList.txt
test/jdk/sun/management/HotspotRuntimeMBean/GetSafepointSyncTime.java
--- a/test/jdk/ProblemList.txt	Wed Jun 06 09:37:44 2018 -0700
+++ b/test/jdk/ProblemList.txt	Wed Jun 06 17:10:37 2018 -0400
@@ -545,7 +545,6 @@
 
 com/sun/management/OperatingSystemMXBean/GetProcessCpuLoad.java 8030957 aix-all
 com/sun/management/OperatingSystemMXBean/GetSystemCpuLoad.java  8030957 aix-all
-sun/management/HotspotRuntimeMBean/GetSafepointSyncTime.java    8174734 generic-all
 
 ############################################################################
 
--- a/test/jdk/sun/management/HotspotRuntimeMBean/GetSafepointSyncTime.java	Wed Jun 06 09:37:44 2018 -0700
+++ b/test/jdk/sun/management/HotspotRuntimeMBean/GetSafepointSyncTime.java	Wed Jun 06 17:10:37 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug     4858522
+ * @bug     4858522 8174734
  * @summary Basic unit test of HotspotRuntimeMBean.getSafepointSyncTime()
  * @author  Steve Bohne
  *
@@ -43,50 +43,71 @@
 
     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();
 
-        // Thread.getAllStackTraces() should cause safepoints.
-        // If this test is failing because it doesn't,
-        // MIN_VALUE_FOR_PASS should be reset to 0
+        checkPositive(count, "count");
+        checkPositive(time, "time");
+
+        // Thread.getAllStackTraces() should cause a safepoint.
+
         for (int i = 0; i < NUM_THREAD_DUMPS; i++) {
             Thread.getAllStackTraces();
         }
 
         long count1 = mbean.getSafepointCount();
-        long value1 = mbean.getSafepointSyncTime();
-
-        System.out.format("Safepoint count=%d (diff=%d), sync time=%d ms (diff=%d)%n",
-                          count1, count1-count, value1, value1-value);
+        long time1 = mbean.getSafepointSyncTime();
 
-        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 + ")");
-        }
+        validate(count, count1, time, time1, "Pass 1");
+
+        // 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.");
     }