|
|
@@ -1,33 +1,30 @@
|
|
|
package cn.reghao.autodop.common.machine;
|
|
|
|
|
|
import cn.reghao.autodop.common.machine.pojo.CpuInfo;
|
|
|
+import cn.reghao.autodop.common.machine.pojo.CpuStat;
|
|
|
import cn.reghao.autodop.common.machine.pojo.CpuUsage;
|
|
|
-import cn.reghao.autodop.common.machine.utils.AbstractCalculator;
|
|
|
-import cn.reghao.autodop.common.machine.utils.BeanConversion;
|
|
|
-import cn.reghao.autodop.common.machine.utils.CpuCalculator;
|
|
|
+import cn.reghao.autodop.common.machine.utils.AbstractPercentageCalculator;
|
|
|
+import cn.reghao.autodop.common.machine.utils.CpuPercentageCalculator;
|
|
|
import cn.reghao.autodop.common.utils.text.TextFile;
|
|
|
-import com.google.gson.Gson;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
+ * CPU 信息
|
|
|
+ *
|
|
|
* @author reghao
|
|
|
* @date 2019-10-25 13:12:36
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class Cpu {
|
|
|
- private final String infoFile = "/proc/cpuinfo";
|
|
|
- private final String statFile = "/proc/stat";
|
|
|
- private final String whiteSpace = "\\s+";
|
|
|
- private AbstractCalculator<CpuUsage> calculator;
|
|
|
-
|
|
|
- public Cpu() {
|
|
|
- this.calculator = new CpuCalculator();
|
|
|
- }
|
|
|
+ private AbstractPercentageCalculator<CpuStat> calculator = new CpuPercentageCalculator();;
|
|
|
+ private TextFile textFile = new TextFile();
|
|
|
+ private int cpuCores = Runtime.getRuntime().availableProcessors();
|
|
|
|
|
|
public List<CpuInfo> info() throws IllegalAccessException {
|
|
|
- List<CpuInfo> infos = new ArrayList<>();
|
|
|
+ /*List<CpuInfo> infos = new ArrayList<>();
|
|
|
+ String infoFile = "/proc/cpuinfo";
|
|
|
String[] infoArray = new TextFile().readFile(infoFile).split("\n\n");
|
|
|
for (String info : infoArray) {
|
|
|
String[] tmp = info.split("\n");
|
|
|
@@ -43,63 +40,48 @@ public class Cpu {
|
|
|
infos.add(cpu);
|
|
|
}
|
|
|
|
|
|
- return infos;
|
|
|
- }
|
|
|
-
|
|
|
- public List<CpuUsage> stat() throws IllegalAccessException {
|
|
|
- List<CpuUsage> usages = new ArrayList<>();
|
|
|
- String stat = new TextFile().readFile(statFile).split("intr")[0];
|
|
|
- String[] stats = stat.split(System.lineSeparator());
|
|
|
-
|
|
|
- BeanConversion<CpuUsage> conversion = new BeanConversion<>();
|
|
|
- for (int i = 0; i < stats.length; i++) {
|
|
|
- String[] tmp = stats[i].split(whiteSpace);
|
|
|
- String[] array = Arrays.copyOfRange(tmp, 1, tmp.length);
|
|
|
-
|
|
|
- CpuUsage usage = new CpuUsage();
|
|
|
- conversion.convert(array, usage);
|
|
|
- usages.add(usage);
|
|
|
- }
|
|
|
-
|
|
|
- return usages;
|
|
|
+ return infos;*/
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * top 命令的 cpu 部分
|
|
|
- * TODO 确保代码执行时间小于 CPU 频率
|
|
|
+ * CPU 使用率
|
|
|
*
|
|
|
- * @param interval 刷新间隔时间
|
|
|
+ * @param interval 采样时间,单位是 ms
|
|
|
* @return
|
|
|
- * @date 2019-10-28 下午9:24
|
|
|
+ * @date 2020-03-19 上午10:07
|
|
|
*/
|
|
|
- public String topCpu(int interval) throws InterruptedException, IllegalAccessException {
|
|
|
- List<CpuUsage> stats1 = stat();
|
|
|
- Thread.sleep( interval*1_000);
|
|
|
- List<CpuUsage> stats2 = stat();
|
|
|
-
|
|
|
- Map<String, Object> res = new LinkedHashMap<>();
|
|
|
- for (int i = 0; i < stats1.size(); i++) {
|
|
|
- CpuUsage[] array = new CpuUsage[2];
|
|
|
- array[0] = stats1.get(i);
|
|
|
- array[1] = stats2.get(i);
|
|
|
+ public CpuUsage usage(long interval) throws InterruptedException {
|
|
|
+ CpuStat stat1 = stat();
|
|
|
+ Thread.sleep(interval);
|
|
|
+ CpuStat stat2 = stat();
|
|
|
|
|
|
- Object r = calculator.format(array);
|
|
|
- if (i == 0) {
|
|
|
- res.put("%cpu", r);
|
|
|
- } else {
|
|
|
- res.put("%cpu"+(i-1), r);
|
|
|
- }
|
|
|
- }
|
|
|
+ return (CpuUsage) calculator.format(stat1, stat2);
|
|
|
+ }
|
|
|
|
|
|
- return new Gson().toJson(res);
|
|
|
+ private CpuStat stat() {
|
|
|
+ String statFile = "/proc/stat";
|
|
|
+ String[] stat = textFile.readFile(statFile).split(System.lineSeparator());
|
|
|
+ String whiteSpace = "\\s+";
|
|
|
+ String[] cpu = stat[0].split(whiteSpace);
|
|
|
+
|
|
|
+ CpuStat cpuStat = new CpuStat();
|
|
|
+ cpuStat.setUser(Long.parseLong(cpu[1]));
|
|
|
+ cpuStat.setNice(Long.parseLong(cpu[2]));
|
|
|
+ cpuStat.setSystem(Long.parseLong(cpu[3]));
|
|
|
+ cpuStat.setIdle(Long.parseLong(cpu[4]));
|
|
|
+ cpuStat.setIowait(Long.parseLong(cpu[5]));
|
|
|
+ cpuStat.setIrq(Long.parseLong(cpu[6]));
|
|
|
+ cpuStat.setSoftirq(Long.parseLong(cpu[7]));
|
|
|
+ cpuStat.setSteal(Long.parseLong(cpu[8]));
|
|
|
+ cpuStat.setGuest(Long.parseLong(cpu[9]));
|
|
|
+ cpuStat.setGuestnice(Long.parseLong(cpu[10]));
|
|
|
+
|
|
|
+ return cpuStat;
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws IllegalAccessException, InterruptedException {
|
|
|
+ public static void main(String[] args) throws InterruptedException {
|
|
|
Cpu cpu = new Cpu();
|
|
|
- cpu.topCpu(3);
|
|
|
-
|
|
|
- for (;;) {
|
|
|
- log.info("\n{}", cpu.topCpu(3));
|
|
|
- }
|
|
|
+ log.info("cpu usage -> {}", cpu.usage(1000));
|
|
|
}
|
|
|
}
|