Java Code Examples for oshi.hardware.CentralProcessor#getSystemCpuLoad()

The following examples show how to use oshi.hardware.CentralProcessor#getSystemCpuLoad() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OshiDevice.java    From iot-java with Eclipse Public License 1.0 7 votes vote down vote up
private static JsonObject createOshiData() {
	HardwareAbstractionLayer hal = si.getHardware();
	CentralProcessor processor = hal.getProcessor();
	double loadAverage = processor.getSystemCpuLoad() * 100;

	GlobalMemory memory = hal.getMemory();
	long availableMemory = memory.getAvailable();
	long totalMemory = memory.getTotal();
	long usedMemory = totalMemory - availableMemory;
	double memoryUtilization = (usedMemory / (double) totalMemory) * 100;

	JsonObject json = new JsonObject();
	json.addProperty("memory", memoryUtilization);
	json.addProperty("cpu", loadAverage);
	return json;
}
 
Example 2
Source File: SystemResourcesCounter.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void calculateCPUUsage(CentralProcessor processor) {
	long[] ticks = processor.getSystemCpuLoadTicks();
	if (this.previousCpuTicks == null) {
		this.previousCpuTicks = ticks;
	}

	long userTicks = ticks[TickType.USER.getIndex()] - previousCpuTicks[TickType.USER.getIndex()];
	long niceTicks = ticks[TickType.NICE.getIndex()] - previousCpuTicks[TickType.NICE.getIndex()];
	long sysTicks = ticks[TickType.SYSTEM.getIndex()] - previousCpuTicks[TickType.SYSTEM.getIndex()];
	long idleTicks = ticks[TickType.IDLE.getIndex()] - previousCpuTicks[TickType.IDLE.getIndex()];
	long iowaitTicks = ticks[TickType.IOWAIT.getIndex()] - previousCpuTicks[TickType.IOWAIT.getIndex()];
	long irqTicks = ticks[TickType.IRQ.getIndex()] - previousCpuTicks[TickType.IRQ.getIndex()];
	long softIrqTicks = ticks[TickType.SOFTIRQ.getIndex()] - previousCpuTicks[TickType.SOFTIRQ.getIndex()];
	long totalCpuTicks = userTicks + niceTicks + sysTicks + idleTicks + iowaitTicks + irqTicks + softIrqTicks;
	this.previousCpuTicks = ticks;

	cpuUser = 100d * userTicks / totalCpuTicks;
	cpuNice = 100d * niceTicks / totalCpuTicks;
	cpuSys = 100d * sysTicks / totalCpuTicks;
	cpuIdle = 100d * idleTicks / totalCpuTicks;
	cpuIOWait = 100d * iowaitTicks / totalCpuTicks;
	cpuIrq = 100d * irqTicks / totalCpuTicks;
	cpuSoftIrq = 100d * softIrqTicks / totalCpuTicks;

	cpuUsage = processor.getSystemCpuLoad() * 100;

	double[] loadAverage = processor.getSystemLoadAverage(3);
	cpuLoad1 = (loadAverage[0] < 0 ? Double.NaN : loadAverage[0]);
	cpuLoad5 = (loadAverage[1] < 0 ? Double.NaN : loadAverage[1]);
	cpuLoad15 = (loadAverage[2] < 0 ? Double.NaN : loadAverage[2]);

	double[] load = processor.getProcessorCpuLoadBetweenTicks();
	checkState(load.length == cpuUsagePerProcessor.length());
	for (int i = 0; i < load.length; i++) {
		cpuUsagePerProcessor.set(i, load[i] * 100);
	}
}
 
