test/jdk/jdk/jfr/api/consumer/recordingstream/TestSetEndTime.java
changeset 58863 c16ac7a2eba4
child 59274 eb3e2a5c2bcd
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
       
     1 /*
       
     2  * Copyright (c) 2019, 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.api.consumer.recordingstream;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.nio.file.Path;
       
    30 import java.nio.file.Paths;
       
    31 import java.time.Duration;
       
    32 import java.time.Instant;
       
    33 import java.util.concurrent.CountDownLatch;
       
    34 import java.util.concurrent.atomic.AtomicBoolean;
       
    35 import java.util.concurrent.atomic.AtomicInteger;
       
    36 
       
    37 import jdk.jfr.Event;
       
    38 import jdk.jfr.Name;
       
    39 import jdk.jfr.Recording;
       
    40 import jdk.jfr.StackTrace;
       
    41 import jdk.jfr.consumer.EventStream;
       
    42 import jdk.jfr.consumer.RecordedEvent;
       
    43 import jdk.jfr.consumer.RecordingFile;
       
    44 import jdk.jfr.consumer.RecordingStream;
       
    45 
       
    46 /**
       
    47  * @test
       
    48  * @summary Tests EventStream::setEndTime
       
    49  * @key jfr
       
    50  * @requires vm.hasJFR
       
    51  * @library /test/lib
       
    52  * @run main/othervm jdk.jfr.api.consumer.recordingstream.TestSetEndTime
       
    53  */
       
    54 public final class TestSetEndTime {
       
    55 
       
    56     @Name("Mark")
       
    57     @StackTrace(false)
       
    58     public final static class Mark extends Event {
       
    59         public boolean include;
       
    60         public int id;
       
    61     }
       
    62 
       
    63     public static void main(String... args) throws Exception {
       
    64         testEventStream();
       
    65         testRecordingStream();
       
    66     }
       
    67 
       
    68     private static void testRecordingStream() throws Exception {
       
    69         while (true) {
       
    70             CountDownLatch closed = new CountDownLatch(1);
       
    71             AtomicInteger count = new AtomicInteger();
       
    72             try (RecordingStream rs = new RecordingStream()) {
       
    73                 rs.setFlushInterval(Duration.ofSeconds(1));
       
    74                 rs.onEvent(e -> {
       
    75                     count.incrementAndGet();
       
    76                 });
       
    77                 // when end is reached stream is closed
       
    78                 rs.onClose(() -> {
       
    79                     closed.countDown();
       
    80                 });
       
    81                 Instant endTime = Instant.now().plus(Duration.ofMillis(100));
       
    82                 System.out.println("Setting end time: " + endTime);
       
    83                 rs.setEndTime(endTime);
       
    84                 rs.startAsync();
       
    85                 for (int i = 0; i < 50; i++) {
       
    86                     Mark m = new Mark();
       
    87                     m.commit();
       
    88                     Thread.sleep(10);
       
    89                 }
       
    90                 closed.await();
       
    91                 System.out.println("Found events: " + count.get());
       
    92                 if (count.get() < 50) {
       
    93                     return;
       
    94                 }
       
    95                 System.out.println("Found 50 events. Retrying");
       
    96                 System.out.println();
       
    97             }
       
    98         }
       
    99     }
       
   100 
       
   101     static void testEventStream() throws InterruptedException, IOException, Exception {
       
   102         while (true) {
       
   103             try (Recording r = new Recording()) {
       
   104                 r.start();
       
   105 
       
   106                 Mark event1 = new Mark();
       
   107                 event1.id = 1;
       
   108                 event1.include = false;
       
   109                 event1.commit(); // start time
       
   110 
       
   111                 nap();
       
   112 
       
   113                 Mark event2 = new Mark();
       
   114                 event2.id = 2;
       
   115                 event2.include = true;
       
   116                 event2.commit();
       
   117 
       
   118                 nap();
       
   119 
       
   120                 Mark event3 = new Mark();
       
   121                 event3.id = 3;
       
   122                 event3.include = false;
       
   123                 event3.commit(); // end time
       
   124 
       
   125                 Path p = Paths.get("recording.jfr");
       
   126                 r.dump(p);
       
   127                 Instant start = null;
       
   128                 Instant end = null;
       
   129                 System.out.println("Find start and end time as instants:");
       
   130                 for (RecordedEvent e : RecordingFile.readAllEvents(p)) {
       
   131                     if (e.getInt("id") == 1) {
       
   132                         start = e.getEndTime();
       
   133                         System.out.println("Start  : " + start);
       
   134                     }
       
   135                     if (e.getInt("id") == 2) {
       
   136                         Instant middle = e.getEndTime();
       
   137                         System.out.println("Middle : " + middle);
       
   138                     }
       
   139                     if (e.getInt("id") == 3) {
       
   140                         end = e.getEndTime();
       
   141                         System.out.println("End    : " + end);
       
   142                     }
       
   143                 }
       
   144                 System.out.println();
       
   145                 System.out.println("Opening stream between " + start + " and " + end);
       
   146                 AtomicBoolean success = new AtomicBoolean(false);
       
   147                 AtomicInteger eventsCount = new AtomicInteger();
       
   148                 try (EventStream d = EventStream.openRepository()) {
       
   149                     d.setStartTime(start.plusNanos(1));
       
   150                     // Stream should close when end is reached
       
   151                     d.setEndTime(end.minusNanos(1));
       
   152                     d.onEvent(e -> {
       
   153                         eventsCount.incrementAndGet();
       
   154                         boolean include = e.getBoolean("include");
       
   155                         System.out.println("Event " + e.getEndTime() + " include=" + include);
       
   156                         if (include) {
       
   157                             success.set(true);
       
   158                         }
       
   159                     });
       
   160                     d.start();
       
   161                     if (eventsCount.get() == 1 && success.get()) {
       
   162                         return;
       
   163                     }
       
   164                 }
       
   165             }
       
   166         }
       
   167 
       
   168     }
       
   169 
       
   170     private static void nap() throws InterruptedException {
       
   171         // Ensure we advance at least 1 ns with fast time
       
   172         Thread.sleep(1);
       
   173     }
       
   174 
       
   175 }