From a37d80e1ee7d6d457200ed8ab71937481adb9b32 Mon Sep 17 00:00:00 2001 From: xyli Date: Fri, 3 Mar 2023 13:57:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ecpu=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/TestCpu.java | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/test/java/TestCpu.java b/src/test/java/TestCpu.java index 1f168ba..e94e644 100644 --- a/src/test/java/TestCpu.java +++ b/src/test/java/TestCpu.java @@ -46,6 +46,62 @@ public class TestCpu { } } + private static long[] readCpu(final Process proc) { + long[] retn = new long[2]; + try { + proc.getOutputStream().close(); + InputStreamReader ir = new InputStreamReader(proc.getInputStream()); + LineNumberReader input = new LineNumberReader(ir); + String line = input.readLine(); + if (line == null || line.length() < 10) { + return null; + } + int capidx = line.indexOf("Caption"); + int cmdidx = line.indexOf("CommandLine"); + int rocidx = line.indexOf("ReadOperationCount"); + int umtidx = line.indexOf("UserModeTime"); + int kmtidx = line.indexOf("KernelModeTime"); + int wocidx = line.indexOf("WriteOperationCount"); + long idletime = 0; + long kneltime = 0; + long usertime = 0; + while ((line = input.readLine()) != null) { + if (line.length() < wocidx) { + continue; + } + String caption = substring(line, capidx, cmdidx - 1).trim(); + String cmd = substring(line, cmdidx, kmtidx - 1).trim(); + if (cmd.contains("wmic.exe")) { + continue; + } + String s1 = substring(line, kmtidx, rocidx - 1).trim(); + String s2 = substring(line, umtidx, wocidx - 1).trim(); + if (caption.equals("System Idle Process") || caption.equals("System")) { + if (s1.length() > 0) + idletime += Long.valueOf(s1); + if (s2.length() > 0) + idletime += Long.valueOf(s2); + continue; + } + if (s1.length() > 0) + kneltime += Long.valueOf(s1); + if (s2.length() > 0) + usertime += Long.valueOf(s2); + } + retn[0] = idletime; + retn[1] = kneltime + usertime; + return retn; + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + try { + proc.getInputStream().close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return null; + } private static String substring(String src, int start_idx, int end_idx) { byte[] b = src.getBytes(); -- Gitee