From fa4ec5f74db7fbba9c0cd063d0128f5631fd343d Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Tue, 31 Oct 2023 21:49:07 +0100 Subject: [PATCH] feat: inject platform-appropriate sensor in commands --- README.md | 2 +- .../power/deployment/PowerProcessor.java | 4 +++- .../deployment/devui/commands/PowerCommands.java | 8 ++++++-- .../deployment/devui/commands/StartCommand.java | 9 +++++---- .../deployment/devui/commands/StopCommand.java | 8 +++++--- .../power/runtime/PowerSensorProducer.java | 15 +++++++++++++-- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5b25b9f..1e7027a 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ running `sudo chmod +r` on these files. Note that this change will only persist To use the extension: 1. Clone this repository locally -2. Build the code using `mvn install` +2. Build the code using `mvn install -DskipTests` (tests currently only work on macOS) 3. Add the extension to the application which energy consumption you wish to measure. Since the extension is not yet released, you will need to add it manually as a dependency to your application: ```xml diff --git a/deployment/src/main/java/io/quarkiverse/power/deployment/PowerProcessor.java b/deployment/src/main/java/io/quarkiverse/power/deployment/PowerProcessor.java index 4faccff..73f82ba 100644 --- a/deployment/src/main/java/io/quarkiverse/power/deployment/PowerProcessor.java +++ b/deployment/src/main/java/io/quarkiverse/power/deployment/PowerProcessor.java @@ -1,6 +1,7 @@ package io.quarkiverse.power.deployment; import io.quarkiverse.power.deployment.devui.commands.PowerCommands; +import io.quarkiverse.power.runtime.PowerSensorProducer; import io.quarkus.deployment.IsDevelopment; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; @@ -19,6 +20,7 @@ FeatureBuildItem feature() { @BuildStep(onlyIf = IsDevelopment.class) void addConsoleCommands(BuildProducer commands) { // register dev console commands - commands.produce(new ConsoleCommandBuildItem(new PowerCommands())); + final var producer = new PowerSensorProducer(); + commands.produce(new ConsoleCommandBuildItem(new PowerCommands(producer.sensor()))); } } diff --git a/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/PowerCommands.java b/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/PowerCommands.java index ab13eaf..97d4027 100644 --- a/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/PowerCommands.java +++ b/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/PowerCommands.java @@ -5,16 +5,20 @@ import org.aesh.command.*; import org.aesh.command.invocation.CommandInvocation; +import io.quarkiverse.power.runtime.PowerSensor; + @GroupCommandDefinition(name = "power", description = "Power consumption commands", generateHelp = true) @SuppressWarnings("rawtypes") public class PowerCommands implements GroupCommand { + private final PowerSensor sensor; - public PowerCommands() { + public PowerCommands(PowerSensor sensor) { + this.sensor = sensor; } @Override public List getCommands() { - return List.of(new StartCommand(), new StopCommand()); + return List.of(new StartCommand(sensor), new StopCommand(sensor)); } @Override diff --git a/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/StartCommand.java b/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/StartCommand.java index 1190b0f..ad22cae 100644 --- a/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/StartCommand.java +++ b/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/StartCommand.java @@ -7,15 +7,12 @@ import org.aesh.command.option.Option; import io.quarkiverse.power.runtime.PowerSensor; -import io.quarkiverse.power.runtime.sensors.linux.rapl.IntelRAPLSensor; import io.quarkus.deployment.console.QuarkusCommand; @CommandDefinition(name = "start", description = "Starts measuring power consumption of the current application") @SuppressWarnings("rawtypes") public class StartCommand extends QuarkusCommand { - - // @Inject - PowerSensor sensor = IntelRAPLSensor.instance; + private final PowerSensor sensor; @Option(name = "stopAfter", shortName = 's', description = "Automatically stop the measures after the specified duration in seconds", defaultValue = "-1") private long duration; @@ -23,6 +20,10 @@ public class StartCommand extends QuarkusCommand { @Option(name = "frequency", shortName = 'f', description = "The frequency at which measurements should be taken, in milliseconds", defaultValue = "1000") private long frequency; + public StartCommand(PowerSensor sensor) { + this.sensor = sensor; + } + @Override public CommandResult doExecute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { try { diff --git a/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/StopCommand.java b/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/StopCommand.java index f3bbc87..7c30525 100644 --- a/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/StopCommand.java +++ b/deployment/src/main/java/io/quarkiverse/power/deployment/devui/commands/StopCommand.java @@ -6,15 +6,17 @@ import org.aesh.command.invocation.CommandInvocation; import io.quarkiverse.power.runtime.PowerSensor; -import io.quarkiverse.power.runtime.sensors.linux.rapl.IntelRAPLSensor; import io.quarkus.deployment.console.QuarkusCommand; @CommandDefinition(name = "stop", description = "Stops power measurement and outputs accumulated power since measures were started") @SuppressWarnings("rawtypes") public class StopCommand extends QuarkusCommand { - // @Inject - PowerSensor sensor = IntelRAPLSensor.instance; + private final PowerSensor sensor; + + public StopCommand(PowerSensor sensor) { + this.sensor = sensor; + } @Override public CommandResult doExecute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { diff --git a/runtime/src/main/java/io/quarkiverse/power/runtime/PowerSensorProducer.java b/runtime/src/main/java/io/quarkiverse/power/runtime/PowerSensorProducer.java index 43098ce..02776df 100644 --- a/runtime/src/main/java/io/quarkiverse/power/runtime/PowerSensorProducer.java +++ b/runtime/src/main/java/io/quarkiverse/power/runtime/PowerSensorProducer.java @@ -3,12 +3,23 @@ import jakarta.enterprise.inject.Produces; import jakarta.inject.Singleton; +import io.quarkiverse.power.runtime.sensors.linux.rapl.IntelRAPLSensor; import io.quarkiverse.power.runtime.sensors.macos.powermetrics.MacOSPowermetricsSensor; @Singleton public class PowerSensorProducer { @Produces - public PowerSensor sensor() { - return new MacOSPowermetricsSensor(); + public PowerSensor sensor() { + final var originalOSName = System.getProperty("os.name"); + String osName = originalOSName.toLowerCase(); + + if (osName.contains("mac os x")) { + return MacOSPowermetricsSensor.instance; + } + + if (!osName.contains("linux")) { + throw new RuntimeException("Unsupported platform: " + originalOSName); + } + return IntelRAPLSensor.instance; } }