author | egahlin |
Fri, 05 Jul 2019 03:36:40 +0200 | |
branch | JEP-349-branch |
changeset 57452 | 6fabe73e5d9a |
parent 57449 | 099789ceff7d |
child 57604 | 838f9a7635b6 |
permissions | -rw-r--r-- |
57361 | 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 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
28 |
import java.io.IOException; |
57361 | 29 |
import java.nio.file.Path; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
30 |
import java.security.AccessControlContext; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
31 |
import java.security.AccessController; |
57361 | 32 |
import java.time.Duration; |
57449 | 33 |
import java.time.Instant; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
34 |
import java.util.Arrays; |
57361 | 35 |
import java.util.Objects; |
36 |
import java.util.function.Consumer; |
|
37 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
38 |
import jdk.jfr.internal.consumer.RecordingInput; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
39 |
|
57361 | 40 |
/** |
41 |
* Implementation of an event stream that operates against a recording file. |
|
42 |
* |
|
43 |
*/ |
|
44 |
final class EventFileStream implements EventStream { |
|
45 |
||
57449 | 46 |
private final static class FileConsumer extends EventConsumer { |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
47 |
private static final int DEFAULT_ARRAY_SIZE = 100_000; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
48 |
private final RecordingInput input; |
57374 | 49 |
private ChunkParser chunkParser; |
57376 | 50 |
private boolean reuse = true; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
51 |
private RecordedEvent[] sortedList; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
52 |
private boolean ordered; |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
53 |
|
57449 | 54 |
public FileConsumer(AccessControlContext acc, RecordingInput input) throws IOException { |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
55 |
super(acc); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
56 |
this.input = input; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
57 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
58 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
59 |
@Override |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
60 |
public void process() throws IOException { |
57376 | 61 |
chunkParser = new ChunkParser(input, reuse); |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
62 |
while (!isClosed()) { |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
63 |
boolean reuse = this.reuse; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
64 |
boolean ordered = this.ordered; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
65 |
chunkParser.setReuse(reuse); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
66 |
chunkParser.setOrdered(ordered); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
67 |
chunkParser.resetEventCache(); |
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
68 |
chunkParser.setParserFilter(eventFilter); |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
69 |
chunkParser.updateEventParsers(); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
70 |
if (ordered) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
71 |
processOrdered(); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
72 |
} else { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
73 |
processUnordered(); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
74 |
} |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
75 |
if (chunkParser.isLastChunk()) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
76 |
return; |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
77 |
} |
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
78 |
runFlushActions(); |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
79 |
chunkParser = chunkParser.nextChunkParser(); |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
80 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
81 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
82 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
83 |
private void processOrdered() throws IOException { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
84 |
if (sortedList == null) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
85 |
sortedList = new RecordedEvent[DEFAULT_ARRAY_SIZE]; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
86 |
} |
57376 | 87 |
RecordedEvent event; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
88 |
int index = 0; |
57376 | 89 |
while (true) { |
57374 | 90 |
event = chunkParser.readEvent(); |
91 |
if (event == null) { |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
92 |
Arrays.sort(sortedList, 0, index, END_TIME); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
93 |
for (int i = 0; i < index; i++) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
94 |
dispatch(sortedList[i]); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
95 |
} |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
96 |
return; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
97 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
98 |
if (index == sortedList.length) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
99 |
RecordedEvent[] tmp = sortedList; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
100 |
sortedList = new RecordedEvent[2 * tmp.length]; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
101 |
System.arraycopy(tmp, 0, sortedList, 0, tmp.length); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
102 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
103 |
sortedList[index++] = event; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
104 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
105 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
106 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
107 |
private void processUnordered() throws IOException { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
108 |
RecordedEvent event; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
109 |
while (!isClosed()) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
110 |
event = chunkParser.readEvent(); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
111 |
if (event == null) { |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
112 |
return; |
57374 | 113 |
} |
114 |
dispatch(event); |
|
115 |
} |
|
116 |
} |
|
117 |
||
57376 | 118 |
public void setReuse(boolean reuse) { |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
119 |
this.reuse = reuse; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
120 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
121 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
122 |
public void setOrdered(boolean ordered) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
123 |
this.ordered = ordered; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
124 |
} |
57386 | 125 |
|
126 |
@Override |
|
127 |
public void close() { |
|
128 |
||
129 |
} |
|
57449 | 130 |
|
131 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
132 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
133 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
134 |
private final RecordingInput input; |
57449 | 135 |
private final FileConsumer eventConsumer; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
136 |
|
57449 | 137 |
public EventFileStream(Path path, Instant from, Instant to) throws IOException { |
57361 | 138 |
Objects.requireNonNull(path); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
139 |
input = new RecordingInput(path.toFile()); |
57449 | 140 |
eventConsumer = new FileConsumer(AccessController.getContext(), input); |
57361 | 141 |
} |
142 |
||
143 |
@Override |
|
144 |
public void onEvent(Consumer<RecordedEvent> action) { |
|
145 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
146 |
eventConsumer.onEvent(action); |
57361 | 147 |
} |
148 |
||
149 |
@Override |
|
150 |
public void onEvent(String eventName, Consumer<RecordedEvent> action) { |
|
151 |
Objects.requireNonNull(eventName); |
|
152 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
153 |
eventConsumer.onEvent(eventName, action); |
57361 | 154 |
} |
155 |
||
156 |
@Override |
|
157 |
public void onFlush(Runnable action) { |
|
158 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
159 |
eventConsumer.onFlush(action); |
57361 | 160 |
} |
161 |
||
162 |
@Override |
|
163 |
public void onClose(Runnable action) { |
|
164 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
165 |
eventConsumer.addCloseAction(action); |
57361 | 166 |
} |
167 |
||
168 |
@Override |
|
169 |
public void close() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
170 |
eventConsumer.setClosed(true); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
171 |
eventConsumer.runCloseActions(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
172 |
try { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
173 |
input.close(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
174 |
} catch (IOException e) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
175 |
// ignore |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
176 |
} |
57361 | 177 |
} |
178 |
||
179 |
@Override |
|
180 |
public boolean remove(Object action) { |
|
181 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
182 |
return eventConsumer.remove(action); |
57361 | 183 |
} |
184 |
||
185 |
@Override |
|
186 |
public void start() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
187 |
eventConsumer.start(0); |
57361 | 188 |
} |
189 |
||
57433 | 190 |
@Override |
57376 | 191 |
public void setReuse(boolean reuse) { |
192 |
eventConsumer.setReuse(reuse); |
|
193 |
} |
|
194 |
||
57361 | 195 |
@Override |
196 |
public void startAsync() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
197 |
eventConsumer.startAsync(0); |
57361 | 198 |
} |
199 |
||
200 |
@Override |
|
201 |
public void awaitTermination(Duration timeout) { |
|
202 |
Objects.requireNonNull(timeout); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
203 |
eventConsumer.awaitTermination(timeout); |
57361 | 204 |
} |
205 |
||
206 |
@Override |
|
207 |
public void awaitTermination() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
208 |
eventConsumer.awaitTermination(); |
57361 | 209 |
} |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
210 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
211 |
@Override |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
212 |
public void setOrdered(boolean ordered) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
213 |
eventConsumer.setOrdered(ordered); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
214 |
} |
57449 | 215 |
|
216 |
@Override |
|
217 |
public void setStartTime(Instant startTime) { |
|
218 |
eventConsumer.setStartTime(startTime); |
|
219 |
} |
|
57361 | 220 |
} |