Use sun.misc.Unsafe to access a direct ByteBuffer

- Add PooledUnsafeDirectByteBuf, a variant of PooledDirectByteBuf, which
  accesses its underlying direct ByteBuffer using sun.misc.Unsafe.
- To decouple Netty from sun.misc.*, sun.misc.Unsafe is accessed via
  PlatformDependent.
- This change solely introduces about 8+% improvement in direct memory
  access according to the tests conducted as described in #918
This commit is contained in:
Trustin Lee 2013-01-10 18:27:16 +09:00
parent e564157381
commit 04bae9bceb
4 changed files with 180 additions and 116 deletions

View File

@ -19,7 +19,6 @@ package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import java.util.Deque;
@ -355,22 +354,6 @@ abstract class PoolArena<T> {
static final class DirectArena extends PoolArena<ByteBuffer> {
private static final boolean UNALIGNED;
static {
boolean unaligned = false;
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) {
// Ignore
}
UNALIGNED = unaligned;
}
DirectArena(PooledByteBufAllocator parent, int pageSize, int maxOrder, int pageShifts, int chunkSize) {
super(parent, pageSize, maxOrder, pageShifts, chunkSize);
}
@ -393,7 +376,7 @@ abstract class PoolArena<T> {
@Override
protected PooledByteBuf<ByteBuffer> newByteBuf(int maxCapacity) {
if (UNALIGNED) {
if (PlatformDependent.isUnaligned()) {
return new PooledUnsafeDirectByteBuf(maxCapacity);
} else {
return new PooledDirectByteBuf(maxCapacity);
@ -406,12 +389,18 @@ abstract class PoolArena<T> {
return;
}
// We must duplicate the NIO buffers because they may be accessed by other Netty buffers.
src = src.duplicate();
dst = dst.duplicate();
src.position(srcOffset).limit(srcOffset + length);
dst.position(dstOffset);
dst.put(src);
if (PlatformDependent.isUnaligned()) {
PlatformDependent.copyMemory(
PlatformDependent.directBufferAddress(src) + srcOffset,
PlatformDependent.directBufferAddress(dst) + dstOffset, length);
} else {
// We must duplicate the NIO buffers because they may be accessed by other Netty buffers.
src = src.duplicate();
dst = dst.duplicate();
src.position(srcOffset).limit(srcOffset + length);
dst.position(dstOffset);
dst.put(src);
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012 The Netty Project
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
@ -17,13 +17,10 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import sun.misc.Unsafe;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedChannelException;
@ -32,39 +29,8 @@ import java.nio.channels.ScatteringByteChannel;
final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
private static final Field ADDRESS_FIELD;
private static final Unsafe UNSAFE;
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
static {
Unsafe unsafe;
try {
Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
singleoneInstanceField.setAccessible(true);
unsafe = (Unsafe) singleoneInstanceField.get(null);
} catch (Throwable t) {
throw new Error(t);
}
UNSAFE = unsafe;
Field addressField;
try {
addressField = Buffer.class.getDeclaredField("address");
addressField.setAccessible(true);
if (addressField.getLong(ByteBuffer.allocate(1)) != 0) {
throw new Error("heap buffer address must be 0");
}
ByteBuffer directBuf = ByteBuffer.allocateDirect(1);
if (addressField.getLong(directBuf) == 0) {
throw new Error("direct buffer address must be non-zero");
}
PlatformDependent.freeDirectBuffer(directBuf);
} catch (Throwable t) {
throw new Error(t);
}
ADDRESS_FIELD = addressField;
}
private long memoryAddress;
PooledUnsafeDirectByteBuf(int maxCapacity) {
@ -74,22 +40,17 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
@Override
void init(PoolChunk<ByteBuffer> chunk, long handle, int offset, int length, int maxLength) {
super.init(chunk, handle, offset, length, maxLength);
initiMemoryAddress();
initMemoryAddress();
}
@Override
void initUnpooled(PoolChunk<ByteBuffer> chunk, int length) {
super.initUnpooled(chunk, length);
initiMemoryAddress();
initMemoryAddress();
}
private void initiMemoryAddress() {
ByteBuffer memory = this.memory;
try {
memoryAddress = ADDRESS_FIELD.getLong(memory) + offset;
} catch (Exception e) {
throw new Error("failed to get the address of a direct buffer", e);
}
private void initMemoryAddress() {
memoryAddress = PlatformDependent.directBufferAddress(memory) + offset;
}
@Override
@ -105,42 +66,37 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
@Override
public byte getByte(int index) {
checkIndex(index);
return UNSAFE.getByte(addr(index));
return PlatformDependent.getByte(addr(index));
}
@Override
public short getShort(int index) {
checkIndex(index, 2);
if (NATIVE_ORDER) {
return UNSAFE.getShort(addr(index));
}
return Short.reverseBytes(UNSAFE.getShort(addr(index)));
short v = PlatformDependent.getShort(addr(index));
return NATIVE_ORDER? v : Short.reverseBytes(v);
}
@Override
public int getUnsignedMedium(int index) {
checkIndex(index, 3);
long addr = addr(index);
return (UNSAFE.getByte(addr) & 0xff) << 16 | (UNSAFE.getByte(addr + 1) & 0xff) << 8 |
UNSAFE.getByte(addr + 2) & 0xff;
return (PlatformDependent.getByte(addr) & 0xff) << 16 |
(PlatformDependent.getByte(addr + 1) & 0xff) << 8 |
PlatformDependent.getByte(addr + 2) & 0xff;
}
@Override
public int getInt(int index) {
checkIndex(index, 4);
if (NATIVE_ORDER) {
return UNSAFE.getInt(addr(index));
}
return Integer.reverseBytes(UNSAFE.getInt(addr(index)));
int v = PlatformDependent.getInt(addr(index));
return NATIVE_ORDER? v : Integer.reverseBytes(v);
}
@Override
public long getLong(int index) {
checkIndex(index, 8);
if (NATIVE_ORDER) {
return UNSAFE.getLong(addr(index));
}
return Long.reverseBytes(UNSAFE.getLong(addr(index)));
long v = PlatformDependent.getLong(addr(index));
return NATIVE_ORDER? v : Long.reverseBytes(v);
}
@Override
@ -148,7 +104,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
checkIndex(index, length);
if (dst instanceof PooledUnsafeDirectByteBuf) {
PooledUnsafeDirectByteBuf bbdst = (PooledUnsafeDirectByteBuf) dst;
UNSAFE.copyMemory(addr(index), bbdst.addr(dstIndex), length);
PlatformDependent.copyMemory(addr(index), bbdst.addr(dstIndex), length);
} else if (dst.hasArray()) {
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
} else {
@ -209,14 +165,14 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
@Override
public ByteBuf setByte(int index, int value) {
checkIndex(index);
UNSAFE.putByte(addr(index), (byte) value);
PlatformDependent.putByte(addr(index), (byte) value);
return this;
}
@Override
public ByteBuf setShort(int index, int value) {
checkIndex(index, 2);
UNSAFE.putShort(addr(index), NATIVE_ORDER? (short) value : Short.reverseBytes((short) value));
PlatformDependent.putShort(addr(index), NATIVE_ORDER? (short) value : Short.reverseBytes((short) value));
return this;
}
@ -224,23 +180,23 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
public ByteBuf setMedium(int index, int value) {
checkIndex(index, 3);
long addr = addr(index);
UNSAFE.putByte(addr, (byte) (value >>> 16));
UNSAFE.putByte(addr + 1, (byte) (value >>> 8));
UNSAFE.putByte(addr + 2, (byte) value);
PlatformDependent.putByte(addr, (byte) (value >>> 16));
PlatformDependent.putByte(addr + 1, (byte) (value >>> 8));
PlatformDependent.putByte(addr + 2, (byte) value);
return this;
}
@Override
public ByteBuf setInt(int index, int value) {
checkIndex(index, 4);
UNSAFE.putInt(addr(index), NATIVE_ORDER? value : Integer.reverseBytes(value));
PlatformDependent.putInt(addr(index), NATIVE_ORDER? value : Integer.reverseBytes(value));
return this;
}
@Override
public ByteBuf setLong(int index, long value) {
checkIndex(index, 8);
UNSAFE.putLong(addr(index), NATIVE_ORDER? value : Long.reverseBytes(value));
PlatformDependent.putLong(addr(index), NATIVE_ORDER? value : Long.reverseBytes(value));
return this;
}
@ -249,7 +205,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
checkIndex(index, length);
if (src instanceof PooledUnsafeDirectByteBuf) {
PooledUnsafeDirectByteBuf bbsrc = (PooledUnsafeDirectByteBuf) src;
UNSAFE.copyMemory(bbsrc.addr(srcIndex), addr(index), length);
PlatformDependent.copyMemory(bbsrc.addr(srcIndex), addr(index), length);
} else if (src.hasArray()) {
setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
} else {
@ -313,7 +269,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
public ByteBuf copy(int index, int length) {
checkIndex(index, length);
PooledUnsafeDirectByteBuf copy = (PooledUnsafeDirectByteBuf) alloc().directBuffer(capacity(), maxCapacity());
UNSAFE.copyMemory(addr(index), copy.addr(index), length);
PlatformDependent.copyMemory(addr(index), copy.addr(index), length);
copy.setIndex(index, index + length);
return copy;
}

View File

@ -97,6 +97,10 @@ public final class PlatformDependent {
return IS_UNALIGNED;
}
public static long directBufferAddress(ByteBuffer buffer) {
return PlatformDependent0.directBufferAddress(buffer);
}
/**
* Try to deallocate the specified direct {@link ByteBuffer}. Please note this method does nothing if
* the current platform does not support this operation or the specified buffer is not a direct buffer.
@ -115,6 +119,42 @@ public final class PlatformDependent {
return PlatformDependent0.objectFieldOffset(field);
}
public static byte getByte(long address) {
return PlatformDependent0.getByte(address);
}
public static short getShort(long address) {
return PlatformDependent0.getShort(address);
}
public static int getInt(long address) {
return PlatformDependent0.getInt(address);
}
public static long getLong(long address) {
return PlatformDependent0.getLong(address);
}
public static void putByte(long address, byte value) {
PlatformDependent0.putByte(address, value);
}
public static void putShort(long address, short value) {
PlatformDependent0.putShort(address, value);
}
public static void putInt(long address, int value) {
PlatformDependent0.putInt(address, value);
}
public static void putLong(long address, long value) {
PlatformDependent0.putLong(address, value);
}
public static void copyMemory(long srcAddr, long dstAddr, long length) {
PlatformDependent0.copyMemory(srcAddr, dstAddr, length);
}
private static boolean isAndroid0() {
boolean android;
try {

View File

@ -20,6 +20,7 @@ import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.Buffer;
import java.nio.ByteBuffer;
/**
@ -28,7 +29,8 @@ import java.nio.ByteBuffer;
final class PlatformDependent0 {
private static final Unsafe UNSAFE;
private static final Field CLEANER_FIELD;
private static final long CLEANER_FIELD_OFFSET;
private static final long ADDRESS_FIELD_OFFSET;
private static final boolean UNALIGNED;
static {
@ -42,28 +44,57 @@ final class PlatformDependent0 {
}
UNSAFE = unsafe;
ByteBuffer direct = ByteBuffer.allocateDirect(1);
Field cleanerField;
try {
cleanerField = direct.getClass().getDeclaredField("cleaner");
cleanerField.setAccessible(true);
Cleaner cleaner = (Cleaner) cleanerField.get(direct);
cleaner.clean();
} catch (Throwable t) {
cleanerField = null;
}
CLEANER_FIELD = cleanerField;
if (unsafe == null) {
CLEANER_FIELD_OFFSET = -1;
ADDRESS_FIELD_OFFSET = -1;
UNALIGNED = false;
} else {
ByteBuffer direct = ByteBuffer.allocateDirect(1);
Field cleanerField;
try {
cleanerField = direct.getClass().getDeclaredField("cleaner");
cleanerField.setAccessible(true);
Cleaner cleaner = (Cleaner) cleanerField.get(direct);
cleaner.clean();
} catch (Throwable t) {
cleanerField = null;
}
CLEANER_FIELD_OFFSET = cleanerField != null? objectFieldOffset(cleanerField) : -1;
boolean unaligned;
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) {
unaligned = false;
boolean unaligned;
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) {
unaligned = false;
}
if (unaligned) {
Field addressField = null;
try {
addressField = Buffer.class.getDeclaredField("address");
addressField.setAccessible(true);
if (addressField.getLong(ByteBuffer.allocate(1)) != 0) {
unaligned = false;
} else {
ByteBuffer directBuf = ByteBuffer.allocateDirect(1);
if (addressField.getLong(directBuf) == 0) {
unaligned = false;
}
freeDirectBuffer(directBuf);
}
} catch (Throwable t) {
unaligned = false;
}
ADDRESS_FIELD_OFFSET = addressField != null? objectFieldOffset(addressField) : -1;
} else {
ADDRESS_FIELD_OFFSET = -1;
}
UNALIGNED = unaligned;
}
UNALIGNED = unaligned;
}
static boolean hasUnsafe() {
@ -71,13 +102,21 @@ final class PlatformDependent0 {
}
static boolean canFreeDirectBuffer() {
return CLEANER_FIELD != null;
return ADDRESS_FIELD_OFFSET >= 0;
}
static long directBufferAddress(ByteBuffer buffer) {
if (!isUnaligned()) {
throw new Error();
}
return getLong(buffer, ADDRESS_FIELD_OFFSET);
}
static void freeDirectBuffer(ByteBuffer buffer) {
Cleaner cleaner;
try {
cleaner = (Cleaner) CLEANER_FIELD.get(buffer);
cleaner = (Cleaner) getObject(buffer, CLEANER_FIELD_OFFSET);
cleaner.clean();
} catch (Throwable t) {
// Nothing we can do here.
@ -92,10 +131,50 @@ final class PlatformDependent0 {
return UNSAFE.getObject(object, fieldOffset);
}
private static long getLong(Object object, long fieldOffset) {
return UNSAFE.getLong(object, fieldOffset);
}
static long objectFieldOffset(Field field) {
return UNSAFE.objectFieldOffset(field);
}
static byte getByte(long address) {
return UNSAFE.getByte(address);
}
static short getShort(long address) {
return UNSAFE.getShort(address);
}
static int getInt(long address) {
return UNSAFE.getInt(address);
}
static long getLong(long address) {
return UNSAFE.getLong(address);
}
static void putByte(long address, byte value) {
UNSAFE.putByte(address, value);
}
static void putShort(long address, short value) {
UNSAFE.putShort(address, value);
}
static void putInt(long address, int value) {
UNSAFE.putInt(address, value);
}
static void putLong(long address, long value) {
UNSAFE.putLong(address, value);
}
static void copyMemory(long srcAddr, long dstAddr, long length) {
UNSAFE.copyMemory(srcAddr, dstAddr, length);
}
private PlatformDependent0() {
}
}