author | sherman |
Wed, 16 Jul 2008 15:09:24 -0700 | |
changeset 842 | 93b8777f4184 |
parent 2 | 90ce3da70b43 |
child 1247 | b4c26443dee5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* @test |
|
842
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
25 |
* @bug 4938372 6541641 |
2 | 26 |
* @summary Flushing dirty pages prior to unmap can cause Cleaner thread to |
27 |
* abort VM if memory system has pages locked |
|
28 |
*/ |
|
29 |
import java.io.File; |
|
30 |
import java.io.RandomAccessFile; |
|
31 |
import java.io.IOException; |
|
32 |
import java.nio.ByteBuffer; |
|
33 |
import java.nio.channels.FileChannel; |
|
34 |
import java.util.ArrayList; |
|
35 |
||
36 |
/** |
|
37 |
* Test case provided by submitter of 4938372. |
|
38 |
*/ |
|
39 |
||
40 |
public class ExpandingMap { |
|
41 |
||
842
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
42 |
public static void main(String[] args) throws Exception { |
2 | 43 |
|
44 |
int initialSize = 20480*1024; |
|
45 |
int maximumMapSize = 16*1024*1024; |
|
46 |
int maximumFileSize = 300000000; |
|
47 |
||
48 |
File file = File.createTempFile("exp", "tmp"); |
|
49 |
file.deleteOnExit(); |
|
50 |
RandomAccessFile f = new RandomAccessFile(file, "rw"); |
|
51 |
f.setLength(initialSize); |
|
52 |
||
53 |
FileChannel fc = f.getChannel(); |
|
54 |
||
55 |
ByteBuffer[] buffers = new ByteBuffer[128]; |
|
56 |
||
57 |
System.out.format("map %d -> %d\n", 0, initialSize); |
|
58 |
buffers[0] = fc.map(FileChannel.MapMode.READ_WRITE, 0, initialSize); |
|
59 |
||
60 |
int currentBuffer = 0; |
|
61 |
int currentSize = initialSize; |
|
62 |
int currentPosition = 0; |
|
63 |
||
64 |
ArrayList<String> junk = new ArrayList<String>(); |
|
65 |
||
66 |
while (currentPosition+currentSize < maximumFileSize) { |
|
67 |
int inc = Math.max(1000*1024, (currentPosition+currentSize)/8); |
|
68 |
||
69 |
int size = currentPosition+currentSize+inc; |
|
70 |
f.setLength(size); |
|
71 |
||
72 |
while (currentSize+inc > maximumMapSize) { |
|
73 |
if (currentSize < maximumMapSize) { |
|
74 |
System.out.format("map %d -> %d\n", currentPosition, |
|
75 |
(currentPosition + maximumMapSize)); |
|
76 |
buffers[currentBuffer] = fc.map(FileChannel.MapMode.READ_WRITE, |
|
77 |
currentPosition, maximumMapSize); |
|
78 |
fillBuffer(buffers[currentBuffer], currentSize); |
|
79 |
} |
|
80 |
currentPosition += maximumMapSize; |
|
81 |
inc = currentSize+inc-maximumMapSize; |
|
82 |
currentSize = 0; |
|
83 |
currentBuffer++; |
|
84 |
if (currentBuffer == buffers.length) { |
|
85 |
ByteBuffer[] old = buffers; |
|
86 |
buffers = new ByteBuffer[currentBuffer+currentBuffer/2]; |
|
87 |
System.arraycopy(old, 0, buffers, 0, currentBuffer); } |
|
88 |
} |
|
89 |
currentSize += inc; |
|
90 |
if (currentSize > 0) { |
|
91 |
System.out.format("map %d -> %d\n", currentPosition, |
|
92 |
(currentPosition + currentSize)); |
|
93 |
buffers[currentBuffer] = fc.map(FileChannel.MapMode.READ_WRITE, |
|
94 |
currentPosition, currentSize); |
|
95 |
fillBuffer(buffers[currentBuffer], currentSize-inc); |
|
96 |
} |
|
97 |
||
98 |
// busy loop needed to reproduce issue |
|
99 |
long t = System.currentTimeMillis(); |
|
100 |
while (System.currentTimeMillis() < t+500) { |
|
101 |
junk.add(String.valueOf(t)); |
|
102 |
if (junk.size() > 100000) junk.clear(); |
|
103 |
} |
|
104 |
} |
|
105 |
||
842
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
106 |
fc.close(); |
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
107 |
// cleanup the ref to mapped buffers so they can be GCed |
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
108 |
for (int i = 0; i < buffers.length; i++) |
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
109 |
buffers[i] = null; |
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
110 |
System.gc(); |
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
111 |
// Take a nap to wait for the Cleaner to cleanup those unrefed maps |
93b8777f4184
6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents:
2
diff
changeset
|
112 |
Thread.sleep(1000); |
2 | 113 |
System.out.println("TEST PASSED"); |
114 |
} |
|
115 |
||
116 |
static void fillBuffer(ByteBuffer buf, int from) { |
|
117 |
int limit = buf.limit(); |
|
118 |
for (int i=from; i<limit; i++) { |
|
119 |
buf.put(i, (byte)i); |
|
120 |
} |
|
121 |
} |
|
122 |
} |