gpt4 book ai didi

java - 使用 Java 获取每个内核的 CPU 负载

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:43 27 4
gpt4 key购买 nike

我使用此代码通过 Java 代码从/proc/stat 获取 CPU 负载:

private static long PREV_IDLE;      //CPU Idle time
private static long PREV_TOTAL; //CPU Total time

public static float getCPUProcOrig() throws Exception
{
BufferedReader cpuReader = null;
try
{
cpuReader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/stat")));
String cpuLine = cpuReader.readLine();
if (cpuLine == null)
{
throw new Exception("/proc/stat didn't work well");
}
else
{
String[] CPU = cpuLine.split("\\s+");

long IDLE = Long.parseLong(CPU[4]);//Get the idle CPU time.

long DIFF_IDLE = IDLE - PREV_IDLE;
long DIFF_TOTAL = TOTAL - PREV_TOTAL;
long DIFF_USAGE = DIFF_TOTAL == 0 ? 0 : (1000 * (DIFF_TOTAL - DIFF_IDLE) / DIFF_TOTAL + 5) / 10;
// System.out.println("CPU: " + DIFF_USAGE + "%");

PREV_TOTAL = TOTAL;
PREV_IDLE = IDLE;
return (float) DIFF_USAGE;
}
}
catch (Exception e)
{
throw e; // It's not desirable to handle the exception here
}
finally
{
if (cpuReader != null)
try
{
cpuReader.close();
}
catch (IOException e)
{
// Do nothing
}
}
}

不幸的是,此代码运行良好,但适用于平均 CPU 负载。我想单独列出所有内核负载。我尝试扩展代码:

private static long PREV_IDLE;      //CPU Idle time
private static long PREV_TOTAL; //CPU Total time
private static final int CONSERVATIVE = 0;
private static final int AVERAGE = 1;
private static final int OPTIMISTIC = 2;

public HashMap<String, HashMap<String, Float>> getCPUProc() throws Exception
{
BufferedReader cpuReader = null;
HashMap<String, HashMap<String, Float>> usageData = new HashMap<>();

try
{
String line;
cpuReader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/stat")));
while ((line = cpuReader.readLine()) != null)
{
String[] CPU = line.split("\\s+");
if (CPU[0].startsWith("cpu"))
{
String cpuName = String.valueOf(CPU[0]);//Get the cpu number.
long IDLE = Long.parseLong(CPU[4]);//Get the idle CPU time.
long TOTAL = Long.parseLong(CPU[1]) + Long.parseLong(CPU[2]) + Long.parseLong(CPU[3]) + Long.parseLong(CPU[4]);
// System.out.println("IDLE : " + IDLE);

long DIFF_IDLE = IDLE - PREV_IDLE;
long DIFF_TOTAL = TOTAL - PREV_TOTAL;
long DIFF_USAGE = DIFF_TOTAL == 0 ? 0 : (1000 * (DIFF_TOTAL - DIFF_IDLE) / DIFF_TOTAL + 5) / 10;
// System.out.println("CPU: " + DIFF_USAGE + "%");

PREV_TOTAL = TOTAL;
PREV_IDLE = IDLE;

HashMap<String, Float> usageData2 = new HashMap<>();
usageData2.put("cpu", (float) DIFF_USAGE);

usageData.put(cpuName, usageData2);
}

// return (float) DIFF_USAGE;
}
}
catch (IOException | NumberFormatException e)
{
throw e; // It's not desirable to handle the exception here
}
finally
{
if (cpuReader != null)
try
{
cpuReader.close();
}
catch (IOException e)
{
// Do nothing
}
}
return usageData;
}

从第一段代码可以看出,有几个静态变量用于计算 CPU 负载。当我尝试从/proc/stat 读取所有行时,这些静态变量未正确使用,核心之间的数据困惑,结果不准确。

你能帮我正确读取负载吗?我没主意了。我该如何修复代码?

最佳答案

问题是行:

long DIFF_IDLE = IDLE - PREV_IDLE;
long DIFF_TOTAL = TOTAL - PREV_TOTAL;
long DIFF_USAGE = DIFF_TOTAL == 0 ? 0 : (1000 * (DIFF_TOTAL - DIFF_IDLE) / DIFF_TOTAL + 5) / 10;
PREV_TOTAL = TOTAL;
PREV_IDLE = IDLE;

如您所见,PREV_IDLEPREV_TOTAL 在所有内核之间共享;可能您想让它们保持特定于核心,因此您应该在使用它们之前加载该值。

一个不错的主意是代替

PREV_TOTAL = TOTAL;
PREV_IDLE = IDLE;

将它们保存到 usageData2

关于java - 使用 Java 获取每个内核的 CPU 负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28787946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com