-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract scheduling to new PowerMeasurer to simplify sensors
Also separate API from SPI classes.
- Loading branch information
Showing
19 changed files
with
298 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
runtime/src/main/java/io/quarkiverse/power/runtime/PowerMeasure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.quarkiverse.power.runtime; | ||
|
||
public interface PowerMeasure<M extends SensorMeasure> extends SensorMeasure { | ||
int numberOfSamples(); | ||
|
||
long duration(); | ||
|
||
M sensorMeasure(); | ||
} |
77 changes: 77 additions & 0 deletions
77
runtime/src/main/java/io/quarkiverse/power/runtime/PowerMeasurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.quarkiverse.power.runtime; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.sun.management.OperatingSystemMXBean; | ||
|
||
import io.quarkiverse.power.runtime.sensors.IncrementableMeasure; | ||
import io.quarkiverse.power.runtime.sensors.OngoingPowerMeasure; | ||
import io.quarkiverse.power.runtime.sensors.PowerSensor; | ||
import io.quarkiverse.power.runtime.sensors.PowerSensorProducer; | ||
|
||
public class PowerMeasurer<M extends IncrementableMeasure> { | ||
public static final OperatingSystemMXBean osBean; | ||
|
||
static { | ||
osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); | ||
} | ||
|
||
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); | ||
private ScheduledFuture<?> scheduled; | ||
private final PowerSensor<M> sensor; | ||
private OngoingPowerMeasure<M> measure; | ||
|
||
private final static PowerMeasurer<? extends SensorMeasure> instance = new PowerMeasurer<>( | ||
PowerSensorProducer.determinePowerSensor()); | ||
|
||
public static PowerMeasurer<? extends SensorMeasure> instance() { | ||
return instance; | ||
} | ||
|
||
public PowerMeasurer(PowerSensor<M> sensor) { | ||
this.sensor = sensor; | ||
} | ||
|
||
public void start(long duration, long frequency, PowerSensor.Writer out) throws Exception { | ||
if (measure == null) { | ||
measure = sensor.start(duration, frequency, out); | ||
|
||
if (duration > 0) { | ||
executor.schedule(() -> stop(out), duration, TimeUnit.SECONDS); | ||
} | ||
|
||
scheduled = executor.scheduleAtFixedRate(() -> update(out), | ||
0, frequency, | ||
TimeUnit.MILLISECONDS); | ||
} | ||
} | ||
|
||
private void update(PowerSensor.Writer out) { | ||
sensor.update(measure, out); | ||
measure.incrementSamples(); | ||
} | ||
|
||
public PowerMeasure<M> stop(PowerSensor.Writer out) { | ||
if (measure != null) { | ||
sensor.stop(); | ||
scheduled.cancel(true); | ||
} | ||
outputConsumptionSinceStarted(out); | ||
return measure; | ||
} | ||
|
||
public PowerMeasure<M> current() { | ||
return measure; | ||
} | ||
|
||
private void outputConsumptionSinceStarted(PowerSensor.Writer out) { | ||
out = out == null ? System.out::println : out; | ||
out.println("Consumed " + measure.total() + " mW over " + (measure.duration() / 1000) | ||
+ " seconds (" + measure.numberOfSamples() + " samples)"); | ||
sensor.additionalInfo(out); | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
runtime/src/main/java/io/quarkiverse/power/runtime/PowerSensor.java
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
runtime/src/main/java/io/quarkiverse/power/runtime/SensorMeasure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.quarkiverse.power.runtime; | ||
|
||
import java.util.Optional; | ||
|
||
public interface SensorMeasure { | ||
String CPU = "cpu"; | ||
String GPU = "gpu"; | ||
String TOTAL = "total"; | ||
|
||
double cpu(); | ||
|
||
default Optional<Double> gpu() { | ||
return Optional.empty(); | ||
} | ||
|
||
default Optional<Double> byKey(String key) { | ||
return switch (key) { | ||
case CPU -> Optional.of(cpu()); | ||
case GPU -> gpu(); | ||
case TOTAL -> Optional.of(total()); | ||
default -> Optional.empty(); | ||
}; | ||
} | ||
|
||
default double total() { | ||
return cpu() + gpu().orElse(0.0); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
runtime/src/main/java/io/quarkiverse/power/runtime/sensors/IncrementableMeasure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.quarkiverse.power.runtime.sensors; | ||
|
||
import io.quarkiverse.power.runtime.SensorMeasure; | ||
|
||
public interface IncrementableMeasure extends SensorMeasure { | ||
|
||
void addCPU(double v); | ||
|
||
default void addGPU(double v) { | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
runtime/src/main/java/io/quarkiverse/power/runtime/sensors/OngoingPowerMeasure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.quarkiverse.power.runtime.sensors; | ||
|
||
import java.util.Optional; | ||
|
||
import io.quarkiverse.power.runtime.PowerMeasure; | ||
|
||
public class OngoingPowerMeasure<M extends IncrementableMeasure> | ||
implements IncrementableMeasure, PowerMeasure<M> { | ||
private final M measure; | ||
private final long startedAt; | ||
private int samplesNb; | ||
|
||
public OngoingPowerMeasure(M measure) { | ||
startedAt = System.currentTimeMillis(); | ||
samplesNb = 0; | ||
this.measure = measure; | ||
} | ||
|
||
public void incrementSamples() { | ||
samplesNb++; | ||
} | ||
|
||
@Override | ||
public double cpu() { | ||
return measure.cpu(); | ||
} | ||
|
||
@Override | ||
public Optional<Double> gpu() { | ||
return measure.gpu(); | ||
} | ||
|
||
@Override | ||
public Optional<Double> byKey(String key) { | ||
return measure.byKey(key); | ||
} | ||
|
||
@Override | ||
public double total() { | ||
return measure.total(); | ||
} | ||
|
||
public int numberOfSamples() { | ||
return samplesNb; | ||
} | ||
|
||
public long duration() { | ||
return System.currentTimeMillis() - startedAt; | ||
} | ||
|
||
@Override | ||
public void addCPU(double v) { | ||
measure.addCPU(v); | ||
} | ||
|
||
@Override | ||
public void addGPU(double v) { | ||
measure.addGPU(v); | ||
} | ||
|
||
public M sensorMeasure() { | ||
return measure; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
runtime/src/main/java/io/quarkiverse/power/runtime/sensors/PowerSensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.quarkiverse.power.runtime.sensors; | ||
|
||
public interface PowerSensor<T extends IncrementableMeasure> { | ||
|
||
OngoingPowerMeasure<T> start(long duration, long frequency, Writer out) throws Exception; | ||
|
||
default void stop() { | ||
} | ||
|
||
void update(OngoingPowerMeasure<T> ongoingMeasure, Writer out); | ||
|
||
default void additionalInfo(Writer out) { | ||
} | ||
|
||
interface Writer { | ||
void println(String message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.