Skip to content

Commit dfb15c3

Browse files
committedDec 22, 2021
8274315: JFR: One closed state per file or stream
Reviewed-by: mgronlun
1 parent e49d4a9 commit dfb15c3

File tree

6 files changed

+62
-22
lines changed

6 files changed

+62
-22
lines changed
 

‎src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordingFile.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import jdk.jfr.internal.consumer.ChunkHeader;
4242
import jdk.jfr.internal.consumer.ChunkParser;
4343
import jdk.jfr.internal.consumer.FileAccess;
44+
import jdk.jfr.internal.consumer.ParserState;
4445
import jdk.jfr.internal.consumer.RecordingInput;
4546

4647
/**
@@ -61,6 +62,7 @@
6162
*/
6263
public final class RecordingFile implements Closeable {
6364

65+
private final ParserState parserState = new ParserState();
6466
private boolean isLastEventInChunk;
6567
private final File file;
6668
private RecordingInput input;
@@ -247,7 +249,7 @@ boolean isLastEventInChunk() {
247249
private void findNext() throws IOException {
248250
while (nextEvent == null) {
249251
if (chunkParser == null) {
250-
chunkParser = new ChunkParser(input);
252+
chunkParser = new ChunkParser(input, parserState);
251253
} else if (!chunkParser.isLastChunk()) {
252254
chunkParser = chunkParser.nextChunkParser();
253255
} else {

‎src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/AbstractEventStream.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public abstract class AbstractEventStream implements EventStream {
6565
private volatile Thread thread;
6666
private Dispatcher dispatcher;
6767

68-
private volatile boolean closed;
68+
protected final ParserState parserState = new ParserState();
6969

7070
private boolean daemon = false;
7171

@@ -215,12 +215,12 @@ public final void awaitTermination(Duration timeout) throws InterruptedException
215215

216216
protected abstract void process() throws IOException;
217217

218-
protected final void setClosed(boolean closed) {
219-
this.closed = closed;
218+
protected final void closeParser() {
219+
parserState.close();
220220
}
221221

222222
protected final boolean isClosed() {
223-
return closed;
223+
return parserState.isClosed();
224224
}
225225

226226
public final void startAsync(long startNanos) {

‎src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/ChunkParser.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,32 @@ private boolean is(int flags) {
9797
private final RecordingInput input;
9898
private final ChunkHeader chunkHeader;
9999
private final TimeConverter timeConverter;
100-
100+
private final ParserState parserState;
101101
private final LongMap<ConstantLookup> constantLookups;
102102

103103
private LongMap<Type> typeMap;
104104
private LongMap<Parser> parsers;
105105
private boolean chunkFinished;
106106

107107
private ParserConfiguration configuration;
108-
private volatile boolean closed;
109108
private MetadataDescriptor previousMetadata;
110109
private MetadataDescriptor metadata;
111110
private boolean staleMetadata = true;
112111

113-
public ChunkParser(RecordingInput input) throws IOException {
114-
this(input, new ParserConfiguration());
112+
public ChunkParser(RecordingInput input, ParserState ps) throws IOException {
113+
this(input, new ParserConfiguration(), ps);
115114
}
116115

117-
ChunkParser(RecordingInput input, ParserConfiguration pc) throws IOException {
118-
this(new ChunkHeader(input), null, pc);
116+
ChunkParser(RecordingInput input, ParserConfiguration pc, ParserState ps) throws IOException {
117+
this(new ChunkHeader(input), null, pc, ps);
119118
}
120119

121-
private ChunkParser(ChunkParser previous) throws IOException {
122-
this(new ChunkHeader(previous.input), previous, new ParserConfiguration());
120+
private ChunkParser(ChunkParser previous, ParserState ps) throws IOException {
121+
this(new ChunkHeader(previous.input), previous, new ParserConfiguration(), ps);
123122
}
124123

125-
private ChunkParser(ChunkHeader header, ChunkParser previous, ParserConfiguration pc) throws IOException {
124+
private ChunkParser(ChunkHeader header, ChunkParser previous, ParserConfiguration pc, ParserState ps) throws IOException {
125+
this.parserState = ps;
126126
this.configuration = pc;
127127
this.input = header.getInput();
128128
this.chunkHeader = header;
@@ -155,7 +155,7 @@ private ChunkParser(ChunkHeader header, ChunkParser previous, ParserConfiguratio
155155
}
156156

157157
public ChunkParser nextChunkParser() throws IOException {
158-
return new ChunkParser(chunkHeader.nextHeader(), this, configuration);
158+
return new ChunkParser(chunkHeader.nextHeader(), this, configuration, parserState);
159159
}
160160

161161
private void updateConfiguration() {
@@ -280,7 +280,7 @@ private boolean awaitUpdatedHeader(long absoluteChunkEnd, long filterEnd) throws
280280
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Waiting for more data (streaming). Read so far: " + chunkHeader.getChunkSize() + " bytes");
281281
}
282282
while (true) {
283-
if (closed) {
283+
if (parserState.isClosed()) {
284284
return true;
285285
}
286286
if (chunkHeader.getLastNanos() > filterEnd) {
@@ -437,7 +437,7 @@ public boolean isLastChunk() throws IOException {
437437
}
438438

439439
ChunkParser newChunkParser() throws IOException {
440-
return new ChunkParser(this);
440+
return new ChunkParser(this, parserState);
441441
}
442442

443443
public boolean isChunkFinished() {
@@ -457,7 +457,7 @@ public boolean isFinalChunk() {
457457
}
458458

459459
public void close() {
460-
this.closed = true;
460+
parserState.close();
461461
try {
462462
input.close();
463463
} catch(IOException e) {

‎src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/EventDirectoryStream.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public EventDirectoryStream(
8383

8484
@Override
8585
public void close() {
86-
setClosed(true);
86+
closeParser();
8787
dispatcher().runCloseActions();
8888
repositoryFiles.close();
8989
if (currentParser != null) {
@@ -148,7 +148,7 @@ protected void processRecursionSafe() throws IOException {
148148
}
149149
currentChunkStartNanos = repositoryFiles.getTimestamp(path);
150150
try (RecordingInput input = new RecordingInput(path.toFile(), fileAccess)) {
151-
currentParser = new ChunkParser(input, disp.parserConfiguration);
151+
currentParser = new ChunkParser(input, disp.parserConfiguration, parserState);
152152
long segmentStart = currentParser.getStartNanos() + currentParser.getChunkDuration();
153153
long filterStart = validStartTime ? disp.startNanos : segmentStart;
154154
long filterEnd = disp.endTime != null ? disp.endNanos : Long.MAX_VALUE;

‎src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/EventFileStream.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void startAsync() {
6464

6565
@Override
6666
public void close() {
67-
setClosed(true);
67+
closeParser();
6868
dispatcher().runCloseActions();
6969
try {
7070
input.close();
@@ -85,7 +85,7 @@ protected void process() throws IOException {
8585
end = disp.endNanos;
8686
}
8787

88-
currentParser = new ChunkParser(input, disp.parserConfiguration);
88+
currentParser = new ChunkParser(input, disp.parserConfiguration, parserState);
8989
while (!isClosed()) {
9090
onMetadata(currentParser);
9191
if (currentParser.getStartNanos() > end) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2021, 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+
public final class ParserState {
29+
private volatile boolean closed;
30+
31+
public boolean isClosed() {
32+
return closed;
33+
}
34+
35+
public void close() {
36+
closed = true;
37+
}
38+
}

0 commit comments

Comments
 (0)
Please sign in to comment.