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