@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); } }
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() != ""); } } }
@Override public Object next() { if (!beforeFirst) { throw new IllegalStateException(); } beforeFirst = false; final MemoryInfo memoryInfo = new MemoryInfo(); final DrillbitEndpoint endpoint = context.getIdentity(); memoryInfo.hostname = endpoint.getAddress(); memoryInfo.user_port = endpoint.getUserPort(); final MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); memoryInfo.heap_current = heapMemoryUsage.getUsed(); memoryInfo.heap_max = heapMemoryUsage.getMax(); BufferPoolMXBean directBean = getDirectBean(); memoryInfo.jvm_direct_current = directBean.getMemoryUsed(); memoryInfo.direct_current = context.getDrillbitContext().getAllocator().getAllocatedMemory(); memoryInfo.direct_max = TopLevelAllocator.MAXIMUM_DIRECT_MEMORY; return memoryInfo; }
static void check(List<BufferPoolMXBean> pools, int minBufferCount, long minTotalCapacity) { int bufferCount = 0; long totalCap = 0; long totalMem = 0; for (BufferPoolMXBean pool: pools) { bufferCount += pool.getCount(); totalCap += pool.getTotalCapacity(); totalMem += pool.getMemoryUsed(); } if (bufferCount < minBufferCount) throw new RuntimeException("Count less than expected"); if (totalMem < minTotalCapacity) throw new RuntimeException("Memory usage less than expected"); if (totalCap < minTotalCapacity) throw new RuntimeException("Total capacity less than expected"); }
static void submitTasks(ExecutorService executor, int count) { for (int i=0; i < count && !failed; i++) { executor.execute(new Runnable() { @Override public void run() { List<PlatformManagedObject> mbeans = new ArrayList<>(); mbeans.add(ManagementFactory.getPlatformMXBean(PlatformLoggingMXBean.class)); mbeans.addAll(ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class)); for (PlatformManagedObject pmo : mbeans) { // Name should not be null if (pmo.getObjectName() == null) { failed = true; throw new RuntimeException("TEST FAILED: getObjectName() returns null"); } } } }); } }
@Override public Object next() { if (!beforeFirst) { throw new IllegalStateException(); } beforeFirst = false; final MemoryInfo memoryInfo = new MemoryInfo(); final NodeEndpoint endpoint = dbContext.getEndpoint(); memoryInfo.hostname = endpoint.getAddress(); memoryInfo.fabric_port = endpoint.getFabricPort(); final MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); memoryInfo.heap_current = heapMemoryUsage.getUsed(); memoryInfo.heap_max = heapMemoryUsage.getMax(); BufferPoolMXBean directBean = getDirectBean(); memoryInfo.jvm_direct_current = directBean.getMemoryUsed(); memoryInfo.direct_current = dbContext.getAllocator().getAllocatedMemory(); memoryInfo.direct_max = SabotConfig.getMaxDirectMemory(); return memoryInfo; }
public static void main(String[] args) { List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); for (BufferPoolMXBean pool:pools) { System.out.println(String.format( "%s %d/%d", pool.getName(), pool.getMemoryUsed(), pool.getTotalCapacity())); } RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean(); for (String arg:RuntimemxBean.getInputArguments()) { System.out.println(arg); } HotSpotDiagnosticMXBean hsdiag = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); for (VMOption i:hsdiag.getDiagnosticOptions()) { System.out.println(i.getName() + ":" + i.getValue()); } System.out.println(hsdiag.getVMOption("MaxDirectMemorySize")); System.out.println(sun.misc.VM.maxDirectMemory()); System.out.println(ByteBuffer.allocateDirect(1000000000)); System.out.println(ByteBuffer.allocateDirect(1000000000)); System.out.println(ByteBuffer.allocateDirect(1000000000)); System.out.println(getDirectMemoryUsed()); }
@Override public Object next() { if (!beforeFirst) { throw new IllegalStateException(); } beforeFirst = false; final MemoryInfo memoryInfo = new MemoryInfo(); final DrillbitEndpoint endpoint = context.getIdentity(); memoryInfo.hostname = endpoint.getAddress(); memoryInfo.user_port = endpoint.getUserPort(); final MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); memoryInfo.heap_current = heapMemoryUsage.getUsed(); memoryInfo.heap_max = heapMemoryUsage.getMax(); BufferPoolMXBean directBean = getDirectBean(); memoryInfo.jvm_direct_current = directBean.getMemoryUsed(); memoryInfo.direct_current = context.getDrillbitContext().getAllocator().getAllocatedMemory(); memoryInfo.direct_max = DrillConfig.getMaxDirectMemory(); return memoryInfo; }
@Override public void run() { try { long memoryUsed = 0; for (BufferPoolMXBean bufferPool : bufferPools) { if (bufferPool.getName().equals("direct")) { memoryUsed = bufferPool.getMemoryUsed(); } } Counter counter = new Counter(ValueType.VALUE); counter.set(memoryUsed); check(counter, config.getDescription(), "Allocate more direct memory. "); } catch (Exception x) { // TODO what? } }
@Override public Map<String, Metric> getMetrics() { Map<String, Metric> metrics = super.getMetrics(); MemoryMXBean memMxBean = ManagementFactory.getMemoryMXBean(); registerGauge(metrics, MEMORY_OBJ_PENDING_FINALIZATION_COUNT_METRIC_NAME, memMxBean.getObjectPendingFinalizationCount()); MemoryUsage heapMemUsage = memMxBean.getHeapMemoryUsage(), nonHeapMemUsage = memMxBean.getNonHeapMemoryUsage(); registerMemoryUsageMetrics(metrics, MEMORY_HEAP_METRIC_NAME_PREFIX, heapMemUsage); registerMemoryUsageMetrics(metrics, MEMORY_NON_HEAP_METRIC_NAME_PREFIX, nonHeapMemUsage); registerMemoryUsageMetrics(metrics, MEMORY_TOTAL_METRIC_NAME_PREFIX, (heapMemUsage.getCommitted() + nonHeapMemUsage.getCommitted()), (heapMemUsage.getInit() + nonHeapMemUsage.getInit()), -1, (heapMemUsage.getUsed() + nonHeapMemUsage.getUsed()), (heapMemUsage.getMax() + nonHeapMemUsage.getMax())); ManagementFactory.getMemoryPoolMXBeans().stream().filter(MemoryPoolMXBean::isValid) .forEach(memPoolMxBean -> registerMemoryPoolMetrics(metrics, memPoolMxBean)); ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class).stream().forEach(bufferPoolMxBean -> registerBufferPoolMetrics(metrics, bufferPoolMxBean)); return metrics; }
private void updateBufferPoolMetrics() { for (BufferPoolMXBean bufferPoolMXBean : bufferPoolMXBeanList) { String normalizedKeyName = bufferPoolMXBean.getName().replaceAll("[^\\w]", "-"); final ByteAmount memoryUsed = ByteAmount.fromBytes(bufferPoolMXBean.getMemoryUsed()); final ByteAmount totalCapacity = ByteAmount.fromBytes(bufferPoolMXBean.getTotalCapacity()); final ByteAmount count = ByteAmount.fromBytes(bufferPoolMXBean.getCount()); // The estimated memory the JVM is using for this buffer pool jvmBufferPoolMemoryUsage.safeScope(normalizedKeyName + "-memory-used") .setValue(memoryUsed.asMegabytes()); // The estimated total capacity of the buffers in this pool jvmBufferPoolMemoryUsage.safeScope(normalizedKeyName + "-total-capacity") .setValue(totalCapacity.asMegabytes()); // THe estimated number of buffers in this pool jvmBufferPoolMemoryUsage.safeScope(normalizedKeyName + "-count") .setValue(count.asMegabytes()); } }
@Before public void printTestName() { /* protect a travis stalled build */ BufferPoolMXBean direct = BufferPool.getDirectBufferPool(); BufferPoolMXBean mapped = BufferPool.getMappedBufferPool(); System.out.println(String.format("Used heap: %s/%s, direct:%s/%s, mapped:%s/%s, Active Threads: %d, Run: %s.%s", FileUtil.humanReadableByteCount(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(), false), FileUtil.humanReadableByteCount(Runtime.getRuntime().maxMemory(), false), FileUtil.humanReadableByteCount(direct.getMemoryUsed(), false), FileUtil.humanReadableByteCount(direct.getTotalCapacity(), false), FileUtil.humanReadableByteCount(mapped.getMemoryUsed(), false), FileUtil.humanReadableByteCount(mapped.getTotalCapacity(), false), Thread.activeCount(), getClass().getSimpleName(), name.getMethodName())); }
@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(); }
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."); }
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; }
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"); }
/** * @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."); }