50113
|
1 |
/*
|
58863
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
50113
|
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 |
|
|
28 |
import java.io.Closeable;
|
|
29 |
import java.io.EOFException;
|
|
30 |
import java.io.File;
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.nio.file.NoSuchFileException;
|
|
33 |
import java.nio.file.Path;
|
|
34 |
import java.util.ArrayList;
|
|
35 |
import java.util.HashSet;
|
|
36 |
import java.util.List;
|
|
37 |
|
|
38 |
import jdk.jfr.EventType;
|
|
39 |
import jdk.jfr.internal.MetadataDescriptor;
|
52850
|
40 |
import jdk.jfr.internal.Type;
|
50113
|
41 |
import jdk.jfr.internal.consumer.ChunkHeader;
|
58863
|
42 |
import jdk.jfr.internal.consumer.ChunkParser;
|
|
43 |
import jdk.jfr.internal.consumer.FileAccess;
|
50113
|
44 |
import jdk.jfr.internal.consumer.RecordingInput;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* A recording file.
|
|
48 |
* <p>
|
|
49 |
* The following example shows how read and print all events in a recording file.
|
|
50 |
*
|
|
51 |
* <pre>
|
|
52 |
* <code>
|
|
53 |
* try (RecordingFile recordingFile = new RecordingFile(Paths.get("recording.jfr"))) {
|
|
54 |
* while (recordingFile.hasMoreEvents()) {
|
|
55 |
* RecordedEvent event = recordingFile.readEvent();
|
|
56 |
* System.out.println(event);
|
|
57 |
* }
|
|
58 |
* }
|
|
59 |
* </code>
|
|
60 |
* </pre>
|
|
61 |
*
|
|
62 |
* @since 9
|
|
63 |
*/
|
|
64 |
public final class RecordingFile implements Closeable {
|
52850
|
65 |
|
|
66 |
private boolean isLastEventInChunk;
|
50113
|
67 |
private final File file;
|
|
68 |
private RecordingInput input;
|
|
69 |
private ChunkParser chunkParser;
|
|
70 |
private RecordedEvent nextEvent;
|
|
71 |
private boolean eof;
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Creates a recording file.
|
|
75 |
*
|
|
76 |
* @param file the path of the file to open, not {@code null}
|
|
77 |
* @throws IOException if it's not a valid recording file, or an I/O error
|
|
78 |
* occurred
|
|
79 |
* @throws NoSuchFileException if the {@code file} can't be located
|
|
80 |
*
|
|
81 |
* @throws SecurityException if a security manager exists and its
|
|
82 |
* {@code checkRead} method denies read access to the file.
|
|
83 |
*/
|
|
84 |
public RecordingFile(Path file) throws IOException {
|
|
85 |
this.file = file.toFile();
|
58863
|
86 |
this.input = new RecordingInput(this.file, FileAccess.UNPRIVILIGED);
|
50113
|
87 |
findNext();
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Reads the next event in the recording.
|
|
92 |
*
|
|
93 |
* @return the next event, not {@code null}
|
|
94 |
*
|
|
95 |
* @throws EOFException if no more events exist in the recording file
|
|
96 |
* @throws IOException if an I/O error occurs.
|
|
97 |
*
|
|
98 |
* @see #hasMoreEvents()
|
|
99 |
*/
|
|
100 |
public RecordedEvent readEvent() throws IOException {
|
|
101 |
if (eof) {
|
|
102 |
ensureOpen();
|
|
103 |
throw new EOFException();
|
|
104 |
}
|
52850
|
105 |
isLastEventInChunk = false;
|
50113
|
106 |
RecordedEvent event = nextEvent;
|
|
107 |
nextEvent = chunkParser.readEvent();
|
|
108 |
if (nextEvent == null) {
|
52850
|
109 |
isLastEventInChunk = true;
|
50113
|
110 |
findNext();
|
|
111 |
}
|
|
112 |
return event;
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Returns {@code true} if unread events exist in the recording file,
|
|
117 |
* {@code false} otherwise.
|
|
118 |
*
|
|
119 |
* @return {@code true} if unread events exist in the recording, {@code false}
|
|
120 |
* otherwise.
|
|
121 |
*/
|
|
122 |
public boolean hasMoreEvents() {
|
|
123 |
return !eof;
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* Returns a list of all event types in this recording.
|
|
128 |
*
|
|
129 |
* @return a list of event types, not {@code null}
|
|
130 |
* @throws IOException if an I/O error occurred while reading from the file
|
|
131 |
*
|
|
132 |
* @see #hasMoreEvents()
|
|
133 |
*/
|
|
134 |
public List<EventType> readEventTypes() throws IOException {
|
|
135 |
ensureOpen();
|
58863
|
136 |
MetadataDescriptor previous = null;
|
50113
|
137 |
List<EventType> types = new ArrayList<>();
|
|
138 |
HashSet<Long> foundIds = new HashSet<>();
|
58863
|
139 |
try (RecordingInput ri = new RecordingInput(file, FileAccess.UNPRIVILIGED)) {
|
50113
|
140 |
ChunkHeader ch = new ChunkHeader(ri);
|
58863
|
141 |
aggregateEventTypeForChunk(ch, null, types, foundIds);
|
52850
|
142 |
while (!ch.isLastChunk()) {
|
|
143 |
ch = ch.nextHeader();
|
58863
|
144 |
previous = aggregateEventTypeForChunk(ch, previous, types, foundIds);
|
52850
|
145 |
}
|
|
146 |
}
|
|
147 |
return types;
|
|
148 |
}
|
|
149 |
|
|
150 |
List<Type> readTypes() throws IOException {
|
|
151 |
ensureOpen();
|
58863
|
152 |
MetadataDescriptor previous = null;
|
52850
|
153 |
List<Type> types = new ArrayList<>();
|
|
154 |
HashSet<Long> foundIds = new HashSet<>();
|
58863
|
155 |
try (RecordingInput ri = new RecordingInput(file, FileAccess.UNPRIVILIGED)) {
|
52850
|
156 |
ChunkHeader ch = new ChunkHeader(ri);
|
58863
|
157 |
ch.awaitFinished();
|
|
158 |
aggregateTypeForChunk(ch, null, types, foundIds);
|
50113
|
159 |
while (!ch.isLastChunk()) {
|
|
160 |
ch = ch.nextHeader();
|
58863
|
161 |
previous = aggregateTypeForChunk(ch, previous, types, foundIds);
|
50113
|
162 |
}
|
|
163 |
}
|
|
164 |
return types;
|
|
165 |
}
|
|
166 |
|
58863
|
167 |
private MetadataDescriptor aggregateTypeForChunk(ChunkHeader ch, MetadataDescriptor previous, List<Type> types, HashSet<Long> foundIds) throws IOException {
|
|
168 |
MetadataDescriptor m = ch.readMetadata(previous);
|
52850
|
169 |
for (Type t : m.getTypes()) {
|
|
170 |
if (!foundIds.contains(t.getId())) {
|
|
171 |
types.add(t);
|
|
172 |
foundIds.add(t.getId());
|
|
173 |
}
|
|
174 |
}
|
58863
|
175 |
return m;
|
52850
|
176 |
}
|
|
177 |
|
58863
|
178 |
private static MetadataDescriptor aggregateEventTypeForChunk(ChunkHeader ch, MetadataDescriptor previous, List<EventType> types, HashSet<Long> foundIds) throws IOException {
|
|
179 |
MetadataDescriptor m = ch.readMetadata(previous);
|
50113
|
180 |
for (EventType t : m.getEventTypes()) {
|
|
181 |
if (!foundIds.contains(t.getId())) {
|
|
182 |
types.add(t);
|
|
183 |
foundIds.add(t.getId());
|
|
184 |
}
|
|
185 |
}
|
58863
|
186 |
return m;
|
50113
|
187 |
}
|
|
188 |
|
|
189 |
/**
|
|
190 |
* Closes this recording file and releases any system resources that are
|
|
191 |
* associated with it.
|
|
192 |
*
|
|
193 |
* @throws IOException if an I/O error occurred
|
|
194 |
*/
|
|
195 |
public void close() throws IOException {
|
|
196 |
if (input != null) {
|
|
197 |
eof = true;
|
|
198 |
input.close();
|
|
199 |
chunkParser = null;
|
|
200 |
input = null;
|
|
201 |
nextEvent = null;
|
|
202 |
}
|
|
203 |
}
|
|
204 |
|
|
205 |
/**
|
|
206 |
* Returns a list of all events in a file.
|
|
207 |
* <p>
|
|
208 |
* This method is intended for simple cases where it's convenient to read all
|
|
209 |
* events in a single operation. It isn't intended for reading large files.
|
|
210 |
*
|
|
211 |
* @param path the path to the file, not {@code null}
|
|
212 |
*
|
|
213 |
* @return the events from the file as a {@code List} object; whether the
|
|
214 |
* {@code List} is modifiable or not is implementation dependent and
|
|
215 |
* therefore not specified, not {@code null}
|
|
216 |
*
|
|
217 |
* @throws IOException if an I/O error occurred, it's not a Flight Recorder
|
|
218 |
* file or a version of a JFR file that can't be parsed
|
|
219 |
*
|
|
220 |
* @throws SecurityException if a security manager exists and its
|
|
221 |
* {@code checkRead} method denies read access to the file.
|
|
222 |
*/
|
|
223 |
public static List<RecordedEvent> readAllEvents(Path path) throws IOException {
|
|
224 |
try (RecordingFile r = new RecordingFile(path)) {
|
|
225 |
List<RecordedEvent> list = new ArrayList<>();
|
|
226 |
while (r.hasMoreEvents()) {
|
|
227 |
list.add(r.readEvent());
|
|
228 |
}
|
|
229 |
return list;
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
58863
|
233 |
// package protected
|
|
234 |
File getFile() {
|
|
235 |
return file;
|
|
236 |
}
|
|
237 |
|
|
238 |
// package protected
|
|
239 |
boolean isLastEventInChunk() {
|
|
240 |
return isLastEventInChunk;
|
|
241 |
}
|
|
242 |
|
|
243 |
|
50113
|
244 |
// either sets next to an event or sets eof to true
|
|
245 |
private void findNext() throws IOException {
|
|
246 |
while (nextEvent == null) {
|
|
247 |
if (chunkParser == null) {
|
|
248 |
chunkParser = new ChunkParser(input);
|
|
249 |
} else if (!chunkParser.isLastChunk()) {
|
|
250 |
chunkParser = chunkParser.nextChunkParser();
|
|
251 |
} else {
|
|
252 |
eof = true;
|
|
253 |
return;
|
|
254 |
}
|
|
255 |
nextEvent = chunkParser.readEvent();
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
private void ensureOpen() throws IOException {
|
|
260 |
if (input == null) {
|
|
261 |
throw new IOException("Stream Closed");
|
|
262 |
}
|
|
263 |
}
|
|
264 |
}
|