Example 3
Source File: SystemResourcesCounter.java    From flink with Apache License 2.0 5 votes vote down vote up
private void calculateCPUUsage(CentralProcessor processor) {
	long[] ticks = processor.getSystemCpuLoadTicks();
	if (this.previousCpuTicks == null) {
		this.previousCpuTicks = ticks;
	}

	long userTicks = ticks[TickType.USER.getIndex()] - previousCpuTicks[TickType.USER.getIndex()];
	long niceTicks = ticks[TickType.NICE.getIndex()] - previousCpuTicks[TickType.NICE.getIndex()];
	long sysTicks = ticks[TickType.SYSTEM.getIndex()] - previousCpuTicks[TickType.SYSTEM.getIndex()];
	long idleTicks = ticks[TickType.IDLE.getIndex()] - previousCpuTicks[TickType.IDLE.getIndex()];
	long iowaitTicks = ticks[TickType.IOWAIT.getIndex()] - previousCpuTicks[TickType.IOWAIT.getIndex()];
	long irqTicks = ticks[TickType.IRQ.getIndex()] - previousCpuTicks[TickType.IRQ.getIndex()];
	long softIrqTicks = ticks[TickType.SOFTIRQ.getIndex()] - previousCpuTicks[TickType.SOFTIRQ.getIndex()];
	long totalCpuTicks = userTicks + niceTicks + sysTicks + idleTicks + iowaitTicks + irqTicks + softIrqTicks;
	this.previousCpuTicks = ticks;

	cpuUser = 100d * userTicks / totalCpuTicks;
	cpuNice = 100d * niceTicks / totalCpuTicks;
	cpuSys = 100d * sysTicks / totalCpuTicks;
	cpuIdle = 100d * idleTicks / totalCpuTicks;
	cpuIOWait = 100d * iowaitTicks / totalCpuTicks;
	cpuIrq = 100d * irqTicks / totalCpuTicks;
	cpuSoftIrq = 100d * softIrqTicks / totalCpuTicks;

	cpuUsage = processor.getSystemCpuLoad() * 100;

	double[] loadAverage = processor.getSystemLoadAverage(3);
	cpuLoad1 = (loadAverage[0] < 0 ? Double.NaN : loadAverage[0]);
	cpuLoad5 = (loadAverage[1] < 0 ? Double.NaN : loadAverage[1]);
	cpuLoad15 = (loadAverage[2] < 0 ? Double.NaN : loadAverage[2]);

	double[] load = processor.getProcessorCpuLoadBetweenTicks();
	checkState(load.length == cpuUsagePerProcessor.length());
	for (int i = 0; i < load.length; i++) {
		cpuUsagePerProcessor.set(i, load[i] * 100);
	}
}
 
Example 4
Source File: SystemResourcesCounter.java    From flink with Apache License 2.0 5 votes vote down vote up
private void calculateCPUUsage(CentralProcessor processor) {
	long[] ticks = processor.getSystemCpuLoadTicks();
	if (this.previousCpuTicks == null) {
		this.previousCpuTicks = ticks;
	}

	long userTicks = ticks[TickType.USER.getIndex()] - previousCpuTicks[TickType.USER.getIndex()];
	long niceTicks = ticks[TickType.NICE.getIndex()] - previousCpuTicks[TickType.NICE.getIndex()];
	long sysTicks = ticks[TickType.SYSTEM.getIndex()] - previousCpuTicks[TickType.SYSTEM.getIndex()];
	long idleTicks = ticks[TickType.IDLE.getIndex()] - previousCpuTicks[TickType.IDLE.getIndex()];
	long iowaitTicks = ticks[TickType.IOWAIT.getIndex()] - previousCpuTicks[TickType.IOWAIT.getIndex()];
	long irqTicks = ticks[TickType.IRQ.getIndex()] - previousCpuTicks[TickType.IRQ.getIndex()];
	long softIrqTicks = ticks[TickType.SOFTIRQ.getIndex()] - previousCpuTicks[TickType.SOFTIRQ.getIndex()];
	long totalCpuTicks = userTicks + niceTicks + sysTicks + idleTicks + iowaitTicks + irqTicks + softIrqTicks;
	this.previousCpuTicks = ticks;

	cpuUser = 100d * userTicks / totalCpuTicks;
	cpuNice = 100d * niceTicks / totalCpuTicks;
	cpuSys = 100d * sysTicks / totalCpuTicks;
	cpuIdle = 100d * idleTicks / totalCpuTicks;
	cpuIOWait = 100d * iowaitTicks / totalCpuTicks;
	cpuIrq = 100d * irqTicks / totalCpuTicks;
	cpuSoftIrq = 100d * softIrqTicks / totalCpuTicks;

	cpuUsage = processor.getSystemCpuLoad() * 100;

	double[] loadAverage = processor.getSystemLoadAverage(3);
	cpuLoad1 = (loadAverage[0] < 0 ? Double.NaN : loadAverage[0]);
	cpuLoad5 = (loadAverage[1] < 0 ? Double.NaN : loadAverage[1]);
	cpuLoad15 = (loadAverage[2] < 0 ? Double.NaN : loadAverage[2]);

	double[] load = processor.getProcessorCpuLoadBetweenTicks();
	checkState(load.length == cpuUsagePerProcessor.length());
	for (int i = 0; i < load.length; i++) {
		cpuUsagePerProcessor.set(i, load[i] * 100);
	}
}