8199927: Make WhiteBox more GC agnostic

Reviewed-by: shade, eosterlund
This commit is contained in:
Per Lidén 2018-03-28 11:38:47 +02:00
parent 24273f04a4
commit 940bc841a7
4 changed files with 35 additions and 102 deletions

View file

@ -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 @@ import sun.hotspot.WhiteBox;
* 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();
}
}