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.debug;
  24 
  25 /**
  26  * A helper class for {@link AbstractKey}s that can nest and need to split out accumulated and flat
  27  * values for some kind of counter-like measurement.
  28  */
  29 public abstract class CloseableCounter implements DebugCloseable {
  30 
  31     protected final DebugContext debug;
  32     protected final CloseableCounter parent;
  33     protected final AccumulatedKey counter;
  34     protected final long start;
  35     protected long nestedAmountToSubtract;
  36 
  37     CloseableCounter(DebugContext debug, CloseableCounter parent, AccumulatedKey counter) {
  38         this.debug = debug;
  39         this.parent = parent;
  40         this.start = getCounterValue();
  41         this.counter = counter;
  42     }
  43 
  44     @Override
  45     public DebugContext getDebug() {
  46         return debug;
  47     }
  48 
  49     /**
  50      * A hook for subclasses. Lets them perform custom operations with the value since the last
  51      * invocation of {@link CloseableCounter#close()} of this accumulated
  52      * {@link CloseableCounter#counter}.
  53      *
  54      * @param difference since the last invocation of this counter flat
  55      */
  56     protected void interceptDifferenceAccm(long difference) {
  57         // hook for subclasses
  58     }
  59 
  60     /**
  61      * A hook for subclasses. Lets them perform custom operations with the value since the last
  62      * invocation of {@link CloseableCounter#close()} of this flat {@link CloseableCounter#counter}.
  63      *
  64      * @param difference since the last invocation of this counter flat
  65      */
  66     protected void interceptDifferenceFlat(long difference) {
  67         // hook for subclasses
  68     }
  69 
  70     @Override
  71     public void close() {
  72         long end = getCounterValue();
  73         long difference = end - start;
  74         if (parent != null) {
  75             if (!counter.getName().equals(parent.counter.getName())) {
  76                 parent.nestedAmountToSubtract += difference;
  77                 // Look for our counter in an outer scope and fix up
  78                 // the adjustment to the flat count
  79                 CloseableCounter ancestor = parent.parent;
  80                 while (ancestor != null) {
  81                     if (ancestor.counter.getName().equals(counter.getName())) {
  82                         ancestor.nestedAmountToSubtract -= difference;
  83                         break;
  84                     }
  85                     ancestor = ancestor.parent;
  86                 }
  87             }
  88         }
  89         long flatAmount = difference - nestedAmountToSubtract;
  90         counter.addToCurrentValue(debug, difference);
  91         counter.flat.addToCurrentValue(debug, flatAmount);
  92         interceptDifferenceAccm(difference);
  93         interceptDifferenceFlat(flatAmount);
  94     }
  95 
  96     abstract long getCounterValue();
  97 }