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