001/*
002 * Copyright (c) 2015, 2015, 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;
024
025import static java.lang.Thread.*;
026
027import java.lang.management.*;
028
029import javax.management.*;
030
031public class Management {
032
033    private static final com.sun.management.ThreadMXBean threadMXBean = Management.initThreadMXBean();
034
035    /**
036     * The amount of memory allocated by
037     * {@link com.sun.management.ThreadMXBean#getThreadAllocatedBytes(long)} itself.
038     */
039    private static final long threadMXBeanOverhead = -getCurrentThreadAllocatedBytes() + getCurrentThreadAllocatedBytes();
040
041    public static long getCurrentThreadAllocatedBytes() {
042        return threadMXBean.getThreadAllocatedBytes(currentThread().getId()) - threadMXBeanOverhead;
043    }
044
045    private static com.sun.management.ThreadMXBean initThreadMXBean() {
046        try {
047            return (com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean();
048        } catch (Error err) {
049            return new UnimplementedBean();
050        }
051    }
052
053    public static ThreadMXBean getThreadMXBean() {
054        return threadMXBean;
055    }
056
057    private static class UnimplementedBean implements ThreadMXBean, com.sun.management.ThreadMXBean {
058
059        public ObjectName getObjectName() {
060            return null;
061        }
062
063        public long getThreadAllocatedBytes(long arg0) {
064            return 0;
065        }
066
067        public long[] getThreadAllocatedBytes(long[] arg0) {
068            return null;
069        }
070
071        public long[] getThreadCpuTime(long[] arg0) {
072            return null;
073        }
074
075        public long[] getThreadUserTime(long[] arg0) {
076            return null;
077        }
078
079        public boolean isThreadAllocatedMemoryEnabled() {
080            return false;
081        }
082
083        public boolean isThreadAllocatedMemorySupported() {
084            return false;
085        }
086
087        public void setThreadAllocatedMemoryEnabled(boolean arg0) {
088        }
089
090        public int getThreadCount() {
091            return 0;
092        }
093
094        public int getPeakThreadCount() {
095            return 0;
096        }
097
098        public long getTotalStartedThreadCount() {
099            return 0;
100        }
101
102        public int getDaemonThreadCount() {
103            return 0;
104        }
105
106        public long[] getAllThreadIds() {
107            return null;
108        }
109
110        public ThreadInfo getThreadInfo(long id) {
111            return null;
112        }
113
114        public ThreadInfo[] getThreadInfo(long[] ids) {
115            return null;
116        }
117
118        public ThreadInfo getThreadInfo(long id, int maxDepth) {
119            return null;
120        }
121
122        public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth) {
123            return null;
124        }
125
126        public boolean isThreadContentionMonitoringSupported() {
127            return false;
128        }
129
130        public boolean isThreadContentionMonitoringEnabled() {
131            return false;
132        }
133
134        public void setThreadContentionMonitoringEnabled(boolean enable) {
135        }
136
137        public long getCurrentThreadCpuTime() {
138            return 0;
139        }
140
141        public long getCurrentThreadUserTime() {
142            return 0;
143        }
144
145        public long getThreadCpuTime(long id) {
146            return 0;
147        }
148
149        public long getThreadUserTime(long id) {
150            return 0;
151        }
152
153        public boolean isThreadCpuTimeSupported() {
154            return false;
155        }
156
157        public boolean isCurrentThreadCpuTimeSupported() {
158            return false;
159        }
160
161        public boolean isThreadCpuTimeEnabled() {
162            return false;
163        }
164
165        public void setThreadCpuTimeEnabled(boolean enable) {
166        }
167
168        public long[] findMonitorDeadlockedThreads() {
169            return null;
170        }
171
172        public void resetPeakThreadCount() {
173        }
174
175        public long[] findDeadlockedThreads() {
176            return null;
177        }
178
179        public boolean isObjectMonitorUsageSupported() {
180            return false;
181        }
182
183        public boolean isSynchronizerUsageSupported() {
184            return false;
185        }
186
187        public ThreadInfo[] getThreadInfo(long[] ids, boolean lockedMonitors, boolean lockedSynchronizers) {
188            return null;
189        }
190
191        public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers) {
192            return null;
193        }
194    }
195}