Skip to content

Commit

Permalink
feat: attempt to detect lack of sudo access better
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Jul 6, 2024
1 parent 499d1e7 commit 5ac7b35
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,33 @@ public ProcessMacOSPowermetricsSensor() {
// extract metadata
try {
final var exec = new ProcessBuilder()
.command("sudo", "powermetrics", "--samplers", "cpu_power", "-i", "10", "-n", "1")
.command("powermetrics", "--samplers", "cpu_power", "-i", "10", "-n", "1")
.start();

// if the process is already dead, get the error
if (!exec.isAlive()) {
final var exitValue = exec.exitValue();
if (exitValue != 0) {
final String errorMsg;
try (final var error = exec.errorReader()) {
errorMsg = error.readLine();
}
throw new RuntimeException(
"Couldn't execute powermetrics. Error code: " + exitValue + ", message: " + errorMsg);
}
}

initMetadata(exec.getInputStream());
} catch (Exception e) {
throw new RuntimeException("Couldn't execute powermetrics to extract metadata", e);
throw new RuntimeException("Couldn't extract sensor metadata", e);
}
}

public void start(long frequency) throws Exception {
if (!isStarted()) {
// it takes some time for the external process in addition to the sampling time so adjust the sampling frequency to account for this so that at most one measure occurs during the sampling time window
final var freq = Long.toString(frequency - 50);
powermetrics = new ProcessBuilder().command("sudo", "powermetrics", "--samplers", "cpu_power,tasks",
powermetrics = new ProcessBuilder().command("powermetrics", "--samplers", "cpu_power,tasks",
"--show-process-samp-norm", "--show-process-gpu", "-i", freq).start();
}
}
Expand Down

0 comments on commit 5ac7b35

Please sign in to comment.