示例#1
0
        [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();
        }
示例#2
0
        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();
        }
示例#3
0
        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();
        }
示例#4
0
        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();
        }
示例#5
0
        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();
        }
示例#6
0
        public void CouldRentAndReturnBuffers2()
        {
            var path  = TestUtils.GetPath();
            var count = 10_000;

            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++)
                {
                    // Rent returns a buffer with zero refcount
                    // Return expects zero refcount
                    // With positive RefCount we do not return,
                    // first we unpin according to app logic, e.g. archive a chunk
                    // and only then return

                    var buffer  = (SharedMemory)pool.Rent(i);
                    var buffer2 = (SharedMemory)pool.Rent(i);

                    // var existing = pool.UnsafeGetBuffer(buffer.BufferRef);

                    // switching to refcount mode, decrement to zero causes return to pool of finalize
                    var h = buffer.Pin();
                    h.Dispose();

                    if (!buffer._isPooled)
                    {
                        Assert.Fail("!buffer._isPooled");
                    }

                    if (!pool.Return(buffer2, true))
                    {
                        Assert.Fail();
                    }
                }
            }

            Benchmark.Dump();

            pool.Dispose();
        }
示例#7
0
        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();
        }