|
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.event.runtime; |
|
27 |
|
28 import java.util.concurrent.CountDownLatch; |
|
29 |
|
30 import java.util.HashMap; |
|
31 import java.util.Map; |
|
32 |
|
33 import jdk.jfr.Event; |
|
34 import jdk.jfr.FlightRecorder; |
|
35 import jdk.jfr.Period; |
|
36 import jdk.jfr.Recording; |
|
37 import jdk.jfr.consumer.RecordingStream; |
|
38 import jdk.jfr.consumer.RecordedEvent; |
|
39 |
|
40 import jdk.test.lib.Asserts; |
|
41 import jdk.test.lib.jfr.EventNames; |
|
42 |
|
43 /** |
|
44 * @test |
|
45 * @summary Verifies at the metalevel that stream contents are written to ongoing recordings |
|
46 * @key jfr |
|
47 * @requires vm.hasJFR |
|
48 * @library /test/lib |
|
49 * @run main/othervm -Xlog:jfr+system+streaming=trace jdk.jfr.event.runtime.TestFlush |
|
50 */ |
|
51 public class TestFlush { |
|
52 private static boolean flushEventAck = false; |
|
53 |
|
54 @Period("2 s") |
|
55 static class ZebraEvent extends Event { |
|
56 } |
|
57 static class CatEvent extends Event { |
|
58 } |
|
59 static class DogEvent extends Event { |
|
60 } |
|
61 static class MouseEvent extends Event { |
|
62 } |
|
63 |
|
64 public static void main(String... args) throws InterruptedException { |
|
65 CountDownLatch dogLatch = new CountDownLatch(1); |
|
66 CountDownLatch catLatch = new CountDownLatch(1); |
|
67 CountDownLatch mouseLatch = new CountDownLatch(1); |
|
68 CountDownLatch zebraLatch = new CountDownLatch(3); |
|
69 |
|
70 FlightRecorder.addPeriodicEvent(ZebraEvent.class, () -> { |
|
71 ZebraEvent ze = new ZebraEvent(); |
|
72 ze.commit(); |
|
73 }); |
|
74 |
|
75 try (RecordingStream rs = new RecordingStream()) { |
|
76 rs.enable(EventNames.Flush); |
|
77 rs.enable(EventNames.FlushStorage); |
|
78 rs.enable(EventNames.FlushStacktrace); |
|
79 rs.enable(EventNames.FlushStringPool); |
|
80 rs.enable(EventNames.FlushMetadata); |
|
81 rs.enable(EventNames.FlushTypeSet); |
|
82 rs.onEvent(e -> { |
|
83 switch (e.getEventType().getName()) { |
|
84 case EventNames.Flush: |
|
85 flushEventAck = true; |
|
86 case EventNames.FlushStorage: |
|
87 case EventNames.FlushStacktrace: |
|
88 case EventNames.FlushStringPool: |
|
89 case EventNames.FlushMetadata: |
|
90 case EventNames.FlushTypeSet: |
|
91 validateFlushEvent(e); |
|
92 return; |
|
93 } |
|
94 if (e.getEventType().getName().equals(CatEvent.class.getName())) { |
|
95 System.out.println("Found cat!"); |
|
96 catLatch.countDown(); |
|
97 return; |
|
98 } |
|
99 if (e.getEventType().getName().equals(DogEvent.class.getName())) { |
|
100 System.out.println("Found dog!"); |
|
101 dogLatch.countDown(); |
|
102 return; |
|
103 } |
|
104 if (e.getEventType().getName().equals(ZebraEvent.class.getName())) { |
|
105 System.out.println("Found zebra!"); |
|
106 zebraLatch.countDown(); |
|
107 return; |
|
108 } |
|
109 if (e.getEventType().getName().equals(MouseEvent.class.getName())) { |
|
110 System.out.println("Found mouse!"); |
|
111 mouseLatch.countDown(); |
|
112 return; |
|
113 } |
|
114 System.out.println("Unexpected event: " + e.getEventType().getName()); |
|
115 }); |
|
116 |
|
117 rs.startAsync(); |
|
118 |
|
119 try (Recording r1 = new Recording()) { |
|
120 r1.start(); |
|
121 MouseEvent me = new MouseEvent(); |
|
122 me.commit(); |
|
123 System.out.println("Mouse emitted"); |
|
124 mouseLatch.await(); |
|
125 try (Recording r2 = new Recording()) { // force chunk rotation in stream |
|
126 r2.start(); |
|
127 DogEvent de = new DogEvent(); |
|
128 de.commit(); |
|
129 System.out.println("Dog emitted"); |
|
130 dogLatch.await(); |
|
131 CatEvent ce = new CatEvent(); |
|
132 ce.commit(); |
|
133 System.out.println("Cat emitted"); |
|
134 catLatch.await(); |
|
135 zebraLatch.await(); |
|
136 acknowledgeFlushEvent(); |
|
137 } |
|
138 } |
|
139 } |
|
140 } |
|
141 |
|
142 private static void printEvent(RecordedEvent re) { |
|
143 System.out.println(re.getEventType().getName()); |
|
144 System.out.println(re.getStartTime().toEpochMilli()); |
|
145 System.out.println(re.getEndTime().toEpochMilli()); |
|
146 } |
|
147 |
|
148 private static void printFlushEvent(RecordedEvent re) { |
|
149 printEvent(re); |
|
150 System.out.println("flushID: " + (long) re.getValue("flushId")); |
|
151 System.out.println("elements: " + (long) re.getValue("elements")); |
|
152 System.out.println("size: " + (long) re.getValue("size")); |
|
153 } |
|
154 |
|
155 private static void validateFlushEvent(RecordedEvent re) { |
|
156 printFlushEvent(re); |
|
157 Asserts.assertTrue(re.getEventType().getName().contains("Flush"), "invalid Event type"); |
|
158 Asserts.assertGT((long) re.getValue("flushId"), 0L, "Invalid flush ID"); |
|
159 Asserts.assertGT((long) re.getValue("elements"), 0L, "No elements"); |
|
160 Asserts.assertGT((long) re.getValue("size"), 0L, "Empty size"); |
|
161 } |
|
162 |
|
163 private static void acknowledgeFlushEvent() { |
|
164 Asserts.assertTrue(flushEventAck, "No Flush event"); |
|
165 } |
|
166 } |