|
1 | 1 | package io.quarkus.test.junit5.virtual.internal; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | 3 | import java.util.List; |
5 | | -import java.util.UUID; |
6 | | -import java.util.concurrent.CopyOnWriteArrayList; |
7 | | -import java.util.concurrent.CountDownLatch; |
8 | | -import java.util.concurrent.TimeUnit; |
9 | 4 | import java.util.function.Consumer; |
10 | | -import java.util.function.Function; |
11 | | -import java.util.logging.Level; |
12 | | -import java.util.logging.Logger; |
13 | 5 |
|
14 | | -import io.smallrye.common.annotation.SuppressForbidden; |
15 | 6 | import jdk.jfr.consumer.RecordedEvent; |
16 | | -import jdk.jfr.consumer.RecordingStream; |
17 | 7 |
|
18 | | -public class Collector implements Consumer<RecordedEvent> { |
19 | | - public static final String CARRIER_PINNED_EVENT_NAME = "jdk.VirtualThreadPinned"; |
20 | | - private static final Logger LOGGER = Logger.getLogger(Collector.class.getName()); |
| 8 | +public interface Collector extends Consumer<RecordedEvent> { |
21 | 9 |
|
22 | | - private final List<Function<RecordedEvent, Boolean>> observers = new CopyOnWriteArrayList<>(); |
| 10 | + String CARRIER_PINNED_EVENT_NAME = "jdk.VirtualThreadPinned"; |
23 | 11 |
|
24 | | - private final List<RecordedEvent> events = new CopyOnWriteArrayList<>(); |
25 | | - |
26 | | - private final RecordingStream recordingStream; |
27 | | - |
28 | | - volatile State state = State.INIT; |
29 | | - |
30 | | - public Collector() { |
31 | | - recordingStream = new RecordingStream(); |
32 | | - recordingStream.enable(CARRIER_PINNED_EVENT_NAME).withStackTrace(); |
33 | | - recordingStream.enable(InternalEvents.SHUTDOWN_EVENT_NAME).withoutStackTrace(); |
34 | | - recordingStream.enable(InternalEvents.CAPTURING_STARTED_EVENT_NAME).withoutStackTrace(); |
35 | | - recordingStream.enable(InternalEvents.CAPTURING_STOPPED_EVENT_NAME).withoutStackTrace(); |
36 | | - recordingStream.enable(InternalEvents.INITIALIZATION_EVENT_NAME).withoutStackTrace(); |
37 | | - recordingStream.setOrdered(true); |
38 | | - recordingStream.setMaxSize(100); |
39 | | - recordingStream.onEvent(this); |
40 | | - } |
41 | | - |
42 | | - @SuppressForbidden(reason = "java.util.logging is authorized here") |
43 | | - public void init() { |
44 | | - long begin = System.nanoTime(); |
45 | | - CountDownLatch latch = new CountDownLatch(1); |
46 | | - observers.add(re -> { |
47 | | - if (re.getEventType().getName().equals(InternalEvents.INITIALIZATION_EVENT_NAME)) { |
48 | | - latch.countDown(); |
49 | | - return true; |
50 | | - } |
51 | | - return false; |
52 | | - }); |
53 | | - recordingStream.startAsync(); |
54 | | - new InternalEvents.InitializationEvent().commit(); |
55 | | - try { |
56 | | - if (latch.await(10, TimeUnit.SECONDS)) { |
57 | | - long end = System.nanoTime(); |
58 | | - state = State.STARTED; |
59 | | - LOGGER.log(Level.FINE, "Event collection started in {0}s", (end - begin) / 1000000); |
60 | | - } else { |
61 | | - throw new IllegalStateException( |
62 | | - "Unable to start JFR collection, RecordingStartedEvent event not received after 10s"); |
63 | | - } |
64 | | - } catch (InterruptedException e) { |
65 | | - Thread.currentThread().interrupt(); |
66 | | - throw new RuntimeException(e); |
| 12 | + static Collector create() { |
| 13 | + if (Boolean.getBoolean("jfr.unsupported.vm")) { |
| 14 | + return new NoJfrCollector(); |
67 | 15 | } |
68 | | - } |
69 | | - |
70 | | - @SuppressForbidden(reason = "java.util.logging is authorized here") |
71 | | - public void start() { |
72 | | - CountDownLatch latch = new CountDownLatch(1); |
73 | | - String id = UUID.randomUUID().toString(); |
74 | | - long begin = System.nanoTime(); |
75 | | - observers.add(re -> { |
76 | | - if (re.getEventType().getName().equals(InternalEvents.CAPTURING_STARTED_EVENT_NAME)) { |
77 | | - if (id.equals(re.getString("id"))) { |
78 | | - events.clear(); |
79 | | - state = State.COLLECTING; |
80 | | - latch.countDown(); |
81 | | - return true; |
82 | | - } |
83 | | - } |
84 | | - return false; |
85 | | - }); |
86 | 16 |
|
87 | | - new InternalEvents.CapturingStartedEvent(id).commit(); |
88 | | - |
89 | | - try { |
90 | | - if (!latch.await(10, TimeUnit.SECONDS)) { |
91 | | - throw new IllegalStateException("Unable to start JFR collection, START_EVENT event not received after 10s"); |
92 | | - } |
93 | | - long end = System.nanoTime(); |
94 | | - LOGGER.log(Level.FINE, "Event capturing started in {0}s", (end - begin) / 1000000); |
95 | | - } catch (InterruptedException e) { |
96 | | - Thread.currentThread().interrupt(); |
97 | | - throw new RuntimeException(e); |
98 | | - } |
| 17 | + return new JfrCollector(); |
99 | 18 | } |
100 | 19 |
|
101 | | - @SuppressForbidden(reason = "java.util.logging is authorized here") |
102 | | - public List<RecordedEvent> stop() { |
103 | | - CountDownLatch latch = new CountDownLatch(1); |
104 | | - String id = UUID.randomUUID().toString(); |
105 | | - var begin = System.nanoTime(); |
106 | | - observers.add(re -> { |
107 | | - if (re.getEventType().getName().equals(InternalEvents.CAPTURING_STOPPED_EVENT_NAME)) { |
108 | | - state = State.STARTED; |
109 | | - latch.countDown(); |
110 | | - return true; |
111 | | - } |
112 | | - return false; |
113 | | - }); |
| 20 | + void init(); |
114 | 21 |
|
115 | | - new InternalEvents.CapturingStoppedEvent(id).commit(); |
| 22 | + void start(); |
116 | 23 |
|
117 | | - try { |
118 | | - if (!latch.await(10, TimeUnit.SECONDS)) { |
119 | | - throw new IllegalStateException("Unable to start JFR collection, STOP_EVENT event not received after 10s"); |
120 | | - } |
121 | | - var end = System.nanoTime(); |
122 | | - LOGGER.log(Level.FINE, "Event collection stopped in {0}s", (end - begin) / 1000000); |
123 | | - return new ArrayList<>(events); |
124 | | - } catch (InterruptedException e) { |
125 | | - Thread.currentThread().interrupt(); |
126 | | - throw new RuntimeException(e); |
127 | | - } |
128 | | - } |
| 24 | + List<RecordedEvent> stop(); |
129 | 25 |
|
130 | | - @SuppressForbidden(reason = "java.util.logging is authorized here") |
131 | | - public void shutdown() { |
132 | | - CountDownLatch latch = new CountDownLatch(1); |
133 | | - var begin = System.nanoTime(); |
134 | | - observers.add(re -> { |
135 | | - if (re.getEventType().getName().equals(InternalEvents.SHUTDOWN_EVENT_NAME)) { |
136 | | - latch.countDown(); |
137 | | - return true; |
138 | | - } |
139 | | - return false; |
140 | | - }); |
141 | | - InternalEvents.ShutdownEvent event = new InternalEvents.ShutdownEvent(); |
142 | | - event.commit(); |
143 | | - try { |
144 | | - if (latch.await(10, TimeUnit.SECONDS)) { |
145 | | - state = State.INIT; |
146 | | - var end = System.nanoTime(); |
147 | | - LOGGER.log(Level.FINE, "Event collector shutdown in {0}s", (end - begin) / 1000000); |
148 | | - recordingStream.close(); |
149 | | - } else { |
150 | | - throw new IllegalStateException( |
151 | | - "Unable to stop JFR collection, RecordingStoppedEvent event not received at 10s"); |
152 | | - } |
153 | | - } catch (InterruptedException e) { |
154 | | - Thread.currentThread().interrupt(); |
155 | | - throw new RuntimeException(e); |
156 | | - } |
157 | | - } |
| 26 | + void shutdown(); |
158 | 27 |
|
159 | 28 | @Override |
160 | | - public void accept(RecordedEvent re) { |
161 | | - if (state == State.COLLECTING) { |
162 | | - events.add(re); |
163 | | - } |
164 | | - List<Function<RecordedEvent, Boolean>> toBeRemoved = new ArrayList<>(); |
165 | | - observers.forEach(c -> { |
166 | | - if (c.apply(re)) { |
167 | | - toBeRemoved.add(c); |
168 | | - } |
169 | | - }); |
170 | | - observers.removeAll(toBeRemoved); |
171 | | - } |
172 | | - |
173 | | - public List<RecordedEvent> getEvents() { |
174 | | - return new ArrayList<>(events); |
175 | | - } |
| 29 | + void accept(RecordedEvent re); |
176 | 30 |
|
177 | | - enum State { |
178 | | - INIT, |
179 | | - STARTED, |
180 | | - COLLECTING |
181 | | - } |
| 31 | + List<RecordedEvent> getEvents(); |
182 | 32 |
|
| 33 | + boolean isRecording(); |
183 | 34 | } |
0 commit comments