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