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 static org.graalvm.compiler.debug.DebugCloseable.VOID_CLOSEABLE;
  26 
  27 import org.graalvm.compiler.debug.Debug;
  28 import org.graalvm.compiler.debug.DebugCloseable;
  29 import org.graalvm.compiler.debug.DebugMemUseTracker;
  30 import org.graalvm.compiler.debug.Management;
  31 import org.graalvm.compiler.debug.internal.method.MethodMetricsImpl;
  32 
  33 public class MemUseTrackerImpl extends AccumulatedDebugValue implements DebugMemUseTracker {
  34     private final boolean intercepting;
  35 
  36     public static long getCurrentThreadAllocatedBytes() {
  37         return Management.getCurrentThreadAllocatedBytes();
  38     }
  39 
  40     /**
  41      * Records the most recent active tracker.
  42      */
  43     private static final ThreadLocal<CloseableCounterImpl> currentTracker = new ThreadLocal<>();
  44 
  45     public MemUseTrackerImpl(String name, boolean conditional, boolean intercepting) {
  46         super(name, conditional, new DebugValue(name + "_Flat", conditional) {
  47 
  48             @Override
  49             public String toString(long value) {
  50                 return valueToString(value);
  51             }
  52 
  53             @Override
  54             public String rawUnit() {
  55                 return "B";
  56             }
  57 
  58             @Override
  59             public String toRawString(long value) {
  60                 return Long.toString(value);
  61             }
  62         });
  63         this.intercepting = intercepting;
  64     }
  65 
  66     @Override
  67     public DebugCloseable start() {
  68         if (!isConditional() || Debug.isMemUseTrackingEnabled()) {
  69             CloseableCounterImpl result = intercepting ? new MemUseInterceptingCloseableCounterImpl(this) : new MemUseCloseableCounterImpl(this);
  70             currentTracker.set(result);
  71             return result;
  72         } else {
  73             return VOID_CLOSEABLE;
  74         }
  75     }
  76 
  77     public static String valueToString(long value) {
  78         return String.format("%d bytes", value);
  79     }
  80 
  81     @Override
  82     public String toString(long value) {
  83         return valueToString(value);
  84     }
  85 
  86     private static final class MemUseCloseableCounterImpl extends CloseableCounterImpl implements DebugCloseable {
  87 
  88         private MemUseCloseableCounterImpl(AccumulatedDebugValue counter) {
  89             super(currentTracker.get(), counter);
  90         }
  91 
  92         @Override
  93         long getCounterValue() {
  94             return getCurrentThreadAllocatedBytes();
  95         }
  96 
  97         @Override
  98         public void close() {
  99             super.close();
 100             currentTracker.set(parent);
 101         }
 102     }
 103 
 104     private static final class MemUseInterceptingCloseableCounterImpl extends CloseableCounterImpl implements DebugCloseable {
 105 
 106         private MemUseInterceptingCloseableCounterImpl(AccumulatedDebugValue counter) {
 107             super(currentTracker.get(), counter);
 108         }
 109 
 110         @Override
 111         long getCounterValue() {
 112             return getCurrentThreadAllocatedBytes();
 113         }
 114 
 115         @Override
 116         public void close() {
 117             super.close();
 118             currentTracker.set(parent);
 119         }
 120 
 121         @Override
 122         protected void interceptDifferenceAccm(long difference) {
 123             if (Debug.isMethodMeterEnabled()) {
 124                 MethodMetricsImpl.addToCurrentScopeMethodMetrics(counter.getName(), difference);
 125             }
 126         }
 127 
 128         @Override
 129         protected void interceptDifferenceFlat(long difference) {
 130             if (Debug.isMethodMeterEnabled()) {
 131                 MethodMetricsImpl.addToCurrentScopeMethodMetrics(counter.flat.getName(), difference);
 132             }
 133         }
 134     }
 135 
 136     @Override
 137     public String rawUnit() {
 138         return "B";
 139     }
 140 
 141     @Override
 142     public String toRawString(long value) {
 143         return Long.toString(value);
 144     }
 145 }