[Test] // TODO review public void CouldRentAndGetExisting() { var path = TestUtils.GetPath(); var count = 100; var pool = new SharedMemoryPool(path, 126, (Wpid)123, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); using (Benchmark.Run("SharedMemoryBuffer acquire/release", count)) { for (int i = 1; i < count; i++) { var buffer = pool.RentMemory(i); ThreadPool.UnsafeQueueUserWorkItem((o) => { if (buffer.ReferenceCount != 0) { Assert.Fail("buffer.Counter.Count != 0"); } // ReSharper disable once AccessToDisposedClosure // ReSharper disable once PossibleNullReferenceException var existing = pool.Buckets[((o as SharedMemory).BufferRef)]; if (buffer.HeaderPointer != existing.Data) { Assert.Fail("buffer.HeaderPointer != existing.Data"); } //if (*(int*)existing.Data != 0) //{ // Assert.Fail($"*(int*)existing.Data {*(int*)existing.Data} != 0"); //} buffer.DisposeFinalize(); }, buffer); //Task.Run(() => //{ // var existing = pool.GetExisting(buffer.BufferRef); // if (existing.BufferRef != buffer.BufferRef) // { // Assert.Fail(); // } //}).Wait(); //if (!pool.Release(buffer)) //{ // Assert.Fail(); //} } } Benchmark.Dump(); Console.WriteLine("Disposing pool"); pool.Dispose(); }
public void CannotDoubleDispose() { var path = TestUtils.GetPath(); var pool = new SharedMemoryPool(path, 10 * 1024, (Wpid)1, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); var memory = pool.RentMemory(32 * 1024); ((IDisposable)memory).Dispose(); Assert.Throws <ObjectDisposedException>(() => { ((IDisposable)memory).Dispose(); }); pool.Dispose(); }
public void CannotDisposeRetained() { var path = TestUtils.GetPath(); var pool = new SharedMemoryPool(path, 10 * 1024, (Wpid)1, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); var memory = pool.RentMemory(32 * 1024); var rm = memory.Retain(); Assert.Throws <InvalidOperationException>(() => { ((IDisposable)memory).Dispose(); }); rm.Dispose(); pool.Dispose(); }
public void CannotRetainDisposed() { var path = TestUtils.GetPath(); var pool = new SharedMemoryPool(path, 10 * 1024, (Wpid)1, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); var memory = pool.RentMemory(32 * 1024); ((IDisposable)memory).Dispose(); Assert.Throws <ObjectDisposedException>(() => { var _ = memory.Retain(); }); Assert.Throws <ObjectDisposedException>(() => { var _ = new RetainedMemory <byte>(memory, 0, memory.Length, false); }); pool.Dispose(); }
public void CouldAcquireAndReleaseBuffers() { var path = TestUtils.GetPath(); #pragma warning disable 219 var count = 1000; #pragma warning restore 219 var pool = new SharedMemoryPool(path, 126, (Wpid)123, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); for (int i = 1100; i < 1000000; i++) { var mem = pool.RentMemory(i); mem.InvariantCheck(); //Assert.GreaterOrEqual(mem.Length, i); //Assert.IsTrue(mem.Length >= i); //Assert.IsTrue(mem.Length >= mem.Length); //Assert.AreEqual(pool.DataSizeToBucketSize(mem.Length), mem.Length); //Assert.IsTrue(pool.BucketSizeToDataSize(mem.Length) == mem.Length); //Assert.IsTrue(BitUtil.IsAligned((long)mem.Pointer, 2048)); if (i % 1000 == 0) { Console.WriteLine($"Requested data: {i}, pool: {mem.LengthPow2}, data: {mem.Length}, data/avail: {(i * 100.0 / mem.Length):0.00}%"); } pool.Return(mem); } //using (Benchmark.Run("SharedMemoryBuffer acquire/release", count)) //{ // for (int i = 0; i < count; i++) // { // var buffer = pool.Acquire(2000); // var h = buffer.Pin(); // h.Dispose(); // var buffer2 = pool.Acquire(2000); // buffer2.Pin(); // buffer2.Unpin(); // } //} // Benchmark.Dump(); // pool.Dispose(); }
public void CouldRetain() { var path = TestUtils.GetPath(); var pool = new SharedMemoryPool(path, 10 * 1024, (Wpid)1, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); var lens = new[] { 15, 16, 17, 127, 128, 129, 1023, 1024, 1025, 4095, 4096, 4097, 8191, 8192, 8193, 31 * 1024, 32 * 1024, 45 * 1024 }; foreach (var len in lens) { var memory = pool.RentMemory(len); Assert.IsTrue(memory.IsPoolable); Assert.IsFalse(memory.IsPooled); Assert.IsFalse(memory.IsRetained); Assert.IsFalse(memory.IsDisposed); Assert.AreEqual(pool, memory.Pool); Assert.AreEqual(memory.Vec.Length, memory.Length); Assert.AreEqual(pool.PoolIdx, memory._poolIdx); Assert.GreaterOrEqual(memory.Length, len, "memory.Length, len"); var pow2Len = BitUtil.IsPowerOfTwo(memory.Length) ? memory.Length : (BitUtil.FindNextPositivePowerOfTwo(memory.Length) / 2); Assert.AreEqual(pow2Len, memory.LengthPow2, "BitUtil.FindNextPositivePowerOfTwo(len) / 2, memory.LengthPow2"); var rm = memory.Retain(0, len); Assert.AreEqual(1, memory.ReferenceCount, "1, memory.ReferenceCount"); Assert.IsTrue(memory.IsRetained); Assert.AreEqual(len, rm.Length, "len, rm.Length"); var rm1 = memory.Retain(len / 2, len / 4); Assert.AreEqual(2, memory.ReferenceCount); Assert.AreEqual(len / 4, rm1.Length); rm.Dispose(); Assert.AreEqual(1, memory.ReferenceCount); rm1.Dispose(); Assert.IsTrue(memory.IsDisposed); } pool.Dispose(); }
public void CouldRentAndReturnBuffers() { var path = TestUtils.GetPath(); var pool = new SharedMemoryPool(path, 126, (Wpid)123, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); for (int i = 1000; i < 1024 * 1024; i++) { var mem = pool.RentMemory(i); mem.InvariantCheck(); Assert.IsTrue(mem.Length >= i); if (i > SharedMemoryPool.Pow2ModeSizeLimit) { Assert.IsTrue(mem.LengthPow2 >= i); Assert.IsTrue(mem.Length - mem.LengthPow2 == 4088); // Assert.AreEqual(pool.DataSizeToBucketSize(mem.Length), mem.LengthPow2); } Assert.IsTrue(mem.Length > mem.LengthPow2); var lenX = (long)mem.PointerPow2 + mem.LengthPow2 - (long)mem.Pointer; Assert.AreEqual(mem.Length, lenX); if (i > 4096) { Assert.IsTrue(BitUtil.IsAligned((long)mem.PointerPow2, 4096)); Assert.IsTrue(BitUtil.IsPowerOfTwo(mem.LengthPow2)); } else { Assert.IsTrue(BitUtil.IsAligned((long)mem.PointerPow2, 2048)); } if (i % 1000 == 0) { Console.WriteLine($"Requested: {i}, RM pool: {mem.LengthPow2}, data: {mem.Length}, pow2/avail: {Math.Round(mem.LengthPow2 * 100.0 / mem.Length, 1)}%"); } pool.Return(mem, false); } }
public void PrintPow2Sizes() { var path = TestUtils.GetPath(); var count = 23; var pool = new SharedMemoryPool(path, 10 * 1024, (Wpid)1, rmMaxBufferLength: SharedMemoryPool.RmMaxPoolBufferLength); using (Benchmark.Run("Rent/Release Buffers", count)) { for (int i = 1; i <= count; i++) { var len = 1 << i; var buffer = pool.RentMemory(len); Console.WriteLine($"{i} - {len} - {buffer.Length} - { Math.Round(100.0 - len * 100.0 / buffer.Length, 2)} - BR: [{buffer.BufferRef}] RefCount: [{buffer.ReferenceCount}]"); pool.Return(buffer, false); } } pool.Dispose(); }