1
1
/*
2
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
22
22
*/
23
23
24
24
import java .io .IOException ;
25
-
26
25
import java .lang .management .BufferPoolMXBean ;
27
26
import java .lang .management .ManagementFactory ;
28
-
29
27
import java .nio .ByteBuffer ;
30
-
31
28
import java .nio .channels .FileChannel ;
32
-
33
29
import java .nio .file .Path ;
34
30
import java .nio .file .Paths ;
31
+ import java .util .List ;
32
+ import java .util .SplittableRandom ;
33
+ import java .util .concurrent .CountDownLatch ;
35
34
36
35
import static java .nio .file .StandardOpenOption .CREATE ;
37
36
import static java .nio .file .StandardOpenOption .TRUNCATE_EXISTING ;
38
37
import static java .nio .file .StandardOpenOption .WRITE ;
39
38
40
- import java .util .List ;
41
- import java .util .Random ;
42
- import java .util .concurrent .CountDownLatch ;
39
+ import jdk .test .lib .RandomFactory ;
43
40
44
41
/*
45
42
* @test
46
43
* @requires sun.arch.data.model == "64"
47
44
* @modules java.management
45
+ * @library /test/lib
48
46
* @build TestMaxCachedBufferSize
49
47
* @run main/othervm TestMaxCachedBufferSize
50
48
* @run main/othervm -Djdk.nio.maxCachedBufferSize=0 TestMaxCachedBufferSize
51
49
* @run main/othervm -Djdk.nio.maxCachedBufferSize=2000 TestMaxCachedBufferSize
52
50
* @run main/othervm -Djdk.nio.maxCachedBufferSize=100000 TestMaxCachedBufferSize
53
51
* @run main/othervm -Djdk.nio.maxCachedBufferSize=10000000 TestMaxCachedBufferSize
54
- * @summary Test the implementation of the jdk.nio.maxCachedBufferSize property.
52
+ * @summary Test the implementation of the jdk.nio.maxCachedBufferSize property
53
+ * (use -Dseed=X to set PRNG seed)
54
+ * @key randomness
55
55
*/
56
56
public class TestMaxCachedBufferSize {
57
57
private static final int DEFAULT_ITERS = 10 * 1000 ;
@@ -70,7 +70,9 @@ public class TestMaxCachedBufferSize {
70
70
private static final int LARGE_BUFFER_FREQUENCY = 100 ;
71
71
72
72
private static final String FILE_NAME_PREFIX = "nio-out-file-" ;
73
- private static final int VERBOSE_PERIOD = 5 * 1000 ;
73
+ private static final int VERBOSE_PERIOD = DEFAULT_ITERS / 10 ;
74
+
75
+ private static final SplittableRandom SRAND = RandomFactory .getSplittableRandom ();
74
76
75
77
private static int iters = DEFAULT_ITERS ;
76
78
private static int threadNum = DEFAULT_THREAD_NUM ;
@@ -86,6 +88,8 @@ private static BufferPoolMXBean getDirectPool() {
86
88
throw new Error ("could not find direct pool" );
87
89
}
88
90
private static final BufferPoolMXBean directPool = getDirectPool ();
91
+ private static long initialCount ;
92
+ private static long initialCapacity ;
89
93
90
94
// Each worker will do write operations on a file channel using
91
95
// buffers of various sizes. The buffer size is randomly chosen to
@@ -95,7 +99,7 @@ private static BufferPoolMXBean getDirectPool() {
95
99
private static class Worker implements Runnable {
96
100
private final int id ;
97
101
private final CountDownLatch finishLatch , exitLatch ;
98
- private final Random random = new Random ();
102
+ private SplittableRandom random = SRAND . split ();
99
103
private long smallBufferCount = 0 ;
100
104
private long largeBufferCount = 0 ;
101
105
@@ -177,8 +181,9 @@ public Worker(int id, CountDownLatch finishLatch, CountDownLatch exitLatch) {
177
181
}
178
182
179
183
public static void checkDirectBuffers (long expectedCount , long expectedMax ) {
180
- final long directCount = directPool .getCount ();
181
- final long directTotalCapacity = directPool .getTotalCapacity ();
184
+ final long directCount = directPool .getCount () - initialCount ;
185
+ final long directTotalCapacity =
186
+ directPool .getTotalCapacity () - initialCapacity ;
182
187
System .out .printf ("Direct %d / %dK\n " ,
183
188
directCount , directTotalCapacity / 1024 );
184
189
@@ -190,12 +195,15 @@ public static void checkDirectBuffers(long expectedCount, long expectedMax) {
190
195
191
196
if (directTotalCapacity > expectedMax ) {
192
197
throw new Error (String .format (
193
- "inconsistent direct buffer total capacity, expectex max = %d, found = %d" ,
198
+ "inconsistent direct buffer total capacity, expected max = %d, found = %d" ,
194
199
expectedMax , directTotalCapacity ));
195
200
}
196
201
}
197
202
198
203
public static void main (String [] args ) {
204
+ initialCount = directPool .getCount ();
205
+ initialCapacity = directPool .getTotalCapacity ();
206
+
199
207
final String maxBufferSizeStr = System .getProperty ("jdk.nio.maxCachedBufferSize" );
200
208
final long maxBufferSize =
201
209
(maxBufferSizeStr != null ) ? Long .valueOf (maxBufferSizeStr ) : Long .MAX_VALUE ;
0 commit comments