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