Skip to content

Commit

Permalink
vjmxcli 中的gcUitl 提高效率 #126
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Sep 21, 2018
1 parent b24d804 commit e4de802
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 63 deletions.
5 changes: 3 additions & 2 deletions vjmxcli/src/main/java/com/vip/vjtools/jmx/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ protected static Object[] doBeans(final MBeanServerConnection mbsc, final Object
final String[] command, final boolean oneBeanOnly) throws Exception {
Object[] result = null;
Set beans = mbsc.queryMBeans(objName, null);
if (beans.size() == 0) {
if (beans.isEmpty()) {
// No bean found. Check if we are to create a bean?
if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
String className = command[0].substring(CREATE_CMD_PREFIX.length());
Expand Down Expand Up @@ -474,7 +474,7 @@ public static Object doSubCommand(MBeanServerConnection mbsc, ObjectInstance ins
result = buffer;
} else if (result instanceof AttributeList) {
AttributeList list = (AttributeList) result;
if (list.size() <= 0) {
if (list.isEmpty()) {
result = null;
} else {
StringBuffer buffer = new StringBuffer("\n");
Expand Down Expand Up @@ -730,6 +730,7 @@ public OneLineSimpleLogger() {
super();
}

@Override
public synchronized String format(LogRecord record) {
this.buffer.setLength(0);
this.date.setTime(record.getMillis());
Expand Down
116 changes: 55 additions & 61 deletions vjmxcli/src/main/java/com/vip/vjtools/jmx/GCutilExpression.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.vip.vjtools.jmx;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.management.MBeanServerConnection;
Expand All @@ -12,30 +10,26 @@

public class GCutilExpression {

private static final String EDEN = "eden";
private static final String SURVIVOR = "survivor";
private static final String OLD = "old";
private static final String TENURED = "tenured"; // 并行GC算法老生代的名称
private static final String PERM = "perm";
private static final String METASPACE = "metaspace";// JDK8永久代名称
public static final String COMPRESSED_CLASS_SPACE = "compressed class space";

private static final DecimalFormat DF = new DecimalFormat("0.00");

// MBean Name
private static final String GARBAGE_COLLECTORS = "java.lang:type=GarbageCollector,name=*";
private static final String MEM_POOL_PREFIX = "java.lang:type=MemoryPool,name=";

// Collectors的三个属性
private static final String MEMORY_POOL_ATTRIBUTE = "MemoryPoolNames";
// Collector的Attribute Name
private static final String COLLECTION_TIME_ATTRIBUTE = "CollectionTime";
private static final String COLLECTION_COUNT_ATTRIBUTE = "CollectionCount";

private ObjectName YGC_COLLECTOR;
private ObjectName FGC_COLLECTOR;
private Map<String, String> pollNameMapping;
private static final DecimalFormat DF = new DecimalFormat("0.00");

private MBeanServerConnection mbsc;

private ObjectName ygcCollector;
private ObjectName fgcCollector;
private ObjectName eden;
private ObjectName old;
private ObjectName sur;
private ObjectName perm;
private ObjectName ccs;

public GCutilExpression(MBeanServerConnection mbsc) throws Exception {
this.mbsc = mbsc;
mappingCollctors();
Expand All @@ -46,94 +40,94 @@ private void mappingCollctors() throws Exception {
Set<ObjectInstance> beans = mbsc.queryMBeans(Client.getObjectName(GARBAGE_COLLECTORS), null);

for (ObjectInstance collector : beans) {
ObjectName collectorName = collector.getObjectName();
String gcName = (String) getAttribute(collectorName, "Name");
if ("Copy".equals(gcName) || "PS Scavenge".equals(gcName) || "ParNew".equals(gcName)
|| "G1 Young Generation".equals(gcName)) {
YGC_COLLECTOR = collectorName;
} else if ("MarkSweepCompact".equals(gcName) || "PS MarkSweep".equals(gcName)
|| "ConcurrentMarkSweep".equals(gcName) || "G1 Old Generation".equals(gcName)) {
FGC_COLLECTOR = collectorName;
ObjectName collectorObjName = collector.getObjectName();
String collectorName = getAttribute(collectorObjName, "Name");
if ("Copy".equals(collectorName) || "PS Scavenge".equals(collectorName) || "ParNew".equals(collectorName)
|| "G1 Young Generation".equals(collectorName)) {
ygcCollector = collectorObjName;
} else if ("MarkSweepCompact".equals(collectorName) || "PS MarkSweep".equals(collectorName)
|| "ConcurrentMarkSweep".equals(collectorName) || "G1 Old Generation".equals(collectorName)) {
fgcCollector = collectorObjName;
} else {
YGC_COLLECTOR = collectorName;
ygcCollector = collectorObjName;
}
}
}

private void mappingPools() throws Exception {
// full gc的collector,下属的memoryPool包括所有需要GC的Pool(Code Reserve等则不在此列)
Set<ObjectInstance> beans = mbsc.queryMBeans(Client.getObjectName(MEM_POOL_PREFIX + "*"), null);
pollNameMapping = new HashMap<String, String>();
for (ObjectInstance collector : beans) {
ObjectName collectorName = collector.getObjectName();
String name = (String) getAttribute(collectorName, "Name");
name = name.trim();
String lowerCaseName = name.toLowerCase();
if (lowerCaseName.contains(SURVIVOR)) {
pollNameMapping.put(SURVIVOR, name);
} else if (lowerCaseName.contains(EDEN)) {
pollNameMapping.put(EDEN, name);
} else if (lowerCaseName.contains(OLD) || lowerCaseName.contains(TENURED)) {
pollNameMapping.put(OLD, name);
} else if (lowerCaseName.contains(PERM) || lowerCaseName.contains(METASPACE)) {
pollNameMapping.put(PERM, name);
} else if (lowerCaseName.contains(COMPRESSED_CLASS_SPACE)) {
pollNameMapping.put(COMPRESSED_CLASS_SPACE, name);
for (ObjectInstance pool : beans) {
ObjectName poolObjName = pool.getObjectName();
String poolName = getAttribute(poolObjName, "Name");
poolName = poolName.trim().toLowerCase();
if (poolName.contains("eden")) {
eden = poolObjName;
} else if (poolName.contains("survivor")) {
sur = poolObjName;
} else if (poolName.contains("old") || poolName.contains("tenured")) {
old = poolObjName;
} else if (poolName.contains("perm") || poolName.contains("metaspace")) {
perm = poolObjName;
} else if (poolName.contains("compressed class space")) {
ccs = poolObjName;
}
}
}

public String getE() throws Exception {
String poolName = MEM_POOL_PREFIX + pollNameMapping.get(EDEN);
return usedPercentage(poolName);
return usedPercentage(eden);
}

public String getS() throws Exception {
String poolName = MEM_POOL_PREFIX + pollNameMapping.get(SURVIVOR);
return usedPercentage(poolName);
return usedPercentage(sur);
}

public String getO() throws Exception {
String poolName = MEM_POOL_PREFIX + pollNameMapping.get(OLD);
return usedPercentage(poolName);
return usedPercentage(old);
}

public String getP() throws Exception {
String poolName = MEM_POOL_PREFIX + pollNameMapping.get(PERM);
return usedPercentage(poolName);
return usedPercentage(perm);
}

public String getCCS() throws Exception {
String poolName = MEM_POOL_PREFIX + pollNameMapping.get(COMPRESSED_CLASS_SPACE);
return usedPercentage(poolName);
return usedPercentage(ccs);
}

public Object getYGC() throws Exception {
return getAttribute(YGC_COLLECTOR, COLLECTION_COUNT_ATTRIBUTE);
return getAttribute(ygcCollector, COLLECTION_COUNT_ATTRIBUTE);
}

public Double getYGCT() throws Exception {
return Double.parseDouble(getAttribute(YGC_COLLECTOR, COLLECTION_TIME_ATTRIBUTE).toString()) / 1000;
return Double.parseDouble(getAttribute(ygcCollector, COLLECTION_TIME_ATTRIBUTE).toString()) / 1000;
}

public Object getFGC() throws Exception {
return getAttribute(FGC_COLLECTOR, COLLECTION_COUNT_ATTRIBUTE);
return getAttribute(fgcCollector, COLLECTION_COUNT_ATTRIBUTE);
}

public Double getFGCT() throws Exception {
return Double.parseDouble(getAttribute(FGC_COLLECTOR, COLLECTION_TIME_ATTRIBUTE).toString()) / 1000;
return Double.parseDouble(getAttribute(fgcCollector, COLLECTION_TIME_ATTRIBUTE).toString()) / 1000;
}

public Object getGCT() throws Exception {
return getFGCT() + getYGCT();
}

private Object getAttribute(ObjectName beanName, String attributeName) throws Exception {
return mbsc.getAttribute(beanName, attributeName);
private <T> T getAttribute(ObjectName beanName, String attributeName) throws Exception {
if (beanName != null) {
return (T) mbsc.getAttribute(beanName, attributeName);
} else {
return null;
}
}

private String usedPercentage(String poolName) throws Exception {
CompositeDataSupport usage = (CompositeDataSupport) mbsc.getAttribute(Client.getObjectName(poolName), "Usage");
private String usedPercentage(ObjectName poolObjectName) throws Exception {
if (poolObjectName == null) {
return DF.format(0.0d);
}

CompositeDataSupport usage = getAttribute(poolObjectName, "Usage");
double max = Double.parseDouble(usage.get("max").toString());

// 如果Max没有设置或GC算法原因没有max,则以committed为准
Expand Down

0 comments on commit e4de802

Please sign in to comment.