8199927: Make WhiteBox more GC agnostic
authorpliden
Wed, 28 Mar 2018 11:38:47 +0200
changeset 49630 2f1b308b4469
parent 49629 b786280276dc
child 49631 2520a95cddf7
8199927: Make WhiteBox more GC agnostic Reviewed-by: shade, eosterlund
src/hotspot/share/prims/whitebox.cpp
test/jtreg-ext/requires/VMProps.java
test/lib/sun/hotspot/WhiteBox.java
test/lib/sun/hotspot/gc/GC.java
--- a/src/hotspot/share/prims/whitebox.cpp	Wed Mar 28 11:38:47 2018 +0200
+++ b/src/hotspot/share/prims/whitebox.cpp	Wed Mar 28 11:38:47 2018 +0200
@@ -32,6 +32,7 @@
 #include "code/codeCache.hpp"
 #include "compiler/methodMatcher.hpp"
 #include "compiler/directivesParser.hpp"
+#include "gc/shared/gcConfig.hpp"
 #include "gc/shared/genCollectedHeap.hpp"
 #include "jvmtifiles/jvmtiEnv.hpp"
 #include "memory/metadataFactory.hpp"
@@ -312,47 +313,16 @@
                                         (size_t) magnitude, (size_t) iterations);
 WB_END
 
-static const jint serial_code   = 1;
-static const jint parallel_code = 2;
-static const jint cms_code      = 4;
-static const jint g1_code       = 8;
-
-WB_ENTRY(jint, WB_CurrentGC(JNIEnv* env, jobject o, jobject obj))
-  if (UseSerialGC) {
-    return serial_code;
-  } else if (UseParallelGC || UseParallelOldGC) {
-    return parallel_code;
-  } if (UseConcMarkSweepGC) {
-    return cms_code;
-  } else if (UseG1GC) {
-    return g1_code;
-  }
-  ShouldNotReachHere();
-  return 0;
+WB_ENTRY(jboolean, WB_IsGCSupported(JNIEnv* env, jobject o, jint name))
+  return GCConfig::is_gc_supported((CollectedHeap::Name)name);
 WB_END
 
-WB_ENTRY(jint, WB_AllSupportedGC(JNIEnv* env, jobject o, jobject obj))
-#if INCLUDE_ALL_GCS
-  return serial_code | parallel_code | cms_code | g1_code;
-#else
-  return serial_code;
-#endif // INCLUDE_ALL_GCS
+WB_ENTRY(jboolean, WB_IsGCSelected(JNIEnv* env, jobject o, jint name))
+  return GCConfig::is_gc_selected((CollectedHeap::Name)name);
 WB_END
 
-WB_ENTRY(jboolean, WB_GCSelectedByErgo(JNIEnv* env, jobject o, jobject obj))
-  if (UseSerialGC) {
-    return FLAG_IS_ERGO(UseSerialGC);
-  } else if (UseParallelGC) {
-    return FLAG_IS_ERGO(UseParallelGC);
-  } else if (UseParallelOldGC) {
-    return FLAG_IS_ERGO(UseParallelOldGC);
-  } else if (UseConcMarkSweepGC) {
-    return FLAG_IS_ERGO(UseConcMarkSweepGC);
-  } else if (UseG1GC) {
-    return FLAG_IS_ERGO(UseG1GC);
-  }
-  ShouldNotReachHere();
-  return false;
+WB_ENTRY(jboolean, WB_IsGCSelectedErgonomically(JNIEnv* env, jobject o))
+  return GCConfig::is_gc_selected_ergonomically();
 WB_END
 
 WB_ENTRY(jboolean, WB_isObjectInOldGen(JNIEnv* env, jobject o, jobject obj))
@@ -2162,10 +2132,10 @@
   {CC"handshakeWalkStack", CC"(Ljava/lang/Thread;Z)I", (void*)&WB_HandshakeWalkStack },
   {CC"addCompilerDirective",    CC"(Ljava/lang/String;)I",
                                                       (void*)&WB_AddCompilerDirective },
-  {CC"removeCompilerDirective",   CC"(I)V",             (void*)&WB_RemoveCompilerDirective },
-  {CC"currentGC",                 CC"()I",            (void*)&WB_CurrentGC},
-  {CC"allSupportedGC",            CC"()I",            (void*)&WB_AllSupportedGC},
-  {CC"gcSelectedByErgo",          CC"()Z",            (void*)&WB_GCSelectedByErgo},
+  {CC"removeCompilerDirective",   CC"(I)V",           (void*)&WB_RemoveCompilerDirective },
+  {CC"isGCSupported",             CC"(I)Z",           (void*)&WB_IsGCSupported},
+  {CC"isGCSelected",              CC"(I)Z",           (void*)&WB_IsGCSelected},
+  {CC"isGCSelectedErgonomically", CC"()Z",            (void*)&WB_IsGCSelectedErgonomically},
   {CC"supportsConcurrentGCPhaseControl", CC"()Z",     (void*)&WB_SupportsConcurrentGCPhaseControl},
   {CC"getConcurrentGCPhases",     CC"()[Ljava/lang/String;",
                                                       (void*)&WB_GetConcurrentGCPhases},
