文章字数:794,阅读全文大约需要3分钟
OSHI可以跨平台查看服务器信息,其中cpu负载信息为当前占用CPU的时间。需要在一段时间内获取两次,然后相减得出这段时间内所占用的时间。这段时间除以总占用时间就是占用百分比。
环境java1.8
使用
- 依赖
1 2 3 4 5
| <dependency> <groupId>com.github.oshi</groupId> <artifactId>oshi-core</artifactId> <version>3.12.2</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| 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;
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 { SystemInfo systemInfo = new SystemInfo(); CentralProcessor processor = systemInfo.getHardware().getProcessor(); long[] prevTicks = processor.getSystemCpuLoadTicks(); 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(); long jvmTotalMemoryByte = runtime.totalMemory(); long jvmMaxMoryByte = runtime.maxMemory(); long freeMemoryByte = runtime.freeMemory(); String jdkVersion = props.getProperty("java.version"); 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); }
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() + " 线程状态:" + 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); } }
|
结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| # 没添加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 线程状态:WAITING 线程数量:5 线程id:3 线程名称:Finalizer 线程状态:WAITING 线程数量:5 线程id:4 线程名称:Signal Dispatcher 线程状态:RUNNABLE 线程数量:5 线程id:5 线程名称:Attach Listener 线程状态:RUNNABLE 线程数量:5 线程id:1 线程名称:main 线程状态:RUNNABLE ----------------操作系统信息---------------- 操作系统名 = Windows 7 系统架构 = amd64 ----------------jvm信息---------------- jvm内存总量 = 123MB jvm已使用内存 = 20.46MB jvm剩余内存 = 102.54MB jvm内存使用率 = 16.64% java版本 = 1.8.0_65
|