org.voovan.tools.TPerformance Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of voovan-framework Show documentation
Show all versions of voovan-framework Show documentation
Voovan is a java framwork and it not depends on any third-party framework.
package org.voovan.tools;
import org.voovan.Global;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 系统性能相关
*
* @author helyho
*
* Voovan Framework.
* WebSite: https://github.com/helyho/Voovan
* Licence: Apache v2 License
*/
public class TPerformance {
private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
private static List LOCAL_IP_ADDRESSES = new ArrayList();
/**
* 内存信息类型枚举
*/
public enum MEMTYPE{
NOHEAP_INIT,
HEAP_INIT,
NOHEAP_MAX,
HEAP_MAX,
NOHEAP_USAGE,
HEAP_USAGE,
NOHEAP_COMMIT,
HEAP_COMMIT
}
/**
* 获取当前系统的负载情况
* @return 系统的负载情况
*/
public static double getSystemLoadAverage(){
return osmxb.getSystemLoadAverage();
}
/**
* 获取当前系统 CPU 数
* @return 系统 CPU 数
*/
public static int getProcessorCount(){
return osmxb.getAvailableProcessors();
}
public static List getLocalIpAddrs() {
if (LOCAL_IP_ADDRESSES.size() == 0) {
try {
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
if(ni.getInetAddresses().hasMoreElements()) {
ip = (InetAddress) ni.getInetAddresses().nextElement();
LOCAL_IP_ADDRESSES.add(ip.getHostAddress());
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
return LOCAL_IP_ADDRESSES;
}
/**
* 获取系统单 CPU 核心的平均负载
* @return 单 CPU 核心的平均负载
*/
public static double cpuPerCoreLoadAvg(){
double perCoreLoadAvg = osmxb.getSystemLoadAverage()/osmxb.getAvailableProcessors();
BigDecimal bg = new BigDecimal(perCoreLoadAvg);
perCoreLoadAvg = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return perCoreLoadAvg;
}
/**
* JVM 虚拟机的内存使用情况
* @return 内存使用情况
* */
public static double getJVMMemoryUsage(){
//maxMemory()这个方法返回的是java虚拟机(这个进程)能构从操作系统那里挖到的最大的内存,以字节为单位, -Xmx参数
//totalMemory()这个方法返回的是java虚拟机现在已经从操作系统那里挖过来的内存大小
//freeMemory() 当前申请到的内存有多少没有使用的空闲内存
Runtime runtime = Runtime.getRuntime();
double memoryUsage = 1-((double)runtime.freeMemory()+(runtime.maxMemory()-runtime.totalMemory()))/(double)runtime.maxMemory();
BigDecimal bg = new BigDecimal(memoryUsage);
memoryUsage = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return memoryUsage;
}
/**
* 获取部分内存信息(栈,非栈)
* @param memType 获取的信息类型
* @return 当前内存数值
*/
public static long getJVMMemoryInfo(MEMTYPE memType){
if(memType==MEMTYPE.NOHEAP_INIT){
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getInit();
} else if(memType==MEMTYPE.NOHEAP_MAX){
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getMax();
} else if(memType==MEMTYPE.NOHEAP_USAGE){
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getUsed();
} else if(memType== MEMTYPE.NOHEAP_COMMIT){
return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().getCommitted();
} else if(memType==MEMTYPE.HEAP_INIT){
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getInit();
} else if(memType==MEMTYPE.HEAP_MAX){
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax();
} else if(memType==MEMTYPE.HEAP_USAGE){
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
} else if(memType==MEMTYPE.HEAP_COMMIT){
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getCommitted();
}else{
throw new RuntimeException("getMemoryInfo function arg error!");
}
}
/**
* 获取当前内存信息
* @return 内存信息描述对象
*/
public static MemoryInfo getJVMMemoryInfo(){
MemoryInfo memoryInfo = new MemoryInfo();
memoryInfo.setHeapInit(getJVMMemoryInfo(MEMTYPE.HEAP_INIT));
memoryInfo.setHeapUsage(getJVMMemoryInfo(MEMTYPE.HEAP_USAGE));
memoryInfo.setHeapCommit(getJVMMemoryInfo(MEMTYPE.HEAP_COMMIT));
memoryInfo.setHeapMax(getJVMMemoryInfo(MEMTYPE.HEAP_MAX));
memoryInfo.setNoHeapInit(getJVMMemoryInfo(MEMTYPE.NOHEAP_INIT));
memoryInfo.setNoHeapUsage(getJVMMemoryInfo(MEMTYPE.NOHEAP_USAGE));
memoryInfo.setNoHeapCommit(getJVMMemoryInfo(MEMTYPE.NOHEAP_COMMIT));
memoryInfo.setNoHeapMax(getJVMMemoryInfo(MEMTYPE.NOHEAP_MAX));
memoryInfo.setFree(Runtime.getRuntime().freeMemory());
memoryInfo.setTotal(Runtime.getRuntime().totalMemory());
memoryInfo.setMax(Runtime.getRuntime().maxMemory());
return memoryInfo;
}
/**
* 获取指定进程的 GC 信息
* @param pid 进程 Id
* @return GC信息
* @throws IOException IO 异常
*/
public static Map getJVMGCInfo(long pid) throws IOException {
LinkedHashMap result = new LinkedHashMap();
InputStream processInputStream = TEnv.createSysProcess("jstat -gcutil "+pid, null, (File)null).getInputStream();
String console = new String(TStream.readAll(processInputStream));
String[] consoleLines = console.split(System.lineSeparator());
String[] titleLine = consoleLines[0].trim().split("\\s+");
String[] dataLine = consoleLines[1].trim().split("\\s+");
for(int i=0; i getJVMGCInfo() throws IOException {
return getJVMGCInfo(TEnv.getCurrentPID());
}
/**
* 获取指定进程的对象信息
* @param pid 进程 Id
* @param regex 对象匹配字符串
* @param headCount 返回的对象数
* @return 虚拟机中的对象信息
* @throws IOException IO 异常
*/
public static Map getJVMObjectInfo(long pid, String regex, Integer headCount) throws IOException {
LinkedHashMap result = new LinkedHashMap();
InputStream processInputStream = TEnv.createSysProcess("jmap -histo "+pid, null, (File)null).getInputStream();
String console = new String(TStream.readAll(processInputStream));
String[] consoleLines = console.split(System.lineSeparator());
if(headCount<0){
headCount = consoleLines.length;
}else{
headCount=headCount+3;
}
for(int lineCount = 3;lineCount 0) {
ObjectInfo objectInfo = new ObjectInfo(name, size, count);
result.put(name,objectInfo);
}
}
return result;
}
/**
* 获取当前JVM加载的对象信息(数量,所占内存大小)
* @param regex 正则表达式
* @param headCount 头部记录数
* @return 系统对象信息的Map
*/
public static Map getJVMObjectInfo(String regex, int headCount) {
if(regex==null){
regex = ".*";
}
Map result;
try {
result = TPerformance.getJVMObjectInfo(TEnv.getCurrentPID(), regex, headCount);
} catch (IOException e) {
result = new Hashtable();
}
return result;
}
/**
* 获取当前 JVM 线程信息描述
* @return 线程信息信息集合
*/
public static Map getThreadPoolInfo(){
Map threadPoolInfo = new HashMap();
ThreadPoolExecutor threadPoolInstance = Global.getThreadPool();
threadPoolInfo.put("ActiveCount",threadPoolInstance.getActiveCount());
threadPoolInfo.put("CorePoolSize",threadPoolInstance.getCorePoolSize());
threadPoolInfo.put("FinishedTaskCount",threadPoolInstance.getCompletedTaskCount());
threadPoolInfo.put("TaskCount",threadPoolInstance.getTaskCount());
threadPoolInfo.put("QueueSize",threadPoolInstance.getQueue().size());
return threadPoolInfo;
}
/**
* 获取当前 JVM 线程信息描述
* @param state 线程状态, nul,返回所有状态的线程
* @param withStack 是否包含堆栈信息
* @return 线程信息集合
*/
public static List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy