Java Code Examples for java.lang.management.ManagementFactory.getPlatformMXBeans()

The following are Jave code examples for showing how to use getPlatformMXBeans() of the java.lang.management.ManagementFactory class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: swage   File: BufferPoolSensor.java   View Source Code Vote up 6 votes
@Override
public void sense(final MetricRecorder.Context metricContext)
{
    List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);

    for (BufferPoolMXBean mxBean : pools) {
        //TODO: something else?
        String name = mxBean.getName();
        Metric countMetric = Metric.define("BufferPoolCount_"+name);
        Metric usedMetric = Metric.define("BufferPoolUsed_"+name);
        Metric maxMetric = Metric.define("BufferPoolMax_"+name);

        metricContext.record(countMetric, mxBean.getCount(), Unit.NONE);
        metricContext.record(usedMetric, mxBean.getMemoryUsed() / M, Unit.MEGABYTE);
        metricContext.record(maxMetric,  mxBean.getTotalCapacity() / M, Unit.MEGABYTE);
    }

}
 
Example 2
Project: openjdk-systemtest   File: ByteBufferMXBeanTest.java   View Source Code Vote up 6 votes
public void testByteBufferMXBeanMethods() {
	List<BufferPoolMXBean> byteBufferPools = 
		ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
	
	for (int counter = 0; counter < 10; counter++) {
	
		for (BufferPoolMXBean bean: byteBufferPools) {
			
			// Name should not be blank or null
			assertNotNull ("MXBean name is null", bean.getName());
			assertFalse ("MXBean name is blank", bean.getName().equalsIgnoreCase(""));
			// Total capacity should not be negative
			assertTrue (bean.getName() + "'s total capacity was not greater than zero", bean.getTotalCapacity() >= 0);
			// Likewise memoryUsed
			assertTrue (bean.getName() + "'s memory used was not greater than zero", bean.getMemoryUsed() >= 0);
			
			assertTrue (bean.getName() + "'s object name is null or blank", 
					bean.getObjectName() != null &&	bean.getObjectName().toString() != "");
		}
	}
}
 
Example 3
Project: openjdk-systemtest   File: EchoServer.java   View Source Code Vote up 5 votes
@Override
protected void heartbeat() {
	// Let them know we are alive
	// Print some stats about the ByteBuffers
	List<BufferPoolMXBean> byteBufferPools = 
		ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);

	String direct = "direct";
	String mapped = "mapped";
	
	for (BufferPoolMXBean bean: byteBufferPools) {
		if (bean.getName().equalsIgnoreCase(direct)) {
			direct += " - " + bean.getMemoryUsed() + "/" + bean.getTotalCapacity();
		}
		if (bean.getName().equalsIgnoreCase(mapped)) {
			mapped += " - " + bean.getMemoryUsed() + "/" + bean.getTotalCapacity();
		}
	}
	
	logMessage(direct + " , " + mapped);
	logMessage("Currently connected clients: " + getConnectedClients().size());
	logMessage("Total bytes received       : " + bytesReceived);
	logMessage("Total clients connected    : " + clientsConnected);
	logMessage("Total clients disconnected : " + clientsDisconnected);
	
	synchronized (connectionsPerSecond) {
		if (!connectionsPerSecond.isEmpty()) {
			logMessage("CPS Max: " + Collections.max(connectionsPerSecond.values()) );
		}
	}
	synchronized (connectionsPerMinute) {
		if (!connectionsPerMinute.isEmpty()) {
			logMessage("CPM Max: " + Collections.max(connectionsPerMinute.values()) );
		}
	}

	logMessage();
}
 
Example 4
Project: QDrill   File: MemoryIterator.java   View Source Code Vote up 5 votes
private BufferPoolMXBean getDirectBean() {
  List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
  for (BufferPoolMXBean b : pools) {
    if (b.getName().equals("direct")) {
      return b;
    }
  }
  throw new IllegalStateException("Unable to find direct buffer bean.  JVM must be too old.");
}
 
Example 5
Project: QDrill   File: DrillTest.java   View Source Code Vote up 5 votes
public SystemManager(){
  memoryBean = ManagementFactory.getMemoryMXBean();
  BufferPoolMXBean localBean = null;
  List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
  for(BufferPoolMXBean b : pools){
    if(b.getName().equals("direct")){
      localBean = b;

    }
  }
  directBean = localBean;
}
 
Example 6
Project: jdk8u-jdk   File: TestMaxCachedBufferSize.java   View Source Code Vote up 5 votes
private static BufferPoolMXBean getDirectPool() {
    final List<BufferPoolMXBean> pools =
              ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    for (BufferPoolMXBean pool : pools) {
        if (pool.getName().equals("direct")) {
            return pool;
        }
    }
    throw new Error("could not find direct pool");
}
 
Example 7
Project: jdk8u-jdk   File: GetDiagnosticOptions.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkDiagnosticOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkDiagnosticOptions(mbean);
}
 
Example 8
Project: jdk8u-jdk   File: GetVMOption.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkVMOption(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkVMOption(mbean);
}
 
