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