Search in sources :

Example 6 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project Mycat-Server by MyCATApache.

the class DirectByteBufferPool method recycle.

public void recycle(ByteBuffer theBuf) {
    if (!(theBuf instanceof DirectBuffer)) {
        theBuf.clear();
        return;
    }
    final long size = theBuf.capacity();
    boolean recycled = false;
    DirectBuffer thisNavBuf = (DirectBuffer) theBuf;
    int chunkCount = theBuf.capacity() / chunkSize;
    DirectBuffer parentBuf = (DirectBuffer) thisNavBuf.attachment();
    int startChunk = (int) ((thisNavBuf.address() - parentBuf.address()) / this.chunkSize);
    for (int i = 0; i < allPages.length; i++) {
        if ((recycled = allPages[i].recycleBuffer((ByteBuffer) parentBuf, startChunk, chunkCount) == true)) {
            break;
        }
    }
    final long threadId = Thread.currentThread().getId();
    if (memoryUsage.containsKey(threadId)) {
        memoryUsage.put(threadId, memoryUsage.get(threadId) - size);
    }
    if (recycled == false) {
        LOGGER.warn("warning ,not recycled buffer " + theBuf);
    }
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 7 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project Mycat-Server by MyCATApache.

the class TestDirectByteBufferPool method testAllocateWithDifferentAddress.

@Test
public void testAllocateWithDifferentAddress() {
    int size = 256;
    int pageSize = size * 4;
    int allocTimes = 8;
    DirectByteBufferPool pool = new DirectByteBufferPool(pageSize, (short) 256, (short) 2, 0);
    Map<Long, ByteBuffer> buffs = new HashMap<Long, ByteBuffer>(8);
    ByteBuffer byteBuffer = null;
    DirectBuffer directBuffer = null;
    ByteBuffer temp = null;
    long address;
    boolean failure = false;
    for (int i = 0; i < allocTimes; i++) {
        byteBuffer = pool.allocate(size);
        if (byteBuffer == null) {
            Assert.fail("Should have enough memory");
        }
        directBuffer = (DirectBuffer) byteBuffer;
        address = directBuffer.address();
        System.out.println(address);
        temp = buffs.get(address);
        buffs.put(address, byteBuffer);
        if (null != temp) {
            failure = true;
            break;
        }
    }
    for (ByteBuffer buff : buffs.values()) {
        pool.recycle(buff);
    }
    if (failure == true) {
        Assert.fail("Allocate with same address");
    }
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 8 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project Mycat-Server by MyCATApache.

the class TestDirectByteBufferPool method testAllocateNullWhenOutOfMemory.

@Test
public void testAllocateNullWhenOutOfMemory() {
    int size = 256;
    int pageSize = size * 4;
    int allocTimes = 9;
    DirectByteBufferPool pool = new DirectByteBufferPool(pageSize, (short) 256, (short) 2, 0);
    long start = System.currentTimeMillis();
    ByteBuffer byteBuffer = null;
    List<ByteBuffer> buffs = new ArrayList<ByteBuffer>();
    int i = 0;
    for (; i < allocTimes; i++) {
        byteBuffer = pool.allocate(size);
        if (byteBuffer == null || !(byteBuffer instanceof DirectBuffer)) {
            break;
        }
        buffs.add(byteBuffer);
    }
    for (ByteBuffer buff : buffs) {
        pool.recycle(buff);
    }
    Assert.assertEquals("Should out of memory when i = " + 8, i, 8);
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 9 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project orientdb by orientechnologies.

the class OByteBufferPool method findDirectByteBufferWithCleaner.

private static ByteBuffer findDirectByteBufferWithCleaner(ByteBuffer buffer, int depthLimit) {
    if (depthLimit == 0)
        return null;
    if (!(buffer instanceof DirectBuffer))
        return null;
    final DirectBuffer directBuffer = (DirectBuffer) buffer;
    final Cleaner cleaner = directBuffer.cleaner();
    if (cleaner != null)
        return buffer;
    final Object attachment = directBuffer.attachment();
    if (!(attachment instanceof ByteBuffer))
        return null;
    return findDirectByteBufferWithCleaner((ByteBuffer) attachment, depthLimit - 1);
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) ByteBuffer(java.nio.ByteBuffer) Cleaner(sun.misc.Cleaner)

Example 10 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project jdk8u_jdk by JetBrains.

the class SctpMultiChannelImpl method receive.

private int receive(int fd, ByteBuffer dst, ResultContainer resultContainer) throws IOException {
    int pos = dst.position();
    int lim = dst.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);
    if (dst instanceof DirectBuffer && rem > 0)
        return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos);
    /* Substitute a native buffer. */
    int newSize = Math.max(rem, 1);
    ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
    try {
        int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0);
        bb.flip();
        if (n > 0 && rem > 0)
            dst.put(bb);
        return n;
    } finally {
        Util.releaseTemporaryDirectBuffer(bb);
    }
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DirectBuffer (sun.nio.ch.DirectBuffer)14 ByteBuffer (java.nio.ByteBuffer)10 Test (org.junit.Test)4 InetSocketAddress (java.net.InetSocketAddress)3 InetAddress (java.net.InetAddress)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Cleaner (sun.misc.Cleaner)2 RandomAccessFile (java.io.RandomAccessFile)1 SocketAddress (java.net.SocketAddress)1 FileChannel (java.nio.channels.FileChannel)1 Histogram (org.HdrHistogram.Histogram)1