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