Apache Lucene open-source search software
LUCENE-6239: Removed RAMUsageEstimator's sun.misc.Unsafe calls
Uwe Schindler08bc225ad880
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 5130343602f..f0acfbfe478 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -106,6 +106,9 @@ Other* LUCENE-6193: Collapse identical catch branches in try-catch statements.(shalin)+* LUCENE-6239: Removed RAMUsageEstimator's sun.misc.Unsafe calls.+ (Robert Muir, Dawid Weiss, Uwe Schindler)+======================= Lucene 5.0.0 =======================New Featuresdiff --git a/lucene/core/src/java/org/apache/lucene/util/Constants.java b/lucene/core/src/java/org/apache/lucene/util/Constants.javaindex d4fe9ead95f..3ec275b9a75 100644--- a/lucene/core/src/java/org/apache/lucene/util/Constants.java+++ b/lucene/core/src/java/org/apache/lucene/util/Constants.java@@ -17,7 +17,6 @@ package org.apache.lucene.util;* limitations under the License.*/-import java.lang.reflect.Field;import java.util.StringTokenizer;@@ -69,25 +68,14 @@ public final class Constants {JVM_MINOR_VERSION = 0;}boolean is64Bit = false;- try {- final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");- final Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");- unsafeField.setAccessible(true);- final Object unsafe = unsafeField.get(null);- final int addressSize = ((Number) unsafeClass.getMethod("addressSize")- .invoke(unsafe)).intValue();- //System.out.println("Address size: " + addressSize);- is64Bit = addressSize >= 8;- } catch (Exception e) {- final String x = System.getProperty("sun.arch.data.model");- if (x != null) {- is64Bit = x.indexOf("64") != -1;+ final String x = System.getProperty("sun.arch.data.model");+ if (x != null) {+ is64Bit = x.contains("64");+ } else {+ if (OS_ARCH != null && OS_ARCH.contains("64")) {+ is64Bit = true;} else {- if (OS_ARCH != null && OS_ARCH.indexOf("64") != -1) {- is64Bit = true;- } else {- is64Bit = false;- }+ is64Bit = false;}}JRE_IS_64BIT = is64Bit;diff --git a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.javaindex 55e4c3bb77d..153adf06fe2 100644--- a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java+++ b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java@@ -17,15 +17,22 @@ package org.apache.lucene.util;* limitations under the License.*/-import java.lang.management.ManagementFactory;-import java.lang.management.PlatformManagedObject;-import java.lang.reflect.*;+import java.lang.reflect.Array;+import java.lang.reflect.Field;+import java.lang.reflect.Method;+import java.lang.reflect.Modifier;import java.text.DecimalFormat;import java.text.DecimalFormatSymbols;-import java.util.*;+import java.util.IdentityHashMap;+import java.util.Locale;+import java.util.Map;/*** Estimates the size (memory representation) of Java objects.+ * <p>+ * This class uses assumptions that were discovered for the Hotspot+ * virtual machine. If you use a non-OpenJDK/Oracle-based JVM,+ * the measurements may be slightly wrong.** @see #shallowSizeOf(Object)* @see #shallowSizeOfInstance(Class)@@ -33,29 +40,6 @@ import java.util.*;* @lucene.internal*/public final class RamUsageEstimator {- /**- * JVM diagnostic features.- */- public static enum JvmFeature {- OBJECT_REFERENCE_SIZE("Object reference size estimated using array index scale"),- ARRAY_HEADER_SIZE("Array header size estimated using array based offset"),- FIELD_OFFSETS("Shallow instance size based on field offsets"),- OBJECT_ALIGNMENT("Object alignment retrieved from HotSpotDiagnostic MX bean");-- public final String description;-- private JvmFeature(String description) {- this.description = description;- }-- @Override- public String toString() {- return super.name() + " (" + description + ")";- }- }-- /** JVM info string for debugging and reports. */- public final static String JVM_INFO_STRING;/** One kilobyte bytes. */public static final long ONE_KB = 1024;@@ -79,7 +63,12 @@ public final class RamUsageEstimator {public final static int NUM_BYTES_DOUBLE = 8;/**- * Number of bytes this jvm uses to represent an object reference.+ * True, iff compressed references (oops) are enabled by this JVM+ */+ public final static boolean COMPRESSED_REFS_ENABLED;++ /**+ * Number of bytes this JVM uses to represent an object reference.*/public final static int NUM_BYTES_OBJECT_REF;@@ -102,9 +91,8 @@ public final class RamUsageEstimator {/*** Sizes of primitive classes.*/- private static final Map<Class<?>,Integer> primitiveSizes;+ private static final Map<Class<?>,Integer> primitiveSizes = new IdentityHashMap<>();static {- primitiveSizes = new IdentityHashMap<>();primitiveSizes.put(boolean.class, Integer.valueOf(NUM_BYTES_BOOLEAN));primitiveSizes.put(byte.class, Integer.valueOf(NUM_BYTES_BYTE));primitiveSizes.put(char.class, Integer.valueOf(NUM_BYTES_CHAR));@@ -115,130 +103,78 @@ public final class RamUsageEstimator {primitiveSizes.put(long.class, Integer.valueOf(NUM_BYTES_LONG));}- /**- * A handle to <code>sun.misc.Unsafe</code>.- */- private final static Object theUnsafe;-- /**- * A handle to <code>sun.misc.Unsafe#fieldOffset(Field)</code>.- */- private final static Method objectFieldOffsetMethod;-- /**- * All the supported "internal" JVM features detected at clinit.- */- private final static EnumSet<JvmFeature> supportedFeatures;-/*** JVMs typically cache small longs. This tries to find out what the range is.*/- private static final long LONG_CACHE_MIN_VALUE, LONG_CACHE_MAX_VALUE;- private static final int LONG_SIZE;+ static final long LONG_CACHE_MIN_VALUE, LONG_CACHE_MAX_VALUE;+ static final int LONG_SIZE;++ /** For testing only */+ static final boolean JVM_IS_HOTSPOT_64BIT;++ static final String MANAGEMENT_FACTORY_CLASS = "java.lang.management.ManagementFactory";+ static final String HOTSPOT_BEAN_CLASS = "com.sun.management.HotSpotDiagnosticMXBean";/*** Initialize constants and try to collect information about the JVM internals.*/- static {- // Initialize empirically measured defaults. We'll modify them to the current- // JVM settings later on if possible.- int referenceSize = Constants.JRE_IS_64BIT ? 8 : 4;- int objectHeader = Constants.JRE_IS_64BIT ? 16 : 8;- // The following is objectHeader + NUM_BYTES_INT, but aligned (object alignment)- // so on 64 bit JVMs it'll be align(16 + 4, @8) = 24.- int arrayHeader = Constants.JRE_IS_64BIT ? 24 : 12;-- supportedFeatures = EnumSet.noneOf(JvmFeature.class);-- Class<?> unsafeClass = null;- Object tempTheUnsafe = null;- try {- unsafeClass = Class.forName("sun.misc.Unsafe");- final Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");- unsafeField.setAccessible(true);- tempTheUnsafe = unsafeField.get(null);- } catch (Exception e) {- // Ignore.- }- theUnsafe = tempTheUnsafe;-- // get object reference size by getting scale factor of Object[] arrays:- try {- final Method arrayIndexScaleM = unsafeClass.getMethod("arrayIndexScale", Class.class);- referenceSize = ((Number) arrayIndexScaleM.invoke(theUnsafe, Object[].class)).intValue();- supportedFeatures.add(JvmFeature.OBJECT_REFERENCE_SIZE);- } catch (Exception e) {- // ignore.- }-- // "best guess" based on reference size. We will attempt to modify- // these to exact values if there is supported infrastructure.- objectHeader = Constants.JRE_IS_64BIT ? (8 + referenceSize) : 8;- arrayHeader = Constants.JRE_IS_64BIT ? (8 + 2 * referenceSize) : 12;-- // get the object header size:- // - first try out if the field offsets are not scaled (see warning in Unsafe docs)- // - get the object header size by getting the field offset of the first field of a dummy object- // If the scaling is byte-wise and unsafe is available, enable dynamic size measurement for- // estimateRamUsage().- Method tempObjectFieldOffsetMethod = null;- try {- final Method objectFieldOffsetM = unsafeClass.getMethod("objectFieldOffset", Field.class);- final Field dummy1Field = DummyTwoLongObject.class.getDeclaredField("dummy1");- final int ofs1 = ((Number) objectFieldOffsetM.invoke(theUnsafe, dummy1Field)).intValue();- final Field dummy2Field = DummyTwoLongObject.class.getDeclaredField("dummy2");- final int ofs2 = ((Number) objectFieldOffsetM.invoke(theUnsafe, dummy2Field)).intValue();- if (Math.abs(ofs2 - ofs1) == NUM_BYTES_LONG) {- final Field baseField = DummyOneFieldObject.class.getDeclaredField("base");- objectHeader = ((Number) objectFieldOffsetM.invoke(theUnsafe, baseField)).intValue();- supportedFeatures.add(JvmFeature.FIELD_OFFSETS);- tempObjectFieldOffsetMethod = objectFieldOffsetM;- }- } catch (Exception e) {- // Ignore.- }- objectFieldOffsetMethod = tempObjectFieldOffsetMethod;-- // Get the array header size by retrieving the array base offset- // (offset of the first element of an array).- try {- final Method arrayBaseOffsetM = unsafeClass.getMethod("arrayBaseOffset", Class.class);- // we calculate that only for byte[] arrays, it's actually the same for all types:- arrayHeader = ((Number) arrayBaseOffsetM.invoke(theUnsafe, byte[].class)).intValue();- supportedFeatures.add(JvmFeature.ARRAY_HEADER_SIZE);- } catch (Exception e) {- // Ignore.- }-- NUM_BYTES_OBJECT_REF = referenceSize;- NUM_BYTES_OBJECT_HEADER = objectHeader;- NUM_BYTES_ARRAY_HEADER = arrayHeader;-- // Try to get the object alignment (the default seems to be 8 on Hotspot,- // regardless of the architecture).- int objectAlignment = 8;- try {- final Class<? extends PlatformManagedObject> beanClazz =- Class.forName("com.sun.management.HotSpotDiagnosticMXBean").asSubclass(PlatformManagedObject.class);- final Object hotSpotBean = ManagementFactory.getPlatformMXBean(beanClazz);- if (hotSpotBean != null) {- final Method getVMOptionMethod = beanClazz.getMethod("getVMOption", String.class);- final Object vmOption = getVMOptionMethod.invoke(hotSpotBean, "ObjectAlignmentInBytes");- objectAlignment = Integer.parseInt(- vmOption.getClass().getMethod("getValue").invoke(vmOption).toString()- );- supportedFeatures.add(JvmFeature.OBJECT_ALIGNMENT);+ static {+ if (Constants.JRE_IS_64BIT) {+ // Try to get compressed oops and object alignment (the default seems to be 8 on Hotspot);+ // (this only works on 64 bit, on 32 bits the alignment and reference size is fixed):+ boolean compressedOops = false;+ int objectAlignment = 8;+ boolean isHotspot = false;+ try {+ final Class<?> beanClazz = Class.forName(HOTSPOT_BEAN_CLASS);+ // we use reflection for this, because the management factory is not part+ // of Java 8's compact profile:+ final Object hotSpotBean = Class.forName(MANAGEMENT_FACTORY_CLASS)+ .getMethod("getPlatformMXBean", Class.class)+ .invoke(null, beanClazz);+ if (hotSpotBean != null) {+ isHotspot = true;+ final Method getVMOptionMethod = beanClazz.getMethod("getVMOption", String.class);+ try {+ final Object vmOption = getVMOptionMethod.invoke(hotSpotBean, "UseCompressedOops");+ compressedOops = Boolean.parseBoolean(+ vmOption.getClass().getMethod("getValue").invoke(vmOption).toString()+ );+ } catch (ReflectiveOperationException | RuntimeException e) {+ isHotspot = false;+ }+ try {+ final Object vmOption = getVMOptionMethod.invoke(hotSpotBean, "ObjectAlignmentInBytes");+ objectAlignment = Integer.parseInt(+ vmOption.getClass().getMethod("getValue").invoke(vmOption).toString()+ );+ } catch (ReflectiveOperationException | RuntimeException e) {+ isHotspot = false;+ }+ }+ } catch (ReflectiveOperationException | RuntimeException e) {+ isHotspot = false;}- } catch (Exception e) {- // Ignore.+ JVM_IS_HOTSPOT_64BIT = isHotspot;+ COMPRESSED_REFS_ENABLED = compressedOops;+ NUM_BYTES_OBJECT_ALIGNMENT = objectAlignment;+ // reference size is 4, if we have compressed oops:+ NUM_BYTES_OBJECT_REF = COMPRESSED_REFS_ENABLED ? 4 : 8;+ // "best guess" based on reference size:+ NUM_BYTES_OBJECT_HEADER = 8 + NUM_BYTES_OBJECT_REF;+ // array header is NUM_BYTES_OBJECT_HEADER + NUM_BYTES_INT, but aligned (object alignment):+ NUM_BYTES_ARRAY_HEADER = (int) alignObjectSize(NUM_BYTES_OBJECT_HEADER + NUM_BYTES_INT);+ } else {+ JVM_IS_HOTSPOT_64BIT = false;+ COMPRESSED_REFS_ENABLED = false;+ NUM_BYTES_OBJECT_ALIGNMENT = 8;+ NUM_BYTES_OBJECT_REF = 4;+ NUM_BYTES_OBJECT_HEADER = 8;+ // For 32 bit JVMs, no extra alignment of array header:+ NUM_BYTES_ARRAY_HEADER = NUM_BYTES_OBJECT_HEADER + NUM_BYTES_INT;}- NUM_BYTES_OBJECT_ALIGNMENT = objectAlignment;-- JVM_INFO_STRING = "[JVM: " +- Constants.JVM_NAME + ", " + Constants.JVM_VERSION + ", " + Constants.JVM_VENDOR + ", " +- Constants.JAVA_VENDOR + ", " + Constants.JAVA_VERSION + "]";-+ // get min/max value of cached Long class instances:long longCacheMinValue = 0;while (longCacheMinValue > Long.MIN_VALUE&& Long.valueOf(longCacheMinValue - 1) == Long.valueOf(longCacheMinValue - 1)) {@@ -253,32 +189,7 @@ public final class RamUsageEstimator {LONG_CACHE_MAX_VALUE = longCacheMaxValue;LONG_SIZE = (int) shallowSizeOfInstance(Long.class);}--- // Object with just one field to determine the object header size by getting the offset of the dummy field:- @SuppressWarnings("unused")- private static final class DummyOneFieldObject {- public byte base;- }-- // Another test object for checking, if the difference in offsets of dummy1 and dummy2 is 8 bytes.- // Only then we can be sure that those are real, unscaled offsets:- @SuppressWarnings("unused")- private static final class DummyTwoLongObject {- public long dummy1, dummy2;- }- /**- * Returns true, if the current JVM is fully supported by {@code RamUsageEstimator}.- * If this method returns {@code false} you are maybe using a 3rd party Java VM- * that is not supporting Oracle/Sun private APIs. The memory estimates can be- * imprecise then (no way of detecting compressed references, alignments, etc.).- * Lucene still tries to use sensible defaults.- */- public static boolean isSupportedJVM() {- return supportedFeatures.size() == JvmFeature.values().length;- }-/*** Aligns an object size to be the next multiple of {@link #NUM_BYTES_OBJECT_ALIGNMENT}.*/@@ -416,41 +327,8 @@ public final class RamUsageEstimator {static long adjustForField(long sizeSoFar, final Field f) {final Class<?> type = f.getType();final int fsize = type.isPrimitive() ? primitiveSizes.get(type) : NUM_BYTES_OBJECT_REF;- if (objectFieldOffsetMethod != null) {- try {- final long offsetPlusSize =- ((Number) objectFieldOffsetMethod.invoke(theUnsafe, f)).longValue() + fsize;- return Math.max(sizeSoFar, offsetPlusSize);- } catch (IllegalAccessException ex) {- throw new RuntimeException("Access problem with sun.misc.Unsafe", ex);- } catch (InvocationTargetException ite) {- final Throwable cause = ite.getCause();- if (cause instanceof RuntimeException)- throw (RuntimeException) cause;- if (cause instanceof Error)- throw (Error) cause;- // this should never happen (Unsafe does not declare- // checked Exceptions for this method), but who knows?- throw new RuntimeException("Call to Unsafe's objectFieldOffset() throwed "+- "checked Exception when accessing field " +- f.getDeclaringClass().getName() + "#" + f.getName(), cause);- }- } else {- // TODO: No alignments based on field type/ subclass fields alignments?- return sizeSoFar + fsize;- }- }-- /** Return the set of unsupported JVM features that improve the estimation. */- public static EnumSet<JvmFeature> getUnsupportedFeatures() {- EnumSet<JvmFeature> unsupported = EnumSet.allOf(JvmFeature.class);- unsupported.removeAll(supportedFeatures);- return unsupported;- }-- /** Return the set of supported JVM features that improve the estimation. */- public static EnumSet<JvmFeature> getSupportedFeatures() {- return EnumSet.copyOf(supportedFeatures);+ // TODO: No alignments based on field type/ subclass fields alignments?+ return sizeSoFar + fsize;}/**diff --git a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.javaindex 97df057798b..fc9a48fb095 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java@@ -88,25 +88,44 @@ public class TestRamUsageEstimator extends LuceneTestCase {}public void testReferenceSize() {- if (!isSupportedJVM()) {- System.err.println("WARN: Your JVM does not support certain Oracle/Sun extensions.");- System.err.println(" Memory estimates may be inaccurate.");- System.err.println(" Please report this to the Lucene mailing list.");- System.err.println("JVM version: " + RamUsageEstimator.JVM_INFO_STRING);- System.err.println("UnsupportedFeatures:");- for (JvmFeature f : RamUsageEstimator.getUnsupportedFeatures()) {- System.err.print(" - " + f.toString());- if (f == RamUsageEstimator.JvmFeature.OBJECT_ALIGNMENT) {- System.err.print("; Please note: 32bit Oracle/Sun VMs don't allow exact OBJECT_ALIGNMENT retrieval, this is a known issue.");- }- System.err.println();- }- }-assertTrue(NUM_BYTES_OBJECT_REF == 4 || NUM_BYTES_OBJECT_REF == 8);- if (!Constants.JRE_IS_64BIT) {- assertEquals("For 32bit JVMs, reference size must always be 4?", 4, NUM_BYTES_OBJECT_REF);+ if (Constants.JRE_IS_64BIT) {+ assertEquals("For 64 bit JVMs, reference size must be 8, unless compressed references are enabled",+ COMPRESSED_REFS_ENABLED ? 4 : 8, NUM_BYTES_OBJECT_REF);+ } else {+ assertEquals("For 32bit JVMs, reference size must always be 4", 4, NUM_BYTES_OBJECT_REF);+ assertFalse("For 32bit JVMs, compressed references can never be enabled", COMPRESSED_REFS_ENABLED);+ }+ }++ public void testHotspotBean() {+ assumeTrue("testHotspotBean only works on 64bit JVMs.", Constants.JRE_IS_64BIT);+ try {+ Class.forName(MANAGEMENT_FACTORY_CLASS);+ } catch (ClassNotFoundException e) {+ assumeNoException("testHotspotBean does not work on Java 8+ compact profile.", e);}+ try {+ Class.forName(HOTSPOT_BEAN_CLASS);+ } catch (ClassNotFoundException e) {+ assumeNoException("testHotspotBean only works on Hotspot (OpenJDK, Oracle) virtual machines.", e);+ }++ assertTrue("We should have been able to detect Hotspot's internal settings from the management bean.", JVM_IS_HOTSPOT_64BIT);+ }++ /** Helper to print out current settings for debugging {@code -Dtests.verbose=true} */+ public void testPrintValues() {+ assumeTrue("Specify -Dtests.verbose=true to print constants of RamUsageEstimator.", VERBOSE);+ System.out.println("JVM_IS_HOTSPOT_64BIT = " + JVM_IS_HOTSPOT_64BIT);+ System.out.println("COMPRESSED_REFS_ENABLED = " + COMPRESSED_REFS_ENABLED);+ System.out.println("NUM_BYTES_OBJECT_ALIGNMENT = " + NUM_BYTES_OBJECT_ALIGNMENT);+ System.out.println("NUM_BYTES_OBJECT_REF = " + NUM_BYTES_OBJECT_REF);+ System.out.println("NUM_BYTES_OBJECT_HEADER = " + NUM_BYTES_OBJECT_HEADER);+ System.out.println("NUM_BYTES_ARRAY_HEADER = " + NUM_BYTES_ARRAY_HEADER);+ System.out.println("LONG_SIZE = " + LONG_SIZE);+ System.out.println("LONG_CACHE_MIN_VALUE = " + LONG_CACHE_MIN_VALUE);+ System.out.println("LONG_CACHE_MAX_VALUE = " + LONG_CACHE_MAX_VALUE);}@SuppressWarnings("unused")