/
DTraceAsmProfiler.java
255 lines (214 loc) · 9.31 KB
/
DTraceAsmProfiler.java
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.jmh.profile;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSpec;
import org.openjdk.jmh.infra.BenchmarkParams;
import org.openjdk.jmh.results.BenchmarkResult;
import org.openjdk.jmh.results.Result;
import org.openjdk.jmh.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
/**
* Mac OS X perfasm profiler based on DTrace "profile-n" provider which samples program counter by timer interrupt.
* Due to DTrace limitations on Mac OS X target JVM cannot be run directly under DTrace control, so DTrace is run separately,
* all processes are sampled and irrelevant samples are filtered out in {@link #readEvents(double, double)} stage.
* Super user privileges are required in order to run DTrace.
* <p>
* If you see a lot of "[unknown]" regions in profile then you are probably hitting kernel code, kernel sampling is not yet supported.
*
* @author Tolstopyatov Vsevolod
* @since 18/10/2017
*/
public class DTraceAsmProfiler extends AbstractPerfAsmProfiler {
private final boolean useSudo;
private final long sampleFrequency;
private volatile String pid;
private volatile Process dtraceProcess;
private OptionSpec<Long> optFrequency;
private OptionSpec<Boolean> optSudo;
public DTraceAsmProfiler(String initLine) throws ProfilerException {
super(initLine, "sampled_pc");
try {
sampleFrequency = set.valueOf(optFrequency);
useSudo = set.valueOf(optSudo);
} catch (OptionException e) {
throw new ProfilerException(e.getMessage());
}
// Check DTrace availability
Collection<String> messages;
if (useSudo) {
messages = Utils.tryWith("sudo", "-n", "dtrace", "-V");
} else {
messages = Utils.tryWith("dtrace", "-V");
}
if (!messages.isEmpty()) {
throw new ProfilerException(messages.toString());
}
}
@Override
public void beforeTrial(BenchmarkParams params) {
super.beforeTrial(params);
}
@Override
public Collection<? extends Result> afterTrial(BenchmarkResult br, long pid, File stdOut, File stdErr) {
if (pid == 0) {
throw new IllegalStateException("DTrace needs the forked VM PID, but it is not initialized");
}
// We cannot use Process.destroy, because it closes the streams right away.
// Instead, deliver TERM by hand and wait for process to gracefully terminate.
long dtracePid = Utils.getPid(dtraceProcess);
if (dtracePid == 0) {
throw new IllegalStateException("Cannot determine dtrace process PID");
}
Collection<String> messages = Utils.tryWith("sudo", "-n", "kill", "-TERM", Long.toString(dtracePid));
if (!messages.isEmpty()) {
throw new IllegalStateException(messages.toString());
}
// Wait for dtrace to finish.
try {
int errcode = dtraceProcess.waitFor();
if (errcode != 0) {
throw new IllegalStateException("Non-zero error code from dtrace: " + errcode);
}
} catch (InterruptedException e) {
throw new IllegalStateException("Interrupted while waiting for profiler to stop");
}
this.pid = String.valueOf(pid);
return super.afterTrial(br, pid, stdOut, stdErr);
}
@Override
public Collection<String> addJVMInvokeOptions(BenchmarkParams params) {
String dtraceLine = "profile-" + sampleFrequency + " /arg1/ { printf(\"%d 0x%lx %d\", pid, arg1, timestamp); ufunc(arg1)}";
if (useSudo) {
dtraceProcess = Utils.runAsync("sudo", "-n", "dtrace", "-n", dtraceLine, "-o", perfBinData.getAbsolutePath());
} else {
dtraceProcess = Utils.runAsync("dtrace", "-n", dtraceLine, "-o", perfBinData.getAbsolutePath());
}
return Collections.emptyList();
}
@Override
public String getDescription() {
return "DTrace profile provider + PrintAssembly Profiler";
}
@Override
protected void addMyOptions(OptionParser parser) {
optFrequency = parser.accepts("frequency",
"Sampling frequency. This is synonymous to profile-#")
.withRequiredArg().ofType(Long.class).describedAs("freq").defaultsTo(1001L);
optSudo = parser.accepts("sudo", "Use sudo to access dtrace.")
.withRequiredArg().ofType(Boolean.class).defaultsTo(true);
}
@Override
protected void parseEvents() {
// Do nothing because DTrace writes text output anyway
}
@Override
protected PerfEvents readEvents(double skipMs, double lenMs) {
long start = (long) skipMs;
long end = (long) (skipMs + lenMs);
try (FileReader fr = new FileReader(perfBinData.file());
BufferedReader reader = new BufferedReader(fr)) {
Deduplicator<MethodDesc> dedup = new Deduplicator<>();
Multimap<MethodDesc, Long> methods = new HashMultimap<>();
Multiset<Long> events = new TreeMultiset<>();
long dtraceTimestampBase = 0L;
String line;
while ((line = reader.readLine()) != null) {
// Filter out DTrace misc
if (!line.contains(":profile")) {
continue;
}
line = line.trim();
line = line.substring(line.indexOf(":profile"));
String[] splits = line.split(" ", 5);
if (splits.length < 2) {
// Suspect completely corrupted line, skip
continue;
}
String sampledPid = splits[1];
if (!sampledPid.equals(pid)) {
continue;
}
// Sometimes DTrace ufunc fails and gives no information about symbols
if (splits.length < 4) {
continue;
}
long timestamp = Long.parseLong(splits[3]);
if (dtraceTimestampBase == 0) {
// Use first event timestamp as base for time comparison
dtraceTimestampBase = timestamp;
continue;
}
long elapsed = timestamp - dtraceTimestampBase;
long elapsedMs = TimeUnit.NANOSECONDS.toMillis(elapsed);
if (elapsedMs < start || elapsedMs > end) {
continue;
}
long address = Long.decode(splits[2]);
events.add(address);
String methodLine = splits[4];
// JIT-compiled code has address instead of symbol information
if (methodLine.startsWith("0x")) {
continue;
}
String symbol = "[unknown]";
String[] methodSplit = methodLine.split("`");
String library = methodSplit[0];
if ("".equals(library)) {
library = "[unknown]";
}
if (methodSplit.length == 2) {
symbol = methodSplit[1];
}
methods.put(dedup.dedup(MethodDesc.nativeMethod(symbol, library)), address);
}
IntervalMap<MethodDesc> methodMap = new IntervalMap<>();
for (MethodDesc md : methods.keys()) {
Collection<Long> longs = methods.get(md);
methodMap.add(md, Utils.min(longs), Utils.max(longs));
}
Map<String, Multiset<Long>> allEvents = new TreeMap<>();
assert requestedEventNames.size() == 1;
allEvents.put(requestedEventNames.get(0), events);
return new PerfEvents(requestedEventNames, allEvents, methodMap);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected String perfBinaryExtension() {
// DTrace produces human-readable txt
return ".txt";
}
}