46640
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.debug;
|
|
24 |
|
|
25 |
import static java.util.FormattableFlags.LEFT_JUSTIFY;
|
|
26 |
import static java.util.FormattableFlags.UPPERCASE;
|
|
27 |
import static org.graalvm.compiler.debug.DebugOptions.Count;
|
|
28 |
import static org.graalvm.compiler.debug.DebugOptions.Counters;
|
|
29 |
import static org.graalvm.compiler.debug.DebugOptions.Dump;
|
|
30 |
import static org.graalvm.compiler.debug.DebugOptions.DumpOnError;
|
|
31 |
import static org.graalvm.compiler.debug.DebugOptions.DumpOnPhaseChange;
|
|
32 |
import static org.graalvm.compiler.debug.DebugOptions.ListMetrics;
|
|
33 |
import static org.graalvm.compiler.debug.DebugOptions.Log;
|
|
34 |
import static org.graalvm.compiler.debug.DebugOptions.MemUseTrackers;
|
|
35 |
import static org.graalvm.compiler.debug.DebugOptions.MethodFilter;
|
|
36 |
import static org.graalvm.compiler.debug.DebugOptions.Time;
|
|
37 |
import static org.graalvm.compiler.debug.DebugOptions.Timers;
|
|
38 |
import static org.graalvm.compiler.debug.DebugOptions.TrackMemUse;
|
|
39 |
import static org.graalvm.compiler.debug.DebugOptions.Verify;
|
|
40 |
|
|
41 |
import java.io.ByteArrayOutputStream;
|
|
42 |
import java.io.File;
|
|
43 |
import java.io.IOException;
|
|
44 |
import java.io.PrintStream;
|
|
45 |
import java.nio.file.Files;
|
|
46 |
import java.nio.file.Path;
|
|
47 |
import java.nio.file.Paths;
|
|
48 |
import java.nio.file.StandardOpenOption;
|
|
49 |
import java.util.ArrayList;
|
|
50 |
import java.util.Arrays;
|
|
51 |
import java.util.Collection;
|
|
52 |
import java.util.Collections;
|
|
53 |
import java.util.Formatter;
|
|
54 |
import java.util.List;
|
|
55 |
import java.util.Map;
|
|
56 |
import java.util.SortedMap;
|
|
57 |
import java.util.TreeMap;
|
|
58 |
|
|
59 |
import org.graalvm.compiler.options.OptionKey;
|
|
60 |
import org.graalvm.compiler.options.OptionValues;
|
|
61 |
import org.graalvm.util.EconomicMap;
|
|
62 |
import org.graalvm.util.EconomicSet;
|
|
63 |
import org.graalvm.util.Pair;
|
|
64 |
|
|
65 |
import jdk.vm.ci.meta.JavaMethod;
|
|
66 |
|
|
67 |
/**
|
|
68 |
* A facility for logging and dumping as well as a container for values associated with
|
|
69 |
* {@link MetricKey}s.
|
|
70 |
*
|
|
71 |
* A {@code DebugContext} object must only be used on the thread that created it. This means it
|
|
72 |
* needs to be passed around as a parameter. For convenience, it can be encapsulated in a widely
|
|
73 |
* used object that is in scope wherever a {@code DebugContext} is needed. However, care must be
|
|
74 |
* taken when such objects can be exposed to multiple threads (e.g., they are in a non-thread-local
|
|
75 |
* cache).
|
|
76 |
*/
|
|
77 |
public final class DebugContext implements AutoCloseable {
|
|
78 |
|
|
79 |
public static final Description NO_DESCRIPTION = null;
|
|
80 |
public static final GlobalMetrics NO_GLOBAL_METRIC_VALUES = null;
|
|
81 |
public static final Iterable<DebugHandlersFactory> NO_CONFIG_CUSTOMIZERS = Collections.emptyList();
|
|
82 |
|
|
83 |
public static final PrintStream DEFAULT_LOG_STREAM = TTY.out;
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Contains the immutable parts of a debug context. This separation allows the immutable parts
|
|
87 |
* to be shared and reduces the overhead of initialization since most immutable fields are
|
|
88 |
* configured by parsing options.
|
|
89 |
*/
|
|
90 |
final Immutable immutable;
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Determines whether metrics are enabled.
|
|
94 |
*/
|
|
95 |
boolean metricsEnabled;
|
|
96 |
|
|
97 |
DebugConfig currentConfig;
|
|
98 |
ScopeImpl currentScope;
|
|
99 |
CloseableCounter currentTimer;
|
|
100 |
CloseableCounter currentMemUseTracker;
|
|
101 |
Scope lastClosedScope;
|
|
102 |
Throwable lastExceptionThrown;
|
|
103 |
|
|
104 |
/**
|
|
105 |
* Stores the {@link MetricKey} values.
|
|
106 |
*/
|
|
107 |
private long[] metricValues;
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Determines if dynamic scopes are enabled.
|
|
111 |
*/
|
|
112 |
public boolean areScopesEnabled() {
|
|
113 |
return immutable.scopesEnabled;
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* The immutable configuration that can be shared between {@link DebugContext} objects.
|
|
118 |
*/
|
|
119 |
static final class Immutable {
|
|
120 |
|
|
121 |
private static final Immutable[] CACHE = new Immutable[5];
|
|
122 |
|
|
123 |
/**
|
|
124 |
* The options from which this object was configured.
|
|
125 |
*/
|
|
126 |
final OptionValues options;
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Specifies if dynamic scopes are enabled.
|
|
130 |
*/
|
|
131 |
final boolean scopesEnabled;
|
|
132 |
|
|
133 |
final boolean listMetrics;
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Names of unscoped counters. A counter is unscoped if this set is empty or contains the
|
|
137 |
* counter's name.
|
|
138 |
*/
|
|
139 |
final EconomicSet<String> unscopedCounters;
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Names of unscoped timers. A timer is unscoped if this set is empty or contains the
|
|
143 |
* timer's name.
|
|
144 |
*/
|
|
145 |
final EconomicSet<String> unscopedTimers;
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Names of unscoped memory usage trackers. A memory usage tracker is unscoped if this set
|
|
149 |
* is empty or contains the memory usage tracker's name.
|
|
150 |
*/
|
|
151 |
final EconomicSet<String> unscopedMemUseTrackers;
|
|
152 |
|
|
153 |
private static EconomicSet<String> parseUnscopedMetricSpec(String spec, boolean unconditional, boolean accumulatedKey) {
|
|
154 |
EconomicSet<String> res;
|
|
155 |
if (spec == null) {
|
|
156 |
if (!unconditional) {
|
|
157 |
res = null;
|
|
158 |
} else {
|
|
159 |
res = EconomicSet.create();
|
|
160 |
}
|
|
161 |
} else {
|
|
162 |
res = EconomicSet.create();
|
|
163 |
if (!spec.isEmpty()) {
|
|
164 |
if (!accumulatedKey) {
|
|
165 |
res.addAll(Arrays.asList(spec.split(",")));
|
|
166 |
} else {
|
|
167 |
for (String n : spec.split(",")) {
|
|
168 |
res.add(n + AccumulatedKey.ACCUMULATED_KEY_SUFFIX);
|
|
169 |
res.add(n + AccumulatedKey.FLAT_KEY_SUFFIX);
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
}
|
|
174 |
}
|
|
175 |
return res;
|
|
176 |
}
|
|
177 |
|
|
178 |
static Immutable create(OptionValues options) {
|
|
179 |
int i = 0;
|
|
180 |
while (i < CACHE.length) {
|
|
181 |
Immutable immutable = CACHE[i];
|
|
182 |
if (immutable == null) {
|
|
183 |
break;
|
|
184 |
}
|
|
185 |
if (immutable.options == options) {
|
|
186 |
return immutable;
|
|
187 |
}
|
|
188 |
i++;
|
|
189 |
}
|
|
190 |
Immutable immutable = new Immutable(options);
|
|
191 |
if (i < CACHE.length) {
|
|
192 |
CACHE[i] = immutable;
|
|
193 |
}
|
|
194 |
return immutable;
|
|
195 |
}
|
|
196 |
|
|
197 |
private static boolean isNotEmpty(OptionKey<String> option, OptionValues options) {
|
|
198 |
return option.getValue(options) != null && !option.getValue(options).isEmpty();
|
|
199 |
}
|
|
200 |
|
|
201 |
private Immutable(OptionValues options) {
|
|
202 |
this.options = options;
|
|
203 |
this.unscopedCounters = parseUnscopedMetricSpec(Counters.getValue(options), "".equals(Count.getValue(options)), false);
|
|
204 |
this.unscopedTimers = parseUnscopedMetricSpec(Timers.getValue(options), "".equals(Time.getValue(options)), true);
|
|
205 |
this.unscopedMemUseTrackers = parseUnscopedMetricSpec(MemUseTrackers.getValue(options), "".equals(TrackMemUse.getValue(options)), true);
|
|
206 |
|
|
207 |
this.scopesEnabled = DumpOnError.getValue(options) ||
|
|
208 |
Dump.getValue(options) != null ||
|
|
209 |
Log.getValue(options) != null ||
|
|
210 |
isNotEmpty(DebugOptions.Count, options) ||
|
|
211 |
isNotEmpty(DebugOptions.Time, options) ||
|
|
212 |
isNotEmpty(DebugOptions.TrackMemUse, options) ||
|
|
213 |
DumpOnPhaseChange.getValue(options) != null;
|
|
214 |
this.listMetrics = ListMetrics.getValue(options);
|
|
215 |
}
|
|
216 |
|
|
217 |
private Immutable() {
|
|
218 |
this.options = new OptionValues(EconomicMap.create());
|
|
219 |
this.unscopedCounters = null;
|
|
220 |
this.unscopedTimers = null;
|
|
221 |
this.unscopedMemUseTrackers = null;
|
|
222 |
this.scopesEnabled = false;
|
|
223 |
this.listMetrics = false;
|
|
224 |
}
|
|
225 |
|
|
226 |
public boolean hasUnscopedMetrics() {
|
|
227 |
return unscopedCounters != null || unscopedTimers != null || unscopedMemUseTrackers != null;
|
|
228 |
}
|
|
229 |
}
|
|
230 |
|
|
231 |
/**
|
|
232 |
* Gets the options this debug context was constructed with.
|
|
233 |
*/
|
|
234 |
public OptionValues getOptions() {
|
|
235 |
return immutable.options;
|
|
236 |
}
|
|
237 |
|
|
238 |
static class Activated extends ThreadLocal<DebugContext> {
|
|
239 |
}
|
|
240 |
|
|
241 |
private static final Activated activated = new Activated();
|
|
242 |
|
|
243 |
/**
|
|
244 |
* An object used to undo the changes made by DebugContext#activate().
|
|
245 |
*/
|
|
246 |
public static class Activation implements AutoCloseable {
|
|
247 |
private final DebugContext parent;
|
|
248 |
|
|
249 |
Activation(DebugContext parent) {
|
|
250 |
this.parent = parent;
|
|
251 |
}
|
|
252 |
|
|
253 |
@Override
|
|
254 |
public void close() {
|
|
255 |
activated.set(parent);
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
/**
|
|
260 |
* Activates this object as the debug context {@linkplain DebugContext#forCurrentThread for the
|
|
261 |
* current thread}. This method should be used in a try-with-resources statement.
|
|
262 |
*
|
|
263 |
* @return an object that will deactivate the debug context for the current thread when
|
|
264 |
* {@link Activation#close()} is called on it
|
|
265 |
*/
|
|
266 |
public Activation activate() {
|
|
267 |
Activation res = new Activation(activated.get());
|
|
268 |
activated.set(this);
|
|
269 |
return res;
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Shared object used to represent a disabled debug context.
|
|
274 |
*/
|
|
275 |
public static final DebugContext DISABLED = new DebugContext(NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, new Immutable(), NO_CONFIG_CUSTOMIZERS);
|
|
276 |
|
|
277 |
/**
|
|
278 |
* Gets the debug context for the current thread. This should only be used when there is no
|
|
279 |
* other reasonable means to get a hold of a debug context.
|
|
280 |
*/
|
|
281 |
public static DebugContext forCurrentThread() {
|
|
282 |
DebugContext current = activated.get();
|
|
283 |
if (current == null) {
|
|
284 |
return DISABLED;
|
|
285 |
}
|
|
286 |
return current;
|
|
287 |
}
|
|
288 |
|
|
289 |
private final GlobalMetrics globalMetrics;
|
|
290 |
|
|
291 |
/**
|
|
292 |
* Describes the computation associated with a {@link DebugContext}.
|
|
293 |
*/
|
|
294 |
public static class Description {
|
|
295 |
/**
|
|
296 |
* The primary input to the computation.
|
|
297 |
*/
|
|
298 |
final Object compilable;
|
|
299 |
|
|
300 |
/**
|
|
301 |
* A runtime based identifier that is most likely to be unique.
|
|
302 |
*/
|
|
303 |
final String identifier;
|
|
304 |
|
|
305 |
public Description(Object compilable, String identifier) {
|
|
306 |
this.compilable = compilable;
|
|
307 |
this.identifier = identifier;
|
|
308 |
}
|
|
309 |
|
|
310 |
@Override
|
|
311 |
public String toString() {
|
|
312 |
String compilableName = compilable instanceof JavaMethod ? ((JavaMethod) compilable).format("%H.%n(%p)%R") : String.valueOf(compilable);
|
|
313 |
return identifier + ":" + compilableName;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
private final Description description;
|
|
318 |
|
|
319 |
/**
|
|
320 |
* Gets a description of the computation associated with this debug context.
|
|
321 |
*
|
|
322 |
* @return {@code null} if no description is available
|
|
323 |
*/
|
|
324 |
public Description getDescription() {
|
|
325 |
return description;
|
|
326 |
}
|
|
327 |
|
|
328 |
/**
|
|
329 |
* Gets the global metrics associated with this debug context.
|
|
330 |
*
|
|
331 |
* @return {@code null} if no global metrics are available
|
|
332 |
*/
|
|
333 |
public GlobalMetrics getGlobalMetrics() {
|
|
334 |
return globalMetrics;
|
|
335 |
}
|
|
336 |
|
|
337 |
/**
|
|
338 |
* Creates a {@link DebugContext} based on a given set of option values and {@code factory}.
|
|
339 |
*/
|
|
340 |
public static DebugContext create(OptionValues options, DebugHandlersFactory factory) {
|
|
341 |
return new DebugContext(NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, Immutable.create(options), Collections.singletonList(factory));
|
|
342 |
}
|
|
343 |
|
|
344 |
/**
|
|
345 |
* Creates a {@link DebugContext} based on a given set of option values and {@code factories}.
|
|
346 |
* The {@link DebugHandlersFactory#LOADER} can be used for the latter.
|
|
347 |
*/
|
|
348 |
public static DebugContext create(OptionValues options, Iterable<DebugHandlersFactory> factories) {
|
|
349 |
return new DebugContext(NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, Immutable.create(options), factories);
|
|
350 |
}
|
|
351 |
|
|
352 |
/**
|
|
353 |
* Creates a {@link DebugContext}.
|
|
354 |
*/
|
|
355 |
public static DebugContext create(OptionValues options, Description description, GlobalMetrics globalMetrics, PrintStream logStream, Iterable<DebugHandlersFactory> factories) {
|
|
356 |
return new DebugContext(description, globalMetrics, logStream, Immutable.create(options), factories);
|
|
357 |
}
|
|
358 |
|
|
359 |
private DebugContext(Description description, GlobalMetrics globalMetrics, PrintStream logStream, Immutable immutable, Iterable<DebugHandlersFactory> factories) {
|
|
360 |
this.immutable = immutable;
|
|
361 |
this.description = description;
|
|
362 |
this.globalMetrics = globalMetrics;
|
|
363 |
if (immutable.scopesEnabled) {
|
|
364 |
OptionValues options = immutable.options;
|
|
365 |
List<DebugDumpHandler> dumpHandlers = new ArrayList<>();
|
|
366 |
List<DebugVerifyHandler> verifyHandlers = new ArrayList<>();
|
|
367 |
for (DebugHandlersFactory factory : factories) {
|
|
368 |
for (DebugHandler handler : factory.createHandlers(options)) {
|
|
369 |
if (handler instanceof DebugDumpHandler) {
|
|
370 |
dumpHandlers.add((DebugDumpHandler) handler);
|
|
371 |
} else {
|
|
372 |
assert handler instanceof DebugVerifyHandler;
|
|
373 |
verifyHandlers.add((DebugVerifyHandler) handler);
|
|
374 |
}
|
|
375 |
}
|
|
376 |
}
|
|
377 |
currentConfig = new DebugConfigImpl(
|
|
378 |
options,
|
|
379 |
Log.getValue(options),
|
|
380 |
Count.getValue(options),
|
|
381 |
TrackMemUse.getValue(options),
|
|
382 |
Time.getValue(options),
|
|
383 |
Dump.getValue(options),
|
|
384 |
Verify.getValue(options),
|
|
385 |
MethodFilter.getValue(options),
|
|
386 |
logStream, dumpHandlers, verifyHandlers);
|
|
387 |
currentScope = new ScopeImpl(this, Thread.currentThread());
|
|
388 |
currentScope.updateFlags(currentConfig);
|
|
389 |
metricsEnabled = true;
|
|
390 |
} else {
|
|
391 |
metricsEnabled = immutable.hasUnscopedMetrics() || immutable.listMetrics;
|
|
392 |
}
|
|
393 |
}
|
|
394 |
|
|
395 |
/**
|
|
396 |
* A special dump level that indicates the dumping machinery is enabled but no dumps will be
|
|
397 |
* produced except through other options.
|
|
398 |
*/
|
|
399 |
public static final int ENABLED_LEVEL = 0;
|
|
400 |
|
|
401 |
/**
|
|
402 |
* Basic debug level.
|
|
403 |
*
|
|
404 |
* For HIR dumping, only ~5 graphs per method: after parsing, after inlining, after high tier,
|
|
405 |
* after mid tier, after low tier.
|
|
406 |
*
|
|
407 |
* LIR dumping: After LIR generation, after each pre-allocation, allocation and post allocation
|
|
408 |
* stage, and after code installation.
|
|
409 |
*/
|
|
410 |
public static final int BASIC_LEVEL = 1;
|
|
411 |
|
|
412 |
/**
|
|
413 |
* Informational debug level.
|
|
414 |
*
|
|
415 |
* HIR dumping: One graph after each applied top-level phase.
|
|
416 |
*
|
|
417 |
* LIR dumping: After each applied phase.
|
|
418 |
*/
|
|
419 |
public static final int INFO_LEVEL = 2;
|
|
420 |
|
|
421 |
/**
|
|
422 |
* Verbose debug level.
|
|
423 |
*
|
|
424 |
* HIR dumping: One graph after each phase (including sub phases).
|
|
425 |
*
|
|
426 |
* LIR dumping: After each phase including sub phases.
|
|
427 |
*/
|
|
428 |
public static final int VERBOSE_LEVEL = 3;
|
|
429 |
|
|
430 |
/**
|
|
431 |
* Detailed debug level.
|
|
432 |
*
|
|
433 |
* HIR dumping: Graphs within phases where interesting for a phase, max ~5 per phase.
|
|
434 |
*
|
|
435 |
* LIR dumping: Dump CFG within phases where interesting.
|
|
436 |
*/
|
|
437 |
public static final int DETAILED_LEVEL = 4;
|
|
438 |
|
|
439 |
/**
|
|
440 |
* Very detailed debug level.
|
|
441 |
*
|
|
442 |
* HIR dumping: Graphs per node granularity graph change (before/after change).
|
|
443 |
*
|
|
444 |
* LIR dumping: Intermediate CFGs of phases where interesting.
|
|
445 |
*/
|
|
446 |
public static final int VERY_DETAILED_LEVEL = 5;
|
|
447 |
|
|
448 |
public boolean isDumpEnabled(int dumpLevel) {
|
|
449 |
return currentScope != null && currentScope.isDumpEnabled(dumpLevel);
|
|
450 |
}
|
|
451 |
|
|
452 |
/**
|
|
453 |
* Determines if verification is enabled for any {@link JavaMethod} in the current scope.
|
|
454 |
*
|
|
455 |
* @see DebugContext#verify(Object, String)
|
|
456 |
*/
|
|
457 |
public boolean isVerifyEnabledForMethod() {
|
|
458 |
if (currentScope == null) {
|
|
459 |
return false;
|
|
460 |
}
|
|
461 |
if (currentConfig == null) {
|
|
462 |
return false;
|
|
463 |
}
|
|
464 |
return currentConfig.isVerifyEnabledForMethod(currentScope);
|
|
465 |
}
|
|
466 |
|
|
467 |
/**
|
|
468 |
* Determines if verification is enabled in the current scope.
|
|
469 |
*
|
|
470 |
* @see DebugContext#verify(Object, String)
|
|
471 |
*/
|
|
472 |
public boolean isVerifyEnabled() {
|
|
473 |
return currentScope != null && currentScope.isVerifyEnabled();
|
|
474 |
}
|
|
475 |
|
|
476 |
public boolean isCountEnabled() {
|
|
477 |
return currentScope != null && currentScope.isCountEnabled();
|
|
478 |
}
|
|
479 |
|
|
480 |
public boolean isTimeEnabled() {
|
|
481 |
return currentScope != null && currentScope.isTimeEnabled();
|
|
482 |
}
|
|
483 |
|
|
484 |
public boolean isMemUseTrackingEnabled() {
|
|
485 |
return currentScope != null && currentScope.isMemUseTrackingEnabled();
|
|
486 |
}
|
|
487 |
|
|
488 |
public boolean isDumpEnabledForMethod() {
|
|
489 |
if (currentConfig == null) {
|
|
490 |
return false;
|
|
491 |
}
|
|
492 |
return currentConfig.isDumpEnabledForMethod(currentScope);
|
|
493 |
}
|
|
494 |
|
|
495 |
public boolean isLogEnabledForMethod() {
|
|
496 |
if (currentScope == null) {
|
|
497 |
return false;
|
|
498 |
}
|
|
499 |
if (currentConfig == null) {
|
|
500 |
return false;
|
|
501 |
}
|
|
502 |
return currentConfig.isLogEnabledForMethod(currentScope);
|
|
503 |
}
|
|
504 |
|
|
505 |
public boolean isLogEnabled() {
|
|
506 |
return currentScope != null && isLogEnabled(BASIC_LEVEL);
|
|
507 |
}
|
|
508 |
|
|
509 |
public boolean isLogEnabled(int logLevel) {
|
|
510 |
return currentScope != null && currentScope.isLogEnabled(logLevel);
|
|
511 |
}
|
|
512 |
|
|
513 |
/**
|
|
514 |
* Gets a string composed of the names in the current nesting of debug
|
|
515 |
* {@linkplain #scope(Object) scopes} separated by {@code '.'}.
|
|
516 |
*/
|
|
517 |
public String getCurrentScopeName() {
|
|
518 |
if (currentScope != null) {
|
|
519 |
return currentScope.getQualifiedName();
|
|
520 |
} else {
|
|
521 |
return "";
|
|
522 |
}
|
|
523 |
}
|
|
524 |
|
|
525 |
/**
|
|
526 |
* Creates and enters a new debug scope which will be a child of the current debug scope.
|
|
527 |
* <p>
|
|
528 |
* It is recommended to use the try-with-resource statement for managing entering and leaving
|
|
529 |
* debug scopes. For example:
|
|
530 |
*
|
|
531 |
* <pre>
|
|
532 |
* try (Scope s = Debug.scope("InliningGraph", inlineeGraph)) {
|
|
533 |
* ...
|
|
534 |
* } catch (Throwable e) {
|
|
535 |
* throw Debug.handle(e);
|
|
536 |
* }
|
|
537 |
* </pre>
|
|
538 |
*
|
|
539 |
* The {@code name} argument is subject to the following type based conversion before having
|
|
540 |
* {@link Object#toString()} called on it:
|
|
541 |
*
|
|
542 |
* <pre>
|
|
543 |
* Type | Conversion
|
|
544 |
* ------------------+-----------------
|
|
545 |
* java.lang.Class | arg.getSimpleName()
|
|
546 |
* |
|
|
547 |
* </pre>
|
|
548 |
*
|
|
549 |
* @param name the name of the new scope
|
|
550 |
* @param contextObjects an array of object to be appended to the {@linkplain #context()
|
|
551 |
* current} debug context
|
|
552 |
* @throws Throwable used to enforce a catch block.
|
|
553 |
* @return the scope entered by this method which will be exited when its {@link Scope#close()}
|
|
554 |
* method is called
|
|
555 |
*/
|
|
556 |
public DebugContext.Scope scope(Object name, Object[] contextObjects) throws Throwable {
|
|
557 |
if (currentScope != null) {
|
|
558 |
return enterScope(convertFormatArg(name).toString(), null, contextObjects);
|
|
559 |
} else {
|
|
560 |
return null;
|
|
561 |
}
|
|
562 |
}
|
|
563 |
|
|
564 |
/**
|
|
565 |
* Similar to {@link #scope(Object, Object[])} but without context objects. Therefore the catch
|
|
566 |
* block can be omitted.
|
|
567 |
*
|
|
568 |
* @see #scope(Object, Object[])
|
|
569 |
*/
|
|
570 |
public DebugContext.Scope scope(Object name) {
|
|
571 |
if (currentScope != null) {
|
|
572 |
return enterScope(convertFormatArg(name).toString(), null);
|
|
573 |
} else {
|
|
574 |
return null;
|
|
575 |
}
|
|
576 |
}
|
|
577 |
|
|
578 |
private final Invariants invariants = Assertions.ENABLED ? new Invariants() : null;
|
|
579 |
|
|
580 |
static StackTraceElement[] getStackTrace(Thread thread) {
|
|
581 |
return thread.getStackTrace();
|
|
582 |
}
|
|
583 |
|
|
584 |
/**
|
|
585 |
* Utility for enforcing {@link DebugContext} invariants via assertions.
|
|
586 |
*/
|
|
587 |
static class Invariants {
|
|
588 |
private final Thread thread;
|
|
589 |
private final StackTraceElement[] origin;
|
|
590 |
|
|
591 |
Invariants() {
|
|
592 |
thread = Thread.currentThread();
|
|
593 |
origin = getStackTrace(thread);
|
|
594 |
}
|
|
595 |
|
|
596 |
boolean checkNoConcurrentAccess() {
|
|
597 |
Thread currentThread = Thread.currentThread();
|
|
598 |
if (currentThread != thread) {
|
|
599 |
Formatter buf = new Formatter();
|
|
600 |
buf.format("Thread local %s object was created on thread %s but is being accessed by thread %s. The most likely cause is " +
|
|
601 |
"that the object is being retrieved from a non-thread-local cache.",
|
|
602 |
DebugContext.class.getName(), thread, currentThread);
|
|
603 |
int debugContextConstructors = 0;
|
|
604 |
boolean addedHeader = false;
|
|
605 |
for (StackTraceElement e : origin) {
|
|
606 |
if (e.getMethodName().equals("<init>") && e.getClassName().equals(DebugContext.class.getName())) {
|
|
607 |
debugContextConstructors++;
|
|
608 |
} else if (debugContextConstructors != 0) {
|
|
609 |
if (!addedHeader) {
|
|
610 |
addedHeader = true;
|
|
611 |
buf.format(" The object was instantiated here:");
|
|
612 |
}
|
|
613 |
// Distinguish from assertion stack trace by using double indent and
|
|
614 |
// "in" instead of "at" prefix.
|
|
615 |
buf.format("%n\t\tin %s", e);
|
|
616 |
}
|
|
617 |
}
|
|
618 |
if (addedHeader) {
|
|
619 |
buf.format("%n");
|
|
620 |
}
|
|
621 |
|
|
622 |
throw new AssertionError(buf.toString());
|
|
623 |
}
|
|
624 |
return true;
|
|
625 |
}
|
|
626 |
}
|
|
627 |
|
|
628 |
boolean checkNoConcurrentAccess() {
|
|
629 |
assert invariants == null || invariants.checkNoConcurrentAccess();
|
|
630 |
return true;
|
|
631 |
}
|
|
632 |
|
|
633 |
private DebugContext.Scope enterScope(CharSequence name, DebugConfig sandboxConfig, Object... newContextObjects) {
|
|
634 |
assert checkNoConcurrentAccess();
|
|
635 |
currentScope = currentScope.scope(name, sandboxConfig, newContextObjects);
|
|
636 |
return currentScope;
|
|
637 |
}
|
|
638 |
|
|
639 |
/**
|
|
640 |
* @see #scope(Object, Object[])
|
|
641 |
* @param context an object to be appended to the {@linkplain #context() current} debug context
|
|
642 |
*/
|
|
643 |
public DebugContext.Scope scope(Object name, Object context) throws Throwable {
|
|
644 |
if (currentScope != null) {
|
|
645 |
return enterScope(convertFormatArg(name).toString(), null, context);
|
|
646 |
} else {
|
|
647 |
return null;
|
|
648 |
}
|
|
649 |
}
|
|
650 |
|
|
651 |
/**
|
|
652 |
* @see #scope(Object, Object[])
|
|
653 |
* @param context1 first object to be appended to the {@linkplain #context() current} debug
|
|
654 |
* context
|
|
655 |
* @param context2 second object to be appended to the {@linkplain #context() current} debug
|
|
656 |
* context
|
|
657 |
*/
|
|
658 |
public DebugContext.Scope scope(Object name, Object context1, Object context2) throws Throwable {
|
|
659 |
if (currentScope != null) {
|
|
660 |
return enterScope(convertFormatArg(name).toString(), null, context1, context2);
|
|
661 |
} else {
|
|
662 |
return null;
|
|
663 |
}
|
|
664 |
}
|
|
665 |
|
|
666 |
/**
|
|
667 |
* @see #scope(Object, Object[])
|
|
668 |
* @param context1 first object to be appended to the {@linkplain #context() current} debug
|
|
669 |
* context
|
|
670 |
* @param context2 second object to be appended to the {@linkplain #context() current} debug
|
|
671 |
* context
|
|
672 |
* @param context3 third object to be appended to the {@linkplain #context() current} debug
|
|
673 |
* context
|
|
674 |
*/
|
|
675 |
public DebugContext.Scope scope(Object name, Object context1, Object context2, Object context3) throws Throwable {
|
|
676 |
if (currentScope != null) {
|
|
677 |
return enterScope(convertFormatArg(name).toString(), null, context1, context2, context3);
|
|
678 |
} else {
|
|
679 |
return null;
|
|
680 |
}
|
|
681 |
}
|
|
682 |
|
|
683 |
/**
|
|
684 |
* Creates and enters a new debug scope which will be disjoint from the current debug scope.
|
|
685 |
* <p>
|
|
686 |
* It is recommended to use the try-with-resource statement for managing entering and leaving
|
|
687 |
* debug scopes. For example:
|
|
688 |
*
|
|
689 |
* <pre>
|
|
690 |
* try (Scope s = Debug.sandbox("CompilingStub", null, stubGraph)) {
|
|
691 |
* ...
|
|
692 |
* } catch (Throwable e) {
|
|
693 |
* throw Debug.handle(e);
|
|
694 |
* }
|
|
695 |
* </pre>
|
|
696 |
*
|
|
697 |
* @param name the name of the new scope
|
|
698 |
* @param config the debug configuration to use for the new scope or {@code null} to disable the
|
|
699 |
* scoping mechanism within the sandbox scope
|
|
700 |
* @param context objects to be appended to the {@linkplain #context() current} debug context
|
|
701 |
* @return the scope entered by this method which will be exited when its {@link Scope#close()}
|
|
702 |
* method is called
|
|
703 |
*/
|
|
704 |
public DebugContext.Scope sandbox(CharSequence name, DebugConfig config, Object... context) throws Throwable {
|
|
705 |
if (config == null) {
|
|
706 |
return disable();
|
|
707 |
}
|
|
708 |
if (currentScope != null) {
|
|
709 |
return enterScope(name, config, context);
|
|
710 |
} else {
|
|
711 |
return null;
|
|
712 |
}
|
|
713 |
}
|
|
714 |
|
46680
|
715 |
/**
|
|
716 |
* Determines if scopes are enabled and this context is in a non-top-level scope.
|
|
717 |
*/
|
|
718 |
public boolean inNestedScope() {
|
|
719 |
if (immutable.scopesEnabled) {
|
|
720 |
if (currentScope == null) {
|
|
721 |
// In an active DisabledScope
|
|
722 |
return true;
|
|
723 |
}
|
|
724 |
return !currentScope.isTopLevel();
|
|
725 |
}
|
|
726 |
return immutable.scopesEnabled && currentScope == null;
|
|
727 |
}
|
|
728 |
|
46640
|
729 |
class DisabledScope implements DebugContext.Scope {
|
|
730 |
final boolean savedMetricsEnabled;
|
|
731 |
final ScopeImpl savedScope;
|
|
732 |
final DebugConfig savedConfig;
|
|
733 |
|
|
734 |
DisabledScope() {
|
|
735 |
this.savedMetricsEnabled = metricsEnabled;
|
|
736 |
this.savedScope = currentScope;
|
|
737 |
this.savedConfig = currentConfig;
|
|
738 |
metricsEnabled = false;
|
|
739 |
currentScope = null;
|
|
740 |
currentConfig = null;
|
|
741 |
}
|
|
742 |
|
|
743 |
@Override
|
|
744 |
public String getQualifiedName() {
|
|
745 |
return "";
|
|
746 |
}
|
|
747 |
|
|
748 |
@Override
|
|
749 |
public Iterable<Object> getCurrentContext() {
|
|
750 |
return Collections.emptyList();
|
|
751 |
}
|
|
752 |
|
|
753 |
@Override
|
|
754 |
public void close() {
|
|
755 |
metricsEnabled = savedMetricsEnabled;
|
|
756 |
currentScope = savedScope;
|
|
757 |
currentConfig = savedConfig;
|
|
758 |
lastClosedScope = this;
|
|
759 |
}
|
|
760 |
}
|
|
761 |
|
|
762 |
/**
|
|
763 |
* Disables all metrics and scope related functionality until {@code close()} is called on the
|
|
764 |
* returned object.
|
|
765 |
*/
|
|
766 |
public DebugContext.Scope disable() {
|
|
767 |
if (currentScope != null) {
|
|
768 |
return new DisabledScope();
|
|
769 |
} else {
|
|
770 |
return null;
|
|
771 |
}
|
|
772 |
}
|
|
773 |
|
|
774 |
public DebugContext.Scope forceLog() throws Throwable {
|
|
775 |
if (currentConfig != null) {
|
|
776 |
ArrayList<Object> context = new ArrayList<>();
|
|
777 |
for (Object obj : context()) {
|
|
778 |
context.add(obj);
|
|
779 |
}
|
|
780 |
DebugConfigImpl config = new DebugConfigImpl(new OptionValues(currentConfig.getOptions(), DebugOptions.Log, ":1000"));
|
|
781 |
return sandbox("forceLog", config, context.toArray());
|
|
782 |
}
|
|
783 |
return null;
|
|
784 |
}
|
|
785 |
|
|
786 |
/**
|
|
787 |
* Opens a scope in which exception
|
|
788 |
* {@linkplain DebugConfig#interceptException(DebugContext, Throwable) interception} is
|
|
789 |
* disabled. The current state of interception is restored when {@link DebugCloseable#close()}
|
|
790 |
* is called on the returned object.
|
|
791 |
*
|
|
792 |
* This is particularly useful to suppress extraneous output in JUnit tests that are expected to
|
|
793 |
* throw an exception.
|
|
794 |
*/
|
|
795 |
public DebugCloseable disableIntercept() {
|
|
796 |
if (currentScope != null) {
|
|
797 |
return currentScope.disableIntercept();
|
|
798 |
}
|
|
799 |
return null;
|
|
800 |
}
|
|
801 |
|
|
802 |
/**
|
|
803 |
* Handles an exception in the context of the debug scope just exited. The just exited scope
|
|
804 |
* must have the current scope as its parent which will be the case if the try-with-resource
|
|
805 |
* pattern recommended by {@link #scope(Object)} and
|
|
806 |
* {@link #sandbox(CharSequence, DebugConfig, Object...)} is used
|
|
807 |
*
|
|
808 |
* @see #scope(Object, Object[])
|
|
809 |
* @see #sandbox(CharSequence, DebugConfig, Object...)
|
|
810 |
*/
|
|
811 |
public RuntimeException handle(Throwable exception) {
|
|
812 |
if (currentScope != null) {
|
|
813 |
return currentScope.handle(exception);
|
|
814 |
} else {
|
|
815 |
if (exception instanceof Error) {
|
|
816 |
throw (Error) exception;
|
|
817 |
}
|
|
818 |
if (exception instanceof RuntimeException) {
|
|
819 |
throw (RuntimeException) exception;
|
|
820 |
}
|
|
821 |
throw new RuntimeException(exception);
|
|
822 |
}
|
|
823 |
}
|
|
824 |
|
|
825 |
public void log(String msg) {
|
|
826 |
log(BASIC_LEVEL, msg);
|
|
827 |
}
|
|
828 |
|
|
829 |
/**
|
|
830 |
* Prints a message to the current debug scope's logging stream if logging is enabled.
|
|
831 |
*
|
|
832 |
* @param msg the message to log
|
|
833 |
*/
|
|
834 |
public void log(int logLevel, String msg) {
|
|
835 |
if (currentScope != null) {
|
|
836 |
currentScope.log(logLevel, msg);
|
|
837 |
}
|
|
838 |
}
|
|
839 |
|
|
840 |
public void log(String format, Object arg) {
|
|
841 |
log(BASIC_LEVEL, format, arg);
|
|
842 |
}
|
|
843 |
|
|
844 |
/**
|
|
845 |
* Prints a message to the current debug scope's logging stream if logging is enabled.
|
|
846 |
*
|
|
847 |
* @param format a format string
|
|
848 |
* @param arg the argument referenced by the format specifiers in {@code format}
|
|
849 |
*/
|
|
850 |
public void log(int logLevel, String format, Object arg) {
|
|
851 |
if (currentScope != null) {
|
|
852 |
currentScope.log(logLevel, format, arg);
|
|
853 |
}
|
|
854 |
}
|
|
855 |
|
|
856 |
public void log(String format, int arg) {
|
|
857 |
log(BASIC_LEVEL, format, arg);
|
|
858 |
}
|
|
859 |
|
|
860 |
/**
|
|
861 |
* Prints a message to the current debug scope's logging stream if logging is enabled.
|
|
862 |
*
|
|
863 |
* @param format a format string
|
|
864 |
* @param arg the argument referenced by the format specifiers in {@code format}
|
|
865 |
*/
|
|
866 |
public void log(int logLevel, String format, int arg) {
|
|
867 |
if (currentScope != null) {
|
|
868 |
currentScope.log(logLevel, format, arg);
|
|
869 |
}
|
|
870 |
}
|
|
871 |
|
|
872 |
public void log(String format, Object arg1, Object arg2) {
|
|
873 |
log(BASIC_LEVEL, format, arg1, arg2);
|
|
874 |
}
|
|
875 |
|
|
876 |
/**
|
|
877 |
* @see #log(int, String, Object)
|
|
878 |
*/
|
|
879 |
public void log(int logLevel, String format, Object arg1, Object arg2) {
|
|
880 |
if (currentScope != null) {
|
|
881 |
currentScope.log(logLevel, format, arg1, arg2);
|
|
882 |
}
|
|
883 |
}
|
|
884 |
|
|
885 |
public void log(String format, int arg1, Object arg2) {
|
|
886 |
log(BASIC_LEVEL, format, arg1, arg2);
|
|
887 |
}
|
|
888 |
|
|
889 |
/**
|
|
890 |
* @see #log(int, String, Object)
|
|
891 |
*/
|
|
892 |
public void log(int logLevel, String format, int arg1, Object arg2) {
|
|
893 |
if (currentScope != null) {
|
|
894 |
currentScope.log(logLevel, format, arg1, arg2);
|
|
895 |
}
|
|
896 |
}
|
|
897 |
|
|
898 |
public void log(String format, Object arg1, int arg2) {
|
|
899 |
log(BASIC_LEVEL, format, arg1, arg2);
|
|
900 |
}
|
|
901 |
|
|
902 |
/**
|
|
903 |
* @see #log(int, String, Object)
|
|
904 |
*/
|
|
905 |
public void log(int logLevel, String format, Object arg1, int arg2) {
|
|
906 |
if (currentScope != null) {
|
|
907 |
currentScope.log(logLevel, format, arg1, arg2);
|
|
908 |
}
|
|
909 |
}
|
|
910 |
|
|
911 |
public void log(String format, int arg1, int arg2) {
|
|
912 |
log(BASIC_LEVEL, format, arg1, arg2);
|
|
913 |
}
|
|
914 |
|
|
915 |
/**
|
|
916 |
* @see #log(int, String, Object)
|
|
917 |
*/
|
|
918 |
public void log(int logLevel, String format, int arg1, int arg2) {
|
|
919 |
if (currentScope != null) {
|
|
920 |
currentScope.log(logLevel, format, arg1, arg2);
|
|
921 |
}
|
|
922 |
}
|
|
923 |
|
|
924 |
public void log(String format, Object arg1, Object arg2, Object arg3) {
|
|
925 |
log(BASIC_LEVEL, format, arg1, arg2, arg3);
|
|
926 |
}
|
|
927 |
|
|
928 |
/**
|
|
929 |
* @see #log(int, String, Object)
|
|
930 |
*/
|
|
931 |
public void log(int logLevel, String format, Object arg1, Object arg2, Object arg3) {
|
|
932 |
if (currentScope != null) {
|
|
933 |
currentScope.log(logLevel, format, arg1, arg2, arg3);
|
|
934 |
}
|
|
935 |
}
|
|
936 |
|
|
937 |
public void log(String format, int arg1, int arg2, int arg3) {
|
|
938 |
log(BASIC_LEVEL, format, arg1, arg2, arg3);
|
|
939 |
}
|
|
940 |
|
|
941 |
/**
|
|
942 |
* @see #log(int, String, Object)
|
|
943 |
*/
|
|
944 |
public void log(int logLevel, String format, int arg1, int arg2, int arg3) {
|
|
945 |
if (currentScope != null) {
|
|
946 |
currentScope.log(logLevel, format, arg1, arg2, arg3);
|
|
947 |
}
|
|
948 |
}
|
|
949 |
|
|
950 |
public void log(String format, Object arg1, Object arg2, Object arg3, Object arg4) {
|
|
951 |
log(BASIC_LEVEL, format, arg1, arg2, arg3, arg4);
|
|
952 |
}
|
|
953 |
|
|
954 |
/**
|
|
955 |
* @see #log(int, String, Object)
|
|
956 |
*/
|
|
957 |
public void log(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4) {
|
|
958 |
if (currentScope != null) {
|
|
959 |
currentScope.log(logLevel, format, arg1, arg2, arg3, arg4);
|
|
960 |
}
|
|
961 |
}
|
|
962 |
|
|
963 |
public void log(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
|
|
964 |
log(BASIC_LEVEL, format, arg1, arg2, arg3, arg4, arg5);
|
|
965 |
}
|
|
966 |
|
|
967 |
/**
|
|
968 |
* @see #log(int, String, Object)
|
|
969 |
*/
|
|
970 |
public void log(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
|
|
971 |
if (currentScope != null) {
|
|
972 |
currentScope.log(logLevel, format, arg1, arg2, arg3, arg4, arg5);
|
|
973 |
}
|
|
974 |
}
|
|
975 |
|
|
976 |
public void log(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) {
|
|
977 |
log(BASIC_LEVEL, format, arg1, arg2, arg3, arg4, arg5, arg6);
|
|
978 |
}
|
|
979 |
|
|
980 |
/**
|
|
981 |
* @see #log(int, String, Object)
|
|
982 |
*/
|
|
983 |
public void log(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) {
|
|
984 |
if (currentScope != null) {
|
|
985 |
currentScope.log(logLevel, format, arg1, arg2, arg3, arg4, arg5, arg6);
|
|
986 |
}
|
|
987 |
}
|
|
988 |
|
|
989 |
public void log(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) {
|
|
990 |
log(BASIC_LEVEL, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
|
|
991 |
}
|
|
992 |
|
|
993 |
public void log(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8) {
|
|
994 |
log(BASIC_LEVEL, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
|
|
995 |
}
|
|
996 |
|
|
997 |
/**
|
|
998 |
* @see #log(int, String, Object)
|
|
999 |
*/
|
|
1000 |
public void log(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) {
|
|
1001 |
if (currentScope != null) {
|
|
1002 |
currentScope.log(logLevel, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
|
|
1003 |
}
|
|
1004 |
}
|
|
1005 |
|
|
1006 |
public void log(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8) {
|
|
1007 |
if (currentScope != null) {
|
|
1008 |
currentScope.log(logLevel, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
|
|
1009 |
}
|
|
1010 |
}
|
|
1011 |
|
|
1012 |
public void log(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9) {
|
|
1013 |
log(BASIC_LEVEL, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
|
|
1014 |
}
|
|
1015 |
|
|
1016 |
public void log(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9) {
|
|
1017 |
if (currentScope != null) {
|
|
1018 |
currentScope.log(logLevel, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
|
|
1019 |
}
|
|
1020 |
}
|
|
1021 |
|
|
1022 |
public void log(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9, Object arg10) {
|
|
1023 |
log(BASIC_LEVEL, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
|
|
1024 |
}
|
|
1025 |
|
|
1026 |
public void log(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9, Object arg10) {
|
|
1027 |
if (currentScope != null) {
|
|
1028 |
currentScope.log(logLevel, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
|
|
1029 |
}
|
|
1030 |
}
|
|
1031 |
|
|
1032 |
public void logv(String format, Object... args) {
|
|
1033 |
logv(BASIC_LEVEL, format, args);
|
|
1034 |
}
|
|
1035 |
|
|
1036 |
/**
|
|
1037 |
* Prints a message to the current debug scope's logging stream. This method must only be called
|
|
1038 |
* if debugging scopes are {@linkplain DebugContext#areScopesEnabled() enabled} as it incurs
|
|
1039 |
* allocation at the call site. If possible, call one of the other {@code log()} methods in this
|
|
1040 |
* class that take a fixed number of parameters.
|
|
1041 |
*
|
|
1042 |
* @param format a format string
|
|
1043 |
* @param args the arguments referenced by the format specifiers in {@code format}
|
|
1044 |
*/
|
|
1045 |
public void logv(int logLevel, String format, Object... args) {
|
|
1046 |
if (currentScope == null) {
|
|
1047 |
throw new InternalError("Use of Debug.logv() must be guarded by a test of Debug.isEnabled()");
|
|
1048 |
}
|
|
1049 |
currentScope.log(logLevel, format, args);
|
|
1050 |
}
|
|
1051 |
|
|
1052 |
/**
|
|
1053 |
* This override exists to catch cases when {@link #log(String, Object)} is called with one
|
|
1054 |
* argument bound to a varargs method parameter. It will bind to this method instead of the
|
|
1055 |
* single arg variant and produce a deprecation warning instead of silently wrapping the
|
|
1056 |
* Object[] inside of another Object[].
|
|
1057 |
*/
|
|
1058 |
@Deprecated
|
|
1059 |
public void log(String format, Object[] args) {
|
|
1060 |
assert false : "shouldn't use this";
|
|
1061 |
log(BASIC_LEVEL, format, args);
|
|
1062 |
}
|
|
1063 |
|
|
1064 |
/**
|
|
1065 |
* This override exists to catch cases when {@link #log(int, String, Object)} is called with one
|
|
1066 |
* argument bound to a varargs method parameter. It will bind to this method instead of the
|
|
1067 |
* single arg variant and produce a deprecation warning instead of silently wrapping the
|
|
1068 |
* Object[] inside of another Object[].
|
|
1069 |
*/
|
|
1070 |
@Deprecated
|
|
1071 |
public void log(int logLevel, String format, Object[] args) {
|
|
1072 |
assert false : "shouldn't use this";
|
|
1073 |
logv(logLevel, format, args);
|
|
1074 |
}
|
|
1075 |
|
|
1076 |
/**
|
|
1077 |
* Forces an unconditional dump. This method exists mainly for debugging. It can also be used to
|
|
1078 |
* force a graph dump from IDEs that support invoking a Java method while at a breakpoint.
|
|
1079 |
*/
|
|
1080 |
public void forceDump(Object object, String format, Object... args) {
|
|
1081 |
DebugConfig config = currentConfig;
|
|
1082 |
Collection<DebugDumpHandler> dumpHandlers;
|
|
1083 |
boolean closeAfterDump;
|
|
1084 |
if (config != null) {
|
|
1085 |
dumpHandlers = config.dumpHandlers();
|
|
1086 |
closeAfterDump = false;
|
|
1087 |
} else {
|
|
1088 |
OptionValues options = getOptions();
|
|
1089 |
dumpHandlers = new ArrayList<>();
|
|
1090 |
for (DebugHandlersFactory factory : DebugHandlersFactory.LOADER) {
|
|
1091 |
for (DebugHandler handler : factory.createHandlers(options)) {
|
|
1092 |
if (handler instanceof DebugDumpHandler) {
|
|
1093 |
dumpHandlers.add((DebugDumpHandler) handler);
|
|
1094 |
}
|
|
1095 |
}
|
|
1096 |
}
|
|
1097 |
closeAfterDump = true;
|
|
1098 |
}
|
|
1099 |
for (DebugDumpHandler dumpHandler : dumpHandlers) {
|
|
1100 |
dumpHandler.dump(this, object, format, args);
|
|
1101 |
if (closeAfterDump) {
|
|
1102 |
dumpHandler.close();
|
|
1103 |
}
|
|
1104 |
}
|
|
1105 |
}
|
|
1106 |
|
|
1107 |
public void dump(int dumpLevel, Object object, String msg) {
|
|
1108 |
if (currentScope != null && currentScope.isDumpEnabled(dumpLevel)) {
|
|
1109 |
currentScope.dump(dumpLevel, object, msg);
|
|
1110 |
}
|
|
1111 |
}
|
|
1112 |
|
|
1113 |
public void dump(int dumpLevel, Object object, String format, Object arg) {
|
|
1114 |
if (currentScope != null && currentScope.isDumpEnabled(dumpLevel)) {
|
|
1115 |
currentScope.dump(dumpLevel, object, format, arg);
|
|
1116 |
}
|
|
1117 |
}
|
|
1118 |
|
|
1119 |
public void dump(int dumpLevel, Object object, String format, Object arg1, Object arg2) {
|
|
1120 |
if (currentScope != null && currentScope.isDumpEnabled(dumpLevel)) {
|
|
1121 |
currentScope.dump(dumpLevel, object, format, arg1, arg2);
|
|
1122 |
}
|
|
1123 |
}
|
|
1124 |
|
|
1125 |
public void dump(int dumpLevel, Object object, String format, Object arg1, Object arg2, Object arg3) {
|
|
1126 |
if (currentScope != null && currentScope.isDumpEnabled(dumpLevel)) {
|
|
1127 |
currentScope.dump(dumpLevel, object, format, arg1, arg2, arg3);
|
|
1128 |
}
|
|
1129 |
}
|
|
1130 |
|
|
1131 |
/**
|
|
1132 |
* This override exists to catch cases when {@link #dump(int, Object, String, Object)} is called
|
|
1133 |
* with one argument bound to a varargs method parameter. It will bind to this method instead of
|
|
1134 |
* the single arg variant and produce a deprecation warning instead of silently wrapping the
|
|
1135 |
* Object[] inside of another Object[].
|
|
1136 |
*/
|
|
1137 |
@Deprecated
|
|
1138 |
public void dump(int dumpLevel, Object object, String format, Object[] args) {
|
|
1139 |
assert false : "shouldn't use this";
|
|
1140 |
if (currentScope != null && currentScope.isDumpEnabled(dumpLevel)) {
|
|
1141 |
currentScope.dump(dumpLevel, object, format, args);
|
|
1142 |
}
|
|
1143 |
}
|
|
1144 |
|
|
1145 |
/**
|
|
1146 |
* Calls all {@link DebugVerifyHandler}s in the current {@linkplain #getConfig() config} to
|
|
1147 |
* perform verification on a given object.
|
|
1148 |
*
|
|
1149 |
* @param object object to verify
|
|
1150 |
* @param message description of verification context
|
|
1151 |
*
|
|
1152 |
* @see DebugVerifyHandler#verify
|
|
1153 |
*/
|
|
1154 |
public void verify(Object object, String message) {
|
|
1155 |
if (currentScope != null && currentScope.isVerifyEnabled()) {
|
|
1156 |
currentScope.verify(object, message);
|
|
1157 |
}
|
|
1158 |
}
|
|
1159 |
|
|
1160 |
/**
|
|
1161 |
* Calls all {@link DebugVerifyHandler}s in the current {@linkplain #getConfig() config} to
|
|
1162 |
* perform verification on a given object.
|
|
1163 |
*
|
|
1164 |
* @param object object to verify
|
|
1165 |
* @param format a format string for the description of the verification context
|
|
1166 |
* @param arg the argument referenced by the format specifiers in {@code format}
|
|
1167 |
*
|
|
1168 |
* @see DebugVerifyHandler#verify
|
|
1169 |
*/
|
|
1170 |
public void verify(Object object, String format, Object arg) {
|
|
1171 |
if (currentScope != null && currentScope.isVerifyEnabled()) {
|
|
1172 |
currentScope.verify(object, format, arg);
|
|
1173 |
}
|
|
1174 |
}
|
|
1175 |
|
|
1176 |
/**
|
|
1177 |
* This override exists to catch cases when {@link #verify(Object, String, Object)} is called
|
|
1178 |
* with one argument bound to a varargs method parameter. It will bind to this method instead of
|
|
1179 |
* the single arg variant and produce a deprecation warning instead of silently wrapping the
|
|
1180 |
* Object[] inside of another Object[].
|
|
1181 |
*/
|
|
1182 |
@Deprecated
|
|
1183 |
public void verify(Object object, String format, Object[] args) {
|
|
1184 |
assert false : "shouldn't use this";
|
|
1185 |
if (currentScope != null && currentScope.isVerifyEnabled()) {
|
|
1186 |
currentScope.verify(object, format, args);
|
|
1187 |
}
|
|
1188 |
}
|
|
1189 |
|
|
1190 |
/**
|
|
1191 |
* Opens a new indentation level (by adding some spaces) based on the current indentation level.
|
|
1192 |
* This should be used in a {@linkplain Indent try-with-resources} pattern.
|
|
1193 |
*
|
|
1194 |
* @return an object that reverts to the current indentation level when
|
|
1195 |
* {@linkplain Indent#close() closed} or null if debugging is disabled
|
|
1196 |
* @see #logAndIndent(int, String)
|
|
1197 |
* @see #logAndIndent(int, String, Object)
|
|
1198 |
*/
|
|
1199 |
public Indent indent() {
|
|
1200 |
if (currentScope != null) {
|
|
1201 |
return currentScope.pushIndentLogger();
|
|
1202 |
}
|
|
1203 |
return null;
|
|
1204 |
}
|
|
1205 |
|
|
1206 |
public Indent logAndIndent(String msg) {
|
|
1207 |
return logAndIndent(BASIC_LEVEL, msg);
|
|
1208 |
}
|
|
1209 |
|
|
1210 |
/**
|
|
1211 |
* A convenience function which combines {@link #log(String)} and {@link #indent()}.
|
|
1212 |
*
|
|
1213 |
* @param msg the message to log
|
|
1214 |
* @return an object that reverts to the current indentation level when
|
|
1215 |
* {@linkplain Indent#close() closed} or null if debugging is disabled
|
|
1216 |
*/
|
|
1217 |
public Indent logAndIndent(int logLevel, String msg) {
|
|
1218 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1219 |
return logvAndIndentInternal(logLevel, msg);
|
|
1220 |
}
|
|
1221 |
return null;
|
|
1222 |
}
|
|
1223 |
|
|
1224 |
public Indent logAndIndent(String format, Object arg) {
|
|
1225 |
return logAndIndent(BASIC_LEVEL, format, arg);
|
|
1226 |
}
|
|
1227 |
|
|
1228 |
/**
|
|
1229 |
* A convenience function which combines {@link #log(String, Object)} and {@link #indent()}.
|
|
1230 |
*
|
|
1231 |
* @param format a format string
|
|
1232 |
* @param arg the argument referenced by the format specifiers in {@code format}
|
|
1233 |
* @return an object that reverts to the current indentation level when
|
|
1234 |
* {@linkplain Indent#close() closed} or null if debugging is disabled
|
|
1235 |
*/
|
|
1236 |
public Indent logAndIndent(int logLevel, String format, Object arg) {
|
|
1237 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1238 |
return logvAndIndentInternal(logLevel, format, arg);
|
|
1239 |
}
|
|
1240 |
return null;
|
|
1241 |
}
|
|
1242 |
|
|
1243 |
public Indent logAndIndent(String format, int arg) {
|
|
1244 |
return logAndIndent(BASIC_LEVEL, format, arg);
|
|
1245 |
}
|
|
1246 |
|
|
1247 |
/**
|
|
1248 |
* A convenience function which combines {@link #log(String, Object)} and {@link #indent()}.
|
|
1249 |
*
|
|
1250 |
* @param format a format string
|
|
1251 |
* @param arg the argument referenced by the format specifiers in {@code format}
|
|
1252 |
* @return an object that reverts to the current indentation level when
|
|
1253 |
* {@linkplain Indent#close() closed} or null if debugging is disabled
|
|
1254 |
*/
|
|
1255 |
public Indent logAndIndent(int logLevel, String format, int arg) {
|
|
1256 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1257 |
return logvAndIndentInternal(logLevel, format, arg);
|
|
1258 |
}
|
|
1259 |
return null;
|
|
1260 |
}
|
|
1261 |
|
|
1262 |
public Indent logAndIndent(String format, int arg1, Object arg2) {
|
|
1263 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2);
|
|
1264 |
}
|
|
1265 |
|
|
1266 |
/**
|
|
1267 |
* @see #logAndIndent(int, String, Object)
|
|
1268 |
*/
|
|
1269 |
public Indent logAndIndent(int logLevel, String format, int arg1, Object arg2) {
|
|
1270 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1271 |
return logvAndIndentInternal(logLevel, format, arg1, arg2);
|
|
1272 |
}
|
|
1273 |
return null;
|
|
1274 |
}
|
|
1275 |
|
|
1276 |
public Indent logAndIndent(String format, Object arg1, int arg2) {
|
|
1277 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2);
|
|
1278 |
}
|
|
1279 |
|
|
1280 |
/**
|
|
1281 |
* @see #logAndIndent(int, String, Object)
|
|
1282 |
*/
|
|
1283 |
public Indent logAndIndent(int logLevel, String format, Object arg1, int arg2) {
|
|
1284 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1285 |
return logvAndIndentInternal(logLevel, format, arg1, arg2);
|
|
1286 |
}
|
|
1287 |
return null;
|
|
1288 |
}
|
|
1289 |
|
|
1290 |
public Indent logAndIndent(String format, int arg1, int arg2) {
|
|
1291 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2);
|
|
1292 |
}
|
|
1293 |
|
|
1294 |
/**
|
|
1295 |
* @see #logAndIndent(int, String, Object)
|
|
1296 |
*/
|
|
1297 |
public Indent logAndIndent(int logLevel, String format, int arg1, int arg2) {
|
|
1298 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1299 |
return logvAndIndentInternal(logLevel, format, arg1, arg2);
|
|
1300 |
}
|
|
1301 |
return null;
|
|
1302 |
}
|
|
1303 |
|
|
1304 |
public Indent logAndIndent(String format, Object arg1, Object arg2) {
|
|
1305 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2);
|
|
1306 |
}
|
|
1307 |
|
|
1308 |
/**
|
|
1309 |
* @see #logAndIndent(int, String, Object)
|
|
1310 |
*/
|
|
1311 |
public Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2) {
|
|
1312 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1313 |
return logvAndIndentInternal(logLevel, format, arg1, arg2);
|
|
1314 |
}
|
|
1315 |
return null;
|
|
1316 |
}
|
|
1317 |
|
|
1318 |
public Indent logAndIndent(String format, Object arg1, Object arg2, Object arg3) {
|
|
1319 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2, arg3);
|
|
1320 |
}
|
|
1321 |
|
|
1322 |
/**
|
|
1323 |
* @see #logAndIndent(int, String, Object)
|
|
1324 |
*/
|
|
1325 |
public Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2, Object arg3) {
|
|
1326 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1327 |
return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3);
|
|
1328 |
}
|
|
1329 |
return null;
|
|
1330 |
}
|
|
1331 |
|
|
1332 |
public Indent logAndIndent(String format, int arg1, int arg2, int arg3) {
|
|
1333 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2, arg3);
|
|
1334 |
}
|
|
1335 |
|
|
1336 |
/**
|
|
1337 |
* @see #logAndIndent(int, String, Object)
|
|
1338 |
*/
|
|
1339 |
public Indent logAndIndent(int logLevel, String format, int arg1, int arg2, int arg3) {
|
|
1340 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1341 |
return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3);
|
|
1342 |
}
|
|
1343 |
return null;
|
|
1344 |
}
|
|
1345 |
|
|
1346 |
public Indent logAndIndent(String format, Object arg1, int arg2, int arg3) {
|
|
1347 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2, arg3);
|
|
1348 |
}
|
|
1349 |
|
|
1350 |
/**
|
|
1351 |
* @see #logAndIndent(int, String, Object)
|
|
1352 |
*/
|
|
1353 |
public Indent logAndIndent(int logLevel, String format, Object arg1, int arg2, int arg3) {
|
|
1354 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1355 |
return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3);
|
|
1356 |
}
|
|
1357 |
return null;
|
|
1358 |
}
|
|
1359 |
|
|
1360 |
public Indent logAndIndent(String format, Object arg1, Object arg2, Object arg3, Object arg4) {
|
|
1361 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2, arg3, arg4);
|
|
1362 |
}
|
|
1363 |
|
|
1364 |
/**
|
|
1365 |
* @see #logAndIndent(int, String, Object)
|
|
1366 |
*/
|
|
1367 |
public Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4) {
|
|
1368 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1369 |
return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3, arg4);
|
|
1370 |
}
|
|
1371 |
return null;
|
|
1372 |
}
|
|
1373 |
|
|
1374 |
public Indent logAndIndent(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
|
|
1375 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2, arg3, arg4, arg5);
|
|
1376 |
}
|
|
1377 |
|
|
1378 |
/**
|
|
1379 |
* @see #logAndIndent(int, String, Object)
|
|
1380 |
*/
|
|
1381 |
public Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
|
|
1382 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1383 |
return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3, arg4, arg5);
|
|
1384 |
}
|
|
1385 |
return null;
|
|
1386 |
}
|
|
1387 |
|
|
1388 |
public Indent logAndIndent(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) {
|
|
1389 |
return logAndIndent(BASIC_LEVEL, format, arg1, arg2, arg3, arg4, arg5, arg6);
|
|
1390 |
}
|
|
1391 |
|
|
1392 |
/**
|
|
1393 |
* @see #logAndIndent(int, String, Object)
|
|
1394 |
*/
|
|
1395 |
public Indent logAndIndent(int logLevel, String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) {
|
|
1396 |
if (currentScope != null && isLogEnabled(logLevel)) {
|
|
1397 |
return logvAndIndentInternal(logLevel, format, arg1, arg2, arg3, arg4, arg5, arg6);
|
|
1398 |
}
|
|
1399 |
return null;
|
|
1400 |
}
|
|
1401 |
|
|
1402 |
/**
|
|
1403 |
* A convenience function which combines {@link #logv(int, String, Object...)} and
|
|
1404 |
* {@link #indent()}.
|
|
1405 |
*
|
|
1406 |
* @param format a format string
|
|
1407 |
* @param args the arguments referenced by the format specifiers in {@code format}
|
|
1408 |
* @return an object that reverts to the current indentation level when
|
|
1409 |
* {@linkplain Indent#close() closed} or null if debugging is disabled
|
|
1410 |
*/
|
|
1411 |
public Indent logvAndIndent(int logLevel, String format, Object... args) {
|
|
1412 |
if (currentScope != null) {
|
|
1413 |
if (isLogEnabled(logLevel)) {
|
|
1414 |
return logvAndIndentInternal(logLevel, format, args);
|
|
1415 |
}
|
|
1416 |
return null;
|
|
1417 |
}
|
|
1418 |
throw new InternalError("Use of Debug.logvAndIndent() must be guarded by a test of Debug.isEnabled()");
|
|
1419 |
}
|
|
1420 |
|
|
1421 |
private Indent logvAndIndentInternal(int logLevel, String format, Object... args) {
|
|
1422 |
assert currentScope != null && isLogEnabled(logLevel) : "must have checked Debug.isLogEnabled()";
|
|
1423 |
currentScope.log(logLevel, format, args);
|
|
1424 |
return currentScope.pushIndentLogger();
|
|
1425 |
}
|
|
1426 |
|
|
1427 |
/**
|
|
1428 |
* This override exists to catch cases when {@link #logAndIndent(String, Object)} is called with
|
|
1429 |
* one argument bound to a varargs method parameter. It will bind to this method instead of the
|
|
1430 |
* single arg variant and produce a deprecation warning instead of silently wrapping the
|
|
1431 |
* Object[] inside of another Object[].
|
|
1432 |
*/
|
|
1433 |
@Deprecated
|
|
1434 |
public void logAndIndent(String format, Object[] args) {
|
|
1435 |
assert false : "shouldn't use this";
|
|
1436 |
logAndIndent(BASIC_LEVEL, format, args);
|
|
1437 |
}
|
|
1438 |
|
|
1439 |
/**
|
|
1440 |
* This override exists to catch cases when {@link #logAndIndent(int, String, Object)} is called
|
|
1441 |
* with one argument bound to a varargs method parameter. It will bind to this method instead of
|
|
1442 |
* the single arg variant and produce a deprecation warning instead of silently wrapping the
|
|
1443 |
* Object[] inside of another Object[].
|
|
1444 |
*/
|
|
1445 |
@Deprecated
|
|
1446 |
public void logAndIndent(int logLevel, String format, Object[] args) {
|
|
1447 |
assert false : "shouldn't use this";
|
|
1448 |
logvAndIndent(logLevel, format, args);
|
|
1449 |
}
|
|
1450 |
|
|
1451 |
public Iterable<Object> context() {
|
|
1452 |
if (currentScope != null) {
|
|
1453 |
return currentScope.getCurrentContext();
|
|
1454 |
} else {
|
|
1455 |
return Collections.emptyList();
|
|
1456 |
}
|
|
1457 |
}
|
|
1458 |
|
|
1459 |
@SuppressWarnings("unchecked")
|
|
1460 |
public <T> List<T> contextSnapshot(Class<T> clazz) {
|
|
1461 |
if (currentScope != null) {
|
|
1462 |
List<T> result = new ArrayList<>();
|
|
1463 |
for (Object o : context()) {
|
|
1464 |
if (clazz.isInstance(o)) {
|
|
1465 |
result.add((T) o);
|
|
1466 |
}
|
|
1467 |
}
|
|
1468 |
return result;
|
|
1469 |
} else {
|
|
1470 |
return Collections.emptyList();
|
|
1471 |
}
|
|
1472 |
}
|
|
1473 |
|
|
1474 |
/**
|
|
1475 |
* Searches the current debug scope, bottom up, for a context object that is an instance of a
|
|
1476 |
* given type. The first such object found is returned.
|
|
1477 |
*/
|
|
1478 |
@SuppressWarnings("unchecked")
|
|
1479 |
public <T> T contextLookup(Class<T> clazz) {
|
|
1480 |
if (currentScope != null) {
|
|
1481 |
for (Object o : context()) {
|
|
1482 |
if (clazz.isInstance(o)) {
|
|
1483 |
return ((T) o);
|
|
1484 |
}
|
|
1485 |
}
|
|
1486 |
}
|
|
1487 |
return null;
|
|
1488 |
}
|
|
1489 |
|
|
1490 |
/**
|
|
1491 |
* Searches the current debug scope, top down, for a context object that is an instance of a
|
|
1492 |
* given type. The first such object found is returned.
|
|
1493 |
*/
|
|
1494 |
@SuppressWarnings("unchecked")
|
|
1495 |
public <T> T contextLookupTopdown(Class<T> clazz) {
|
|
1496 |
if (currentScope != null) {
|
|
1497 |
T found = null;
|
|
1498 |
for (Object o : context()) {
|
|
1499 |
if (clazz.isInstance(o)) {
|
|
1500 |
found = (T) o;
|
|
1501 |
}
|
|
1502 |
}
|
|
1503 |
return found;
|
|
1504 |
}
|
|
1505 |
return null;
|
|
1506 |
}
|
|
1507 |
|
|
1508 |
/**
|
|
1509 |
* Creates a {@linkplain MemUseTrackerKey memory use tracker}.
|
|
1510 |
*/
|
|
1511 |
public static MemUseTrackerKey memUseTracker(CharSequence name) {
|
|
1512 |
return createMemUseTracker("%s", name, null);
|
|
1513 |
}
|
|
1514 |
|
|
1515 |
/**
|
|
1516 |
* Creates a debug memory use tracker. Invoking this method is equivalent to:
|
|
1517 |
*
|
|
1518 |
* <pre>
|
|
1519 |
* Debug.memUseTracker(format, arg, null)
|
|
1520 |
* </pre>
|
|
1521 |
*
|
|
1522 |
* except that the string formatting only happens if mem tracking is enabled.
|
|
1523 |
*
|
|
1524 |
* @see #counter(String, Object, Object)
|
|
1525 |
*/
|
|
1526 |
public static MemUseTrackerKey memUseTracker(String format, Object arg) {
|
|
1527 |
return createMemUseTracker(format, arg, null);
|
|
1528 |
}
|
|
1529 |
|
|
1530 |
/**
|
|
1531 |
* Creates a debug memory use tracker. Invoking this method is equivalent to:
|
|
1532 |
*
|
|
1533 |
* <pre>
|
|
1534 |
* Debug.memUseTracker(String.format(format, arg1, arg2))
|
|
1535 |
* </pre>
|
|
1536 |
*
|
|
1537 |
* except that the string formatting only happens if memory use tracking is enabled. In
|
|
1538 |
* addition, each argument is subject to the following type based conversion before being passed
|
|
1539 |
* as an argument to {@link String#format(String, Object...)}:
|
|
1540 |
*
|
|
1541 |
* <pre>
|
|
1542 |
* Type | Conversion
|
|
1543 |
* ------------------+-----------------
|
|
1544 |
* java.lang.Class | arg.getSimpleName()
|
|
1545 |
* |
|
|
1546 |
* </pre>
|
|
1547 |
*
|
|
1548 |
* @see #memUseTracker(CharSequence)
|
|
1549 |
*/
|
|
1550 |
public static MemUseTrackerKey memUseTracker(String format, Object arg1, Object arg2) {
|
|
1551 |
return createMemUseTracker(format, arg1, arg2);
|
|
1552 |
}
|
|
1553 |
|
|
1554 |
private static MemUseTrackerKey createMemUseTracker(String format, Object arg1, Object arg2) {
|
|
1555 |
return new MemUseTrackerKeyImpl(format, arg1, arg2);
|
|
1556 |
}
|
|
1557 |
|
|
1558 |
/**
|
|
1559 |
* Creates a {@linkplain CounterKey counter}.
|
|
1560 |
*/
|
|
1561 |
public static CounterKey counter(CharSequence name) {
|
|
1562 |
return createCounter("%s", name, null);
|
|
1563 |
}
|
|
1564 |
|
|
1565 |
/**
|
|
1566 |
* Gets a tally of the metric values in this context and a given tally.
|
|
1567 |
*
|
|
1568 |
* @param tally the tally to which the metrics should be added
|
|
1569 |
* @return a tally of the metric values in this context and {@code tally}. This will be
|
|
1570 |
* {@code tally} if this context has no metric values or {@code tally} is wide enough to
|
|
1571 |
* hold all the metric values in this context otherwise it will be a new array.
|
|
1572 |
*/
|
|
1573 |
public long[] addValuesTo(long[] tally) {
|
|
1574 |
if (metricValues == null) {
|
|
1575 |
return tally;
|
|
1576 |
}
|
|
1577 |
if (tally == null) {
|
|
1578 |
return metricValues.clone();
|
|
1579 |
} else if (metricValues.length >= tally.length) {
|
|
1580 |
long[] newTally = metricValues.clone();
|
|
1581 |
for (int i = 0; i < tally.length; i++) {
|
|
1582 |
newTally[i] += tally[i];
|
|
1583 |
}
|
|
1584 |
return newTally;
|
|
1585 |
} else {
|
|
1586 |
for (int i = 0; i < metricValues.length; i++) {
|
|
1587 |
tally[i] += metricValues[i];
|
|
1588 |
}
|
|
1589 |
return tally;
|
|
1590 |
}
|
|
1591 |
}
|
|
1592 |
|
|
1593 |
/**
|
|
1594 |
* Creates and returns a sorted map from metric names to their values in {@code values}.
|
|
1595 |
*
|
|
1596 |
* @param values values for metrics in the {@link KeyRegistry}.
|
|
1597 |
*/
|
|
1598 |
public static EconomicMap<MetricKey, Long> convertValuesToKeyValueMap(long[] values) {
|
|
1599 |
List<MetricKey> keys = KeyRegistry.getKeys();
|
|
1600 |
Collections.sort(keys, MetricKey.NAME_COMPARATOR);
|
|
1601 |
EconomicMap<MetricKey, Long> res = EconomicMap.create(keys.size());
|
|
1602 |
for (MetricKey key : keys) {
|
|
1603 |
int index = ((AbstractKey) key).getIndex();
|
|
1604 |
if (index >= values.length) {
|
|
1605 |
res.put(key, 0L);
|
|
1606 |
} else {
|
|
1607 |
res.put(key, values[index]);
|
|
1608 |
}
|
|
1609 |
}
|
|
1610 |
return res;
|
|
1611 |
}
|
|
1612 |
|
|
1613 |
void setMetricValue(int keyIndex, long l) {
|
|
1614 |
ensureMetricValuesSize(keyIndex);
|
|
1615 |
metricValues[keyIndex] = l;
|
|
1616 |
}
|
|
1617 |
|
|
1618 |
long getMetricValue(int keyIndex) {
|
|
1619 |
if (metricValues == null || metricValues.length <= keyIndex) {
|
|
1620 |
return 0L;
|
|
1621 |
}
|
|
1622 |
return metricValues[keyIndex];
|
|
1623 |
}
|
|
1624 |
|
|
1625 |
private void ensureMetricValuesSize(int index) {
|
|
1626 |
if (metricValues == null) {
|
|
1627 |
metricValues = new long[index + 1];
|
|
1628 |
}
|
|
1629 |
if (metricValues.length <= index) {
|
|
1630 |
metricValues = Arrays.copyOf(metricValues, index + 1);
|
|
1631 |
}
|
|
1632 |
}
|
|
1633 |
|
|
1634 |
public static String applyFormattingFlagsAndWidth(String s, int flags, int width) {
|
|
1635 |
if (flags == 0 && width < 0) {
|
|
1636 |
return s;
|
|
1637 |
}
|
|
1638 |
StringBuilder sb = new StringBuilder(s);
|
|
1639 |
|
|
1640 |
// apply width and justification
|
|
1641 |
int len = sb.length();
|
|
1642 |
if (len < width) {
|
|
1643 |
for (int i = 0; i < width - len; i++) {
|
|
1644 |
if ((flags & LEFT_JUSTIFY) == LEFT_JUSTIFY) {
|
|
1645 |
sb.append(' ');
|
|
1646 |
} else {
|
|
1647 |
sb.insert(0, ' ');
|
|
1648 |
}
|
|
1649 |
}
|
|
1650 |
}
|
|
1651 |
|
|
1652 |
String res = sb.toString();
|
|
1653 |
if ((flags & UPPERCASE) == UPPERCASE) {
|
|
1654 |
res = res.toUpperCase();
|
|
1655 |
}
|
|
1656 |
return res;
|
|
1657 |
}
|
|
1658 |
|
|
1659 |
/**
|
|
1660 |
* Creates a debug counter. Invoking this method is equivalent to:
|
|
1661 |
*
|
|
1662 |
* <pre>
|
|
1663 |
* Debug.counter(format, arg, null)
|
|
1664 |
* </pre>
|
|
1665 |
*
|
|
1666 |
* except that the string formatting only happens if count is enabled.
|
|
1667 |
*
|
|
1668 |
* @see #counter(String, Object, Object)
|
|
1669 |
*/
|
|
1670 |
public static CounterKey counter(String format, Object arg) {
|
|
1671 |
return createCounter(format, arg, null);
|
|
1672 |
}
|
|
1673 |
|
|
1674 |
/**
|
|
1675 |
* Creates a debug counter. Invoking this method is equivalent to:
|
|
1676 |
*
|
|
1677 |
* <pre>
|
|
1678 |
* Debug.counter(String.format(format, arg1, arg2))
|
|
1679 |
* </pre>
|
|
1680 |
*
|
|
1681 |
* except that the string formatting only happens if count is enabled. In addition, each
|
|
1682 |
* argument is subject to the following type based conversion before being passed as an argument
|
|
1683 |
* to {@link String#format(String, Object...)}:
|
|
1684 |
*
|
|
1685 |
* <pre>
|
|
1686 |
* Type | Conversion
|
|
1687 |
* ------------------+-----------------
|
|
1688 |
* java.lang.Class | arg.getSimpleName()
|
|
1689 |
* |
|
|
1690 |
* </pre>
|
|
1691 |
*
|
|
1692 |
* @see #counter(CharSequence)
|
|
1693 |
*/
|
|
1694 |
public static CounterKey counter(String format, Object arg1, Object arg2) {
|
|
1695 |
return createCounter(format, arg1, arg2);
|
|
1696 |
}
|
|
1697 |
|
|
1698 |
private static CounterKey createCounter(String format, Object arg1, Object arg2) {
|
|
1699 |
return new CounterKeyImpl(format, arg1, arg2);
|
|
1700 |
}
|
|
1701 |
|
|
1702 |
public DebugConfig getConfig() {
|
|
1703 |
return currentConfig;
|
|
1704 |
}
|
|
1705 |
|
|
1706 |
/**
|
|
1707 |
* Creates a {@linkplain TimerKey timer}.
|
|
1708 |
* <p>
|
|
1709 |
* A disabled timer has virtually no overhead.
|
|
1710 |
*/
|
|
1711 |
public static TimerKey timer(CharSequence name) {
|
|
1712 |
return createTimer("%s", name, null);
|
|
1713 |
}
|
|
1714 |
|
|
1715 |
/**
|
|
1716 |
* Creates a debug timer. Invoking this method is equivalent to:
|
|
1717 |
*
|
|
1718 |
* <pre>
|
|
1719 |
* Debug.timer(format, arg, null)
|
|
1720 |
* </pre>
|
|
1721 |
*
|
|
1722 |
* except that the string formatting only happens if timing is enabled.
|
|
1723 |
*
|
|
1724 |
* @see #timer(String, Object, Object)
|
|
1725 |
*/
|
|
1726 |
public static TimerKey timer(String format, Object arg) {
|
|
1727 |
return createTimer(format, arg, null);
|
|
1728 |
}
|
|
1729 |
|
|
1730 |
/**
|
|
1731 |
* Creates a debug timer. Invoking this method is equivalent to:
|
|
1732 |
*
|
|
1733 |
* <pre>
|
|
1734 |
* Debug.timer(String.format(format, arg1, arg2))
|
|
1735 |
* </pre>
|
|
1736 |
*
|
|
1737 |
* except that the string formatting only happens if timing is enabled. In addition, each
|
|
1738 |
* argument is subject to the following type based conversion before being passed as an argument
|
|
1739 |
* to {@link String#format(String, Object...)}:
|
|
1740 |
*
|
|
1741 |
* <pre>
|
|
1742 |
* Type | Conversion
|
|
1743 |
* ------------------+-----------------
|
|
1744 |
* java.lang.Class | arg.getSimpleName()
|
|
1745 |
* |
|
|
1746 |
* </pre>
|
|
1747 |
*
|
|
1748 |
* @see #timer(CharSequence)
|
|
1749 |
*/
|
|
1750 |
public static TimerKey timer(String format, Object arg1, Object arg2) {
|
|
1751 |
return createTimer(format, arg1, arg2);
|
|
1752 |
}
|
|
1753 |
|
|
1754 |
/**
|
|
1755 |
* There are paths where construction of formatted class names are common and the code below is
|
|
1756 |
* surprisingly expensive, so compute it once and cache it.
|
|
1757 |
*/
|
|
1758 |
private static final ClassValue<String> formattedClassName = new ClassValue<String>() {
|
|
1759 |
@Override
|
|
1760 |
protected String computeValue(Class<?> c) {
|
|
1761 |
final String simpleName = c.getSimpleName();
|
|
1762 |
Class<?> enclosingClass = c.getEnclosingClass();
|
|
1763 |
if (enclosingClass != null) {
|
|
1764 |
String prefix = "";
|
|
1765 |
while (enclosingClass != null) {
|
|
1766 |
prefix = enclosingClass.getSimpleName() + "_" + prefix;
|
|
1767 |
enclosingClass = enclosingClass.getEnclosingClass();
|
|
1768 |
}
|
|
1769 |
return prefix + simpleName;
|
|
1770 |
} else {
|
|
1771 |
return simpleName;
|
|
1772 |
}
|
|
1773 |
}
|
|
1774 |
};
|
|
1775 |
|
|
1776 |
public static Object convertFormatArg(Object arg) {
|
|
1777 |
if (arg instanceof Class) {
|
|
1778 |
return formattedClassName.get((Class<?>) arg);
|
|
1779 |
}
|
|
1780 |
return arg;
|
|
1781 |
}
|
|
1782 |
|
|
1783 |
static String formatDebugName(String format, Object arg1, Object arg2) {
|
|
1784 |
return String.format(format, convertFormatArg(arg1), convertFormatArg(arg2));
|
|
1785 |
}
|
|
1786 |
|
|
1787 |
private static TimerKey createTimer(String format, Object arg1, Object arg2) {
|
|
1788 |
return new TimerKeyImpl(format, arg1, arg2);
|
|
1789 |
}
|
|
1790 |
|
|
1791 |
/**
|
|
1792 |
* Represents a debug scope entered by {@link DebugContext#scope(Object)} or
|
|
1793 |
* {@link DebugContext#sandbox(CharSequence, DebugConfig, Object...)}. Leaving the scope is
|
|
1794 |
* achieved via {@link #close()}.
|
|
1795 |
*/
|
|
1796 |
public interface Scope extends AutoCloseable {
|
|
1797 |
/**
|
|
1798 |
* Gets the names of this scope and its ancestors separated by {@code '.'}.
|
|
1799 |
*/
|
|
1800 |
String getQualifiedName();
|
|
1801 |
|
|
1802 |
Iterable<Object> getCurrentContext();
|
|
1803 |
|
|
1804 |
@Override
|
|
1805 |
void close();
|
|
1806 |
}
|
|
1807 |
|
|
1808 |
boolean isTimerEnabled(TimerKeyImpl key) {
|
|
1809 |
if (!metricsEnabled) {
|
|
1810 |
// Pulling this common case out of `isTimerEnabledSlow`
|
|
1811 |
// gives C1 a better chance to inline this method.
|
|
1812 |
return false;
|
|
1813 |
}
|
|
1814 |
return isTimerEnabledSlow(key);
|
|
1815 |
}
|
|
1816 |
|
|
1817 |
private boolean isTimerEnabledSlow(AbstractKey key) {
|
|
1818 |
if (currentScope != null && currentScope.isTimeEnabled()) {
|
|
1819 |
return true;
|
|
1820 |
}
|
|
1821 |
if (immutable.listMetrics) {
|
|
1822 |
key.ensureInitialized();
|
|
1823 |
}
|
|
1824 |
assert checkNoConcurrentAccess();
|
|
1825 |
EconomicSet<String> unscoped = immutable.unscopedTimers;
|
|
1826 |
return unscoped != null && (unscoped.isEmpty() || unscoped.contains(key.getName()));
|
|
1827 |
}
|
|
1828 |
|
|
1829 |
/**
|
|
1830 |
* Determines if a given timer is enabled in the current scope.
|
|
1831 |
*/
|
|
1832 |
boolean isCounterEnabled(CounterKeyImpl key) {
|
|
1833 |
if (!metricsEnabled) {
|
|
1834 |
// Pulling this common case out of `isCounterEnabledSlow`
|
|
1835 |
// gives C1 a better chance to inline this method.
|
|
1836 |
return false;
|
|
1837 |
}
|
|
1838 |
return isCounterEnabledSlow(key);
|
|
1839 |
}
|
|
1840 |
|
|
1841 |
private boolean isCounterEnabledSlow(AbstractKey key) {
|
|
1842 |
if (currentScope != null && currentScope.isCountEnabled()) {
|
|
1843 |
return true;
|
|
1844 |
}
|
|
1845 |
if (immutable.listMetrics) {
|
|
1846 |
key.ensureInitialized();
|
|
1847 |
}
|
|
1848 |
assert checkNoConcurrentAccess();
|
|
1849 |
EconomicSet<String> unscoped = immutable.unscopedCounters;
|
|
1850 |
return unscoped != null && (unscoped.isEmpty() || unscoped.contains(key.getName()));
|
|
1851 |
}
|
|
1852 |
|
|
1853 |
boolean isMemUseTrackerEnabled(MemUseTrackerKeyImpl key) {
|
|
1854 |
if (!metricsEnabled) {
|
|
1855 |
// Pulling this common case out of `isMemUseTrackerEnabledSlow`
|
|
1856 |
// gives C1 a better chance to inline this method.
|
|
1857 |
return false;
|
|
1858 |
}
|
|
1859 |
return isMemUseTrackerEnabledSlow(key);
|
|
1860 |
}
|
|
1861 |
|
|
1862 |
private boolean isMemUseTrackerEnabledSlow(AbstractKey key) {
|
|
1863 |
if (currentScope != null && currentScope.isMemUseTrackingEnabled()) {
|
|
1864 |
return true;
|
|
1865 |
}
|
|
1866 |
if (immutable.listMetrics) {
|
|
1867 |
key.ensureInitialized();
|
|
1868 |
}
|
|
1869 |
assert checkNoConcurrentAccess();
|
|
1870 |
EconomicSet<String> unscoped = immutable.unscopedMemUseTrackers;
|
|
1871 |
return unscoped != null && (unscoped.isEmpty() || unscoped.contains(key.getName()));
|
|
1872 |
}
|
|
1873 |
|
|
1874 |
public boolean areMetricsEnabled() {
|
|
1875 |
return metricsEnabled;
|
|
1876 |
}
|
|
1877 |
|
|
1878 |
@Override
|
|
1879 |
public void close() {
|
|
1880 |
closeDumpHandlers(false);
|
|
1881 |
if (description != null) {
|
|
1882 |
printMetrics(description);
|
|
1883 |
}
|
|
1884 |
if (metricsEnabled && globalMetrics != null && metricValues != null) {
|
|
1885 |
globalMetrics.add(this);
|
|
1886 |
}
|
|
1887 |
metricValues = null;
|
|
1888 |
}
|
|
1889 |
|
|
1890 |
public void closeDumpHandlers(boolean ignoreErrors) {
|
|
1891 |
if (currentConfig != null) {
|
|
1892 |
currentConfig.closeDumpHandlers(ignoreErrors);
|
|
1893 |
}
|
|
1894 |
}
|
|
1895 |
|
|
1896 |
/**
|
|
1897 |
* Records how many times a given method has been compiled.
|
|
1898 |
*/
|
|
1899 |
private static EconomicMap<Integer, Integer> compilations;
|
|
1900 |
|
|
1901 |
/**
|
|
1902 |
* Maintains maximum buffer size used by {@link #printMetrics(Description)} to minimize buffer
|
|
1903 |
* resizing during subsequent calls to this method.
|
|
1904 |
*/
|
|
1905 |
private static int metricsBufSize = 50_000;
|
|
1906 |
|
|
1907 |
/**
|
|
1908 |
* Flag that allows the first call to {@link #printMetrics(Description)} to delete the file that
|
|
1909 |
* will be appended to.
|
|
1910 |
*/
|
|
1911 |
private static boolean metricsFileDeleteCheckPerformed;
|
|
1912 |
|
|
1913 |
/**
|
|
1914 |
* Prints metric values in this object to the file (if any) specified by
|
|
1915 |
* {@link DebugOptions#MetricsFile}.
|
|
1916 |
*/
|
|
1917 |
public void printMetrics(Description desc) {
|
|
1918 |
if (metricValues == null) {
|
|
1919 |
return;
|
|
1920 |
}
|
|
1921 |
String metricsFile = DebugOptions.MetricsFile.getValue(getOptions());
|
|
1922 |
if (metricsFile != null) {
|
|
1923 |
// Use identity to distinguish methods that have been redefined
|
|
1924 |
// or loaded by different class loaders.
|
|
1925 |
Object compilable = desc.compilable;
|
|
1926 |
Integer identity = System.identityHashCode(compilable);
|
|
1927 |
int compilationNr;
|
|
1928 |
synchronized (PRINT_METRICS_LOCK) {
|
|
1929 |
if (!metricsFileDeleteCheckPerformed) {
|
|
1930 |
metricsFileDeleteCheckPerformed = true;
|
|
1931 |
File file = new File(metricsFile);
|
|
1932 |
if (file.exists()) {
|
|
1933 |
// This can return false in case something like /dev/stdout
|
|
1934 |
// is specified. If the file is unwriteable, the file open
|
|
1935 |
// below will fail.
|
|
1936 |
file.delete();
|
|
1937 |
}
|
|
1938 |
}
|
|
1939 |
if (compilations == null) {
|
|
1940 |
compilationNr = 0;
|
|
1941 |
compilations = EconomicMap.create();
|
|
1942 |
} else {
|
|
1943 |
Integer value = compilations.get(identity);
|
|
1944 |
compilationNr = value == null ? 0 : value + 1;
|
|
1945 |
}
|
|
1946 |
compilations.put(identity, compilationNr);
|
|
1947 |
}
|
|
1948 |
|
|
1949 |
// Release the lock while generating the content to reduce contention.
|
|
1950 |
// This means `compilationNr` fields may show up out of order in the file.
|
|
1951 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(metricsBufSize);
|
|
1952 |
PrintStream out = new PrintStream(baos);
|
|
1953 |
if (metricsFile.endsWith(".csv") || metricsFile.endsWith(".CSV")) {
|
|
1954 |
printMetricsCSV(out, compilable, identity, compilationNr, desc.identifier);
|
|
1955 |
} else {
|
|
1956 |
printMetrics(out, compilable, identity, compilationNr, desc.identifier);
|
|
1957 |
}
|
|
1958 |
|
|
1959 |
byte[] content = baos.toByteArray();
|
|
1960 |
Path path = Paths.get(metricsFile);
|
|
1961 |
synchronized (PRINT_METRICS_LOCK) {
|
|
1962 |
metricsBufSize = Math.max(metricsBufSize, content.length);
|
|
1963 |
try {
|
|
1964 |
Files.write(path, content, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
|
1965 |
} catch (IOException e) {
|
|
1966 |
}
|
|
1967 |
}
|
|
1968 |
}
|
|
1969 |
|
|
1970 |
}
|
|
1971 |
|
|
1972 |
/**
|
|
1973 |
* Lock to serialize writes to {@link DebugOptions#MetricsFile}.
|
|
1974 |
*/
|
|
1975 |
private static final Object PRINT_METRICS_LOCK = new Object();
|
|
1976 |
|
|
1977 |
/**
|
|
1978 |
* Appends metrics in CSV format to {@code out} for a single method compilation.
|
|
1979 |
*
|
|
1980 |
* @param identity the identity hash code of {@code compilable}
|
|
1981 |
* @param compilationNr where this compilation lies in the ordered sequence of all compilations
|
|
1982 |
* identified by {@code identity}
|
|
1983 |
* @param compilationId the runtime issued identifier for the compilation
|
|
1984 |
*/
|
|
1985 |
private void printMetricsCSV(PrintStream out, Object compilable, Integer identity, int compilationNr, String compilationId) {
|
|
1986 |
String compilableName = compilable instanceof JavaMethod ? ((JavaMethod) compilable).format("%H.%n(%p)%R") : String.valueOf(compilable);
|
|
1987 |
String csvFormat = CSVUtil.buildFormatString("%s", "%s", "%d", "%s");
|
|
1988 |
String format = String.format(csvFormat, CSVUtil.Escape.escapeArgs(compilableName, identity, compilationNr, compilationId));
|
|
1989 |
char sep = CSVUtil.SEPARATOR;
|
|
1990 |
format += sep + "%s" + sep + "%s" + sep + "%s";
|
|
1991 |
for (MetricKey key : KeyRegistry.getKeys()) {
|
|
1992 |
int index = ((AbstractKey) key).getIndex();
|
|
1993 |
if (index < metricValues.length) {
|
|
1994 |
Pair<String, String> valueAndUnit = key.toCSVFormat(metricValues[index]);
|
|
1995 |
CSVUtil.Escape.println(out, format, CSVUtil.Escape.escape(key.getName()), valueAndUnit.getLeft(), valueAndUnit.getRight());
|
|
1996 |
}
|
|
1997 |
}
|
|
1998 |
}
|
|
1999 |
|
|
2000 |
/**
|
|
2001 |
* Appends metrics in a human readable format to {@code out} for a single method compilation.
|
|
2002 |
*
|
|
2003 |
* @param identity the identity hash code of {@code compilable}
|
|
2004 |
* @param compilationNr where this compilation lies in the ordered sequence of all compilations
|
|
2005 |
* identified by {@code identity}
|
|
2006 |
* @param compilationId the runtime issued identifier for the compilation
|
|
2007 |
*/
|
|
2008 |
private void printMetrics(PrintStream out, Object compilable, Integer identity, int compilationNr, String compilationId) {
|
|
2009 |
String compilableName = compilable instanceof JavaMethod ? ((JavaMethod) compilable).format("%H.%n(%p)%R") : String.valueOf(compilable);
|
|
2010 |
int maxKeyWidth = compilableName.length();
|
|
2011 |
SortedMap<String, String> res = new TreeMap<>();
|
|
2012 |
for (MetricKey key : KeyRegistry.getKeys()) {
|
|
2013 |
int index = ((AbstractKey) key).getIndex();
|
|
2014 |
if (index < metricValues.length && metricValues[index] != 0) {
|
|
2015 |
String name = key.getName();
|
|
2016 |
long value = metricValues[index];
|
|
2017 |
String valueString;
|
|
2018 |
if (key instanceof TimerKey) {
|
|
2019 |
// Report timers in ms
|
|
2020 |
TimerKey timer = (TimerKey) key;
|
|
2021 |
long ms = timer.getTimeUnit().toMillis(value);
|
|
2022 |
if (ms == 0) {
|
|
2023 |
continue;
|
|
2024 |
}
|
|
2025 |
valueString = ms + "ms";
|
|
2026 |
} else {
|
|
2027 |
valueString = String.valueOf(value);
|
|
2028 |
}
|
|
2029 |
res.put(name, valueString);
|
|
2030 |
maxKeyWidth = Math.max(maxKeyWidth, name.length());
|
|
2031 |
}
|
|
2032 |
}
|
|
2033 |
|
|
2034 |
String title = String.format("%s [id:%s compilation:%d compilation_id:%s]", compilableName, identity, compilationNr, compilationId);
|
|
2035 |
out.println(new String(new char[title.length()]).replace('\0', '#'));
|
|
2036 |
out.printf("%s%n", title);
|
|
2037 |
out.println(new String(new char[title.length()]).replace('\0', '~'));
|
|
2038 |
|
|
2039 |
for (Map.Entry<String, String> e : res.entrySet()) {
|
|
2040 |
out.printf("%-" + String.valueOf(maxKeyWidth) + "s = %20s%n", e.getKey(), e.getValue());
|
|
2041 |
}
|
|
2042 |
out.println();
|
|
2043 |
}
|
|
2044 |
}
|