src/jdk.jfr/share/classes/jdk/jfr/consumer/UseCasesStream.java
branchJEP-349-branch
changeset 58145 bc54ed8d908a
parent 58129 7b751fe181a5
child 58146 9f3aadcaa430
equal deleted inserted replaced
58129:7b751fe181a5 58145:bc54ed8d908a
     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.consumer;
       
    27 
       
    28 import java.io.FileWriter;
       
    29 import java.io.IOException;
       
    30 import java.io.PrintWriter;
       
    31 import java.nio.file.Path;
       
    32 import java.nio.file.Paths;
       
    33 import java.text.ParseException;
       
    34 import java.time.Duration;
       
    35 import java.util.ArrayDeque;
       
    36 import java.util.Deque;
       
    37 
       
    38 import jdk.jfr.Configuration;
       
    39 import jdk.jfr.EventType;
       
    40 import jdk.jfr.ValueDescriptor;
       
    41 
       
    42 final class UseCasesStream {
       
    43 
       
    44     //
       
    45     // Use case: Out-of-the-Box Experience
       
    46     //
       
    47     // - Simple things should be simple
       
    48     // - Pique interest, i.e. a one-liner on Stack Overflow
       
    49     // - Few lines of code as possible
       
    50     // - Should be easier than alternative technologies, like JMX and JVM TI
       
    51     //
       
    52     // - Non-goals: Corner-cases, advanced configuration, releasing resources
       
    53     //
       
    54     public static void outOfTheBox() throws Exception {
       
    55         try (RecordingStream rs = new RecordingStream()) {
       
    56             rs.enable("jdk.ExceptionThrown");
       
    57             rs.onEvent(e -> System.out.println(e.getString("message")));
       
    58             rs.start();
       
    59         }
       
    60 
       
    61         try (RecordingStream rs = new RecordingStream()) {
       
    62             rs.enable("jdk.JavaMonitorEnter").withThreshold(Duration.ofMillis(20)).withoutStackTrace();
       
    63             rs.onEvent(System.out::println);
       
    64             rs.start();
       
    65         }
       
    66 
       
    67         try (RecordingStream rs = new RecordingStream()) {
       
    68             rs.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1));
       
    69             rs.onEvent(System.out::println);
       
    70             rs.start();
       
    71         }
       
    72 
       
    73         try (RecordingStream rs = new RecordingStream()) {
       
    74             rs.enable("jdk.GarbageCollection");
       
    75             rs.onEvent(System.out::println);
       
    76             rs.start();
       
    77         }
       
    78     }
       
    79 
       
    80     // Use case: Event Forwarding
       
    81     //
       
    82     // - Forward arbitrary event to frameworks such as RxJava, JSON/XML and
       
    83     // Kafka
       
    84     // - Handle flooding
       
    85     // - Performant
       
    86     // - Graceful shutdown
       
    87     // - Non-goals: Filter events
       
    88     //
       
    89     public static void eventForwarding() throws InterruptedException, IOException, ParseException {
       
    90         // KafkaProducer producer = new KafkaProducer<String, String>();
       
    91         try (RecordingStream rs = new RecordingStream(Configuration.getConfiguration("default"))) {
       
    92             rs.setMaxAge(Duration.ofMinutes(5));
       
    93             rs.setMaxSize(1000_000_000L);
       
    94             rs.setOrdered(false);
       
    95             rs.setReuse(true); // default
       
    96             // rs.consume(e -> producer.send(new
       
    97             // ProducerRecord<String,String>("topic", e.getString("key"),
       
    98             // e.getString("value"))));
       
    99             rs.start();
       
   100         }
       
   101         // Write primitive values to XML
       
   102         try (RecordingStream rs = new RecordingStream(Configuration.getConfiguration("deafult"))) {
       
   103             try (PrintWriter p = new PrintWriter(new FileWriter("recording.xml"))) {
       
   104                 p.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
       
   105                 p.println("<events>");
       
   106                 rs.onEvent(e -> {
       
   107                     EventType type = e.getEventType();
       
   108                     p.println("  <event type=\"" + type.getName() + "\" start=\"" + e.getStartTime() + "\" end=\"" + e.getEndTime() + "\">");
       
   109                     for (ValueDescriptor field : e.getEventType().getFields()) {
       
   110                         Object value = e.getValue(field.getName());
       
   111                         if (value instanceof Number || field.getTypeName().equals("java.lang.String")) {
       
   112                             p.println("    <value field=\"" + field.getName() + "\">" + value + "</value>");
       
   113                         }
       
   114                     }
       
   115                 });
       
   116                 rs.start();
       
   117                 p.println("</events>");
       
   118             }
       
   119         }
       
   120     }
       
   121 
       
   122     // Use case: Repository Access
       
   123     //
       
   124     // - Read the disk repository from another process, for example a side car
       
   125     // in aDocker container
       
   126     // - Be able to configure flush interval from command line or jcmd.
       
   127     // - Graceful shutdown
       
   128     //
       
   129     public static void repositoryAccess() throws IOException, InterruptedException {
       
   130         Path repository = Paths.get("c:\\repository").toAbsolutePath();
       
   131         String command = new String();
       
   132         command += "java -XX:StartFlightRecording:flush-interval=2s";
       
   133         command += "-XX:FlightRecorderOption:repository=" + repository + " Application";
       
   134         Process myProcess = Runtime.getRuntime().exec(command);
       
   135         try (RecordingStream rs = new RecordingStream()) {
       
   136             rs.onEvent(System.out::println);
       
   137             rs.startAsync();
       
   138             Thread.sleep(10_000);
       
   139             myProcess.destroy();
       
   140             Thread.sleep(10_000);
       
   141         }
       
   142     }
       
   143 
       
   144     // Use: Tooling
       
   145     //
       
   146     // - Monitor a stream of data for a very long time
       
   147     // - Predictable interval, i.e. once every second
       
   148     // - Notification with minimal delay
       
   149     // - Events with the same period should arrive together
       
   150     // - Consume events in chronological order
       
   151     // - Low overhead
       
   152     //
       
   153     public static void tooling() throws IOException, ParseException {
       
   154         Deque<Double> measurements = new ArrayDeque<>();
       
   155         try (RecordingStream rs = new RecordingStream(Configuration.getConfiguration("profile"))) {
       
   156             rs.setFlushInterval(Duration.ofSeconds(1));
       
   157             rs.setMaxAge(Duration.ofMinutes(1));
       
   158             rs.setOrdered(true); // default
       
   159             rs.setReuse(false);
       
   160             rs.onEvent("jdk.CPULoad", e -> {
       
   161                 double d = e.getDouble("totalMachine");
       
   162                 measurements.addFirst(d);
       
   163                 if (measurements.size() > 60) {
       
   164                     measurements.removeLast();
       
   165                 }
       
   166                 // repaint();
       
   167             });
       
   168             rs.start();
       
   169         }
       
   170     }
       
   171 
       
   172     // Use case: Low Impact
       
   173     //
       
   174     // - Support event subscriptions in a low latency environment (minimal GC
       
   175     // pauses)
       
   176     // - Filter out relevant events to minimize disk overhead and allocation
       
   177     // pressure
       
   178     // - Avoid impact from other recordings
       
   179     // - Avoid observer effect, in particular self-recursion
       
   180     //
       
   181     // Non-goals: one-liner
       
   182     //
       
   183     public static void lowImpact() throws InterruptedException, IOException, ParseException {
       
   184         try (RecordingStream rs = new RecordingStream()) {
       
   185             rs.setReuse(true); // default
       
   186             rs.enable("jdk.JavaMonitorEnter").withThreshold(Duration.ofMillis(10));
       
   187             rs.enable("jdk.ExceptionThrow");
       
   188             rs.onEvent("jdk.JavaMonitorEnter", System.out::println);
       
   189             rs.onEvent("jdk.ExceptionThrow", System.out::println);
       
   190             rs.start();
       
   191         }
       
   192     }
       
   193 }