You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2016/03/15 20:02:22 UTC
[1/3] incubator-geode git commit: renamed SimpleMemoryAllocator to
MemoryAllocator
Repository: incubator-geode
Updated Branches:
refs/heads/feature/GEODE-1101 [created] 4f84da8cb
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java
deleted file mode 100644
index c61f2f4..0000000
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.gemstone.gemfire.internal.offheap;
-
-import static org.junit.Assert.*;
-import static com.googlecode.catchexception.CatchException.*;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-import com.gemstone.gemfire.test.junit.categories.UnitTest;
-
-import junit.framework.TestCase;
-
-/**
- * Tests fill pattern validation for the {@link SimpleMemoryAllocatorImpl}.
- * @author rholmes
- */
-@Category(UnitTest.class)
-public class SimpleMemoryAllocatorFillPatternJUnitTest {
-
- /** Size of single test slab.*/
- private static final int SLAB_SIZE = 1024 * 1024 * 50;
-
- /** Canned data for write operations. */
- private static final byte[] WRITE_BYTES = new String("Some string data.").getBytes();
-
- /** Chunk size for basic huge allocation test. */
- private static final int HUGE_CHUNK_SIZE = 1024 * 200;
-
- /** The number of chunks to allocate in order to force compaction. */
- private static final int COMPACTION_CHUNKS = 3;
-
- /** Our slab size divided in three (with some padding for safety). */
- private static final int COMPACTION_CHUNK_SIZE = (SLAB_SIZE / COMPACTION_CHUNKS) - 1024;
-
- /** This should force compaction when allocated. */
- private static final int FORCE_COMPACTION_CHUNK_SIZE = COMPACTION_CHUNK_SIZE * 2;
-
- /** Our test victim. */
- private SimpleMemoryAllocatorImpl allocator = null;
-
- /** Our test victim's memory slab. */
- private SlabImpl slab = null;
-
- /**
- * Enables fill validation and creates the test victim.
- */
- @Before
- public void setUp() throws Exception {
- System.setProperty("gemfire.validateOffHeapWithFill", "true");
- this.slab = new SlabImpl(SLAB_SIZE);
- this.allocator = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{this.slab});
- }
-
- /**
- * Frees off heap memory.
- */
- @After
- public void tearDown() throws Exception {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- System.clearProperty("gemfire.validateOffHeapWithFill");
- }
-
- /**
- * This tests the fill pattern for a single tiny Chunk allocation.
- * @throws Exception
- */
- @Test
- public void testFillPatternBasicForTinyAllocations() throws Exception {
- doFillPatternBasic(1024);
- }
-
- /**
- * This tests the fill pattern for a single huge Chunk allocation.
- * @throws Exception
- */
- @Test
- public void testFillPatternBasicForHugeAllocations() throws Exception {
- doFillPatternBasic(HUGE_CHUNK_SIZE);
- }
-
- private void doFillPatternBasic(final int chunkSize) {
- /*
- * Pull a chunk off the fragment. This will have no fill because
- * it is a "fresh" chunk.
- */
- OffHeapStoredObject chunk = (OffHeapStoredObject) this.allocator.allocate(chunkSize);
-
- /*
- * Chunk should have valid fill from initial fragment allocation.
- */
- chunk.validateFill();
-
- // "Dirty" the chunk so the release has something to fill over
- chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
-
- // This should free the Chunk (ref count == 1)
- chunk.release();
-
- /*
- * This chunk should have a fill because it was reused from the
- * free list (assuming no fragmentation at this point...)
- */
- chunk = (OffHeapStoredObject) this.allocator.allocate(chunkSize);
-
- // Make sure we have a fill this time
- chunk.validateFill();
-
- // Give the fill code something to write over during the release
- chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
- chunk.release();
-
- // Again, make sure the release implemented the fill
- chunk.validateFill();
-
- // "Dirty up" the free chunk
- chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
-
- catchException(chunk).validateFill();
- assertTrue(caughtException() instanceof IllegalStateException);
- assertEquals("Fill pattern violated for chunk " + chunk.getAddress() + " with size " + chunk.getSize(), caughtException().getMessage());
-
- }
-
- /**
- * This tests that fill validation is working properly on newly created fragments after
- * a compaction.
- * @throws Exception
- */
- @Test
- public void testFillPatternAfterCompaction() throws Exception {
- /*
- * Stores our allocated memory.
- */
- OffHeapStoredObject[] allocatedChunks = new OffHeapStoredObject[COMPACTION_CHUNKS];
-
- /*
- * Use up most of our memory
- * Our memory looks like [ ][ ][ ]
- */
- for(int i =0;i < allocatedChunks.length;++i) {
- allocatedChunks[i] = (OffHeapStoredObject) this.allocator.allocate(COMPACTION_CHUNK_SIZE);
- allocatedChunks[i].validateFill();
- }
-
- /*
- * Release some of our allocated chunks.
- */
- for(int i=0;i < 2;++i) {
- allocatedChunks[i].release();
- allocatedChunks[i].validateFill();
- }
-
- /*
- * Now, allocate another chunk that is slightly larger than one of
- * our initial chunks. This should force a compaction causing our
- * memory to look like [ ][ ].
- */
- OffHeapStoredObject slightlyLargerChunk = (OffHeapStoredObject) this.allocator.allocate(FORCE_COMPACTION_CHUNK_SIZE);
-
- /*
- * Make sure the compacted memory has the fill validation.
- */
- slightlyLargerChunk.validateFill();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorJUnitTest.java
deleted file mode 100644
index 135aba2..0000000
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorJUnitTest.java
+++ /dev/null
@@ -1,594 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.gemstone.gemfire.internal.offheap;
-
-import static org.junit.Assert.*;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.concurrent.atomic.AtomicReference;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.contrib.java.lang.system.RestoreSystemProperties;
-import org.junit.experimental.categories.Category;
-
-import com.gemstone.gemfire.OutOfOffHeapMemoryException;
-import com.gemstone.gemfire.cache.CacheClosedException;
-import com.gemstone.gemfire.internal.logging.NullLogWriter;
-import com.gemstone.gemfire.test.junit.categories.UnitTest;
-
-@Category(UnitTest.class)
-public class SimpleMemoryAllocatorJUnitTest {
- @Rule
- public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();
-
- private static int round(int multiple, int v) {
- return ((v+multiple-1)/multiple)*multiple;
- }
- @Test
- public void testNullGetAllocator() {
- try {
- SimpleMemoryAllocatorImpl.getAllocator();
- fail("expected CacheClosedException");
- } catch (CacheClosedException expected) {
- }
- }
- @Test
- public void testConstructor() {
- try {
- SimpleMemoryAllocatorImpl.createForUnitTest(null, null, null);
- fail("expected IllegalArgumentException");
- } catch (IllegalArgumentException expected) {
- }
- }
- @Test
- public void testCreate() {
- System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "false");
- {
- NullOutOfOffHeapMemoryListener listener = new NullOutOfOffHeapMemoryListener();
- NullOffHeapMemoryStats stats = new NullOffHeapMemoryStats();
- try {
- SimpleMemoryAllocatorImpl.createForUnitTest(listener, stats, 10, 950, 100, new SlabFactory() {
- @Override
- public Slab create(int size) {
- throw new OutOfMemoryError("expected");
- }
- });
- } catch (OutOfMemoryError expected) {
- }
- assertTrue(listener.isClosed());
- assertTrue(stats.isClosed());
- }
- {
- NullOutOfOffHeapMemoryListener listener = new NullOutOfOffHeapMemoryListener();
- NullOffHeapMemoryStats stats = new NullOffHeapMemoryStats();
- int MAX_SLAB_SIZE = 100;
- try {
- SlabFactory factory = new SlabFactory() {
- private int createCount = 0;
- @Override
- public Slab create(int size) {
- createCount++;
- if (createCount == 1) {
- return new SlabImpl(size);
- } else {
- throw new OutOfMemoryError("expected");
- }
- }
- };
- SimpleMemoryAllocatorImpl.createForUnitTest(listener, stats, 10, 950, MAX_SLAB_SIZE, factory);
- } catch (OutOfMemoryError expected) {
- }
- assertTrue(listener.isClosed());
- assertTrue(stats.isClosed());
- }
- {
- NullOutOfOffHeapMemoryListener listener = new NullOutOfOffHeapMemoryListener();
- NullOffHeapMemoryStats stats = new NullOffHeapMemoryStats();
- SlabFactory factory = new SlabFactory() {
- @Override
- public Slab create(int size) {
- return new SlabImpl(size);
- }
- };
- MemoryAllocator ma =
- SimpleMemoryAllocatorImpl.createForUnitTest(listener, stats, 10, 950, 100, factory);
- try {
- assertFalse(listener.isClosed());
- assertFalse(stats.isClosed());
- ma.close();
- assertTrue(listener.isClosed());
- assertFalse(stats.isClosed());
- listener = new NullOutOfOffHeapMemoryListener();
- NullOffHeapMemoryStats stats2 = new NullOffHeapMemoryStats();
- {
- SlabImpl slab = new SlabImpl(1024);
- try {
- SimpleMemoryAllocatorImpl.createForUnitTest(listener, stats2, new SlabImpl[]{slab});
- } catch (IllegalStateException expected) {
- assertTrue("unexpected message: " + expected.getMessage(),
- expected.getMessage().equals("attempted to reuse existing off-heap memory even though new off-heap memory was allocated"));
- } finally {
- slab.free();
- }
- assertFalse(stats.isClosed());
- assertTrue(listener.isClosed());
- assertTrue(stats2.isClosed());
- }
- listener = new NullOutOfOffHeapMemoryListener();
- stats2 = new NullOffHeapMemoryStats();
- MemoryAllocator ma2 = SimpleMemoryAllocatorImpl.createForUnitTest(listener, stats2, 10, 950, 100, factory);
- assertSame(ma, ma2);
- assertTrue(stats.isClosed());
- assertFalse(listener.isClosed());
- assertFalse(stats2.isClosed());
- stats = stats2;
- ma.close();
- assertTrue(listener.isClosed());
- assertFalse(stats.isClosed());
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- assertTrue(stats.isClosed());
- }
- }
- @Test
- public void testBasics() {
- int BATCH_SIZE = 1;
- int TINY_MULTIPLE = FreeListManager.TINY_MULTIPLE;
- int HUGE_MULTIPLE = FreeListManager.HUGE_MULTIPLE;
- int perObjectOverhead = OffHeapStoredObject.HEADER_SIZE;
- int maxTiny = FreeListManager.MAX_TINY-perObjectOverhead;
- int minHuge = maxTiny+1;
- int TOTAL_MEM = (maxTiny+perObjectOverhead)*BATCH_SIZE /*+ (maxBig+perObjectOverhead)*BATCH_SIZE*/ + round(TINY_MULTIPLE, minHuge+1+perObjectOverhead)*BATCH_SIZE + (TINY_MULTIPLE+perObjectOverhead)*BATCH_SIZE /*+ (MIN_BIG_SIZE+perObjectOverhead)*BATCH_SIZE*/ + round(TINY_MULTIPLE, minHuge+perObjectOverhead+1);
- SlabImpl slab = new SlabImpl(TOTAL_MEM);
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- assertEquals(TOTAL_MEM, ma.getFreeMemory());
- assertEquals(TOTAL_MEM, ma.freeList.getFreeFragmentMemory());
- assertEquals(0, ma.freeList.getFreeTinyMemory());
- assertEquals(0, ma.freeList.getFreeHugeMemory());
- StoredObject tinymc = ma.allocate(maxTiny);
- assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
- assertEquals(round(TINY_MULTIPLE, maxTiny+perObjectOverhead)*(BATCH_SIZE-1), ma.freeList.getFreeTinyMemory());
- StoredObject hugemc = ma.allocate(minHuge);
- assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, minHuge+perObjectOverhead)/*-round(BIG_MULTIPLE, maxBig+perObjectOverhead)*/-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
- long freeSlab = ma.freeList.getFreeFragmentMemory();
- long oldFreeHugeMemory = ma.freeList.getFreeHugeMemory();
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-1), oldFreeHugeMemory);
- hugemc.release();
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead), ma.freeList.getFreeHugeMemory()-oldFreeHugeMemory);
- assertEquals(TOTAL_MEM/*-round(BIG_MULTIPLE, maxBig+perObjectOverhead)*/-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
- assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
- long oldFreeTinyMemory = ma.freeList.getFreeTinyMemory();
- tinymc.release();
- assertEquals(round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.freeList.getFreeTinyMemory()-oldFreeTinyMemory);
- assertEquals(TOTAL_MEM, ma.getFreeMemory());
- // now lets reallocate from the free lists
- tinymc = ma.allocate(maxTiny);
- assertEquals(oldFreeTinyMemory, ma.freeList.getFreeTinyMemory());
- assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
- hugemc = ma.allocate(minHuge);
- assertEquals(oldFreeHugeMemory, ma.freeList.getFreeHugeMemory());
- assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, minHuge+perObjectOverhead)/*-round(BIG_MULTIPLE, maxBig+perObjectOverhead)*/-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
- hugemc.release();
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead), ma.freeList.getFreeHugeMemory()-oldFreeHugeMemory);
- assertEquals(TOTAL_MEM/*-round(BIG_MULTIPLE, maxBig+perObjectOverhead)*/-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
- assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
- tinymc.release();
- assertEquals(round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.freeList.getFreeTinyMemory()-oldFreeTinyMemory);
- assertEquals(TOTAL_MEM, ma.getFreeMemory());
- // None of the reallocates should have come from the slab.
- assertEquals(freeSlab, ma.freeList.getFreeFragmentMemory());
- tinymc = ma.allocate(1);
- assertEquals(round(TINY_MULTIPLE, 1+perObjectOverhead), tinymc.getSize());
- assertEquals(freeSlab-(round(TINY_MULTIPLE, 1+perObjectOverhead)*BATCH_SIZE), ma.freeList.getFreeFragmentMemory());
- freeSlab = ma.freeList.getFreeFragmentMemory();
- tinymc.release();
- assertEquals(round(TINY_MULTIPLE, maxTiny+perObjectOverhead)+(round(TINY_MULTIPLE, 1+perObjectOverhead)*BATCH_SIZE), ma.freeList.getFreeTinyMemory()-oldFreeTinyMemory);
-
- hugemc = ma.allocate(minHuge+1);
- assertEquals(round(TINY_MULTIPLE, minHuge+1+perObjectOverhead), hugemc.getSize());
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-1), ma.freeList.getFreeHugeMemory());
- hugemc.release();
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*BATCH_SIZE, ma.freeList.getFreeHugeMemory());
- hugemc = ma.allocate(minHuge);
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-1), ma.freeList.getFreeHugeMemory());
- if (BATCH_SIZE > 1) {
- StoredObject hugemc2 = ma.allocate(minHuge);
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-2), ma.freeList.getFreeHugeMemory());
- hugemc2.release();
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-1), ma.freeList.getFreeHugeMemory());
- }
- hugemc.release();
- assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*BATCH_SIZE, ma.freeList.getFreeHugeMemory());
- // now that we do compaction the following allocate works.
- hugemc = ma.allocate(minHuge + HUGE_MULTIPLE + HUGE_MULTIPLE-1);
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
-
- @Test
- public void testChunkCreateDirectByteBuffer() {
- SlabImpl slab = new SlabImpl(1024*1024);
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- ByteBuffer bb = ByteBuffer.allocate(1024);
- for (int i=0; i < 1024; i++) {
- bb.put((byte) i);
- }
- bb.position(0);
- OffHeapStoredObject c = (OffHeapStoredObject) ma.allocateAndInitialize(bb.array(), false, false);
- assertEquals(1024, c.getDataSize());
- if (!Arrays.equals(bb.array(), c.getRawBytes())) {
- fail("arrays are not equal. Expected " + Arrays.toString(bb.array()) + " but found: " + Arrays.toString(c.getRawBytes()));
- }
- ByteBuffer dbb = c.createDirectByteBuffer();
- assertEquals(true, dbb.isDirect());
- assertEquals(bb, dbb);
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
-
- @Test
- public void testDebugLog() {
- SimpleMemoryAllocatorImpl.debugLog("test debug log", false);
- SimpleMemoryAllocatorImpl.debugLog("test debug log", true);
- }
- @Test
- public void testGetLostChunks() {
- SlabImpl slab = new SlabImpl(1024*1024);
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- assertEquals(Collections.emptyList(), ma.getLostChunks());
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
- @Test
- public void testFindSlab() {
- final int SLAB_SIZE = 1024*1024;
- SlabImpl slab = new SlabImpl(SLAB_SIZE);
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- assertEquals(0, ma.findSlab(slab.getMemoryAddress()));
- assertEquals(0, ma.findSlab(slab.getMemoryAddress()+SLAB_SIZE-1));
- try {
- ma.findSlab(slab.getMemoryAddress()-1);
- fail("expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- ma.findSlab(slab.getMemoryAddress()+SLAB_SIZE);
- fail("expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
- @Test
- public void testValidateAddressAndSize() {
- final int SLAB_SIZE = 1024*1024;
- SlabImpl slab = new SlabImpl(SLAB_SIZE);
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- try {
- SimpleMemoryAllocatorImpl.validateAddress(0L);
- fail("expected IllegalStateException");
- } catch (IllegalStateException expected) {
- assertEquals("Unexpected exception message: " + expected.getMessage(), true, expected.getMessage().contains("addr was smaller than expected"));
- }
- try {
- SimpleMemoryAllocatorImpl.validateAddress(1L);
- fail("expected IllegalStateException");
- } catch (IllegalStateException expected) {
- assertEquals("Unexpected exception message: " + expected.getMessage(), true, expected.getMessage().contains("Valid addresses must be in one of the following ranges:"));
- }
- SimpleMemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress(), SLAB_SIZE, false);
- SimpleMemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress(), SLAB_SIZE, true);
- SimpleMemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress(), -1, true);
- try {
- SimpleMemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress()-1, SLAB_SIZE, true);
- fail("expected IllegalStateException");
- } catch (IllegalStateException expected) {
- assertEquals("Unexpected exception message: " + expected.getMessage(), true, expected.getMessage().equals(" address 0x" + Long.toString(slab.getMemoryAddress()-1, 16) + " does not address the original slab memory"));
- }
- try {
- SimpleMemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress(), SLAB_SIZE+1, true);
- fail("expected IllegalStateException");
- } catch (IllegalStateException expected) {
- assertEquals("Unexpected exception message: " + expected.getMessage(), true, expected.getMessage().equals(" address 0x" + Long.toString(slab.getMemoryAddress()+SLAB_SIZE, 16) + " does not address the original slab memory"));
- }
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
- @Test
- public void testMemoryInspection() {
- final int SLAB_SIZE = 1024*1024;
- SlabImpl slab = new SlabImpl(SLAB_SIZE);
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- MemoryInspector inspector = ma.getMemoryInspector();
- assertNotNull(inspector);
- assertEquals(null, inspector.getFirstBlock());
- assertEquals(Collections.emptyList(), inspector.getSnapshot());
- assertEquals(Collections.emptyList(), inspector.getAllocatedBlocks());
- assertEquals(null, inspector.getBlockAfter(null));
- inspector.createSnapshot();
- // call this twice for code coverage
- inspector.createSnapshot();
- try {
- assertEquals(inspector.getAllBlocks(), inspector.getSnapshot());
- MemoryBlock firstBlock = inspector.getFirstBlock();
- assertNotNull(firstBlock);
- assertEquals(1024*1024, firstBlock.getBlockSize());
- assertEquals("N/A", firstBlock.getDataType());
- assertEquals(-1, firstBlock.getFreeListId());
- assertTrue(firstBlock.getAddress() > 0);
- assertNull(firstBlock.getNextBlock());
- assertEquals(0, firstBlock.getRefCount());
- assertEquals(0, firstBlock.getSlabId());
- assertEquals(MemoryBlock.State.UNUSED, firstBlock.getState());
- assertFalse(firstBlock.isCompressed());
- assertFalse(firstBlock.isSerialized());
- assertEquals(null, inspector.getBlockAfter(firstBlock));
- } finally {
- inspector.clearSnapshot();
- }
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
-
- @Test
- public void testClose() {
- System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "false");
- SlabImpl slab = new SlabImpl(1024*1024);
- boolean freeSlab = true;
- SlabImpl[] slabs = new SlabImpl[]{slab};
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), slabs);
- ma.close();
- ma.close();
- System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
- try {
- ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), slabs);
- ma.close();
- freeSlab = false;
- ma.close();
- } finally {
- System.clearProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
- }
- } finally {
- if (freeSlab) {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
-
- }
-
- @Test
- public void testCompaction() {
- final int perObjectOverhead = OffHeapStoredObject.HEADER_SIZE;
- final int BIG_ALLOC_SIZE = 150000;
- final int SMALL_ALLOC_SIZE = BIG_ALLOC_SIZE/2;
- final int TOTAL_MEM = BIG_ALLOC_SIZE;
- SlabImpl slab = new SlabImpl(TOTAL_MEM);
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- StoredObject bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
- try {
- StoredObject smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
- fail("Expected out of memory");
- } catch (OutOfOffHeapMemoryException expected) {
- }
- bmc.release();
- assertEquals(TOTAL_MEM, ma.freeList.getFreeMemory());
- StoredObject smc1 = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
- StoredObject smc2 = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
- smc2.release();
- assertEquals(TOTAL_MEM-SMALL_ALLOC_SIZE, ma.freeList.getFreeMemory());
- try {
- bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
- fail("Expected out of memory");
- } catch (OutOfOffHeapMemoryException expected) {
- }
- smc1.release();
- assertEquals(TOTAL_MEM, ma.freeList.getFreeMemory());
- bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
- bmc.release();
- assertEquals(TOTAL_MEM, ma.freeList.getFreeMemory());
- ArrayList<StoredObject> mcs = new ArrayList<StoredObject>();
- for (int i=0; i < BIG_ALLOC_SIZE/(8+perObjectOverhead); i++) {
- mcs.add(ma.allocate(8));
- }
- checkMcs(mcs);
- assertEquals(0, ma.freeList.getFreeMemory());
- try {
- ma.allocate(8);
- fail("expected out of memory");
- } catch (OutOfOffHeapMemoryException expected) {
- }
- mcs.remove(0).release(); // frees 8+perObjectOverhead
- assertEquals(8+perObjectOverhead, ma.freeList.getFreeMemory());
- mcs.remove(0).release(); // frees 8+perObjectOverhead
- assertEquals((8+perObjectOverhead)*2, ma.freeList.getFreeMemory());
- ma.allocate(16).release(); // allocates and frees 16+perObjectOverhead; still have perObjectOverhead
- assertEquals((8+perObjectOverhead)*2, ma.freeList.getFreeMemory());
- mcs.remove(0).release(); // frees 8+perObjectOverhead
- assertEquals((8+perObjectOverhead)*3, ma.freeList.getFreeMemory());
- mcs.remove(0).release(); // frees 8+perObjectOverhead
- assertEquals((8+perObjectOverhead)*4, ma.freeList.getFreeMemory());
- // At this point I should have 8*4 + perObjectOverhead*4 of free memory
- ma.allocate(8*4+perObjectOverhead*3).release();
- assertEquals((8+perObjectOverhead)*4, ma.freeList.getFreeMemory());
- mcs.remove(0).release(); // frees 8+perObjectOverhead
- assertEquals((8+perObjectOverhead)*5, ma.freeList.getFreeMemory());
- // At this point I should have 8*5 + perObjectOverhead*5 of free memory
- try {
- ma.allocate((8*5+perObjectOverhead*4)+1);
- fail("expected out of memory");
- } catch (OutOfOffHeapMemoryException expected) {
- }
- mcs.remove(0).release(); // frees 8+perObjectOverhead
- assertEquals((8+perObjectOverhead)*6, ma.freeList.getFreeMemory());
- checkMcs(mcs);
- // At this point I should have 8*6 + perObjectOverhead*6 of free memory
- StoredObject mc24 = ma.allocate(24);
- checkMcs(mcs);
- assertEquals((8+perObjectOverhead)*6 - (24+perObjectOverhead), ma.freeList.getFreeMemory());
- // At this point I should have 8*3 + perObjectOverhead*5 of free memory
- StoredObject mc16 = ma.allocate(16);
- checkMcs(mcs);
- assertEquals((8+perObjectOverhead)*6 - (24+perObjectOverhead) - (16+perObjectOverhead), ma.freeList.getFreeMemory());
- // At this point I should have 8*1 + perObjectOverhead*4 of free memory
- mcs.add(ma.allocate(8));
- checkMcs(mcs);
- assertEquals((8+perObjectOverhead)*6 - (24+perObjectOverhead) - (16+perObjectOverhead) - (8+perObjectOverhead), ma.freeList.getFreeMemory());
- // At this point I should have 8*0 + perObjectOverhead*3 of free memory
- StoredObject mcDO = ma.allocate(perObjectOverhead*2);
- checkMcs(mcs);
- // At this point I should have 8*0 + perObjectOverhead*0 of free memory
- assertEquals(0, ma.freeList.getFreeMemory());
- try {
- ma.allocate(1);
- fail("expected out of memory");
- } catch (OutOfOffHeapMemoryException expected) {
- }
- checkMcs(mcs);
- assertEquals(0, ma.freeList.getFreeMemory());
- mcDO.release();
- assertEquals((perObjectOverhead*3), ma.freeList.getFreeMemory());
- mcs.remove(mcs.size()-1).release();
- assertEquals((perObjectOverhead*3)+(8+perObjectOverhead), ma.freeList.getFreeMemory());
- mc16.release();
- assertEquals((perObjectOverhead*3)+(8+perObjectOverhead)+(16+perObjectOverhead), ma.freeList.getFreeMemory());
- mc24.release();
- assertEquals((perObjectOverhead*3)+(8+perObjectOverhead)+(16+perObjectOverhead)+(24+perObjectOverhead), ma.freeList.getFreeMemory());
-
- long freeMem = ma.freeList.getFreeMemory();
- for (StoredObject mc: mcs) {
- mc.release();
- assertEquals(freeMem+(8+perObjectOverhead), ma.freeList.getFreeMemory());
- freeMem += (8+perObjectOverhead);
- }
- mcs.clear();
- assertEquals(TOTAL_MEM, ma.freeList.getFreeMemory());
- bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
- bmc.release();
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
-
- long expectedMemoryUsage;
- boolean memoryUsageEventReceived;
- @Test
- public void testUsageEventListener() {
- final int perObjectOverhead = OffHeapStoredObject.HEADER_SIZE;
- final int SMALL_ALLOC_SIZE = 1000;
- SlabImpl slab = new SlabImpl(3000);
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- MemoryUsageListener listener = new MemoryUsageListener() {
- @Override
- public void updateMemoryUsed(final long bytesUsed) {
- SimpleMemoryAllocatorJUnitTest.this.memoryUsageEventReceived = true;
- assertEquals(SimpleMemoryAllocatorJUnitTest.this.expectedMemoryUsage, bytesUsed);
- }
- };
- ma.addMemoryUsageListener(listener);
-
- this.expectedMemoryUsage = SMALL_ALLOC_SIZE;
- this.memoryUsageEventReceived = false;
- StoredObject smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
- assertEquals(true, this.memoryUsageEventReceived);
-
- this.expectedMemoryUsage = SMALL_ALLOC_SIZE * 2;
- this.memoryUsageEventReceived = false;
- smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
- assertEquals(true, this.memoryUsageEventReceived);
-
- MemoryUsageListener unaddedListener = new MemoryUsageListener() {
- @Override
- public void updateMemoryUsed(final long bytesUsed) {
- throw new IllegalStateException("Should never be called");
- }
- };
- ma.removeMemoryUsageListener(unaddedListener);
-
- ma.removeMemoryUsageListener(listener);
-
- ma.removeMemoryUsageListener(unaddedListener);
-
- this.expectedMemoryUsage = SMALL_ALLOC_SIZE * 2;
- this.memoryUsageEventReceived = false;
- smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
- assertEquals(false, this.memoryUsageEventReceived);
-
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
- private void checkMcs(ArrayList<StoredObject> mcs) {
- for (StoredObject mc: mcs) {
- assertEquals(8+8, mc.getSize());
- }
- }
-
- @Test
- public void testOutOfOffHeapMemory() {
- final int perObjectOverhead = OffHeapStoredObject.HEADER_SIZE;
- final int BIG_ALLOC_SIZE = 150000;
- final int SMALL_ALLOC_SIZE = BIG_ALLOC_SIZE/2;
- final int TOTAL_MEM = BIG_ALLOC_SIZE;
- final SlabImpl slab = new SlabImpl(TOTAL_MEM);
- final AtomicReference<OutOfOffHeapMemoryException> ooom = new AtomicReference<OutOfOffHeapMemoryException>();
- final OutOfOffHeapMemoryListener oooml = new OutOfOffHeapMemoryListener() {
- @Override
- public void outOfOffHeapMemory(OutOfOffHeapMemoryException cause) {
- ooom.set(cause);
- }
- @Override
- public void close() {
- }
- };
- try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(oooml, new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
- // make a big allocation
- StoredObject bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
- assertNull(ooom.get());
- // drive the ma to ooom with small allocations
- try {
- StoredObject smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
- fail("Expected out of memory");
- } catch (OutOfOffHeapMemoryException expected) {
- }
- assertNotNull(ooom.get());
- assertTrue(ooom.get().getMessage().contains("Out of off-heap memory. Could not allocate size of "));
- } finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java
index d444865..681bec0 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java
@@ -39,7 +39,7 @@ import com.gemstone.gemfire.test.junit.categories.UnitTest;
@Category(UnitTest.class)
public class TinyMemoryBlockJUnitTest {
- private SimpleMemoryAllocatorImpl ma;
+ private MemoryAllocatorImpl ma;
private OutOfOffHeapMemoryListener ooohml;
private OffHeapMemoryStats stats;
@@ -50,7 +50,7 @@ public class TinyMemoryBlockJUnitTest {
};
private static class TestableFreeListManager extends FreeListManager {
- TestableFreeListManager(SimpleMemoryAllocatorImpl ma, final Slab[] slabs) {
+ TestableFreeListManager(MemoryAllocatorImpl ma, final Slab[] slabs) {
super (ma, slabs);
}
}
@@ -73,12 +73,12 @@ public class TinyMemoryBlockJUnitTest {
public void setUp() throws Exception {
ooohml = mock(OutOfOffHeapMemoryListener.class);
stats = mock(OffHeapMemoryStats.class);
- ma = (SimpleMemoryAllocatorImpl) SimpleMemoryAllocatorImpl.createForUnitTest(ooohml, stats, slabs);
+ ma = (MemoryAllocatorImpl) MemoryAllocatorImpl.createForUnitTest(ooohml, stats, slabs);
}
@After
public void tearDown() throws Exception {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
protected Object getValue() {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TxReleasesOffHeapOnCloseJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TxReleasesOffHeapOnCloseJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TxReleasesOffHeapOnCloseJUnitTest.java
index 2bc5759..d8999fe 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TxReleasesOffHeapOnCloseJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TxReleasesOffHeapOnCloseJUnitTest.java
@@ -49,7 +49,7 @@ public class TxReleasesOffHeapOnCloseJUnitTest {
@Test
public void testTxReleasesOffHeapOnClose() {
createCache();
- SimpleMemoryAllocatorImpl sma = SimpleMemoryAllocatorImpl.getAllocator();
+ MemoryAllocatorImpl sma = MemoryAllocatorImpl.getAllocator();
RegionFactory rf = cache.createRegionFactory();
rf.setOffHeap(true);
Region r = rf.create("testTxReleasesOffHeapOnClose");
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java
index 8380f57..28e0439 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java
@@ -23,7 +23,7 @@ import java.nio.ByteBuffer;
import org.junit.experimental.categories.Category;
import com.gemstone.gemfire.internal.offheap.OffHeapStoredObject;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.internal.offheap.StoredObject;
import com.gemstone.gemfire.internal.tcp.ByteBufferInputStream.ByteSource;
import com.gemstone.gemfire.internal.tcp.ByteBufferInputStream.ByteSourceFactory;
@@ -34,7 +34,7 @@ public class OffHeapByteBufferByteSourceJUnitTest extends OffHeapByteSourceJUnit
@Override
protected ByteSource createByteSource(byte[] bytes) {
- StoredObject so = SimpleMemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, false, false);
+ StoredObject so = MemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, false, false);
if (so instanceof OffHeapStoredObject) {
OffHeapStoredObject c = (OffHeapStoredObject) so;
ByteBuffer bb = c.createDirectByteBuffer();
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java
index 2111f79..7946b7e 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java
@@ -23,7 +23,7 @@ import org.junit.experimental.categories.Category;
import com.gemstone.gemfire.internal.offheap.OffHeapStoredObject;
import com.gemstone.gemfire.internal.offheap.NullOffHeapMemoryStats;
import com.gemstone.gemfire.internal.offheap.NullOutOfOffHeapMemoryListener;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.internal.offheap.StoredObject;
import com.gemstone.gemfire.internal.offheap.SlabImpl;
import com.gemstone.gemfire.internal.tcp.ByteBufferInputStream.ByteSource;
@@ -36,12 +36,12 @@ public class OffHeapByteSourceJUnitTest extends ByteSourceJUnitTest {
@Before
public void setUp() throws Exception {
- SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
+ MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
}
@After
public void tearDown() throws Exception {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
@Override
@@ -51,7 +51,7 @@ public class OffHeapByteSourceJUnitTest extends ByteSourceJUnitTest {
@Override
protected ByteSource createByteSource(byte[] bytes) {
- StoredObject so = SimpleMemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, false, false);
+ StoredObject so = MemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, false, false);
if (so instanceof OffHeapStoredObject) {
// bypass the factory to make sure that OffHeapByteSource is tested
return new OffHeapByteSource(so);
[2/3] incubator-geode git commit: renamed SimpleMemoryAllocator to
MemoryAllocator
Posted by ds...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/LifecycleListenerJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/LifecycleListenerJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/LifecycleListenerJUnitTest.java
index a009661..e10ca0a 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/LifecycleListenerJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/LifecycleListenerJUnitTest.java
@@ -54,7 +54,7 @@ public class LifecycleListenerJUnitTest {
this.afterCreateCallbacks.clear();
this.afterReuseCallbacks.clear();
this.beforeCloseCallbacks.clear();
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
@Test
@@ -63,7 +63,7 @@ public class LifecycleListenerJUnitTest {
LifecycleListener.removeLifecycleListener(this.listener);
SlabImpl slab = new SlabImpl(1024); // 1k
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(),
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(),
new SlabImpl[] { slab });
Assert.assertEquals(0, this.afterCreateCallbacks.size());
@@ -83,7 +83,7 @@ public class LifecycleListenerJUnitTest {
public void testCallbacksAreCalledAfterCreate() {
LifecycleListener.addLifecycleListener(this.listener);
SlabImpl slab = new SlabImpl(1024); // 1k
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(),
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(),
new SlabImpl[] { slab });
Assert.assertEquals(1, this.afterCreateCallbacks.size());
@@ -104,10 +104,10 @@ public class LifecycleListenerJUnitTest {
LifecycleListener.addLifecycleListener(this.listener);
- System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "false");
+ System.setProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "false");
SlabImpl slab = new SlabImpl(1024); // 1k
- SimpleMemoryAllocatorImpl ma = createAllocator(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { slab });
+ MemoryAllocatorImpl ma = createAllocator(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { slab });
Assert.assertEquals(1, this.afterCreateCallbacks.size());
Assert.assertEquals(0, this.afterReuseCallbacks.size());
@@ -125,7 +125,7 @@ public class LifecycleListenerJUnitTest {
Assert.assertEquals(1, this.afterReuseCallbacks.size());
Assert.assertEquals(1, this.beforeCloseCallbacks.size());
- SimpleMemoryAllocatorImpl ma2 = createAllocator(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { slab });
+ MemoryAllocatorImpl ma2 = createAllocator(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { slab });
assertEquals(null, ma2);
Assert.assertEquals(1, this.afterCreateCallbacks.size());
@@ -139,20 +139,20 @@ public class LifecycleListenerJUnitTest {
Assert.assertEquals(2, this.beforeCloseCallbacks.size());
}
- private SimpleMemoryAllocatorImpl createAllocator(OutOfOffHeapMemoryListener ooohml, OffHeapMemoryStats ohms, SlabImpl[] slab) {
+ private MemoryAllocatorImpl createAllocator(OutOfOffHeapMemoryListener ooohml, OffHeapMemoryStats ohms, SlabImpl[] slab) {
try {
- return SimpleMemoryAllocatorImpl.createForUnitTest(ooohml, ohms, slab);
+ return MemoryAllocatorImpl.createForUnitTest(ooohml, ohms, slab);
} catch (IllegalStateException e) {
return null;
}
}
- private void closeAndFree(SimpleMemoryAllocatorImpl ma) {
- System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
+ private void closeAndFree(MemoryAllocatorImpl ma) {
+ System.setProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
try {
ma.close();
} finally {
- System.clearProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
+ System.clearProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
}
}
@@ -162,7 +162,7 @@ public class LifecycleListenerJUnitTest {
LifecycleListener.addLifecycleListener(this.listener);
SlabImpl slab = new SlabImpl(1024); // 1k
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { slab });
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { slab });
Assert.assertEquals(1, this.afterCreateCallbacks.size());
Assert.assertEquals(0, this.afterReuseCallbacks.size());
@@ -175,7 +175,7 @@ public class LifecycleListenerJUnitTest {
Assert.assertEquals(1, this.beforeCloseCallbacks.size());
slab = new SlabImpl(1024); // 1k
- SimpleMemoryAllocatorImpl ma2 = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { slab });
+ MemoryAllocatorImpl ma2 = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[] { slab });
Assert.assertEquals(2, this.afterCreateCallbacks.size());
Assert.assertEquals(0, this.afterReuseCallbacks.size());
@@ -189,11 +189,11 @@ public class LifecycleListenerJUnitTest {
}
static final class LifecycleListenerCallback {
- private final SimpleMemoryAllocatorImpl allocator;
+ private final MemoryAllocatorImpl allocator;
private final long timeStamp;
private final Throwable creationTime;
- LifecycleListenerCallback(SimpleMemoryAllocatorImpl allocator) {
+ LifecycleListenerCallback(MemoryAllocatorImpl allocator) {
this.allocator = allocator;
this.timeStamp = System.currentTimeMillis();
this.creationTime = new Exception();
@@ -213,17 +213,17 @@ public class LifecycleListenerJUnitTest {
}
@Override
- public void afterCreate(SimpleMemoryAllocatorImpl allocator) {
+ public void afterCreate(MemoryAllocatorImpl allocator) {
this.afterCreateCallbacks.add(new LifecycleListenerCallback(allocator));
}
@Override
- public void afterReuse(SimpleMemoryAllocatorImpl allocator) {
+ public void afterReuse(MemoryAllocatorImpl allocator) {
this.afterReuseCallbacks.add(new LifecycleListenerCallback(allocator));
}
@Override
- public void beforeClose(SimpleMemoryAllocatorImpl allocator) {
+ public void beforeClose(MemoryAllocatorImpl allocator) {
this.beforeCloseCallbacks.add(new LifecycleListenerCallback(allocator));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorFillPatternIntegrationTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorFillPatternIntegrationTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorFillPatternIntegrationTest.java
new file mode 100644
index 0000000..2f202f8
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorFillPatternIntegrationTest.java
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.internal.offheap;
+
+import static org.junit.Assert.*;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
+
+/**
+ * Tests fill pattern validation for the {@link MemoryAllocatorImpl}.
+ */
+@Category(IntegrationTest.class)
+public class MemoryAllocatorFillPatternIntegrationTest {
+ private static Random random = ThreadLocalRandom.current();
+
+ /**
+ * Chunk operation types.
+ */
+ static enum Operation {
+ ALLOCATE,
+ FREE,
+ WRITE;
+
+ // Holds all Operation values
+ private static Operation[] values = Operation.values();
+
+ static Operation randomOperation() {
+ return values[random.nextInt(values.length)];
+ }
+ };
+
+ /** Number of worker threads for advanced tests. */
+ private static final int WORKER_THREAD_COUNT = 5;
+
+ /** Size of single test slab.*/
+ private static final int SLAB_SIZE = 1024 * 1024 * 50;
+
+ /** Maximum number of bytes a worker thread can allocate during advanced tests. */
+ private static final int MAX_WORKER_ALLOCATION_TOTAL_SIZE = SLAB_SIZE / WORKER_THREAD_COUNT / 2;
+
+ /** Maximum allocation for a single Chunk. */
+ private static final int MAX_WORKER_ALLOCATION_SIZE = 512;
+
+ /** Canned data for write operations. */
+ private static final byte[] WRITE_BYTES = new String("Some string data.").getBytes();
+
+ /** Minimum size for write operations. */
+ private static final int MIN_WORKER_ALLOCATION_SIZE = WRITE_BYTES.length;
+
+ /** Runtime for worker threads. */
+ private static final long RUN_TIME_IN_MILLIS = 1 * 1000 * 5;
+
+ /** Chunk size for basic huge allocation test. */
+ private static final int HUGE_CHUNK_SIZE = 1024 * 200;
+
+ /** Our test victim. */
+ private MemoryAllocatorImpl allocator = null;
+
+ /** Our test victim's memory slab. */
+ private SlabImpl slab = null;
+
+ /**
+ * Enables fill validation and creates the test victim.
+ */
+ @Before
+ public void setUp() throws Exception {
+ System.setProperty("gemfire.validateOffHeapWithFill", "true");
+ this.slab = new SlabImpl(SLAB_SIZE);
+ this.allocator = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{this.slab});
+ }
+
+ /**
+ * Frees off heap memory.
+ */
+ @After
+ public void tearDown() throws Exception {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ System.clearProperty("gemfire.validateOffHeapWithFill");
+ }
+
+ /**
+ * This test hammers a MemoryAllocatorImpl with multiple threads exercising
+ * the fill validation of tiny Chunks for one minute. This, of course, exercises many aspects of
+ * the MemoryAllocatorImpl and its helper classes.
+ * @throws Exception
+ */
+ @Test
+ public void testFillPatternAdvancedForTinyAllocations() throws Exception {
+ doFillPatternAdvancedTest(new ChunkSizer() {
+ @Override
+ public int allocationSize() {
+ int allocation = random.nextInt(MAX_WORKER_ALLOCATION_SIZE+1);
+
+ while(allocation < MIN_WORKER_ALLOCATION_SIZE) {
+ allocation = random.nextInt(MAX_WORKER_ALLOCATION_SIZE+1);
+ }
+ return allocation;
+ }
+ });
+ }
+
+ /**
+ * This test hammers a MemoryAllocatorImpl with multiple threads exercising
+ * the fill validation of huge Chunks for one minute. This, of course, exercises many aspects of
+ * the MemoryAllocatorImpl and its helper classes.
+ * @throws Exception
+ */
+ @Test
+ public void testFillPatternAdvancedForHugeAllocations() throws Exception {
+ doFillPatternAdvancedTest(new ChunkSizer() {
+ @Override
+ public int allocationSize() {
+ return HUGE_CHUNK_SIZE;
+ }
+ });
+ }
+
+ private interface ChunkSizer {
+ int allocationSize();
+ }
+
+ private void doFillPatternAdvancedTest(final ChunkSizer chunkSizer) throws InterruptedException {
+ // Used to manage worker thread completion
+ final CountDownLatch latch = new CountDownLatch(WORKER_THREAD_COUNT);
+
+ // Use to track any errors the worker threads will encounter
+ final List<Throwable> threadErrorList = Collections.synchronizedList(new LinkedList<Throwable>());
+
+ /*
+ * Start up a number of worker threads. These threads will randomly allocate, free,
+ * and write to Chunks.
+ */
+ for(int i = 0;i < WORKER_THREAD_COUNT;++i) {
+ new Thread(new Runnable() {
+ // Total allocation in bytes for this thread
+ private int totalAllocation = 0;
+
+ // List of Chunks allocated by this thread
+ private List<OffHeapStoredObject> chunks = new LinkedList<OffHeapStoredObject>();
+
+ // Time to end thread execution
+ private long endTime = System.currentTimeMillis() + RUN_TIME_IN_MILLIS;
+
+ /**
+ * Allocates a chunk and adds it to the thread's Chunk list.
+ */
+ private void allocate() {
+ int allocation = chunkSizer.allocationSize();
+ OffHeapStoredObject chunk = (OffHeapStoredObject) allocator.allocate(allocation);
+
+ // This should always work just after allocation
+ chunk.validateFill();
+
+ chunks.add(chunk);
+ totalAllocation += chunk.getSize();
+ }
+
+ /**
+ * Frees a random chunk from the Chunk list.
+ */
+ private void free() {
+ OffHeapStoredObject chunk = chunks.remove(random.nextInt(chunks.size()));
+ totalAllocation -= chunk.getSize();
+
+ /*
+ * Chunk is filled here but another thread may have already grabbed it so we
+ * cannot validate the fill.
+ */
+ chunk.release();
+ }
+
+ /**
+ * Writes canned data to a random Chunk from the Chunk list.
+ */
+ private void write() {
+ OffHeapStoredObject chunk = chunks.get(random.nextInt(chunks.size()));
+ chunk.writeDataBytes(0, WRITE_BYTES);
+ }
+
+ /**
+ * Randomly selects Chunk operations and executes them
+ * for a period of time. Collects any error thrown during execution.
+ */
+ @Override
+ public void run() {
+ try {
+ for(long currentTime = System.currentTimeMillis();currentTime < endTime;currentTime = System.currentTimeMillis()) {
+ Operation op = (totalAllocation == 0 ? Operation.ALLOCATE : (totalAllocation >= MAX_WORKER_ALLOCATION_TOTAL_SIZE ? Operation.FREE : Operation.randomOperation()));
+ switch(op) {
+ case ALLOCATE:
+ allocate();
+ break;
+ case FREE:
+ free();
+ break;
+ case WRITE:
+ write();
+ break;
+ }
+ }
+ } catch (Throwable t) {
+ threadErrorList.add(t);
+ } finally {
+ latch.countDown();
+ }
+ }
+ }).start();
+ }
+
+ // Make sure each thread ended cleanly
+ assertTrue(latch.await(2, TimeUnit.MINUTES));
+
+ // Fail on the first error we find
+ if(!threadErrorList.isEmpty()) {
+ fail(threadErrorList.get(0).getMessage());
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorFillPatternJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorFillPatternJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorFillPatternJUnitTest.java
new file mode 100644
index 0000000..f1d223d
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorFillPatternJUnitTest.java
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.internal.offheap;
+
+import static org.junit.Assert.*;
+import static com.googlecode.catchexception.CatchException.*;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests fill pattern validation for the {@link MemoryAllocatorImpl}.
+ * @author rholmes
+ */
+@Category(UnitTest.class)
+public class MemoryAllocatorFillPatternJUnitTest {
+
+ /** Size of single test slab.*/
+ private static final int SLAB_SIZE = 1024 * 1024 * 50;
+
+ /** Canned data for write operations. */
+ private static final byte[] WRITE_BYTES = new String("Some string data.").getBytes();
+
+ /** Chunk size for basic huge allocation test. */
+ private static final int HUGE_CHUNK_SIZE = 1024 * 200;
+
+ /** The number of chunks to allocate in order to force compaction. */
+ private static final int COMPACTION_CHUNKS = 3;
+
+ /** Our slab size divided in three (with some padding for safety). */
+ private static final int COMPACTION_CHUNK_SIZE = (SLAB_SIZE / COMPACTION_CHUNKS) - 1024;
+
+ /** This should force compaction when allocated. */
+ private static final int FORCE_COMPACTION_CHUNK_SIZE = COMPACTION_CHUNK_SIZE * 2;
+
+ /** Our test victim. */
+ private MemoryAllocatorImpl allocator = null;
+
+ /** Our test victim's memory slab. */
+ private SlabImpl slab = null;
+
+ /**
+ * Enables fill validation and creates the test victim.
+ */
+ @Before
+ public void setUp() throws Exception {
+ System.setProperty("gemfire.validateOffHeapWithFill", "true");
+ this.slab = new SlabImpl(SLAB_SIZE);
+ this.allocator = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{this.slab});
+ }
+
+ /**
+ * Frees off heap memory.
+ */
+ @After
+ public void tearDown() throws Exception {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ System.clearProperty("gemfire.validateOffHeapWithFill");
+ }
+
+ /**
+ * This tests the fill pattern for a single tiny Chunk allocation.
+ * @throws Exception
+ */
+ @Test
+ public void testFillPatternBasicForTinyAllocations() throws Exception {
+ doFillPatternBasic(1024);
+ }
+
+ /**
+ * This tests the fill pattern for a single huge Chunk allocation.
+ * @throws Exception
+ */
+ @Test
+ public void testFillPatternBasicForHugeAllocations() throws Exception {
+ doFillPatternBasic(HUGE_CHUNK_SIZE);
+ }
+
+ private void doFillPatternBasic(final int chunkSize) {
+ /*
+ * Pull a chunk off the fragment. This will have no fill because
+ * it is a "fresh" chunk.
+ */
+ OffHeapStoredObject chunk = (OffHeapStoredObject) this.allocator.allocate(chunkSize);
+
+ /*
+ * Chunk should have valid fill from initial fragment allocation.
+ */
+ chunk.validateFill();
+
+ // "Dirty" the chunk so the release has something to fill over
+ chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
+
+ // This should free the Chunk (ref count == 1)
+ chunk.release();
+
+ /*
+ * This chunk should have a fill because it was reused from the
+ * free list (assuming no fragmentation at this point...)
+ */
+ chunk = (OffHeapStoredObject) this.allocator.allocate(chunkSize);
+
+ // Make sure we have a fill this time
+ chunk.validateFill();
+
+ // Give the fill code something to write over during the release
+ chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
+ chunk.release();
+
+ // Again, make sure the release implemented the fill
+ chunk.validateFill();
+
+ // "Dirty up" the free chunk
+ chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
+
+ catchException(chunk).validateFill();
+ assertTrue(caughtException() instanceof IllegalStateException);
+ assertEquals("Fill pattern violated for chunk " + chunk.getAddress() + " with size " + chunk.getSize(), caughtException().getMessage());
+
+ }
+
+ /**
+ * This tests that fill validation is working properly on newly created fragments after
+ * a compaction.
+ * @throws Exception
+ */
+ @Test
+ public void testFillPatternAfterCompaction() throws Exception {
+ /*
+ * Stores our allocated memory.
+ */
+ OffHeapStoredObject[] allocatedChunks = new OffHeapStoredObject[COMPACTION_CHUNKS];
+
+ /*
+ * Use up most of our memory
+ * Our memory looks like [ ][ ][ ]
+ */
+ for(int i =0;i < allocatedChunks.length;++i) {
+ allocatedChunks[i] = (OffHeapStoredObject) this.allocator.allocate(COMPACTION_CHUNK_SIZE);
+ allocatedChunks[i].validateFill();
+ }
+
+ /*
+ * Release some of our allocated chunks.
+ */
+ for(int i=0;i < 2;++i) {
+ allocatedChunks[i].release();
+ allocatedChunks[i].validateFill();
+ }
+
+ /*
+ * Now, allocate another chunk that is slightly larger than one of
+ * our initial chunks. This should force a compaction causing our
+ * memory to look like [ ][ ].
+ */
+ OffHeapStoredObject slightlyLargerChunk = (OffHeapStoredObject) this.allocator.allocate(FORCE_COMPACTION_CHUNK_SIZE);
+
+ /*
+ * Make sure the compacted memory has the fill validation.
+ */
+ slightlyLargerChunk.validateFill();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorJUnitTest.java
new file mode 100644
index 0000000..7639f8d
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorJUnitTest.java
@@ -0,0 +1,594 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.internal.offheap;
+
+import static org.junit.Assert.*;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.RestoreSystemProperties;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.OutOfOffHeapMemoryException;
+import com.gemstone.gemfire.cache.CacheClosedException;
+import com.gemstone.gemfire.internal.logging.NullLogWriter;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class MemoryAllocatorJUnitTest {
+ @Rule
+ public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();
+
+ private static int round(int multiple, int v) {
+ return ((v+multiple-1)/multiple)*multiple;
+ }
+ @Test
+ public void testNullGetAllocator() {
+ try {
+ MemoryAllocatorImpl.getAllocator();
+ fail("expected CacheClosedException");
+ } catch (CacheClosedException expected) {
+ }
+ }
+ @Test
+ public void testConstructor() {
+ try {
+ MemoryAllocatorImpl.createForUnitTest(null, null, null);
+ fail("expected IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+ }
+ }
+ @Test
+ public void testCreate() {
+ System.setProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "false");
+ {
+ NullOutOfOffHeapMemoryListener listener = new NullOutOfOffHeapMemoryListener();
+ NullOffHeapMemoryStats stats = new NullOffHeapMemoryStats();
+ try {
+ MemoryAllocatorImpl.createForUnitTest(listener, stats, 10, 950, 100, new SlabFactory() {
+ @Override
+ public Slab create(int size) {
+ throw new OutOfMemoryError("expected");
+ }
+ });
+ } catch (OutOfMemoryError expected) {
+ }
+ assertTrue(listener.isClosed());
+ assertTrue(stats.isClosed());
+ }
+ {
+ NullOutOfOffHeapMemoryListener listener = new NullOutOfOffHeapMemoryListener();
+ NullOffHeapMemoryStats stats = new NullOffHeapMemoryStats();
+ int MAX_SLAB_SIZE = 100;
+ try {
+ SlabFactory factory = new SlabFactory() {
+ private int createCount = 0;
+ @Override
+ public Slab create(int size) {
+ createCount++;
+ if (createCount == 1) {
+ return new SlabImpl(size);
+ } else {
+ throw new OutOfMemoryError("expected");
+ }
+ }
+ };
+ MemoryAllocatorImpl.createForUnitTest(listener, stats, 10, 950, MAX_SLAB_SIZE, factory);
+ } catch (OutOfMemoryError expected) {
+ }
+ assertTrue(listener.isClosed());
+ assertTrue(stats.isClosed());
+ }
+ {
+ NullOutOfOffHeapMemoryListener listener = new NullOutOfOffHeapMemoryListener();
+ NullOffHeapMemoryStats stats = new NullOffHeapMemoryStats();
+ SlabFactory factory = new SlabFactory() {
+ @Override
+ public Slab create(int size) {
+ return new SlabImpl(size);
+ }
+ };
+ MemoryAllocator ma =
+ MemoryAllocatorImpl.createForUnitTest(listener, stats, 10, 950, 100, factory);
+ try {
+ assertFalse(listener.isClosed());
+ assertFalse(stats.isClosed());
+ ma.close();
+ assertTrue(listener.isClosed());
+ assertFalse(stats.isClosed());
+ listener = new NullOutOfOffHeapMemoryListener();
+ NullOffHeapMemoryStats stats2 = new NullOffHeapMemoryStats();
+ {
+ SlabImpl slab = new SlabImpl(1024);
+ try {
+ MemoryAllocatorImpl.createForUnitTest(listener, stats2, new SlabImpl[]{slab});
+ } catch (IllegalStateException expected) {
+ assertTrue("unexpected message: " + expected.getMessage(),
+ expected.getMessage().equals("attempted to reuse existing off-heap memory even though new off-heap memory was allocated"));
+ } finally {
+ slab.free();
+ }
+ assertFalse(stats.isClosed());
+ assertTrue(listener.isClosed());
+ assertTrue(stats2.isClosed());
+ }
+ listener = new NullOutOfOffHeapMemoryListener();
+ stats2 = new NullOffHeapMemoryStats();
+ MemoryAllocator ma2 = MemoryAllocatorImpl.createForUnitTest(listener, stats2, 10, 950, 100, factory);
+ assertSame(ma, ma2);
+ assertTrue(stats.isClosed());
+ assertFalse(listener.isClosed());
+ assertFalse(stats2.isClosed());
+ stats = stats2;
+ ma.close();
+ assertTrue(listener.isClosed());
+ assertFalse(stats.isClosed());
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ assertTrue(stats.isClosed());
+ }
+ }
+ @Test
+ public void testBasics() {
+ int BATCH_SIZE = 1;
+ int TINY_MULTIPLE = FreeListManager.TINY_MULTIPLE;
+ int HUGE_MULTIPLE = FreeListManager.HUGE_MULTIPLE;
+ int perObjectOverhead = OffHeapStoredObject.HEADER_SIZE;
+ int maxTiny = FreeListManager.MAX_TINY-perObjectOverhead;
+ int minHuge = maxTiny+1;
+ int TOTAL_MEM = (maxTiny+perObjectOverhead)*BATCH_SIZE /*+ (maxBig+perObjectOverhead)*BATCH_SIZE*/ + round(TINY_MULTIPLE, minHuge+1+perObjectOverhead)*BATCH_SIZE + (TINY_MULTIPLE+perObjectOverhead)*BATCH_SIZE /*+ (MIN_BIG_SIZE+perObjectOverhead)*BATCH_SIZE*/ + round(TINY_MULTIPLE, minHuge+perObjectOverhead+1);
+ SlabImpl slab = new SlabImpl(TOTAL_MEM);
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ assertEquals(TOTAL_MEM, ma.getFreeMemory());
+ assertEquals(TOTAL_MEM, ma.freeList.getFreeFragmentMemory());
+ assertEquals(0, ma.freeList.getFreeTinyMemory());
+ assertEquals(0, ma.freeList.getFreeHugeMemory());
+ StoredObject tinymc = ma.allocate(maxTiny);
+ assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
+ assertEquals(round(TINY_MULTIPLE, maxTiny+perObjectOverhead)*(BATCH_SIZE-1), ma.freeList.getFreeTinyMemory());
+ StoredObject hugemc = ma.allocate(minHuge);
+ assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, minHuge+perObjectOverhead)/*-round(BIG_MULTIPLE, maxBig+perObjectOverhead)*/-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
+ long freeSlab = ma.freeList.getFreeFragmentMemory();
+ long oldFreeHugeMemory = ma.freeList.getFreeHugeMemory();
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-1), oldFreeHugeMemory);
+ hugemc.release();
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead), ma.freeList.getFreeHugeMemory()-oldFreeHugeMemory);
+ assertEquals(TOTAL_MEM/*-round(BIG_MULTIPLE, maxBig+perObjectOverhead)*/-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
+ assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
+ long oldFreeTinyMemory = ma.freeList.getFreeTinyMemory();
+ tinymc.release();
+ assertEquals(round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.freeList.getFreeTinyMemory()-oldFreeTinyMemory);
+ assertEquals(TOTAL_MEM, ma.getFreeMemory());
+ // now lets reallocate from the free lists
+ tinymc = ma.allocate(maxTiny);
+ assertEquals(oldFreeTinyMemory, ma.freeList.getFreeTinyMemory());
+ assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
+ hugemc = ma.allocate(minHuge);
+ assertEquals(oldFreeHugeMemory, ma.freeList.getFreeHugeMemory());
+ assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, minHuge+perObjectOverhead)/*-round(BIG_MULTIPLE, maxBig+perObjectOverhead)*/-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
+ hugemc.release();
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead), ma.freeList.getFreeHugeMemory()-oldFreeHugeMemory);
+ assertEquals(TOTAL_MEM/*-round(BIG_MULTIPLE, maxBig+perObjectOverhead)*/-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
+ assertEquals(TOTAL_MEM-round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.getFreeMemory());
+ tinymc.release();
+ assertEquals(round(TINY_MULTIPLE, maxTiny+perObjectOverhead), ma.freeList.getFreeTinyMemory()-oldFreeTinyMemory);
+ assertEquals(TOTAL_MEM, ma.getFreeMemory());
+ // None of the reallocates should have come from the slab.
+ assertEquals(freeSlab, ma.freeList.getFreeFragmentMemory());
+ tinymc = ma.allocate(1);
+ assertEquals(round(TINY_MULTIPLE, 1+perObjectOverhead), tinymc.getSize());
+ assertEquals(freeSlab-(round(TINY_MULTIPLE, 1+perObjectOverhead)*BATCH_SIZE), ma.freeList.getFreeFragmentMemory());
+ freeSlab = ma.freeList.getFreeFragmentMemory();
+ tinymc.release();
+ assertEquals(round(TINY_MULTIPLE, maxTiny+perObjectOverhead)+(round(TINY_MULTIPLE, 1+perObjectOverhead)*BATCH_SIZE), ma.freeList.getFreeTinyMemory()-oldFreeTinyMemory);
+
+ hugemc = ma.allocate(minHuge+1);
+ assertEquals(round(TINY_MULTIPLE, minHuge+1+perObjectOverhead), hugemc.getSize());
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-1), ma.freeList.getFreeHugeMemory());
+ hugemc.release();
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*BATCH_SIZE, ma.freeList.getFreeHugeMemory());
+ hugemc = ma.allocate(minHuge);
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-1), ma.freeList.getFreeHugeMemory());
+ if (BATCH_SIZE > 1) {
+ StoredObject hugemc2 = ma.allocate(minHuge);
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-2), ma.freeList.getFreeHugeMemory());
+ hugemc2.release();
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*(BATCH_SIZE-1), ma.freeList.getFreeHugeMemory());
+ }
+ hugemc.release();
+ assertEquals(round(TINY_MULTIPLE, minHuge+perObjectOverhead)*BATCH_SIZE, ma.freeList.getFreeHugeMemory());
+ // now that we do compaction the following allocate works.
+ hugemc = ma.allocate(minHuge + HUGE_MULTIPLE + HUGE_MULTIPLE-1);
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+
+ @Test
+ public void testChunkCreateDirectByteBuffer() {
+ SlabImpl slab = new SlabImpl(1024*1024);
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ ByteBuffer bb = ByteBuffer.allocate(1024);
+ for (int i=0; i < 1024; i++) {
+ bb.put((byte) i);
+ }
+ bb.position(0);
+ OffHeapStoredObject c = (OffHeapStoredObject) ma.allocateAndInitialize(bb.array(), false, false);
+ assertEquals(1024, c.getDataSize());
+ if (!Arrays.equals(bb.array(), c.getRawBytes())) {
+ fail("arrays are not equal. Expected " + Arrays.toString(bb.array()) + " but found: " + Arrays.toString(c.getRawBytes()));
+ }
+ ByteBuffer dbb = c.createDirectByteBuffer();
+ assertEquals(true, dbb.isDirect());
+ assertEquals(bb, dbb);
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+
+ @Test
+ public void testDebugLog() {
+ MemoryAllocatorImpl.debugLog("test debug log", false);
+ MemoryAllocatorImpl.debugLog("test debug log", true);
+ }
+ @Test
+ public void testGetLostChunks() {
+ SlabImpl slab = new SlabImpl(1024*1024);
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ assertEquals(Collections.emptyList(), ma.getLostChunks());
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+ @Test
+ public void testFindSlab() {
+ final int SLAB_SIZE = 1024*1024;
+ SlabImpl slab = new SlabImpl(SLAB_SIZE);
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ assertEquals(0, ma.findSlab(slab.getMemoryAddress()));
+ assertEquals(0, ma.findSlab(slab.getMemoryAddress()+SLAB_SIZE-1));
+ try {
+ ma.findSlab(slab.getMemoryAddress()-1);
+ fail("expected IllegalStateException");
+ } catch (IllegalStateException expected) {
+ }
+ try {
+ ma.findSlab(slab.getMemoryAddress()+SLAB_SIZE);
+ fail("expected IllegalStateException");
+ } catch (IllegalStateException expected) {
+ }
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+ @Test
+ public void testValidateAddressAndSize() {
+ final int SLAB_SIZE = 1024*1024;
+ SlabImpl slab = new SlabImpl(SLAB_SIZE);
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ try {
+ MemoryAllocatorImpl.validateAddress(0L);
+ fail("expected IllegalStateException");
+ } catch (IllegalStateException expected) {
+ assertEquals("Unexpected exception message: " + expected.getMessage(), true, expected.getMessage().contains("addr was smaller than expected"));
+ }
+ try {
+ MemoryAllocatorImpl.validateAddress(1L);
+ fail("expected IllegalStateException");
+ } catch (IllegalStateException expected) {
+ assertEquals("Unexpected exception message: " + expected.getMessage(), true, expected.getMessage().contains("Valid addresses must be in one of the following ranges:"));
+ }
+ MemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress(), SLAB_SIZE, false);
+ MemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress(), SLAB_SIZE, true);
+ MemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress(), -1, true);
+ try {
+ MemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress()-1, SLAB_SIZE, true);
+ fail("expected IllegalStateException");
+ } catch (IllegalStateException expected) {
+ assertEquals("Unexpected exception message: " + expected.getMessage(), true, expected.getMessage().equals(" address 0x" + Long.toString(slab.getMemoryAddress()-1, 16) + " does not address the original slab memory"));
+ }
+ try {
+ MemoryAllocatorImpl.validateAddressAndSizeWithinSlab(slab.getMemoryAddress(), SLAB_SIZE+1, true);
+ fail("expected IllegalStateException");
+ } catch (IllegalStateException expected) {
+ assertEquals("Unexpected exception message: " + expected.getMessage(), true, expected.getMessage().equals(" address 0x" + Long.toString(slab.getMemoryAddress()+SLAB_SIZE, 16) + " does not address the original slab memory"));
+ }
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+ @Test
+ public void testMemoryInspection() {
+ final int SLAB_SIZE = 1024*1024;
+ SlabImpl slab = new SlabImpl(SLAB_SIZE);
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryInspector inspector = ma.getMemoryInspector();
+ assertNotNull(inspector);
+ assertEquals(null, inspector.getFirstBlock());
+ assertEquals(Collections.emptyList(), inspector.getSnapshot());
+ assertEquals(Collections.emptyList(), inspector.getAllocatedBlocks());
+ assertEquals(null, inspector.getBlockAfter(null));
+ inspector.createSnapshot();
+ // call this twice for code coverage
+ inspector.createSnapshot();
+ try {
+ assertEquals(inspector.getAllBlocks(), inspector.getSnapshot());
+ MemoryBlock firstBlock = inspector.getFirstBlock();
+ assertNotNull(firstBlock);
+ assertEquals(1024*1024, firstBlock.getBlockSize());
+ assertEquals("N/A", firstBlock.getDataType());
+ assertEquals(-1, firstBlock.getFreeListId());
+ assertTrue(firstBlock.getAddress() > 0);
+ assertNull(firstBlock.getNextBlock());
+ assertEquals(0, firstBlock.getRefCount());
+ assertEquals(0, firstBlock.getSlabId());
+ assertEquals(MemoryBlock.State.UNUSED, firstBlock.getState());
+ assertFalse(firstBlock.isCompressed());
+ assertFalse(firstBlock.isSerialized());
+ assertEquals(null, inspector.getBlockAfter(firstBlock));
+ } finally {
+ inspector.clearSnapshot();
+ }
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+
+ @Test
+ public void testClose() {
+ System.setProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "false");
+ SlabImpl slab = new SlabImpl(1024*1024);
+ boolean freeSlab = true;
+ SlabImpl[] slabs = new SlabImpl[]{slab};
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), slabs);
+ ma.close();
+ ma.close();
+ System.setProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
+ try {
+ ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), slabs);
+ ma.close();
+ freeSlab = false;
+ ma.close();
+ } finally {
+ System.clearProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
+ }
+ } finally {
+ if (freeSlab) {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+
+ }
+
+ @Test
+ public void testCompaction() {
+ final int perObjectOverhead = OffHeapStoredObject.HEADER_SIZE;
+ final int BIG_ALLOC_SIZE = 150000;
+ final int SMALL_ALLOC_SIZE = BIG_ALLOC_SIZE/2;
+ final int TOTAL_MEM = BIG_ALLOC_SIZE;
+ SlabImpl slab = new SlabImpl(TOTAL_MEM);
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ StoredObject bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
+ try {
+ StoredObject smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
+ fail("Expected out of memory");
+ } catch (OutOfOffHeapMemoryException expected) {
+ }
+ bmc.release();
+ assertEquals(TOTAL_MEM, ma.freeList.getFreeMemory());
+ StoredObject smc1 = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
+ StoredObject smc2 = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
+ smc2.release();
+ assertEquals(TOTAL_MEM-SMALL_ALLOC_SIZE, ma.freeList.getFreeMemory());
+ try {
+ bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
+ fail("Expected out of memory");
+ } catch (OutOfOffHeapMemoryException expected) {
+ }
+ smc1.release();
+ assertEquals(TOTAL_MEM, ma.freeList.getFreeMemory());
+ bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
+ bmc.release();
+ assertEquals(TOTAL_MEM, ma.freeList.getFreeMemory());
+ ArrayList<StoredObject> mcs = new ArrayList<StoredObject>();
+ for (int i=0; i < BIG_ALLOC_SIZE/(8+perObjectOverhead); i++) {
+ mcs.add(ma.allocate(8));
+ }
+ checkMcs(mcs);
+ assertEquals(0, ma.freeList.getFreeMemory());
+ try {
+ ma.allocate(8);
+ fail("expected out of memory");
+ } catch (OutOfOffHeapMemoryException expected) {
+ }
+ mcs.remove(0).release(); // frees 8+perObjectOverhead
+ assertEquals(8+perObjectOverhead, ma.freeList.getFreeMemory());
+ mcs.remove(0).release(); // frees 8+perObjectOverhead
+ assertEquals((8+perObjectOverhead)*2, ma.freeList.getFreeMemory());
+ ma.allocate(16).release(); // allocates and frees 16+perObjectOverhead; still have perObjectOverhead
+ assertEquals((8+perObjectOverhead)*2, ma.freeList.getFreeMemory());
+ mcs.remove(0).release(); // frees 8+perObjectOverhead
+ assertEquals((8+perObjectOverhead)*3, ma.freeList.getFreeMemory());
+ mcs.remove(0).release(); // frees 8+perObjectOverhead
+ assertEquals((8+perObjectOverhead)*4, ma.freeList.getFreeMemory());
+ // At this point I should have 8*4 + perObjectOverhead*4 of free memory
+ ma.allocate(8*4+perObjectOverhead*3).release();
+ assertEquals((8+perObjectOverhead)*4, ma.freeList.getFreeMemory());
+ mcs.remove(0).release(); // frees 8+perObjectOverhead
+ assertEquals((8+perObjectOverhead)*5, ma.freeList.getFreeMemory());
+ // At this point I should have 8*5 + perObjectOverhead*5 of free memory
+ try {
+ ma.allocate((8*5+perObjectOverhead*4)+1);
+ fail("expected out of memory");
+ } catch (OutOfOffHeapMemoryException expected) {
+ }
+ mcs.remove(0).release(); // frees 8+perObjectOverhead
+ assertEquals((8+perObjectOverhead)*6, ma.freeList.getFreeMemory());
+ checkMcs(mcs);
+ // At this point I should have 8*6 + perObjectOverhead*6 of free memory
+ StoredObject mc24 = ma.allocate(24);
+ checkMcs(mcs);
+ assertEquals((8+perObjectOverhead)*6 - (24+perObjectOverhead), ma.freeList.getFreeMemory());
+ // At this point I should have 8*3 + perObjectOverhead*5 of free memory
+ StoredObject mc16 = ma.allocate(16);
+ checkMcs(mcs);
+ assertEquals((8+perObjectOverhead)*6 - (24+perObjectOverhead) - (16+perObjectOverhead), ma.freeList.getFreeMemory());
+ // At this point I should have 8*1 + perObjectOverhead*4 of free memory
+ mcs.add(ma.allocate(8));
+ checkMcs(mcs);
+ assertEquals((8+perObjectOverhead)*6 - (24+perObjectOverhead) - (16+perObjectOverhead) - (8+perObjectOverhead), ma.freeList.getFreeMemory());
+ // At this point I should have 8*0 + perObjectOverhead*3 of free memory
+ StoredObject mcDO = ma.allocate(perObjectOverhead*2);
+ checkMcs(mcs);
+ // At this point I should have 8*0 + perObjectOverhead*0 of free memory
+ assertEquals(0, ma.freeList.getFreeMemory());
+ try {
+ ma.allocate(1);
+ fail("expected out of memory");
+ } catch (OutOfOffHeapMemoryException expected) {
+ }
+ checkMcs(mcs);
+ assertEquals(0, ma.freeList.getFreeMemory());
+ mcDO.release();
+ assertEquals((perObjectOverhead*3), ma.freeList.getFreeMemory());
+ mcs.remove(mcs.size()-1).release();
+ assertEquals((perObjectOverhead*3)+(8+perObjectOverhead), ma.freeList.getFreeMemory());
+ mc16.release();
+ assertEquals((perObjectOverhead*3)+(8+perObjectOverhead)+(16+perObjectOverhead), ma.freeList.getFreeMemory());
+ mc24.release();
+ assertEquals((perObjectOverhead*3)+(8+perObjectOverhead)+(16+perObjectOverhead)+(24+perObjectOverhead), ma.freeList.getFreeMemory());
+
+ long freeMem = ma.freeList.getFreeMemory();
+ for (StoredObject mc: mcs) {
+ mc.release();
+ assertEquals(freeMem+(8+perObjectOverhead), ma.freeList.getFreeMemory());
+ freeMem += (8+perObjectOverhead);
+ }
+ mcs.clear();
+ assertEquals(TOTAL_MEM, ma.freeList.getFreeMemory());
+ bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
+ bmc.release();
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+
+ long expectedMemoryUsage;
+ boolean memoryUsageEventReceived;
+ @Test
+ public void testUsageEventListener() {
+ final int perObjectOverhead = OffHeapStoredObject.HEADER_SIZE;
+ final int SMALL_ALLOC_SIZE = 1000;
+ SlabImpl slab = new SlabImpl(3000);
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryUsageListener listener = new MemoryUsageListener() {
+ @Override
+ public void updateMemoryUsed(final long bytesUsed) {
+ MemoryAllocatorJUnitTest.this.memoryUsageEventReceived = true;
+ assertEquals(MemoryAllocatorJUnitTest.this.expectedMemoryUsage, bytesUsed);
+ }
+ };
+ ma.addMemoryUsageListener(listener);
+
+ this.expectedMemoryUsage = SMALL_ALLOC_SIZE;
+ this.memoryUsageEventReceived = false;
+ StoredObject smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
+ assertEquals(true, this.memoryUsageEventReceived);
+
+ this.expectedMemoryUsage = SMALL_ALLOC_SIZE * 2;
+ this.memoryUsageEventReceived = false;
+ smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
+ assertEquals(true, this.memoryUsageEventReceived);
+
+ MemoryUsageListener unaddedListener = new MemoryUsageListener() {
+ @Override
+ public void updateMemoryUsed(final long bytesUsed) {
+ throw new IllegalStateException("Should never be called");
+ }
+ };
+ ma.removeMemoryUsageListener(unaddedListener);
+
+ ma.removeMemoryUsageListener(listener);
+
+ ma.removeMemoryUsageListener(unaddedListener);
+
+ this.expectedMemoryUsage = SMALL_ALLOC_SIZE * 2;
+ this.memoryUsageEventReceived = false;
+ smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
+ assertEquals(false, this.memoryUsageEventReceived);
+
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+ private void checkMcs(ArrayList<StoredObject> mcs) {
+ for (StoredObject mc: mcs) {
+ assertEquals(8+8, mc.getSize());
+ }
+ }
+
+ @Test
+ public void testOutOfOffHeapMemory() {
+ final int perObjectOverhead = OffHeapStoredObject.HEADER_SIZE;
+ final int BIG_ALLOC_SIZE = 150000;
+ final int SMALL_ALLOC_SIZE = BIG_ALLOC_SIZE/2;
+ final int TOTAL_MEM = BIG_ALLOC_SIZE;
+ final SlabImpl slab = new SlabImpl(TOTAL_MEM);
+ final AtomicReference<OutOfOffHeapMemoryException> ooom = new AtomicReference<OutOfOffHeapMemoryException>();
+ final OutOfOffHeapMemoryListener oooml = new OutOfOffHeapMemoryListener() {
+ @Override
+ public void outOfOffHeapMemory(OutOfOffHeapMemoryException cause) {
+ ooom.set(cause);
+ }
+ @Override
+ public void close() {
+ }
+ };
+ try {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(oooml, new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ // make a big allocation
+ StoredObject bmc = ma.allocate(BIG_ALLOC_SIZE-perObjectOverhead);
+ assertNull(ooom.get());
+ // drive the ma to ooom with small allocations
+ try {
+ StoredObject smc = ma.allocate(SMALL_ALLOC_SIZE-perObjectOverhead);
+ fail("Expected out of memory");
+ } catch (OutOfOffHeapMemoryException expected) {
+ }
+ assertNotNull(ooom.get());
+ assertTrue(ooom.get().getMessage().contains("Out of off-heap memory. Could not allocate size of "));
+ } finally {
+ MemoryAllocatorImpl.freeOffHeapMemory();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java
index e1c3f4e..f0563ad 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java
@@ -46,7 +46,7 @@ import com.gemstone.gemfire.test.junit.categories.UnitTest;
@Category(UnitTest.class)
public class MemoryBlockNodeJUnitTest {
- private SimpleMemoryAllocatorImpl ma;
+ private MemoryAllocatorImpl ma;
private OutOfOffHeapMemoryListener ooohml;
private OffHeapMemoryStats stats;
private Slab[] slabs = {
@@ -82,12 +82,12 @@ public class MemoryBlockNodeJUnitTest {
public void setUp() {
ooohml = mock(OutOfOffHeapMemoryListener.class);
stats = mock(OffHeapMemoryStats.class);
- ma = (SimpleMemoryAllocatorImpl) SimpleMemoryAllocatorImpl.createForUnitTest(ooohml, stats, slabs);
+ ma = (MemoryAllocatorImpl) MemoryAllocatorImpl.createForUnitTest(ooohml, stats, slabs);
}
@After
public void tearDown() {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
protected Object getValue() {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java
index fa4e776..95b6869 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java
@@ -47,7 +47,7 @@ public class OffHeapHelperJUnitTest extends AbstractStoredObjectTestBase {
OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
OffHeapMemoryStats stats = mock(OffHeapMemoryStats.class);
- ma = SimpleMemoryAllocatorImpl.create(ooohml, stats, 3, OffHeapStorage.MIN_SLAB_SIZE * 3, OffHeapStorage.MIN_SLAB_SIZE);
+ ma = MemoryAllocatorImpl.create(ooohml, stats, 3, OffHeapStorage.MIN_SLAB_SIZE * 3, OffHeapStorage.MIN_SLAB_SIZE);
}
@@ -71,7 +71,7 @@ public class OffHeapHelperJUnitTest extends AbstractStoredObjectTestBase {
@After
public void tearDown() {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapIndexJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapIndexJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapIndexJUnitTest.java
index ec494e8..571060b 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapIndexJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapIndexJUnitTest.java
@@ -57,7 +57,7 @@ public class OffHeapIndexJUnitTest {
@After
public void tearDown() {
this.gfc.close();
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
// TODO cleanup default disk store files
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java
index ae1b35d..fb1aa41 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java
@@ -75,7 +75,7 @@ public abstract class OffHeapRegionBase {
private void closeCache(GemFireCacheImpl gfc, boolean keepOffHeapAllocated) {
gfc.close();
if (!keepOffHeapAllocated) {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
// TODO cleanup default disk store files
}
@@ -100,7 +100,7 @@ public abstract class OffHeapRegionBase {
assertEquals(0, ma.getUsedMemory());
// do an allocation larger than the slab size
// TODO: currently the compact will product slabs bigger than the max slab size
- // (see the todo comment on compact() in SimpleMemoryAllocator).
+ // (see the todo comment on compact() in FreeListManager).
// So we request 20m here since that it the total size.
try {
ma.allocate(1024*1024*20);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
index cf47a72..5cb6afb 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
@@ -65,12 +65,12 @@ public class OffHeapRegionEntryHelperJUnitTest {
OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
OffHeapMemoryStats stats = mock(OffHeapMemoryStats.class);
- ma = SimpleMemoryAllocatorImpl.create(ooohml, stats, 1, OffHeapStorage.MIN_SLAB_SIZE * 1, OffHeapStorage.MIN_SLAB_SIZE);
+ ma = MemoryAllocatorImpl.create(ooohml, stats, 1, OffHeapStorage.MIN_SLAB_SIZE * 1, OffHeapStorage.MIN_SLAB_SIZE);
}
@After
public void tearDown() {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
private OffHeapStoredObject createChunk(Object value) {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
index d878358..d30d4c4 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java
@@ -152,7 +152,7 @@ public class OffHeapStorageJUnitTest {
StatisticsFactory localStatsFactory = new LocalStatisticsFactory(null);
InternalDistributedSystem ids = mock(InternalDistributedSystem.class);
MemoryAllocator ma = OffHeapStorage.createOffHeapStorage(localStatsFactory, OffHeapStorage.MIN_SLAB_SIZE, ids);
- System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
+ System.setProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
ma.close();
}
@@ -260,11 +260,11 @@ public class OffHeapStorageJUnitTest {
verify(ooohml).outOfOffHeapMemory(ex);
} finally {
- System.setProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
+ System.setProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
try {
ma.close();
} finally {
- System.clearProperty(SimpleMemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
+ System.clearProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStackJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStackJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStackJUnitTest.java
index 884787f..1d19854 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStackJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStackJUnitTest.java
@@ -107,13 +107,13 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackCreatedWithAddressIsNotEmpty() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
OffHeapStoredObjectAddressStack stack = new OffHeapStoredObjectAddressStack(chunk.getAddress());
assertEquals(false, stack.isEmpty());
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
@@ -121,14 +121,14 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackWithChunkIsNotEmpty() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
OffHeapStoredObjectAddressStack stack = new OffHeapStoredObjectAddressStack();
stack.offer(chunk.getAddress());
assertEquals(false, stack.isEmpty());
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
@@ -136,7 +136,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackWithChunkTopEqualsAddress() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
long addr = chunk.getAddress();
@@ -144,7 +144,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
stack.offer(addr);
assertEquals(addr, stack.getTopAddress());
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
@@ -163,7 +163,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackWithChunkClearReturnsAddressAndEmptiesStack() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
long addr = chunk.getAddress();
@@ -173,7 +173,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
assertEquals(addr, clearAddr);
assertEquals(true, stack.isEmpty());
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
@@ -181,7 +181,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackWithChunkPollReturnsAddressAndEmptiesStack() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
long addr = chunk.getAddress();
@@ -191,7 +191,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
assertEquals(addr, pollAddr);
assertEquals(true, stack.isEmpty());
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
@@ -199,7 +199,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackWithChunkTotalSizeIsChunkSize() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
int chunkSize = chunk.getSize();
@@ -208,7 +208,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
stack.offer(addr);
assertEquals(chunkSize, stack.computeTotalSize());
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
@@ -217,7 +217,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackWithChunkLogShowsMsgAndSize() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
int chunkSize = chunk.getSize();
@@ -228,15 +228,15 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
stack.logSizes(lw, "foo");
verify(lw).info("foo"+chunkSize);
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
private class TestableSyncChunkStack extends OffHeapStoredObjectAddressStack {
public boolean doConcurrentMod = true;
public int chunk2Size;
- private SimpleMemoryAllocatorImpl ma;
- TestableSyncChunkStack(SimpleMemoryAllocatorImpl ma) {
+ private MemoryAllocatorImpl ma;
+ TestableSyncChunkStack(MemoryAllocatorImpl ma) {
this.ma = ma;
}
@Override
@@ -253,7 +253,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackWithChunkTotalSizeIsChunkSizeWithConcurrentMod() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
int chunkSize = chunk.getSize();
@@ -263,7 +263,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
long totalSize = stack.computeTotalSize();
assertEquals("chunkSize=" + chunkSize + " chunk2Size=" + stack.chunk2Size, chunkSize + stack.chunk2Size, totalSize);
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
@@ -272,7 +272,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
public void stackWithChunkLogShowsMsgAndSizeWithConcurrentMod() {
SlabImpl slab = new SlabImpl(1024);
try {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{slab});
OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocate(100);
int chunkSize = chunk.getSize();
@@ -284,7 +284,7 @@ public class OffHeapStoredObjectAddressStackJUnitTest {
verify(lw).info("foo"+chunkSize);
verify(lw).info("foo"+stack.chunk2Size);
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java
index 956acb4..85c8d4c 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java
@@ -63,12 +63,12 @@ public class OffHeapStoredObjectJUnitTest extends AbstractStoredObjectTestBase {
OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
OffHeapMemoryStats stats = mock(OffHeapMemoryStats.class);
- ma = SimpleMemoryAllocatorImpl.create(ooohml, stats, 3, OffHeapStorage.MIN_SLAB_SIZE * 3, OffHeapStorage.MIN_SLAB_SIZE);
+ ma = MemoryAllocatorImpl.create(ooohml, stats, 3, OffHeapStorage.MIN_SLAB_SIZE * 3, OffHeapStorage.MIN_SLAB_SIZE);
}
@After
public void tearDown() {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java
index f8a5c8e..aa09449 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java
@@ -184,7 +184,7 @@ public class OffHeapValidationJUnitTest {
MemoryBlock firstBlock = inspector.getFirstBlock();
assertEquals(MemoryBlock.State.UNUSED, firstBlock.getState());
- //System.out.println(((SimpleMemoryAllocatorImpl)inspector).getSnapshot());
+ //System.out.println(((MemoryAllocatorImpl)inspector).getSnapshot());
// sort the ExpectedValues into the same order as the MemberBlocks from inspector
Collections.sort(expected,
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
index 7157eaa..5d79192 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
@@ -33,7 +33,7 @@ import com.gemstone.gemfire.internal.HeapDataOutputStream;
import com.gemstone.gemfire.internal.cache.EntryEventImpl;
import com.gemstone.gemfire.internal.offheap.NullOffHeapMemoryStats;
import com.gemstone.gemfire.internal.offheap.NullOutOfOffHeapMemoryListener;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.internal.offheap.StoredObject;
import com.gemstone.gemfire.internal.offheap.SlabImpl;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
@@ -43,16 +43,16 @@ public class OffHeapWriteObjectAsByteArrayJUnitTest {
@Before
public void setUp() throws Exception {
- SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
+ MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
}
@After
public void tearDown() throws Exception {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
private StoredObject createStoredObject(byte[] bytes, boolean isSerialized, boolean isCompressed) {
- return SimpleMemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, isSerialized, isCompressed);
+ return MemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, isSerialized, isCompressed);
}
private DataInputStream createInput(HeapDataOutputStream hdos) {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OutOfOffHeapMemoryDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OutOfOffHeapMemoryDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OutOfOffHeapMemoryDUnitTest.java
index 25de4ea..9c2e5e8 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OutOfOffHeapMemoryDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OutOfOffHeapMemoryDUnitTest.java
@@ -91,7 +91,7 @@ public class OutOfOffHeapMemoryDUnitTest extends CacheTestCase {
@SuppressWarnings("unused") // invoked by reflection from tearDown2()
private static void cleanup() {
disconnectFromDS();
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
cache.set(null);
system.set(null);
isSmallerVM.set(false);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java
deleted file mode 100644
index 51bc0a2..0000000
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java
+++ /dev/null
@@ -1,246 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.gemstone.gemfire.internal.offheap;
-
-import static org.junit.Assert.*;
-
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Random;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ThreadLocalRandom;
-import java.util.concurrent.TimeUnit;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-import com.gemstone.gemfire.test.junit.categories.IntegrationTest;
-
-/**
- * Tests fill pattern validation for the {@link SimpleMemoryAllocatorImpl}.
- */
-@Category(IntegrationTest.class)
-public class SimpleMemoryAllocatorFillPatternIntegrationTest {
- private static Random random = ThreadLocalRandom.current();
-
- /**
- * Chunk operation types.
- */
- static enum Operation {
- ALLOCATE,
- FREE,
- WRITE;
-
- // Holds all Operation values
- private static Operation[] values = Operation.values();
-
- static Operation randomOperation() {
- return values[random.nextInt(values.length)];
- }
- };
-
- /** Number of worker threads for advanced tests. */
- private static final int WORKER_THREAD_COUNT = 5;
-
- /** Size of single test slab.*/
- private static final int SLAB_SIZE = 1024 * 1024 * 50;
-
- /** Maximum number of bytes a worker thread can allocate during advanced tests. */
- private static final int MAX_WORKER_ALLOCATION_TOTAL_SIZE = SLAB_SIZE / WORKER_THREAD_COUNT / 2;
-
- /** Maximum allocation for a single Chunk. */
- private static final int MAX_WORKER_ALLOCATION_SIZE = 512;
-
- /** Canned data for write operations. */
- private static final byte[] WRITE_BYTES = new String("Some string data.").getBytes();
-
- /** Minimum size for write operations. */
- private static final int MIN_WORKER_ALLOCATION_SIZE = WRITE_BYTES.length;
-
- /** Runtime for worker threads. */
- private static final long RUN_TIME_IN_MILLIS = 1 * 1000 * 5;
-
- /** Chunk size for basic huge allocation test. */
- private static final int HUGE_CHUNK_SIZE = 1024 * 200;
-
- /** Our test victim. */
- private SimpleMemoryAllocatorImpl allocator = null;
-
- /** Our test victim's memory slab. */
- private SlabImpl slab = null;
-
- /**
- * Enables fill validation and creates the test victim.
- */
- @Before
- public void setUp() throws Exception {
- System.setProperty("gemfire.validateOffHeapWithFill", "true");
- this.slab = new SlabImpl(SLAB_SIZE);
- this.allocator = SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{this.slab});
- }
-
- /**
- * Frees off heap memory.
- */
- @After
- public void tearDown() throws Exception {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- System.clearProperty("gemfire.validateOffHeapWithFill");
- }
-
- /**
- * This test hammers a SimpleMemoryAllocatorImpl with multiple threads exercising
- * the fill validation of tiny Chunks for one minute. This, of course, exercises many aspects of
- * the SimpleMemoryAllocatorImpl and its helper classes.
- * @throws Exception
- */
- @Test
- public void testFillPatternAdvancedForTinyAllocations() throws Exception {
- doFillPatternAdvancedTest(new ChunkSizer() {
- @Override
- public int allocationSize() {
- int allocation = random.nextInt(MAX_WORKER_ALLOCATION_SIZE+1);
-
- while(allocation < MIN_WORKER_ALLOCATION_SIZE) {
- allocation = random.nextInt(MAX_WORKER_ALLOCATION_SIZE+1);
- }
- return allocation;
- }
- });
- }
-
- /**
- * This test hammers a SimpleMemoryAllocatorImpl with multiple threads exercising
- * the fill validation of huge Chunks for one minute. This, of course, exercises many aspects of
- * the SimpleMemoryAllocatorImpl and its helper classes.
- * @throws Exception
- */
- @Test
- public void testFillPatternAdvancedForHugeAllocations() throws Exception {
- doFillPatternAdvancedTest(new ChunkSizer() {
- @Override
- public int allocationSize() {
- return HUGE_CHUNK_SIZE;
- }
- });
- }
-
- private interface ChunkSizer {
- int allocationSize();
- }
-
- private void doFillPatternAdvancedTest(final ChunkSizer chunkSizer) throws InterruptedException {
- // Used to manage worker thread completion
- final CountDownLatch latch = new CountDownLatch(WORKER_THREAD_COUNT);
-
- // Use to track any errors the worker threads will encounter
- final List<Throwable> threadErrorList = Collections.synchronizedList(new LinkedList<Throwable>());
-
- /*
- * Start up a number of worker threads. These threads will randomly allocate, free,
- * and write to Chunks.
- */
- for(int i = 0;i < WORKER_THREAD_COUNT;++i) {
- new Thread(new Runnable() {
- // Total allocation in bytes for this thread
- private int totalAllocation = 0;
-
- // List of Chunks allocated by this thread
- private List<OffHeapStoredObject> chunks = new LinkedList<OffHeapStoredObject>();
-
- // Time to end thread execution
- private long endTime = System.currentTimeMillis() + RUN_TIME_IN_MILLIS;
-
- /**
- * Allocates a chunk and adds it to the thread's Chunk list.
- */
- private void allocate() {
- int allocation = chunkSizer.allocationSize();
- OffHeapStoredObject chunk = (OffHeapStoredObject) allocator.allocate(allocation);
-
- // This should always work just after allocation
- chunk.validateFill();
-
- chunks.add(chunk);
- totalAllocation += chunk.getSize();
- }
-
- /**
- * Frees a random chunk from the Chunk list.
- */
- private void free() {
- OffHeapStoredObject chunk = chunks.remove(random.nextInt(chunks.size()));
- totalAllocation -= chunk.getSize();
-
- /*
- * Chunk is filled here but another thread may have already grabbed it so we
- * cannot validate the fill.
- */
- chunk.release();
- }
-
- /**
- * Writes canned data to a random Chunk from the Chunk list.
- */
- private void write() {
- OffHeapStoredObject chunk = chunks.get(random.nextInt(chunks.size()));
- chunk.writeDataBytes(0, WRITE_BYTES);
- }
-
- /**
- * Randomly selects Chunk operations and executes them
- * for a period of time. Collects any error thrown during execution.
- */
- @Override
- public void run() {
- try {
- for(long currentTime = System.currentTimeMillis();currentTime < endTime;currentTime = System.currentTimeMillis()) {
- Operation op = (totalAllocation == 0 ? Operation.ALLOCATE : (totalAllocation >= MAX_WORKER_ALLOCATION_TOTAL_SIZE ? Operation.FREE : Operation.randomOperation()));
- switch(op) {
- case ALLOCATE:
- allocate();
- break;
- case FREE:
- free();
- break;
- case WRITE:
- write();
- break;
- }
- }
- } catch (Throwable t) {
- threadErrorList.add(t);
- } finally {
- latch.countDown();
- }
- }
- }).start();
- }
-
- // Make sure each thread ended cleanly
- assertTrue(latch.await(2, TimeUnit.MINUTES));
-
- // Fail on the first error we find
- if(!threadErrorList.isEmpty()) {
- fail(threadErrorList.get(0).getMessage());
- }
- }
-
-}
[3/3] incubator-geode git commit: renamed SimpleMemoryAllocator to
MemoryAllocator
Posted by ds...@apache.org.
renamed SimpleMemoryAllocator to MemoryAllocator
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/4f84da8c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/4f84da8c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/4f84da8c
Branch: refs/heads/feature/GEODE-1101
Commit: 4f84da8cbc8d38fd49e907dd7c00dae1eef0ccdf
Parents: ce8d087
Author: Darrel Schneider <ds...@pivotal.io>
Authored: Tue Mar 15 11:39:30 2016 -0700
Committer: Darrel Schneider <ds...@pivotal.io>
Committed: Tue Mar 15 11:39:30 2016 -0700
----------------------------------------------------------------------
.../internal/cache/AbstractRegionEntry.java | 22 +-
.../gemfire/internal/cache/UpdateOperation.java | 2 +-
.../tier/sockets/ClientUpdateMessageImpl.java | 2 +-
.../cache/wan/GatewaySenderEventImpl.java | 10 -
.../gemfire/internal/offheap/Fragment.java | 2 +-
.../internal/offheap/FreeListManager.java | 8 +-
.../internal/offheap/LifecycleListener.java | 20 +-
.../internal/offheap/MemoryAllocatorImpl.java | 507 ++++++++++++++++
.../internal/offheap/MemoryBlockNode.java | 4 +-
.../internal/offheap/OffHeapStorage.java | 2 +-
.../internal/offheap/OffHeapStoredObject.java | 26 +-
.../OffHeapStoredObjectAddressStack.java | 4 +-
.../internal/offheap/RefCountChangeInfo.java | 2 +-
.../internal/offheap/ReferenceCountHelper.java | 4 +-
.../offheap/SimpleMemoryAllocatorImpl.java | 507 ----------------
.../gemfire/cache30/MultiVMRegionTestCase.java | 22 +-
.../cache/ClientServerGetAllDUnitTest.java | 4 +-
.../gemfire/internal/cache/OffHeapTestUtil.java | 6 +-
.../cache/OffHeapValueWrapperJUnitTest.java | 8 +-
.../cache/OldValueImporterTestBase.java | 26 +-
.../internal/offheap/FreeListManagerTest.java | 6 +-
.../offheap/LifecycleListenerJUnitTest.java | 36 +-
...moryAllocatorFillPatternIntegrationTest.java | 246 ++++++++
.../MemoryAllocatorFillPatternJUnitTest.java | 183 ++++++
.../offheap/MemoryAllocatorJUnitTest.java | 594 +++++++++++++++++++
.../offheap/MemoryBlockNodeJUnitTest.java | 6 +-
.../offheap/OffHeapHelperJUnitTest.java | 4 +-
.../internal/offheap/OffHeapIndexJUnitTest.java | 2 +-
.../internal/offheap/OffHeapRegionBase.java | 4 +-
.../OffHeapRegionEntryHelperJUnitTest.java | 4 +-
.../offheap/OffHeapStorageJUnitTest.java | 6 +-
...ffHeapStoredObjectAddressStackJUnitTest.java | 40 +-
.../offheap/OffHeapStoredObjectJUnitTest.java | 4 +-
.../offheap/OffHeapValidationJUnitTest.java | 2 +-
.../OffHeapWriteObjectAsByteArrayJUnitTest.java | 8 +-
.../offheap/OutOfOffHeapMemoryDUnitTest.java | 2 +-
...moryAllocatorFillPatternIntegrationTest.java | 246 --------
...mpleMemoryAllocatorFillPatternJUnitTest.java | 183 ------
.../offheap/SimpleMemoryAllocatorJUnitTest.java | 594 -------------------
.../offheap/TinyMemoryBlockJUnitTest.java | 8 +-
.../TxReleasesOffHeapOnCloseJUnitTest.java | 2 +-
.../OffHeapByteBufferByteSourceJUnitTest.java | 4 +-
.../gemfire/pdx/OffHeapByteSourceJUnitTest.java | 8 +-
43 files changed, 1676 insertions(+), 1704 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/AbstractRegionEntry.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/AbstractRegionEntry.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/AbstractRegionEntry.java
index a103e96..90d36d1 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/AbstractRegionEntry.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/AbstractRegionEntry.java
@@ -64,7 +64,7 @@ import com.gemstone.gemfire.internal.logging.log4j.LogMarker;
import com.gemstone.gemfire.internal.offheap.MemoryAllocator;
import com.gemstone.gemfire.internal.offheap.OffHeapHelper;
import com.gemstone.gemfire.internal.offheap.ReferenceCountHelper;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.internal.offheap.StoredObject;
import com.gemstone.gemfire.internal.offheap.annotations.Released;
import com.gemstone.gemfire.internal.offheap.annotations.Retained;
@@ -1084,24 +1084,6 @@ public abstract class AbstractRegionEntry implements RegionEntry,
}
return basicEquals(deserializedObj, cd.getDeserializedForReading());
}
-// boolean result = Arrays.equals((byte[])cdVal, serializedObj);
-// if (!result) {
-// try {
-// Object o1 = BlobHelper.deserializeBlob((byte[])cdVal);
-// Object o2 = BlobHelper.deserializeBlob(serializedObj);
-// SimpleMemoryAllocatorImpl.debugLog("checkCDEquals o1=<" + o1 + "> o2=<" + o2 + ">", false);
-// if (o1.equals(o2)) {
-// SimpleMemoryAllocatorImpl.debugLog("they are equal! a1=<" + Arrays.toString((byte[])cdVal) + "> a2=<" + Arrays.toString(serializedObj) + ">", false);
-// }
-// } catch (IOException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// } catch (ClassNotFoundException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// }
-// }
-// return result;
} else {
// prefer object form
if (obj instanceof CachedDeserializable) {
@@ -1337,7 +1319,7 @@ public abstract class AbstractRegionEntry implements RegionEntry,
byte[] compressedData = compressBytes(r, data);
boolean isCompressed = compressedData != data;
ReferenceCountHelper.setReferenceCountOwner(this);
- MemoryAllocator ma = SimpleMemoryAllocatorImpl.getAllocator(); // fix for bug 47875
+ MemoryAllocator ma = MemoryAllocatorImpl.getAllocator(); // fix for bug 47875
val = ma.allocateAndInitialize(compressedData, isSerialized, isCompressed, data);
ReferenceCountHelper.setReferenceCountOwner(null);
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/UpdateOperation.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/UpdateOperation.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/UpdateOperation.java
index d26f50c..acdec28 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/UpdateOperation.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/UpdateOperation.java
@@ -43,7 +43,7 @@ import com.gemstone.gemfire.internal.cache.EntryEventImpl.SerializedCacheValueIm
import com.gemstone.gemfire.internal.cache.tier.sockets.ClientProxyMembershipID;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
import com.gemstone.gemfire.internal.logging.LogService;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.internal.offheap.StoredObject;
import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
import com.gemstone.gemfire.internal.util.BlobHelper;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/ClientUpdateMessageImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/ClientUpdateMessageImpl.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/ClientUpdateMessageImpl.java
index 97d9a3d..6d1508f 100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/ClientUpdateMessageImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/ClientUpdateMessageImpl.java
@@ -56,7 +56,7 @@ import com.gemstone.gemfire.internal.cache.lru.Sizeable;
import com.gemstone.gemfire.internal.cache.tier.MessageType;
import com.gemstone.gemfire.internal.cache.versions.VersionTag;
import com.gemstone.gemfire.internal.logging.LogService;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
/**
* Class <code>ClientUpdateMessageImpl</code> is a message representing a cache
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/wan/GatewaySenderEventImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/wan/GatewaySenderEventImpl.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/wan/GatewaySenderEventImpl.java
index b64a654..d8922f8 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/wan/GatewaySenderEventImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/wan/GatewaySenderEventImpl.java
@@ -571,16 +571,6 @@ public class GatewaySenderEventImpl implements
}
/**
- * This method is meant for internal use by the SimpleMemoryAllocatorImpl.
- * Others should use getRawValue instead.
- * @return if the result is an off-heap reference then callers must use it before this event is released.
- */
- @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE)
- public Object getValueObject() {
- return this.valueObj;
- }
-
- /**
* Return this event's deserialized value
*
* @return this event's deserialized value
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/Fragment.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/Fragment.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/Fragment.java
index d36a71c..0ea6cf8 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/Fragment.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/Fragment.java
@@ -38,7 +38,7 @@ public class Fragment implements MemoryBlock {
private static AtomicIntegerFieldUpdater<Fragment> freeIdxUpdater = AtomicIntegerFieldUpdater.newUpdater(Fragment.class, "freeIdx");
public Fragment(long addr, int size) {
- SimpleMemoryAllocatorImpl.validateAddress(addr);
+ MemoryAllocatorImpl.validateAddress(addr);
this.baseAddr = addr;
this.size = size;
freeIdxUpdater.set(this, 0);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
index 05010ab..c943a7e 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
@@ -35,7 +35,7 @@ import com.gemstone.gemfire.OutOfOffHeapMemoryException;
import com.gemstone.gemfire.internal.logging.LogService;
/**
- * Manages the free lists for a SimpleMemoryAllocatorImpl
+ * Manages the free lists and slabs for a MemoryAllocator
*/
public class FreeListManager {
static final Logger logger = LogService.getLogger();
@@ -127,9 +127,9 @@ public class FreeListManager {
*/
private final AtomicInteger lastFragmentAllocation = new AtomicInteger(0);
private final CopyOnWriteArrayList<Fragment> fragmentList;
- private final SimpleMemoryAllocatorImpl ma;
+ private final MemoryAllocatorImpl ma;
- public FreeListManager(SimpleMemoryAllocatorImpl ma, final Slab[] slabs) {
+ public FreeListManager(MemoryAllocatorImpl ma, final Slab[] slabs) {
this.ma = ma;
this.slabs = slabs;
long total = 0;
@@ -762,7 +762,7 @@ public class FreeListManager {
private List<MemoryBlock> getTinyFreeBlocks() {
final List<MemoryBlock> value = new ArrayList<MemoryBlock>();
- final SimpleMemoryAllocatorImpl sma = this.ma;
+ final MemoryAllocatorImpl sma = this.ma;
for (int i = 0; i < this.tinyFreeLists.length(); i++) {
if (this.tinyFreeLists.get(i) == null) continue;
long addr = this.tinyFreeLists.get(i).getTopAddress();
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/LifecycleListener.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/LifecycleListener.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/LifecycleListener.java
index 613b12a..4a80057 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/LifecycleListener.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/LifecycleListener.java
@@ -22,23 +22,23 @@ import java.util.concurrent.CopyOnWriteArrayList;
/**
* Used by tests to get notifications about the lifecycle of a
- * SimpleMemoryAllocatorImpl.
+ * MemoryAllocatorImpl.
*
* @author Kirk Lund
*/
public interface LifecycleListener {
/**
- * Callback is invoked after creating a new SimpleMemoryAllocatorImpl.
+ * Callback is invoked after creating a new MemoryAllocatorImpl.
*
* Create occurs during the first initialization of an
* InternalDistributedSystem within the JVM.
*
* @param allocator the instance that has just been created
*/
- public void afterCreate(SimpleMemoryAllocatorImpl allocator);
+ public void afterCreate(MemoryAllocatorImpl allocator);
/**
- * Callback is invoked after reopening an existing SimpleMemoryAllocatorImpl
+ * Callback is invoked after reopening an existing MemoryAllocatorImpl
* for reuse.
*
* Reuse occurs during any intialization of an
@@ -47,30 +47,30 @@ public interface LifecycleListener {
*
* @param allocator the instance that has just been reopened for reuse
*/
- public void afterReuse(SimpleMemoryAllocatorImpl allocator);
+ public void afterReuse(MemoryAllocatorImpl allocator);
/**
- * Callback is invoked before closing the SimpleMemoryAllocatorImpl
+ * Callback is invoked before closing the MemoryAllocatorImpl
*
* Close occurs after the InternalDistributedSystem and DistributionManager
* have completely disconnected.
*
* @param allocator the instance that is about to be closed
*/
- public void beforeClose(SimpleMemoryAllocatorImpl allocator);
+ public void beforeClose(MemoryAllocatorImpl allocator);
- static void invokeBeforeClose(SimpleMemoryAllocatorImpl allocator) {
+ static void invokeBeforeClose(MemoryAllocatorImpl allocator) {
for (Iterator<LifecycleListener> iter = lifecycleListeners.iterator(); iter.hasNext();) {
LifecycleListener listener = iter.next();
listener.beforeClose(allocator);
}
}
- static void invokeAfterReuse(SimpleMemoryAllocatorImpl allocator) {
+ static void invokeAfterReuse(MemoryAllocatorImpl allocator) {
for (Iterator<LifecycleListener> iter = lifecycleListeners.iterator(); iter.hasNext();) {
LifecycleListener listener = iter.next();
listener.afterReuse(allocator);
}
}
- static void invokeAfterCreate(SimpleMemoryAllocatorImpl allocator) {
+ static void invokeAfterCreate(MemoryAllocatorImpl allocator) {
for (Iterator<LifecycleListener> iter = lifecycleListeners.iterator(); iter.hasNext();) {
LifecycleListener listener = iter.next();
listener.afterCreate(allocator);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorImpl.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorImpl.java
new file mode 100644
index 0000000..2050dd4
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryAllocatorImpl.java
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.internal.offheap;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.logging.log4j.Logger;
+
+import com.gemstone.gemfire.cache.CacheClosedException;
+import com.gemstone.gemfire.cache.Region;
+import com.gemstone.gemfire.cache.RegionService;
+import com.gemstone.gemfire.internal.cache.BucketRegion;
+import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
+import com.gemstone.gemfire.internal.cache.LocalRegion;
+import com.gemstone.gemfire.internal.cache.PartitionedRegion;
+import com.gemstone.gemfire.internal.cache.PartitionedRegionDataStore;
+import com.gemstone.gemfire.internal.cache.RegionEntry;
+import com.gemstone.gemfire.internal.logging.LogService;
+import com.gemstone.gemfire.internal.offheap.annotations.OffHeapIdentifier;
+import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
+
+/**
+ * This allocator is somewhat like an Arena allocator.
+ * We start out with an array of multiple large chunks of memory.
+ * We also keep lists of any chunk that have been allocated and freed.
+ * An allocation will always try to find a chunk in a free list that is a close fit to the requested size.
+ * If no close fits exist then it allocates the next slice from the front of one the original large chunks.
+ * If we can not find enough free memory then all the existing free memory is compacted.
+ * If we still do not have enough to make the allocation an exception is thrown.
+ *
+ * @author darrel
+ * @author Kirk Lund
+ * @since 9.0
+ */
+public class MemoryAllocatorImpl implements MemoryAllocator {
+
+ static final Logger logger = LogService.getLogger();
+
+ public static final String FREE_OFF_HEAP_MEMORY_PROPERTY = "gemfire.free-off-heap-memory";
+
+ private volatile OffHeapMemoryStats stats;
+
+ private volatile OutOfOffHeapMemoryListener ooohml;
+
+ OutOfOffHeapMemoryListener getOutOfOffHeapMemoryListener() {
+ return this.ooohml;
+ }
+
+ public final FreeListManager freeList;
+
+ private MemoryInspector memoryInspector;
+
+ private volatile MemoryUsageListener[] memoryUsageListeners = new MemoryUsageListener[0];
+
+ private static MemoryAllocatorImpl singleton = null;
+
+ public static MemoryAllocatorImpl getAllocator() {
+ MemoryAllocatorImpl result = singleton;
+ if (result == null) {
+ throw new CacheClosedException("Off Heap memory allocator does not exist.");
+ }
+ return result;
+ }
+
+ private static final boolean DO_EXPENSIVE_VALIDATION = Boolean.getBoolean("gemfire.OFF_HEAP_DO_EXPENSIVE_VALIDATION");
+
+ public static MemoryAllocator create(OutOfOffHeapMemoryListener ooohml, OffHeapMemoryStats stats, int slabCount,
+ long offHeapMemorySize, long maxSlabSize) {
+ return create(ooohml, stats, slabCount, offHeapMemorySize, maxSlabSize, null,
+ new SlabFactory() {
+ @Override
+ public Slab create(int size) {
+ return new SlabImpl(size);
+ }
+ });
+ }
+
+ private static MemoryAllocatorImpl create(OutOfOffHeapMemoryListener ooohml, OffHeapMemoryStats stats, int slabCount,
+ long offHeapMemorySize, long maxSlabSize, Slab[] slabs,
+ SlabFactory slabFactory) {
+ MemoryAllocatorImpl result = singleton;
+ boolean created = false;
+ try {
+ if (result != null) {
+ result.reuse(ooohml, stats, offHeapMemorySize, slabs);
+ logger.info("Reusing {} bytes of off-heap memory. The maximum size of a single off-heap object is {} bytes.", result.getTotalMemory(), result.freeList.getLargestSlabSize());
+ created = true;
+ LifecycleListener.invokeAfterReuse(result);
+ } else {
+ if (slabs == null) {
+ // allocate memory chunks
+ logger.info("Allocating {} bytes of off-heap memory. The maximum size of a single off-heap object is {} bytes.", offHeapMemorySize, maxSlabSize);
+ slabs = new SlabImpl[slabCount];
+ long uncreatedMemory = offHeapMemorySize;
+ for (int i=0; i < slabCount; i++) {
+ try {
+ if (uncreatedMemory >= maxSlabSize) {
+ slabs[i] = slabFactory.create((int) maxSlabSize);
+ uncreatedMemory -= maxSlabSize;
+ } else {
+ // the last slab can be smaller then maxSlabSize
+ slabs[i] = slabFactory.create((int) uncreatedMemory);
+ }
+ } catch (OutOfMemoryError err) {
+ if (i > 0) {
+ logger.error("Off-heap memory creation failed after successfully allocating {} bytes of off-heap memory.", (i*maxSlabSize));
+ }
+ for (int j=0; j < i; j++) {
+ if (slabs[j] != null) {
+ slabs[j].free();
+ }
+ }
+ throw err;
+ }
+ }
+ }
+
+ result = new MemoryAllocatorImpl(ooohml, stats, slabs);
+ singleton = result;
+ LifecycleListener.invokeAfterCreate(result);
+ created = true;
+ }
+ } finally {
+ if (!created) {
+ if (stats != null) {
+ stats.close();
+ }
+ if (ooohml != null) {
+ ooohml.close();
+ }
+ }
+ }
+ return result;
+ }
+ static MemoryAllocatorImpl createForUnitTest(OutOfOffHeapMemoryListener ooohml, OffHeapMemoryStats stats, int slabCount,
+ long offHeapMemorySize, long maxSlabSize, SlabFactory memChunkFactory) {
+ return create(ooohml, stats, slabCount, offHeapMemorySize, maxSlabSize, null,
+ memChunkFactory);
+ }
+ public static MemoryAllocatorImpl createForUnitTest(OutOfOffHeapMemoryListener oooml, OffHeapMemoryStats stats, Slab[] slabs) {
+ int slabCount = 0;
+ long offHeapMemorySize = 0;
+ long maxSlabSize = 0;
+ if (slabs != null) {
+ slabCount = slabs.length;
+ for (int i=0; i < slabCount; i++) {
+ int slabSize = slabs[i].getSize();
+ offHeapMemorySize += slabSize;
+ if (slabSize > maxSlabSize) {
+ maxSlabSize = slabSize;
+ }
+ }
+ }
+ return create(oooml, stats, slabCount, offHeapMemorySize, maxSlabSize, slabs, null);
+ }
+
+
+ private void reuse(OutOfOffHeapMemoryListener oooml, OffHeapMemoryStats newStats, long offHeapMemorySize, Slab[] slabs) {
+ if (isClosed()) {
+ throw new IllegalStateException("Can not reuse a closed off-heap memory manager.");
+ }
+ if (oooml == null) {
+ throw new IllegalArgumentException("OutOfOffHeapMemoryListener is null");
+ }
+ if (getTotalMemory() != offHeapMemorySize) {
+ logger.warn("Using {} bytes of existing off-heap memory instead of the requested {}.", getTotalMemory(), offHeapMemorySize);
+ }
+ if (!this.freeList.okToReuse(slabs)) {
+ throw new IllegalStateException("attempted to reuse existing off-heap memory even though new off-heap memory was allocated");
+ }
+ this.ooohml = oooml;
+ newStats.initialize(this.stats);
+ this.stats = newStats;
+ }
+
+ private MemoryAllocatorImpl(final OutOfOffHeapMemoryListener oooml, final OffHeapMemoryStats stats, final Slab[] slabs) {
+ if (oooml == null) {
+ throw new IllegalArgumentException("OutOfOffHeapMemoryListener is null");
+ }
+
+ this.ooohml = oooml;
+ this.stats = stats;
+
+ this.stats.setFragments(slabs.length);
+ this.stats.setLargestFragment(slabs[0].getSize());
+
+ this.freeList = new FreeListManager(this, slabs);
+ this.memoryInspector = new MemoryInspectorImpl(this.freeList);
+
+ this.stats.incMaxMemory(this.freeList.getTotalMemory());
+ this.stats.incFreeMemory(this.freeList.getTotalMemory());
+ }
+
+ public List<OffHeapStoredObject> getLostChunks() {
+ List<OffHeapStoredObject> liveChunks = this.freeList.getLiveChunks();
+ List<OffHeapStoredObject> regionChunks = getRegionLiveChunks();
+ Set<OffHeapStoredObject> liveChunksSet = new HashSet<>(liveChunks);
+ Set<OffHeapStoredObject> regionChunksSet = new HashSet<>(regionChunks);
+ liveChunksSet.removeAll(regionChunksSet);
+ return new ArrayList<OffHeapStoredObject>(liveChunksSet);
+ }
+
+ /**
+ * Returns a possibly empty list that contains all the Chunks used by regions.
+ */
+ private List<OffHeapStoredObject> getRegionLiveChunks() {
+ ArrayList<OffHeapStoredObject> result = new ArrayList<OffHeapStoredObject>();
+ RegionService gfc = GemFireCacheImpl.getInstance();
+ if (gfc != null) {
+ Iterator<Region<?,?>> rootIt = gfc.rootRegions().iterator();
+ while (rootIt.hasNext()) {
+ Region<?,?> rr = rootIt.next();
+ getRegionLiveChunks(rr, result);
+ Iterator<Region<?,?>> srIt = rr.subregions(true).iterator();
+ while (srIt.hasNext()) {
+ getRegionLiveChunks(srIt.next(), result);
+ }
+ }
+ }
+ return result;
+ }
+
+ private void getRegionLiveChunks(Region<?,?> r, List<OffHeapStoredObject> result) {
+ if (r.getAttributes().getOffHeap()) {
+
+ if (r instanceof PartitionedRegion) {
+ PartitionedRegionDataStore prs = ((PartitionedRegion) r).getDataStore();
+ if (prs != null) {
+ Set<BucketRegion> brs = prs.getAllLocalBucketRegions();
+ if (brs != null) {
+ for (BucketRegion br : brs) {
+ if (br != null && !br.isDestroyed()) {
+ this.basicGetRegionLiveChunks(br, result);
+ }
+
+ }
+ }
+ }
+ } else {
+ this.basicGetRegionLiveChunks((LocalRegion) r, result);
+ }
+
+ }
+
+ }
+
+ private void basicGetRegionLiveChunks(LocalRegion r, List<OffHeapStoredObject> result) {
+ for (Object key : r.keySet()) {
+ RegionEntry re = ((LocalRegion) r).getRegionEntry(key);
+ if (re != null) {
+ /**
+ * value could be GATEWAY_SENDER_EVENT_IMPL_VALUE or region entry value.
+ */
+ @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE)
+ Object value = re._getValue();
+ if (value instanceof OffHeapStoredObject) {
+ result.add((OffHeapStoredObject) value);
+ }
+ }
+ }
+ }
+
+ private OffHeapStoredObject allocateOffHeapStoredObject(int size) {
+ OffHeapStoredObject result = this.freeList.allocate(size);
+ int resultSize = result.getSize();
+ stats.incObjects(1);
+ stats.incUsedMemory(resultSize);
+ stats.incFreeMemory(-resultSize);
+ notifyListeners();
+ if (ReferenceCountHelper.trackReferenceCounts()) {
+ ReferenceCountHelper.refCountChanged(result.getAddress(), false, 1);
+ }
+ return result;
+ }
+
+ @Override
+ public StoredObject allocate(int size) {
+ //System.out.println("allocating " + size);
+ OffHeapStoredObject result = allocateOffHeapStoredObject(size);
+ //("allocated off heap object of size " + size + " @" + Long.toHexString(result.getMemoryAddress()), true);
+ return result;
+ }
+
+ public static void debugLog(String msg, boolean logStack) {
+ if (logStack) {
+ logger.info(msg, new RuntimeException(msg));
+ } else {
+ logger.info(msg);
+ }
+ }
+
+ @Override
+ public StoredObject allocateAndInitialize(byte[] v, boolean isSerialized, boolean isCompressed) {
+ return allocateAndInitialize(v, isSerialized, isCompressed, null);
+ }
+ @Override
+ public StoredObject allocateAndInitialize(byte[] v, boolean isSerialized, boolean isCompressed, byte[] originalHeapData) {
+ long addr = OffHeapRegionEntryHelper.encodeDataAsAddress(v, isSerialized, isCompressed);
+ if (addr != 0L) {
+ return new TinyStoredObject(addr);
+ }
+ OffHeapStoredObject result = allocateOffHeapStoredObject(v.length);
+ //debugLog("allocated off heap object of size " + v.length + " @" + Long.toHexString(result.getMemoryAddress()), true);
+ //debugLog("allocated off heap object of size " + v.length + " @" + Long.toHexString(result.getMemoryAddress()) + "chunkSize=" + result.getSize() + " isSerialized=" + isSerialized + " v=" + Arrays.toString(v), true);
+ result.setSerializedValue(v);
+ result.setSerialized(isSerialized);
+ result.setCompressed(isCompressed);
+ if (originalHeapData != null) {
+ result = new OffHeapStoredObjectWithHeapForm(result, originalHeapData);
+ }
+ return result;
+ }
+
+ @Override
+ public long getFreeMemory() {
+ return this.freeList.getFreeMemory();
+ }
+
+ @Override
+ public long getUsedMemory() {
+ return this.freeList.getUsedMemory();
+ }
+
+ @Override
+ public long getTotalMemory() {
+ return this.freeList.getTotalMemory();
+ }
+
+ @Override
+ public void close() {
+ try {
+ LifecycleListener.invokeBeforeClose(this);
+ } finally {
+ this.ooohml.close();
+ if (Boolean.getBoolean(FREE_OFF_HEAP_MEMORY_PROPERTY)) {
+ realClose();
+ }
+ }
+ }
+
+ public static void freeOffHeapMemory() {
+ MemoryAllocatorImpl ma = singleton;
+ if (ma != null) {
+ ma.realClose();
+ }
+ }
+
+ private void realClose() {
+ // Removing this memory immediately can lead to a SEGV. See 47885.
+ if (setClosed()) {
+ this.freeList.freeSlabs();
+ this.stats.close();
+ singleton = null;
+ }
+ }
+
+ private final AtomicBoolean closed = new AtomicBoolean();
+ private boolean isClosed() {
+ return this.closed.get();
+ }
+ /**
+ * Returns true if caller is the one who should close; false if some other thread
+ * is already closing.
+ */
+ private boolean setClosed() {
+ return this.closed.compareAndSet(false, true);
+ }
+
+
+ FreeListManager getFreeListManager() {
+ return this.freeList;
+ }
+
+ /**
+ * Return the slabId of the slab that contains the given addr.
+ */
+ int findSlab(long addr) {
+ return this.freeList.findSlab(addr);
+ }
+
+ public OffHeapMemoryStats getStats() {
+ return this.stats;
+ }
+
+ @Override
+ public void addMemoryUsageListener(final MemoryUsageListener listener) {
+ synchronized (this.memoryUsageListeners) {
+ final MemoryUsageListener[] newMemoryUsageListeners = Arrays.copyOf(this.memoryUsageListeners, this.memoryUsageListeners.length + 1);
+ newMemoryUsageListeners[this.memoryUsageListeners.length] = listener;
+ this.memoryUsageListeners = newMemoryUsageListeners;
+ }
+ }
+
+ @Override
+ public void removeMemoryUsageListener(final MemoryUsageListener listener) {
+ synchronized (this.memoryUsageListeners) {
+ int listenerIndex = -1;
+ for (int i = 0; i < this.memoryUsageListeners.length; i++) {
+ if (this.memoryUsageListeners[i] == listener) {
+ listenerIndex = i;
+ break;
+ }
+ }
+
+ if (listenerIndex != -1) {
+ final MemoryUsageListener[] newMemoryUsageListeners = new MemoryUsageListener[this.memoryUsageListeners.length - 1];
+ System.arraycopy(this.memoryUsageListeners, 0, newMemoryUsageListeners, 0, listenerIndex);
+ System.arraycopy(this.memoryUsageListeners, listenerIndex + 1, newMemoryUsageListeners, listenerIndex,
+ this.memoryUsageListeners.length - listenerIndex - 1);
+ this.memoryUsageListeners = newMemoryUsageListeners;
+ }
+ }
+ }
+
+ void notifyListeners() {
+ final MemoryUsageListener[] savedListeners = this.memoryUsageListeners;
+
+ if (savedListeners.length == 0) {
+ return;
+ }
+
+ final long bytesUsed = getUsedMemory();
+ for (int i = 0; i < savedListeners.length; i++) {
+ savedListeners[i].updateMemoryUsed(bytesUsed);
+ }
+ }
+
+ static void validateAddress(long addr) {
+ validateAddressAndSize(addr, -1);
+ }
+
+ static void validateAddressAndSize(long addr, int size) {
+ // if the caller does not have a "size" to provide then use -1
+ if ((addr & 7) != 0) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("address was not 8 byte aligned: 0x").append(Long.toString(addr, 16));
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.singleton;
+ if (ma != null) {
+ sb.append(". Valid addresses must be in one of the following ranges: ");
+ ma.freeList.getSlabDescriptions(sb);
+ }
+ throw new IllegalStateException(sb.toString());
+ }
+ if (addr >= 0 && addr < 1024) {
+ throw new IllegalStateException("addr was smaller than expected 0x" + addr);
+ }
+ validateAddressAndSizeWithinSlab(addr, size, DO_EXPENSIVE_VALIDATION);
+ }
+
+ static void validateAddressAndSizeWithinSlab(long addr, int size, boolean doExpensiveValidation) {
+ if (doExpensiveValidation) {
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.singleton;
+ if (ma != null) {
+ if (!ma.freeList.validateAddressAndSizeWithinSlab(addr, size)) {
+ throw new IllegalStateException(" address 0x" + Long.toString(addr, 16) + " does not address the original slab memory");
+ }
+ }
+ }
+ }
+
+ public synchronized List<MemoryBlock> getOrphans() {
+ List<OffHeapStoredObject> liveChunks = this.freeList.getLiveChunks();
+ List<OffHeapStoredObject> regionChunks = getRegionLiveChunks();
+ liveChunks.removeAll(regionChunks);
+ List<MemoryBlock> orphans = new ArrayList<MemoryBlock>();
+ for (OffHeapStoredObject chunk: liveChunks) {
+ orphans.add(new MemoryBlockNode(this, chunk));
+ }
+ Collections.sort(orphans,
+ new Comparator<MemoryBlock>() {
+ @Override
+ public int compare(MemoryBlock o1, MemoryBlock o2) {
+ return Long.valueOf(o1.getAddress()).compareTo(o2.getAddress());
+ }
+ });
+ //this.memoryBlocks = new WeakReference<List<MemoryBlock>>(orphans);
+ return orphans;
+ }
+
+ @Override
+ public MemoryInspector getMemoryInspector() {
+ return this.memoryInspector;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNode.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNode.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNode.java
index 6e2414f..a72d618 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNode.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNode.java
@@ -26,9 +26,9 @@ import com.gemstone.gemfire.cache.CacheClosedException;
* Basic implementation of MemoryBlock for test validation only.
*/
public class MemoryBlockNode implements MemoryBlock {
- private final SimpleMemoryAllocatorImpl ma;
+ private final MemoryAllocatorImpl ma;
private final MemoryBlock block;
- MemoryBlockNode(SimpleMemoryAllocatorImpl ma, MemoryBlock block) {
+ MemoryBlockNode(MemoryAllocatorImpl ma, MemoryBlock block) {
this.ma = ma;
this.block = block;
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
index 9c6c75a..2bdcfba 100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java
@@ -199,7 +199,7 @@ public class OffHeapStorage implements OffHeapMemoryStats {
final int slabCount = calcSlabCount(maxSlabSize, offHeapMemorySize);
- return SimpleMemoryAllocatorImpl.create(ooohml, stats, slabCount, offHeapMemorySize, maxSlabSize);
+ return MemoryAllocatorImpl.create(ooohml, stats, slabCount, offHeapMemorySize, maxSlabSize);
}
private static final long MAX_SLAB_SIZE = Integer.MAX_VALUE;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java
index 68c9bdd..9861a54 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java
@@ -96,7 +96,7 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
final static byte FILL_BYTE = 0x3c;
protected OffHeapStoredObject(long memoryAddress, int chunkSize) {
- SimpleMemoryAllocatorImpl.validateAddressAndSize(memoryAddress, chunkSize);
+ MemoryAllocatorImpl.validateAddressAndSize(memoryAddress, chunkSize);
this.memoryAddress = memoryAddress;
setSize(chunkSize);
AddressableMemoryManager.writeIntVolatile(getAddress()+REF_COUNT_OFFSET, MAGIC_NUMBER);
@@ -121,7 +121,7 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
* memoryAddress. The off heap header has already been initialized.
*/
protected OffHeapStoredObject(long memoryAddress) {
- SimpleMemoryAllocatorImpl.validateAddress(memoryAddress);
+ MemoryAllocatorImpl.validateAddress(memoryAddress);
this.memoryAddress = memoryAddress;
}
@@ -168,8 +168,8 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
// TODO OFFHEAP: no need to copy to heap. Just get the address of each and compare each byte. No need to call incReads when reading from address.
int i;
// inc it twice since we are reading two different objects
- SimpleMemoryAllocatorImpl.getAllocator().getStats().incReads();
- SimpleMemoryAllocatorImpl.getAllocator().getStats().incReads();
+ MemoryAllocatorImpl.getAllocator().getStats().incReads();
+ MemoryAllocatorImpl.getAllocator().getStats().incReads();
for (i=0; i < mySize-(dataCache1.length-1); i+=dataCache1.length) {
this.readDataBytes(i, dataCache1);
other.readDataBytes(i, dataCache2);
@@ -206,7 +206,7 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
final byte[] dataCache = new byte[1024];
int idx=0;
int i;
- SimpleMemoryAllocatorImpl.getAllocator().getStats().incReads();
+ MemoryAllocatorImpl.getAllocator().getStats().incReads();
for (i=0; i < mySize-(dataCache.length-1); i+=dataCache.length) {
this.readDataBytes(i, dataCache);
for (int j=0; j < dataCache.length; j++) {
@@ -416,7 +416,7 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
byte[] result = new byte[getDataSize()];
readDataBytes(0, result);
//debugLog("reading", true);
- SimpleMemoryAllocatorImpl.getAllocator().getStats().incReads();
+ MemoryAllocatorImpl.getAllocator().getStats().incReads();
return result;
}
protected byte[] getRawBytes() {
@@ -487,19 +487,19 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
}
public static int getSize(long memAddr) {
- SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+ MemoryAllocatorImpl.validateAddress(memAddr);
return AddressableMemoryManager.readInt(memAddr+CHUNK_SIZE_OFFSET);
}
public static void setSize(long memAddr, int size) {
- SimpleMemoryAllocatorImpl.validateAddressAndSize(memAddr, size);
+ MemoryAllocatorImpl.validateAddressAndSize(memAddr, size);
AddressableMemoryManager.writeInt(memAddr+CHUNK_SIZE_OFFSET, size);
}
public static long getNext(long memAddr) {
- SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+ MemoryAllocatorImpl.validateAddress(memAddr);
return AddressableMemoryManager.readLong(memAddr+HEADER_SIZE);
}
public static void setNext(long memAddr, long next) {
- SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+ MemoryAllocatorImpl.validateAddress(memAddr);
AddressableMemoryManager.writeLong(memAddr+HEADER_SIZE, next);
}
@@ -595,7 +595,7 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
}
public static boolean retain(long memAddr) {
- SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+ MemoryAllocatorImpl.validateAddress(memAddr);
int uc;
int rawBits;
int retryCount = 0;
@@ -628,7 +628,7 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
release(memAddr, null);
}
static void release(final long memAddr, FreeListManager freeListManager) {
- SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+ MemoryAllocatorImpl.validateAddress(memAddr);
int newCount;
int rawBits;
boolean returnToAllocator;
@@ -661,7 +661,7 @@ import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
ReferenceCountHelper.freeRefCountInfo(memAddr);
}
if (freeListManager == null) {
- freeListManager = SimpleMemoryAllocatorImpl.getAllocator().getFreeListManager();
+ freeListManager = MemoryAllocatorImpl.getAllocator().getFreeListManager();
}
freeListManager.free(memAddr);
} else {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStack.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStack.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStack.java
index 40d0143..b69d3a6 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStack.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectAddressStack.java
@@ -29,7 +29,7 @@ public class OffHeapStoredObjectAddressStack {
private volatile long topAddr;
public OffHeapStoredObjectAddressStack(long addr) {
- if (addr != 0L) SimpleMemoryAllocatorImpl.validateAddress(addr);
+ if (addr != 0L) MemoryAllocatorImpl.validateAddress(addr);
this.topAddr = addr;
}
public OffHeapStoredObjectAddressStack() {
@@ -40,7 +40,7 @@ public class OffHeapStoredObjectAddressStack {
}
public void offer(long e) {
assert e != 0;
- SimpleMemoryAllocatorImpl.validateAddress(e);
+ MemoryAllocatorImpl.validateAddress(e);
synchronized (this) {
OffHeapStoredObject.setNext(e, this.topAddr);
this.topAddr = e;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/RefCountChangeInfo.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/RefCountChangeInfo.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/RefCountChangeInfo.java
index e3b4b1f..cc67a58 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/RefCountChangeInfo.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/RefCountChangeInfo.java
@@ -23,7 +23,7 @@ import com.gemstone.gemfire.internal.shared.StringPrintWriter;
@SuppressWarnings("serial")
/**
- * Used by SimpleMemoryAllocatorImpl to debug off-heap memory leaks.
+ * Used by MemoryAllocatorImpl to debug off-heap memory leaks.
*/
public class RefCountChangeInfo extends Throwable {
private final String threadName;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ReferenceCountHelper.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ReferenceCountHelper.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ReferenceCountHelper.java
index 778b329..f6696b0 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ReferenceCountHelper.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ReferenceCountHelper.java
@@ -214,7 +214,7 @@ public class ReferenceCountHelper {
}
}
if (list == LOCKED) {
- SimpleMemoryAllocatorImpl.debugLog("refCount " + (decRefCount ? "deced" : "inced") + " after orphan detected for @" + Long.toHexString(address), true);
+ MemoryAllocatorImpl.debugLog("refCount " + (decRefCount ? "deced" : "inced") + " after orphan detected for @" + Long.toHexString(address), true);
return;
}
RefCountChangeInfo info = new RefCountChangeInfo(decRefCount, rc, owner);
@@ -242,7 +242,7 @@ public class ReferenceCountHelper {
if (!trackReferenceCounts()) return;
List<RefCountChangeInfo> freedInfo = stacktraces.remove(address);
if (freedInfo == LOCKED) {
- SimpleMemoryAllocatorImpl.debugLog("freed after orphan detected for @" + Long.toHexString(address), true);
+ MemoryAllocatorImpl.debugLog("freed after orphan detected for @" + Long.toHexString(address), true);
} else if (trackFreedReferenceCounts()) {
if (freedInfo != null) {
freedStacktraces.put(address, freedInfo);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
deleted file mode 100644
index f7fa888..0000000
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
+++ /dev/null
@@ -1,507 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.gemstone.gemfire.internal.offheap;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicBoolean;
-
-import org.apache.logging.log4j.Logger;
-
-import com.gemstone.gemfire.cache.CacheClosedException;
-import com.gemstone.gemfire.cache.Region;
-import com.gemstone.gemfire.cache.RegionService;
-import com.gemstone.gemfire.internal.cache.BucketRegion;
-import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
-import com.gemstone.gemfire.internal.cache.LocalRegion;
-import com.gemstone.gemfire.internal.cache.PartitionedRegion;
-import com.gemstone.gemfire.internal.cache.PartitionedRegionDataStore;
-import com.gemstone.gemfire.internal.cache.RegionEntry;
-import com.gemstone.gemfire.internal.logging.LogService;
-import com.gemstone.gemfire.internal.offheap.annotations.OffHeapIdentifier;
-import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
-
-/**
- * This allocator is somewhat like an Arena allocator.
- * We start out with an array of multiple large chunks of memory.
- * We also keep lists of any chunk that have been allocated and freed.
- * An allocation will always try to find a chunk in a free list that is a close fit to the requested size.
- * If no close fits exist then it allocates the next slice from the front of one the original large chunks.
- * If we can not find enough free memory then all the existing free memory is compacted.
- * If we still do not have enough to make the allocation an exception is thrown.
- *
- * @author darrel
- * @author Kirk Lund
- * @since 9.0
- */
-public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
-
- static final Logger logger = LogService.getLogger();
-
- public static final String FREE_OFF_HEAP_MEMORY_PROPERTY = "gemfire.free-off-heap-memory";
-
- private volatile OffHeapMemoryStats stats;
-
- private volatile OutOfOffHeapMemoryListener ooohml;
-
- OutOfOffHeapMemoryListener getOutOfOffHeapMemoryListener() {
- return this.ooohml;
- }
-
- public final FreeListManager freeList;
-
- private MemoryInspector memoryInspector;
-
- private volatile MemoryUsageListener[] memoryUsageListeners = new MemoryUsageListener[0];
-
- private static SimpleMemoryAllocatorImpl singleton = null;
-
- public static SimpleMemoryAllocatorImpl getAllocator() {
- SimpleMemoryAllocatorImpl result = singleton;
- if (result == null) {
- throw new CacheClosedException("Off Heap memory allocator does not exist.");
- }
- return result;
- }
-
- private static final boolean DO_EXPENSIVE_VALIDATION = Boolean.getBoolean("gemfire.OFF_HEAP_DO_EXPENSIVE_VALIDATION");
-
- public static MemoryAllocator create(OutOfOffHeapMemoryListener ooohml, OffHeapMemoryStats stats, int slabCount,
- long offHeapMemorySize, long maxSlabSize) {
- return create(ooohml, stats, slabCount, offHeapMemorySize, maxSlabSize, null,
- new SlabFactory() {
- @Override
- public Slab create(int size) {
- return new SlabImpl(size);
- }
- });
- }
-
- private static SimpleMemoryAllocatorImpl create(OutOfOffHeapMemoryListener ooohml, OffHeapMemoryStats stats, int slabCount,
- long offHeapMemorySize, long maxSlabSize, Slab[] slabs,
- SlabFactory slabFactory) {
- SimpleMemoryAllocatorImpl result = singleton;
- boolean created = false;
- try {
- if (result != null) {
- result.reuse(ooohml, stats, offHeapMemorySize, slabs);
- logger.info("Reusing {} bytes of off-heap memory. The maximum size of a single off-heap object is {} bytes.", result.getTotalMemory(), result.freeList.getLargestSlabSize());
- created = true;
- LifecycleListener.invokeAfterReuse(result);
- } else {
- if (slabs == null) {
- // allocate memory chunks
- logger.info("Allocating {} bytes of off-heap memory. The maximum size of a single off-heap object is {} bytes.", offHeapMemorySize, maxSlabSize);
- slabs = new SlabImpl[slabCount];
- long uncreatedMemory = offHeapMemorySize;
- for (int i=0; i < slabCount; i++) {
- try {
- if (uncreatedMemory >= maxSlabSize) {
- slabs[i] = slabFactory.create((int) maxSlabSize);
- uncreatedMemory -= maxSlabSize;
- } else {
- // the last slab can be smaller then maxSlabSize
- slabs[i] = slabFactory.create((int) uncreatedMemory);
- }
- } catch (OutOfMemoryError err) {
- if (i > 0) {
- logger.error("Off-heap memory creation failed after successfully allocating {} bytes of off-heap memory.", (i*maxSlabSize));
- }
- for (int j=0; j < i; j++) {
- if (slabs[j] != null) {
- slabs[j].free();
- }
- }
- throw err;
- }
- }
- }
-
- result = new SimpleMemoryAllocatorImpl(ooohml, stats, slabs);
- singleton = result;
- LifecycleListener.invokeAfterCreate(result);
- created = true;
- }
- } finally {
- if (!created) {
- if (stats != null) {
- stats.close();
- }
- if (ooohml != null) {
- ooohml.close();
- }
- }
- }
- return result;
- }
- static SimpleMemoryAllocatorImpl createForUnitTest(OutOfOffHeapMemoryListener ooohml, OffHeapMemoryStats stats, int slabCount,
- long offHeapMemorySize, long maxSlabSize, SlabFactory memChunkFactory) {
- return create(ooohml, stats, slabCount, offHeapMemorySize, maxSlabSize, null,
- memChunkFactory);
- }
- public static SimpleMemoryAllocatorImpl createForUnitTest(OutOfOffHeapMemoryListener oooml, OffHeapMemoryStats stats, Slab[] slabs) {
- int slabCount = 0;
- long offHeapMemorySize = 0;
- long maxSlabSize = 0;
- if (slabs != null) {
- slabCount = slabs.length;
- for (int i=0; i < slabCount; i++) {
- int slabSize = slabs[i].getSize();
- offHeapMemorySize += slabSize;
- if (slabSize > maxSlabSize) {
- maxSlabSize = slabSize;
- }
- }
- }
- return create(oooml, stats, slabCount, offHeapMemorySize, maxSlabSize, slabs, null);
- }
-
-
- private void reuse(OutOfOffHeapMemoryListener oooml, OffHeapMemoryStats newStats, long offHeapMemorySize, Slab[] slabs) {
- if (isClosed()) {
- throw new IllegalStateException("Can not reuse a closed off-heap memory manager.");
- }
- if (oooml == null) {
- throw new IllegalArgumentException("OutOfOffHeapMemoryListener is null");
- }
- if (getTotalMemory() != offHeapMemorySize) {
- logger.warn("Using {} bytes of existing off-heap memory instead of the requested {}.", getTotalMemory(), offHeapMemorySize);
- }
- if (!this.freeList.okToReuse(slabs)) {
- throw new IllegalStateException("attempted to reuse existing off-heap memory even though new off-heap memory was allocated");
- }
- this.ooohml = oooml;
- newStats.initialize(this.stats);
- this.stats = newStats;
- }
-
- private SimpleMemoryAllocatorImpl(final OutOfOffHeapMemoryListener oooml, final OffHeapMemoryStats stats, final Slab[] slabs) {
- if (oooml == null) {
- throw new IllegalArgumentException("OutOfOffHeapMemoryListener is null");
- }
-
- this.ooohml = oooml;
- this.stats = stats;
-
- this.stats.setFragments(slabs.length);
- this.stats.setLargestFragment(slabs[0].getSize());
-
- this.freeList = new FreeListManager(this, slabs);
- this.memoryInspector = new MemoryInspectorImpl(this.freeList);
-
- this.stats.incMaxMemory(this.freeList.getTotalMemory());
- this.stats.incFreeMemory(this.freeList.getTotalMemory());
- }
-
- public List<OffHeapStoredObject> getLostChunks() {
- List<OffHeapStoredObject> liveChunks = this.freeList.getLiveChunks();
- List<OffHeapStoredObject> regionChunks = getRegionLiveChunks();
- Set<OffHeapStoredObject> liveChunksSet = new HashSet<>(liveChunks);
- Set<OffHeapStoredObject> regionChunksSet = new HashSet<>(regionChunks);
- liveChunksSet.removeAll(regionChunksSet);
- return new ArrayList<OffHeapStoredObject>(liveChunksSet);
- }
-
- /**
- * Returns a possibly empty list that contains all the Chunks used by regions.
- */
- private List<OffHeapStoredObject> getRegionLiveChunks() {
- ArrayList<OffHeapStoredObject> result = new ArrayList<OffHeapStoredObject>();
- RegionService gfc = GemFireCacheImpl.getInstance();
- if (gfc != null) {
- Iterator<Region<?,?>> rootIt = gfc.rootRegions().iterator();
- while (rootIt.hasNext()) {
- Region<?,?> rr = rootIt.next();
- getRegionLiveChunks(rr, result);
- Iterator<Region<?,?>> srIt = rr.subregions(true).iterator();
- while (srIt.hasNext()) {
- getRegionLiveChunks(srIt.next(), result);
- }
- }
- }
- return result;
- }
-
- private void getRegionLiveChunks(Region<?,?> r, List<OffHeapStoredObject> result) {
- if (r.getAttributes().getOffHeap()) {
-
- if (r instanceof PartitionedRegion) {
- PartitionedRegionDataStore prs = ((PartitionedRegion) r).getDataStore();
- if (prs != null) {
- Set<BucketRegion> brs = prs.getAllLocalBucketRegions();
- if (brs != null) {
- for (BucketRegion br : brs) {
- if (br != null && !br.isDestroyed()) {
- this.basicGetRegionLiveChunks(br, result);
- }
-
- }
- }
- }
- } else {
- this.basicGetRegionLiveChunks((LocalRegion) r, result);
- }
-
- }
-
- }
-
- private void basicGetRegionLiveChunks(LocalRegion r, List<OffHeapStoredObject> result) {
- for (Object key : r.keySet()) {
- RegionEntry re = ((LocalRegion) r).getRegionEntry(key);
- if (re != null) {
- /**
- * value could be GATEWAY_SENDER_EVENT_IMPL_VALUE or region entry value.
- */
- @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE)
- Object value = re._getValue();
- if (value instanceof OffHeapStoredObject) {
- result.add((OffHeapStoredObject) value);
- }
- }
- }
- }
-
- private OffHeapStoredObject allocateOffHeapStoredObject(int size) {
- OffHeapStoredObject result = this.freeList.allocate(size);
- int resultSize = result.getSize();
- stats.incObjects(1);
- stats.incUsedMemory(resultSize);
- stats.incFreeMemory(-resultSize);
- notifyListeners();
- if (ReferenceCountHelper.trackReferenceCounts()) {
- ReferenceCountHelper.refCountChanged(result.getAddress(), false, 1);
- }
- return result;
- }
-
- @Override
- public StoredObject allocate(int size) {
- //System.out.println("allocating " + size);
- OffHeapStoredObject result = allocateOffHeapStoredObject(size);
- //("allocated off heap object of size " + size + " @" + Long.toHexString(result.getMemoryAddress()), true);
- return result;
- }
-
- public static void debugLog(String msg, boolean logStack) {
- if (logStack) {
- logger.info(msg, new RuntimeException(msg));
- } else {
- logger.info(msg);
- }
- }
-
- @Override
- public StoredObject allocateAndInitialize(byte[] v, boolean isSerialized, boolean isCompressed) {
- return allocateAndInitialize(v, isSerialized, isCompressed, null);
- }
- @Override
- public StoredObject allocateAndInitialize(byte[] v, boolean isSerialized, boolean isCompressed, byte[] originalHeapData) {
- long addr = OffHeapRegionEntryHelper.encodeDataAsAddress(v, isSerialized, isCompressed);
- if (addr != 0L) {
- return new TinyStoredObject(addr);
- }
- OffHeapStoredObject result = allocateOffHeapStoredObject(v.length);
- //debugLog("allocated off heap object of size " + v.length + " @" + Long.toHexString(result.getMemoryAddress()), true);
- //debugLog("allocated off heap object of size " + v.length + " @" + Long.toHexString(result.getMemoryAddress()) + "chunkSize=" + result.getSize() + " isSerialized=" + isSerialized + " v=" + Arrays.toString(v), true);
- result.setSerializedValue(v);
- result.setSerialized(isSerialized);
- result.setCompressed(isCompressed);
- if (originalHeapData != null) {
- result = new OffHeapStoredObjectWithHeapForm(result, originalHeapData);
- }
- return result;
- }
-
- @Override
- public long getFreeMemory() {
- return this.freeList.getFreeMemory();
- }
-
- @Override
- public long getUsedMemory() {
- return this.freeList.getUsedMemory();
- }
-
- @Override
- public long getTotalMemory() {
- return this.freeList.getTotalMemory();
- }
-
- @Override
- public void close() {
- try {
- LifecycleListener.invokeBeforeClose(this);
- } finally {
- this.ooohml.close();
- if (Boolean.getBoolean(FREE_OFF_HEAP_MEMORY_PROPERTY)) {
- realClose();
- }
- }
- }
-
- public static void freeOffHeapMemory() {
- SimpleMemoryAllocatorImpl ma = singleton;
- if (ma != null) {
- ma.realClose();
- }
- }
-
- private void realClose() {
- // Removing this memory immediately can lead to a SEGV. See 47885.
- if (setClosed()) {
- this.freeList.freeSlabs();
- this.stats.close();
- singleton = null;
- }
- }
-
- private final AtomicBoolean closed = new AtomicBoolean();
- private boolean isClosed() {
- return this.closed.get();
- }
- /**
- * Returns true if caller is the one who should close; false if some other thread
- * is already closing.
- */
- private boolean setClosed() {
- return this.closed.compareAndSet(false, true);
- }
-
-
- FreeListManager getFreeListManager() {
- return this.freeList;
- }
-
- /**
- * Return the slabId of the slab that contains the given addr.
- */
- int findSlab(long addr) {
- return this.freeList.findSlab(addr);
- }
-
- public OffHeapMemoryStats getStats() {
- return this.stats;
- }
-
- @Override
- public void addMemoryUsageListener(final MemoryUsageListener listener) {
- synchronized (this.memoryUsageListeners) {
- final MemoryUsageListener[] newMemoryUsageListeners = Arrays.copyOf(this.memoryUsageListeners, this.memoryUsageListeners.length + 1);
- newMemoryUsageListeners[this.memoryUsageListeners.length] = listener;
- this.memoryUsageListeners = newMemoryUsageListeners;
- }
- }
-
- @Override
- public void removeMemoryUsageListener(final MemoryUsageListener listener) {
- synchronized (this.memoryUsageListeners) {
- int listenerIndex = -1;
- for (int i = 0; i < this.memoryUsageListeners.length; i++) {
- if (this.memoryUsageListeners[i] == listener) {
- listenerIndex = i;
- break;
- }
- }
-
- if (listenerIndex != -1) {
- final MemoryUsageListener[] newMemoryUsageListeners = new MemoryUsageListener[this.memoryUsageListeners.length - 1];
- System.arraycopy(this.memoryUsageListeners, 0, newMemoryUsageListeners, 0, listenerIndex);
- System.arraycopy(this.memoryUsageListeners, listenerIndex + 1, newMemoryUsageListeners, listenerIndex,
- this.memoryUsageListeners.length - listenerIndex - 1);
- this.memoryUsageListeners = newMemoryUsageListeners;
- }
- }
- }
-
- void notifyListeners() {
- final MemoryUsageListener[] savedListeners = this.memoryUsageListeners;
-
- if (savedListeners.length == 0) {
- return;
- }
-
- final long bytesUsed = getUsedMemory();
- for (int i = 0; i < savedListeners.length; i++) {
- savedListeners[i].updateMemoryUsed(bytesUsed);
- }
- }
-
- static void validateAddress(long addr) {
- validateAddressAndSize(addr, -1);
- }
-
- static void validateAddressAndSize(long addr, int size) {
- // if the caller does not have a "size" to provide then use -1
- if ((addr & 7) != 0) {
- StringBuilder sb = new StringBuilder();
- sb.append("address was not 8 byte aligned: 0x").append(Long.toString(addr, 16));
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.singleton;
- if (ma != null) {
- sb.append(". Valid addresses must be in one of the following ranges: ");
- ma.freeList.getSlabDescriptions(sb);
- }
- throw new IllegalStateException(sb.toString());
- }
- if (addr >= 0 && addr < 1024) {
- throw new IllegalStateException("addr was smaller than expected 0x" + addr);
- }
- validateAddressAndSizeWithinSlab(addr, size, DO_EXPENSIVE_VALIDATION);
- }
-
- static void validateAddressAndSizeWithinSlab(long addr, int size, boolean doExpensiveValidation) {
- if (doExpensiveValidation) {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.singleton;
- if (ma != null) {
- if (!ma.freeList.validateAddressAndSizeWithinSlab(addr, size)) {
- throw new IllegalStateException(" address 0x" + Long.toString(addr, 16) + " does not address the original slab memory");
- }
- }
- }
- }
-
- public synchronized List<MemoryBlock> getOrphans() {
- List<OffHeapStoredObject> liveChunks = this.freeList.getLiveChunks();
- List<OffHeapStoredObject> regionChunks = getRegionLiveChunks();
- liveChunks.removeAll(regionChunks);
- List<MemoryBlock> orphans = new ArrayList<MemoryBlock>();
- for (OffHeapStoredObject chunk: liveChunks) {
- orphans.add(new MemoryBlockNode(this, chunk));
- }
- Collections.sort(orphans,
- new Comparator<MemoryBlock>() {
- @Override
- public int compare(MemoryBlock o1, MemoryBlock o2) {
- return Long.valueOf(o1.getAddress()).compareTo(o2.getAddress());
- }
- });
- //this.memoryBlocks = new WeakReference<List<MemoryBlock>>(orphans);
- return orphans;
- }
-
- @Override
- public MemoryInspector getMemoryInspector() {
- return this.memoryInspector;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java b/geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java
index dce68cf..8127eea 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/cache30/MultiVMRegionTestCase.java
@@ -110,7 +110,7 @@ import com.gemstone.gemfire.internal.cache.versions.RegionVersionVector;
import com.gemstone.gemfire.internal.cache.versions.VMRegionVersionVector;
import com.gemstone.gemfire.internal.cache.versions.VersionTag;
import com.gemstone.gemfire.internal.logging.LogService;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.internal.offheap.StoredObject;
import com.gemstone.gemfire.test.dunit.AsyncInvocation;
import com.gemstone.gemfire.test.dunit.DistributedTestCase;
@@ -2000,7 +2000,7 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
LocalRegion reRegion;
reRegion = (LocalRegion) region;
RegionEntry re = reRegion.getRegionEntry(key2);
@@ -2066,7 +2066,7 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
@@ -2087,7 +2087,7 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
LocalRegion reRegion;
reRegion = (LocalRegion) region;
@@ -2153,7 +2153,7 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
LocalRegion reRegion;
reRegion = (LocalRegion) region;
@@ -2177,7 +2177,7 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
}
}
@@ -2237,7 +2237,7 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(2, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(2, ma.getStats().getObjects());
}
}
@@ -2257,7 +2257,7 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
@@ -2312,7 +2312,7 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
}
@@ -2331,13 +2331,13 @@ public abstract class MultiVMRegionTestCase extends RegionTestCase {
assertEquals(1, region.size());
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
assertEquals(1, ma.getStats().getObjects());
}
region.destroyRegion(arg);
if (region.getAttributes().getOffHeap() && !(region instanceof PartitionedRegion)) {
GemFireCacheImpl gfc = (GemFireCacheImpl) getCache();
- final SimpleMemoryAllocatorImpl ma = (SimpleMemoryAllocatorImpl) gfc.getOffHeapStore();
+ final MemoryAllocatorImpl ma = (MemoryAllocatorImpl) gfc.getOffHeapStore();
WaitCriterion waitForStatChange = new WaitCriterion() {
public boolean done() {
return ma.getStats().getObjects() == 0;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ClientServerGetAllDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ClientServerGetAllDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ClientServerGetAllDUnitTest.java
index f7f633c..803593e 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ClientServerGetAllDUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ClientServerGetAllDUnitTest.java
@@ -39,7 +39,7 @@ import com.gemstone.gemfire.cache30.ClientServerTestCase;
import com.gemstone.gemfire.distributed.internal.DistributionConfig;
import com.gemstone.gemfire.internal.AvailablePort;
import com.gemstone.gemfire.internal.AvailablePortHelper;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.test.dunit.Assert;
import com.gemstone.gemfire.test.dunit.AsyncInvocation;
import com.gemstone.gemfire.test.dunit.DistributedTestUtils;
@@ -270,7 +270,7 @@ import com.gemstone.gemfire.test.dunit.VM;
server.invoke(new CacheSerializableRunnable("Dump OffHeap Stats") {
@Override
public void run2() throws CacheException {
- SimpleMemoryAllocatorImpl ma = SimpleMemoryAllocatorImpl.getAllocator();
+ MemoryAllocatorImpl ma = MemoryAllocatorImpl.getAllocator();
System.out.println("STATS: objects=" + ma.getStats().getObjects() + " usedMemory=" + ma.getStats().getUsedMemory() + " reads=" + ma.getStats().getReads());
}
});
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapTestUtil.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapTestUtil.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapTestUtil.java
index 8fd6895..f9d2c2a 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapTestUtil.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapTestUtil.java
@@ -25,15 +25,15 @@ import com.gemstone.gemfire.cache.CacheClosedException;
import com.gemstone.gemfire.internal.offheap.MemoryBlock;
import com.gemstone.gemfire.internal.offheap.RefCountChangeInfo;
import com.gemstone.gemfire.internal.offheap.ReferenceCountHelper;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
@SuppressWarnings("deprecation")
public class OffHeapTestUtil {
public static void checkOrphans() { // TODO:KIRK: need to do something special to guarantee proper tearDown
- SimpleMemoryAllocatorImpl allocator = null;
+ MemoryAllocatorImpl allocator = null;
try {
- allocator = SimpleMemoryAllocatorImpl.getAllocator();
+ allocator = MemoryAllocatorImpl.getAllocator();
} catch (CacheClosedException ignore) {
// no off-heap memory so no orphans
return;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapValueWrapperJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapValueWrapperJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapValueWrapperJUnitTest.java
index 0829009..550d133 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapValueWrapperJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OffHeapValueWrapperJUnitTest.java
@@ -31,7 +31,7 @@ import com.gemstone.gemfire.internal.cache.DiskEntry.Helper.OffHeapValueWrapper;
import com.gemstone.gemfire.internal.cache.DiskEntry.Helper.Flushable;
import com.gemstone.gemfire.internal.offheap.NullOffHeapMemoryStats;
import com.gemstone.gemfire.internal.offheap.NullOutOfOffHeapMemoryListener;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.internal.offheap.SlabImpl;
import com.gemstone.gemfire.internal.offheap.StoredObject;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
@@ -40,18 +40,18 @@ import com.gemstone.gemfire.test.junit.categories.UnitTest;
public class OffHeapValueWrapperJUnitTest {
private static OffHeapValueWrapper createChunkValueWrapper(byte[] bytes, boolean isSerialized) {
- StoredObject c = SimpleMemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, isSerialized, false);
+ StoredObject c = MemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, isSerialized, false);
return new OffHeapValueWrapper(c);
}
@Before
public void setUp() throws Exception {
- SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
+ MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
}
@After
public void tearDown() throws Exception {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
@Test
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
index 84d7fc7..6dbe100 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
@@ -30,7 +30,7 @@ import com.gemstone.gemfire.internal.offheap.OffHeapStoredObject;
import com.gemstone.gemfire.internal.offheap.TinyStoredObject;
import com.gemstone.gemfire.internal.offheap.NullOffHeapMemoryStats;
import com.gemstone.gemfire.internal.offheap.NullOutOfOffHeapMemoryListener;
-import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryAllocatorImpl;
import com.gemstone.gemfire.internal.offheap.SlabImpl;
import com.gemstone.gemfire.internal.util.BlobHelper;
@@ -109,8 +109,8 @@ public abstract class OldValueImporterTestBase {
// off-heap DataAsAddress byte array
{
- SimpleMemoryAllocatorImpl sma =
- SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
+ MemoryAllocatorImpl sma =
+ MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
try {
byte[] baValue = new byte[] {1,2};
TinyStoredObject baValueSO = (TinyStoredObject) sma.allocateAndInitialize(baValue, false, false);
@@ -121,13 +121,13 @@ public abstract class OldValueImporterTestBase {
fromData(imsg, bytes);
assertArrayEquals(baValue, (byte[])getOldValueFromImporter(imsg));
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
// off-heap Chunk byte array
{
- SimpleMemoryAllocatorImpl sma =
- SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
+ MemoryAllocatorImpl sma =
+ MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
try {
byte[] baValue = new byte[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
OffHeapStoredObject baValueSO = (OffHeapStoredObject) sma.allocateAndInitialize(baValue, false, false);
@@ -138,13 +138,13 @@ public abstract class OldValueImporterTestBase {
fromData(imsg, bytes);
assertArrayEquals(baValue, (byte[])getOldValueFromImporter(imsg));
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
// off-heap DataAsAddress String
{
- SimpleMemoryAllocatorImpl sma =
- SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
+ MemoryAllocatorImpl sma =
+ MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
try {
String baValue = "12";
byte[] baValueBlob = BlobHelper.serializeToBlob(baValue);
@@ -156,13 +156,13 @@ public abstract class OldValueImporterTestBase {
fromData(imsg, bytes);
assertArrayEquals(baValueBlob, ((VMCachedDeserializable)getOldValueFromImporter(imsg)).getSerializedValue());
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
// off-heap Chunk String
{
- SimpleMemoryAllocatorImpl sma =
- SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
+ MemoryAllocatorImpl sma =
+ MemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
try {
String baValue = "12345678";
byte[] baValueBlob = BlobHelper.serializeToBlob(baValue);
@@ -174,7 +174,7 @@ public abstract class OldValueImporterTestBase {
fromData(imsg, bytes);
assertArrayEquals(baValueBlob, ((VMCachedDeserializable)getOldValueFromImporter(imsg)).getSerializedValue());
} finally {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ MemoryAllocatorImpl.freeOffHeapMemory();
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/4f84da8c/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
index 382bd98..950d90b 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
@@ -43,7 +43,7 @@ public class FreeListManagerTest {
}
private final int DEFAULT_SLAB_SIZE = 1024*1024*5;
- private final SimpleMemoryAllocatorImpl ma = mock(SimpleMemoryAllocatorImpl.class);
+ private final MemoryAllocatorImpl ma = mock(MemoryAllocatorImpl.class);
private final OffHeapMemoryStats stats = mock(OffHeapMemoryStats.class);
private TestableFreeListManager freeListManager;
@@ -68,7 +68,7 @@ public class FreeListManagerTest {
}
}
- private static TestableFreeListManager createFreeListManager(SimpleMemoryAllocatorImpl ma, Slab[] slabs) {
+ private static TestableFreeListManager createFreeListManager(MemoryAllocatorImpl ma, Slab[] slabs) {
return new TestableFreeListManager(ma, slabs);
}
@@ -871,7 +871,7 @@ public class FreeListManagerTest {
}
}
- public TestableFreeListManager(SimpleMemoryAllocatorImpl ma, Slab[] slabs) {
+ public TestableFreeListManager(MemoryAllocatorImpl ma, Slab[] slabs) {
super(ma, slabs);
}