-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: RAPL sensor now properly use CPU share instead of full measure #13
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
124f067
fix: RAPL sensor now properly use CPU share instead of full measure
metacosm 2d5f6db
fix: tests now also pass on Linux (provided setup is correct)
metacosm 9ea5812
fix: properly compute power for RAPL
metacosm 3828989
refactor: simplify IntelRAPLMeasure
metacosm dbfdd3f
chore: remove forgotten output
metacosm a126e89
fix: wrong assertion
metacosm 12c1437
refactor: abstract RAPL file, revert to naive implementation
metacosm 264592c
chore: add test
metacosm 1694f93
fix: should now work okish
metacosm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
65 changes: 55 additions & 10 deletions
65
runtime/src/main/java/io/quarkiverse/power/runtime/sensors/linux/rapl/IntelRAPLMeasure.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 |
---|---|---|
@@ -1,27 +1,72 @@ | ||
package io.quarkiverse.power.runtime.sensors.linux.rapl; | ||
|
||
import io.quarkiverse.power.runtime.SensorMeasure; | ||
import io.quarkiverse.power.runtime.sensors.IncrementableMeasure; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class IntelRAPLMeasure implements IncrementableMeasure { | ||
private final long initial; | ||
private long cpu; | ||
private final long startedAt = System.currentTimeMillis(); | ||
private final Map<String, Accumulator> values = new HashMap<>(); | ||
|
||
static class Accumulator { | ||
private double previous; | ||
private double accumulated; | ||
|
||
public Accumulator recordNewValue(long currentValue, double durationSinceLastRecord, double share) { | ||
accumulateAttributedDeltaOverDuration(currentValue, durationSinceLastRecord, share); | ||
previous = currentValue; | ||
return this; | ||
} | ||
|
||
public IntelRAPLMeasure(long initial) { | ||
this.initial = initial; | ||
private void accumulateAttributedDeltaOverDuration(long currentValue, double durationSinceLastRecord, double share) { | ||
accumulated += (currentValue - previous) / durationSinceLastRecord * share; | ||
} | ||
|
||
public double accumulated() { | ||
return accumulated; | ||
} | ||
} | ||
|
||
double getValue(String name) { | ||
final var value = values.get(name); | ||
return value == null ? 0.0 : value.accumulated() / 1000; | ||
} | ||
|
||
void updateValue(String name, long current, double cpuShare, double frequency) { | ||
values.compute(name, (n, previous) -> Objects.requireNonNullElseGet(previous, Accumulator::new).recordNewValue(current, frequency, cpuShare)); | ||
} | ||
|
||
@Override | ||
public double cpu() { | ||
return ((double) cpu / durationSinceStart()) / 1_000; | ||
return getValue("package-0"); | ||
} | ||
|
||
@Override | ||
public void addCPU(double v) { | ||
cpu += ((long) v - initial); | ||
public Optional<Double> gpu() { | ||
return Optional.ofNullable(values.get(SensorMeasure.GPU)).map(value -> value.accumulated() / 1000); | ||
} | ||
|
||
private long durationSinceStart() { | ||
return System.currentTimeMillis() - startedAt; | ||
@Override | ||
public Optional<Double> byKey(String key) { | ||
final var v = IncrementableMeasure.super.byKey(key); // first get from default implementation | ||
if(v.isEmpty()) { | ||
// try local keys | ||
return Optional.ofNullable(values.get(key)).map(value -> value.accumulated() / 1000); | ||
} else { | ||
return v; | ||
} | ||
} | ||
|
||
@Override | ||
public double total() { | ||
return values.values().stream().map(Accumulator::accumulated).reduce(Double::sum).orElse(0.0) / 1000; | ||
} | ||
|
||
@Override | ||
public void addCPU(double v) { | ||
throw new IllegalStateException("Shouldn't be called"); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
...e/src/test/java/io/quarkiverse/power/runtime/sensors/linux/rapl/IntelRAPLMeasureTest.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,37 @@ | ||
package io.quarkiverse.power.runtime.sensors.linux.rapl; | ||
|
||
import io.quarkiverse.power.runtime.SensorMeasure; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.quarkiverse.power.runtime.SensorMeasure.GPU; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class IntelRAPLMeasureTest { | ||
|
||
private static final String CPU = "package-0"; | ||
|
||
|
||
@Test | ||
void updateValue() { | ||
final var measure = new IntelRAPLMeasure(); | ||
measure.updateValue(CPU, 10000, 0.5, 100); // add 50 to cpu | ||
measure.updateValue(GPU, 10000, 0.2, 100); // add 20 to gpu | ||
measure.updateValue(CPU, 20000, 0.4, 100); // add 40 to cpu | ||
measure.updateValue(CPU, 30000, 0.3, 100); // add 30 to cpu | ||
measure.updateValue(GPU, 30000, 0.4, 100); // add 80 to cpu | ||
|
||
final var cpu1 = (10000 * 0.5 / 100); | ||
final var cpu2 = (20000 - 10000) * 0.4 / 100; | ||
final var cpu3 = (30000 - 20000) * 0.3 / 100; | ||
final var gpu1 = 10000 * 0.2 / 100; | ||
final var gpu2 = (30000 - 10000) * 0.4 / 100; | ||
assertEquals((cpu1 + cpu2 + cpu3) / 1000, measure.cpu()); | ||
assertEquals((cpu1 + cpu2 + cpu3) / 1000, measure.getValue(CPU)); | ||
assertEquals((gpu1 + gpu2) / 1000, measure.getValue(GPU)); | ||
assertEquals(measure.cpu() + measure.getValue(GPU), measure.total()); | ||
assertEquals(measure.getValue(GPU), measure.gpu().orElseThrow()); | ||
assertEquals(measure.total(), measure.byKey(SensorMeasure.TOTAL).orElseThrow()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ByteBuffer here should be better pooled somewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My brain is fried, I guess, can't get it to work now, will get back to it on Monday.