1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jfr.event.io; 27 28 import static jdk.test.lib.Asserts.assertEquals; 29 30 import java.io.File; 31 import java.io.RandomAccessFile; 32 import java.nio.ByteBuffer; 33 import java.nio.channels.FileChannel; 34 import java.time.Duration; 35 import java.util.ArrayList; 36 import java.util.List; 37 38 import jdk.jfr.Recording; 39 import jdk.jfr.consumer.RecordedEvent; 40 import jdk.test.lib.jfr.Events; 41 42 /* 43 * @test 44 * @key jfr 45 * @library /test/lib /test/jdk 46 * @run main/othervm jdk.jfr.event.io.TestFileChannelEvents 47 */ 48 public class TestFileChannelEvents { 49 public static void main(String[] args) throws Throwable { 50 File tmp = File.createTempFile("TestFileChannelEvents", ".tmp", new File(".")); 51 tmp.deleteOnExit(); 52 Recording recording = new Recording(); 53 List<IOEvent> expectedEvents = new ArrayList<>(); 54 55 try (RandomAccessFile rf = new RandomAccessFile(tmp, "rw"); FileChannel ch = rf.getChannel();) { 56 recording.enable(IOEvent.EVENT_FILE_FORCE).withThreshold(Duration.ofMillis(0)); 57 recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0)); 58 recording.enable(IOEvent.EVENT_FILE_WRITE).withThreshold(Duration.ofMillis(0)); 59 recording.start(); 60 61 ByteBuffer bufA = ByteBuffer.allocateDirect(10); 62 ByteBuffer bufB = ByteBuffer.allocateDirect(20); 63 bufA.put("1234567890".getBytes()); 64 bufB.put("1234567890".getBytes()); 65 66 // test write(ByteBuffer) 67 bufA.flip(); 68 long size = ch.write(bufA); 69 expectedEvents.add(IOEvent.createFileWriteEvent(size, tmp)); 70 71 // test write(ByteBuffer, long) 72 bufA.flip(); 73 size = ch.write(bufA, bufA.capacity() / 2); 74 expectedEvents.add(IOEvent.createFileWriteEvent(size, tmp)); 75 76 // test write(ByteBuffer[]) 77 bufA.flip(); 78 bufA.limit(5); 79 bufB.flip(); 80 bufB.limit(5); 81 size = ch.write(new ByteBuffer[] { bufA, bufB }); 82 expectedEvents.add(IOEvent.createFileWriteEvent(size, tmp)); 83 84 // test force(boolean) 85 ch.force(true); 86 expectedEvents.add(IOEvent.createFileForceEvent(tmp)); 87 88 // reset file 89 ch.position(0); 90 91 // test read(ByteBuffer) 92 bufA.clear(); 93 size = ch.read(bufA); 94 expectedEvents.add(IOEvent.createFileReadEvent(size, tmp)); 95 96 // test read(ByteBuffer, long) 97 bufA.clear(); 98 size = ch.read(bufA, bufA.capacity() / 2); 99 expectedEvents.add(IOEvent.createFileReadEvent(size, tmp)); 100 101 // test read(ByteBuffer[]) 102 bufA.clear(); 103 bufA.limit(5); 104 bufB.clear(); 105 bufB.limit(5); 106 size = ch.read(new ByteBuffer[] { bufA, bufB }); 107 expectedEvents.add(IOEvent.createFileReadEvent(size, tmp)); 108 109 // Read at EOF. Should get size -1 in event. 110 ch.position(ch.size()); 111 bufA.clear(); 112 size = ch.read(bufA); 113 assertEquals(size, -1L, "Expected size -1 when read at EOF"); 114 expectedEvents.add(IOEvent.createFileReadEvent(size, tmp)); 115 116 ch.close(); 117 recording.stop(); 118 List<RecordedEvent> events = Events.fromRecording(recording); 119 IOHelper.verifyEqualsInOrder(events, expectedEvents); 120 } 121 } 122 }