Source for gnu.java.lang.management.ThreadMXBeanImpl

   1: /* ThreadMXBeanImpl.java - Implementation of a thread bean
   2:    Copyright (C) 2006 Free Software Foundation
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.java.lang.management;
  39: 
  40: import gnu.classpath.SystemProperties;
  41: 
  42: import java.lang.management.ThreadInfo;
  43: import java.lang.management.ThreadMXBean;
  44: 
  45: import javax.management.NotCompliantMBeanException;
  46: 
  47: /**
  48:  * Provides access to information about the threads 
  49:  * of the virtual machine.  An instance of this bean is
  50:  * obtained by calling
  51:  * {@link ManagementFactory#getThreadMXBean()}.
  52:  * See {@link java.lang.management.ThreadMXBean} for
  53:  * full documentation.
  54:  *
  55:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  56:  * @since 1.5
  57:  */
  58: public final class ThreadMXBeanImpl
  59:   extends BeanImpl
  60:   implements ThreadMXBean
  61: {
  62: 
  63:   /**
  64:    * Constant for current thread time support.
  65:    */
  66:   private static final String CURRENT_THREAD_TIME_SUPPORT = 
  67:     "gnu.java.lang.management.CurrentThreadTimeSupport";
  68: 
  69:   /**
  70:    * Constant for thread time support.
  71:    */
  72:   private static final String THREAD_TIME_SUPPORT = 
  73:     "gnu.java.lang.management.ThreadTimeSupport";
  74: 
  75:   /**
  76:    * Constant for thread contention support.
  77:    */
  78:   private static final String CONTENTION_SUPPORT = 
  79:     "gnu.java.lang.management.ThreadContentionSupport";
  80: 
  81:   /**
  82:    * Constant for initial value of thread time support.
  83:    */
  84:   private static final String TIME_ENABLED = 
  85:     "gnu.java.lang.management.ThreadTimeInitallyEnabled";
  86:   
  87:   /**
  88:    * Flag to indicate whether time monitoring is enabled or not.
  89:    */
  90:   private boolean timeEnabled;
  91: 
  92:   /**
  93:    * Flag to indicate whether contention monitoring is enabled or not.
  94:    */
  95:   private boolean contentionEnabled;
  96: 
  97:   /**
  98:    * Default constructor to set up flag states.  The
  99:    * VM has to specify whether time monitoring is initially
 100:    * enabled or not.
 101:    *
 102:    * @throws NotCompliantMBeanException if this class doesn't implement
 103:    *                                    the interface or a method appears
 104:    *                                    in the interface that doesn't comply
 105:    *                                    with the naming conventions.
 106:    */
 107:   public ThreadMXBeanImpl()
 108:     throws NotCompliantMBeanException
 109:   {
 110:     super(ThreadMXBean.class);
 111:     timeEnabled = Boolean.parseBoolean(SystemProperties.getProperty(TIME_ENABLED));
 112:     contentionEnabled = false;
 113:   }
 114: 
 115:   public long[] findMonitorDeadlockedThreads()
 116:   {
 117:     checkMonitorPermissions();
 118:     return VMThreadMXBeanImpl.findMonitorDeadlockedThreads();
 119:   }
 120: 
 121:   public long[] getAllThreadIds()
 122:   {
 123:     checkMonitorPermissions();
 124:     return VMThreadMXBeanImpl.getAllThreadIds();
 125:   }
 126: 
 127:   public long getCurrentThreadCpuTime()
 128:   {
 129:     if (!isCurrentThreadCpuTimeSupported())
 130:       throw new UnsupportedOperationException("Current thread CPU " +
 131:                           "time not supported.");
 132:     if (!timeEnabled)
 133:       return -1;
 134:     return VMThreadMXBeanImpl.getCurrentThreadCpuTime();
 135:   }
 136: 
 137:   public long getCurrentThreadUserTime()
 138:   {
 139:     if (!isCurrentThreadCpuTimeSupported())
 140:       throw new UnsupportedOperationException("Current thread user " +
 141:                           "time not supported.");
 142:     if (!timeEnabled)
 143:       return -1;
 144:     return VMThreadMXBeanImpl.getCurrentThreadUserTime();
 145:   }
 146: 
 147:   public int getDaemonThreadCount()
 148:   {
 149:     return VMThreadMXBeanImpl.getDaemonThreadCount();
 150:   }
 151: 
 152:   public int getPeakThreadCount()
 153:   {
 154:     return VMThreadMXBeanImpl.getPeakThreadCount();
 155:   }
 156: 
 157:   public int getThreadCount()
 158:   {
 159:     return VMThreadMXBeanImpl.getThreadCount();
 160:   }
 161: 
 162:   public long getThreadCpuTime(long id)
 163:   {
 164:     if (!isThreadCpuTimeSupported())
 165:       throw new UnsupportedOperationException("Thread CPU time not " +
 166:                           "supported.");
 167:     if (id <= 0)
 168:       throw new IllegalArgumentException("Invalid thread id: " + id);
 169:     if (!timeEnabled)
 170:       return -1;
 171:     return VMThreadMXBeanImpl.getThreadCpuTime(id);
 172:   }
 173: 
 174:   public ThreadInfo getThreadInfo(long id)
 175:   {
 176:     return getThreadInfo(id, 0);
 177:   }
 178: 
 179:   public ThreadInfo[] getThreadInfo(long[] ids)
 180:   {
 181:     return getThreadInfo(ids, 0);
 182:   }
 183: 
 184:   public ThreadInfo getThreadInfo(long id, int maxDepth)
 185:   {
 186:     checkMonitorPermissions();
 187:     if (id <= 0)
 188:       throw new IllegalArgumentException("Invalid thread id: " + id);
 189:     if (maxDepth < 0)
 190:       throw new IllegalArgumentException("Invalid depth: " + maxDepth);
 191:     return VMThreadMXBeanImpl.getThreadInfoForId(id, maxDepth);
 192:   }
 193: 
 194:   public ThreadInfo[] getThreadInfo(long[] ids, int maxDepth)
 195:   {
 196:     checkMonitorPermissions();
 197:     if (maxDepth < 0)
 198:       throw new IllegalArgumentException("Invalid depth: " + maxDepth);
 199:     ThreadInfo[] infos = new ThreadInfo[ids.length];
 200:     for (int a = 0; a < ids.length; ++a)
 201:       {
 202:     if (ids[a] <= 0)
 203:       throw new IllegalArgumentException("Invalid thread id " + a +
 204:                          ": " + ids[a]);
 205:     infos[a] = VMThreadMXBeanImpl.getThreadInfoForId(ids[a], maxDepth);
 206:       }
 207:     return infos;
 208:   }
 209: 
 210:   public long getThreadUserTime(long id)
 211:   {
 212:     if (!isThreadCpuTimeSupported())
 213:       throw new UnsupportedOperationException("Thread user time not " +
 214:                           "supported.");
 215:     if (id <= 0)
 216:       throw new IllegalArgumentException("Invalid thread id: " + id);
 217:     if (!timeEnabled)
 218:       return -1;
 219:     return VMThreadMXBeanImpl.getThreadUserTime(id);
 220:   }
 221: 
 222:   public long getTotalStartedThreadCount()
 223:   {
 224:     return VMThreadMXBeanImpl.getTotalStartedThreadCount();
 225:   }
 226: 
 227:   public boolean isCurrentThreadCpuTimeSupported()
 228:   {
 229:     if (isThreadCpuTimeSupported())
 230:       return true;
 231:     return SystemProperties.getProperty(CURRENT_THREAD_TIME_SUPPORT) != null;
 232:   }
 233: 
 234:   public boolean isThreadContentionMonitoringEnabled()
 235:   {
 236:     if (isThreadContentionMonitoringSupported())
 237:       return contentionEnabled;
 238:     else
 239:       throw new UnsupportedOperationException("Contention monitoring " +
 240:                           "not supported.");
 241:   }
 242: 
 243:   public boolean isThreadContentionMonitoringSupported()
 244:   {
 245:     return SystemProperties.getProperty(CONTENTION_SUPPORT) != null;
 246:   }
 247: 
 248:   public boolean isThreadCpuTimeEnabled()
 249:   {
 250:     if (isThreadCpuTimeSupported() ||
 251:     isCurrentThreadCpuTimeSupported())
 252:       return timeEnabled;
 253:     else
 254:       throw new UnsupportedOperationException("Thread time not " +
 255:                           "supported.");
 256:   }
 257:   
 258:   public boolean isThreadCpuTimeSupported()
 259:   {
 260:     return SystemProperties.getProperty(THREAD_TIME_SUPPORT) != null;
 261:   }
 262: 
 263:   public void resetPeakThreadCount()
 264:   {
 265:     checkControlPermissions();
 266:     VMThreadMXBeanImpl.resetPeakThreadCount();
 267:   }
 268:   
 269:   public void setThreadContentionMonitoringEnabled(boolean enable)
 270:   {
 271:     checkControlPermissions();
 272:     if (isThreadContentionMonitoringSupported())
 273:       contentionEnabled = enable;
 274:     else
 275:       throw new UnsupportedOperationException("Contention monitoring " +
 276:                           "not supported.");
 277:   }    
 278:   
 279:   public void setThreadCpuTimeEnabled(boolean enable)
 280:   {
 281:     checkControlPermissions();
 282:     if (isThreadCpuTimeSupported() ||
 283:     isCurrentThreadCpuTimeSupported())
 284:       timeEnabled = enable;
 285:     else
 286:       throw new UnsupportedOperationException("Thread time not " +
 287:                           "supported.");
 288:   }
 289: 
 290: }