Example 9
Project: jdk8u-jdk   File: SetAllVMOptions.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    setAllVMOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    setAllVMOptions(mbean);
}
 
Example 10
Project: openjdk-jdk10   File: TestMaxCachedBufferSize.java   View Source Code Vote up 5 votes
private static BufferPoolMXBean getDirectPool() {
    final List<BufferPoolMXBean> pools =
              ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    for (BufferPoolMXBean pool : pools) {
        if (pool.getName().equals("direct")) {
            return pool;
        }
    }
    throw new Error("could not find direct pool");
}
 
Example 11
Project: openjdk-jdk10   File: GetDiagnosticOptions.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkDiagnosticOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkDiagnosticOptions(mbean);
}
 
Example 12
Project: openjdk-jdk10   File: GetVMOption.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkVMOption(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkVMOption(mbean);
}
 
Example 13
Project: openjdk-jdk10   File: SetAllVMOptions.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    setAllVMOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    setAllVMOptions(mbean);
}
 
Example 14
Project: openjdk-jdk10   File: GetDoubleVMOption.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkVMOption(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkVMOption(mbean);
}
 
Example 15
Project: dremio-oss   File: MemoryIterator.java   View Source Code Vote up 5 votes
/**
 * @return Direct buffer JMX bean
 */
public static BufferPoolMXBean getDirectBean() {
  List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
  for (BufferPoolMXBean b : pools) {
    if (b.getName().equals("direct")) {
      return b;
    }
  }
  throw new IllegalStateException("Unable to find direct buffer bean.  JVM must be too old.");
}
 
Example 16
Project: dremio-oss   File: DremioTest.java   View Source Code Vote up 5 votes
public SystemManager(){
  memoryBean = ManagementFactory.getMemoryMXBean();
  BufferPoolMXBean localBean = null;
  List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
  for(BufferPoolMXBean b : pools){
    if(b.getName().equals("direct")){
      localBean = b;

    }
  }
  directBean = localBean;
}
 
Example 17
Project: elasticsearch_my   File: JvmStats.java   View Source Code Vote up 4 votes
public static JvmStats jvmStats() {
    MemoryUsage memUsage = memoryMXBean.getHeapMemoryUsage();
    long heapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
    long heapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
    long heapMax = memUsage.getMax() < 0 ? 0 : memUsage.getMax();
    memUsage = memoryMXBean.getNonHeapMemoryUsage();
    long nonHeapUsed = memUsage.getUsed() < 0 ? 0 : memUsage.getUsed();
    long nonHeapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    List<MemoryPool> pools = new ArrayList<>();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        try {
            MemoryUsage usage = memoryPoolMXBean.getUsage();
            MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
            String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), null);
            if (name == null) { // if we can't resolve it, its not interesting.... (Per Gen, Code Cache)
                continue;
            }
            pools.add(new MemoryPool(name,
                    usage.getUsed() < 0 ? 0 : usage.getUsed(),
                    usage.getMax() < 0 ? 0 : usage.getMax(),
                    peakUsage.getUsed() < 0 ? 0 : peakUsage.getUsed(),
                    peakUsage.getMax() < 0 ? 0 : peakUsage.getMax()
            ));
        } catch (Exception ex) {
            /* ignore some JVMs might barf here with:
             * java.lang.InternalError: Memory Pool not found
             * we just omit the pool in that case!*/
        }
    }
    Mem mem = new Mem(heapCommitted, heapUsed, heapMax, nonHeapCommitted, nonHeapUsed, Collections.unmodifiableList(pools));
    Threads threads = new Threads(threadMXBean.getThreadCount(), threadMXBean.getPeakThreadCount());

    List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    GarbageCollector[] collectors = new GarbageCollector[gcMxBeans.size()];
    for (int i = 0; i < collectors.length; i++) {
        GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
        collectors[i] = new GarbageCollector(GcNames.getByGcName(gcMxBean.getName(), gcMxBean.getName()),
                gcMxBean.getCollectionCount(), gcMxBean.getCollectionTime());
    }
    GarbageCollectors garbageCollectors = new GarbageCollectors(collectors);
    List<BufferPool> bufferPoolsList = Collections.emptyList();
    try {
        List<BufferPoolMXBean> bufferPools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
        bufferPoolsList = new ArrayList<>(bufferPools.size());
        for (BufferPoolMXBean bufferPool : bufferPools) {
            bufferPoolsList.add(new BufferPool(bufferPool.getName(), bufferPool.getCount(),
                    bufferPool.getTotalCapacity(), bufferPool.getMemoryUsed()));
        }
    } catch (Exception e) {
        // buffer pools are not available
    }

    Classes classes = new Classes(classLoadingMXBean.getLoadedClassCount(), classLoadingMXBean.getTotalLoadedClassCount(),
            classLoadingMXBean.getUnloadedClassCount());

    return new JvmStats(System.currentTimeMillis(), runtimeMXBean.getUptime(), mem, threads,
            garbageCollectors, bufferPoolsList, classes);
}
 
