jdk/test/java/nio/channels/FileChannel/ExpandingMap.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5970 d4e98bbfb0be
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5970
diff changeset
     2
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * questions.
2
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
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    28
 * @run main/othervm ExpandingMap
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.RandomAccessFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Test case provided by submitter of 4938372.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ExpandingMap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
842
93b8777f4184 6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents: 2
diff changeset
    43
    public static void main(String[] args) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        int initialSize = 20480*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        int maximumMapSize = 16*1024*1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        int maximumFileSize = 300000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        File file = File.createTempFile("exp", "tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        file.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        RandomAccessFile f = new RandomAccessFile(file, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        f.setLength(initialSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        FileChannel fc = f.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        ByteBuffer[] buffers = new ByteBuffer[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.format("map %d -> %d\n", 0, initialSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        buffers[0] = fc.map(FileChannel.MapMode.READ_WRITE, 0, initialSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int currentBuffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int currentSize = initialSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int currentPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ArrayList<String> junk = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        while (currentPosition+currentSize < maximumFileSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            int inc = Math.max(1000*1024, (currentPosition+currentSize)/8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            int size = currentPosition+currentSize+inc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            f.setLength(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            while (currentSize+inc > maximumMapSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                if (currentSize < maximumMapSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    System.out.format("map %d -> %d\n", currentPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        (currentPosition + maximumMapSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    buffers[currentBuffer] = fc.map(FileChannel.MapMode.READ_WRITE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        currentPosition, maximumMapSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    fillBuffer(buffers[currentBuffer], currentSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                currentPosition += maximumMapSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                inc = currentSize+inc-maximumMapSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                currentSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                currentBuffer++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if (currentBuffer == buffers.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    ByteBuffer[] old = buffers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    buffers = new ByteBuffer[currentBuffer+currentBuffer/2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    System.arraycopy(old, 0, buffers, 0, currentBuffer);                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            currentSize += inc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (currentSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                System.out.format("map %d -> %d\n", currentPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    (currentPosition + currentSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                buffers[currentBuffer] = fc.map(FileChannel.MapMode.READ_WRITE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                     currentPosition, currentSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                fillBuffer(buffers[currentBuffer], currentSize-inc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // busy loop needed to reproduce issue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            long t = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            while (System.currentTimeMillis() < t+500) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                junk.add(String.valueOf(t));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                if (junk.size() > 100000) junk.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
842
93b8777f4184 6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents: 2
diff changeset
   107
        fc.close();
93b8777f4184 6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents: 2
diff changeset
   108
        // 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
   109
        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
   110
            buffers[i] = null;
93b8777f4184 6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents: 2
diff changeset
   111
        System.gc();
93b8777f4184 6541641: (fc) java/nio/channels/FileChannel/ExpandingMap.java should clean up after itself
sherman
parents: 2
diff changeset
   112
        // 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
   113
        Thread.sleep(1000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        System.out.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void fillBuffer(ByteBuffer buf, int from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int limit = buf.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        for (int i=from; i<limit; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            buf.put(i, (byte)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}