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