author | jbhateja |
Tue, 26 Nov 2019 16:09:25 +0300 | |
changeset 59278 | 8375560db76b |
parent 50796 | 1f1eb24facdd |
permissions | -rw-r--r-- |
47327 | 1 |
/* |
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
47327 | 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* @test |
|
25 |
* @bug 8147615 |
|
26 |
* @summary Test whether an unreferenced FileChannel is actually cleaned |
|
27 |
* @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris") | (os.family == "aix") |
|
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
28 |
* @library /test/lib |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
29 |
* @build jdk.test.lib.util.FileUtils CleanerTest |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
30 |
* @modules java.management java.base/sun.nio.ch:+open |
47327 | 31 |
* @run main/othervm CleanerTest |
32 |
*/ |
|
33 |
||
34 |
import com.sun.management.UnixOperatingSystemMXBean; |
|
35 |
import java.lang.management.ManagementFactory; |
|
36 |
import java.lang.management.OperatingSystemMXBean; |
|
37 |
import java.lang.ref.PhantomReference; |
|
38 |
import java.lang.ref.Reference; |
|
39 |
import java.lang.ref.ReferenceQueue; |
|
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
40 |
import java.lang.ref.PhantomReference; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
41 |
import java.lang.ref.WeakReference; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
42 |
import java.lang.reflect.Field; |
47327 | 43 |
import java.nio.channels.FileChannel; |
44 |
import java.nio.file.Files; |
|
45 |
import java.nio.file.Path; |
|
46 |
import java.nio.file.Paths; |
|
47 |
import java.nio.file.StandardOpenOption; |
|
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
48 |
import java.util.HashSet; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
49 |
|
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
50 |
import jdk.test.lib.util.FileUtils; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
51 |
|
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
52 |
import sun.nio.ch.FileChannelImpl; |
47327 | 53 |
|
54 |
public class CleanerTest { |
|
55 |
public static void main(String[] args) throws Throwable { |
|
56 |
OperatingSystemMXBean mxBean = |
|
57 |
ManagementFactory.getOperatingSystemMXBean(); |
|
58 |
UnixOperatingSystemMXBean unixMxBean = null; |
|
59 |
if (mxBean instanceof UnixOperatingSystemMXBean) { |
|
60 |
unixMxBean = (UnixOperatingSystemMXBean)mxBean; |
|
61 |
} else { |
|
62 |
System.out.println("Non-Unix system: skipping test."); |
|
63 |
return; |
|
64 |
} |
|
65 |
||
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
66 |
FileUtils.listFileDescriptors(System.out); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
67 |
long fdCount0 = unixMxBean.getOpenFileDescriptorCount(); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
68 |
|
47327 | 69 |
Path path = Paths.get(System.getProperty("test.dir", "."), "junk"); |
70 |
try { |
|
71 |
FileChannel fc = FileChannel.open(path, StandardOpenOption.CREATE, |
|
72 |
StandardOpenOption.READ, StandardOpenOption.WRITE); |
|
73 |
||
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
74 |
// Prepare to wait for Channel, FD and Cleaner to be reclaimed |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
75 |
ReferenceQueue<Object> refQueue = new ReferenceQueue<>(); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
76 |
HashSet<Reference<?>> pending = new HashSet<>(); |
47327 | 77 |
|
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
78 |
Reference<Object> fcRef = new PhantomReference<>(fc, refQueue); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
79 |
pending.add(fcRef); |
47327 | 80 |
|
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
81 |
Field fdField = FileChannelImpl.class.getDeclaredField("fd"); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
82 |
fdField.setAccessible(true); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
83 |
Object fd = fdField.get(fc); // get the fd from the channel |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
84 |
WeakReference<Object> fdWeak = new WeakReference<>(fd, refQueue); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
85 |
pending.add(fdWeak); |
47327 | 86 |
|
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
87 |
Field closerField = FileChannelImpl.class.getDeclaredField("closer"); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
88 |
closerField.setAccessible(true); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
89 |
Object closer = closerField.get(fc); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
90 |
System.out.printf(" cleanup: %s, fd: %s, cf: %s%n", fc, fd, closer); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
91 |
|
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
92 |
if (closer != null) { |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
93 |
WeakReference<Object> closerWeak = new WeakReference<>(closer, refQueue); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
94 |
pending.add(closerWeak); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
95 |
System.out.printf(" closerWeak: %s%n", closerWeak); |
47327 | 96 |
} |
97 |
||
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
98 |
// Wait for all of the objects being tracked to be reclaimed; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
99 |
// The test will timeout if they are not reclaimed within the jtreg timeout |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
100 |
Reference<?> r; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
101 |
while (((r = refQueue.remove(1000L)) != null) |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
102 |
|| !pending.isEmpty()) { |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
103 |
System.out.printf(" r: %s, pending: %d%n", r, pending.size()); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
104 |
if (r != null) { |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
105 |
pending.remove(r); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
106 |
} else { |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
107 |
fc = null; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
108 |
fd = null; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
109 |
closer = null; |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
110 |
System.gc(); // attempt to reclaim them |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
111 |
} |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
112 |
} |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
113 |
|
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
114 |
Reference.reachabilityFence(fc); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
115 |
Reference.reachabilityFence(fd); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
116 |
Reference.reachabilityFence(closer); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
117 |
|
47327 | 118 |
long fdCount = unixMxBean.getOpenFileDescriptorCount(); |
50796
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
119 |
if (fdCount != fdCount0) { |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
120 |
// Add debugging info about file descriptor changes |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
121 |
System.out.printf("initial count of open file descriptors: %d%n", fdCount0); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
122 |
System.out.printf("final count of open file descriptors: %d%n", fdCount); |
1f1eb24facdd
8205547: FileChannel/CleanerTest.java fails due to expected FD count
rriggs
parents:
47327
diff
changeset
|
123 |
FileUtils.listFileDescriptors(System.out); |
47327 | 124 |
} |
125 |
} finally { |
|
126 |
Files.delete(path); |
|
127 |
} |
|
128 |
} |
|
129 |
} |