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