author | egahlin |
Fri, 09 Aug 2019 01:18:18 +0200 | |
branch | JEP-349-branch |
changeset 57690 | 9316d02dd4a5 |
parent 52850 | f527b24990d7 |
child 58112 | e7754025004b |
permissions | -rw-r--r-- |
52850 | 1 |
/* |
2 |
* Copyright (c) 2016, 2018, 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.internal.tool; |
|
27 |
||
28 |
import java.io.BufferedInputStream; |
|
29 |
import java.io.DataInputStream; |
|
30 |
import java.io.File; |
|
31 |
import java.io.FileInputStream; |
|
32 |
import java.io.FileOutputStream; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.InputStream; |
|
35 |
import java.io.PrintStream; |
|
36 |
import java.nio.file.Files; |
|
37 |
import java.nio.file.InvalidPathException; |
|
38 |
import java.nio.file.Path; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Deque; |
|
41 |
import java.util.List; |
|
42 |
||
43 |
import jdk.jfr.internal.consumer.ChunkHeader; |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
52850
diff
changeset
|
44 |
import jdk.jfr.internal.consumer.FileAccess; |
52850 | 45 |
import jdk.jfr.internal.consumer.RecordingInput; |
46 |
||
47 |
final class Disassemble extends Command { |
|
48 |
||
49 |
@Override |
|
50 |
public String getName() { |
|
51 |
return "disassemble"; |
|
52 |
} |
|
53 |
||
54 |
@Override |
|
55 |
public List<String> getOptionSyntax() { |
|
56 |
List<String> list = new ArrayList<>(); |
|
57 |
list.add("[--output <directory>]"); |
|
58 |
list.add("[--max-chunks <chunks>]"); |
|
59 |
list.add("[--max-size <size>]"); |
|
60 |
list.add("<file>"); |
|
61 |
return list; |
|
62 |
} |
|
63 |
||
64 |
@Override |
|
65 |
public void displayOptionUsage(PrintStream stream) { |
|
66 |
stream.println(" --output <directory> The location to write the disassembled file,"); |
|
67 |
stream.println(" by default the current directory"); |
|
68 |
stream.println(""); |
|
69 |
stream.println(" --max-chunks <chunks> Maximum number of chunks per disassembled file,"); |
|
70 |
stream.println(" by default 5. The chunk size varies, but is "); |
|
71 |
stream.println(" typically around 15 MB."); |
|
72 |
stream.println(""); |
|
73 |
stream.println(" --max-size <size> Maximum number of bytes per file."); |
|
74 |
stream.println(""); |
|
75 |
stream.println(" <file> Location of the recording file (.jfr)"); |
|
76 |
} |
|
77 |
||
78 |
@Override |
|
79 |
public String getDescription() { |
|
80 |
return "Disassamble a recording file into smaller files/chunks"; |
|
81 |
} |
|
82 |
||
83 |
@Override |
|
84 |
public void execute(Deque<String> options) throws UserSyntaxException, UserDataException { |
|
85 |
if (options.isEmpty()) { |
|
86 |
throw new UserSyntaxException("missing file"); |
|
87 |
} |
|
88 |
Path file = getJFRInputFile(options); |
|
89 |
int maxChunks = Integer.MAX_VALUE; |
|
90 |
int maxsize = Integer.MAX_VALUE; |
|
91 |
String output = System.getProperty("user.dir"); |
|
92 |
int optionCount = options.size(); |
|
93 |
while (optionCount > 0) { |
|
94 |
if (acceptOption(options, "--output")) { |
|
95 |
output = options.pop(); |
|
96 |
} |
|
97 |
if (acceptOption(options, "--max-size")) { |
|
98 |
String value = options.pop(); |
|
99 |
try { |
|
100 |
maxsize = Integer.parseInt(value); |
|
101 |
if (maxsize < 1) { |
|
102 |
throw new UserDataException("max size must be at least 1"); |
|
103 |
} |
|
104 |
} catch (NumberFormatException nfe) { |
|
105 |
throw new UserDataException("not a valid value for --max-size."); |
|
106 |
} |
|
107 |
} |
|
108 |
if (acceptOption(options, "--max-chunks")) { |
|
109 |
String value = options.pop(); |
|
110 |
try { |
|
111 |
maxChunks = Integer.parseInt(value); |
|
112 |
if (maxChunks < 1) { |
|
113 |
throw new UserDataException("max chunks must be at least 1."); |
|
114 |
} |
|
115 |
} catch (NumberFormatException nfe) { |
|
116 |
throw new UserDataException("not a valid value for --max-size."); |
|
117 |
} |
|
118 |
} |
|
119 |
if (optionCount == options.size()) { |
|
120 |
// No progress made |
|
121 |
throw new UserSyntaxException("unknown option " + options.peek()); |
|
122 |
} |
|
123 |
optionCount = options.size(); |
|
124 |
} |
|
125 |
Path outputPath = getDirectory(output); |
|
126 |
||
127 |
println(); |
|
128 |
println("Examining recording " + file + " ..."); |
|
129 |
List<Long> sizes; |
|
130 |
if (maxsize != Integer.MAX_VALUE && maxChunks == Integer.MAX_VALUE) { |
|
131 |
try { |
|
132 |
long fileSize = Files.size(file); |
|
133 |
if (maxsize >=fileSize) { |
|
134 |
println(); |
|
135 |
println("File size (" + fileSize +") does not exceed max size (" + maxsize + ")"); |
|
136 |
return; |
|
137 |
} |
|
138 |
} catch (IOException e) { |
|
139 |
throw new UserDataException("unexpected i/o error when determining file size" + e.getMessage()); |
|
140 |
} |
|
141 |
} |
|
142 |
if (maxsize == Integer.MAX_VALUE && maxChunks == Integer.MAX_VALUE) { |
|
143 |
maxChunks = 5; |
|
144 |
} |
|
145 |
||
146 |
try { |
|
147 |
sizes = findChunkSizes(file); |
|
148 |
} catch (IOException e) { |
|
149 |
throw new UserDataException("unexpected i/o error. " + e.getMessage()); |
|
150 |
} |
|
151 |
if (maxsize == Integer.MAX_VALUE == sizes.size() <= maxChunks) { |
|
152 |
throw new UserDataException("number of chunks in recording (" + sizes.size() + ") doesn't exceed max chunks (" + maxChunks + ")"); |
|
153 |
} |
|
154 |
println(); |
|
155 |
if (sizes.size() > 0) { |
|
156 |
List<Long> combinedSizes = combineChunkSizes(sizes, maxChunks, maxsize); |
|
157 |
print("File consists of " + sizes.size() + " chunks. The recording will be split into "); |
|
158 |
println(combinedSizes.size() + " files"); |
|
159 |
println(); |
|
160 |
splitFile(outputPath, file, combinedSizes); |
|
161 |
} else { |
|
162 |
throw new UserDataException("no JFR chunks found in file."); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
private List<Long> findChunkSizes(Path p) throws IOException { |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
52850
diff
changeset
|
167 |
try (RecordingInput input = new RecordingInput(p.toFile(), FileAccess.UNPRIVILIGED)) { |
52850 | 168 |
List<Long> sizes = new ArrayList<>(); |
169 |
ChunkHeader ch = new ChunkHeader(input); |
|
170 |
sizes.add(ch.getSize()); |
|
171 |
while (!ch.isLastChunk()) { |
|
172 |
ch = ch.nextHeader(); |
|
173 |
sizes.add(ch.getSize()); |
|
174 |
} |
|
175 |
return sizes; |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
private List<Long> combineChunkSizes(List<Long> sizes, int maxChunks, long maxSize) { |
|
180 |
List<Long> reduced = new ArrayList<Long>(); |
|
181 |
int chunks = 1; |
|
182 |
long fileSize = sizes.get(0); |
|
183 |
for (int i = 1; i < sizes.size(); i++) { |
|
184 |
long size = sizes.get(i); |
|
185 |
if (fileSize + size > maxSize) { |
|
186 |
reduced.add(fileSize); |
|
187 |
chunks = 1; |
|
188 |
fileSize = size; |
|
189 |
continue; |
|
190 |
} |
|
191 |
fileSize += size; |
|
192 |
if (chunks == maxChunks) { |
|
193 |
reduced.add(fileSize); |
|
194 |
fileSize = 0; |
|
195 |
chunks = 1; |
|
196 |
continue; |
|
197 |
} |
|
198 |
chunks++; |
|
199 |
} |
|
200 |
if (fileSize != 0) { |
|
201 |
reduced.add(fileSize); |
|
202 |
} |
|
203 |
return reduced; |
|
204 |
} |
|
205 |
||
206 |
private void splitFile(Path directory, Path file, List<Long> splitPositions) throws UserDataException { |
|
207 |
int padAmountZeros = String.valueOf(splitPositions.size() - 1).length(); |
|
208 |
String fileName = file.getFileName().toString(); |
|
209 |
String fileFormatter = fileName.subSequence(0, fileName.length() - 4) + "_%0" + padAmountZeros + "d.jfr"; |
|
210 |
for (int i = 0; i < splitPositions.size(); i++) { |
|
211 |
String formattedFilename = String.format(fileFormatter, i); |
|
212 |
try { |
|
213 |
Path p = directory.resolve(formattedFilename); |
|
214 |
if (Files.exists(p)) { |
|
215 |
throw new UserDataException("can't create disassembled file " + p + ", a file with that name already exist"); |
|
216 |
} |
|
217 |
} catch (InvalidPathException ipe) { |
|
218 |
throw new UserDataException("can't construct path with filename" + formattedFilename); |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
try (DataInputStream stream = new DataInputStream(new BufferedInputStream(new FileInputStream(file.toFile())))) { |
|
223 |
for (int i = 0; i < splitPositions.size(); i++) { |
|
224 |
Long l = splitPositions.get(i); |
|
225 |
byte[] bytes = readBytes(stream, l.intValue()); |
|
226 |
String formattedFilename = String.format(fileFormatter, i); |
|
227 |
Path p = directory.resolve(formattedFilename); |
|
228 |
File splittedFile = p.toFile(); |
|
229 |
println("Writing " + splittedFile + " ... " + bytes.length); |
|
230 |
FileOutputStream fos = new FileOutputStream(splittedFile); |
|
231 |
fos.write(bytes); |
|
232 |
fos.close(); |
|
233 |
} |
|
234 |
} catch (IOException ioe) { |
|
235 |
throw new UserDataException("i/o error writing file " + file); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
private byte[] readBytes(InputStream stream, int count) throws UserDataException, IOException { |
|
240 |
byte[] data = new byte[count]; |
|
241 |
int totalRead = 0; |
|
242 |
while (totalRead < data.length) { |
|
243 |
int read = stream.read(data, totalRead, data.length - totalRead); |
|
244 |
if (read == -1) { |
|
245 |
throw new UserDataException("unexpected end of data"); |
|
246 |
} |
|
247 |
totalRead += read; |
|
248 |
} |
|
249 |
return data; |
|
250 |
} |
|
251 |
} |