private static void checkGarbageCollectionNotificationInfoContent( GarbageCollectionNotificationInfo notif) throws Exception { System.out.println("GC notification for " + notif.getGcName()); System.out.print("Action: " + notif.getGcAction()); System.out.println(" Cause: " + notif.getGcCause()); GcInfo info = notif.getGcInfo(); System.out.print("GC Info #" + info.getId()); System.out.print(" start:" + info.getStartTime()); System.out.print(" end:" + info.getEndTime()); System.out.println(" (" + info.getDuration() + "ms)"); Map<String, MemoryUsage> usage = info.getMemoryUsageBeforeGc(); List<String> pnames = new ArrayList<String>(); for (Map.Entry entry : usage.entrySet()) { String poolname = (String) entry.getKey(); pnames.add(poolname); MemoryUsage busage = (MemoryUsage) entry.getValue(); MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname); if (ausage == null) { throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname); } System.out.println("Usage for pool " + poolname); System.out.println(" Before GC: " + busage); System.out.println(" After GC: " + ausage); } // check if memory usage for all memory pools are returned List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean p : pools) { if (!pnames.contains(p.getName())) { throw new RuntimeException( "GcInfo does not contain " + "memory usage for pool " + p.getName()); } } }
public static void main(String[] args) throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); final Boolean isNotificationSupported = AccessController.doPrivileged( new PrivilegedAction<Boolean>() { public Boolean run() { try { Class cl = Class.forName("sun.management.VMManagementImpl"); Field f = cl.getDeclaredField("gcNotificationSupport"); f.setAccessible(true); return f.getBoolean(null); } catch (ClassNotFoundException e) { return false; } catch (NoSuchFieldException e) { return false; } catch (IllegalAccessException e) { return false; } } }); if (!isNotificationSupported) { System.out.println("GC Notification not supported by the JVM, test skipped"); return; } final ObjectName gcMXBeanPattern = new ObjectName("java.lang:type=GarbageCollector,*"); Set<ObjectName> names = mbs.queryNames(gcMXBeanPattern, null); if (names.isEmpty()) throw new Exception("Test incorrect: no GC MXBeans"); number = names.size(); for (ObjectName n : names) { if (mbs.isInstanceOf(n, "javax.management.NotificationEmitter")) { listenerInvoked.put(n.getCanonicalName(), null); GcListener listener = new GcListener(); mbs.addNotificationListener(n, listener, null, null); } } // Invocation of System.gc() to trigger major GC System.gc(); // Allocation of many short living and small objects to trigger minor GC Object data[] = new Object[32]; for (int i = 0; i < 100000000; i++) { data[i % 32] = new int[8]; } int wakeup = 0; synchronized (synchronizer) { while (count != number) { synchronizer.wait(10000); wakeup++; if (wakeup > 10) break; } } for (GarbageCollectionNotificationInfo notif : listenerInvoked.values()) { checkGarbageCollectionNotificationInfoContent(notif); } System.out.println("Test passed"); }
private static void checkPlatformMBeans() throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); Set<ObjectName> mbeanNames = mbs.queryNames(null, null); for (ObjectName name : mbeanNames) { if (!mbs.isInstanceOf(name, NotificationBroadcaster.class.getName())) { System.out.println(name + ": not a NotificationBroadcaster"); } else { MBeanInfo mbi = mbs.getMBeanInfo(name); check(name.toString(), mbi.getNotifications()); } } }
private void tryConnect(boolean requireRemoteSSL) throws IOException { if (jmxUrl == null && "localhost".equals(hostName) && port == 0) { // Monitor self this.jmxc = null; this.mbsc = ManagementFactory.getPlatformMBeanServer(); this.server = Snapshot.newSnapshot(mbsc); } else { // Monitor another process if (lvm != null) { if (!lvm.isManageable()) { lvm.startManagementAgent(); if (!lvm.isManageable()) { // FIXME: what to throw throw new IOException(lvm + "not manageable"); } } if (this.jmxUrl == null) { this.jmxUrl = new JMXServiceURL(lvm.connectorAddress()); } } Map<String, Object> env = new HashMap<String, Object>(); if (requireRemoteSSL) { env.put("jmx.remote.x.check.stub", "true"); } // Need to pass in credentials ? if (userName == null && password == null) { if (isVmConnector()) { // Check for SSL config on reconnection only if (stub == null) { checkSslConfig(); } this.jmxc = new RMIConnector(stub, null); jmxc.connect(env); } else { this.jmxc = JMXConnectorFactory.connect(jmxUrl, env); } } else { env.put(JMXConnector.CREDENTIALS, new String[] {userName, password}); if (isVmConnector()) { // Check for SSL config on reconnection only if (stub == null) { checkSslConfig(); } this.jmxc = new RMIConnector(stub, null); jmxc.connect(env); } else { this.jmxc = JMXConnectorFactory.connect(jmxUrl, env); } } this.mbsc = jmxc.getMBeanServerConnection(); this.server = Snapshot.newSnapshot(mbsc); } this.isDead = false; try { ObjectName on = new ObjectName(THREAD_MXBEAN_NAME); this.hasPlatformMXBeans = server.isRegistered(on); this.hasHotSpotDiagnosticMXBean = server.isRegistered(new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME)); // check if it has 6.0 new APIs if (this.hasPlatformMXBeans) { MBeanOperationInfo[] mopis = server.getMBeanInfo(on).getOperations(); // look for findDeadlockedThreads operations; for (MBeanOperationInfo op : mopis) { if (op.getName().equals("findDeadlockedThreads")) { this.supportsLockUsage = true; break; } } on = new ObjectName(COMPILATION_MXBEAN_NAME); this.hasCompilationMXBean = server.isRegistered(on); } } catch (MalformedObjectNameException e) { // should not reach here throw new InternalError(e.getMessage()); } catch (IntrospectionException e) { InternalError ie = new InternalError(e.getMessage()); ie.initCause(e); throw ie; } catch (InstanceNotFoundException e) { InternalError ie = new InternalError(e.getMessage()); ie.initCause(e); throw ie; } catch (ReflectionException e) { InternalError ie = new InternalError(e.getMessage()); ie.initCause(e); throw ie; } if (hasPlatformMXBeans) { // WORKAROUND for bug 5056632 // Check if the access role is correct by getting a RuntimeMXBean getRuntimeMXBean(); } }