author | egahlin |
Mon, 02 Sep 2019 21:08:41 +0200 | |
branch | JEP-349-branch |
changeset 57985 | be121cbf3284 |
parent 57971 | aa7b1ea52413 |
child 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 |
|
57985 | 46 |
public EventFileStream(AccessControlContext acc, Path path) throws IOException { |
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); |
65 |
runCloseActions(); |
|
66 |
try { |
|
67 |
input.close(); |
|
68 |
} catch (IOException e) { |
|
69 |
// ignore |
|
70 |
} |
|
57361 | 71 |
} |
72 |
||
73 |
@Override |
|
57985 | 74 |
public void process() throws IOException { |
75 |
StreamConfiguration c = configuration; |
|
76 |
long start = 0; |
|
77 |
long end = Long.MAX_VALUE; |
|
78 |
if (c.getStartTime() != null) { |
|
79 |
start = c.getStartNanos(); |
|
80 |
} |
|
81 |
if (c.getEndTime() != null) { |
|
82 |
end = c.getEndNanos(); |
|
83 |
} |
|
57361 | 84 |
|
57985 | 85 |
chunkParser = new ChunkParser(input, c.getReuse()); |
86 |
while (!isClosed()) { |
|
87 |
if (chunkParser.getStartNanos() > end) { |
|
88 |
close(); |
|
89 |
return; |
|
90 |
} |
|
91 |
c = configuration; |
|
92 |
boolean ordered = c.getOrdered(); |
|
93 |
chunkParser.setFlushOperation(getFlushOperation()); |
|
94 |
chunkParser.setFilterStart(start); |
|
95 |
chunkParser.setFilterEnd(end); |
|
96 |
chunkParser.setReuse(c.getReuse()); |
|
97 |
chunkParser.setOrdered(ordered); |
|
98 |
chunkParser.resetEventCache(); |
|
99 |
chunkParser.setParserFilter(c.getFiler()); |
|
100 |
chunkParser.updateEventParsers(); |
|
101 |
clearLastDispatch(); |
|
102 |
if (ordered) { |
|
103 |
processOrdered(); |
|
104 |
} else { |
|
105 |
processUnordered(); |
|
106 |
} |
|
107 |
if (chunkParser.isLastChunk()) { |
|
108 |
return; |
|
109 |
} |
|
110 |
chunkParser = chunkParser.nextChunkParser(); |
|
111 |
} |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
112 |
} |
57449 | 113 |
|
57985 | 114 |
private void processOrdered() throws IOException { |
115 |
if (sortedList == null) { |
|
116 |
sortedList = new RecordedEvent[10_000]; |
|
117 |
} |
|
118 |
RecordedEvent event; |
|
119 |
int index = 0; |
|
120 |
while (true) { |
|
121 |
event = chunkParser.readEvent(); |
|
122 |
if (event == null) { |
|
123 |
Arrays.sort(sortedList, 0, index, END_TIME); |
|
124 |
for (int i = 0; i < index; i++) { |
|
125 |
dispatch(sortedList[i]); |
|
126 |
} |
|
127 |
return; |
|
128 |
} |
|
129 |
if (index == sortedList.length) { |
|
130 |
RecordedEvent[] tmp = sortedList; |
|
131 |
sortedList = new RecordedEvent[2 * tmp.length]; |
|
132 |
System.arraycopy(tmp, 0, sortedList, 0, tmp.length); |
|
133 |
} |
|
134 |
sortedList[index++] = event; |
|
135 |
} |
|
57449 | 136 |
} |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57604
diff
changeset
|
137 |
|
57985 | 138 |
private void processUnordered() throws IOException { |
139 |
while (!isClosed()) { |
|
140 |
RecordedEvent event = chunkParser.readEvent(); |
|
141 |
if (event == null) { |
|
142 |
return; |
|
143 |
} |
|
144 |
dispatch(event); |
|
145 |
} |
|
57971 | 146 |
} |
57361 | 147 |
} |