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;
  24 
  25 import static org.graalvm.compiler.debug.DebugCloseable.VOID_CLOSEABLE;
  26 
  27 import org.graalvm.util.Pair;
  28 
  29 class MemUseTrackerKeyImpl extends AccumulatedKey implements MemUseTrackerKey {
  30 
  31     MemUseTrackerKeyImpl(String format, Object arg1, Object arg2) {
  32         super(new FlatMemUseTracker(format, arg1, arg2), format, arg1, arg2);
  33     }
  34 
  35     @Override
  36     public DebugCloseable start(DebugContext debug) {
  37         if (debug.isMemUseTrackerEnabled(this)) {
  38             CloseableCounter result = new MemUseCloseableCounterImpl(this, debug);
  39             debug.currentMemUseTracker = result;
  40             return result;
  41         }
  42         return VOID_CLOSEABLE;
  43     }
  44 
  45     public static String valueToString(long value) {
  46         return String.format("%d bytes", value);
  47     }
  48 
  49     @Override
  50     public String toHumanReadableFormat(long value) {
  51         return valueToString(value);
  52     }
  53 
  54     static final class FlatMemUseTracker extends AbstractKey implements MetricKey {
  55 
  56         FlatMemUseTracker(String nameFormat, Object nameArg1, Object nameArg2) {
  57             super(nameFormat, nameArg1, nameArg2);
  58         }
  59 
  60         @Override
  61         protected String createName(String format, Object arg1, Object arg2) {
  62             return super.createName(format, arg1, arg2) + FLAT_KEY_SUFFIX;
  63         }
  64 
  65         @Override
  66         public MetricKey doc(String doc) {
  67             throw new IllegalArgumentException("Cannot set documentation for derived key " + getName());
  68         }
  69 
  70         @Override
  71         public String getDocName() {
  72             return null;
  73         }
  74 
  75         @Override
  76         public String toHumanReadableFormat(long value) {
  77             return valueToString(value);
  78         }
  79 
  80         @Override
  81         public Pair<String, String> toCSVFormat(long value) {
  82             return Pair.create(String.valueOf(value), "bytes");
  83         }
  84     }
  85 
  86     static final class MemUseCloseableCounterImpl extends CloseableCounter implements DebugCloseable {
  87 
  88         private final DebugContext debug;
  89 
  90         MemUseCloseableCounterImpl(AccumulatedKey counter, DebugContext debug) {
  91             super(debug, debug.currentMemUseTracker, counter);
  92             this.debug = debug;
  93         }
  94 
  95         @Override
  96         long getCounterValue() {
  97             return MemUseTrackerKey.getCurrentThreadAllocatedBytes();
  98         }
  99 
 100         @Override
 101         public void close() {
 102             super.close();
 103             debug.currentMemUseTracker = parent;
 104         }
 105     }
 106 
 107     @Override
 108     public Pair<String, String> toCSVFormat(long value) {
 109         return Pair.create(String.valueOf(value), "bytes");
 110     }
 111 
 112     @Override
 113     public MemUseTrackerKey doc(String doc) {
 114         setDoc(doc);
 115         return this;
 116     }
 117 }