Skip to content

Commit

Permalink
core: add BufferPool metrice; reduce MyPerf4J-ASM.jar size; upgrade a…
Browse files Browse the repository at this point in the history
…sm version to 7.0; upgrade to 2.1.0
  • Loading branch information
LinShunKang committed Nov 4, 2018
1 parent 495dc3c commit 7f56fc1
Show file tree
Hide file tree
Showing 21 changed files with 434 additions and 199 deletions.
2 changes: 1 addition & 1 deletion MyPerf4J-ASM/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>MyPerf4J</artifactId>
<groupId>MyPerf4J</groupId>
<version>2.0.2</version>
<version>2.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion MyPerf4J-Base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>MyPerf4J</artifactId>
<groupId>MyPerf4J</groupId>
<version>2.0.2</version>
<version>2.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>MyPerf4J-Base</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ProfilingConfig {

private String memoryMetricsFile;

private String bufferPoolMetricsFile;

private String threadMetricsFile;

private String logRollingTimeUnit;
Expand Down Expand Up @@ -110,6 +112,14 @@ public void setMemoryMetricsFile(String memoryMetricsFile) {
this.memoryMetricsFile = memoryMetricsFile;
}

public String getBufferPoolMetricsFile() {
return bufferPoolMetricsFile;
}

public void setBufferPoolMetricsFile(String bufferPoolMetricsFile) {
this.bufferPoolMetricsFile = bufferPoolMetricsFile;
}

public String getThreadMetricsFile() {
return threadMetricsFile;
}
Expand Down Expand Up @@ -248,6 +258,7 @@ public String toString() {
", classMetricsFile='" + classMetricsFile + '\'' +
", gcMetricsFile='" + gcMetricsFile + '\'' +
", memoryMetricsFile='" + memoryMetricsFile + '\'' +
", bufferPoolMetricsFile='" + bufferPoolMetricsFile + '\'' +
", threadMetricsFile='" + threadMetricsFile + '\'' +
", recorderMode='" + recorderMode + '\'' +
", backupRecorderCount=" + backupRecorderCount +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface PropertyKeys {

String MEM_METRICS_FILE = "MemMetricsFile";

String BUF_POOL_METRICS_FILE = "BufPoolMetricsFile";

String THREAD_METRICS_FILE = "ThreadMetricsFile";

String LOG_ROLLING_TIME_TIME_UNIT = "LogRollingTimeUnit";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cn.myperf4j.base.metric;

import java.lang.management.BufferPoolMXBean;

/**
* Created by LinShunkang on 2018/11/1
*/
public class JvmBufferPoolMetrics extends Metrics {

private static final long serialVersionUID = 1308517280962399791L;

private String name;

private long count;

private long memoryUsed;

private long memoryCapacity;

public JvmBufferPoolMetrics(BufferPoolMXBean mxBean) {
this.name = mxBean.getName();
this.count = mxBean.getCount();
this.memoryUsed = mxBean.getMemoryUsed();
this.memoryCapacity = mxBean.getTotalCapacity();
}

public String getName() {
return name;
}

public long getCount() {
return count;
}

public long getMemoryUsed() {
return memoryUsed;
}

public long getMemoryCapacity() {
return memoryCapacity;
}

@Override
public String toString() {
return "JvmBufferPoolMetrics{" +
"name='" + name + '\'' +
", count=" + count +
", memoryUsed=" + memoryUsed +
", memoryCapacity=" + memoryCapacity +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cn.myperf4j.base.metric.formatter;

import cn.myperf4j.base.metric.JvmBufferPoolMetrics;

/**
* Created by LinShunkang on 2018/8/21
*/
public interface JvmBufferPoolMetricsFormatter extends MetricsFormatter<JvmBufferPoolMetrics> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.myperf4j.base.metric.formatter.impl;

import cn.myperf4j.base.metric.JvmBufferPoolMetrics;
import cn.myperf4j.base.metric.formatter.JvmBufferPoolMetricsFormatter;
import cn.myperf4j.base.util.DateFormatUtils;

import java.util.List;

/**
* Created by LinShunkang on 1919/8/21
*/
public class DefaultJvmBufferPoolMetricsFormatter implements JvmBufferPoolMetricsFormatter {

@Override
public String format(List<JvmBufferPoolMetrics> metricsList, long startMillis, long stopMillis) {
String dataTitleFormat = "%-19s%19s%19s%19s%n";
StringBuilder sb = new StringBuilder((metricsList.size() + 2) * (3 * 19 + 64));
sb.append("MyPerf4J JVM BufferPool Metrics [").append(DateFormatUtils.format(startMillis)).append(", ").append(DateFormatUtils.format(stopMillis)).append("]").append(String.format("%n"));
sb.append(String.format(dataTitleFormat, "Name", "Count", "MemoryUsed", "MemoryCapacity"));
if (metricsList.isEmpty()) {
return sb.toString();
}

String dataFormat = "%-19s%19d%19d%19d%n";
for (int i = 0; i < metricsList.size(); ++i) {
JvmBufferPoolMetrics metrics = metricsList.get(i);
sb.append(String.format(dataFormat,
metrics.getName(),
metrics.getCount(),
metrics.getMemoryUsed(),
metrics.getMemoryCapacity()));
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cn.myperf4j.base.metric.processor;

import cn.myperf4j.base.config.ProfilingConfig;
import cn.myperf4j.base.log.ILogger;
import cn.myperf4j.base.log.LoggerFactory;

/**
* Created by LinShunkang on 2018/8/25
*/
public abstract class AbstractJvmBufferPoolMetricsProcessor implements JvmBufferPoolMetricsProcessor {

protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.getInstance().getBufferPoolMetricsFile());

@Override
public void beforeProcess(long processId, long startMillis, long stopMillis) {
//empty
}

@Override
public void afterProcess(long processId, long startMillis, long stopMillis) {
logger.flushLog();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cn.myperf4j.base.metric.processor;

import cn.myperf4j.base.metric.JvmBufferPoolMetrics;

/**
* Created by LinShunkang on 2018/8/21
*/
public interface JvmBufferPoolMetricsProcessor extends MetricsProcessor<JvmBufferPoolMetrics> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public static JvmMemoryMetricsProcessor getMemoryMetricsProcessor(int processorT
}
}

public static JvmBufferPoolMetricsProcessor getBufferPoolMetricsProcessor(int processorType) {
switch (processorType) {
case PropertyValues.METRICS_PROCESS_TYPE_STDOUT:
case PropertyValues.METRICS_PROCESS_TYPE_LOGGER:
return new LoggerJvmBufferPoolMetricsProcessor();
case PropertyValues.METRICS_PROCESS_TYPE_INFLUX_DB:
return new InfluxDBJvmBufferPoolMetricsProcessor();
default:
return new LoggerJvmBufferPoolMetricsProcessor();
}
}

public static JvmThreadMetricsProcessor getThreadMetricsProcessor(int processorType) {
switch (processorType) {
case PropertyValues.METRICS_PROCESS_TYPE_STDOUT:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cn.myperf4j.base.metric.processor.influxdb;

import cn.myperf4j.base.config.ProfilingConfig;
import cn.myperf4j.base.metric.JvmBufferPoolMetrics;
import cn.myperf4j.base.metric.processor.AbstractJvmBufferPoolMetricsProcessor;

/**
* Created by LinShunkang on 2018/8/25
*/
public class InfluxDBJvmBufferPoolMetricsProcessor extends AbstractJvmBufferPoolMetricsProcessor {

private ThreadLocal<StringBuilder> sbThreadLocal = new ThreadLocal<StringBuilder>() {
@Override
protected StringBuilder initialValue() {
return new StringBuilder(128);
}
};

@Override
public void process(JvmBufferPoolMetrics metrics, long processId, long startMillis, long stopMillis) {
StringBuilder sb = sbThreadLocal.get();
try {
logger.log(createLineProtocol(metrics, startMillis * 1000 * 1000L, sb));
} finally {
sb.setLength(0);
}
}

private String createLineProtocol(JvmBufferPoolMetrics metrics, long startNanos, StringBuilder sb) {
sb.append("jvm_buffer_pool_metrics")
.append(",AppName=").append(ProfilingConfig.getInstance().getAppName())
.append(",PoolName=").append(metrics.getName())
.append(" Count=").append(metrics.getCount()).append("i")
.append(",MemoryUsed=").append(metrics.getMemoryUsed()).append("i")
.append(",MemoryCapacity=").append(metrics.getMemoryCapacity()).append("i")
.append(" ").append(startNanos);
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cn.myperf4j.base.metric.processor.log;

import cn.myperf4j.base.metric.JvmBufferPoolMetrics;
import cn.myperf4j.base.metric.formatter.JvmBufferPoolMetricsFormatter;
import cn.myperf4j.base.metric.formatter.impl.DefaultJvmBufferPoolMetricsFormatter;
import cn.myperf4j.base.metric.processor.AbstractJvmBufferPoolMetricsProcessor;
import cn.myperf4j.base.util.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by LinShunkang on 2018/8/25
*/
public class LoggerJvmBufferPoolMetricsProcessor extends AbstractJvmBufferPoolMetricsProcessor {

private ConcurrentHashMap<Long, List<JvmBufferPoolMetrics>> metricsMap = new ConcurrentHashMap<>(8);

private JvmBufferPoolMetricsFormatter metricsFormatter = new DefaultJvmBufferPoolMetricsFormatter();

@Override
public void beforeProcess(long processId, long startMillis, long stopMillis) {
metricsMap.put(processId, new ArrayList<JvmBufferPoolMetrics>(2));
}

@Override
public void process(JvmBufferPoolMetrics metrics, long processId, long startMillis, long stopMillis) {
List<JvmBufferPoolMetrics> metricsList = metricsMap.get(processId);
if (metricsList != null) {
metricsList.add(metrics);
} else {
Logger.error("LoggerJvmBufferPoolMetricsProcessor.process(" + processId + ", " + startMillis + ", " + stopMillis + "): metricsList is null!!!");
}
}

@Override
public void afterProcess(long processId, long startMillis, long stopMillis) {
List<JvmBufferPoolMetrics> metricsList = metricsMap.remove(processId);
if (metricsList != null) {
logger.logAndFlush(metricsFormatter.format(metricsList, startMillis, stopMillis));
} else {
Logger.error("LoggerJvmBufferPoolMetricsProcessor.afterProcess(" + processId + ", " + startMillis + ", " + stopMillis + "): metricsList is null!!!");
}
}
}
2 changes: 1 addition & 1 deletion MyPerf4J-Core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>MyPerf4J</artifactId>
<groupId>MyPerf4J</groupId>
<version>2.0.2</version>
<version>2.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ private boolean initProfilingConfig() {
config.setClassMetricsFile(PropertyValues.STDOUT_FILE);
config.setGcMetricsFile(PropertyValues.STDOUT_FILE);
config.setMemoryMetricsFile(PropertyValues.STDOUT_FILE);
config.setBufferPoolMetricsFile(PropertyValues.STDOUT_FILE);
} else {
config.setMethodMetricsFile(MyProperties.getStr(PropertyKeys.METHOD_METRICS_FILE, PropertyValues.DEFAULT_METRICS_FILE));
config.setClassMetricsFile(MyProperties.getStr(PropertyKeys.CLASS_METRICS_FILE, PropertyValues.NULL_FILE));
config.setGcMetricsFile(MyProperties.getStr(PropertyKeys.GC_METRICS_FILE, PropertyValues.NULL_FILE));
config.setMemoryMetricsFile(MyProperties.getStr(PropertyKeys.MEM_METRICS_FILE, PropertyValues.NULL_FILE));
config.setBufferPoolMetricsFile(MyProperties.getStr(PropertyKeys.BUF_POOL_METRICS_FILE, PropertyValues.NULL_FILE));
}
config.setThreadMetricsFile(MyProperties.getStr(PropertyKeys.THREAD_METRICS_FILE, PropertyValues.NULL_FILE));
config.setLogRollingTimeUnit(MyProperties.getStr(PropertyKeys.LOG_ROLLING_TIME_TIME_UNIT, PropertyValues.LOG_ROLLING_TIME_DAILY));
Expand Down Expand Up @@ -345,8 +347,9 @@ private Scheduler createJVMMetricsScheduler() {
JvmClassMetricsProcessor classProcessor = MetricsProcessorFactory.getClassMetricsProcessor(processorType);
JvmGCMetricsProcessor gcProcessor = MetricsProcessorFactory.getGCMetricsProcessor(processorType);
JvmMemoryMetricsProcessor memoryProcessor = MetricsProcessorFactory.getMemoryMetricsProcessor(processorType);
JvmBufferPoolMetricsProcessor bufferPoolProcessor = MetricsProcessorFactory.getBufferPoolMetricsProcessor(processorType);
JvmThreadMetricsProcessor threadProcessor = MetricsProcessorFactory.getThreadMetricsProcessor(processorType);
return new JvmMetricsScheduler(classProcessor, gcProcessor, memoryProcessor, threadProcessor);
return new JvmMetricsScheduler(classProcessor, gcProcessor, memoryProcessor, bufferPoolProcessor, threadProcessor);
}

public abstract boolean initOther();
Expand Down
Loading

0 comments on commit 7f56fc1

Please sign in to comment.