java?oshi如何查看cpu信息
oshi查看cpu信息
OSHI可以跨平臺查看服務器信息,其中cpu負載信息為當前占用CPU的時間。需要在一段時間內獲取兩次,然后相減得出這段時間內所占用的時間。這段時間除以總占用時間就是占用百分比。
pom引入依賴
<dependency> ? ? ? <groupId>com.github.oshi</groupId> ? ? ? <artifactId>oshi-core</artifactId> ? ? ? <version>3.12.2</version> </dependency>
測試類
package io.greatcolin.jvmMessage; import oshi.SystemInfo; import oshi.hardware.CentralProcessor; import oshi.hardware.GlobalMemory; import java.text.DecimalFormat; import java.util.Properties; import java.util.concurrent.TimeUnit; /** * @author colin.cheng * @version V1.0 * @date Created In 16:04 2019/8/16 */ public class OshiTest { public static void main(String[] args) { while (true){ try { OshiTest.printlnCpuInfo(); OshiTest.MemInfo(); OshiTest.getThread(); OshiTest.setSysInfo(); OshiTest.setJvmInfo(); TimeUnit.SECONDS.sleep(5); }catch (Exception e){ e.printStackTrace(); } } } private static void printlnCpuInfo() throws InterruptedException { //System.out.println("----------------cpu信息----------------"); SystemInfo systemInfo = new SystemInfo(); CentralProcessor processor = systemInfo.getHardware().getProcessor(); long[] prevTicks = processor.getSystemCpuLoadTicks(); // 睡眠1s TimeUnit.SECONDS.sleep(1); long[] ticks = processor.getSystemCpuLoadTicks(); long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()]; long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()]; long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()]; long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()]; long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()]; long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()]; long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()]; long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()]; long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal; System.out.println("----------------cpu信息----------------"); System.out.println("cpu核數:" + processor.getLogicalProcessorCount()); System.out.println("cpu系統使用率:" + new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu)); System.out.println("cpu用戶使用率:" + new DecimalFormat("#.##%").format(user * 1.0 / totalCpu)); System.out.println("cpu當前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu)); System.out.println("cpu當前使用率:" + new DecimalFormat("#.##%").format(1.0-(idle * 1.0 / totalCpu))); } public static void MemInfo(){ System.out.println("----------------主機內存信息----------------"); SystemInfo systemInfo = new SystemInfo(); GlobalMemory memory = systemInfo.getHardware().getMemory(); //總內存 long totalByte = memory.getTotal(); //剩余 long acaliableByte = memory.getAvailable(); System.out.println("總內存 = " + formatByte(totalByte)); System.out.println("使用" + formatByte(totalByte-acaliableByte)); System.out.println("剩余內存 = " + formatByte(acaliableByte)); System.out.println("使用率:" + new DecimalFormat("#.##%").format((totalByte-acaliableByte)*1.0/totalByte)); } public static void setSysInfo(){ System.out.println("----------------操作系統信息----------------"); Properties props = System.getProperties(); //系統名稱 String osName = props.getProperty("os.name"); //架構名稱 String osArch = props.getProperty("os.arch"); System.out.println("操作系統名 = " + osName); System.out.println("系統架構 = " + osArch); } public static void setJvmInfo(){ System.out.println("----------------jvm信息----------------"); Properties props = System.getProperties(); Runtime runtime = Runtime.getRuntime(); //jvm總內存 long jvmTotalMemoryByte = runtime.totalMemory(); //jvm最大可申請 long jvmMaxMoryByte = runtime.maxMemory(); //空閑空間 long freeMemoryByte = runtime.freeMemory(); //jdk版本 String jdkVersion = props.getProperty("java.version"); //jdk路徑 String jdkHome = props.getProperty("java.home"); System.out.println("jvm內存總量 = " + formatByte(jvmTotalMemoryByte)); System.out.println("jvm已使用內存 = " + formatByte(jvmTotalMemoryByte-freeMemoryByte)); System.out.println("jvm剩余內存 = " + formatByte(freeMemoryByte)); System.out.println("jvm內存使用率 = " + new DecimalFormat("#.##%").format((jvmTotalMemoryByte-freeMemoryByte)*1.0/jvmTotalMemoryByte)); System.out.println("java版本 = " + jdkVersion); //System.out.println("jdkHome = " + jdkHome); } public static void getThread(){ System.out.println("----------------線程信息----------------"); ThreadGroup currentGroup =Thread.currentThread().getThreadGroup(); while (currentGroup.getParent()!=null){ // 返回此線程組的父線程組 currentGroup=currentGroup.getParent(); } //此線程組中活動線程的估計數 int noThreads = currentGroup.activeCount(); Thread[] lstThreads = new Thread[noThreads]; //把對此線程組中的所有活動子組的引用復制到指定數組中。 currentGroup.enumerate(lstThreads); for (Thread thread : lstThreads) { System.out.println("線程數量:"+noThreads+" 線程id:" + thread.getId() + " 線程名稱:" + thread.getName() + " 線程狀態(tài):" + thread.getState()); } } public static String formatByte(long byteNumber){ //換算單位 double FORMAT = 1024.0; double kbNumber = byteNumber/FORMAT; if(kbNumber<FORMAT){ return new DecimalFormat("#.##KB").format(kbNumber); } double mbNumber = kbNumber/FORMAT; if(mbNumber<FORMAT){ return new DecimalFormat("#.##MB").format(mbNumber); } double gbNumber = mbNumber/FORMAT; if(gbNumber<FORMAT){ return new DecimalFormat("#.##GB").format(gbNumber); } double tbNumber = gbNumber/FORMAT; return new DecimalFormat("#.##TB").format(tbNumber); } }
輸出結果
# 沒添加slf4j的依賴,不影響
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
----------------cpu信息----------------
cpu核數:4
cpu系統使用率:1.88%
cpu用戶使用率:2.73%
cpu當前等待率:0%
cpu當前使用率:4.71%
----------------主機內存信息----------------
總內存 = 7.88GB
使用5.89GB
剩余內存 = 1.99GB
使用率:74.72%
----------------線程信息----------------
線程數量:5 線程id:2 線程名稱:Reference Handler 線程狀態(tài):WAITING
線程數量:5 線程id:3 線程名稱:Finalizer 線程狀態(tài):WAITING
線程數量:5 線程id:4 線程名稱:Signal Dispatcher 線程狀態(tài):RUNNABLE
線程數量:5 線程id:5 線程名稱:Attach Listener 線程狀態(tài):RUNNABLE
線程數量:5 線程id:1 線程名稱:main 線程狀態(tài):RUNNABLE
----------------操作系統信息----------------
操作系統名 = Windows 7
系統架構 = amd64
----------------jvm信息----------------
jvm內存總量 = 123MB
jvm已使用內存 = 20.46MB
jvm剩余內存 = 102.54MB
jvm內存使用率 = 16.64%
java版本 = 1.8.0_65
oshi獲取cpu/內存使用率前十的進程
CPU
@Override public void getFirstCpuUsed() { ? ? OperatingSystem windowsOperatingSystem = new WindowsOperatingSystem(); ? ? List<OSProcess> processList = windowsOperatingSystem.getProcesses(10, OperatingSystem.ProcessSort.CPU); ? ? for (OSProcess process : processList) { ? ? ? ? //進程名,進程ID,進程CPU使用率 ? ? ? ? System.out.println(String.format("name:%s PID: %d CPU:%.3f", ? ? ? ? ? ? ? ? process.getName(),process.getProcessID(), ? ? ? ? ? ? ? ? process.getProcessCpuLoadCumulative())); ? ? } }
內存
public void getFirstMemUsed(){ ? ? OperatingSystem windowsOperatingSystem = new WindowsOperatingSystem(); ? ? List<OSProcess> processList = windowsOperatingSystem.getProcesses(10, OperatingSystem.ProcessSort.MEMORY); ? ? for (OSProcess process : processList) { ? ? ? ? //進程名,京城ID,進程CPU使用率 ? ? ? ? System.out.println(String.format("name:%s PID: %d CPU:%.3f", ? ? ? ? ? ? ? ? process.getName(),process.getProcessID(), ? ? ? ? ? ? ? ? process.getProcessCpuLoadCumulative())); ? ? } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。