Path: blob/master/test/jdk/java/nio/channels/FileChannel/ExpandingMap.java
41154 views
/*1* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4938372 654164125* @summary Flushing dirty pages prior to unmap can cause Cleaner thread to26* abort VM if memory system has pages locked27* @run main/othervm ExpandingMap28*/29import java.io.File;30import java.io.RandomAccessFile;31import java.io.IOException;32import java.nio.ByteBuffer;33import java.nio.channels.FileChannel;34import java.util.ArrayList;3536/**37* Test case provided by submitter of 4938372.38*/3940public class ExpandingMap {4142public static void main(String[] args) throws Exception {4344int initialSize = 20480*1024;45int maximumMapSize = 16*1024*1024;46int maximumFileSize = 300000000;4748File file = File.createTempFile("exp", "tmp");49file.deleteOnExit();50RandomAccessFile f = new RandomAccessFile(file, "rw");51f.setLength(initialSize);5253FileChannel fc = f.getChannel();5455ByteBuffer[] buffers = new ByteBuffer[128];5657System.out.format("map %d -> %d\n", 0, initialSize);58buffers[0] = fc.map(FileChannel.MapMode.READ_WRITE, 0, initialSize);5960int currentBuffer = 0;61int currentSize = initialSize;62int currentPosition = 0;6364ArrayList<String> junk = new ArrayList<String>();6566while (currentPosition+currentSize < maximumFileSize) {67int inc = Math.max(1000*1024, (currentPosition+currentSize)/8);6869int size = currentPosition+currentSize+inc;70f.setLength(size);7172while (currentSize+inc > maximumMapSize) {73if (currentSize < maximumMapSize) {74System.out.format("map %d -> %d\n", currentPosition,75(currentPosition + maximumMapSize));76buffers[currentBuffer] = fc.map(FileChannel.MapMode.READ_WRITE,77currentPosition, maximumMapSize);78fillBuffer(buffers[currentBuffer], currentSize);79}80currentPosition += maximumMapSize;81inc = currentSize+inc-maximumMapSize;82currentSize = 0;83currentBuffer++;84if (currentBuffer == buffers.length) {85ByteBuffer[] old = buffers;86buffers = new ByteBuffer[currentBuffer+currentBuffer/2];87System.arraycopy(old, 0, buffers, 0, currentBuffer); }88}89currentSize += inc;90if (currentSize > 0) {91System.out.format("map %d -> %d\n", currentPosition,92(currentPosition + currentSize));93buffers[currentBuffer] = fc.map(FileChannel.MapMode.READ_WRITE,94currentPosition, currentSize);95fillBuffer(buffers[currentBuffer], currentSize-inc);96}9798// busy loop needed to reproduce issue99long t = System.currentTimeMillis();100while (System.currentTimeMillis() < t+500) {101junk.add(String.valueOf(t));102if (junk.size() > 100000) junk.clear();103}104}105106fc.close();107// cleanup the ref to mapped buffers so they can be GCed108for (int i = 0; i < buffers.length; i++)109buffers[i] = null;110System.gc();111// Take a nap to wait for the Cleaner to cleanup those unrefed maps112Thread.sleep(1000);113System.out.println("TEST PASSED");114}115116static void fillBuffer(ByteBuffer buf, int from) {117int limit = buf.limit();118for (int i=from; i<limit; i++) {119buf.put(i, (byte)i);120}121}122}123124125