author | egahlin |
Mon, 16 Sep 2019 09:45:22 +0200 | |
branch | JEP-349-branch |
changeset 58145 | bc54ed8d908a |
parent 58129 | src/jdk.jfr/share/classes/jdk/jfr/consumer/EventFileStream.java@7b751fe181a5 |
child 58197 | 0ef79bd7fb5c |
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 |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
28 |
import java.io.IOException; |
57361 | 29 |
import java.nio.file.Path; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
30 |
import java.security.AccessControlContext; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
31 |
import java.util.Arrays; |
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
32 |
import java.util.Comparator; |
57361 | 33 |
import java.util.Objects; |
34 |
||
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
35 |
import jdk.jfr.consumer.RecordedEvent; |
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
36 |
import jdk.jfr.internal.consumer.Dispatcher; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57604
diff
changeset
|
37 |
import jdk.jfr.internal.consumer.FileAccess; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
38 |
import jdk.jfr.internal.consumer.RecordingInput; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
39 |
|
57361 | 40 |
/** |
41 |
* Implementation of an event stream that operates against a recording file. |
|
42 |
* |
|
43 |
*/ |
|
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
44 |
public final class EventFileStream extends AbstractEventStream { |
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
45 |
private final static Comparator<? super RecordedEvent> EVENT_COMPARATOR = JdkJfrConsumer.instance().eventComparator(); |
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
46 |
|
57985 | 47 |
private final RecordingInput input; |
48 |
private ChunkParser chunkParser; |
|
49 |
private RecordedEvent[] sortedList; |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57604
diff
changeset
|
50 |
|
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
51 |
public EventFileStream(AccessControlContext acc, Path path) throws IOException { |
57985 | 52 |
super(acc, false); |
57361 | 53 |
Objects.requireNonNull(path); |
57985 | 54 |
this.input = new RecordingInput(path.toFile(), FileAccess.UNPRIVILIGED); |
57361 | 55 |
} |
56 |
||
57 |
@Override |
|
57985 | 58 |
public void start() { |
59 |
start(0); |
|
57361 | 60 |
} |
61 |
||
62 |
@Override |
|
57985 | 63 |
public void startAsync() { |
64 |
startAsync(0); |
|
57361 | 65 |
} |
66 |
||
67 |
@Override |
|
68 |
public void close() { |
|
57985 | 69 |
setClosed(true); |
58129 | 70 |
dispatcher().runCloseActions(); |
57985 | 71 |
try { |
72 |
input.close(); |
|
73 |
} catch (IOException e) { |
|
74 |
// ignore |
|
75 |
} |
|
57361 | 76 |
} |
77 |
||
78 |
@Override |
|
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
79 |
protected void process() throws IOException { |
58129 | 80 |
Dispatcher disp = dispatcher(); |
57985 | 81 |
long start = 0; |
82 |
long end = Long.MAX_VALUE; |
|
58129 | 83 |
if (disp.startTime != null) { |
84 |
start = disp.startNanos; |
|
57985 | 85 |
} |
58129 | 86 |
if (disp.endTime != null) { |
87 |
end = disp.endNanos; |
|
57985 | 88 |
} |
57361 | 89 |
|
58129 | 90 |
chunkParser = new ChunkParser(input, disp.parserConfiguration); |
57985 | 91 |
while (!isClosed()) { |
92 |
if (chunkParser.getStartNanos() > end) { |
|
93 |
close(); |
|
94 |
return; |
|
95 |
} |
|
58129 | 96 |
disp = dispatcher(); |
97 |
disp.parserConfiguration.filterStart = start; |
|
98 |
disp.parserConfiguration.filterEnd = end; |
|
99 |
chunkParser.updateConfiguration(disp.parserConfiguration, true); |
|
57985 | 100 |
chunkParser.setFlushOperation(getFlushOperation()); |
58129 | 101 |
if (disp.parserConfiguration.ordered) { |
102 |
processOrdered(disp); |
|
57985 | 103 |
} else { |
58129 | 104 |
processUnordered(disp); |
57985 | 105 |
} |
58129 | 106 |
if (isClosed() || chunkParser.isLastChunk()) { |
57985 | 107 |
return; |
108 |
} |
|
109 |
chunkParser = chunkParser.nextChunkParser(); |
|
110 |
} |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
111 |
} |
57449 | 112 |
|
58129 | 113 |
private void processOrdered(Dispatcher c) throws IOException { |
57985 | 114 |
if (sortedList == null) { |
115 |
sortedList = new RecordedEvent[10_000]; |
|
116 |
} |
|
117 |
RecordedEvent event; |
|
118 |
int index = 0; |
|
119 |
while (true) { |
|
120 |
event = chunkParser.readEvent(); |
|
121 |
if (event == null) { |
|
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
122 |
Arrays.sort(sortedList, 0, index, EVENT_COMPARATOR); |
57985 | 123 |
for (int i = 0; i < index; i++) { |
58129 | 124 |
c.dispatch(sortedList[i]); |
57985 | 125 |
} |
126 |
return; |
|
127 |
} |
|
128 |
if (index == sortedList.length) { |
|
129 |
RecordedEvent[] tmp = sortedList; |
|
130 |
sortedList = new RecordedEvent[2 * tmp.length]; |
|
131 |
System.arraycopy(tmp, 0, sortedList, 0, tmp.length); |
|
132 |
} |
|
133 |
sortedList[index++] = event; |
|
134 |
} |
|
57449 | 135 |
} |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57604
diff
changeset
|
136 |
|
58129 | 137 |
private void processUnordered(Dispatcher c) throws IOException { |
57985 | 138 |
while (!isClosed()) { |
139 |
RecordedEvent event = chunkParser.readEvent(); |
|
140 |
if (event == null) { |
|
141 |
return; |
|
142 |
} |
|
58129 | 143 |
c.dispatch(event); |
57985 | 144 |
} |
57971 | 145 |
} |
57361 | 146 |
} |