test/jdk/java/nio/channels/FileChannel/CleanerTest.java
author jbhateja
Tue, 26 Nov 2019 16:09:25 +0300
changeset 59278 8375560db76b
parent 50796 1f1eb24facdd
permissions -rw-r--r--
8234394: C2: Dynamic register class support in ADLC Reviewed-by: vlivanov, sviswanathan, thartmann, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47327
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     4
 *
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     8
 *
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    13
 * accompanied this code).
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    14
 *
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    18
 *
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    21
 * questions.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    22
 */
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    23
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    24
/* @test
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    25
 * @bug 8147615
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    26
 * @summary Test whether an unreferenced FileChannel is actually cleaned
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    31
 * @run main/othervm CleanerTest
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    32
 */
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    33
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    34
import com.sun.management.UnixOperatingSystemMXBean;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    35
import java.lang.management.ManagementFactory;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    36
import java.lang.management.OperatingSystemMXBean;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    37
import java.lang.ref.PhantomReference;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    38
import java.lang.ref.Reference;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    43
import java.nio.channels.FileChannel;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    44
import java.nio.file.Files;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    45
import java.nio.file.Path;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    46
import java.nio.file.Paths;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    53
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    54
public class CleanerTest {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    55
    public static void main(String[] args) throws Throwable {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    56
        OperatingSystemMXBean mxBean =
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    57
            ManagementFactory.getOperatingSystemMXBean();
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    58
        UnixOperatingSystemMXBean unixMxBean = null;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    59
        if (mxBean instanceof UnixOperatingSystemMXBean) {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    60
            unixMxBean = (UnixOperatingSystemMXBean)mxBean;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    61
        } else {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    62
            System.out.println("Non-Unix system: skipping test.");
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    63
            return;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    64
        }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    69
        Path path = Paths.get(System.getProperty("test.dir", "."), "junk");
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    70
        try {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    71
            FileChannel fc = FileChannel.open(path, StandardOpenOption.CREATE,
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    72
                StandardOpenOption.READ, StandardOpenOption.WRITE);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    96
            }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
   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
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
   124
            }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
   125
        } finally {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
   126
            Files.delete(path);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
   127
        }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
   128
    }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
   129
}