package com.github.buzztaiki.jmpstat;

import java.io.IOException;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JmpStat {
    private final MBeanServerConnection server;
    private final PrintStream out;

    public JmpStat(MBeanServerConnection server, PrintStream out) {
        this.server = server;
        this.out = out;
    }

    public boolean hasMemoryPool(Set<String> poolNames) throws IOException {
        for (MemoryPoolMXBean memoryPool : getMemoryPools()) {
            if (poolNames.contains(memoryPool.getName())) {
                return true;
            }
        }
        return false;
    }

    public void printAllPools() throws IOException {
        for (MemoryPoolMXBean memoryPool : getMemoryPools()) {
            out.format("%s\t%s%n", memoryPool.getName(), memoryPool.getUsage());
        }
    }

    public void pollLoop(Set<String> poolNames, long interval) throws IOException {
        out.println("pool_name\tinit\tused\tcommitted\tmax");
        for (;;) {
            for (MemoryPoolMXBean memoryPool : getMemoryPools()) {
                if (!poolNames.contains(memoryPool.getName())) {
                    continue;
                }
                MemoryUsage usage = memoryPool.getUsage();
                out.format("%s\t%d\t%d\t%d\t%d%n",
                    memoryPool.getName(),
                    usage.getInit()/1024,
                    usage.getUsed()/1024,
                    usage.getCommitted()/1024,
                    usage.getMax()/1024);
            }
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                return;
            }
        }
    }

    private Iterable<MemoryPoolMXBean> getMemoryPools() throws IOException {
        return ManagementFactory.getPlatformMXBeans(server, MemoryPoolMXBean.class);
    }
}

@@ -209,27 +209,29 @@
 agement.
-MemoryUsage
+RuntimeMXBean
 ;%0Aimport
@@ -240,45 +240,35 @@
 ava.
-util.Arrays;%0Aimport java.util.HashSet
+lang.management.MemoryUsage
 ;%0Aim
@@ -337,151 +337,8 @@
 ion;
-%0Aimport javax.management.remote.JMXConnector;%0Aimport javax.management.remote.JMXConnectorFactory;%0Aimport javax.management.remote.JMXServiceURL;
 %0A%0Apu
@@ -1172,16 +1172,24 @@
 rintln(%22
+uptime%5Ct
 pool_nam
@@ -1221,16 +1221,129 @@
 tmax%22);%0A
+        RuntimeMXBean runtime = getRuntime();%0A        Iterable%3CMemoryPoolMXBean%3E memoryPools = getMemoryPools();%0A
         
@@ -1392,36 +1392,33 @@
 an memoryPool : 
-getM
+m
 emoryPools()) %7B%0A
@@ -1403,34 +1403,32 @@
 ol : memoryPools
-()
 ) %7B%0A            
@@ -1612,24 +1612,30 @@
 ut.format(%22%25
+.1f%5Ct%25
 s%5Ct%25d%5Ct%25d%5Ct%25
@@ -1644,16 +1644,64 @@
 t%25d%25n%22,%0A
+                    runtime.getUptime()/1000.0,%0A
         
@@ -2248,11 +2248,159 @@
 );%0A    %7D
+%0A%0A    private RuntimeMXBean getRuntime() throws IOException %7B%0A        return ManagementFactory.getPlatformMXBean(server, RuntimeMXBean.class);%0A    %7D
 %0A%7D%0A

add uptime and reuse mbean