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