58863
|
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.internal.consumer;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.nio.file.Path;
|
|
30 |
import java.security.AccessControlContext;
|
|
31 |
import java.time.Instant;
|
|
32 |
import java.util.Arrays;
|
|
33 |
import java.util.Comparator;
|
|
34 |
import java.util.Objects;
|
|
35 |
|
|
36 |
import jdk.jfr.consumer.RecordedEvent;
|
|
37 |
import jdk.jfr.internal.JVM;
|
59226
|
38 |
import jdk.jfr.internal.PlatformRecording;
|
58863
|
39 |
import jdk.jfr.internal.Utils;
|
|
40 |
import jdk.jfr.internal.consumer.ChunkParser.ParserConfiguration;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Implementation of an {@code EventStream}} that operates against a directory
|
|
44 |
* with chunk files.
|
|
45 |
*
|
|
46 |
*/
|
59226
|
47 |
public class EventDirectoryStream extends AbstractEventStream {
|
58863
|
48 |
|
|
49 |
private final static Comparator<? super RecordedEvent> EVENT_COMPARATOR = JdkJfrConsumer.instance().eventComparator();
|
|
50 |
|
|
51 |
private final RepositoryFiles repositoryFiles;
|
59226
|
52 |
private final PlatformRecording recording;
|
58863
|
53 |
private final FileAccess fileAccess;
|
|
54 |
|
|
55 |
private ChunkParser currentParser;
|
|
56 |
private long currentChunkStartNanos;
|
|
57 |
private RecordedEvent[] sortedCache;
|
|
58 |
private int threadExclusionLevel = 0;
|
|
59 |
|
59226
|
60 |
public EventDirectoryStream(AccessControlContext acc, Path p, FileAccess fileAccess, PlatformRecording recording) throws IOException {
|
|
61 |
super(acc, recording);
|
58863
|
62 |
this.fileAccess = Objects.requireNonNull(fileAccess);
|
59226
|
63 |
this.recording = recording;
|
58863
|
64 |
this.repositoryFiles = new RepositoryFiles(fileAccess, p);
|
|
65 |
}
|
|
66 |
|
|
67 |
@Override
|
|
68 |
public void close() {
|
|
69 |
setClosed(true);
|
|
70 |
dispatcher().runCloseActions();
|
|
71 |
repositoryFiles.close();
|
|
72 |
}
|
|
73 |
|
|
74 |
@Override
|
|
75 |
public void start() {
|
|
76 |
start(Utils.timeToNanos(Instant.now()));
|
|
77 |
}
|
|
78 |
|
|
79 |
@Override
|
|
80 |
public void startAsync() {
|
|
81 |
startAsync(Utils.timeToNanos(Instant.now()));
|
|
82 |
}
|
|
83 |
|
|
84 |
@Override
|
|
85 |
protected void process() throws IOException {
|
|
86 |
JVM jvm = JVM.getJVM();
|
|
87 |
Thread t = Thread.currentThread();
|
|
88 |
try {
|
|
89 |
if (jvm.isExcluded(t)) {
|
|
90 |
threadExclusionLevel++;
|
|
91 |
} else {
|
|
92 |
jvm.exclude(t);
|
|
93 |
}
|
|
94 |
processRecursionSafe();
|
|
95 |
} finally {
|
|
96 |
if (threadExclusionLevel > 0) {
|
|
97 |
threadExclusionLevel--;
|
|
98 |
} else {
|
|
99 |
jvm.include(t);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
protected void processRecursionSafe() throws IOException {
|
|
105 |
Dispatcher disp = dispatcher();
|
|
106 |
|
|
107 |
Path path;
|
59226
|
108 |
boolean validStartTime = recording != null || disp.startTime != null;
|
58863
|
109 |
if (validStartTime) {
|
|
110 |
path = repositoryFiles.firstPath(disp.startNanos);
|
|
111 |
} else {
|
|
112 |
path = repositoryFiles.lastPath();
|
|
113 |
}
|
|
114 |
if (path == null) { // closed
|
|
115 |
return;
|
|
116 |
}
|
|
117 |
currentChunkStartNanos = repositoryFiles.getTimestamp(path);
|
|
118 |
try (RecordingInput input = new RecordingInput(path.toFile(), fileAccess)) {
|
|
119 |
currentParser = new ChunkParser(input, disp.parserConfiguration);
|
|
120 |
long segmentStart = currentParser.getStartNanos() + currentParser.getChunkDuration();
|
|
121 |
long filterStart = validStartTime ? disp.startNanos : segmentStart;
|
|
122 |
long filterEnd = disp.endTime != null ? disp.endNanos: Long.MAX_VALUE;
|
|
123 |
|
|
124 |
while (!isClosed()) {
|
|
125 |
boolean awaitnewEvent = false;
|
|
126 |
while (!isClosed() && !currentParser.isChunkFinished()) {
|
|
127 |
disp = dispatcher();
|
|
128 |
ParserConfiguration pc = disp.parserConfiguration;
|
|
129 |
pc.filterStart = filterStart;
|
|
130 |
pc.filterEnd = filterEnd;
|
|
131 |
currentParser.updateConfiguration(pc, true);
|
|
132 |
currentParser.setFlushOperation(getFlushOperation());
|
|
133 |
if (pc.isOrdered()) {
|
|
134 |
awaitnewEvent = processOrdered(disp, awaitnewEvent);
|
|
135 |
} else {
|
|
136 |
awaitnewEvent = processUnordered(disp, awaitnewEvent);
|
|
137 |
}
|
|
138 |
if (currentParser.getStartNanos() + currentParser.getChunkDuration() > filterEnd) {
|
|
139 |
close();
|
|
140 |
return;
|
|
141 |
}
|
|
142 |
}
|
59226
|
143 |
if (isLastChunk()) {
|
|
144 |
// Recording was stopped/closed externally, and no more data to process.
|
|
145 |
return;
|
|
146 |
}
|
58863
|
147 |
|
59226
|
148 |
if (repositoryFiles.hasFixedPath() && currentParser.isFinalChunk()) {
|
|
149 |
// JVM process exited/crashed, or repository migrated to an unknown location
|
|
150 |
return;
|
|
151 |
}
|
58863
|
152 |
if (isClosed()) {
|
59226
|
153 |
// Stream was closed
|
58863
|
154 |
return;
|
|
155 |
}
|
|
156 |
long durationNanos = currentParser.getChunkDuration();
|
|
157 |
if (durationNanos == 0) {
|
|
158 |
// Avoid reading the same chunk again and again if
|
|
159 |
// duration is 0 ns
|
|
160 |
durationNanos++;
|
|
161 |
}
|
|
162 |
path = repositoryFiles.nextPath(currentChunkStartNanos + durationNanos);
|
|
163 |
if (path == null) {
|
|
164 |
return; // stream closed
|
|
165 |
}
|
|
166 |
currentChunkStartNanos = repositoryFiles.getTimestamp(path);
|
|
167 |
input.setFile(path);
|
|
168 |
currentParser = currentParser.newChunkParser();
|
|
169 |
// TODO: Optimization. No need filter when we reach new chunk
|
|
170 |
// Could set start = 0;
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
59226
|
175 |
private boolean isLastChunk() {
|
|
176 |
if (recording == null) {
|
|
177 |
return false;
|
|
178 |
}
|
|
179 |
return recording.getFinalChunkStartNanos() >= currentParser.getStartNanos();
|
|
180 |
}
|
|
181 |
|
58863
|
182 |
private boolean processOrdered(Dispatcher c, boolean awaitNewEvents) throws IOException {
|
|
183 |
if (sortedCache == null) {
|
|
184 |
sortedCache = new RecordedEvent[100_000];
|
|
185 |
}
|
|
186 |
int index = 0;
|
|
187 |
while (true) {
|
|
188 |
RecordedEvent e = currentParser.readStreamingEvent(awaitNewEvents);
|
|
189 |
if (e == null) {
|
|
190 |
// wait for new event with next call to
|
|
191 |
// readStreamingEvent()
|
|
192 |
awaitNewEvents = true;
|
|
193 |
break;
|
|
194 |
}
|
|
195 |
awaitNewEvents = false;
|
|
196 |
if (index == sortedCache.length) {
|
|
197 |
sortedCache = Arrays.copyOf(sortedCache, sortedCache.length * 2);
|
|
198 |
}
|
|
199 |
sortedCache[index++] = e;
|
|
200 |
}
|
|
201 |
|
|
202 |
// no events found
|
|
203 |
if (index == 0 && currentParser.isChunkFinished()) {
|
|
204 |
return awaitNewEvents;
|
|
205 |
}
|
|
206 |
// at least 2 events, sort them
|
|
207 |
if (index > 1) {
|
|
208 |
Arrays.sort(sortedCache, 0, index, EVENT_COMPARATOR);
|
|
209 |
}
|
|
210 |
for (int i = 0; i < index; i++) {
|
|
211 |
c.dispatch(sortedCache[i]);
|
|
212 |
}
|
|
213 |
return awaitNewEvents;
|
|
214 |
}
|
|
215 |
|
|
216 |
private boolean processUnordered(Dispatcher c, boolean awaitNewEvents) throws IOException {
|
|
217 |
while (true) {
|
|
218 |
RecordedEvent e = currentParser.readStreamingEvent(awaitNewEvents);
|
|
219 |
if (e == null) {
|
|
220 |
return true;
|
|
221 |
} else {
|
|
222 |
c.dispatch(e);
|
|
223 |
}
|
|
224 |
}
|
|
225 |
}
|
59226
|
226 |
|
58863
|
227 |
}
|