author | egahlin |
Mon, 05 Aug 2019 19:58:56 +0200 | |
branch | JEP-349-branch |
changeset 57638 | 3b41affae2d2 |
parent 57628 | f5f590eaecf5 |
child 57640 | 46a77fccd251 |
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 |
||
28 |
import java.io.IOException; |
|
57386 | 29 |
import java.nio.file.Path; |
57361 | 30 |
import java.security.AccessControlContext; |
31 |
import java.time.Duration; |
|
32 |
import java.time.Instant; |
|
57386 | 33 |
import java.util.Arrays; |
34 |
import java.util.Comparator; |
|
57361 | 35 |
import java.util.Objects; |
36 |
import java.util.function.Consumer; |
|
37 |
||
57628 | 38 |
import jdk.jfr.internal.SecuritySupport.SafePath; |
57386 | 39 |
import jdk.jfr.internal.consumer.RecordingInput; |
57433 | 40 |
import jdk.jfr.internal.consumer.RepositoryFiles; |
57361 | 41 |
|
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57428
diff
changeset
|
42 |
/** |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57428
diff
changeset
|
43 |
* Implementation of an {@code EventStream}} that operates against a directory |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57428
diff
changeset
|
44 |
* with chunk files. |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57428
diff
changeset
|
45 |
* |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57428
diff
changeset
|
46 |
*/ |
57361 | 47 |
final class EventDirectoryStream implements EventStream { |
48 |
||
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
49 |
static final class DirectoryStream extends AbstractEventStream { |
57386 | 50 |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57449
diff
changeset
|
51 |
private static final Comparator<? super RecordedEvent> END_TIME = (e1, e2) -> Long.compare(e1.endTimeTicks, e2.endTimeTicks); |
57386 | 52 |
private static final int DEFAULT_ARRAY_SIZE = 10_000; |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
53 |
|
57386 | 54 |
private final RepositoryFiles repositoryFiles; |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
55 |
|
57386 | 56 |
private ChunkParser chunkParser; |
57 |
private RecordedEvent[] sortedList; |
|
57449 | 58 |
protected long chunkStartNanos; |
57386 | 59 |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
60 |
public DirectoryStream(AccessControlContext acc, Path p) throws IOException { |
57386 | 61 |
super(acc); |
57638 | 62 |
repositoryFiles = new RepositoryFiles(p == null ? null : new SafePath(p)); |
57386 | 63 |
} |
64 |
||
65 |
@Override |
|
66 |
public void process() throws IOException { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
67 |
StreamConfiguration c1 = configuration; |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
68 |
chunkStartNanos = c1.getStartNanos(); |
57449 | 69 |
Path path; |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
70 |
if (c1.getStartTime() == AbstractEventStream.NEXT_EVENT) { |
57449 | 71 |
// TODO: Need to skip forward to the next event |
72 |
// For now, use the last chunk. |
|
73 |
path = repositoryFiles.lastPath(); |
|
74 |
} else { |
|
75 |
path = repositoryFiles.nextPath(chunkStartNanos); |
|
76 |
} |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
77 |
if (path == null) { // closed |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
78 |
return; |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
79 |
} |
57449 | 80 |
chunkStartNanos = repositoryFiles.getTimestamp(path) + 1; |
57386 | 81 |
try (RecordingInput input = new RecordingInput(path.toFile())) { |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
82 |
chunkParser = new ChunkParser(input, c1.getReuse()); |
57386 | 83 |
while (!isClosed()) { |
57425 | 84 |
boolean awaitnewEvent = false; |
85 |
while (!isClosed() && !chunkParser.isChunkFinished()) { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
86 |
final StreamConfiguration c2 = configuration; |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
87 |
boolean ordered = c2.getOrdered(); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
88 |
chunkParser.setReuse(c2.getReuse()); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
89 |
chunkParser.setOrdered(ordered); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
90 |
chunkParser.setFirstNanos(c2.getStartNanos()); |
57425 | 91 |
chunkParser.resetEventCache(); |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
92 |
chunkParser.setParserFilter(c2.getFilter()); |
57425 | 93 |
chunkParser.updateEventParsers(); |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
94 |
clearLastDispatch(); |
57425 | 95 |
if (ordered) { |
96 |
awaitnewEvent = processOrdered(awaitnewEvent); |
|
97 |
} else { |
|
98 |
awaitnewEvent = processUnordered(awaitnewEvent); |
|
99 |
} |
|
100 |
runFlushActions(); |
|
101 |
} |
|
57628 | 102 |
if (isClosed()) { |
103 |
return; |
|
104 |
} |
|
57449 | 105 |
path = repositoryFiles.nextPath(chunkStartNanos); |
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57428
diff
changeset
|
106 |
if (path == null) { |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57428
diff
changeset
|
107 |
return; // stream closed |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57428
diff
changeset
|
108 |
} |
57449 | 109 |
chunkStartNanos = repositoryFiles.getTimestamp(path) + 1; |
57425 | 110 |
input.setFile(path); |
57427 | 111 |
chunkParser = chunkParser.newChunkParser(); |
57386 | 112 |
} |
113 |
} |
|
114 |
} |
|
115 |
||
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
116 |
|
57425 | 117 |
private boolean processOrdered(boolean awaitNewEvents) throws IOException { |
57386 | 118 |
if (sortedList == null) { |
119 |
sortedList = new RecordedEvent[DEFAULT_ARRAY_SIZE]; |
|
120 |
} |
|
57425 | 121 |
int index = 0; |
57386 | 122 |
while (true) { |
57425 | 123 |
RecordedEvent e = chunkParser.readStreamingEvent(awaitNewEvents); |
124 |
if (e == null) { |
|
125 |
// wait for new event with next call to |
|
126 |
// readStreamingEvent() |
|
127 |
awaitNewEvents = true; |
|
128 |
break; |
|
129 |
} |
|
130 |
awaitNewEvents = false; |
|
131 |
if (index == sortedList.length) { |
|
132 |
sortedList = Arrays.copyOf(sortedList, sortedList.length * 2); |
|
57386 | 133 |
} |
57425 | 134 |
sortedList[index++] = e; |
135 |
} |
|
57386 | 136 |
|
57425 | 137 |
// no events found |
138 |
if (index == 0 && chunkParser.isChunkFinished()) { |
|
139 |
return awaitNewEvents; |
|
57386 | 140 |
} |
57425 | 141 |
// at least 2 events, sort them |
142 |
if (index > 1) { |
|
143 |
Arrays.sort(sortedList, 0, index, END_TIME); |
|
144 |
} |
|
145 |
for (int i = 0; i < index; i++) { |
|
146 |
dispatch(sortedList[i]); |
|
147 |
} |
|
148 |
return awaitNewEvents; |
|
57386 | 149 |
} |
150 |
||
57425 | 151 |
private boolean processUnordered(boolean awaitNewEvents) throws IOException { |
57386 | 152 |
while (true) { |
57425 | 153 |
RecordedEvent e = chunkParser.readStreamingEvent(awaitNewEvents); |
154 |
if (e == null) { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
155 |
return true; |
57425 | 156 |
} else { |
57386 | 157 |
dispatch(e); |
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
@Override |
|
163 |
public void close() { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
164 |
setClosed(true); |
57386 | 165 |
repositoryFiles.close(); |
166 |
} |
|
167 |
} |
|
168 |
||
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
169 |
private final AbstractEventStream eventStream; |
57361 | 170 |
|
57449 | 171 |
public EventDirectoryStream(AccessControlContext acc, Path p, Instant startTime) throws IOException { |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
172 |
eventStream = new DirectoryStream(acc, p); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
173 |
eventStream.setStartTime(startTime); |
57361 | 174 |
} |
175 |
||
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
176 |
@Override |
57361 | 177 |
public void close() { |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
178 |
eventStream.close(); |
57361 | 179 |
} |
180 |
||
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
181 |
@Override |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
182 |
public void onFlush(Runnable action) { |
57361 | 183 |
Objects.requireNonNull(action); |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
184 |
eventStream.onFlush(action); |
57361 | 185 |
} |
186 |
||
187 |
@Override |
|
188 |
public void start() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
189 |
start(Instant.now().toEpochMilli() * 1000 * 1000L); |
57361 | 190 |
} |
191 |
||
192 |
@Override |
|
193 |
public void startAsync() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
194 |
startAsync(Instant.now().toEpochMilli() * 1000 * 1000L); |
57361 | 195 |
} |
196 |
||
197 |
@Override |
|
198 |
public void onEvent(Consumer<RecordedEvent> action) { |
|
199 |
Objects.requireNonNull(action); |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
200 |
eventStream.onEvent(action); |
57361 | 201 |
} |
202 |
||
203 |
@Override |
|
204 |
public void onEvent(String eventName, Consumer<RecordedEvent> action) { |
|
205 |
Objects.requireNonNull(eventName); |
|
206 |
Objects.requireNonNull(action); |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
207 |
eventStream.onEvent(eventName, action); |
57361 | 208 |
} |
209 |
||
210 |
@Override |
|
211 |
public void onClose(Runnable action) { |
|
212 |
Objects.requireNonNull(action); |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
213 |
eventStream.addCloseAction(action); |
57361 | 214 |
} |
215 |
||
216 |
@Override |
|
217 |
public boolean remove(Object action) { |
|
218 |
Objects.requireNonNull(action); |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
219 |
return eventStream.remove(action); |
57361 | 220 |
} |
221 |
||
222 |
@Override |
|
223 |
public void awaitTermination(Duration timeout) { |
|
224 |
Objects.requireNonNull(timeout); |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
225 |
eventStream.awaitTermination(timeout); |
57361 | 226 |
} |
227 |
||
228 |
@Override |
|
229 |
public void awaitTermination() { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
230 |
eventStream.awaitTermination(Duration.ofMillis(0)); |
57361 | 231 |
} |
57376 | 232 |
|
233 |
@Override |
|
234 |
public void setReuse(boolean reuse) { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
235 |
eventStream.setReuse(reuse); |
57376 | 236 |
} |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
237 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
238 |
@Override |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
239 |
public void setOrdered(boolean ordered) { |
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
240 |
eventStream.setOrdered(ordered); |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
241 |
} |
57449 | 242 |
|
243 |
@Override |
|
244 |
public void setStartTime(Instant startTime) { |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
245 |
eventStream.setStartTime(startTime); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
246 |
} |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
247 |
|
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
248 |
public void start(long startNanos) { |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
249 |
eventStream.start(startNanos); |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
250 |
} |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
251 |
|
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
252 |
public void startAsync(long startNanos) { |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
253 |
eventStream.startAsync(startNanos); |
57449 | 254 |
} |
57361 | 255 |
} |