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