28package com.jogamp.common.nio;
30import java.io.BufferedInputStream;
32import java.io.FileInputStream;
33import java.io.IOException;
34import java.io.InputStream;
35import java.io.RandomAccessFile;
36import java.nio.MappedByteBuffer;
37import java.nio.channels.FileChannel;
39import org.junit.AfterClass;
40import org.junit.Assert;
41import org.junit.BeforeClass;
44import com.jogamp.common.os.Platform;
45import com.jogamp.common.util.IOUtil;
46import com.jogamp.junit.util.SingletonJunitCase;
48import org.junit.FixMethodOrder;
49import org.junit.runners.MethodSorters;
59@FixMethodOrder(MethodSorters.NAME_ASCENDING)
62 static final int buffer__8KiB = 1 << 13;
65 static final int halfMiB = 1 << 19;
67 static final int oneMiB = 1 << 20;
69 static final int tenMiB = 1 << 24;
71 static final int hunMiB = 1 << 27;
73 static final int halfGiB = 1 << 29;
75 static final int oneGiB = 1 << 30;
77 static final long twoPlusGiB = ( 2L << 30 ) + halfMiB;
79 static final String fileHalfMiB =
"./testHalfMiB.bin" ;
80 static final String fileOneMiB =
"./testOneMiB.bin" ;
81 static final String fileTenMiB =
"./testTenMiB.bin" ;
82 static final String fileHunMiB =
"./testHunMiB.bin" ;
83 static final String fileHalfGiB =
"./testHalfGiB.bin" ;
84 static final String fileOneGiB =
"./testOneGiB.bin" ;
85 static final String fileTwoPlusGiB =
"./testTwoPlusGiB.bin" ;
86 static final String fileOut =
"./testOut.bin" ;
88 public static final String PrintPrecision =
"%8.3f";
89 public static final double MIB = 1024.0*1024.0;
93 public static void setup() throws IOException {
94 final Runtime runtime = Runtime.getRuntime();
95 System.err.printf(
"Total Memory : "+PrintPrecision+
" MiB%n", runtime.totalMemory() / MIB);
96 System.err.printf(
"Max Memory : "+PrintPrecision+
" MiB%n", runtime.maxMemory() / MIB);
98 setup(fileHalfMiB, halfMiB);
99 setup(fileOneMiB, oneMiB);
100 setup(fileTenMiB, tenMiB);
101 setup(fileHunMiB, hunMiB);
102 setup(fileHalfGiB, halfGiB);
103 setup(fileOneGiB, oneGiB);
104 setup(fileTwoPlusGiB, twoPlusGiB);
106 static void setup(
final String fname,
final long size)
throws IOException {
107 final File file =
new File(fname);
108 final RandomAccessFile out =
new RandomAccessFile(file,
"rws");
115 cleanup(fileHalfMiB);
119 cleanup(fileHalfGiB);
121 cleanup(fileTwoPlusGiB);
124 static void cleanup(
final String fname) {
125 final File file =
new File(fname);
135 testCopyIntSize1Impl(fileTenMiB, tenMiB);
137 testCopyIntSize1Impl(fileHunMiB, hunMiB);
139 testCopyIntSize1Impl(fileHalfGiB, halfGiB);
144 static enum SrcType { COPY, MMAP1, MMAP2_NONE, MMAP2_SOFT, MMAP2_HARD };
149 testCopyIntSize1Impl2(0, SrcType.MMAP2_NONE, 0, fileOneMiB, oneMiB);
151 testCopyIntSize1Impl2(0, SrcType.MMAP2_NONE, 0, fileOneGiB, oneGiB);
159 testCopyIntSize1Impl2(0, SrcType.MMAP2_SOFT, 0, fileOneMiB, oneMiB);
161 testCopyIntSize1Impl2(0, SrcType.MMAP2_SOFT, 0, fileOneGiB, oneGiB);
169 testCopyIntSize1Impl2(0, SrcType.MMAP2_HARD, 0, fileOneMiB, oneMiB);
172 testCopyIntSize1Impl2(0, SrcType.MMAP2_HARD, 0, fileTwoPlusGiB, twoPlusGiB);
176 void testCopyIntSize1Impl(
final String testFileName,
final long expSize)
throws IOException {
177 testCopyIntSize1Impl2(0, SrcType.COPY, buffer__8KiB, testFileName, expSize);
178 testCopyIntSize1Impl2(0, SrcType.COPY, hunMiB, testFileName, expSize);
179 testCopyIntSize1Impl2(0, SrcType.MMAP1, 0, testFileName, expSize);
180 testCopyIntSize1Impl2(0, SrcType.MMAP2_SOFT, 0, testFileName, expSize);
181 System.err.println();
183 boolean testCopyIntSize1Impl2(
final int iter,
final SrcType srcType,
final int reqBufferSize,
final String testFileName,
final long expSize)
throws IOException {
184 final int expSizeI = (int) ( expSize <= Integer.MAX_VALUE ? expSize : Integer.MAX_VALUE );
185 final int bufferSize = reqBufferSize < expSizeI ? reqBufferSize : expSizeI;
186 final File testFile =
new File(testFileName);
187 final long hasSize1 = testFile.length();
188 final long t0 = System.currentTimeMillis();
189 Assert.assertEquals(expSize, hasSize1);
191 final Runtime runtime = Runtime.getRuntime();
192 final long[] usedMem0 = { 0 };
193 final long[] freeMem0 = { 0 };
194 final long[] usedMem1 = { 0 };
195 final long[] freeMem1 = { 0 };
197 final String prefix =
"test #"+iter+
" "+String.format(PrintPrecision+
" MiB", expSize/MIB);
198 System.err.printf(
"%s: mode %-5s, bufferSize %9d: BEGIN%n", prefix, srcType.toString(), bufferSize);
199 dumpMem(prefix+
" before", runtime, -1, -1, usedMem0, freeMem0 );
201 final IOException[] ioe = {
null };
202 OutOfMemoryError oome =
null;
203 InputStream bis =
null;
204 FileInputStream fis =
null;
205 FileChannel fic =
null;
206 boolean isMappedByteBufferInputStream =
false;
208 fis =
new FileInputStream(testFile);
209 if( SrcType.COPY == srcType ) {
210 if( hasSize1 > Integer.MAX_VALUE ) {
212 throw new IllegalArgumentException(
"file size > MAX_INT for "+srcType+
": "+hasSize1+
" of "+testFile);
214 bis =
new BufferedInputStream(fis, bufferSize);
215 }
else if( SrcType.MMAP1 == srcType ) {
216 if( hasSize1 > Integer.MAX_VALUE ) {
218 throw new IllegalArgumentException(
"file size > MAX_INT for "+srcType+
": "+hasSize1+
" of "+testFile);
220 fic = fis.getChannel();
221 final MappedByteBuffer fmap = fic.map(FileChannel.MapMode.READ_ONLY, 0, hasSize1);
222 bis =
new ByteBufferInputStream(fmap);
224 isMappedByteBufferInputStream =
true;
225 MappedByteBufferInputStream.CacheMode cmode;
227 case MMAP2_NONE: cmode = MappedByteBufferInputStream.CacheMode.FLUSH_NONE;
229 case MMAP2_SOFT: cmode = MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT;
231 case MMAP2_HARD: cmode = MappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD;
233 default: fis.close();
234 throw new InternalError(
"XX: "+srcType);
236 final MappedByteBufferInputStream mis =
new MappedByteBufferInputStream(fis.getChannel(), FileChannel.MapMode.READ_ONLY, cmode);
237 Assert.assertEquals(expSize, mis.remaining());
238 Assert.assertEquals(expSize, mis.length());
239 Assert.assertEquals(0, mis.position());
242 }
catch (
final IOException e) {
243 if( e.getCause() instanceof OutOfMemoryError ) {
244 oome = (OutOfMemoryError) e.getCause();
248 }
catch (
final OutOfMemoryError m) {
251 IOException ioe2 =
null;
253 if(
null != ioe[0] ||
null != oome ) {
255 System.err.printf(
"%s: mode %-5s, bufferSize %9d: OutOfMemoryError.1 %s%n",
256 prefix, srcType.toString(), bufferSize, oome.getMessage());
257 oome.printStackTrace();
259 Assert.assertNull(ioe[0]);
263 Assert.assertEquals(expSizeI, bis.available());
265 final long t1 = System.currentTimeMillis();
267 final File out =
new File(fileOut);
268 IOUtil.copyStream2File(bis, out);
269 final long t2 = System.currentTimeMillis();
272 if( isMappedByteBufferInputStream ) {
273 suffix =
", cacheMode "+((MappedByteBufferInputStream)bis).getCacheMode();
277 System.err.printf(
"%s: mode %-5s, bufferSize %9d: total %5d, setup %5d, copy %5d ms%s%n",
278 prefix, srcType.toString(), bufferSize, t2-t0, t1-t0, t2-t1, suffix);
280 Assert.assertEquals(expSize, out.length());
283 Assert.assertEquals(0, bis.available());
284 if( isMappedByteBufferInputStream ) {
285 final MappedByteBufferInputStream mis = (MappedByteBufferInputStream)bis;
286 Assert.assertEquals(0, mis.remaining());
287 Assert.assertEquals(expSize, mis.length());
288 Assert.assertEquals(expSize, mis.position());
290 dumpMem(prefix+
" after ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 );
294 }
catch (
final InterruptedException e) { }
295 dumpMem(prefix+
" gc'ed ", runtime, usedMem0[0], freeMem0[0], usedMem1, freeMem1 );
296 }
catch(
final IOException e ) {
297 if( e.getCause() instanceof OutOfMemoryError ) {
298 oome = (OutOfMemoryError) e.getCause();
302 }
catch (
final OutOfMemoryError m) {
314 System.err.printf(
"%s: mode %-5s, bufferSize %9d: END%n", prefix, srcType.toString(), bufferSize);
315 System.err.println();
317 if(
null != ioe2 ||
null != oome ) {
319 System.err.printf(
"%s: mode %-5s, bufferSize %9d: OutOfMemoryError.2 %s%n",
320 prefix, srcType.toString(), bufferSize, oome.getMessage());
321 oome.printStackTrace();
323 Assert.assertNull(ioe2);
332 final Runtime runtime,
final long usedMem0,
333 final long freeMem0,
final long[] usedMemN,
334 final long[] freeMemN )
336 usedMemN[0] = runtime.totalMemory() - runtime.freeMemory();
337 freeMemN[0] = runtime.freeMemory();
339 System.err.printf(
"%s Used Memory : "+PrintPrecision, pre, usedMemN[0] / MIB);
341 System.err.printf(
", delta "+PrintPrecision, (usedMemN[0]-usedMem0) / MIB);
343 System.err.println(
" MiB");
352 static boolean manualTest =
false;
354 public static void main(
final String args[])
throws IOException {
355 for(
int i=0; i<args.length; i++) {
356 if(args[i].equals(
"-manual")) {
361 org.junit.runner.JUnitCore.
main(tstname);