Example 18
Project: jdk8u-jdk   File: Basic.java   View Source Code Vote up 4 votes
public static void main(String[] args) throws Exception {
    Random rand = new Random();

    // allocate a few direct buffers
    int bufferCount = 5 + rand.nextInt(20);
    buffers = new ArrayList<ByteBuffer>(bufferCount);
    long totalCapacity = 0L;
    for (int i=0; i<bufferCount; i++) {
        int cap = 1024 + rand.nextInt(4096);
        buffers.add( ByteBuffer.allocateDirect(cap) );
        totalCapacity += cap;
    }

    // create a mapped buffer
    Path tmpfile = Files.createTempFile("blah", null);
    tmpfile.toFile().deleteOnExit();
    try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) {
        mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100);
        bufferCount++;
        totalCapacity += mbb.capacity();
    }

    // use platform MXBeans directly
    List<BufferPoolMXBean> pools =
        ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    check(pools, bufferCount, totalCapacity);

    // use MBeanServer
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> mbeans = server.queryNames(
        new ObjectName("java.nio:type=BufferPool,*"), null);
    pools = new ArrayList<BufferPoolMXBean>();
    for (ObjectName name: mbeans) {
        BufferPoolMXBean pool = ManagementFactory
            .newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class);
        pools.add(pool);
    }
    check(pools, bufferCount, totalCapacity);

    // attempt to unmap mapped buffer
    WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb);
    mbb = null;
    do {
        System.gc();
        Thread.sleep(250);
    } while (ref.get() != null);
}
 
Example 19
Project: openjdk-jdk10   File: Basic.java   View Source Code Vote up 4 votes
public static void main(String[] args) throws Exception {
    Random rand = new Random();

    // allocate a few direct buffers
    int bufferCount = 5 + rand.nextInt(20);
    buffers = new ArrayList<ByteBuffer>(bufferCount);
    long totalCapacity = 0L;
    for (int i=0; i<bufferCount; i++) {
        int cap = 1024 + rand.nextInt(4096);
        buffers.add( ByteBuffer.allocateDirect(cap) );
        totalCapacity += cap;
    }

    // create a mapped buffer
    Path tmpfile = Files.createTempFile("blah", null);
    tmpfile.toFile().deleteOnExit();
    try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) {
        mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100);
        bufferCount++;
        totalCapacity += mbb.capacity();
    }

    // use platform MXBeans directly
    List<BufferPoolMXBean> pools =
        ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    check(pools, bufferCount, totalCapacity);

    // use MBeanServer
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> mbeans = server.queryNames(
        new ObjectName("java.nio:type=BufferPool,*"), null);
    pools = new ArrayList<BufferPoolMXBean>();
    for (ObjectName name: mbeans) {
        BufferPoolMXBean pool = ManagementFactory
            .newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class);
        pools.add(pool);
    }
    check(pools, bufferCount, totalCapacity);

    // attempt to unmap mapped buffer
    WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb);
    mbb = null;
    do {
        System.gc();
        Thread.sleep(250);
    } while (ref.get() != null);
}
 
Example 20
Project: openjdk-systemtest   File: ByteBufferMXBeanTest.java   View Source Code Vote up 2 votes
public void testGetPlatformMXBeans() {
	
	// BufferPoolMXBean
	List<BufferPoolMXBean> byteBufferPools = 
		ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
	
	assertTrue("No BufferPoolMXBeans returned", byteBufferPools.size() > 0);
	
	// ThreadMXBean
	List<ThreadMXBean> threadMXBeans = 
		ManagementFactory.getPlatformMXBeans(ThreadMXBean.class);
	
	assertTrue("No ThreadMXBeans returned", threadMXBeans.size() > 0);
	
	// ClassLoadingMXBean
	List<ClassLoadingMXBean> classloadingMXBeans = 
		ManagementFactory.getPlatformMXBeans(ClassLoadingMXBean.class);
	
	assertTrue("No ClassLoadingMXBeans returned", classloadingMXBeans.size() > 0);
	
	// GarbageCollectorMXBean
	List<GarbageCollectorMXBean> gcMXBeans = 
		ManagementFactory.getPlatformMXBeans(GarbageCollectorMXBean.class);
	
	assertTrue("No GarbageCollectorMXBeans returned", gcMXBeans.size() > 0);
	
	// MemoryManagerMXBean
	List<MemoryManagerMXBean> memoryManagerBeans = 
		ManagementFactory.getPlatformMXBeans(MemoryManagerMXBean.class);
	
	assertTrue("No MemoryManagerMXBeans returned", memoryManagerBeans.size() > 0);

	// MemoryPoolMXBean
	List<MemoryPoolMXBean> memoryPoolBeans = 
		ManagementFactory.getPlatformMXBeans(MemoryPoolMXBean.class);
	
	assertTrue("No MemoryPoolMXBeans returned", memoryPoolBeans.size() > 0);
	
	// RuntimeMXBean
	List<RuntimeMXBean> runtimeBeans = 
		ManagementFactory.getPlatformMXBeans(RuntimeMXBean.class);
	
	assertTrue("No RuntimeMXBeans returned", runtimeBeans.size() > 0);
	
}