Skip to content

Commit

Permalink
Use system cpu
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Mar 20, 2021
1 parent 7325113 commit 202de0a
Showing 1 changed file with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,81 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.CentralProcessor.TickType;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;

public class OshiPerformanceCounter implements PerformanceCounter {

private static final Logger logger = LoggerFactory.getLogger(OshiPerformanceCounter.class);
private static final String ID = Constants.PERFORMANCE_COUNTER_PREFIX + "OshiPerformanceCounter";
private final static double MILLIS_IN_SECOND = 1000;

private static final double MILLIS_IN_SECOND = 1000;

private long prevCollectionInMillis = -1;
private double prevProcessIO;
private double currentProcessIO;
private OSProcess processInfo;
private double prevTotalProcessorTime;
private double currentTotalProcessorTime;
private long prevProcessBytes;
private long prevTotalProcessorMillis;

private final OSProcess processInfo;
private final CentralProcessor processor;

public OshiPerformanceCounter() {
SystemInfo systemInfo = new SystemInfo();
OperatingSystem osInfo = systemInfo.getOperatingSystem();
processInfo = osInfo.getProcess(osInfo.getProcessId());
processInfo.updateAttributes();

currentProcessIO = (double) (processInfo.getBytesRead() + processInfo.getBytesWritten());
prevProcessIO = currentProcessIO;
processor = systemInfo.getHardware().getProcessor();

currentTotalProcessorTime = processInfo.getUserTime() + processInfo.getKernelTime();
prevTotalProcessorTime = currentTotalProcessorTime;
updateAttributes(processInfo);
prevProcessBytes = getProcessBytes(processInfo);
prevTotalProcessorMillis = getTotalProcessorMillis(processor);
}

@Override public String getId() {
@Override
public String getId() {
return ID;
}

@Override public void report(TelemetryClient telemetryClient) {
processInfo.updateAttributes();

@Override
public void report(TelemetryClient telemetryClient) {
long currentCollectionInMillis = System.currentTimeMillis();
currentProcessIO = (double) (processInfo.getBytesRead() + processInfo.getBytesWritten());
currentTotalProcessorTime = processInfo.getUserTime() + processInfo.getKernelTime();
updateAttributes(processInfo);
long currentProcessBytes = getProcessBytes(processInfo);
long currTotalProcessorMillis = getTotalProcessorMillis(processor);
if (prevCollectionInMillis != -1) {
double timeElapsedInSeconds = (currentCollectionInMillis - prevCollectionInMillis) / MILLIS_IN_SECOND;
double processIo = (currentProcessIO - prevProcessIO) / timeElapsedInSeconds;
send(telemetryClient, processIo, Constants.PROCESS_IO_PC_METRIC_NAME);
logger.trace("Sent performance counter for '{}': '{}'", Constants.PROCESS_IO_PC_METRIC_NAME, processIo);

double processorTime = (currentTotalProcessorTime - prevTotalProcessorTime) / timeElapsedInSeconds;
send(telemetryClient, processorTime, Constants.TOTAL_CPU_PC_METRIC_NAME);
logger.trace("Sent performance counter for '{}': '{}'", Constants.TOTAL_CPU_PC_METRIC_NAME, processorTime);
double elapsedMillis = currentCollectionInMillis - prevCollectionInMillis;
double elapsedSeconds = elapsedMillis / MILLIS_IN_SECOND;
double processBytes = (currentProcessBytes - prevProcessBytes) / elapsedSeconds;
send(telemetryClient, processBytes, Constants.PROCESS_IO_PC_METRIC_NAME);
logger.trace("Sent performance counter for '{}': '{}'", Constants.PROCESS_IO_PC_METRIC_NAME, processBytes);

// TODO should this be divided by physical processors or logical processors?
double processorLoad = (currTotalProcessorMillis - prevTotalProcessorMillis) / (elapsedMillis * processor.getPhysicalProcessorCount());
send(telemetryClient, processorLoad, Constants.TOTAL_CPU_PC_METRIC_NAME);
logger.trace("Sent performance counter for '{}': '{}'", Constants.TOTAL_CPU_PC_METRIC_NAME, processorLoad);
}

prevProcessIO = currentProcessIO;
prevTotalProcessorTime = currentTotalProcessorTime;
prevProcessBytes = currentProcessBytes;
prevTotalProcessorMillis = currTotalProcessorMillis;
prevCollectionInMillis = currentCollectionInMillis;
}

private static void updateAttributes(OSProcess processInfo) {
if (!processInfo.updateAttributes()) {
logger.debug("could not update process attributes");
}
}

// must call updateAttributes on processInfo before calling this method
private static long getProcessBytes(OSProcess processInfo) {
return processInfo.getBytesRead() + processInfo.getBytesWritten();
}

private static long getTotalProcessorMillis(CentralProcessor processor) {
long[] systemCpuLoadTicks = processor.getSystemCpuLoadTicks();
return systemCpuLoadTicks[TickType.USER.getIndex()] + systemCpuLoadTicks[TickType.SYSTEM.getIndex()];
}

private void send(TelemetryClient telemetryClient, double value, String metricName) {
MetricTelemetry telemetry = new MetricTelemetry(metricName, value);
telemetryClient.track(telemetry);
Expand Down

0 comments on commit 202de0a

Please sign in to comment.