author | rriggs |
Tue, 16 Jan 2018 10:48:58 -0500 | |
changeset 48534 | 12d9ff9e0a4b |
parent 48224 | be0df5ab3093 |
child 50727 | 081b132c4dc0 |
permissions | -rw-r--r-- |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
1 |
/* |
48534 | 2 |
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
4 |
* |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
8 |
* |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
13 |
* accompanied this code). |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
14 |
* |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
18 |
* |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
21 |
* questions. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
22 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
23 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
24 |
import java.io.File; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
25 |
import java.io.FileDescriptor; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
26 |
import java.io.FileNotFoundException; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
27 |
import java.io.RandomAccessFile; |
48534 | 28 |
import java.lang.management.ManagementFactory; |
29 |
import java.lang.management.OperatingSystemMXBean; |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
30 |
import java.lang.ref.Cleaner; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
31 |
import java.lang.ref.Reference; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
32 |
import java.lang.ref.ReferenceQueue; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
33 |
import java.lang.ref.WeakReference; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
34 |
import java.lang.reflect.Field; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
35 |
import java.util.HashSet; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
36 |
|
48534 | 37 |
import com.sun.management.UnixOperatingSystemMXBean; |
38 |
||
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
39 |
/** |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
40 |
* @test |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
41 |
* @bug 8080225 |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
42 |
* @modules java.base/java.io:open |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
43 |
* @summary Test to ensure that an unclosed and unreferenced RandomAccessFile closes the fd |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
44 |
* @run main/othervm UnreferencedRAFClosesFd |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
45 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
46 |
public class UnreferencedRAFClosesFd { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
47 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
48 |
static final String FILE_NAME = "empty.txt"; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
49 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
50 |
/* standalone interface */ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
51 |
public static void main(String argv[]) throws Exception { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
52 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
53 |
File inFile= new File(System.getProperty("test.dir", "."), FILE_NAME); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
54 |
inFile.createNewFile(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
55 |
inFile.deleteOnExit(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
56 |
|
48534 | 57 |
long fdCount0 = getFdCount(); |
58 |
System.out.printf("initial count of open file descriptors: %d%n", fdCount0); |
|
59 |
||
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
60 |
String name = inFile.getPath(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
61 |
RandomAccessFile raf; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
62 |
try { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
63 |
// raf is explicitly *not* closed to allow cleaner to work |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
64 |
raf = new RandomAccessFile(name, "rw"); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
65 |
} catch (FileNotFoundException e) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
66 |
System.out.println("Unexpected exception " + e); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
67 |
throw(e); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
68 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
69 |
FileDescriptor fd = raf.getFD(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
70 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
71 |
Field fdField = FileDescriptor.class.getDeclaredField("cleanup"); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
72 |
fdField.setAccessible(true); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
73 |
Cleaner.Cleanable cleanup = (Cleaner.Cleanable)fdField.get(fd); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
74 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
75 |
// Prepare to wait for FOS, FD, Cleanup to be reclaimed |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
76 |
ReferenceQueue<Object> queue = new ReferenceQueue<>(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
77 |
HashSet<Reference<?>> pending = new HashSet<>(3); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
78 |
pending.add(new WeakReference<>(cleanup, queue)); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
79 |
pending.add(new WeakReference<>(raf, queue)); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
80 |
pending.add(new WeakReference<>(fd, queue)); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
81 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
82 |
Reference<?> r; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
83 |
while (((r = queue.remove(10L)) != null) |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
84 |
|| !pending.isEmpty()) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
85 |
System.out.printf("r: %s, pending: %d%n", r, pending.size()); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
86 |
if (r != null) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
87 |
pending.remove(r); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
88 |
} else { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
89 |
cleanup = null; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
90 |
raf = null; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
91 |
fd = null; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
92 |
System.gc(); // attempt to reclaim the RAF, cleanup, and fd |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
93 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
94 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
95 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
96 |
// Keep these variables in scope as gc roots |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
97 |
Reference.reachabilityFence(cleanup); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
98 |
Reference.reachabilityFence(fd); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
99 |
Reference.reachabilityFence(raf); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
100 |
Reference.reachabilityFence(pending); |
48534 | 101 |
|
102 |
// Check the final count of open file descriptors |
|
103 |
long fdCount = getFdCount(); |
|
104 |
System.out.printf("final count of open file descriptors: %d%n", fdCount); |
|
105 |
if (fdCount != fdCount0) { |
|
106 |
throw new AssertionError("raw fd count wrong: expected: " + fdCount0 |
|
107 |
+ ", actual: " + fdCount); |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
||
112 |
// Get the count of open file descriptors, or -1 if not available |
|
113 |
private static long getFdCount() { |
|
114 |
OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean(); |
|
115 |
return (mxBean instanceof UnixOperatingSystemMXBean) |
|
116 |
? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount() |
|
117 |
: -1L; |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
118 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
diff
changeset
|
119 |
} |