コード例 #1
0
 public override ByteBuffer Allocate(int bufferSize)
 {
     lock (this)
     {
         AssertOpen();
         try
         {
             long address = UnsafeUtil.allocateMemory(bufferSize, _memoryAllocationTracker);
             try
             {
                 ByteBuffer buffer = UnsafeUtil.newDirectByteBuffer(address, bufferSize);
                 UnsafeUtil.initDirectByteBuffer(buffer, address, bufferSize);
                 _allocations.Add(new Allocation(address, bufferSize));
                 return(buffer);
             }
             catch (Exception)
             {
                 // What ever went wrong we can safely fall back to on-heap buffer. Free the allocated memory right away first.
                 UnsafeUtil.free(address, bufferSize, _memoryAllocationTracker);
                 return(AllocateHeapBuffer(bufferSize));
             }
         }
         catch (NativeMemoryAllocationRefusedError)
         {
             // What ever went wrong we can safely fall back to on-heap buffer.
             return(AllocateHeapBuffer(bufferSize));
         }
     }
 }
コード例 #2
0
        private short GetShortBigEndian(long p)
        {
            short a = ( short )(UnsafeUtil.getByte(p) & 0xFF);
            short b = ( short )(UnsafeUtil.getByte(p + 1) & 0xFF);

            return(( short )((a << 8) | b));
        }
コード例 #3
0
 private void PutBytes(long page, sbyte[] data, int srcOffset, int tgtOffset, int length)
 {
     for (int i = 0; i < length; i++)
     {
         UnsafeUtil.putByte(page + srcOffset + i, data[tgtOffset + i]);
     }
 }
コード例 #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private int swapIn(org.neo4j.io.fs.StoreChannel channel, long bufferAddress, int bufferSize, long fileOffset, int filePageSize) throws java.io.IOException
        private int SwapIn(StoreChannel channel, long bufferAddress, int bufferSize, long fileOffset, int filePageSize)
        {
            int readTotal = 0;

            try
            {
                ByteBuffer bufferProxy = Proxy(bufferAddress, filePageSize);
                int        read;
                do
                {
                    read = channel.Read(bufferProxy, fileOffset + readTotal);
                } while (read != -1 && (readTotal += read) < filePageSize);

                // Zero-fill the rest.
                Debug.Assert(readTotal >= 0 && filePageSize <= bufferSize && readTotal <= filePageSize, format("pointer = %h, readTotal = %s, length = %s, page size = %s", bufferAddress, readTotal, filePageSize, bufferSize));
                UnsafeUtil.setMemory(bufferAddress + readTotal, filePageSize - readTotal, MuninnPageCache.ZERO_BYTE);
                return(readTotal);
            }
            catch (IOException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                string msg = format("Read failed after %s of %s bytes from fileOffset %s", readTotal, filePageSize, fileOffset);
                throw new IOException(msg, e);
            }
        }
コード例 #5
0
        public override void PutByte(sbyte value)
        {
            long p = NextBoundedPointer(_sizeOfByte);

            UnsafeUtil.putByte(p, value);
            _offset++;
        }
コード例 #6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private long readPositionedVectoredToFileChannel(long startFilePageId, long[] bufferAddresses, int arrayOffset, int length) throws Exception
        private long ReadPositionedVectoredToFileChannel(long startFilePageId, long[] bufferAddresses, int arrayOffset, int length)
        {
            long        fileOffset = PageIdToPosition(startFilePageId);
            FileChannel channel    = UnwrappedChannel(startFilePageId);

            ByteBuffer[] srcs      = ConvertToByteBuffers(bufferAddresses, arrayOffset, length);
            long         bytesRead = LockPositionReadVectorAndRetryIfInterrupted(startFilePageId, channel, fileOffset, srcs, MAX_INTERRUPTED_CHANNEL_REOPEN_ATTEMPTS);

            if (bytesRead == -1)
            {
                foreach (long address in bufferAddresses)
                {
                    UnsafeUtil.setMemory(address, _filePageSize, MuninnPageCache.ZERO_BYTE);
                }
                return(0);
            }
            else if (bytesRead < (( long )_filePageSize) * length)
            {
                int pagesRead = ( int )(bytesRead / _filePageSize);
                int bytesReadIntoLastReadPage = ( int )(bytesRead % _filePageSize);
                int pagesNeedingZeroing       = length - pagesRead;
                for (int i = 0; i < pagesNeedingZeroing; i++)
                {
                    long address     = bufferAddresses[arrayOffset + pagesRead + i];
                    long bytesToZero = _filePageSize;
                    if (i == 0)
                    {
                        address     += bytesReadIntoLastReadPage;
                        bytesToZero -= bytesReadIntoLastReadPage;
                    }
                    UnsafeUtil.setMemory(address, bytesToZero, MuninnPageCache.ZERO_BYTE);
                }
            }
            return(bytesRead);
        }
コード例 #7
0
            internal virtual void ProcessorDone(long time)
            {
                UnsafeUtil.getAndAddLong(this, outerInstance.PROCESSING_TIME_OFFSET, time);
                int prevCompletedProcessors = UnsafeUtil.getAndAddInt(this, outerInstance.COMPLETED_PROCESSORS_OFFSET, 1);

                Debug.Assert(prevCompletedProcessors < Processors, prevCompletedProcessors + " vs " + Processors + " for " + Ticket);
            }
コード例 #8
0
 private void AbortPageFault(Exception throwable, int[] chunk, long chunkOffset, LatchMap.Latch latch, PageFaultEvent faultEvent)
 {
     UnsafeUtil.putIntVolatile(chunk, chunkOffset, UNMAPPED_TTE);
     latch.Release();
     faultEvent.Done(throwable);
     PinEvent.done();
 }
コード例 #9
0
 private void PutIntBigEndian(int value, long p)
 {
     UnsafeUtil.putByte(p, ( sbyte )(value >> 24));
     UnsafeUtil.putByte(p + 1, ( sbyte )(value >> 16));
     UnsafeUtil.putByte(p + 2, ( sbyte )(value >> 8));
     UnsafeUtil.putByte(p + 3, ( sbyte )value);
 }
コード例 #10
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private boolean uncommonPin(long filePageId, long chunkOffset, int[] chunk) throws java.io.IOException
        private bool UncommonPin(long filePageId, long chunkOffset, int[] chunk)
        {
            if (NoFault)
            {
                // The only page state that needs to be cleared is the currentPageId, since it was set prior to pin.
                CurrentPageIdConflict = UNBOUND_PAGE_ID;
                return(true);
            }
            // Looks like there's no mapping, so we'd like to do a page fault.
            LatchMap.Latch latch = PagedFile.pageFaultLatches.takeOrAwaitLatch(filePageId);
            if (latch != null)
            {
                // We managed to inject our latch, so we now own the right to perform the page fault. We also
                // have a duty to eventually release and remove the latch, no matter what happens now.
                // However, we first have to double-check that a page fault did not complete in-between our initial
                // check in the translation table, and us getting a latch.
                if (UnsafeUtil.getIntVolatile(chunk, chunkOffset) == UNMAPPED_TTE)
                {
                    // Sweet, we didn't race with any other fault on this translation table entry.
                    long pageRef = PageFault(filePageId, Swapper, chunkOffset, chunk, latch);
                    PinCursorToPage(pageRef, filePageId, Swapper);
                    return(true);
                }
                // Oops, looks like we raced with another page fault on this file page.
                // Let's release our latch and retry the pin.
                latch.Release();
            }
            // We found a latch, so someone else is already doing a page fault for this page.
            // The `takeOrAwaitLatch` already waited for this latch to be released on our behalf,
            // so now we just have to do another iteration of the loop to see what's in the translation table now.
            return(false);
        }
コード例 #11
0
        public override void RemoveHopBit(int index, int hd)
        {
            long adr     = HopBitsAddress(index);
            int  hopBits = UnsafeUtil.getInt(adr);

            hopBits |= 1 << hd;
            UnsafeUtil.putInt(adr, hopBits);
        }
コード例 #12
0
ファイル: GrabAllocator.cs プロジェクト: Neo4Net/Neo4Net
 internal Grab(Grab next, long size, MemoryAllocationTracker memoryTracker)
 {
     this.NextConflict  = next;
     this.Address       = UnsafeUtil.allocateMemory(size, memoryTracker);
     this.Limit         = Address + size;
     this.MemoryTracker = memoryTracker;
     NextPointer        = Address;
 }
コード例 #13
0
        public override void MoveHopBit(int index, int hd, int delta)
        {
            long adr     = HopBitsAddress(index);
            int  hopBits = UnsafeUtil.getInt(adr);

            hopBits ^= (1 << hd) | (1 << (hd + delta));
            UnsafeUtil.putInt(adr, hopBits);
        }
コード例 #14
0
        public override void PutHopBit(int index, int hd)
        {
            long adr     = HopBitsAddress(index);
            int  hopBits = UnsafeUtil.getInt(adr);

            hopBits &= ~(1 << hd);
            UnsafeUtil.putInt(adr, hopBits);
        }
