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.internal;
  24 
  25 import org.graalvm.compiler.debug.Debug;
  26 import org.graalvm.compiler.debug.DebugCounter;
  27 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl;
  28 
  29 public abstract class CounterImpl extends DebugValue implements DebugCounter {
  30 
  31     public CounterImpl(String name, boolean conditional) {
  32         super(name, conditional);
  33         if (isEnabled()) {
  34             // Allows for zero counters to be shown
  35             getCurrentValue();
  36         }
  37     }
  38 
  39     @Override
  40     public void increment() {
  41         add(1);
  42     }
  43 
  44     @Override
  45     public String rawUnit() {
  46         return "";
  47     }
  48 
  49     @Override
  50     public String toRawString(long value) {
  51         return Long.toString(value);
  52     }
  53 
  54     @Override
  55     public String toString(long value) {
  56         return Long.toString(value);
  57     }
  58 
  59     private static final class InterceptingCounterImpl extends CounterImpl {
  60 
  61         private InterceptingCounterImpl(String name, boolean conditional) {
  62             super(name, conditional);
  63         }
  64 
  65         @Override
  66         public void add(long value) {
  67             if (isEnabled()) {
  68                 if (Debug.isMethodMeterEnabled()) {
  69                     MethodMetricsImpl.addToCurrentScopeMethodMetrics(getName(), value);
  70                 }
  71                 super.addToCurrentValue(value);
  72             }
  73         }
  74     }
  75 
  76     private static final class PlainCounterImpl extends CounterImpl {
  77 
  78         private PlainCounterImpl(String name, boolean conditional) {
  79             super(name, conditional);
  80         }
  81 
  82         @Override
  83         public void add(long value) {
  84             if (isEnabled()) {
  85                 super.addToCurrentValue(value);
  86             }
  87         }
  88     }
  89 
  90     public static DebugCounter create(String name, boolean conditional, boolean intercepting) {
  91         if (intercepting) {
  92             return new InterceptingCounterImpl(name, conditional);
  93         }
  94         return new PlainCounterImpl(name, conditional);
  95     }
  96 }