From 7df0947f94936ff5d0d4449f13766b33261c923f Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Tue, 23 May 2017 13:29:19 +0200 Subject: [PATCH] Rename the underlying unit of allocation in the MemoryManager from Slab to Grab, to avoid confusion things with the Slab allocator --- .../impl/internal/dragons/MemoryManager.java | 76 +++++++++---------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/community/unsafe/src/main/java/org/neo4j/unsafe/impl/internal/dragons/MemoryManager.java b/community/unsafe/src/main/java/org/neo4j/unsafe/impl/internal/dragons/MemoryManager.java index df9f6228bb0ff..943264fab93b4 100644 --- a/community/unsafe/src/main/java/org/neo4j/unsafe/impl/internal/dragons/MemoryManager.java +++ b/community/unsafe/src/main/java/org/neo4j/unsafe/impl/internal/dragons/MemoryManager.java @@ -22,15 +22,15 @@ /** * The memory manager is simple: it only allocates memory, until it itself is finalizable and frees it all in one go. * - * The memory is allocated in large segments, and the memory returned by the memory manager is page aligned, and plays - * well with transparent huge pages and other operating system optimisations. + * The memory is allocated in large segments, called "grabs", and the memory returned by the memory manager is page + * aligned, and plays well with transparent huge pages and other operating system optimisations. * * The memory manager assumes that the memory claimed from it is evenly divisible in units of pages. */ public final class MemoryManager { /** - * The amount of memory, in bytes, to grab in each Slab. + * The amount of memory, in bytes, to grab in each Grab. */ private static final long GRAB_SIZE = FeatureToggles.getInteger( MemoryManager.class, "GRAB_SIZE", 512 * 1024 ); // 512 KiB @@ -40,7 +40,7 @@ public final class MemoryManager private long memoryReserve; private final long alignment; - private Slab slabs; + private Grab grabs; /** * Create a new MemoryManager that will allocate the given amount of memory, to pointers that are aligned to the @@ -62,11 +62,11 @@ public MemoryManager( long expectedMaxMemory, long alignment ) public synchronized long sumUsedMemory() { long sum = 0; - Slab s = slabs; - while ( s != null ) + Grab grab = grabs; + while ( grab != null ) { - sum += s.nextAlignedPointer - s.address; - s = s.next; + sum += grab.nextAlignedPointer - grab.address; + grab = grab.next; } return sum; } @@ -80,47 +80,47 @@ public synchronized long allocateAligned( long bytes ) { if ( bytes > GRAB_SIZE ) { - // This is a huge allocation. Put it in its own slab and keep any existing slab at the head. - Slab nextSlab = slabs == null ? null : slabs.next; - Slab allocationSlab = new Slab( nextSlab, bytes, alignment ); - if ( !allocationSlab.canAllocate( bytes ) ) + // This is a huge allocation. Put it in its own grab and keep any existing grab at the head. + Grab nextGrab = grabs == null ? null : grabs.next; + Grab allocationGrab = new Grab( nextGrab, bytes, alignment ); + if ( !allocationGrab.canAllocate( bytes ) ) { - allocationSlab.free(); - allocationSlab = new Slab( nextSlab, bytes + alignment, alignment ); + allocationGrab.free(); + allocationGrab = new Grab( nextGrab, bytes + alignment, alignment ); } - long allocation = allocationSlab.allocate( bytes ); - slabs = slabs == null ? allocationSlab : slabs.setNext( allocationSlab ); + long allocation = allocationGrab.allocate( bytes ); + grabs = grabs == null ? allocationGrab : grabs.setNext( allocationGrab ); memoryReserve -= bytes; return allocation; } - if ( slabs == null || !slabs.canAllocate( bytes ) ) + if ( grabs == null || !grabs.canAllocate( bytes ) ) { - long slabGrab = Math.min( GRAB_SIZE, memoryReserve ); - if ( slabGrab < bytes ) + long desiredGrabSize = Math.min( GRAB_SIZE, memoryReserve ); + if ( desiredGrabSize < bytes ) { - slabGrab = bytes; - Slab slab = new Slab( slabs, slabGrab, alignment ); - if ( slab.canAllocate( bytes ) ) + desiredGrabSize = bytes; + Grab grab = new Grab( grabs, desiredGrabSize, alignment ); + if ( grab.canAllocate( bytes ) ) { - memoryReserve -= slabGrab; - slabs = slab; - return slabs.allocate( bytes ); + memoryReserve -= desiredGrabSize; + grabs = grab; + return grabs.allocate( bytes ); } - slab.free(); - slabGrab = bytes + alignment; + grab.free(); + desiredGrabSize = bytes + alignment; } - memoryReserve -= slabGrab; - slabs = new Slab( slabs, slabGrab, alignment ); + memoryReserve -= desiredGrabSize; + grabs = new Grab( grabs, desiredGrabSize, alignment ); } - return slabs.allocate( bytes ); + return grabs.allocate( bytes ); } @Override protected synchronized void finalize() throws Throwable { super.finalize(); - Slab current = slabs; + Grab current = grabs; while ( current != null ) { @@ -129,15 +129,15 @@ protected synchronized void finalize() throws Throwable } } - private static class Slab + private static class Grab { - public final Slab next; + public final Grab next; private final long address; private final long limit; private final long alignMask; private long nextAlignedPointer; - Slab( Slab next, long size, long alignment ) + Grab( Grab next, long size, long alignment ) { this.next = next; this.address = UnsafeUtil.allocateMemory( size ); @@ -147,7 +147,7 @@ private static class Slab nextAlignedPointer = nextAligned( address ); } - Slab( Slab next, long address, long limit, long alignMask, long nextAlignedPointer ) + Grab( Grab next, long address, long limit, long alignMask, long nextAlignedPointer ) { this.next = next; this.address = address; @@ -182,9 +182,9 @@ boolean canAllocate( long bytes ) return nextAlignedPointer + bytes <= limit; } - Slab setNext( Slab slab ) + Grab setNext( Grab grab ) { - return new Slab( slab, address, limit, alignMask, nextAlignedPointer ); + return new Grab( grab, address, limit, alignMask, nextAlignedPointer ); } @Override @@ -193,7 +193,7 @@ public String toString() long size = limit - address; long reserve = nextAlignedPointer > limit ? 0 : limit - nextAlignedPointer; double use = (1.0 - reserve / ((double) size)) * 100.0; - return String.format( "Slab[size = %d bytes, reserve = %d bytes, use = %5.2f %%]", size, reserve, use ); + return String.format( "Grab[size = %d bytes, reserve = %d bytes, use = %5.2f %%]", size, reserve, use ); } } }