1 23 24 package com.sun.enterprise.admin.monitor.registry.spi; 25 import java.util.List ; 26 import java.util.Iterator ; 27 import java.util.Collection ; 28 import java.util.ArrayList ; 29 import java.util.logging.Logger ; 30 import java.lang.management.ManagementFactory ; 31 import java.lang.management.GarbageCollectorMXBean ; 32 import java.lang.management.ThreadMXBean ; 33 import java.lang.management.ThreadInfo ; 34 import com.sun.enterprise.admin.common.constant.AdminConstants; 35 import javax.management.j2ee.statistics.Stats ; 36 import com.sun.enterprise.admin.monitor.stats.*; 37 import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType; 38 import com.sun.enterprise.admin.monitor.registry.MonitoringLevel; 39 import com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener; 40 import com.sun.enterprise.admin.monitor.registry.spi.MonitoringRegistrationHelper; 41 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistrationException; 42 import com.sun.enterprise.admin.monitor.registry.StatsHolder; 43 import com.sun.enterprise.admin.monitor.registry.spi.StatsHolderImpl; 44 import com.sun.enterprise.admin.monitor.registry.spi.reconfig.MonitoringConfigurationHandler; 45 import com.sun.enterprise.admin.monitor.stats.spi.JVMCompilationStatsImpl; 46 import com.sun.enterprise.admin.monitor.stats.spi.JVMClassLoadingStatsImpl; 47 import com.sun.enterprise.admin.monitor.stats.spi.JVMRuntimeStatsImpl; 48 import com.sun.enterprise.admin.monitor.stats.spi.JVMOperatingSystemStatsImpl; 49 import com.sun.enterprise.admin.monitor.stats.spi.JVMGarbageCollectorStatsImpl; 50 import com.sun.enterprise.admin.monitor.stats.spi.JVMMemoryStatsImpl; 51 import com.sun.enterprise.admin.monitor.stats.spi.JVMThreadStatsImpl; 52 import com.sun.enterprise.admin.monitor.stats.spi.JVMThreadInfoStatsImpl; 53 54 55 60 61 public class JVMMonitoringManager implements MonitoringLevelListener { 62 63 private static final Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 64 private static final JVMMonitoringManager jmm = new JVMMonitoringManager(); 65 private final int STACK_DEPTH = 5; 66 private final String THREAD_NODE_NAME = "thread"; 67 68 69 private JVMMonitoringManager() { 70 } 71 72 public static JVMMonitoringManager getInstance() { 73 return jmm; 74 } 75 76 80 public void registerStats(StatsHolder rootNode) { 81 MonitoringLevel level = MonitoringConfigurationHandler.getLevel(MonitoredObjectType.JVM); 82 if(level != MonitoringLevel.OFF) { 84 try { 85 registerJVMCompilationStats(rootNode); 86 registerJVMClassLoadingStats(rootNode); 87 registerJVMRuntimeStats(rootNode); 88 registerJVMOperatingSystemStats(rootNode); 89 registerJVMGarbageCollectorStats(rootNode); 90 registerJVMMemoryStats(rootNode); 91 registerJVMThreadStats(rootNode); 92 if(level == MonitoringLevel.HIGH) 93 registerJVMThreadInfoStats(rootNode); 94 } catch(MonitoringRegistrationException mre) { 95 logger.finest("MonitoringRegistrationException in the registration of JVM1.5 Stats: "+ mre.getLocalizedMessage()); 96 } catch(Exception e) { 97 logger.finest("Exception in the registration of JVM 1.5 Stats: "+ e.getLocalizedMessage()); 98 } 99 } 100 } 101 102 105 110 public void registerJVMCompilationStats(StatsHolder rootNode) throws 111 MonitoringRegistrationException { 112 113 final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 114 assert(jvmNode != null): "jvm node is null"; 115 StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_COMPILATION.getTypeName(), 116 MonitoredObjectType.JVM_COMPILATION); 117 childNode.setStats(new JVMCompilationStatsImpl()); 118 childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMCompilationStats.class); 119 childNode.setDottedName(DottedNameFactory.getJVMCompilationDottedName()); 120 childNode.setObjectName(MonitoringObjectNames.getJVMCompilationObjectName()); 121 childNode.registerMBean(); 122 } 123 124 129 public void registerJVMClassLoadingStats(StatsHolder rootNode) 130 throws MonitoringRegistrationException { 131 132 final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 133 assert(jvmNode != null): "jvm node is null"; 134 StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_CLASSLOADING.getTypeName(), 135 MonitoredObjectType.JVM_CLASSLOADING); 136 childNode.setStats(new JVMClassLoadingStatsImpl()); 137 childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMClassLoadingStats.class); 138 childNode.setDottedName(DottedNameFactory.getJVMClassLoadingDottedName()); 139 childNode.setObjectName(MonitoringObjectNames.getJVMClassLoadingObjectName()); 140 childNode.registerMBean(); 141 } 142 143 148 public void registerJVMRuntimeStats(StatsHolder rootNode) throws 149 MonitoringRegistrationException { 150 151 final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 152 assert(jvmNode != null): "jvm node is null"; 153 StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_RUNTIME.getTypeName(), 154 MonitoredObjectType.JVM_RUNTIME); 155 childNode.setStats(new JVMRuntimeStatsImpl()); 156 childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMRuntimeStats.class); 157 childNode.setDottedName(DottedNameFactory.getJVMRuntimeDottedName()); 158 childNode.setObjectName(MonitoringObjectNames.getJVMRuntimeObjectName()); 159 childNode.registerMBean(); 160 } 161 162 163 168 public void registerJVMOperatingSystemStats(StatsHolder rootNode) 169 throws MonitoringRegistrationException { 170 171 final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 172 assert(jvmNode != null): "jvm node is null"; 173 StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_OS.getTypeName(), 174 MonitoredObjectType.JVM_OS); 175 childNode.setStats(new JVMOperatingSystemStatsImpl()); 176 childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMOperatingSystemStats.class); 177 childNode.setDottedName(DottedNameFactory.getJVMOSDottedName()); 178 childNode.setObjectName(MonitoringObjectNames.getJVMOSObjectName()); 179 childNode.registerMBean(); 180 } 181 182 189 public void unregisterStats(StatsHolder rootNode, boolean includeThreadInfo) throws 190 MonitoringRegistrationException { 191 192 StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 193 assert(jvmNode != null):"jvm node is null"; 194 Collection c = new ArrayList (); 195 c.addAll(jvmNode.getAllChildren()); 196 Iterator iter = c.iterator(); 197 while(iter.hasNext()) { 198 StatsHolder childNode = (StatsHolder)iter.next(); 199 200 if(childNode.getType() == MonitoredObjectType.JVM_GCS) { 201 Collection c1 = new ArrayList (); 202 c1.addAll(childNode.getAllChildren()); 203 Iterator it = c1.iterator(); 204 while(it.hasNext()) { 205 StatsHolder s = (StatsHolder)it.next(); 206 s.unregisterMBean(); 207 childNode.removeChild(s.getName()); 208 } 209 } 210 211 if((childNode.getType() == MonitoredObjectType.JVM_THREAD) && includeThreadInfo) { 212 Collection c1 = new ArrayList (); 213 c1.addAll(childNode.getAllChildren()); 214 Iterator it = c1.iterator(); 215 while(it.hasNext()) { 216 StatsHolder s = (StatsHolder)it.next(); 217 s.unregisterMBean(); 218 childNode.removeChild(s.getName()); 219 } 220 } 221 222 childNode.unregisterMBean(); 223 jvmNode.removeChild(childNode.getName()); 224 } 225 } 226 227 232 public void registerJVMGarbageCollectorStats(StatsHolder rootNode) throws 233 MonitoringRegistrationException { 234 235 StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 236 assert(jvmNode != null):"jvm node is null"; 237 StatsHolder gcsNode = jvmNode.addChild(MonitoredObjectType.JVM_GCS.getTypeName(), 239 MonitoredObjectType.JVM_GCS); 240 gcsNode.setDottedName(DottedNameFactory.getJVMGCSDottedName()); 241 gcsNode.setObjectName(MonitoringObjectNames.getJVMGCSSObjectName()); 242 gcsNode.registerMBean(); 243 List beanList = ManagementFactory.getGarbageCollectorMXBeans(); 245 Iterator iter = beanList.iterator(); 246 while(iter.hasNext()) { 247 GarbageCollectorMXBean bean = (GarbageCollectorMXBean ) iter.next(); 248 StatsHolder gcNode = gcsNode.addChild(bean.getName(), MonitoredObjectType.JVM_GC); 249 gcNode.setStats(new JVMGarbageCollectorStatsImpl(bean)); 250 gcNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMGarbageCollectorStats.class); 251 gcNode.setDottedName(DottedNameFactory.getJVMGCDottedName(bean.getName())); 252 gcNode.setObjectName(MonitoringObjectNames.getJVMGCObjectName(bean.getName())); 253 gcNode.registerMBean(); 254 } 255 } 256 257 262 public void registerJVMMemoryStats(StatsHolder rootNode) 263 throws MonitoringRegistrationException { 264 265 final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 266 assert(jvmNode != null): "jvm node is null"; 267 StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_MEMORY.getTypeName(), 268 MonitoredObjectType.JVM_MEMORY); 269 childNode.setStats(new JVMMemoryStatsImpl()); 270 childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMMemoryStats.class); 271 childNode.setDottedName(DottedNameFactory.getJVMMemoryDottedName()); 272 childNode.setObjectName(MonitoringObjectNames.getJVMMemoryObjectName()); 273 childNode.registerMBean(); 274 } 275 276 public void registerJVMThreadStats(StatsHolder rootNode) 277 throws MonitoringRegistrationException { 278 279 final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 280 assert(jvmNode != null): "jvm node is null"; 281 StatsHolder childNode = jvmNode.addChild(MonitoredObjectType.JVM_THREAD.getTypeName(), 282 MonitoredObjectType.JVM_THREAD); 283 childNode.setStats(new JVMThreadStatsImpl()); 284 childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMThreadStats.class); 285 childNode.setDottedName(DottedNameFactory.getJVMThreadDottedName()); 286 childNode.setObjectName(MonitoringObjectNames.getJVMThreadObjectName()); 287 childNode.registerMBean(); 288 } 289 290 public void registerJVMThreadInfoStats(StatsHolder rootNode) throws 291 MonitoringRegistrationException { 292 293 final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 294 assert(jvmNode != null): "jvm node is null"; 295 final StatsHolder threadSystemNode = jvmNode.getChild(MonitoredObjectType.JVM_THREAD.getTypeName()); 296 assert(threadSystemNode != null): "thread-system node is null"; 297 ThreadMXBean bean = ManagementFactory.getThreadMXBean(); 298 long ids[] = bean.getAllThreadIds(); 299 for(int i=0; i < ids.length; i++) { 300 ThreadInfo info = bean.getThreadInfo(ids[i], STACK_DEPTH); 301 String threadName = THREAD_NODE_NAME + "-" + ids[i]; 302 StatsHolder childNode = threadSystemNode.addChild(threadName, MonitoredObjectType.JVM_THREAD_INFO); 303 childNode.setStats(new JVMThreadInfoStatsImpl(info)); 304 childNode.setStatsClass(com.sun.enterprise.admin.monitor.stats.JVMThreadInfoStats.class); 305 childNode.setDottedName(DottedNameFactory.getJVMThreadInfoDottedName(threadName)); 306 childNode.setObjectName(MonitoringObjectNames.getJVMThreadInfoObjectName(threadName)); 307 childNode.registerMBean(); 308 } 309 } 310 311 public void unregisterJVMThreadInfoStats(StatsHolder rootNode) throws 312 MonitoringRegistrationException { 313 314 final StatsHolder jvmNode = rootNode.getChild(MonitoredObjectType.JVM.getTypeName()); 315 assert(jvmNode != null): "jvm node is null"; 316 final StatsHolder threadSystemNode = jvmNode.getChild(MonitoredObjectType.JVM_THREAD.getTypeName()); 317 assert(threadSystemNode != null): "thread-system node is null"; 318 Collection c = new ArrayList (); 319 c.addAll(threadSystemNode.getAllChildren()); 320 Iterator iter = c.iterator(); 321 while(iter.hasNext()) { 322 StatsHolder s = (StatsHolder)iter.next(); 323 s.unregisterMBean(); 324 threadSystemNode.removeChild(s.getName()); 325 } 326 } 327 328 private void registerAllStats(StatsHolder rootNode, boolean includeThreadInfo) { 329 try { 330 registerJVMCompilationStats(rootNode); 331 registerJVMClassLoadingStats(rootNode); 332 registerJVMRuntimeStats(rootNode); 333 registerJVMOperatingSystemStats(rootNode); 334 registerJVMGarbageCollectorStats(rootNode); 335 registerJVMMemoryStats(rootNode); 336 registerJVMThreadStats(rootNode); 337 if(includeThreadInfo) 338 registerJVMThreadInfoStats(rootNode); 339 } catch(MonitoringRegistrationException mre) { 340 logger.finest("MonitoringRegistrationException in the registration of JVM1.5 Stats: "+ mre.getLocalizedMessage()); 341 } catch(Exception e) { 342 logger.finest("Exception in the registration of JVM 1.5 Stats: "+ e.getLocalizedMessage()); 343 } 344 } 345 346 347 public void changeLevel (MonitoringLevel from, 349 MonitoringLevel to, 350 MonitoredObjectType type) { 351 352 logger.finest("changeLevel being invoked on the JVM"); 353 final MonitoringRegistrationHelper registryImpl = 354 (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance(); 355 356 StatsHolder root = registryImpl.getRootStatsHolder(); 357 boolean includeThreadInfo = false; 358 359 if(to == MonitoringLevel.OFF) { 360 if(from == MonitoringLevel.HIGH) 361 includeThreadInfo = true; 362 try { 363 unregisterStats(root, includeThreadInfo); 364 } catch(MonitoringRegistrationException mre) { 365 logger.finest("MonitoringRegistrationException in the unregistration of JVM 1.5 Stats: "+ mre.getLocalizedMessage()); 366 } catch(Exception e) { 367 logger.finest("Exception in the unregistration of JVM 1.5 Stats: "+ e.getLocalizedMessage()); 368 } 369 } 370 371 if(from == MonitoringLevel.OFF) { 372 if(to == MonitoringLevel.HIGH) 373 includeThreadInfo = true; 374 registerAllStats(root, includeThreadInfo); 375 } 376 377 if(from == MonitoringLevel.LOW && to == MonitoringLevel.HIGH) { 378 try { 379 registerJVMThreadInfoStats(root); 380 } catch(MonitoringRegistrationException mre) { 381 logger.finest("MonitoringRegistrationException in the registration of JVM ThreadInfoStats: "+ mre.getLocalizedMessage()); 382 } catch(Exception e) { 383 logger.finest("Exception in the registration of JVM ThreadInfo Stats: "+ e.getLocalizedMessage()); 384 } 385 } 386 387 if(from == MonitoringLevel.HIGH && to == MonitoringLevel.LOW) { 388 try{ 389 unregisterJVMThreadInfoStats(root); 390 } catch(MonitoringRegistrationException mre) { 391 logger.finest("MonitoringRegistrationException in the unregistration of JVM ThreadInfoStats: "+ mre.getLocalizedMessage()); 392 } catch(Exception e) { 393 logger.finest("Exception in the unregistration of JVM ThreadInfo Stats: "+ e.getLocalizedMessage()); 394 } 395 } 396 397 } 398 399 public void changeLevel (MonitoringLevel from, 400 MonitoringLevel to, 401 Stats handback) { 402 } 403 404 public void setLevel (MonitoringLevel level) { 405 } 406 } 407 | Popular Tags |