diff options
author | tedyu <yuzhihong@gmail.com> | 2016-03-29 17:16:53 -0700 |
---|---|---|
committer | Shixiong Zhu <shixiong@databricks.com> | 2016-03-29 17:16:53 -0700 |
commit | e1f6845391078726f60e760f0ea68ccf81f9eca9 (patch) | |
tree | 84646ef2a0db279e0d4842635f40048f96265614 /common/unsafe/src | |
parent | 366cac6fb0bb5591a0463c4696f5b9de2a294022 (diff) | |
download | spark-e1f6845391078726f60e760f0ea68ccf81f9eca9.tar.gz spark-e1f6845391078726f60e760f0ea68ccf81f9eca9.tar.bz2 spark-e1f6845391078726f60e760f0ea68ccf81f9eca9.zip |
[SPARK-12181] Check Cached unaligned-access capability before using Unsafe
## What changes were proposed in this pull request?
For MemoryMode.OFF_HEAP, Unsafe.getInt etc. are used with no restriction.
However, the Oracle implementation uses these methods only if the class variable unaligned (commented as "Cached unaligned-access capability") is true, which seems to be calculated whether the architecture is i386, x86, amd64, or x86_64.
I think we should perform similar check for the use of Unsafe.
Reference: https://github.com/netty/netty/blob/4.1/common/src/main/java/io/netty/util/internal/PlatformDependent0.java#L112
## How was this patch tested?
Unit test suite
Author: tedyu <yuzhihong@gmail.com>
Closes #11943 from tedyu/master.
Diffstat (limited to 'common/unsafe/src')
-rw-r--r-- | common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java b/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java index 18761bfd22..672552cc65 100644 --- a/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java +++ b/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java @@ -18,6 +18,7 @@ package org.apache.spark.unsafe; import java.lang.reflect.Field; +import java.lang.reflect.Method; import sun.misc.Unsafe; @@ -37,6 +38,33 @@ public final class Platform { public static final int DOUBLE_ARRAY_OFFSET; + private static final boolean unaligned; + static { + boolean _unaligned; + // use reflection to access unaligned field + try { + Class<?> bitsClass = + Class.forName("java.nio.Bits", false, ClassLoader.getSystemClassLoader()); + Method unalignedMethod = bitsClass.getDeclaredMethod("unaligned"); + unalignedMethod.setAccessible(true); + _unaligned = Boolean.TRUE.equals(unalignedMethod.invoke(null)); + } catch (Throwable t) { + // We at least know x86 and x64 support unaligned access. + String arch = System.getProperty("os.arch", ""); + //noinspection DynamicRegexReplaceableByCompiledPattern + _unaligned = arch.matches("^(i[3-6]86|x86(_64)?|x64|amd64)$"); + } + unaligned = _unaligned; + } + + /** + * @return true when running JVM is having sun's Unsafe package available in it and underlying + * system having unaligned-access capability. + */ + public static boolean unaligned() { + return unaligned; + } + public static int getInt(Object object, long offset) { return _UNSAFE.getInt(object, offset); } |