|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hugegraph.core.memory; |
| 19 | + |
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
| 21 | + |
| 22 | +import org.apache.hugegraph.memory.MemoryManager; |
| 23 | +import org.apache.hugegraph.memory.allocator.MemoryAllocator; |
| 24 | +import org.apache.hugegraph.memory.allocator.NettyMemoryAllocator; |
| 25 | +import org.apache.hugegraph.memory.allocator.OnHeapMemoryStrategy; |
| 26 | +import org.apache.hugegraph.util.Bytes; |
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.BeforeClass; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import io.netty.buffer.ByteBuf; |
| 33 | +import io.netty.util.IllegalReferenceCountException; |
| 34 | +import io.netty.util.ReferenceCountUtil; |
| 35 | + |
| 36 | +public class MemoryAllocateTest { |
| 37 | + |
| 38 | + private static MemoryAllocator nettyAllocator; |
| 39 | + private static MemoryAllocator onHeapAllocator; |
| 40 | + private static MemoryManager memoryManager; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setup() { |
| 44 | + MemoryManager.getInstance().getCurrentOffHeapAllocatedMemoryInBytes().set(0); |
| 45 | + MemoryManager.getInstance().getCurrentOnHeapAllocatedMemoryInBytes().set(0); |
| 46 | + } |
| 47 | + |
| 48 | + @BeforeClass |
| 49 | + public static void beforeClass() { |
| 50 | + nettyAllocator = new NettyMemoryAllocator(MemoryManager.getInstance()); |
| 51 | + onHeapAllocator = new OnHeapMemoryStrategy(MemoryManager.getInstance()); |
| 52 | + memoryManager = MemoryManager.getInstance(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testNettyAllocate() { |
| 57 | + ByteBuf memoryBlock1 = (ByteBuf) nettyAllocator.tryToAllocate(Bytes.KB); |
| 58 | + ByteBuf memoryBlock2 = (ByteBuf) nettyAllocator.tryToAllocate(Bytes.MB); |
| 59 | + Assert.assertNotNull(memoryBlock1); |
| 60 | + Assert.assertEquals(Bytes.KB, memoryBlock1.capacity()); |
| 61 | + Assert.assertNotNull(memoryBlock2); |
| 62 | + Assert.assertEquals(Bytes.MB, memoryBlock2.capacity()); |
| 63 | + Assert.assertEquals(Bytes.KB + Bytes.MB, |
| 64 | + memoryManager.getCurrentOffHeapAllocatedMemoryInBytes().get()); |
| 65 | + ByteBuf memoryBlock3 = (ByteBuf) nettyAllocator.tryToAllocate(Bytes.GB); |
| 66 | + Assert.assertNull(memoryBlock3); |
| 67 | + memoryBlock3 = (ByteBuf) nettyAllocator.forceAllocate(Bytes.GB); |
| 68 | + Assert.assertNotNull(memoryBlock3); |
| 69 | + Assert.assertEquals(Bytes.GB, memoryBlock3.capacity()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testNettyDeallocate() { |
| 74 | + ByteBuf buf = (ByteBuf) nettyAllocator.tryToAllocate(Bytes.KB); |
| 75 | + Assert.assertNotNull(buf); |
| 76 | + Assert.assertTrue(buf.isWritable()); |
| 77 | + Assert.assertEquals(buf.capacity(), |
| 78 | + memoryManager.getCurrentOffHeapAllocatedMemoryInBytes().get()); |
| 79 | + Assert.assertEquals(1, ReferenceCountUtil.refCnt(buf)); |
| 80 | + nettyAllocator.releaseMemoryBlock(buf); |
| 81 | + Assert.assertThrows(IllegalReferenceCountException.class, buf::memoryAddress); |
| 82 | + Assert.assertEquals(0, ReferenceCountUtil.refCnt(buf)); |
| 83 | + nettyAllocator.returnMemoryToManager(buf.capacity()); |
| 84 | + Assert.assertEquals(0, |
| 85 | + memoryManager.getCurrentOffHeapAllocatedMemoryInBytes().get()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testOnHeapAllocate() { |
| 90 | + AtomicReference<byte[]> memoryBlock1 = |
| 91 | + (AtomicReference<byte[]>) onHeapAllocator.tryToAllocate(Bytes.KB); |
| 92 | + AtomicReference<byte[]> memoryBlock2 = |
| 93 | + (AtomicReference<byte[]>) onHeapAllocator.tryToAllocate(Bytes.MB); |
| 94 | + Assert.assertNotNull(memoryBlock1); |
| 95 | + Assert.assertEquals(Bytes.KB, memoryBlock1.get().length); |
| 96 | + Assert.assertNotNull(memoryBlock2); |
| 97 | + Assert.assertEquals(Bytes.MB, memoryBlock2.get().length); |
| 98 | + Assert.assertEquals(Bytes.KB + Bytes.MB, |
| 99 | + memoryManager.getCurrentOnHeapAllocatedMemoryInBytes().get()); |
| 100 | + AtomicReference<byte[]> memoryBlock3 = |
| 101 | + (AtomicReference<byte[]>) onHeapAllocator.tryToAllocate(Bytes.GB); |
| 102 | + Assert.assertNull(memoryBlock3); |
| 103 | + memoryBlock3 = (AtomicReference<byte[]>) onHeapAllocator.forceAllocate(Bytes.GB); |
| 104 | + Assert.assertNotNull(memoryBlock3); |
| 105 | + Assert.assertEquals(Bytes.GB, memoryBlock3.get().length); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testOnHeapDeallocate() { |
| 110 | + AtomicReference<byte[]> buf = |
| 111 | + (AtomicReference<byte[]>) onHeapAllocator.tryToAllocate(Bytes.KB); |
| 112 | + Assert.assertNotNull(buf); |
| 113 | + Assert.assertEquals(buf.get().length, |
| 114 | + memoryManager.getCurrentOnHeapAllocatedMemoryInBytes().get()); |
| 115 | + onHeapAllocator.returnMemoryToManager(buf.get().length); |
| 116 | + Assert.assertEquals(0, |
| 117 | + memoryManager.getCurrentOnHeapAllocatedMemoryInBytes().get()); |
| 118 | + onHeapAllocator.releaseMemoryBlock(buf); |
| 119 | + Assert.assertNull(buf.get()); |
| 120 | + } |
| 121 | +} |
0 commit comments