1 /*
   2  * Copyright (c) 2015, 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.core;
  24 
  25 import org.graalvm.compiler.debug.Debug;
  26 import org.graalvm.compiler.debug.Debug.Params;
  27 import org.graalvm.compiler.debug.DebugInitializationParticipant;
  28 import org.graalvm.compiler.debug.GraalDebugConfig;
  29 import org.graalvm.compiler.debug.TTY;
  30 import org.graalvm.compiler.debug.internal.method.MethodMetricsPrinter;
  31 import org.graalvm.compiler.options.OptionValues;
  32 import org.graalvm.compiler.serviceprovider.ServiceProvider;
  33 
  34 /**
  35  * A service provider that may modify the initialization of {@link Debug} based on the values
  36  * specified for various {@link GraalDebugConfig} options.
  37  */
  38 @ServiceProvider(DebugInitializationParticipant.class)
  39 public class GraalDebugInitializationParticipant implements DebugInitializationParticipant {
  40 
  41     @Override
  42     public void apply(Params params) {
  43         OptionValues options = params.getOptions();
  44         if (GraalDebugConfig.areDebugScopePatternsEnabled(options)) {
  45             params.enable = true;
  46         }
  47         if ("".equals(GraalDebugConfig.Options.Count.getValue(options))) {
  48             params.enableUnscopedCounters = true;
  49         }
  50         if ("".equals(GraalDebugConfig.Options.MethodMeter.getValue(options))) {
  51             params.enableUnscopedMethodMetrics = true;
  52             // mm requires full debugging support
  53             params.enable = true;
  54         }
  55         if ("".equals(GraalDebugConfig.Options.Time.getValue(options))) {
  56             params.enableUnscopedTimers = true;
  57         }
  58         if ("".equals(GraalDebugConfig.Options.TrackMemUse.getValue(options))) {
  59             params.enableUnscopedMemUseTrackers = true;
  60         }
  61         // unscoped counters/timers/mem use trackers/method metrics should respect method filter
  62         // semantics
  63         if (!params.enable && (params.enableUnscopedMemUseTrackers || params.enableUnscopedMethodMetrics || params.enableUnscopedCounters || params.enableUnscopedTimers) &&
  64                         GraalDebugConfig.isNotEmpty(GraalDebugConfig.Options.MethodFilter, options)) {
  65             params.enable = true;
  66             params.enableMethodFilter = true;
  67         }
  68 
  69         if (!params.enableUnscopedMethodMetrics && GraalDebugConfig.Options.MethodMeter.getValue(options) != null) {
  70             // mm requires full debugging support
  71             params.enable = true;
  72         }
  73 
  74         if (GraalDebugConfig.isGlobalMetricsInterceptedByMethodMetricsEnabled(options)) {
  75             if (!params.enable) {
  76                 TTY.println("WARNING: MethodMeter is disabled but GlobalMetricsInterceptedByMethodMetrics is enabled. Ignoring MethodMeter and GlobalMetricsInterceptedByMethodMetrics.");
  77             } else {
  78                 parseMethodMetricsDebugValueInterception(params, options);
  79             }
  80         }
  81         if (GraalDebugConfig.isNotEmpty(GraalDebugConfig.Options.MethodMeter, options) || params.enableUnscopedMethodMetrics) {
  82             if (!MethodMetricsPrinter.methodMetricsDumpingEnabled(options)) {
  83                 TTY.println("WARNING: MethodMeter is enabled but MethodMeter dumping is disabled. Output will not contain MethodMetrics.");
  84             }
  85         }
  86     }
  87 
  88     private static void parseMethodMetricsDebugValueInterception(Params params, OptionValues options) {
  89         String interceptionGroup = GraalDebugConfig.Options.GlobalMetricsInterceptedByMethodMetrics.getValue(options);
  90         boolean intercepted = false;
  91         if (interceptionGroup.contains("Timers")) {
  92             params.interceptTime = true;
  93             intercepted = true;
  94         }
  95         if (interceptionGroup.contains("Counters")) {
  96             params.interceptCount = true;
  97             intercepted = true;
  98         }
  99         if (interceptionGroup.contains("MemUseTrackers")) {
 100             params.interceptMem = true;
 101             intercepted = true;
 102         }
 103 
 104         if (!intercepted) {
 105             TTY.println("WARNING: Ignoring GlobalMetricsInterceptedByMethodMetrics as the supplied argument does not contain Timers/Counters/MemUseTrackers.");
 106         }
 107     }
 108 }