【JAVA之获取CPU/内存信息】
代码
import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import oshi.SystemInfo; import oshi.hardware.CentralProcessor; import oshi.hardware.CentralProcessor.TickType; import oshi.hardware.GlobalMemory; import oshi.hardware.HardwareAbstractionLayer; import oshi.util.FormatUtil; import oshi.util.Util; import java.util.Arrays; /** * os information test */ public class OshiTest { private static Logger logger = LoggerFactory.getLogger(OshiTest.class); @Test public void test() { SystemInfo si = new SystemInfo(); HardwareAbstractionLayer hal = si.getHardware(); logger.info("Checking Memory..."); printMemory(hal.getMemory()); logger.info("Checking CPU..."); printCpu(hal.getProcessor()); } private static void printMemory(GlobalMemory memory) { logger.info("memory avail:{} MB" , memory.getAvailable() / 1024 / 1024 );//memory avail:6863 MB logger.info("memory total:{} MB" , memory.getTotal() / 1024 / 1024 );//memory total:16384 MB } private static void printCpu(CentralProcessor processor) { logger.info(String.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoad() * 100));//CPU load: 24.9% (OS MXBean) logger.info("CPU load averages : {}", processor.getSystemLoadAverage());//CPU load averages : 1.5234375 logger.info("Uptime: " + FormatUtil.formatElapsedSecs(processor.getSystemUptime())); logger.info("Context Switches/Interrupts: " + processor.getContextSwitches() + " / " + processor.getInterrupts()); long[] prevTicks = processor.getSystemCpuLoadTicks(); logger.info("CPU, IOWait, and IRQ ticks @ 0 sec:" + Arrays.toString(prevTicks)); //Wait a second... Util.sleep(1000); long[] ticks = processor.getSystemCpuLoadTicks(); logger.info("CPU, IOWait, and IRQ ticks @ 1 sec:" + Arrays.toString(ticks)); long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()]; long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()]; long sys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()]; long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()]; long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()]; long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()]; long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()]; long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()]; long totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal; logger.info(String.format( "User: %.1f%% Nice: %.1f%% System: %.1f%% Idle: %.1f%% IOwait: %.1f%% IRQ: %.1f%% SoftIRQ: %.1f%% Steal: %.1f%%%n", 100d * user / totalCpu, 100d * nice / totalCpu, 100d * sys / totalCpu, 100d * idle / totalCpu, 100d * iowait / totalCpu, 100d * irq / totalCpu, 100d * softirq / totalCpu, 100d * steal / totalCpu)); logger.info(String.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks() * 100)); double[] loadAverage = processor.getSystemLoadAverage(3); logger.info("CPU load averages:" + (loadAverage[0] < 0 ? " N/A" : String.format(" %.2f", loadAverage[0])) + (loadAverage[1] < 0 ? " N/A" : String.format(" %.2f", loadAverage[1])) + (loadAverage[2] < 0 ? " N/A" : String.format(" %.2f", loadAverage[2]))); // per core CPU StringBuilder procCpu = new StringBuilder("CPU load per processor:"); double[] load = processor.getProcessorCpuLoadBetweenTicks(); for (double avg : load) { procCpu.append(String.format(" %.1f%%", avg * 100)); } logger.info(procCpu.toString()); } }
结果
16:03:08.573 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - Checking Memory...
16:03:10.592 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - memory avail:155 MB
16:03:10.594 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - memory total:4095 MB
16:03:10.594 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - Checking CPU...
16:03:10.772 [main] DEBUG oshi.hardware.common.AbstractCentralProcessor - Oracle MXBean detected.
16:03:12.319 [main] DEBUG oshi.hardware.platform.windows.WindowsCentralProcessor - Initialized Processor
16:03:12.322 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - CPU load: 41.0% (OS MXBean)
16:03:12.322 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - CPU load averages : -1.0
16:03:12.326 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - Uptime: 1 days, 20:39:54
16:03:12.328 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - Context Switches/Interrupts: 1714488866 / 570600239
16:03:12.330 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - CPU, IOWait, and IRQ ticks @ 0 sec:[37287359, 0, 19608289, 234918099, 0, 520263, 600050, 0]
16:03:13.332 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - CPU, IOWait, and IRQ ticks @ 1 sec:[37287421, 0, 19608313, 234920002, 0, 520263, 600057, 0]
16:03:13.333 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - User: 3.1% Nice: 0.0% System: 1.2% Idle: 95.3% IOwait: 0.0% IRQ: 0.0% SoftIRQ: 0.4% Steal: 0.0%
16:03:13.333 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - CPU load: 19.8% (counting ticks)
16:03:13.333 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - CPU load averages: N/A N/A N/A
16:03:13.337 [main] INFO org.apache.dolphinscheduler.common.os.OshiTest - CPU load per processor: 21.3% 18.3%
上一篇: PropertyUtils 文件解读
下一篇: Telegraf介绍
推荐阅读
-
bootstrap table之通用方法( 时间控件,导出,动态下拉框, 表单验证 ,选中与获取信息)代码分享
-
使用python获取CPU和内存信息的思路与实现(linux系统)
-
Python数据分析之获取双色球历史信息的方法示例
-
linux命令大全之ss命令详解(获取socket统计信息)
-
Java/Android 大文件获取SHA1防止内存溢出
-
C++/Qt/QtQuick高级应用系列(4)_获取CPU/内存使用率
-
lunix脚本进程挂掉时显示cpu和内存信息及挂掉的时间
-
详解Linux中监控CPU与虚拟内存运行信息的命令用法
-
SqlServer如何通过SQL语句获取处理器(CPU)、内存(Memory)、磁盘(Disk)以及操作系统相关信息
-
Python获取android设备cpu和内存占用情况