use of com.shulie.instrument.simulator.module.model.memory.MemoryEntry in project LinkAgent by shulieTech.
the class MemoryModule method getBufferPoolMemoryInfo.
private static List<MemoryEntry> getBufferPoolMemoryInfo() {
try {
List<MemoryEntry> bufferPoolMemEntries = new ArrayList<MemoryEntry>();
@SuppressWarnings("rawtypes") Class bufferPoolMXBeanClass = Class.forName("java.lang.management.BufferPoolMXBean");
@SuppressWarnings("unchecked") List<BufferPoolMXBean> bufferPoolMXBeans = ManagementFactory.getPlatformMXBeans(bufferPoolMXBeanClass);
for (BufferPoolMXBean mbean : bufferPoolMXBeans) {
long used = mbean.getMemoryUsed();
long total = mbean.getTotalCapacity();
bufferPoolMemEntries.add(new MemoryEntry(mbean.getName(), -1L, used, total, -1L));
}
return bufferPoolMemEntries;
} catch (Throwable e) {
// ignore
}
return Collections.EMPTY_LIST;
}
use of com.shulie.instrument.simulator.module.model.memory.MemoryEntry in project LinkAgent by shulieTech.
the class MemoryModule method info.
@Command(value = "info", description = "内存信息")
public CommandResponse info(final Map<String, String> args) {
try {
List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
MemoryInfo memoryInfo = new MemoryInfo();
// heap
MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
memoryInfo.setHeapMemory(createMemoryEntry(TYPE_HEAP, heapMemoryUsage));
List<MemoryEntry> heapMemEntries = new ArrayList<MemoryEntry>();
for (MemoryPoolMXBean poolMXBean : memoryPoolMXBeans) {
if (MemoryType.HEAP.equals(poolMXBean.getType())) {
MemoryUsage usage = poolMXBean.getUsage();
heapMemEntries.add(createMemoryEntry(poolMXBean.getName(), usage));
}
}
memoryInfo.setHeapMemories(heapMemEntries);
// non-heap
MemoryUsage nonHeapMemoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
List<MemoryEntry> nonheapMemEntries = new ArrayList<MemoryEntry>();
memoryInfo.setNonheapMemory(createMemoryEntry(TYPE_NON_HEAP, nonHeapMemoryUsage));
for (MemoryPoolMXBean poolMXBean : memoryPoolMXBeans) {
if (MemoryType.NON_HEAP.equals(poolMXBean.getType())) {
MemoryUsage usage = poolMXBean.getUsage();
nonheapMemEntries.add(createMemoryEntry(poolMXBean.getName(), usage));
}
}
memoryInfo.setNonheapMemories(nonheapMemEntries);
memoryInfo.setBufferPoolMemories(getBufferPoolMemoryInfo());
return CommandResponse.success(memoryInfo);
} catch (Throwable e) {
return CommandResponse.failure(e);
}
}
use of com.shulie.instrument.simulator.module.model.memory.MemoryEntry in project LinkAgent by shulieTech.
the class PerfResponseBuilder method build.
public static PerfResponse build(List<ThreadInfo> threadInfoList, GcInfo gcInfo, MemoryInfo memoryInfo) {
PerfResponse response = new PerfResponse();
response.setAgentId(Pradar.AGENT_ID_NOT_CONTAIN_USER_INFO);
response.setAppIp(PradarCoreUtils.getLocalAddress());
response.setAppName(AppNameUtils.appName());
response.setProcessId(RuntimeUtils.getPid());
response.setTimestamp(System.currentTimeMillis());
if (gcInfo != null) {
response.setFullGcCost(gcInfo.getOldGcTime());
response.setFullGcCount(gcInfo.getOldGcCount());
response.setYoungGcCost(gcInfo.getYoungGcTime());
response.setYoungGcCount(gcInfo.getYoungGcCount());
}
if (memoryInfo != null) {
response.setTotalMemory(memoryInfo.getHeapMemory().getTotal() + memoryInfo.getNonheapMemory().getTotal());
long youngMemoryUsed = 0L;
long youngMemoryTotal = 0L;
long oldMemoryUsed = 0L;
long oldMemoryTotal = 0L;
// TODO 目前这块兼容的是 jdk8及以上版本,还需要兼容 jdk8以下版本和其他版本的 jdk
for (MemoryEntry memoryEntry : memoryInfo.getHeapMemories()) {
String poolName = beautifyName(memoryEntry.getName());
if (poolName.equals("ps_eden_space") || poolName.equals("ps_survivor_space") || poolName.equals("eden_space") || poolName.equals("survivor_space") || poolName.equals("g1_eden_space") || poolName.equals("g1_survivor_space") || poolName.equals("par_eden_space") || poolName.equals("par_survivor_space")) {
youngMemoryUsed = youngMemoryUsed + memoryEntry.getUsed();
youngMemoryTotal = youngMemoryTotal + memoryEntry.getMax();
} else if (poolName.equals("ps_old_gen") || poolName.equals("tenured_gen") || poolName.equals("g1_old_gen") || poolName.equals("cms_old_gen")) {
oldMemoryUsed = oldMemoryUsed + memoryEntry.getUsed();
oldMemoryTotal = oldMemoryTotal + memoryEntry.getMax();
} else {
LOGGER.warn("unknown poolname is {}", poolName);
}
}
response.setYoungMemory(youngMemoryUsed);
response.setTotalYoung(youngMemoryTotal);
response.setOldMemory(oldMemoryUsed);
response.setTotalOld(oldMemoryTotal);
response.setTotalNonHeapMemory(memoryInfo.getNonheapMemory() == null ? 0L : memoryInfo.getNonheapMemory().getTotal());
long totalBufferPoolMemory = 0L;
for (MemoryEntry entry : memoryInfo.getBufferPoolMemories()) {
totalBufferPoolMemory += entry.getTotal();
}
response.setTotalBufferPoolMemory(totalBufferPoolMemory);
long permMemoryUsed = 0L;
long permMemoryTotal = 0L;
for (MemoryEntry memoryEntry : memoryInfo.getNonheapMemories()) {
String poolName = beautifyName(memoryEntry.getName());
// TODO 目前这块兼容的是 jdk8及以上版本,还需要兼容 jdk8以下版本和其他版本的 jdk
if (StringUtils.equalsIgnoreCase(poolName, "metaspace")) {
permMemoryTotal = memoryEntry.getMax();
permMemoryUsed = memoryEntry.getUsed();
}
}
response.setPermMemory(permMemoryUsed);
response.setTotalPerm(permMemoryTotal);
response.setHeapMemory(memoryInfo.getHeapMemory());
response.setHeapMemories(memoryInfo.getHeapMemories());
response.setNonheapMemory(memoryInfo.getNonheapMemory());
response.setNonheapMemories(memoryInfo.getNonheapMemories());
response.setBufferPoolMemories(memoryInfo.getBufferPoolMemories());
}
if (CollectionUtils.isNotEmpty(threadInfoList)) {
List<ThreadVO> threadVOS = new ArrayList<ThreadVO>(threadInfoList.size());
for (ThreadInfo threadInfo : threadInfoList) {
ThreadVO threadVO = new ThreadVO();
threadVO.setThreadId(threadInfo.getId());
threadVO.setThreadName(threadInfo.getName());
threadVO.setGroupName(threadInfo.getGroupName());
threadVO.setThreadCpuUsage(threadInfo.getCpuUsage());
threadVO.setCpuTime(threadInfo.getCpuTime());
threadVO.setInterrupted(threadInfo.isInterrupted());
threadVO.setThreadStatus(threadInfo.getState());
threadVO.setLockName(threadInfo.getLockName());
threadVO.setLockOwnerName(threadInfo.getLockOwnerName());
threadVO.setLockOwnerId(threadInfo.getLockOwnerId());
threadVO.setSuspended(threadInfo.isSuspended());
threadVO.setInNative(threadInfo.isInNative());
threadVO.setDaemon(threadInfo.isDaemon());
threadVO.setPriority(threadInfo.getPriority());
threadVO.setBlockedTime(threadInfo.getBlockedTime());
threadVO.setBlockedCount(threadInfo.getBlockedCount());
threadVO.setWaitedCount(threadInfo.getWaitedCount());
threadVO.setWaitedTime(threadInfo.getWaitedTime());
threadVO.setThreadStack(getThreadStackInfo(threadInfo.getThreadStacks()));
threadVO.setLockIdentityHashCode(threadInfo.getLockIdentityHashCode());
threadVO.setBlockingThreadCount(threadInfo.getBlockingThreadCount());
threadVO.setTraceId(threadInfo.getTraceId());
threadVO.setRpcId(threadInfo.getRpcId());
threadVO.setClusterTest(threadInfo.isClusterTest());
threadVOS.add(threadVO);
}
response.setThreadDataList(threadVOS);
}
return response;
}
Aggregations