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)); } } }
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)); }
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]); } }
//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); } }
public override void PutByte(sbyte value) { long p = NextBoundedPointer(_sizeOfByte); UnsafeUtil.putByte(p, value); _offset++; }
//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); }
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); }
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(); }
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); }
//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); }
public override void RemoveHopBit(int index, int hd) { long adr = HopBitsAddress(index); int hopBits = UnsafeUtil.getInt(adr); hopBits |= 1 << hd; UnsafeUtil.putInt(adr, hopBits); }
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; }
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); }
public override void PutHopBit(int index, int hd) { long adr = HopBitsAddress(index); int hopBits = UnsafeUtil.getInt(adr); hopBits &= ~(1 << hd); UnsafeUtil.putInt(adr, hopBits); }
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); }
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); }
internal virtual void InitBuffer(long pageRef) { if (GetAddress(pageRef) == 0L) { long addr = _memoryAllocator.allocateAligned(CachePageSize, _bufferAlignment); UnsafeUtil.putLong(OffAddress(pageRef), addr); } }
//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); } }
//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); }
private int GetIntAt(long p) { if (UnsafeUtil.allowUnalignedMemoryAccess) { int x = UnsafeUtil.getInt(p); return(UnsafeUtil.storeByteOrderIsNative ? x : Integer.reverseBytes(x)); } return(GetIntBigEndian(p)); }
private void IncreaseFileSizeTo(long newFileSize) { long currentFileSize; do { currentFileSize = CurrentFileSize; } while (currentFileSize < newFileSize && !UnsafeUtil.compareAndSwapLong(this, _fileSizeOffset, currentFileSize, newFileSize)); }
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); }
private short GetShortAt(long p) { if (UnsafeUtil.allowUnalignedMemoryAccess) { short x = UnsafeUtil.getShort(p); return(UnsafeUtil.storeByteOrderIsNative ? x : Short.reverseBytes(x)); } return(GetShortBigEndian(p)); }
//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); }
//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); }
public override void PutBytes(int bytes, sbyte value) { long p = GetBoundedPointer(_offset, bytes); if (!_outOfBounds) { UnsafeUtil.setMemory(p, bytes, value); } _offset += bytes; }
private void PutLongAt(long p, long value) { if (UnsafeUtil.allowUnalignedMemoryAccess) { UnsafeUtil.putLong(p, UnsafeUtil.storeByteOrderIsNative ? value : Long.reverseBytes(value)); } else { PutLongBigEndian(value, p); } }
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); }
//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)); }
private void PutShortAt(long p, short value) { if (UnsafeUtil.allowUnalignedMemoryAccess) { UnsafeUtil.putShort(p, UnsafeUtil.storeByteOrderIsNative ? value : Short.reverseBytes(value)); } else { PutShortBigEndian(value, p); } }