Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public static String prepare(long pid) {
return " " + pid + " ";
}

public static String prepare(String pidAsString) {
return " " + pidAsString + " ";
}

public long pid() {
return Long.parseLong(stringForMatching().trim());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Pattern;

import net.laprun.sustainability.power.SensorMeasure;
import net.laprun.sustainability.power.SensorMetadata;
Expand Down Expand Up @@ -102,18 +101,95 @@ private static class ProcessRecord {
final double cpu;
final double gpu;
final String pid;
private static final Pattern spaces = Pattern.compile("\\s+");

public ProcessRecord(String line) throws IllegalArgumentException {
// Expected normal output:
//Name ID CPU ms/s samp ms/s User% Deadlines (<2 ms, 2-5 ms) Wakeups (Intr, Pkg idle) GPU ms/s
//iTerm2 1008 46.66 46.91 83.94 0.00 0.00 30.46 0.00 0.00
final var processData = spaces.split(line, 10);
if (processData.length != 10) {
throw new IllegalArgumentException("Received line doesn't conform to expected format: " + line);
// Expected summary output:
//Name ID CPU ms/s samp ms/s [total] User% Deadlines/s [total] (<2 ms, 2-5 ms) Wakeups/s [total] (Intr, Pkg idle) Dead GPU ms/s
//WindowServer 406 493.74 493.96 [5165.88 ] 64.82 65.95 [690 ] 0.00 [0 ] 656.62 [6870 ] 4.21 [44 ] N 0.00

try {
// Trim leading/trailing whitespace
line = line.trim();

// Find first whitespace block after process name (marks start of ID)
int idStart = findFirstWhitespace(line);
if (idStart == -1) {
throw new IllegalArgumentException("Cannot find ID in line: " + line);
}

// Skip whitespace to get to ID
idStart = skipWhitespace(line, idStart);
int idEnd = findNextWhitespace(line, idStart);
pid = RegisteredPID.prepare(line.substring(idStart, idEnd));

// Skip CPU ms/s column (skip whitespace, then number, then whitespace)
int pos = skipWhitespace(line, idEnd);
pos = skipNumber(line, pos);

// Now at samp ms/s
pos = skipWhitespace(line, pos);
int sampStart = pos;
int sampEnd = skipNumber(line, sampStart);
cpu = Double.parseDouble(line.substring(sampStart, sampEnd));

// Skip to end and work backwards to find GPU ms/s
// The GPU value is the last numeric value on the line
int lastNumEnd = line.length();
while (lastNumEnd > 0 && Character.isWhitespace(line.charAt(lastNumEnd - 1))) {
lastNumEnd--;
}

int lastNumStart = lastNumEnd;
while (lastNumStart > 0 && isNumberChar(line.charAt(lastNumStart - 1))) {
lastNumStart--;
}

if (lastNumStart < lastNumEnd) {
gpu = Double.parseDouble(line.substring(lastNumStart, lastNumEnd));
} else {
throw new IllegalArgumentException("Cannot find GPU value in line: " + line);
}

} catch (Exception e) {
throw new IllegalArgumentException("Received line doesn't conform to expected format: " + line, e);
}
pid = " " + processData[1] + " "; // pad to match prepared version for matching
cpu = Double.parseDouble(processData[3]);
gpu = Double.parseDouble(processData[9]);
}

private static int findFirstWhitespace(String line) {
for (int i = 0; i < line.length(); i++) {
if (Character.isWhitespace(line.charAt(i))) {
return i;
}
}
return -1;
}

private static int skipWhitespace(String line, int pos) {
while (pos < line.length() && Character.isWhitespace(line.charAt(pos))) {
pos++;
}
return pos;
}

private static int findNextWhitespace(String line, int pos) {
while (pos < line.length() && !Character.isWhitespace(line.charAt(pos))) {
pos++;
}
return pos;
}

private static int skipNumber(String line, int pos) {
while (pos < line.length() && isNumberChar(line.charAt(pos))) {
pos++;
}
return pos;
}

private static boolean isNumberChar(char c) {
return Character.isDigit(c) || c == '.' || c == '-' || c == 'e' || c == 'E';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ void extractPowerMeasureForM2() {
checkPowerMeasure("monterey-m2.txt", 10, MacOSPowermetricsSensor.CPU);
}

@Test
void extractPowerMeasureForM4() {
final var sensor = new ResourceMacOSPowermetricsSensor("tahoe-m4-summary.txt");
final var metadata = sensor.metadata();
final var pid0 = sensor.register(2976);

final var cpu = metadata.metadataFor(MacOSPowermetricsSensor.CPU);
// re-open the stream to read the measure this time
final var measure = sensor.update(0L);

final var totalCPUPower = 420;
final var totalCPUTime = 1287.34;
// Process CPU power should be equal to sample ms/s divided for process (here: 116.64) by total samples (1222.65) times total CPU power
var pidCPUShare = 224.05 / totalCPUTime;
assertEquals(pidCPUShare * totalCPUPower, getComponent(measure, pid0, cpu));
}

@Test
void extractPowerMeasureForIntel() {
checkPowerMeasure("sonoma-intel.txt", 8.53f, MacOSPowermetricsSensor.PACKAGE);
Expand Down
Loading
Loading