Skip to content

Commit

Permalink
feat: inject platform-appropriate sensor in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Nov 1, 2023
1 parent e6eb1ca commit fa4ec5f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +20,7 @@ FeatureBuildItem feature() {
@BuildStep(onlyIf = IsDevelopment.class)
void addConsoleCommands(BuildProducer<ConsoleCommandBuildItem> commands) {
// register dev console commands
commands.produce(new ConsoleCommandBuildItem(new PowerCommands()));
final var producer = new PowerSensorProducer();
commands.produce(new ConsoleCommandBuildItem(new PowerCommands(producer.sensor())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Command> getCommands() {
return List.of(new StartCommand(), new StopCommand());
return List.of(new StartCommand(sensor), new StopCommand(sensor));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
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;

@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit fa4ec5f

Please sign in to comment.