-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJavaFlames.java
More file actions
146 lines (129 loc) · 5.06 KB
/
JavaFlames.java
File metadata and controls
146 lines (129 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import com.sun.net.httpserver.HttpServer;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordedFrame;
import jdk.jfr.consumer.RecordedMethod;
import jdk.jfr.consumer.RecordingFile;
import java.awt.Desktop;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class JavaFlames {
private static final int HTTP_PORT = 8090;
private static final String PATH_TO_DATA = "data";
public static void main(String[] args) throws IOException {
if (args.length != 1) {
exit(1, "expected jfr input file as argument");
}
var jfrFile = Paths.get(args[0]);
if (!Files.exists(jfrFile)) {
exit(2, jfrFile + " not found.");
}
startHttpServer(jfrFile);
var url = "http://localhost:%d?baseLineInput=%s&baseLineTitle=%s".formatted(HTTP_PORT, PATH_TO_DATA, jfrFile.toFile().getName());
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(URI.create(url));
} else {
System.out.println("Done! Open a browser and point it to: " + url);
}
}
private static void exit(int code, String message) {
System.err.println(message);
System.exit(code);
}
private static void startHttpServer(Path jfrFile) throws IOException {
var httpServer = HttpServer.create(new InetSocketAddress("localhost", HTTP_PORT), 0);
httpServer.createContext("/", exchange -> {
final Path htmlPage = Paths.get("index.html");
exchange.sendResponseHeaders(200, Files.size(htmlPage));
try (var responseBody = exchange.getResponseBody(); var fis = new FileInputStream(htmlPage.toFile())) {
fis.transferTo(responseBody);
}
});
httpServer.createContext("/" + PATH_TO_DATA, exchange -> {
exchange.sendResponseHeaders(200, 0);
try(var responseBody = exchange.getResponseBody()){
produceFlameGraphLog(jfrFile).forEach(io(line -> responseBody.write(line.getBytes(StandardCharsets.UTF_8))));
}
System.exit(0);
});
httpServer.start();
}
public static Stream<String> produceFlameGraphLog(final Path jfrRecording) throws IOException {
var recordingFile = new RecordingFile(jfrRecording);
return extractEvents(recordingFile)
.filter(it -> "jdk.ExecutionSample".equalsIgnoreCase(it.getEventType().getName()))
.map(event -> collapseFrames(event.getStackTrace().getFrames()))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream().map(e -> "%s %d\n".formatted(e.getKey(), e.getValue()))
.onClose(io(recordingFile::close));
}
private static String collapseFrames(List<RecordedFrame> frames) {
var methodNames = new ArrayDeque<String>(frames.size());
for (var frame : frames) {
final RecordedMethod method = frame.getMethod();
methodNames.addFirst("%s::%s".formatted(method.getType().getName(), method.getName()));
}
return String.join(";", methodNames);
}
private static Stream<RecordedEvent> extractEvents(RecordingFile recordingFile) {
return Stream.generate(() ->
recordingFile.hasMoreEvents() ?
io(recordingFile::readEvent).get() :
null
).takeWhile(Objects::nonNull);
}
// Helpers for dealing with checked IOException's in lambdas
@FunctionalInterface
interface IORunnable {
void run() throws IOException;
}
@FunctionalInterface
interface IOConsumer<T> {
void apply(T input) throws IOException;
}
@FunctionalInterface
interface IOSupplier<T> {
T get() throws IOException;
}
private static <T> Supplier<T> io(IOSupplier<T> supplier) {
return () -> {
try {
return supplier.get();
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
};
}
private static <T> Consumer<T> io(IOConsumer<T> consumer) {
return t -> {
try {
consumer.apply(t);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
};
}
private static Runnable io(IORunnable runnable) {
return () -> {
try {
runnable.run();
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
};
}
}