Path: blob/master/test/jdk/java/nio/channels/FileChannel/FileExtensionAndMap.java
41154 views
/*1* Copyright (c) 2016, 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* @ignore This test has huge disk space requirements25* @bug 816862826* @summary Test extending files to very large sizes without hitting a SIGBUS27* @requires (os.family == "linux")28* @run main/othervm/timeout=600 -Xms4g -Xmx4g FileExtensionAndMap29* @run main/othervm/timeout=600 -Xms4g -Xmx4g FileExtensionAndMap true30*/3132import java.io.File;33import java.io.IOException;34import java.io.RandomAccessFile;35import java.nio.MappedByteBuffer;36import java.nio.channels.ClosedChannelException;37import java.nio.channels.FileChannel;38import java.nio.channels.FileChannel.MapMode;39import java.nio.file.Files;40import java.nio.file.Path;41import java.nio.file.Paths;42import java.nio.file.StandardCopyOption;43import java.nio.file.StandardOpenOption;44import java.util.concurrent.ExecutorService;45import java.util.concurrent.Executors;46import java.util.concurrent.ForkJoinPool;47import java.util.concurrent.Semaphore;48import java.util.stream.IntStream;4950public class FileExtensionAndMap {5152private static final ExecutorService CACHED_EXECUTORSERVICE =53Executors.newCachedThreadPool();5455private static final String TMPDIR = System.getProperty("test.dir", ".");5657private static boolean useRaf = false;5859public static void main(String args[]) throws Exception {60if (args.length > 2) {61throw new IllegalArgumentException62("Arguments: [true|false [targetFolder]]");63}6465String defaultFolder = TMPDIR + File.separator + "target";66if (args.length > 0) {67useRaf = Boolean.valueOf(args[0]);68if (args.length > 1) {69defaultFolder = args[1];70}71}72final String targetFolder = defaultFolder;73Path p = Paths.get(targetFolder);74boolean targetExists = Files.exists(p);75if (!targetExists) {76Files.createDirectory(p);77}7879System.out.printf("Using RandomAccessFile: %s; target folder: %s%n",80useRaf, targetFolder);8182ForkJoinPool fjPool = new ForkJoinPool(3);83fjPool.submit(() -> {84IntStream.range(0, 20).parallel().forEach((index) -> {85String fileName = "testBigFile_" + index + ".dat";86Path source = null;87Path target = null;88try {89source = Paths.get(TMPDIR, fileName);90testCreateBigFile(source);91target = Paths.get(targetFolder, fileName);92testFileCopy(source, target);93} catch (Throwable th) {94System.err.println("Error copying file with fileName: "95+ fileName + " : " + th.getMessage());96th.printStackTrace(System.err);97} finally {98try {99if (source != null) {100Files.deleteIfExists(source);101}102} catch (Throwable ignored) {103}104try {105if (target != null) {106Files.deleteIfExists(target);107}108} catch (Throwable ignored) {109}110}111});112}).join();113114if (!targetExists) {115Files.delete(p);116}117}118119private static void testFileCopy(Path source, Path target)120throws IOException {121Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);122System.out.println("Finished copying file with fileName: "123+ source.getFileName());124}125126private static void testCreateBigFile(Path segmentFile)127throws IOException {128final Semaphore concurrencySemaphore = new Semaphore(5);129long fileSize = 3L * 1024L * 1024L * 1024L;130int blockSize = 10 * 1024 * 1024;131int loopCount = (int) Math.floorDiv(fileSize, blockSize);132133String fileName = segmentFile.getFileName().toString();134if (useRaf) {135try (RandomAccessFile raf136= new RandomAccessFile(segmentFile.toFile(), "rw")) {137raf.setLength(fileSize);138try (FileChannel fc = raf.getChannel()) {139for (int i = 0; i < loopCount; i++) {140final long startPosition = 1L * blockSize * i;141concurrencySemaphore.acquireUninterruptibly();142CACHED_EXECUTORSERVICE.submit(() -> {143writeTemplateData(fileName, fc, startPosition,144blockSize, concurrencySemaphore);145});146}147} finally {148concurrencySemaphore.acquireUninterruptibly(5);149}150}151} else {152Path file = Files.createFile(segmentFile);153try (FileChannel fc = FileChannel.open(file,154StandardOpenOption.READ, StandardOpenOption.WRITE)) {155for (int i = 0; i < loopCount; i++) {156final long startPosition = 1L * blockSize * i;157concurrencySemaphore.acquireUninterruptibly();158CACHED_EXECUTORSERVICE.submit(() -> {159writeTemplateData(fileName, fc, startPosition,160blockSize, concurrencySemaphore);161});162}163}164}165}166167private static void writeTemplateData(String fileName,168FileChannel fc, long startPosition, int blockSize,169Semaphore concurrencySemaphore) {170try {171byte[] EMPTY_RECORD = new byte[blockSize / 256];172173MappedByteBuffer mappedByteBuffer = fc.map(MapMode.READ_WRITE,174startPosition, blockSize);175IntStream.range(0, 256).forEach((recordIndex) -> {176try {177mappedByteBuffer.position((int) (recordIndex *178EMPTY_RECORD.length));179mappedByteBuffer.put(EMPTY_RECORD, 0, EMPTY_RECORD.length);180} catch (Throwable th) {181System.err.println182("Error in FileExtensionAndMap.writeTemplateData empty record for fileName: "183+ fileName + ", startPosition: " + startPosition + ", recordIndex: "184+ recordIndex + " : " + th.getMessage());185th.printStackTrace(System.err);186}187});188189mappedByteBuffer.force();190} catch (Throwable th) {191if (!(th instanceof ClosedChannelException)) {192System.err.println193("Error in FileExtensionAndMap.writeTemplateData empty record for fileName: "194+ fileName + ", startPosition: " + startPosition + " : "195+ th.getMessage());196th.printStackTrace(System.err);197}198} finally {199concurrencySemaphore.release();200}201}202}203204205