-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MemoryManager.java
199 lines (180 loc) · 6.68 KB
/
MemoryManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.unsafe.impl.internal.dragons;
/**
* 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, 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 Grab.
*/
private static final long GRAB_SIZE = FeatureToggles.getInteger( MemoryManager.class, "GRAB_SIZE", 512 * 1024 ); // 512 KiB
/**
* The amount of memory that this memory manager can still allocate.
*/
private long memoryReserve;
private final long alignment;
private Grab grabs;
/**
* Create a new MemoryManager that will allocate the given amount of memory, to pointers that are aligned to the
* given alignment size.
* @param expectedMaxMemory The maximum amount of memory that this memory manager is expected to allocate. The
* actual amount of memory used can end up greater than this value, if some of it gets wasted on alignment padding.
* @param alignment The byte multiple that the allocated pointers have to be aligned at.
*/
public MemoryManager( long expectedMaxMemory, long alignment )
{
if ( alignment == 0 )
{
throw new IllegalArgumentException( "Alignment cannot be zero" );
}
this.memoryReserve = expectedMaxMemory;
this.alignment = alignment;
}
public synchronized long sumUsedMemory()
{
long sum = 0;
Grab grab = grabs;
while ( grab != null )
{
sum += grab.nextAlignedPointer - grab.address;
grab = grab.next;
}
return sum;
}
/**
* Allocate a contiguous, aligned region of memory of the given size in bytes.
* @param bytes the number of bytes to allocate.
* @return A pointer to the allocated memory.
*/
public synchronized long allocateAligned( long bytes )
{
if ( bytes > GRAB_SIZE )
{
// 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 ) )
{
allocationGrab.free();
allocationGrab = new Grab( nextGrab, bytes + alignment, alignment );
}
long allocation = allocationGrab.allocate( bytes );
grabs = grabs == null ? allocationGrab : grabs.setNext( allocationGrab );
memoryReserve -= bytes;
return allocation;
}
if ( grabs == null || !grabs.canAllocate( bytes ) )
{
long desiredGrabSize = Math.min( GRAB_SIZE, memoryReserve );
if ( desiredGrabSize < bytes )
{
desiredGrabSize = bytes;
Grab grab = new Grab( grabs, desiredGrabSize, alignment );
if ( grab.canAllocate( bytes ) )
{
memoryReserve -= desiredGrabSize;
grabs = grab;
return grabs.allocate( bytes );
}
grab.free();
desiredGrabSize = bytes + alignment;
}
memoryReserve -= desiredGrabSize;
grabs = new Grab( grabs, desiredGrabSize, alignment );
}
return grabs.allocate( bytes );
}
@Override
protected synchronized void finalize() throws Throwable
{
super.finalize();
Grab current = grabs;
while ( current != null )
{
current.free();
current = current.next;
}
}
private static class Grab
{
public final Grab next;
private final long address;
private final long limit;
private final long alignMask;
private long nextAlignedPointer;
Grab( Grab next, long size, long alignment )
{
this.next = next;
this.address = UnsafeUtil.allocateMemory( size );
this.limit = address + size;
this.alignMask = alignment - 1;
nextAlignedPointer = nextAligned( address );
}
Grab( Grab next, long address, long limit, long alignMask, long nextAlignedPointer )
{
this.next = next;
this.address = address;
this.limit = limit;
this.alignMask = alignMask;
this.nextAlignedPointer = nextAlignedPointer;
}
private long nextAligned( long pointer )
{
if ( (pointer & ~alignMask) == pointer )
{
return pointer;
}
return (pointer + alignMask) & ~alignMask;
}
long allocate( long bytes )
{
long allocation = nextAlignedPointer;
nextAlignedPointer = nextAligned( nextAlignedPointer + bytes );
return allocation;
}
void free()
{
UnsafeUtil.free( address );
}
boolean canAllocate( long bytes )
{
return nextAlignedPointer + bytes <= limit;
}
Grab setNext( Grab grab )
{
return new Grab( grab, address, limit, alignMask, nextAlignedPointer );
}
@Override
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( "Grab[size = %d bytes, reserve = %d bytes, use = %5.2f %%]", size, reserve, use );
}
}
}