001/*
002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.debug.internal;
024
025import static com.oracle.graal.debug.DebugCloseable.*;
026import com.oracle.graal.debug.*;
027
028public final class MemUseTrackerImpl extends AccumulatedDebugValue implements DebugMemUseTracker {
029
030    public static long getCurrentThreadAllocatedBytes() {
031        return Management.getCurrentThreadAllocatedBytes();
032    }
033
034    /**
035     * Records the most recent active tracker.
036     */
037    private static final ThreadLocal<CloseableCounterImpl> currentTracker = new ThreadLocal<>();
038
039    public MemUseTrackerImpl(String name, boolean conditional) {
040        super(name, conditional, new DebugValue(name + "_Flat", conditional) {
041
042            @Override
043            public String toString(long value) {
044                return valueToString(value);
045            }
046        });
047    }
048
049    @Override
050    public DebugCloseable start() {
051        if (!isConditional() || Debug.isMemUseTrackingEnabled()) {
052            MemUseCloseableCounterImpl result = new MemUseCloseableCounterImpl(this);
053            currentTracker.set(result);
054            return result;
055        } else {
056            return VOID_CLOSEABLE;
057        }
058    }
059
060    public static String valueToString(long value) {
061        return String.format("%d bytes", value);
062    }
063
064    @Override
065    public String toString(long value) {
066        return valueToString(value);
067    }
068
069    private static final class MemUseCloseableCounterImpl extends CloseableCounterImpl implements DebugCloseable {
070
071        private MemUseCloseableCounterImpl(AccumulatedDebugValue counter) {
072            super(currentTracker.get(), counter);
073        }
074
075        @Override
076        long getCounterValue() {
077            return getCurrentThreadAllocatedBytes();
078        }
079
080        @Override
081        public void close() {
082            super.close();
083            currentTracker.set(parent);
084        }
085    }
086}