--- a/test/jtreg-ext/requires/VMProps.java	Wed Mar 28 11:38:47 2018 +0200
+++ b/test/jtreg-ext/requires/VMProps.java	Wed Mar 28 11:38:47 2018 +0200
@@ -228,13 +228,9 @@
      *    User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC
      * @param map - property-value pairs
      */
-    protected void vmGC(Map<String, String> map){
-        GC currentGC = GC.current();
-        boolean isByErgo = GC.currentSetByErgo();
-        List<GC> supportedGC = GC.allSupported();
+    protected void vmGC(Map<String, String> map) {
         for (GC gc: GC.values()) {
-            boolean isSupported = supportedGC.contains(gc);
-            boolean isAcceptable = isSupported && (gc == currentGC || isByErgo);
+            boolean isAcceptable = gc.isSupported() && (gc.isSelected() || GC.isSelectedErgonomically());
             map.put("vm.gc." + gc.name(), "" + isAcceptable);
         }
     }
--- a/test/lib/sun/hotspot/WhiteBox.java	Wed Mar 28 11:38:47 2018 +0200
+++ b/test/lib/sun/hotspot/WhiteBox.java	Wed Mar 28 11:38:47 2018 +0200
@@ -382,9 +382,9 @@
 
   // Don't use these methods directly
   // Use sun.hotspot.gc.GC class instead.
-  public native int currentGC();
-  public native int allSupportedGC();
-  public native boolean gcSelectedByErgo();
+  public native boolean isGCSupported(int name);
+  public native boolean isGCSelected(int name);
+  public native boolean isGCSelectedErgonomically();
 
   // Force Young GC
   public native void youngGC();
--- a/test/lib/sun/hotspot/gc/GC.java	Wed Mar 28 11:38:47 2018 +0200
+++ b/test/lib/sun/hotspot/gc/GC.java	Wed Mar 28 11:38:47 2018 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 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,8 +23,6 @@
 
 package sun.hotspot.gc;
 
-import java.util.ArrayList;
-import java.util.List;
 import sun.hotspot.WhiteBox;
 
 /**
@@ -32,72 +30,41 @@
  * retrieved from the VM with the WhiteBox API.
  */
 public enum GC {
+    /*
+     * Enum values much match CollectedHeap::Name
+     */
     Serial(1),
     Parallel(2),
-    ConcMarkSweep(4),
-    G1(8);
+    ConcMarkSweep(3),
+    G1(4);
 
-    private static final GC CURRENT_GC;
-    private static final int ALL_GC_CODES;
-    private static final boolean IS_BY_ERGO;
-    static {
-        WhiteBox WB = WhiteBox.getWhiteBox();
-        ALL_GC_CODES = WB.allSupportedGC();
-        IS_BY_ERGO = WB.gcSelectedByErgo();
+    private static final WhiteBox WB = WhiteBox.getWhiteBox();
 
-        int currentCode = WB.currentGC();
-        GC tmp = null;
-        for (GC gc: GC.values()) {
-            if (gc.code == currentCode) {
-                tmp = gc;
-                break;
-            }
-        }
-        if (tmp == null) {
-            throw new Error("Unknown current GC code " + currentCode);
-        }
-        CURRENT_GC = tmp;
-    }
+    private final int name;
 
-    private final int code;
-    private GC(int code) {
-        this.code = code;
+    private GC(int name) {
+        this.name = name;
     }
 
     /**
-     * @return true if the collector is supported by the VM, false otherwise.
+     * @return true if this GC is supported by the VM
      */
     public boolean isSupported() {
-        return (ALL_GC_CODES & code) != 0;
-    }
-
-
-    /**
-     * @return the current collector used by VM.
-     */
-    public static GC current() {
-        return CURRENT_GC;
+        return WB.isGCSupported(name);
     }
 
     /**
-     * @return true if GC was selected by ergonomic, false if specified
-     * explicitly by the command line flag.
+     * @return true if this GC is currently selected/used
      */
-    public static boolean currentSetByErgo() {
-        return IS_BY_ERGO;
+    public boolean isSelected() {
+        return WB.isGCSelected(name);
     }
 
     /**
-     * @return List of collectors supported by the VM.
+     * @return true if GC was selected ergonomically, as opposed
+     *         to being explicitly specified on the command line
      */
-    public static List<GC> allSupported() {
-        List<GC> list = new ArrayList<>();
-        for (GC gc: GC.values()) {
-            if (gc.isSupported()) {
-                list.add(gc);
-            }
-        }
-        return list;
+    public static boolean isSelectedErgonomically() {
+        return WB.isGCSelectedErgonomically();
     }
 }
-