Search in sources :

Example 1 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class MemoryManager method allocatePages.

/**
	 * Allocates a set of memory segments from this memory manager. If the memory manager pre-allocated the
	 * segments, they will be taken from the pool of memory segments. Otherwise, they will be allocated
	 * as part of this call.
	 * 
	 * @param owner The owner to associate with the memory segment, for the fallback release.
	 * @param target The list into which to put the allocated memory pages.
	 * @param numPages The number of pages to allocate.
	 * @throws MemoryAllocationException Thrown, if this memory manager does not have the requested amount
	 *                                   of memory pages any more.
	 */
public void allocatePages(Object owner, List<MemorySegment> target, int numPages) throws MemoryAllocationException {
    // sanity check
    if (owner == null) {
        throw new IllegalArgumentException("The memory owner must not be null.");
    }
    // reserve array space, if applicable
    if (target instanceof ArrayList) {
        ((ArrayList<MemorySegment>) target).ensureCapacity(numPages);
    }
    // -------------------- BEGIN CRITICAL SECTION -------------------
    synchronized (lock) {
        if (isShutDown) {
            throw new IllegalStateException("Memory manager has been shut down.");
        }
        // lazy case, the 'freeSegments.size()' is zero.
        if (numPages > (memoryPool.getNumberOfAvailableMemorySegments() + numNonAllocatedPages)) {
            throw new MemoryAllocationException("Could not allocate " + numPages + " pages. Only " + (memoryPool.getNumberOfAvailableMemorySegments() + numNonAllocatedPages) + " pages are remaining.");
        }
        Set<MemorySegment> segmentsForOwner = allocatedSegments.get(owner);
        if (segmentsForOwner == null) {
            segmentsForOwner = new HashSet<MemorySegment>(numPages);
            allocatedSegments.put(owner, segmentsForOwner);
        }
        if (isPreAllocated) {
            for (int i = numPages; i > 0; i--) {
                MemorySegment segment = memoryPool.requestSegmentFromPool(owner);
                target.add(segment);
                segmentsForOwner.add(segment);
            }
        } else {
            for (int i = numPages; i > 0; i--) {
                MemorySegment segment = memoryPool.allocateNewSegment(owner);
                target.add(segment);
                segmentsForOwner.add(segment);
            }
            numNonAllocatedPages -= numPages;
        }
    }
// -------------------- END CRITICAL SECTION -------------------
}
Also used : ArrayList(java.util.ArrayList) MemorySegment(org.apache.flink.core.memory.MemorySegment) HybridMemorySegment(org.apache.flink.core.memory.HybridMemorySegment) HeapMemorySegment(org.apache.flink.core.memory.HeapMemorySegment)

Example 2 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class MemoryManagerLazyAllocationTest method allocateAllSingle.

@Test
public void allocateAllSingle() {
    try {
        final AbstractInvokable mockInvoke = new DummyInvokable();
        List<MemorySegment> segments = new ArrayList<MemorySegment>();
        try {
            for (int i = 0; i < NUM_PAGES; i++) {
                segments.add(this.memoryManager.allocatePages(mockInvoke, 1).get(0));
            }
        } catch (MemoryAllocationException e) {
            fail("Unable to allocate memory");
        }
        for (MemorySegment seg : segments) {
            this.memoryManager.release(seg);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) AbstractInvokable(org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Example 3 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class BufferTest method testgetNioBufferThreadSafe.

@Test
public void testgetNioBufferThreadSafe() {
    final MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(1024);
    final BufferRecycler recycler = Mockito.mock(BufferRecycler.class);
    Buffer buffer = new Buffer(segment, recycler);
    ByteBuffer buf1 = buffer.getNioBuffer();
    ByteBuffer buf2 = buffer.getNioBuffer();
    assertNotNull(buf1);
    assertNotNull(buf2);
    assertTrue("Repeated call to getNioBuffer() returns the same nio buffer", buf1 != buf2);
}
Also used : ByteBuffer(java.nio.ByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Example 4 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class RecordWriterTest method createBufferProvider.

private BufferProvider createBufferProvider(final int bufferSize) throws IOException, InterruptedException {
    BufferProvider bufferProvider = mock(BufferProvider.class);
    when(bufferProvider.requestBufferBlocking()).thenAnswer(new Answer<Buffer>() {

        @Override
        public Buffer answer(InvocationOnMock invocationOnMock) throws Throwable {
            MemorySegment segment = MemorySegmentFactory.allocateUnpooledSegment(bufferSize);
            Buffer buffer = new Buffer(segment, DiscardingRecycler.INSTANCE);
            return buffer;
        }
    });
    return bufferProvider;
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestInfiniteBufferProvider(org.apache.flink.runtime.io.network.util.TestInfiniteBufferProvider) BufferProvider(org.apache.flink.runtime.io.network.buffer.BufferProvider) MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 5 with MemorySegment

use of org.apache.flink.core.memory.MemorySegment in project flink by apache.

the class BufferFileWriterFileSegmentReaderTest method fillBufferWithAscendingNumbers.

public static int fillBufferWithAscendingNumbers(Buffer buffer, int currentNumber) {
    MemorySegment segment = buffer.getMemorySegment();
    final int size = buffer.getSize();
    for (int i = 0; i < size; i += 4) {
        segment.putInt(i, currentNumber++);
    }
    return currentNumber;
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment)

Aggregations

MemorySegment (org.apache.flink.core.memory.MemorySegment)375 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)52 DummyInvokable (org.apache.flink.runtime.operators.testutils.DummyInvokable)44 IOException (java.io.IOException)37 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)29 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)26 NetworkBuffer (org.apache.flink.runtime.io.network.buffer.NetworkBuffer)25 MemoryAllocationException (org.apache.flink.runtime.memory.MemoryAllocationException)24 IntPair (org.apache.flink.runtime.operators.testutils.types.IntPair)24 FileIOChannel (org.apache.flink.runtime.io.disk.iomanager.FileIOChannel)20 EOFException (java.io.EOFException)18 ByteBuffer (java.nio.ByteBuffer)18 AbstractInvokable (org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable)18 TestData (org.apache.flink.runtime.operators.testutils.TestData)18 Random (java.util.Random)16 UniformIntPairGenerator (org.apache.flink.runtime.operators.testutils.UniformIntPairGenerator)16 Chunk (org.apache.flink.runtime.state.heap.space.Chunk)15 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)15 IOManagerAsync (org.apache.flink.runtime.io.disk.iomanager.IOManagerAsync)14