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

The following examples show how to use oshi.hardware.CentralProcessor#getProcessorCpuLoadBetweenTicks() . 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: CPUsCombinedPercSampler.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void sample(final IMonitoringController monitoringController) {
	if (!monitoringController.isMonitoringEnabled() || !monitoringController.isProbeActivated(SignatureFactory.createCPUSignature())) {
		return;
	}
	final CentralProcessor centralProcessor = this.hardwareAbstractionLayer.getProcessor();
	final double[] cpuLoads = centralProcessor.getProcessorCpuLoadBetweenTicks();
	final ITimeSource timesource = monitoringController.getTimeSource();

	for (int i = 0; i < cpuLoads.length; i++) {
		if (monitoringController.isProbeActivated(SignatureFactory.createCPUSignature(i))) {

			final double combinedUtilization = cpuLoads[i];
			final ResourceUtilizationRecord r = new ResourceUtilizationRecord(timesource.getTime(),
					monitoringController.getHostname(), CPU_RESOURCE_NAME_PREFIX + i, combinedUtilization);
			monitoringController.newMonitoringRecord(r);
		}
	}
}
 
Example 2
Source File: OshiPlatformCache.java    From hawkular-agent with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the given metric's value, or null if there is no processor with the given number.
 *
 * @param processorNumber number of the processor, as a String
 * @param metricToCollect the metric to collect
 * @return the value of the metric, or null if there is no processor with the given number
 */
public Double getProcessorMetric(String processorNumber, ID metricToCollect) {

    CentralProcessor processor = getProcessor();
    if (processor == null) {
        return null;
    }

    int processorIndex;
    try {
        processorIndex = Integer.parseInt(processorNumber);
        if (processorIndex < 0 || processorIndex >= processor.getLogicalProcessorCount()) {
            return null;
        }
    } catch (Exception e) {
        return null;
    }

    if (PlatformMetricType.PROCESSOR_CPU_USAGE.getMetricTypeId().equals(metricToCollect)) {
        return processor.getProcessorCpuLoadBetweenTicks()[processorIndex];
    } else {
        throw new UnsupportedOperationException("Invalid processor metric to collect: " + metricToCollect);
    }
}
 
Example 3
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 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);
	}
}
 
Example 5
Source File: CPUsDetailedPercSampler.java    From kieker with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void sample(final IMonitoringController monitoringController) {
	if (!monitoringController.isMonitoringEnabled() || !monitoringController.isProbeActivated(SignatureFactory.createCPUSignature())) {
		return;
	}
	final CentralProcessor centralProcessor = this.hardwareAbstractionLayer.getProcessor();
	final long[][] processorLoadTicks = centralProcessor.getProcessorCpuLoadTicks();
	if (this.converters == null) {
		this.converters = new CPUsDetailedPercConverter[processorLoadTicks.length];
		for (int i = 0; i < this.converters.length; i++) {
			this.converters[i] = new CPUsDetailedPercConverter(i);
		}
	}
	final ITimeSource timesource = monitoringController.getTimeSource();
	for (int i = 0; i < processorLoadTicks.length; i++) {
		if (monitoringController.isProbeActivated(SignatureFactory.createCPUSignature(i))) {
			final CPUsDetailedPercConverter converter = this.converters[i];
			final long[] plt = processorLoadTicks[i];
			converter.passNewProcessorLoadTicks(plt);
			converter.convertToPercentage();

			final double system = converter.getSystemPerc();
			final double wait = converter.getWaitPerc();
			final double nice = converter.getNicePerc();
			final double idle = converter.getIdlePerc();
			final double user = converter.getUserPerc();
			final double irq = converter.getIrqPerc();
			final double combined = centralProcessor.getProcessorCpuLoadBetweenTicks()[i];

			final CPUUtilizationRecord r = new CPUUtilizationRecord(timesource.getTime(),
					monitoringController.getHostname(), Integer.toString(i), user, system, wait, nice, irq,
					combined, idle);
			monitoringController.newMonitoringRecord(r);
		}
	}
}
 
Example 6
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);
	}
}