author | egahlin |
Sat, 26 Oct 2019 23:59:51 +0200 | |
branch | JEP-349-branch |
changeset 58806 | a7d850b47b19 |
parent 58571 | 3d823192d9ca |
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 |
||
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
26 |
package jdk.jfr.internal.consumer; |
57361 | 27 |
|
28 |
import java.io.IOException; |
|
57386 | 29 |
import java.nio.file.Path; |
57361 | 30 |
import java.security.AccessControlContext; |
31 |
import java.time.Instant; |
|
57386 | 32 |
import java.util.Arrays; |
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
33 |
import java.util.Comparator; |
57361 | 34 |
import java.util.Objects; |
35 |
||
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
36 |
import jdk.jfr.consumer.RecordedEvent; |
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58197
diff
changeset
|
37 |
import jdk.jfr.internal.JVM; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
38 |
import jdk.jfr.internal.Utils; |
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
39 |
import jdk.jfr.internal.consumer.ChunkParser.ParserConfiguration; |
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 |
*/ |
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
46 |
public final class EventDirectoryStream extends AbstractEventStream { |
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
47 |
|
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
48 |
private final static Comparator<? super RecordedEvent> EVENT_COMPARATOR = JdkJfrConsumer.instance().eventComparator(); |
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
49 |
|
57985 | 50 |
private final RepositoryFiles repositoryFiles; |
51 |
private final boolean active; |
|
52 |
private final FileAccess fileAccess; |
|
58129 | 53 |
|
58197 | 54 |
private ChunkParser currentParser; |
55 |
private long currentChunkStartNanos; |
|
56 |
private RecordedEvent[] sortedCache; |
|
58571 | 57 |
private int threadExclusionLevel = 0; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
58 |
|
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
59 |
public EventDirectoryStream(AccessControlContext acc, Path p, FileAccess fileAccess, boolean active) throws IOException { |
57985 | 60 |
super(acc, active); |
61 |
this.fileAccess = Objects.requireNonNull(fileAccess); |
|
62 |
this.active = active; |
|
63 |
this.repositoryFiles = new RepositoryFiles(fileAccess, p); |
|
57361 | 64 |
} |
65 |
||
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
66 |
@Override |
57361 | 67 |
public void close() { |
57985 | 68 |
setClosed(true); |
58129 | 69 |
dispatcher().runCloseActions(); |
57985 | 70 |
repositoryFiles.close(); |
57361 | 71 |
} |
72 |
||
73 |
@Override |
|
74 |
public void start() { |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
75 |
start(Utils.timeToNanos(Instant.now())); |
57361 | 76 |
} |
77 |
||
78 |
@Override |
|
79 |
public void startAsync() { |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57640
diff
changeset
|
80 |
startAsync(Utils.timeToNanos(Instant.now())); |
57361 | 81 |
} |
82 |
||
83 |
@Override |
|
58153 | 84 |
protected void process() throws IOException { |
58571 | 85 |
JVM jvm = JVM.getJVM(); |
86 |
Thread t = Thread.currentThread(); |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58197
diff
changeset
|
87 |
try { |
58571 | 88 |
if (jvm.isExcluded(t)) { |
89 |
threadExclusionLevel++; |
|
90 |
} else { |
|
91 |
jvm.exclude(t); |
|
92 |
} |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58197
diff
changeset
|
93 |
processRecursionSafe(); |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58197
diff
changeset
|
94 |
} finally { |
58571 | 95 |
if (threadExclusionLevel > 0) { |
96 |
threadExclusionLevel--; |
|
97 |
} else { |
|
98 |
jvm.include(t); |
|
99 |
} |
|
58445
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58197
diff
changeset
|
100 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58197
diff
changeset
|
101 |
} |
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58197
diff
changeset
|
102 |
|
1893a674db04
Add TestRecusrive and only skip events for DirectoryStream
egahlin
parents:
58197
diff
changeset
|
103 |
protected void processRecursionSafe() throws IOException { |
58129 | 104 |
Dispatcher disp = dispatcher(); |
105 |
||
57985 | 106 |
Path path; |
58129 | 107 |
boolean validStartTime = active || disp.startTime != null; |
57985 | 108 |
if (validStartTime) { |
58129 | 109 |
path = repositoryFiles.firstPath(disp.startNanos); |
57985 | 110 |
} else { |
111 |
path = repositoryFiles.lastPath(); |
|
112 |
} |
|
113 |
if (path == null) { // closed |
|
114 |
return; |
|
115 |
} |
|
58197 | 116 |
currentChunkStartNanos = repositoryFiles.getTimestamp(path); |
57985 | 117 |
try (RecordingInput input = new RecordingInput(path.toFile(), fileAccess)) { |
58197 | 118 |
currentParser = new ChunkParser(input, disp.parserConfiguration); |
119 |
long segmentStart = currentParser.getStartNanos() + currentParser.getChunkDuration(); |
|
58129 | 120 |
long filterStart = validStartTime ? disp.startNanos : segmentStart; |
121 |
long filterEnd = disp.endTime != null ? disp.endNanos: Long.MAX_VALUE; |
|
122 |
||
57985 | 123 |
while (!isClosed()) { |
124 |
boolean awaitnewEvent = false; |
|
58197 | 125 |
while (!isClosed() && !currentParser.isChunkFinished()) { |
58129 | 126 |
disp = dispatcher(); |
127 |
ParserConfiguration pc = disp.parserConfiguration; |
|
128 |
pc.filterStart = filterStart; |
|
129 |
pc.filterEnd = filterEnd; |
|
58197 | 130 |
currentParser.updateConfiguration(pc, true); |
131 |
currentParser.setFlushOperation(getFlushOperation()); |
|
132 |
if (pc.isOrdered()) { |
|
58129 | 133 |
awaitnewEvent = processOrdered(disp, awaitnewEvent); |
57985 | 134 |
} else { |
58129 | 135 |
awaitnewEvent = processUnordered(disp, awaitnewEvent); |
57985 | 136 |
} |
58197 | 137 |
if (currentParser.getStartNanos() + currentParser.getChunkDuration() > filterEnd) { |
57985 | 138 |
close(); |
139 |
return; |
|
140 |
} |
|
141 |
} |
|
57361 | 142 |
|
57985 | 143 |
if (isClosed()) { |
144 |
return; |
|
145 |
} |
|
58197 | 146 |
long durationNanos = currentParser.getChunkDuration(); |
58806 | 147 |
if (durationNanos == 0) { |
148 |
// Avoid reading the same chunk again and again if |
|
149 |
// duration is 0 ns |
|
150 |
durationNanos++; |
|
151 |
} |
|
58197 | 152 |
path = repositoryFiles.nextPath(currentChunkStartNanos + durationNanos); |
57985 | 153 |
if (path == null) { |
154 |
return; // stream closed |
|
155 |
} |
|
58197 | 156 |
currentChunkStartNanos = repositoryFiles.getTimestamp(path); |
57985 | 157 |
input.setFile(path); |
58197 | 158 |
currentParser = currentParser.newChunkParser(); |
57985 | 159 |
// TODO: Optimization. No need filter when we reach new chunk |
160 |
// Could set start = 0; |
|
161 |
} |
|
162 |
} |
|
57361 | 163 |
} |
57376 | 164 |
|
58129 | 165 |
private boolean processOrdered(Dispatcher c, boolean awaitNewEvents) throws IOException { |
58197 | 166 |
if (sortedCache == null) { |
167 |
sortedCache = new RecordedEvent[100_000]; |
|
57985 | 168 |
} |
169 |
int index = 0; |
|
170 |
while (true) { |
|
58197 | 171 |
RecordedEvent e = currentParser.readStreamingEvent(awaitNewEvents); |
57985 | 172 |
if (e == null) { |
173 |
// wait for new event with next call to |
|
174 |
// readStreamingEvent() |
|
175 |
awaitNewEvents = true; |
|
176 |
break; |
|
177 |
} |
|
178 |
awaitNewEvents = false; |
|
58197 | 179 |
if (index == sortedCache.length) { |
180 |
sortedCache = Arrays.copyOf(sortedCache, sortedCache.length * 2); |
|
57985 | 181 |
} |
58197 | 182 |
sortedCache[index++] = e; |
57985 | 183 |
} |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
184 |
|
57985 | 185 |
// no events found |
58197 | 186 |
if (index == 0 && currentParser.isChunkFinished()) { |
57985 | 187 |
return awaitNewEvents; |
188 |
} |
|
189 |
// at least 2 events, sort them |
|
190 |
if (index > 1) { |
|
58197 | 191 |
Arrays.sort(sortedCache, 0, index, EVENT_COMPARATOR); |
57985 | 192 |
} |
193 |
for (int i = 0; i < index; i++) { |
|
58197 | 194 |
c.dispatch(sortedCache[i]); |
57985 | 195 |
} |
196 |
return awaitNewEvents; |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
197 |
} |
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
198 |
|
58129 | 199 |
private boolean processUnordered(Dispatcher c, boolean awaitNewEvents) throws IOException { |
57985 | 200 |
while (true) { |
58197 | 201 |
RecordedEvent e = currentParser.readStreamingEvent(awaitNewEvents); |
57985 | 202 |
if (e == null) { |
203 |
return true; |
|
204 |
} else { |
|
58129 | 205 |
c.dispatch(e); |
57985 | 206 |
} |
207 |
} |
|
57604
838f9a7635b6
Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
57452
diff
changeset
|
208 |
} |
57361 | 209 |
} |