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

The following examples show how to use oshi.hardware.CentralProcessor#getProcessorCpuLoadTicks() . 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: CPULoadCollector.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public List<MetricFamilySamples> collect() {
        SystemInfo systemInfo = new SystemInfo();
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
//        List<CentralProcessor.LogicalProcessor> logicalProcessors = processor.getLogicalProcessors();
        long[][] pTickList = processor.getProcessorCpuLoadTicks();

        GaugeMetricFamily gaugeMetricFamily = new GaugeMetricFamily("mycat_cpu_utility", "mycat_cpu_utility", ImmutableList.of("index"));

        long[][] tickList = processor.getProcessorCpuLoadTicks();
        for (int i = 0; i < pTickList.length; i++) {
            long[] prevTicks = pTickList[i];
            long[] ticks = tickList[i];

            long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
            long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
            long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
            long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
            long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
            long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
            long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
            long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
            long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;

            double utility  = 1.0 - (idle * 1.0 / totalCpu);
            if (!Double.isInfinite(utility)) {
                utility = 0;
            }
            gaugeMetricFamily.addMetric(ImmutableList.of(String.valueOf(i)),utility);

        }




        return ImmutableList.of(gaugeMetricFamily);
    }
 
Example 2
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);
		}
	}
}