コード例 #15
0
ファイル: PageList.cs プロジェクト: Neo4Net/Neo4Net
        internal virtual bool IsBoundTo(long pageRef, int swapperId, long filePageId)
        {
            long address         = OffPageBinding(pageRef);
            long expectedBinding = (filePageId << _shiftPartialFilePageId) + swapperId;
            long actualBinding   = ( long )(( ulong )UnsafeUtil.getLong(address) >> SHIFT_SWAPPER_ID);

            return(expectedBinding == actualBinding);
        }
コード例 #16
0
ファイル: PageList.cs プロジェクト: Neo4Net/Neo4Net
        private void SetSwapperId(long pageRef, int swapperId)
        {
            swapperId = swapperId << SHIFT_SWAPPER_ID;
            long address = OffPageBinding(pageRef);
            long v       = UnsafeUtil.getLong(address) & _maskNotSwapperId;

            UnsafeUtil.putLong(address, v + swapperId);
        }
コード例 #17
0
ファイル: PageList.cs プロジェクト: Neo4Net/Neo4Net
 internal virtual void InitBuffer(long pageRef)
 {
     if (GetAddress(pageRef) == 0L)
     {
         long addr = _memoryAllocator.allocateAligned(CachePageSize, _bufferAlignment);
         UnsafeUtil.putLong(OffAddress(pageRef), addr);
     }
 }
コード例 #18
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @After public void teardown()
        public virtual void Teardown()
        {
            UnsafeUtil.exchangeNativeAccessCheckEnabled(_prevAccessCheck);
            if (_populator != null)
            {
                _populator.close(true);
            }
        }
コード例 #19
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Before public void setup() throws java.io.IOException, org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void Setup()
        {
            _indexProvider = ProviderCreator.apply(this);
            Rules.fileSystem().mkdirs(_indexProvider.directoryStructure().rootDirectory());
            _populator = _indexProvider.getPopulator(Descriptor, _samplingConfig, heapBufferFactory(1024));
            when(_nodePropertyAccessor.getNodePropertyValue(anyLong(), anyInt())).thenThrow(typeof(System.NotSupportedException));
            _prevAccessCheck = UnsafeUtil.exchangeNativeAccessCheckEnabled(false);
        }
コード例 #20
0
 private int GetIntAt(long p)
 {
     if (UnsafeUtil.allowUnalignedMemoryAccess)
     {
         int x = UnsafeUtil.getInt(p);
         return(UnsafeUtil.storeByteOrderIsNative ? x : Integer.reverseBytes(x));
     }
     return(GetIntBigEndian(p));
 }
コード例 #21
0
        private void IncreaseFileSizeTo(long newFileSize)
        {
            long currentFileSize;

            do
            {
                currentFileSize = CurrentFileSize;
            } while (currentFileSize < newFileSize && !UnsafeUtil.compareAndSwapLong(this, _fileSizeOffset, currentFileSize, newFileSize));
        }
コード例 #22
0
        private int GetIntBigEndian(long p)
        {
            int a = UnsafeUtil.getByte(p) & 0xFF;
            int b = UnsafeUtil.getByte(p + 1) & 0xFF;
            int c = UnsafeUtil.getByte(p + 2) & 0xFF;
            int d = UnsafeUtil.getByte(p + 3) & 0xFF;

            return((a << 24) | (b << 16) | (c << 8) | d);
        }
コード例 #23
0
 private short GetShortAt(long p)
 {
     if (UnsafeUtil.allowUnalignedMemoryAccess)
     {
         short x = UnsafeUtil.getShort(p);
         return(UnsafeUtil.storeByteOrderIsNative ? x : Short.reverseBytes(x));
     }
     return(GetShortBigEndian(p));
 }
