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  * 
  46  * @library /lib /
  47  * @run main/othervm jdk.jfr.event.io.TestFileChannelEvents
  48  */
  49 public class TestFileChannelEvents {
  50     public static void main(String[] args) throws Throwable {
  51         File tmp = File.createTempFile("TestFileChannelEvents", ".tmp", new File("."));
  52         tmp.deleteOnExit();
  53         Recording recording = new Recording();
  54         List<IOEvent> expectedEvents = new ArrayList<>();
  55 
  56         try (RandomAccessFile rf = new RandomAccessFile(tmp, "rw"); FileChannel ch = rf.getChannel();) {
  57             recording.enable(IOEvent.EVENT_FILE_FORCE).withThreshold(Duration.ofMillis(0));
  58             recording.enable(IOEvent.EVENT_FILE_READ).withThreshold(Duration.ofMillis(0));
  59             recording.enable(IOEvent.EVENT_FILE_WRITE).withThreshold(Duration.ofMillis(0));
  60             recording.start();
  61 
  62             ByteBuffer bufA = ByteBuffer.allocateDirect(10);
  63             ByteBuffer bufB = ByteBuffer.allocateDirect(20);
  64             bufA.put("1234567890".getBytes());
  65             bufB.put("1234567890".getBytes());
  66 
  67             // test write(ByteBuffer)
  68             bufA.flip();
  69             long size = ch.write(bufA);
  70             expectedEvents.add(IOEvent.createFileWriteEvent(size, tmp));
  71 
  72             // test write(ByteBuffer, long)
  73             bufA.flip();
  74             size = ch.write(bufA, bufA.capacity() / 2);
  75             expectedEvents.add(IOEvent.createFileWriteEvent(size, tmp));
  76 
  77             // test write(ByteBuffer[])
  78             bufA.flip();
  79             bufA.limit(5);
  80             bufB.flip();
  81             bufB.limit(5);
  82             size = ch.write(new ByteBuffer[] { bufA, bufB });
  83             expectedEvents.add(IOEvent.createFileWriteEvent(size, tmp));
  84 
  85             // test force(boolean)
  86             ch.force(true);
  87             expectedEvents.add(IOEvent.createFileForceEvent(tmp));
  88 
  89             // reset file
  90             ch.position(0);
  91 
  92             // test read(ByteBuffer)
  93             bufA.clear();
  94             size = ch.read(bufA);
  95             expectedEvents.add(IOEvent.createFileReadEvent(size, tmp));
  96 
  97             // test read(ByteBuffer, long)
  98             bufA.clear();
  99             size = ch.read(bufA, bufA.capacity() / 2);
 100             expectedEvents.add(IOEvent.createFileReadEvent(size, tmp));
 101 
 102             // test read(ByteBuffer[])
 103             bufA.clear();
 104             bufA.limit(5);
 105             bufB.clear();
 106             bufB.limit(5);
 107             size = ch.read(new ByteBuffer[] { bufA, bufB });
 108             expectedEvents.add(IOEvent.createFileReadEvent(size, tmp));
 109 
 110             // Read at EOF. Should get size -1 in event.
 111             ch.position(ch.size());
 112             bufA.clear();
 113             size = ch.read(bufA);
 114             assertEquals(size, -1L, "Expected size -1 when read at EOF");
 115             expectedEvents.add(IOEvent.createFileReadEvent(size, tmp));
 116 
 117             ch.close();
 118             recording.stop();
 119             List<RecordedEvent> events = Events.fromRecording(recording);
 120             IOHelper.verifyEqualsInOrder(events, expectedEvents);
 121         }
 122     }
 123 }