63 import jdk.internal.vm.compiler.collections.Pair;
64 import org.graalvm.compiler.options.OptionKey;
65 import org.graalvm.compiler.options.OptionValues;
66 import org.graalvm.compiler.serviceprovider.GraalServices;
67 import org.graalvm.graphio.GraphOutput;
68
69 import jdk.vm.ci.meta.JavaMethod;
70
71 /**
72 * A facility for logging and dumping as well as a container for values associated with
73 * {@link MetricKey}s.
74 *
75 * A {@code DebugContext} object must only be used on the thread that created it. This means it
76 * needs to be passed around as a parameter. For convenience, it can be encapsulated in a widely
77 * used object that is in scope wherever a {@code DebugContext} is needed. However, care must be
78 * taken when such objects can be exposed to multiple threads (e.g., they are in a non-thread-local
79 * cache).
80 */
81 public final class DebugContext implements AutoCloseable {
82
83 public static final Description NO_DESCRIPTION = null;
84 public static final GlobalMetrics NO_GLOBAL_METRIC_VALUES = null;
85 public static final Iterable<DebugHandlersFactory> NO_CONFIG_CUSTOMIZERS = Collections.emptyList();
86
87 public static final PrintStream DEFAULT_LOG_STREAM = TTY.out;
88
89 /**
90 * Contains the immutable parts of a debug context. This separation allows the immutable parts
91 * to be shared and reduces the overhead of initialization since most immutable fields are
92 * configured by parsing options.
93 */
94 final Immutable immutable;
95
96 /**
97 * Determines whether metrics are enabled.
98 */
99 boolean metricsEnabled;
100
101 DebugConfigImpl currentConfig;
102 ScopeImpl currentScope;
103 CloseableCounter currentTimer;
385 *
386 * @return {@code null} if no global metrics are available
387 */
388 public GlobalMetrics getGlobalMetrics() {
389 return globalMetrics;
390 }
391
392 /**
393 * Creates a {@link DebugContext} based on a given set of option values and {@code factory}.
394 */
395 public static DebugContext create(OptionValues options, DebugHandlersFactory factory) {
396 return new DebugContext(NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, Immutable.create(options), Collections.singletonList(factory));
397 }
398
399 /**
400 * Creates a {@link DebugContext} based on a given set of option values and {@code factories}.
401 * The {@link DebugHandlersFactory#LOADER} can be used for the latter.
402 */
403 public static DebugContext create(OptionValues options, Iterable<DebugHandlersFactory> factories) {
404 return new DebugContext(NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, Immutable.create(options), factories);
405 }
406
407 /**
408 * Creates a {@link DebugContext}.
409 */
410 public static DebugContext create(OptionValues options, Description description, GlobalMetrics globalMetrics, PrintStream logStream, Iterable<DebugHandlersFactory> factories) {
411 return new DebugContext(description, globalMetrics, logStream, Immutable.create(options), factories);
412 }
413
414 private DebugContext(Description description, GlobalMetrics globalMetrics, PrintStream logStream, Immutable immutable, Iterable<DebugHandlersFactory> factories) {
415 this.immutable = immutable;
416 this.description = description;
417 this.globalMetrics = globalMetrics;
418 if (immutable.scopesEnabled) {
419 OptionValues options = immutable.options;
420 List<DebugDumpHandler> dumpHandlers = new ArrayList<>();
421 List<DebugVerifyHandler> verifyHandlers = new ArrayList<>();
422 for (DebugHandlersFactory factory : factories) {
423 for (DebugHandler handler : factory.createHandlers(options)) {
424 if (handler instanceof DebugDumpHandler) {
|
63 import jdk.internal.vm.compiler.collections.Pair;
64 import org.graalvm.compiler.options.OptionKey;
65 import org.graalvm.compiler.options.OptionValues;
66 import org.graalvm.compiler.serviceprovider.GraalServices;
67 import org.graalvm.graphio.GraphOutput;
68
69 import jdk.vm.ci.meta.JavaMethod;
70
71 /**
72 * A facility for logging and dumping as well as a container for values associated with
73 * {@link MetricKey}s.
74 *
75 * A {@code DebugContext} object must only be used on the thread that created it. This means it
76 * needs to be passed around as a parameter. For convenience, it can be encapsulated in a widely
77 * used object that is in scope wherever a {@code DebugContext} is needed. However, care must be
78 * taken when such objects can be exposed to multiple threads (e.g., they are in a non-thread-local
79 * cache).
80 */
81 public final class DebugContext implements AutoCloseable {
82
83 public static final Description NO_DESCRIPTION = new Description(null, "NO_DESCRIPTION");
84 public static final GlobalMetrics NO_GLOBAL_METRIC_VALUES = null;
85 public static final Iterable<DebugHandlersFactory> NO_CONFIG_CUSTOMIZERS = Collections.emptyList();
86
87 public static final PrintStream DEFAULT_LOG_STREAM = TTY.out;
88
89 /**
90 * Contains the immutable parts of a debug context. This separation allows the immutable parts
91 * to be shared and reduces the overhead of initialization since most immutable fields are
92 * configured by parsing options.
93 */
94 final Immutable immutable;
95
96 /**
97 * Determines whether metrics are enabled.
98 */
99 boolean metricsEnabled;
100
101 DebugConfigImpl currentConfig;
102 ScopeImpl currentScope;
103 CloseableCounter currentTimer;
385 *
386 * @return {@code null} if no global metrics are available
387 */
388 public GlobalMetrics getGlobalMetrics() {
389 return globalMetrics;
390 }
391
392 /**
393 * Creates a {@link DebugContext} based on a given set of option values and {@code factory}.
394 */
395 public static DebugContext create(OptionValues options, DebugHandlersFactory factory) {
396 return new DebugContext(NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, Immutable.create(options), Collections.singletonList(factory));
397 }
398
399 /**
400 * Creates a {@link DebugContext} based on a given set of option values and {@code factories}.
401 * The {@link DebugHandlersFactory#LOADER} can be used for the latter.
402 */
403 public static DebugContext create(OptionValues options, Iterable<DebugHandlersFactory> factories) {
404 return new DebugContext(NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, Immutable.create(options), factories);
405 }
406
407 public static DebugContext create(OptionValues options, PrintStream logStream, DebugHandlersFactory factory) {
408 return new DebugContext(NO_DESCRIPTION, NO_GLOBAL_METRIC_VALUES, logStream, Immutable.create(options), Collections.singletonList(factory));
409 }
410
411 /**
412 * Creates a {@link DebugContext} based on a given set of option values and {@code factories}.
413 * The {@link DebugHandlersFactory#LOADER} can be used for the latter.
414 */
415 public static DebugContext create(OptionValues options, Description description, Iterable<DebugHandlersFactory> factories) {
416 return new DebugContext(description, NO_GLOBAL_METRIC_VALUES, DEFAULT_LOG_STREAM, Immutable.create(options), factories);
417 }
418
419 /**
420 * Creates a {@link DebugContext}.
421 */
422 public static DebugContext create(OptionValues options, Description description, GlobalMetrics globalMetrics, PrintStream logStream, Iterable<DebugHandlersFactory> factories) {
423 return new DebugContext(description, globalMetrics, logStream, Immutable.create(options), factories);
424 }
425
426 private DebugContext(Description description, GlobalMetrics globalMetrics, PrintStream logStream, Immutable immutable, Iterable<DebugHandlersFactory> factories) {
427 this.immutable = immutable;
428 this.description = description;
429 this.globalMetrics = globalMetrics;
430 if (immutable.scopesEnabled) {
431 OptionValues options = immutable.options;
432 List<DebugDumpHandler> dumpHandlers = new ArrayList<>();
433 List<DebugVerifyHandler> verifyHandlers = new ArrayList<>();
434 for (DebugHandlersFactory factory : factories) {
435 for (DebugHandler handler : factory.createHandlers(options)) {
436 if (handler instanceof DebugDumpHandler) {
|