コード例 #24
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private long pageFault(long filePageId, org.neo4j.io.pagecache.PageSwapper swapper, long chunkOffset, int[] chunk, LatchMap.Latch latch) throws java.io.IOException
        private long PageFault(long filePageId, PageSwapper swapper, long chunkOffset, int[] chunk, LatchMap.Latch latch)
        {
            // We are page faulting. This is a critical time, because we currently have the given latch in the chunk array
            // slot that we are faulting into. We MUST make sure to release that latch, and remove it from the chunk, no
            // matter what happens. Otherwise other threads will get stuck waiting forever for our page fault to finish.
            // If we manage to get a free page to fault into, then we will also be taking a write lock on that page, to
            // protect it against concurrent eviction as we assigning a binding to the page. If anything goes wrong, then
            // we must make sure to release that write lock as well.
            PageFaultEvent faultEvent = PinEvent.beginPageFault();
            long           pageRef;

            try
            {
                // The grabFreePage method might throw.
                pageRef = PagedFile.grabFreeAndExclusivelyLockedPage(faultEvent);

                // We got a free page, and we know that we have race-free access to it. Well, it's not entirely race
                // free, because other paged files might have it in their translation tables (or rather, their reads of
                // their translation tables might race with eviction) and try to pin it.
                // However, they will all fail because when they try to pin, because the page will be exclusively locked
                // and possibly bound to our page.
            }
            catch (Exception throwable)
            {
                // Make sure to unstuck the page fault latch.
                AbortPageFault(throwable, chunk, chunkOffset, latch, faultEvent);
                throw throwable;
            }
            try
            {
                // Check if we're racing with unmapping. We have the page lock
                // here, so the unmapping would have already happened. We do this
                // check before page.fault(), because that would otherwise reopen
                // the file channel.
                AssertPagedFileStillMappedAndGetIdOfLastPage();
                PagedFile.initBuffer(pageRef);
                PagedFile.fault(pageRef, swapper, PagedFile.swapperId, filePageId, faultEvent);
            }
            catch (Exception throwable)
            {
                // Make sure to unlock the page, so the eviction thread can pick up our trash.
                PagedFile.unlockExclusive(pageRef);
                // Make sure to unstuck the page fault latch.
                AbortPageFault(throwable, chunk, chunkOffset, latch, faultEvent);
                throw throwable;
            }
            // Put the page in the translation table before we undo the exclusive lock, as we could otherwise race with
            // eviction, and the onEvict callback expects to find a MuninnPage object in the table.
            UnsafeUtil.putIntVolatile(chunk, chunkOffset, PagedFile.toId(pageRef));
            // Once we page has been published to the translation table, we can convert our exclusive lock to whatever we
            // need for the page cursor.
            ConvertPageFaultLock(pageRef);
            latch.Release();
            faultEvent.Done();
            return(pageRef);
        }
コード例 #25
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private ByteBuffer[] convertToByteBuffers(long[] bufferAddresses, int arrayOffset, int length) throws Exception
        private ByteBuffer[] ConvertToByteBuffers(long[] bufferAddresses, int arrayOffset, int length)
        {
            ByteBuffer[] buffers = new ByteBuffer[length];
            for (int i = 0; i < length; i++)
            {
                long address = bufferAddresses[arrayOffset + i];
                buffers[i] = UnsafeUtil.newDirectByteBuffer(address, _filePageSize);
            }
            return(buffers);
        }
コード例 #26
0
        public override void PutBytes(int bytes, sbyte value)
        {
            long p = GetBoundedPointer(_offset, bytes);

            if (!_outOfBounds)
            {
                UnsafeUtil.setMemory(p, bytes, value);
            }
            _offset += bytes;
        }
コード例 #27
0
 private void PutLongAt(long p, long value)
 {
     if (UnsafeUtil.allowUnalignedMemoryAccess)
     {
         UnsafeUtil.putLong(p, UnsafeUtil.storeByteOrderIsNative ? value : Long.reverseBytes(value));
     }
     else
     {
         PutLongBigEndian(value, p);
     }
 }
コード例 #28
0
 private void PutLongBigEndian(long value, long p)
 {
     UnsafeUtil.putByte(p, ( sbyte )(value >> 56));
     UnsafeUtil.putByte(p + 1, ( sbyte )(value >> 48));
     UnsafeUtil.putByte(p + 2, ( sbyte )(value >> 40));
     UnsafeUtil.putByte(p + 3, ( sbyte )(value >> 32));
     UnsafeUtil.putByte(p + 4, ( sbyte )(value >> 24));
     UnsafeUtil.putByte(p + 5, ( sbyte )(value >> 16));
     UnsafeUtil.putByte(p + 6, ( sbyte )(value >> 8));
     UnsafeUtil.putByte(p + 7, ( sbyte )value);
 }
コード例 #29
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static ByteBuffer proxy(long buffer, int bufferLength) throws java.io.IOException
        private static ByteBuffer Proxy(long buffer, int bufferLength)
        {
            ByteBuffer buf = _proxyCache.get();

            if (buf != null)
            {
                UnsafeUtil.initDirectByteBuffer(buf, buffer, bufferLength);
                return(buf);
            }
            return(CreateAndGetNewBuffer(buffer, bufferLength));
        }
コード例 #30
0
 private void PutShortAt(long p, short value)
 {
     if (UnsafeUtil.allowUnalignedMemoryAccess)
     {
         UnsafeUtil.putShort(p, UnsafeUtil.storeByteOrderIsNative ? value : Short.reverseBytes(value));
     }
     else
     {
         PutShortBigEndian(value, p);
     }
 }