author | mchung |
Tue, 06 Nov 2018 10:01:16 -0800 | |
changeset 52427 | 3c6aa484536c |
parent 51088 | c36ca9d88f54 |
child 53902 | 7a6fd71449e7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51088
diff
changeset
|
2 |
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.nio; |
|
27 |
||
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51088
diff
changeset
|
28 |
import jdk.internal.access.JavaLangRefAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51088
diff
changeset
|
29 |
import jdk.internal.access.JavaNioAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
51088
diff
changeset
|
30 |
import jdk.internal.access.SharedSecrets; |
33656 | 31 |
import jdk.internal.misc.Unsafe; |
34882 | 32 |
import jdk.internal.misc.VM; |
2 | 33 |
|
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
36511
diff
changeset
|
34 |
import java.util.concurrent.atomic.AtomicLong; |
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
36511
diff
changeset
|
35 |
|
2 | 36 |
/** |
37 |
* Access to bits, native and otherwise. |
|
38 |
*/ |
|
39 |
||
40 |
class Bits { // package-private |
|
41 |
||
42 |
private Bits() { } |
|
43 |
||
44 |
||
45 |
// -- Swapping -- |
|
46 |
||
47 |
static short swap(short x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
48 |
return Short.reverseBytes(x); |
2 | 49 |
} |
50 |
||
51 |
static char swap(char x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
52 |
return Character.reverseBytes(x); |
2 | 53 |
} |
54 |
||
55 |
static int swap(int x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
56 |
return Integer.reverseBytes(x); |
2 | 57 |
} |
58 |
||
59 |
static long swap(long x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
60 |
return Long.reverseBytes(x); |
2 | 61 |
} |
62 |
||
63 |
||
64 |
// -- Unsafe access -- |
|
65 |
||
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
66 |
private static final Unsafe UNSAFE = Unsafe.getUnsafe(); |
2 | 67 |
|
68 |
// -- Processor and memory-system properties -- |
|
69 |
||
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
70 |
private static int PAGE_SIZE = -1; |
2 | 71 |
|
72 |
static int pageSize() { |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
73 |
if (PAGE_SIZE == -1) |
49581
bd45ce23b1ac
8201494: Avoid early initialization of java.nio.Bits
redestad
parents:
48356
diff
changeset
|
74 |
PAGE_SIZE = UNSAFE.pageSize(); |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
75 |
return PAGE_SIZE; |
2 | 76 |
} |
77 |
||
6128
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5994
diff
changeset
|
78 |
static int pageCount(long size) { |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5994
diff
changeset
|
79 |
return (int)(size + (long)pageSize() - 1L) / pageSize(); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5994
diff
changeset
|
80 |
} |
2 | 81 |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
82 |
private static boolean UNALIGNED = UNSAFE.unalignedAccess(); |
2 | 83 |
|
84 |
static boolean unaligned() { |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
85 |
return UNALIGNED; |
2 | 86 |
} |
87 |
||
88 |
||
89 |
// -- Direct memory management -- |
|
90 |
||
91 |
// A user-settable upper limit on the maximum amount of allocatable |
|
92 |
// direct buffer memory. This value may be changed during VM |
|
93 |
// initialization if it is launched with "-XX:MaxDirectMemorySize=<size>". |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
94 |
private static volatile long MAX_MEMORY = VM.maxDirectMemory(); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
95 |
private static final AtomicLong RESERVED_MEMORY = new AtomicLong(); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
96 |
private static final AtomicLong TOTAL_CAPACITY = new AtomicLong(); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
97 |
private static final AtomicLong COUNT = new AtomicLong(); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
98 |
private static volatile boolean MEMORY_LIMIT_SET; |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33656
diff
changeset
|
99 |
|
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
100 |
// max. number of sleeps during try-reserving with exponentially |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
101 |
// increasing delay before throwing OutOfMemoryError: |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
102 |
// 1, 2, 4, 8, 16, 32, 64, 128, 256 (total 511 ms ~ 0.5 s) |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
103 |
// which means that OOME will be thrown after 0.5 s of trying |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
104 |
private static final int MAX_SLEEPS = 9; |
2 | 105 |
|
106 |
// These methods should be called whenever direct memory is allocated or |
|
107 |
// freed. They allow the user to control the amount of direct memory |
|
108 |
// which a process may access. All sizes are specified in bytes. |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
109 |
static void reserveMemory(long size, int cap) { |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
110 |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
111 |
if (!MEMORY_LIMIT_SET && VM.initLevel() >= 1) { |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
112 |
MAX_MEMORY = VM.maxDirectMemory(); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
113 |
MEMORY_LIMIT_SET = true; |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
114 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
115 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
116 |
// optimist! |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
117 |
if (tryReserveMemory(size, cap)) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
118 |
return; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
119 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
120 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
121 |
final JavaLangRefAccess jlra = SharedSecrets.getJavaLangRefAccess(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
122 |
boolean interrupted = false; |
2 | 123 |
try { |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
124 |
|
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
125 |
// Retry allocation until success or there are no more |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
126 |
// references (including Cleaners that might free direct |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
127 |
// buffer memory) to process and allocation still fails. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
128 |
boolean refprocActive; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
129 |
do { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
130 |
try { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
131 |
refprocActive = jlra.waitForReferenceProcessing(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
132 |
} catch (InterruptedException e) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
133 |
// Defer interrupts and keep trying. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
134 |
interrupted = true; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
135 |
refprocActive = true; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
136 |
} |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
137 |
if (tryReserveMemory(size, cap)) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
138 |
return; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
139 |
} |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
140 |
} while (refprocActive); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
141 |
|
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
142 |
// trigger VM's Reference processing |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
143 |
System.gc(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
144 |
|
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
145 |
// A retry loop with exponential back-off delays. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
146 |
// Sometimes it would suffice to give up once reference |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
147 |
// processing is complete. But if there are many threads |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
148 |
// competing for memory, this gives more opportunities for |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
149 |
// any given thread to make progress. In particular, this |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
150 |
// seems to be enough for a stress test like |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
151 |
// DirectBufferAllocTest to (usually) succeed, while |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
152 |
// without it that test likely fails. Since failure here |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
153 |
// ends in OOME, there's no need to hurry. |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
154 |
long sleepTime = 1; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
155 |
int sleeps = 0; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
156 |
while (true) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
157 |
if (tryReserveMemory(size, cap)) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
158 |
return; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
159 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
160 |
if (sleeps >= MAX_SLEEPS) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
161 |
break; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
162 |
} |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
163 |
try { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
164 |
if (!jlra.waitForReferenceProcessing()) { |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
165 |
Thread.sleep(sleepTime); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
166 |
sleepTime <<= 1; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
167 |
sleeps++; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
168 |
} |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
169 |
} catch (InterruptedException e) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
40180
diff
changeset
|
170 |
interrupted = true; |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
171 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
172 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
173 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
174 |
// no luck |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
175 |
throw new OutOfMemoryError("Direct buffer memory"); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
176 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
177 |
} finally { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
178 |
if (interrupted) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
179 |
// don't swallow interrupts |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
180 |
Thread.currentThread().interrupt(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
181 |
} |
2 | 182 |
} |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
183 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
184 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
185 |
private static boolean tryReserveMemory(long size, int cap) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
186 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
187 |
// -XX:MaxDirectMemorySize limits the total capacity rather than the |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
188 |
// actual memory usage, which will differ when buffers are page |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
189 |
// aligned. |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
190 |
long totalCap; |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
191 |
while (cap <= MAX_MEMORY - (totalCap = TOTAL_CAPACITY.get())) { |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
192 |
if (TOTAL_CAPACITY.compareAndSet(totalCap, totalCap + cap)) { |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
193 |
RESERVED_MEMORY.addAndGet(size); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
194 |
COUNT.incrementAndGet(); |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
195 |
return true; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
196 |
} |
2 | 197 |
} |
198 |
||
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
199 |
return false; |
2 | 200 |
} |
201 |
||
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
202 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
203 |
static void unreserveMemory(long size, int cap) { |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
204 |
long cnt = COUNT.decrementAndGet(); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
205 |
long reservedMem = RESERVED_MEMORY.addAndGet(-size); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
206 |
long totalCap = TOTAL_CAPACITY.addAndGet(-cap); |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
207 |
assert cnt >= 0 && reservedMem >= 0 && totalCap >= 0; |
2 | 208 |
} |
209 |
||
51088
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
210 |
static final JavaNioAccess.BufferPool BUFFER_POOL = new JavaNioAccess.BufferPool() { |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
211 |
@Override |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
212 |
public String getName() { |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
213 |
return "direct"; |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
214 |
} |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
215 |
@Override |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
216 |
public long getCount() { |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
217 |
return Bits.COUNT.get(); |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
218 |
} |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
219 |
@Override |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
220 |
public long getTotalCapacity() { |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
221 |
return Bits.TOTAL_CAPACITY.get(); |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
222 |
} |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
223 |
@Override |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
224 |
public long getMemoryUsed() { |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
225 |
return Bits.RESERVED_MEMORY.get(); |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
226 |
} |
c36ca9d88f54
8207235: ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class) throws NPE
redestad
parents:
49581
diff
changeset
|
227 |
}; |
2 | 228 |
|
229 |
// These numbers represent the point at which we have empirically |
|
230 |
// determined that the average cost of a JNI call exceeds the expense |
|
231 |
// of an element by element copy. These numbers may change over time. |
|
232 |
static final int JNI_COPY_TO_ARRAY_THRESHOLD = 6; |
|
233 |
static final int JNI_COPY_FROM_ARRAY_THRESHOLD = 6; |
|
234 |
} |