|
1 | 1 | package net.laprun.sustainability.cli; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
3 | 5 | import java.util.List; |
4 | 6 |
|
5 | | -import net.laprun.sustainability.power.PowerMeasurer; |
| 7 | +import io.quarkus.logging.Log; |
| 8 | +import io.quarkus.runtime.Quarkus; |
| 9 | +import io.smallrye.mutiny.Uni; |
| 10 | +import io.smallrye.mutiny.infrastructure.Infrastructure; |
| 11 | +import net.laprun.sustainability.power.Measure; |
| 12 | +import net.laprun.sustainability.power.SensorUnit; |
| 13 | +import net.laprun.sustainability.power.analysis.total.TotalSyntheticComponent; |
| 14 | +import net.laprun.sustainability.power.sensors.PowerSensor; |
| 15 | +import net.laprun.sustainability.power.sensors.macos.powermetrics.FileMacOSPowermetricsSensor; |
6 | 16 | import picocli.CommandLine; |
7 | 17 |
|
8 | 18 | @CommandLine.Command |
9 | 19 | public class Power implements Runnable { |
10 | 20 |
|
11 | | - @CommandLine.Parameters(hidden = true) |
| 21 | + @CommandLine.Parameters(index = "0..*", hidden = true) |
12 | 22 | List<String> cmd; |
13 | 23 |
|
14 | 24 | @CommandLine.Option(names = { "-n", |
15 | 25 | "--name" }, description = "Optional name for the application, defaults to passed command line") |
16 | 26 | String name; |
17 | 27 |
|
18 | | - private final PowerMeasurer measurer; |
| 28 | + private final File output; |
| 29 | + private final PowerSensor sensor; |
| 30 | + private Process powermetrics; |
19 | 31 |
|
20 | | - public Power(PowerMeasurer measurer) { |
21 | | - this.measurer = measurer; |
| 32 | + public Power() throws IOException { |
| 33 | + /* |
| 34 | + * this.output = File.createTempFile("power-", ".tmp"); |
| 35 | + * output.deleteOnExit(); |
| 36 | + */ |
| 37 | + this.output = new File("output.txt"); |
| 38 | + output.createNewFile(); |
| 39 | + sensor = new FileMacOSPowermetricsSensor(output); |
| 40 | + Log.info(output.getAbsolutePath()); |
22 | 41 | } |
23 | 42 |
|
24 | 43 | @Override |
25 | 44 | public void run() { |
| 45 | + if (cmd == null || cmd.isEmpty()) { |
| 46 | + Log.info("No command specified, exiting."); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + final var cmdPid = Uni.createFrom() |
| 51 | + .item(this::runPowermetrics) |
| 52 | + .runSubscriptionOn(Infrastructure.getDefaultWorkerPool()) |
| 53 | + .chain(powermetrics -> Uni.createFrom() |
| 54 | + .item(this::runCommandToMeasure) |
| 55 | + .onTermination() |
| 56 | + .invoke(powermetrics::destroy)) |
| 57 | + .await() |
| 58 | + .indefinitely(); |
| 59 | + |
| 60 | + final int exit; |
| 61 | + try { |
| 62 | + Log.infof("powermetrics exit code: %d", powermetrics.waitFor()); |
| 63 | + } catch (InterruptedException e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
| 66 | + extractPowerConsumption(cmdPid); |
| 67 | + |
| 68 | + Quarkus.waitForExit(); |
| 69 | + } |
| 70 | + |
| 71 | + private Measure extractPowerConsumption(long pid) { |
| 72 | + sensor.stop(); |
| 73 | + // first read metadata |
| 74 | + final var metadata = sensor.metadata(); |
| 75 | + Log.infof("Metadata:\n%s", metadata); |
| 76 | + // register pid |
| 77 | + final var registeredPID = sensor.register(pid); |
| 78 | + // re-open output file to read process info |
| 79 | + final var measure = sensor.update(0L); |
| 80 | + final var power = new TotalSyntheticComponent(metadata, SensorUnit.W, 0, 1, 2) |
| 81 | + .asMeasure(measure.getOrDefault(registeredPID).components()); |
| 82 | + |
| 83 | + Log.info("Measured power: " + power); |
| 84 | + Quarkus.asyncExit(); |
| 85 | + |
| 86 | + return power; |
| 87 | + } |
| 88 | + |
| 89 | + private Process runPowermetrics() { |
| 90 | + Log.info("Starting powermetrics"); |
| 91 | + try { |
| 92 | + powermetrics = new ProcessBuilder() |
| 93 | + .command("powermetrics", "--samplers", |
| 94 | + "cpu_power,tasks", |
| 95 | + "--show-process-samp-norm", |
| 96 | + "--show-process-gpu", |
| 97 | + "--show-usage-summary", |
| 98 | + "-i", "0") // no sampling frequency, just record aggregate |
| 99 | + .redirectOutput(output) |
| 100 | + .start(); |
| 101 | + return powermetrics; |
| 102 | + } catch (IOException e) { |
| 103 | + throw new RuntimeException(e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private long runCommandToMeasure() { |
| 108 | + Log.infof("Recording energy consumption for: %s", cmd); |
26 | 109 | final var command = new ProcessBuilder().command(cmd); |
27 | | - final var start = System.nanoTime(); |
28 | 110 | try { |
29 | 111 | final var process = command.start(); |
30 | | - var duration = System.nanoTime() - start; |
31 | | - System.out.println("start duration: " + duration); |
32 | | - final var processTracker = measurer.startTrackingProcess(process); |
33 | | - process.onExit().thenAccept(unused -> processTracker.cancel()); |
| 112 | + process.waitFor(); |
| 113 | + return process.pid(); |
34 | 114 | } catch (Exception e) { |
35 | 115 | throw new RuntimeException(e); |
36 | 116 | } |
|
0 commit comments